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.

History
54 lines·1.5 KB·GAS RISC-V Raw Blame
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
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
22park:
23 wfi
26 .text
27 .p2align 2
28 .global putc, getc, halt
29putc: // a0 = byte
30 li t0, UART
311: lbu t1, UART_LSR(t0)
32 andi t1, t1, LSR_THRE
33 beqz t1, 1b
34 sb a0, 0(t0)
35 ret
37getc: // returns a0 = byte
38 li t0, UART
391: lbu t1, UART_LSR(t0)
40 andi t1, t1, LSR_DR
41 beqz t1, 1b
42 lbu a0, 0(t0)
43 ret
45halt:
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
51 .section .bss
52 .p2align 4
53 .space 4096
54stack_top: