pic18/vm-stc16.ss
#lang planet zwizwa/staapl/pic18 \ -*- forth -*-

\ Stand-alone standard Forth compiler.

\ SEE vm-dtc.ss 

\ The compiler is written in low-level 8-bit Forth instead of as a
\ self-hosted standard Forth application.  This makes bootstrapping
\ more straightforward, as only the dictionary needs to be created.

\ The Forth is subroutine-threaded, to make interoperation with the
\ 8-bit Forth more straightforward.

staapl pic18/double-math
staapl pic18/double-comma
staapl pic18/conditionals

\ Words are compiled first to RAM.  For this a fixed size circular
\ buffer is reserved which uses the bottom bits of the "here" pointer,
\ which contains the FLASH address.

    

\ Code is subroutine threaded.  As a first approximation, only
\ relative addressing is used.  This allows for +- 2K byte jumps.
\ Later this should compile indirection jumps or 2-word instructions.
    
: _compile \ xt_lo xt_hi --
    _here _@ _- _>> _1-  \ relative word address
    #x07 and             \ clear opcode bits
    #xD8 or              \ add opcode bits
    _, ;

\ Literals use a dolit primitive to not make them too big.
: dolit
    xl @ fl !
    xh @ fh !
    @f+ @f+
    fl @ xl !
    fh @ xh ! ;
    
: _literal
    dolit
    ' dolit address 1 <<< ,,
    _, _, ;