A Forth of the 1970s kind in about 4 K of ROM, for sysl. It fits in half the ROM fig-Forth needed. The sysl core holds the inner interpreter, sectorforth's eight primitives, its two I/O primitives and its five variables, plus three built-in constants. Every other built-in word is Forth, and those words run in place from ROM.
$ bittyforth
bittyforth -- Ctrl-D to leave
2 3 + .
5 ok
: square dup * ;
ok
7 square .
49 ok
: countdown begin dup . 1- dup 0= until drop ;
ok
5 countdown
5 4 3 2 1 ok
variable total 10 total ! 32 total +! total ?
42 ok
255 hex . decimal
FF ok
: fact dup 1 > if dup 1- recurse * then ;
ok
7 fact .
5040 ok
1 0 /
/0?
foo
foo ?
bittyforth's primitive set and the shape of its bootstrap come from
sectorforth by Cesar Blum. bittyforth takes sectorforth's eight
primitives (@ ! sp@ rp@ 0= + nand exit), its two I/O primitives (key emit) and its five
variables (tib state >in here latest).
The bootstrap is an outer interpreter that knows : and ; and is just enough to compile the
first Forth. Some of kernel.fs's early words are adapted from sectorforth's examples. sectorforth
is MIT licensed, and its notice is in LICENSE-sectorforth.
It is not a straight port. bittyforth differs in these ways:
- It is written in sysl rather than x86 assembly, so it runs on any target sysl builds for.
- It adds three built-in constants,
0,1and2.litneeds a 2 before any literal exists, and 0 and 1 are the commonest constants. - It is token-threaded: a call is one byte. On this word set that is 865 bytes smaller than sectorforth's two-byte addresses.
- The built-in words are a ROM image that runs in place. Only 50 bytes are copied to RAM at boot.
- The caller sizes the RAM, the input buffer and both stacks.
- 133 built-in words are Forth, built on those primitives, variables and constants. They include
the whole outer interpreter: parsing, dictionary search, number conversion,
:,;andquit. - The bootstrap runs only on the host, where it builds the image. It parses decimal numbers, so the Forth source is written with plain literals and needs no number-building words. It adds nothing to the ROM.
- It takes 4301 bytes of ROM in all: a 3075-byte image and 1226 bytes of thumb code.
At the terminal, run this from the repository root:
sysl run examples/bittyforth.sysl
sysl run examples/bittyforth.sysl -- file.fs
On a board, add the package and link the core alone. It is @no_alloc and needs no operating
system:
dependencies {
bittyforth { git = "github.com/sysl-lang/bittyforth", version = "0.1.0" }
}
import sh.sysl.bittyforth.{IMAGE, Mem, R_KEY, boot, emitted, give_key, run}
var ram: [1024]u8 = [0; 1024]
var m = Mem(IMAGE, ram[..], ram[0..<0])
boot(&m, 80, 32, 32)
loop
if run(&m) == R_KEY then give_key(&m, u8(uart_getc())) else uart_putc(int(emitted(&m)))
examples/board.sysl is this program in full. sysl build-c examples/board.sysl --target thumb-freestanding builds it into an archive whose only undefined symbols are uart_getc,
uart_putc, memcpy and memset. The machine's RAM is the array you give it, and the input buffer and stack sizes are
boot's arguments.
sh/sysl/bittyforth/
vm.sysl the inner interpreter, the primitives, the ROM and RAM memory map, boot
bootstrap.sysl an outer interpreter in sectorforth's shape, with numbers: builds the image
kernel.fs every other built-in word, in Forth
image.sysl the ROM image, embedded from image.bin
host/__posix__/ building the image, the census of its bytes, a machine fed line by line
examples/
bittyforth.sysl the terminal program, through sysl-lang/repl
board.sysl the same machine on a board, over a UART
docs/design.md the memory map, the token encoding, the primitives, what fig-Forth words are left out
| bytes | |
|---|---|
| ROM image: the built-in words' headers, bodies and token table, and 50 bytes of RAM's initial contents | 3075 |
core code (build-c --target thumb-freestanding -Oz) |
1226 |
| ROM in all | 4301 (fig-Forth: about 8 K) |
| copied from ROM to RAM at boot | 50 |
| RAM in the board example: variables 50, input buffer 80, two 32-cell stacks 128, user dictionary 766 | 1024 |
Each built-in word is called by a one-byte token. On this word set that is 865 bytes smaller than
two-byte addresses (bittyforth --census shows the count, and docs/design.md explains it).
To rebuild the image after changing kernel.fs:
sysl run examples/bittyforth.sysl -- --image sh/sysl/bittyforth/image.bin
Left out on purpose: <BUILDS DOES>, vocabularies, FORGET, double numbers, pictured output,
+LOOP, disk and block words, ERROR/MESSAGE, user variables, the assembler and BYE. Each has
its reason in docs/design.md ("What is left out of fig-Forth, and why"); most need
RAM or ROM that nothing in the system uses.
sysl test .
55 tests, also clean under AddressSanitizer (SYSL_EXTRA_CFLAGS="-fsanitize=address -g" sysl test .).
They run Forth programs through the machine and check the output (arithmetic, control flow,
recursion, defining words, division by zero, a full dictionary), check that the committed image is
what the bootstrap builds from kernel.fs today, and check the census of the image's bytes and what
boot copies to RAM.
ISC. The parts adapted from sectorforth are also under its MIT license.