\ not a module, needs 'load' to define undefined symbols
\ -*- forth -*-


\ neeed to be provided by users
\ declare console-on
\ declare console-off
\ declare receive
\ declare transmit
\ declare hello


\ --- INTERPRETER ---

\ control xts
: jsr    push receive TOSL ! receive TOSH ! ;
: ack    0 transmit ;  \ zero length message


\ NOTE: console-on/off is disabled. fix this somewhere else.    
: ferase   flash erase   ack ;
: fprog    flash program ack ;


\ token 0 is nop, so a stream of zeros can be used as soft interrupt

\ Except for nop and reset, all tokens return at least one byte result
\ to synchronize on. The protocol has Remote Procedure Call (RPC)
\ semantics, but implements it without the need for simultaneous
\ buffering and handling.
    
\ token --
: interpret
    #x0F and route
     ack     ., receive/ack  ., 1/transmit ., jsr/ack .,
     lda     ., ldf          ., ack        ., reset
     n@a+    ., n@f+         ., n!a+       ., n!f+    .,    
     chkblk  ., ack          ., ferase     ., fprog   ;


: 1/transmit  1 transmit transmit ;
: receive/ack receive ack ;
: jsr/ack     jsr ack ;    
        
\ Bytecode interpreter main loop.  The zero length message is ignored,
\ all other messages are interpreted and assumed to be of correct
\ length.  ( This is debug: target doesn't need to second-guess the host. )

macro
: 0=  #xFF + drop nc? ;
forth
    
: interpreter
    receive 0= not if receive interpret then
    interpreter ;

\ Block transfer. These take the size from the command input to make
\ the host -> target protocol context-free, and send out message
\ length to do the same for target -> host protocol.

: receive-dup-transmit receive dup transmit ;
    
: n@f+ receive-dup-transmit for @f+ transmit next ;
: n@a+ receive-dup-transmit for @a+ transmit next ;
    
: n!f+ receive for receive !f+ next ack ;
: n!a+ receive for receive !a+ next ack ;

\ pointer initialization
: lda  receive receive a!! ack ;
: ldf  receive receive f!! ack ;
    
\ program block memory check
: chkblk 255 64 for @f+ and next 1 transmit transmit ;

