\ 1861display

needs bitmap
needs graphics
needs 1802exceptions
needs 1802io
needs skin-viewhooks

needs 1861paint
needs elfcolor

decimal

variable videodmastate
: next-line  ( -- )
    0 videodmastate ! ;
: wait-cycle  ( -- )   ;
: do-video-dma  ( -- )
    1 videodmastate +!
    videodmastate @  select
    xt wait-cycle    \ 0 - never happens
    xt raise-dma-out   \ 1
    xt wait-cycle            \ 2
    xt wait-cycle            \ 3
    xt wait-cycle            \ 4 
    xt wait-cycle            \ 5
    xt wait-cycle            \ 6
    xt wait-cycle            \ 7
    xt wait-cycle            \ 8
    xt lower-dma-out   \ 9
    xt wait-cycle            \ 10
    xt wait-cycle            \ 11
    xt wait-cycle            \ 12
    xt wait-cycle            \ 13
    xt next-line              \ 14
    end-select execute ;

variable (1861-cycle1)
variable cycle-count

: 1861-cycle6  ( -- )
    1 cycle-count +! 
    cycle-count @  3668 = if
        0 cycle-count !
        (1861-cycle1) @ (cycle) !
    then ;

: 1861-cycle5  ( -- )
    cycle-count @ dup  2910 < if
        do-video-dma
    then
    ( cycle-count )  2912 = if
        lower-EF1
        paint-if-changed
        ['] 1861-cycle6 (cycle) !
    then 
    1 cycle-count +! ;

: 1861-cycle4  ( -- )
    cycle-count @ dup 1122 > if
       do-video-dma
    then
    ( cycle-count )  2856 = if
        raise-EF1
        ['] 1861-cycle5 (cycle) !
    then
    1 cycle-count +! ;

: 1861-cycle3  ( -- )
    cycle-count @ 1120 = if
        lower-interrupt
        lower-EF1
        ['] 1861-cycle4 (cycle) !
    then
    1 cycle-count +! ;

: 1861-cycle2  ( -- )
    cycle-count @ 1092 = if
        raise-interrupt
        ['] 1861-cycle3 (cycle) !
    then
    1 cycle-count +! ;

: 1861-cycle1  ( -- )
    cycle-count @ 1064 = if
        0 woffset !
        0 videodmastate !
        raise-EF1
        ['] 1861-cycle2 (cycle) !
    then 
    1 cycle-count +! ;

: (paint-crt-border)  ( -- )
    132 132 2 2 5 rounded-rectangle ;

: paint-crt-border  ( -- ) 
    supports-8bit? if   dark-gray
    else   0.
    then
    foreground
    (paint-crt-border)
    0. foreground ;

: 1861-running?  ( -- flag )
    (cycle) @ ['] noop <> ;

\ To start the 1861 running, we'll
\ poke into the (cycle) vector in
\ 1802cycle. When the cycle word is
\ invoked, it'll end up here for the 
\ video routines to track. 
: start-1861  ( -- ) 
    1861-running? if exit then
    ['] 1861-cycle1 (1861-cycle1) !
   true difference !
    ['] 1861-cycle1  (cycle) ! 
    ['] 1861-dma  (dma-out) !
    0 woffset !
    0 cycle-count !
    (set-videoview) @ execute
    paint-crt-border ;

: stop-1861  ( -- )
    \ stop 1861 if it's running
    1861-running? if
        ['] noop (cycle) !
        ['] drop (dma-out) !
        (set-normalview) @ execute
    then  ;

: force-video-repaint  ( -- )
    1861-running? if
        paint-crt-border
        paint-frame
    then ;

\ init forward reference when app
\ starts
: init-1861  ( -- )
    ['] start-1861 (start-1861) !
    ['] noop (cycle) !
    ['] drop (dma-out) ! ;

