Cart-Forth reference

Cart-Forth is a small Forth subset compiled to spriteCart's stack-VM bytecode. Words are whitespace-separated; data flows through a single 16-bit signed stack.

Stack primitives

WordStack effectSummary
dup( a -- a a )Duplicate top of stack.
drop( a -- )Discard top of stack.
swap( a b -- b a )Swap top two cells.
over( a b -- a b a )Copy second cell to top.
rot( a b c -- b c a )Rotate top three cells.
nip( a b -- b )Discard second cell.
tuck( a b -- b a b )Copy top below second.
pick( xn ... x0 n -- xn ... x0 xn )Copy the nth cell from the top (0-indexed) to the top; 0 pick = dup, 1 pick = over.
2dup( a b -- a b a b )Duplicate the top cell pair (equivalent to `over over`).
+( a b -- a+b )Add.
-( a b -- a-b )Subtract.
*( a b -- a*b )Multiply.
/( a b -- a/b )Divide (signed; division by zero traps).
mod( a b -- a%b )Modulo.
/mod( a b -- a%b a/b )Push modulus then quotient (both signed).
negate( a -- -a )Two's complement negation.
abs( a -- |a| )Absolute value.
min( a b -- min )Minimum (signed).
max( a b -- max )Maximum (signed).
=( a b -- flag )Push -1 if equal, else 0.
<>( a b -- flag )Push -1 if not equal, else 0.
<( a b -- flag )Push -1 if a < b (signed), else 0.
>( a b -- flag )Push -1 if a > b (signed), else 0.
<=( a b -- flag )Push -1 if a ≤ b (signed), else 0.
>=( a b -- flag )Push -1 if a ≥ b (signed), else 0.
and( a b -- a&b )Bitwise AND.
or( a b -- a|b )Bitwise OR.
xor( a b -- a^b )Bitwise XOR.
invert( a -- ~a )Bitwise NOT.
lshift( a n -- a<<n )Logical left shift by n bits (shift count masked to 4 bits).
rshift( a n -- a>>n )Logical right shift by n bits (unsigned; shift count masked to 4 bits).
@( addr -- v )Fetch cell at addr.
!( v addr -- )Store v at addr.
c@( addr -- byte )Fetch byte at addr.
c!( byte addr -- )Store byte at addr.
+!( v addr -- )Add v to the cell at addr (fetch, add, store).

Defining words

WordStack effectSummary
:( -- )Begin a colon-definition; ends with `;`.
;( -- )End a colon-definition.
variable( -- )Define a 1-cell variable; the new word pushes its address.
constant( v -- )Define a constant; the new word pushes `v`.

Control flow

WordStack effectSummary
if( flag -- )Begin a conditional. Paired with `else` (optional) and `then`.
else( -- )Else branch of an `if`.
then( -- )End an `if` (no relation to other Forths' postfix-then).
begin( -- )Top of a `begin … until` loop.
until( flag -- )Loop back to `begin` while flag is 0; exit when non-zero.
do( limit start -- )Begin a counted loop body; `i` accesses the index.
loop( -- )End a `do` loop; increments index by 1.
i( -- i )Push the current `do` loop index.
exit( -- )Return early from the enclosing colon-definition.

Literals

WordStack effectSummary
s"( -- addr len )String literal: pushes (RAM address, byte length).
."( -- )Not yet implemented — use `s" ..." print` for now.

Host words

Host words compile to a single HCALL <id> instruction. The full HCALL wire reference (with hex ids) lives at /docs/hcalls.

WordStack effectSummary
cls( c -- )Clear the screen to color index `c`.
pixel( x y c -- )Set pixel at (x,y) to color index `c`.
sprite( tile x y flags -- )Draw sprite `tile` at (x,y). `flags` bits: 1=hflip, 2=vflip, 4=wrap. Color 0 in the tile is transparent.
rect( x y w h c -- )Draw filled w×h rectangle at (x,y) in color `c`.
line( x0 y0 x1 y1 c -- )Draw a line from (x0,y0) to (x1,y1) in color `c`.
print( x y c addr len -- )Render text from RAM at (x,y) in color `c` using the 4×6 font; `\n` advances to a new line.
camera( x y -- )Set the camera offset (subtracted from draw coords).
clip( x y w h -- )Set the clipping rectangle for subsequent draws.
note!( pitch instrument voice -- )Trigger MIDI `pitch` on `voice` using `instrument`'s ADSR.
mute!( voice -- )Silence `voice` immediately (no release ramp).
play-pattern( pattern -- )Start the host tracker engine on `pattern`. Loops until `stop-music`.
play-once( pattern -- )Like `play-pattern` but auto-stops on row wrap (one pass). Use for SFX + game-over jingles.
stop-music( -- )Stop the host tracker engine.
btn( id -- flag )Push -1 if button `id` is held this frame, else 0.
btnp( id -- flag )Push -1 if button `id` was pressed this frame (rising edge), else 0.