A small, native Forth for microcontrollers, with a VS Code extension for live, interactive development and debugging.
Made for learning and experimenting: see the stack after every line, the machine code each word compiles to, and every register by its datasheet name. A way into Forth, ARM assembly and how a microcontroller really works.
Beta: RP2350, on the Raspberry Pi Pico 2 and Pico 2 W.
- A Forth kernel in under 8 KB of flash.
- ANS / Forth 2012 CORE compliant: see Forth 2012 CORE tests and John Hayes' 1995 suite for ANS Forth in test/core.fr.
- Native-code compiler: transforms word definitions to machine code on the chip.
- Subroutine threading (STC) with inlining: each word in a definition is compiled as a call (BL), but small primitives like
DUPand+are copied in directly. - Dictionary: new word definitions are held in a 64 KB dictionary in RAM.
SAVEstores it in flash. - Assembler module: write CODE definitions in native assembly blocks (between
CODEandEND-CODE). - Register names included: generated from the chip's SVD file, so every register is there by its datasheet name (
UART0.UARTDR,IO_BANK0), with nothing to load and no dictionary space used. - Interrupts in Forth:
' TICK 0 IRQ!and the handler runs, while the console stays free. A fault in your code prints an error and returns to the prompt. - Drivers in Forth: for GPIO, timers, ADC, PWM, UART, SPI, I2C, the watchdog and more.
- A VS Code extension: a debugger over SWD (breakpoints on words, stepping word by word with the stack beside each word, registers in the Variables view), a PC-sampling profiler, the target's stack after every line, stack effects computed from stack comments while editing, live values beside the source, hot reload, an Overview of words, memory and pins, Thumb-2 disassembly of any word, a memory view, a scope, notebooks.
You will need:
- VS Code
- a Raspberry Pi Pico 2 or Pico 2 W
- a Raspberry Pi Debug Probe
What to do:
- Install littleFORTH from VS Code's Extensions view. Run Forth: Use the Host Build and type Forth straight away: the kernel also runs inside VS Code.
- With a Pico 2 or Pico 2 W and a Raspberry Pi Debug Probe: wire the probe, flash from the extension, and blink an LED. Getting started takes you through it.
- The console is on UART0 (GP0/GP1), through the probe. A USB-serial adapter on GP0/GP1 gives the console only: no debugger, profiler or SWD flashing.
- Getting started
- The extension
- Words
- Drivers
- How it works
- Inside the kernel
- Building from source
- Changes
| Folder | What is in it |
|---|---|
| src/ | The kernel: interpreter, compiler, code generator, words |
| board/ | One file per board: startup, clocks, console, linker script |
| lib/ | Forth libraries: RP2350 drivers, the Thumb-2 assembler, SAVE |
| examples/ | Example programs for the Pico 2 |
| extension/ | The VS Code extension |
| test/ | Kernel tests, including the Hayes CORE suite |
| tools/ | Build, test and code-generation scripts |
| docs/ | Documentation |
Where it starts: on the Pico 2, kmain() in
board/pico2.c sets up the chip, then calls kernel_init()
and quit(), the outer interpreter, in src/interp.c. The
host build's main() is in board/host.c.
Bug reports, questions and ideas are welcome in Issues.
littleFORTH is free and made in spare time. If it is useful to you, buying me a coffee keeps it going.
MIT (see LICENSE). Third-party files and their licences are listed in NOTICE.
