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.

Code
Blame·1 commit across 45 linesoldernewer
First runnable RV64 Forth kernelOct 11 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
16putc: // 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
27getc: // 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
381: li a0, -1
39 addi sp, sp, 16
40 ret
41
42halt:
43 li a0, 0
44 li a7, SYS_exit_group
45 ecall