Skip to content

kristianwiklund/rp2040-forth

Repository files navigation

rp2040-forth

A forth implementation for rp2040. Inspired by (obvious if one read the code) - but not a port of - jonesforth.

It will likely never be "complete" - the primary purpose is as a vehicle for me to learn arm (+ rp2040= assembler.

Bugs

Yes, and lots of them.

Notably, there is no error handling at all. This is by design (as in - I haven't gotten around to fixing it yet). Use an SWD adapter (picoprobe, jlink, ...) with gdb to avoid having to pull the plug all the time. You want one anyway, it makes life easier.

It also hangs when using USB for serial. No idea why. I use an UART adapter when developing and haven't prioritized a fix.

STM32F103RBT6 (STM32F103RB nucleo) "port"

In progress, which is the reason for the switch to platformio from the rp2040 SDK. Prints things, creates words, freezes :-)

Implemented words (not complete)

Word Description Example
." Print a string terminated with ". Includes final space before last " in output. ." banana!"
SPACE Print a single space
CR Print a single newline
CONSTANT Create a constant from the first value on the stack. Returns its value when called. 10 CONSTANT TEN TEN .
VARIABLE Create a variable. Returns its address when called VARIABLE ANSWER ANSWER 42 ! ANSWER @ .
BEGIN Start a BEGIN-UNTIl loop. Compile only : A 10 BEGIN DUP . 1 - DUP 0 < UNTIL ; A
UNTIL Finish a BEGIN-UNTIL loop. Compile only. Loops until TRUE on top of the stack See "BEGIN"
OVER Copies the second-from-the-top stack value to the top: w1 w2 – w1 w2 w1
0>=, 0>, 0<>, 0<=, 0<, <>, >=, <=, 0=, =, >, < Various comparisons. See the gforth manual
NOT Inverts (boolean) the top word on the stack 1 NOT .
MOD Modulo 5 3 MOD .
/ Division 6 2 / .
/MOD Div-Mod. a b -- a%b a/b 6 2 MOD .
- a b -- a-b 6 2 - .
+, *
INTERPRET The main loop, starts through the boot sequence and keeps restarting itself until the processor crashes
NOP No operation
EXECUTE Broken at the moment
FIND Find a word from a string pointer/length on the stack. pointer length -- pointer (Doesn't work) WORD WORDS FIND DROP TYPE
RESET Sometimes restarts the machine correctly.
ROT x1 x2 x3 -- x2 x3 x1
DROP Removes the topmost value from the stack
SWAP Swaps the two topmost stack values
DUP Copies the topmost stack value
WORDS List all words
BASE Variable controlling the output and input number base. Max radix is 36. 0 is rejected. 1 might give interesting results. 16 BASE ! 128 a base ! .
TYPE Prints the null-terminated string pointed to by the top of the stack
EMIT Print the character on the top of the stack 13 EMIT
. Print the number on the top of the stack 42 .
WORD Read a word from input. Put a pointer to the string on the top of the stack. Non-persistent
READ-LINE Reads a line from input and updates the buffer. Useless at the moment
FLUSHSTDIN Used by the inner loop to clean out cruft from the serial port
KEY Read one character from the current input buffer
S" Compile a string to the next free memory.
SKIPSTRING (Invisible) Helper word to use compiled strings within a compiled word without crashing
[ Leave compile mode
] Enter compile mode
IMMEDIATE Mark the last created word as immediate
SEE Decompile (sort of...) a word SEE A
A Demo word counting down from 10 to 0 A
IF - ELSE - THEN See [gforth docs}(http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Selection.html)
BRANCH Jump to the next execution address as specified in the word in the execution list directly after BRANCH
0BRANCH As branch, but only if the top word on the stack is zero
! Stores a value in a variable 16 BASE !
@ Fetches a value from a variable BASE @ .
HERE Returns the address to the first free word in the memory
LIT Indicates that the next word in the execution list is a literal LIT ' LIT
ALIGN Align the free memory pointer to instruction boundary. Typically good to do if it is in risk of misalignment
c, Compiles a byte to the end of the memory and increases the free memory pointer with 1
, Compiles a word (alignment-safe) to the end of the memory and increases the free memory pointer with 4
LATEST Return the pointer to the last defined word and puts it on top of the stack
; End of word declaration : A ." A " ;
: Start of word declaration : A ." A " ;
' Interpreter: search for the next word in the stream, put the pointer on the stack (or 0 if failed).
Compiler: compile the address of the next word in the stream. Used to compile a word instead of executing it in immediate mode
HIDDEN Hides the last created word from FIND
CREATE Creates a forth header in the word list
STATE Constant indicating if we are in interpreter mode (0) or compile mode (1)
DEBUG Variable to enable (1) / disable (0) debug printouts 1 DEBUG !
SP0 Constant: The initial value of the stack pointer
SP@ Constant: The current value of the stack pointer
DEPTH Constant: The depth of the stack before DEPTH is run
R> Move top of return stack to top of value stack
>R Move top of value stack to top of return stack