acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

harlan/rv-forth

Public
Star5

A tiny Forth kernel for RV64: Linux and QEMU virt builds, linked from handwritten assembly.

First runnable RV64 Forth kernel

Browse files
harlancommitted first commit commit 2b2de373ac64ab62341ec56b703ca79488541cba
failing 4 steps in 6 ms
added acompiled.toml +29−0
@@ -0,0 +1,29 @@
1+# Verified builds for rv-forth. The kernel is linked twice: as a Linux process,
2+# which every build runs, and as a flat image for QEMU virt.
3+
4+[project]
5+isa = "riscv64"
6+assembler = "gas"
7+target = "RV64IMAC: QEMU virt, bare metal, and Linux"
8+
9+[[step]]
10+name = "link for Linux"
11+run = ["clang", "--target=riscv64-linux-gnu", "-march=rv64imac", "-mabi=lp64", "-fno-pic", "-g", "-nostdlib", "-static", "-fuse-ld=lld", "-o", "build/forth-linux", "src/forth.S", "src/linux.S"]
12+
13+[[step]]
14+name = "link for QEMU virt"
15+run = ["clang", "--target=riscv64-unknown-elf", "-march=rv64imac_zicsr", "-mabi=lp64", "-mcmodel=medany", "-g", "-nostdlib", "-fuse-ld=lld", "-T", "link.ld", "-o", "build/forth.elf", "src/forth.S", "src/virt.S"]
16+
17+[[step]]
18+name = "flat image"
19+run = ["llvm-objcopy", "-O", "binary", "build/forth.elf", "build/forth.bin"]
20+
21+[[step]]
22+name = "squares of 1 to 10"
23+on = "linux"
24+run = ["./build/forth-linux"]
25+expect = "1 4 9 16 25 36 49 64 81 100"
26+
27+[artifact]
28+path = "build/forth.bin"
29+paths = ["build/forth-linux"]
added link.ld +12−0
@@ -0,0 +1,12 @@
1+/* QEMU virt: RAM starts at 0x80000000, and -bios loads the image there. */
2+OUTPUT_ARCH(riscv)
3+ENTRY(_start)
4+
5+SECTIONS
6+{
7+ . = 0x80000000;
8+ .text : { *(.text.init) *(.text .text.*) }
9+ .rodata : { *(.rodata .rodata.*) }
10+ .data : { *(.data .data.*) }
11+ .bss : { *(.bss .bss.*) }
12+}
added src/forth.S +233−0
@@ -0,0 +1,233 @@
1+// forth.S: an indirect-threaded Forth kernel for RV64.
2+//
3+// s0 IP the next cell of threaded code
4+// s1 DSP data stack, full descending, one 64-bit cell per item
5+// s2 RSP return stack, full descending
6+//
7+// A word is a codeword (the address of machine code) followed by its body.
8+// Code words point at their own assembly; colon definitions point at DOCOL,
9+// which saves IP and starts interpreting the body.
10+//
11+// The board lives elsewhere. virt.S boots QEMU virt bare-metal and linux.S
12+// runs the kernel as a Linux process. Each one supplies _start, which sets up
13+// sp and jumps to forth_start, and putc, getc and halt.
14+
15+// --- the inner interpreter ---------------------------------------------------
16+
17+ .macro NEXT
18+ ld t0, 0(s0) // t0 = execution token (address of a codeword)
19+ addi s0, s0, 8
20+ ld t1, 0(t0) // the codeword: where its machine code lives
21+ jr t1
22+ .endm
23+
24+ .macro PUSHRSP reg
25+ addi s2, s2, -8
26+ sd \reg, 0(s2)
27+ .endm
28+
29+ .macro POPRSP reg
30+ ld \reg, 0(s2)
31+ addi s2, s2, 8
32+ .endm
33+
34+ .macro PUSH reg
35+ addi s1, s1, -8
36+ sd \reg, 0(s1)
37+ .endm
38+
39+ .macro POP reg
40+ ld \reg, 0(s1)
41+ addi s1, s1, 8
42+ .endm
43+
44+// A primitive: an execution token in .rodata whose codeword points at the code below it.
45+ .macro defcode label
46+ .section .rodata
47+ .p2align 3
48+\label:
49+ .dword code_\label
50+ .text
51+ .p2align 2
52+code_\label:
53+ .endm
54+
55+// A colon definition: an execution token whose codeword is DOCOL; the body follows.
56+ .macro defword label
57+ .section .rodata
58+ .p2align 3
59+\label:
60+ .dword DOCOL
61+ .endm
62+
63+ .text
64+ .p2align 2
65+ .global forth_start
66+forth_start:
67+ la s1, dstack_top
68+ la s2, rstack_top
69+ la s0, cold
70+ NEXT
71+
72+DOCOL:
73+ PUSHRSP s0
74+ addi s0, t0, 8 // the body starts after the codeword
75+ NEXT
76+
77+// --- primitives ----------------------------------------------------------------
78+
79+ defcode EXIT
80+ POPRSP s0
81+ NEXT
82+
83+ defcode LIT // ( -- n ) the next cell is a number
84+ ld t2, 0(s0)
85+ addi s0, s0, 8
86+ PUSH t2
87+ NEXT
88+
89+ defcode DUP // ( a -- a a )
90+ ld t2, 0(s1)
91+ PUSH t2
92+ NEXT
93+
94+ defcode DROP // ( a -- )
95+ addi s1, s1, 8
96+ NEXT
97+
98+ defcode SWAP // ( a b -- b a )
99+ ld t2, 0(s1)
100+ ld t3, 8(s1)
101+ sd t3, 0(s1)
102+ sd t2, 8(s1)
103+ NEXT
104+
105+ defcode OVER // ( a b -- a b a )
106+ ld t2, 8(s1)
107+ PUSH t2
108+ NEXT
109+
110+ defcode ADD // ( a b -- a+b )
111+ POP t2
112+ ld t3, 0(s1)
113+ add t3, t3, t2
114+ sd t3, 0(s1)
115+ NEXT
116+
117+ defcode SUB // ( a b -- a-b )
118+ POP t2
119+ ld t3, 0(s1)
120+ sub t3, t3, t2
121+ sd t3, 0(s1)
122+ NEXT
123+
124+ defcode MUL // ( a b -- a*b )
125+ POP t2
126+ ld t3, 0(s1)
127+ mul t3, t3, t2
128+ sd t3, 0(s1)
129+ NEXT
130+
131+ defcode FETCH // ( addr -- n )
132+ ld t2, 0(s1)
133+ ld t2, 0(t2)
134+ sd t2, 0(s1)
135+ NEXT
136+
137+ defcode STORE // ( n addr -- )
138+ POP t2
139+ POP t3
140+ sd t3, 0(t2)
141+ NEXT
142+
143+ defcode BRANCH // the next cell is a byte offset from itself
144+ ld t2, 0(s0)
145+ add s0, s0, t2
146+ NEXT
147+
148+ defcode ZBRANCH // ( flag -- ) branch when flag is zero
149+ POP t3
150+ beqz t3, code_BRANCH
151+ addi s0, s0, 8 // skip the offset
152+ NEXT
153+
154+ defcode EMIT // ( c -- )
155+ POP a0
156+ call putc
157+ NEXT
158+
159+ defcode KEY // ( -- c )
160+ call getc
161+ PUSH a0
162+ NEXT
163+
164+ defcode DOT // ( n -- ) print as signed decimal
165+ POP a0
166+ call print_dec
167+ NEXT
168+
169+ defcode HALT
170+ tail halt
171+
172+// --- colon definitions -----------------------------------------------------------
173+
174+ defword SQUARE // ( n -- n*n )
175+ .dword DUP, MUL, EXIT
176+
177+// cold: print the squares of 1 to 10, then halt.
178+ .section .rodata
179+ .p2align 3
180+cold:
181+ .dword LIT, 1
182+.Lloop:
183+ .dword DUP, SQUARE, DOT
184+ .dword LIT, 1, ADD
185+ .dword DUP, LIT, 11, SUB
186+ .dword ZBRANCH, .Ldone - .
187+ .dword BRANCH, .Lloop - .
188+.Ldone:
189+ .dword DROP, LIT, 10, EMIT, HALT
190+
191+// --- printing ----------------------------------------------------------------------
192+
193+ .text
194+ .p2align 2
195+print_dec: // a0 = signed value; prints digits and a space
196+ addi sp, sp, -48
197+ sd ra, 40(sp)
198+ sd s4, 32(sp)
199+ sd s5, 24(sp)
200+ mv s4, a0
201+ bgez s4, 1f
202+ li a0, '-'
203+ call putc
204+ neg s4, s4 // INT64_MIN stays INT64_MIN; divu reads it as 2^63
205+1: addi s5, sp, 24 // digits are written backwards into sp+0..23
206+ li t2, 10
207+2: remu t3, s4, t2
208+ divu s4, s4, t2
209+ addi t3, t3, '0'
210+ addi s5, s5, -1
211+ sb t3, 0(s5)
212+ bnez s4, 2b
213+3: lbu a0, 0(s5)
214+ call putc
215+ addi s5, s5, 1
216+ addi t4, sp, 24
217+ bltu s5, t4, 3b
218+ li a0, ' '
219+ call putc
220+ ld s5, 24(sp)
221+ ld s4, 32(sp)
222+ ld ra, 40(sp)
223+ addi sp, sp, 48
224+ ret
225+
226+// --- stacks ----------------------------------------------------------------------------
227+
228+ .section .bss
229+ .p2align 4
230+ .space 4096
231+dstack_top:
232+ .space 4096
233+rstack_top:
added src/linux.S +45−0
@@ -0,0 +1,45 @@
1+// linux.S: the board for a Linux process. The kernel has already set up sp,
2+// and every byte goes through an ecall.
3+//
4+// ./build/forth-linux on RV64 Linux, or under qemu-riscv64
5+
6+ .equ SYS_read, 63
7+ .equ SYS_write, 64
8+ .equ SYS_exit_group, 94
9+
10+ .text
11+ .p2align 2
12+ .global _start, putc, getc, halt
13+_start:
14+ j forth_start
15+
16+putc: // a0 = byte
17+ addi sp, sp, -16
18+ sb a0, 0(sp)
19+ li a0, 1 // stdout
20+ mv a1, sp
21+ li a2, 1
22+ li a7, SYS_write
23+ ecall
24+ addi sp, sp, 16
25+ ret
26+
27+getc: // returns a0 = byte, or -1 at end of input
28+ addi sp, sp, -16
29+ li a0, 0 // stdin
30+ mv a1, sp
31+ li a2, 1
32+ li a7, SYS_read
33+ ecall
34+ blez a0, 1f
35+ lbu a0, 0(sp)
36+ addi sp, sp, 16
37+ ret
38+1: li a0, -1
39+ addi sp, sp, 16
40+ ret
41+
42+halt:
43+ li a0, 0
44+ li a7, SYS_exit_group
45+ ecall
added src/virt.S +54−0
@@ -0,0 +1,54 @@
1+// virt.S: the board for QEMU virt, bare metal.
2+//
3+// -bios loads the flat image at 0x80000000 and starts every hart there in
4+// M-mode. Hart 0 runs Forth; the others wait in wfi for good.
5+//
6+// qemu-system-riscv64 -machine virt -nographic -bios build/forth.bin
7+
8+ .equ UART, 0x10000000 // NS16550A
9+ .equ UART_LSR, 5 // line status register
10+ .equ LSR_THRE, 0x20 // transmit holding register empty
11+ .equ LSR_DR, 0x01 // data ready
12+ .equ TEST, 0x100000 // SiFive test device
13+ .equ TEST_PASS, 0x5555 // power off, exit status 0
14+
15+ .section .text.init, "ax"
16+ .global _start
17+_start:
18+ csrr t0, mhartid
19+ bnez t0, park
20+ la sp, stack_top
21+ j forth_start
22+park:
23+ wfi
24+ j park
25+
26+ .text
27+ .p2align 2
28+ .global putc, getc, halt
29+putc: // a0 = byte
30+ li t0, UART
31+1: lbu t1, UART_LSR(t0)
32+ andi t1, t1, LSR_THRE
33+ beqz t1, 1b
34+ sb a0, 0(t0)
35+ ret
36+
37+getc: // returns a0 = byte
38+ li t0, UART
39+1: lbu t1, UART_LSR(t0)
40+ andi t1, t1, LSR_DR
41+ beqz t1, 1b
42+ lbu a0, 0(t0)
43+ ret
44+
45+halt:
46+ li t0, TEST
47+ li t1, TEST_PASS
48+ sw t1, 0(t0)
49+ j park // only reached if there is no test device
50+
51+ .section .bss
52+ .p2align 4
53+ .space 4096
54+stack_top: