            org     $6000
start       lda     #%01001100      ; $ff90 bits: coco mmu mc3 mc2
            sta     INIT0
initdisp    clra
            ldx     #screens
            ldb     resolution
            lslb
            leax    [b,x]
            tfr     x,y             ; x & y point to screen structure
            ldb     SCR_WIDTH,x
            ldx     #str_width
            lbsr    IntToStr
            lda     SCR_OR_FLG,y    ; initialize starting $ff99 value and save it
            pshs    a
            lda     lpr             ; initialize $ff98 with lines per ro, right most
            sta     $ff98
            sta     copyff98
            ldx     #lprs           ; get a displayable lpr values
            leax    a,x
            clra
            ldb     ,x
            ldx     #str_lpr
            lbsr    IntToStr
            * FF99
            tfr     y,x             ; x & y now point to screen data structure
            puls    a               ; restore the starting value for $ff99
            ora     #%00000001      ; $ff99 attributes enabled
            pshs    a,x
            clra
            ldb     rows
            ldx     #rowsvals
            leax    b,x
            ldb     ,x
            ldx     #str_rows
            lbsr    IntToStr
            puls    x,a
            lslb
            lslb
            lslb
            lslb
            lslb
            andb    #%01100000
            pshs    b               ; TODO weird
            ora     ,s
            puls    b
            sta     $ff99
            sta     copyff99
            * configure border at $ff9a
            pshs    x               ; restore just before setmmu
            clra
            ldb     colborder
            ldx     #str_border
            lbsr    IntToStr
            stb     $ff9a
            stb     copyff9a
            * initialize the remaining registers we don't care about
            clra
            sta     $ff9b
            sta     $ff9c
            sta     $ff9e
            sta     $ff9f
            lda     #%11011000
            sta     $ff9d
            * initialize palette 0 at $ffb0 and 8 at $ffb8 which are the bg and fg respectively
            clra
            ldb     colpal0
            ldx     #str_pal0
            lbsr    IntToStr
            stb     $ffb0
            stb     copyffb0
            clra
            ldb     colpal8
            ldx     #str_pal8
            lbsr    IntToStr
            stb     $ffb8
            stb     copyffb8
            clra
            ldb     colpal9
            ldx     #str_pal9
            lbsr    IntToStr
            stb     $ffb9
            stb     copyffb9
            puls    x
setmmu      lda     #$36            ; page $36
            orcc    #$50            ; disable interrupts
            sta     $ffa1
            andcc   #$af            ; re-enable interrupts
dispscrn    ldy     #$2000          ; y is screen begining, x is screen structure
            ldd     SCR_END,x
            pshs    x,d
            lda     SCR_WIDTH,x
            sta     width
            ldb     chattr
            leax    SCR_CONTENT,x
loopds      lda     ,x+
            std     ,y++
            cmpy    ,s
            bne     loopds
            puls    d,x
            lda     chattr2
            ldb     width
            ldy     SCR_FA_B,x
loopds2     sta     ,y
            leay    b,y
            leay    b,y
            cmpy    SCR_FA_E,x
            bne     loopds2
            * display labels
            ldy     SCR_LWIDTH,x
            pshs    x
            ldx     #str_width
            lbsr    dispstr
            puls    x
            ldy     SCR_LPR,x
            pshs    x
            ldx     #str_lpr
            lbsr    dispstr
            puls    x
            ldy     SCR_ROWS,x
            pshs    x
            ldx     #str_rows
            lbsr    dispstr
            puls    x
            ldy     SCR_BORDER,x
            pshs    x
            ldx     #str_border
            lbsr    dispstr
            puls    x
            ldy     SCR_PAL0,x
            pshs    x
            ldx     #str_pal0
            lbsr    dispstr
            puls    x
            ldy     SCR_PAL8,x
            pshs    x
            ldx     #str_pal8
            lbsr    dispstr
            puls    x
            ldy     SCR_PAL9,x
            pshs    x
            ldx     #str_pal9
            lbsr    dispstr
            puls    x

            ldy     SCR_FF98,x
            lda     copyff98
            lbsr    dispbyte
            ldy     SCR_FF99,x
            lda     copyff99
            lbsr    dispbyte
            ldy     SCR_FF9A,x
            lda     copyff9a
            lbsr    dispbyte
            ldy     SCR_FFB0,x
            lda     copyffb0
            lbsr    dispbyte
            ldy     SCR_FFB8,x
            lda     copyffb8
            lbsr    dispbyte
            ldy     SCR_FFB9,x
            lda     copyffb9
            lbsr    dispbyte
            tfr     x,y
            clra

pollkb      jsr     [POLCAT]        ; polls keyboard
            beq     pollkb          ; no key pressed
            cmpa    #'W'            ; W key?
            bne     tstpressB
            ldb     resolution
            incb
            andb    #%00000011
            stb     resolution
            jmp     initdisp
tstpressB   cmpa    #'B'            ; B key?
            bne     tstpress0       ; if not, test next
            ldb     colborder
            incb
            andb    #%00111111
            stb     colborder
            jmp     initdisp
tstpress0   cmpa    #'0'            ; 0 key?
            bne     tstpress8       ; if not, test next
            ldb     colpal0
            incb
            andb    #%00111111
            stb     colpal0
            jmp     initdisp
tstpress8   cmpa    #'8'            ; 8 key?
            bne     tstpress9       ; if not, test next
            ldb     colpal8
            incb
            andb    #%00111111
            stb     colpal8
            jmp     initdisp
tstpress9   cmpa    #'9'            ; 9 key?
            bne     tstpressK       ; if not, test next
            ldb     colpal9
            incb
            andb    #%00111111
            stb     colpal9
            jmp     initdisp
tstpressK   cmpa    #'K'             ; K key?
            bne     tstpressU        ; if not, test next
            ldb     chattr
            eorb    #%10000000
            stb     chattr
            bsr     refattr
            jmp     initdisp
tstpressU   cmpa    #'U'            ; U key?
            bne     tstpressL       ; if not, test next
            ldb     chattr
            eorb    #%01000000
            stb     chattr
            bsr     refattr
            jmp     initdisp
tstpressL   cmpa    #'L'            ; L key?
            bne     tstpressR       ; if not, test next
            ldb     lpr
            incb
            cmpb    #7
            bne     stlpr
            ldb     #0
stlpr       stb     lpr
            jmp     initdisp
tstpressR   cmpa    #'R'            ; R key?
            bne     end             ; if not, test next
            ldb     rows
            incb
            andb    #%00000011
            stb     rows
            jmp     initdisp
end         rts

refattr     ldy     #$2000          ; ys is screen begining, x is screen structure
            ldb     chattr
loopra      lda     ,y+
            cmpa    #' '
            beq     loopraskip
            stb     ,y+
            jmp     loopraend
loopraskip  lda     ,y+
loopraend   cmpy    SCR_END,x
            bne     loopra
            rts
* display byte. byte to display in a, video location in y
dispbyte    pshs    a,b,x,y
            tfr     y,x
            leax    16,x
            pshs    x
dispbyte1   tfr     a,b
            andb    #%10000000
            bne     disp1
            ldb     #'0'
            jmp     putdigit
disp1       ldb     #'1'
putdigit    stb     ,y++
            lsla
            cmpy    ,s
            blt     dispbyte1
            puls    x
            puls    y,x,b,a
            rts

dispstr     pshs    a,b,x,y
            leax    2,x
            ldb     #3
dispsloop   lda     ,x+
            sta     ,y++
            decb
            bne     dispsloop
            puls    y,x,b,a
            rts

* copy of the gime registers because the originals can't be read
* so we set these when we write in the real ones, so we can display their content
copyff98    fcb     0
copyff99    fcb     0
copyff9a    fcb     0
copyffb0    fcb     0
copyffb8    fcb     0
copyffb9    fcb     0
*
resolution  fcb     0
width       fcb     0
str_width   rmb     6
lpr         fcb     3               ; 0 to 6
str_lpr     rmb     6
rows        fcb     0               ; 0 to 3
str_rows    rmb     6
colborder   fcb     0
str_border  rmb     6
colpal0     fcb     18
str_pal0    rmb     6
colpal8     fcb     0
str_pal8    rmb     6
colpal9     fcb     40
str_pal9    rmb     6
chattr      fcb     0
chattr2     fcb     %00001000
* ROM subroutines
INIT0       equ     $ff90
POLCAT	    equ 	$a000	        ; read keyboard ROM routine
* screen structure:
SCR_WIDTH   equ     0
SCR_OR_FLG  equ     1
SCR_END     equ     2
SCR_LWIDTH  equ     4
SCR_LPR     equ     6
SCR_ROWS    equ     8
SCR_BORDER  equ     10
SCR_PAL0    equ     12
SCR_PAL8    equ     14
SCR_PAL9    equ     16
SCR_FF98    equ     18
SCR_FF99    equ     20
SCR_FF9A    equ     22
SCR_FFB0    equ     24
SCR_FFB8    equ     26
SCR_FFB9    equ     28
SCR_FA_B    equ     30
SCR_FA_E    equ     32
SCR_CONTENT equ     34
* width 32 constants
screen32    fcb     32
            fcb     %00001000
            fdb     $2000+(25*32*2)         ; screen end address
            fdb     $2000+(5*32*2)+(26*2)   ; width
            fdb     $2000+(6*32*2)+(26*2)   ; lines per ch row
            fdb     $2000+(7*32*2)+(26*2)   ; rows
            fdb     $2000+(8*32*2)+(26*2)   ; border
            fdb     $2000+(9*32*2)+(26*2)   ; pal 0
            fdb     $2000+(10*32*2)+(26*2)  ; pal 8
            fdb     $2000+(11*32*2)+(26*2)  ; pal 9
            fdb     $2000+(16*32*2)+(13*2)  ; ff98
            fdb     $2000+(17*32*2)+(13*2)  ; ff99
            fdb     $2000+(18*32*2)+(13*2)  ; ff9a
            fdb     $2000+(19*32*2)+(13*2)  ; ffb0
            fdb     $2000+(20*32*2)+(13*2)  ; ffb8
            fdb     $2000+(21*32*2)+(13*2)  ; ffb9
            fdb     $2001+(5*32*2)+(7*2)    ; first attr1
            fdb     $2001+(14*32*2)+(7*2)   ; end attr1
            fcc     /12345678901234567890123456789012/
            fcc     /2        1         2         3  /
            fcc     /3                               /
            fcc     /4                               /
            fcc     /5                               /
            fcc     /6     [W] Width:                /
            fcc     /7     [L] Lines per row:        /
            fcc     /8     [R] Rows displayed:       /
            fcc     /9     [B] Border color:         /
            fcc     /10    [0] Palette 0 color:      /
            fcc     /11    [8] Palette 8 color:      /
            fcc     /12    [9] Palette 9 color:      /
            fcc     /13    [K] Toggle blinking       /
            fcc     /14    [U] Toggle underline      /
            fcc     /15                              /
            fcc     /16                              /
            fcc     /17    $FF98:                    /
            fcc     /18    $FF99:                    /
            fcc     /19    $FF9A:                    /
            fcc     /20    $FFB0:                    /
            fcc     /21    $FFB8:                    /
            fcc     /22    $FFB9:                    /
            fcc     /23                              /
            fcc     /24                              /
            fcc     /25                              /
* width 40 constants
screen40    fcb     40
            fcb     %00001100
            fdb     $2000+(25*40*2)         ; screen end address
            fdb     $2000+(5*40*2)+(30*2)   ; width
            fdb     $2000+(6*40*2)+(30*2)   ; lines per ch row
            fdb     $2000+(7*40*2)+(30*2)   ; rows
            fdb     $2000+(8*40*2)+(30*2)   ; border
            fdb     $2000+(9*40*2)+(30*2)   ; pal 0
            fdb     $2000+(10*40*2)+(30*2)  ; pal 8
            fdb     $2000+(11*40*2)+(30*2)  ; pal 9
            fdb     $2000+(16*40*2)+(17*2)  ; ff98
            fdb     $2000+(17*40*2)+(17*2)  ; ff99
            fdb     $2000+(18*40*2)+(17*2)  ; ff9a
            fdb     $2000+(19*40*2)+(17*2)  ; ffb0
            fdb     $2000+(20*40*2)+(17*2)  ; ffb8
            fdb     $2000+(21*40*2)+(17*2)  ; ffb9
            fdb     $2001+(5*40*2)+(11*2)   ; first attr1
            fdb     $2001+(14*40*2)+(11*2)  ; end attr1
            fcc     /1234567890123456789012345678901234567890/
            fcc     /2        1         2         3         4/
            fcc     /3                                       /
            fcc     /4                                       /
            fcc     /5                                       /
            fcc     /6         [W] Width:                    /
            fcc     /7         [L] Lines per row:            /
            fcc     /8         [R] Rows displayed:           /
            fcc     /9         [B] Border color:             /
            fcc     /10        [0] Palette 0 color:          /
            fcc     /11        [8] Palette 8 color:          /
            fcc     /12        [9] Palette 9 color:          /
            fcc     /13        [K] Toggle blinking           /
            fcc     /14        [U] Toggle underline          /
            fcc     /15                                      /
            fcc     /16                                      /
            fcc     /17        $FF98:                        /
            fcc     /18        $FF99:                        /
            fcc     /19        $FF9A:                        /
            fcc     /20        $FFB0:                        /
            fcc     /21        $FFB8:                        /
            fcc     /22        $FFB9:                        /
            fcc     /23                                      /
            fcc     /24                                      /
            fcc     /25                                      /
* width 64 constants
screen64    fcb     64
            fcb     %00010000
            fdb     $2c80                   ; screen end address
            fdb     $2000+(5*64*2)+(42*2)   ; width
            fdb     $2000+(6*64*2)+(42*2)   ; lines per ch row
            fdb     $2000+(7*64*2)+(42*2)   ; rows
            fdb     $2000+(8*64*2)+(42*2)   ; border
            fdb     $2000+(9*64*2)+(42*2)   ; pal 0
            fdb     $2000+(10*64*2)+(42*2)  ; pal 8
            fdb     $2000+(11*64*2)+(42*2)  ; pal 8
            fdb     $2000+(16*64*2)+(29*2)  ; ff98
            fdb     $2000+(17*64*2)+(29*2)  ; ff99
            fdb     $2000+(18*64*2)+(29*2)  ; ff9a
            fdb     $2000+(19*64*2)+(29*2)  ; ffb0
            fdb     $2000+(20*64*2)+(29*2)  ; ffb8
            fdb     $2000+(21*64*2)+(29*2)  ; ffb9
            fdb     $2001+(5*64*2)+(23*2)   ; first attr1
            fdb     $2001+(14*64*2)+(23*2)  ; end attr1
            fcc     /1234567890123456789012345678901234567890123456789012345678901234/
            fcc     /2        1         2         3         4         5         6    /
            fcc     /3                                                               /
            fcc     /4                                                               /
            fcc     /5                                                               /
            fcc     /6                     [W] Width:                                /
            fcc     /7                     [L] Lines per row:                        /
            fcc     /8                     [R] Rows displayed:                       /
            fcc     /9                     [B] Border color:                         /
            fcc     /10                    [0] Palette 0 color:                      /
            fcc     /11                    [8] Palette 8 color:                      /
            fcc     /12                    [9] Palette 9 color:                      /
            fcc     /13                    [K] Toggle blinking                       /
            fcc     /14                    [U] Toggle underline                      /
            fcc     /15                                                              /
            fcc     /16                                                              /
            fcc     /17                    $FF98:                                    /
            fcc     /18                    $FF99:                                    /
            fcc     /19                    $FF9A:                                    /
            fcc     /20                    $FFB0:                                    /
            fcc     /21                    $FFB8:                                    /
            fcc     /22                    $FFB9:                                    /
            fcc     /23                                                              /
            fcc     /24                                                              /
            fcc     /25                                                              /
