\ 1802

needs case

needs 1802regs
needs 1802memory
needs 1802exceptions
needs 1802cycle
needs 1802io
needs 1802opcodes1
needs 1802opcodes2
needs 1802opcodes3
needs 1802optable
needs 1802reset

needs 1861display

decimal

\ event-delay is fed to (ekey) and 
\ is the time in 1/100th of a second
\ that we should wait for an event
\ before timing out. This should be
\ 0. for run mode because of the
\ background tasking, but may be -1
\ (infinite) for other cpu modes.
2variable event-delay

\ repeat-rate is how many times we
\ should execute do-state each time
\ through the event loop.
variable repeat-rate

\ vector to the state we'll execute
variable cpu-state

: do-instruction  ( -- )
    cycle 
    fetch-byte      ( opcode )
    cycle
    do-opcode ;

: wait-state  ( -- )   ;

variable wait-for-release
0 wait-for-release !
: load-state  ( -- ) 
    EF4   wait-for-release @ 0=  and if
        4 input@ 
        0 R @  byte!     \ store byte
        0 R @ byte@    \ read it back
        1  0 R +!               \ incr R0 for next
        \ Now stuff in output port 4 so
        \ that the LEDs pick up the value;
        \ if M/P is on (examine mode),
        \ this byte may not be what was
        \ just written.
        4 output!
        -1 wait-for-release !
    else
        EF4 0=   wait-for-release @ and if
            0 wait-for-release !
        then
    then ;

: reset-state  ( -- )
    reset-cpu
    ['] wait-state cpu-state !  ;

\ variable oldcount
\ variable cycle-count
: run-state ( -- )
    dma-out? if 
        do-dma-out reset-idle
    else
        interrupt? if
            do-interrupt reset-idle
        else
            not-idle? if
                do-instruction 
            else
                cycle
            then
        then
    then ;

: do-state  ( -- )
    cpu-state @ execute ;

\ mode 0 - reset (run=0, load=0)
\ mode 1 - load (run=0, load=1)
\ mode 2 - run (run=1, load=0)
\ mode 3 - pause (run=1, load=1)
variable mode
0 mode !      \ initially in reset mode

: update-cpu-state  ( -- )
    mode @ case
        0 of
            stop-1861
            -1. event-delay 2!
            1 repeat-rate !
            ['] reset-state cpu-state !
       endof
       1 of
            stop-1861
            -1. event-delay 2!
            1 repeat-rate !
            ['] load-state cpu-state !
        endof
        2 of
            0. event-delay 2!
            \ primes preferred for repeat
            47 repeat-rate ! 
            ['] run-state cpu-state !
        endof
        3 of
            -1. event-delay 2!
            1 repeat-rate !
            ['] wait-state cpu-state !
        endof
    endcase ;

\ these are called when the user flips
\ the state of a toggle switch
: set-run-switch  ( flag -- )
    mode @ swap
    if     2 or     else      1 and     then
    mode !
    update-cpu-state ;

: set-load-switch  ( flag -- )
    mode @ swap
    if    1 or     else      2 and    then
    mode !
    update-cpu-state ;

: run-switch?  ( -- flag )
    mode @   2 and ;

: load-switch?  ( -- flag )
    mode @   1 and ;

: init-emulator  ( -- )
    0 mode !
    update-cpu-state ;