* width 80 constants
screen80    fcb     80
            fcb     %00010100
            fdb     $2fa0                   ; screen end address
            fdb     $2000+(5*80*2)+(50*2)   ; width
            fdb     $2000+(6*80*2)+(50*2)   ; lines per ch row
            fdb     $2000+(7*80*2)+(50*2)   ; rows
            fdb     $2000+(8*80*2)+(50*2)   ; border
            fdb     $2000+(9*80*2)+(50*2)   ; pal 0
            fdb     $2000+(10*80*2)+(50*2)  ; pal 8
            fdb     $2000+(11*80*2)+(50*2)  ; pal 9
            fdb     $2000+(16*80*2)+(37*2)  ; ff98
            fdb     $2000+(17*80*2)+(37*2)  ; ff99
            fdb     $2000+(18*80*2)+(37*2)  ; ff9a
            fdb     $2000+(19*80*2)+(37*2)  ; ffb0
            fdb     $2000+(20*80*2)+(37*2)  ; ffb8
            fdb     $2000+(21*80*2)+(37*2)  ; ffb9
            fdb     $2001+(5*80*2)+(31*2)   ; first attr1
            fdb     $2001+(14*80*2)+(31*2)  ; end attr1
            fcc     /12345678901234567890123456789012345678901234567890123456789012345678901234567890/
            fcc     /2        1         2         3         4         5         6         7         8/
            fcc     /3                                                                               /
            fcc     /4                                                                               /
            fcc     /5                                                                               /
            fcc     /6                             [W] Resolution:                                   /
            fcc     /7                             [L] Lines per row:                                /
            fcc     /8                             [R] Rows displayed:                               /
            fcc     /9                             [B] Border color:                                 /
            fcc     /10                            [0] Palette 0 color:                              /
            fcc     /11                            [8] Palette 8 color:                              /
            fcc     /12                            [9] Palette 9 color:                              /
            fcc     /13                            [K] Toggle blinking                               /
            fcc     /14                            [U] Toggle underline                              /
            fcc     /15                                                                              /
            fcc     /16                                                                              /
            fcc     /17                            $FF98:                                            /
            fcc     /18                            $FF99:                                            /
            fcc     /19                            $FF9A:                                            /
            fcc     /20                            $FFB0:                                            /
            fcc     /21                            $FFB8:                                            /
            fcc     /22                            $FFB9:                                            /
            fcc     /23                                                                              /
            fcc     /24                                                                              /
            fcc     /25                                                                              /
screens     fdb     screen32
            fdb     screen40
            fdb     screen64
            fdb     screen80
lprs        fcb     1
            fcb     2
            fcb     3
            fcb     8
            fcb     9
            fcb     10
            fcb     12
rowsvals    fcb     192
            fcb     200
            fcb     210
            fcb     225
*======================================================================================================================
* IntToStr: convert an unsigned integer to a string.
*
* D (r):        Unsigned integer value
* X (r):        buffer pointer (should be at least 6 bytes)
*----------------------------------------------------------------------------------------------------------------------
IntToStr    PSHU    X,Y,A,B,CC
            cmpd    #0
            bne     intostr2
            lda     #' '
            ldb     #4
itosloop    sta     ,x+
            decb
            bne     itosloop
            lda     #'0'
            sta     ,x+
            jmp     gssEnd
intostr2    BSR	    ITOA003
            LDX     3,U
            LDB     #' '
trimZeros   LDA     ,X
            CMPA    #'0'
            BNE     gssEnd
            STB     ,X+
            JMP     trimZeros
gssEnd      PULU    X,Y,A,B,CC
            RTS
ITOA003	    LDY     #10000
            BSR	    ITOA000
            LDY     #1000
            BSR	    ITOA000
ITOA004     LDY	    #100
            BSR	    ITOA000
            LDY	    #10
            BSR	    ITOA000
            LDY	    #1
ITOA000	    STD	    NUMBER
            STY	    DIGIT
            LDA	    #'0'
            STA	    ,X
ITOA001     LDD	    NUMBER
            SUBD    DIGIT
            BCS	    ITOA002
            STD	    NUMBER
            LDA	    ,X
            INCA
            STA	    ,X
            JMP	    ITOA001
ITOA002     CLRA
            LEAX    1,X
            STA     ,X
            LDD     NUMBER
            RTS
NUMBER      FDB	0
DIGIT       FDB	0

            end     start
