Basys 3 RISC-V computer

The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 18:09:18 -08:00
commit 5930b958b4
103 files changed
+55677

No files matched your search

+178
View File
@@ -0,0 +1,178 @@
# Interrupts: each source, masking by mie and mstatus.MIE, priority, and code
# that is interrupted again and again giving the same result as without.
# Uses the test device in sim/tb_cpu.v at FFFFFF00.
#include "riscv_test.h"
#include "test_macros.h"
#define DEV -256 /* 0xFFFFFF00 */
#define SOFT 1
#define TIMER 2
#define EXT 4
#define LOCAL 8
# raise lines after delay cycles
#define RAISE(lines, delay) li t0, ((delay) << 8) | (lines); sw t0, DEV(zero)
# spin until the handler has run (s2 != count), or fail after a while
#define WAIT_IRQ(count) \
li t1, 1000; \
1: li t0, count; bne s2, t0, 2f; addi t1, t1, -1; bnez t1, 1b; j fail; 2:
RVTEST_RV32M
RVTEST_CODE_BEGIN
li s2, 0
csrsi mstatus, MSTATUS_MIE
# each source, with its own enable bit
li TESTNUM, 2
li t0, MIP_MTIP; csrw mie, t0
RAISE(TIMER, 20)
WAIT_IRQ(0)
li t0, 0x80000007; bne s1, t0, fail
li TESTNUM, 3
li t0, MIP_MEIP; csrw mie, t0
RAISE(EXT, 5)
WAIT_IRQ(1)
li t0, 0x8000000b; bne s1, t0, fail
li TESTNUM, 4
li t0, MIP_MSIP; csrw mie, t0
RAISE(SOFT, 0)
WAIT_IRQ(2)
li t0, 0x80000003; bne s1, t0, fail
li TESTNUM, 5
li t0, 1 << 16; csrw mie, t0
RAISE(LOCAL, 3)
WAIT_IRQ(3)
li t0, 0x80000010; bne s1, t0, fail
# masked by mie: pending shows in mip but nothing is taken
li TESTNUM, 6
csrw mie, zero
RAISE(TIMER, 0)
li t1, 100
1: addi t1, t1, -1; bnez t1, 1b
li t0, 4; bne s2, t0, fail
csrr t0, mip; andi t0, t0, MIP_MTIP; beqz t0, fail
li t0, MIP_MTIP; csrs mie, t0 # now it is taken
WAIT_IRQ(4)
# masked by mstatus.MIE
li TESTNUM, 7
csrci mstatus, MSTATUS_MIE
RAISE(EXT, 0)
li t0, MIP_MEIP; csrw mie, t0
li t1, 100
1: addi t1, t1, -1; bnez t1, 1b
li t0, 5; bne s2, t0, fail
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(5)
li t0, 0x8000000b; bne s1, t0, fail
# the handler saw MPIE set and MIE clear
li t0, MSTATUS_MPIE; and t1, s3, t0; beqz t1, fail
andi t1, s3, MSTATUS_MIE; bnez t1, fail
# and mret turned MIE back on
csrr t0, mstatus; andi t0, t0, MSTATUS_MIE; beqz t0, fail
# priority: external, then software, then timer, then local
li TESTNUM, 8
li t0, MIP_MEIP | MIP_MSIP | MIP_MTIP | (1 << 16); csrw mie, t0
csrci mstatus, MSTATUS_MIE
RAISE(EXT | SOFT | TIMER | LOCAL, 0)
li t1, 10
1: addi t1, t1, -1; bnez t1, 1b
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(6)
li t0, 0x8000000b; bne s1, t0, fail
li TESTNUM, 9
csrci mstatus, MSTATUS_MIE
RAISE(SOFT | TIMER | LOCAL, 0)
li t1, 10
1: addi t1, t1, -1; bnez t1, 1b
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(7)
li t0, 0x80000003; bne s1, t0, fail
li TESTNUM, 10
csrci mstatus, MSTATUS_MIE
RAISE(TIMER | LOCAL, 0)
li t1, 10
1: addi t1, t1, -1; bnez t1, 1b
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(8)
li t0, 0x80000007; bne s1, t0, fail
# a workout: once quietly, once with the timer firing every few cycles
li TESTNUM, 11
csrw mie, zero
jal workout
mv s5, a0
li s4, 1 # handler re-arms the timer
li t0, MIP_MTIP; csrw mie, t0
RAISE(TIMER, 3)
jal workout
csrw mie, zero
li s4, 0
bne a0, s5, fail
li t0, 100; blt s2, t0, fail # it really was interrupted a lot
TEST_PASSFAIL
# mixes ALU, mul, div, loads, stores and branches; result in a0
workout:
la a1, buf
li a0, 12345
li a2, 150
1:
mul a3, a0, a2
addi a3, a3, 77
divu a4, a3, a2
rem a5, a3, a2
xor a0, a0, a4
add a0, a0, a5
slli a6, a2, 2
andi a6, a6, 60
add a6, a6, a1
sw a0, 0(a6)
lh a7, 2(a6)
lbu a3, 1(a6)
add a0, a0, a7
sub a0, a0, a3
mulh a4, a0, a0
add a0, a0, a4
sb a0, 5(a6)
lw a4, 4(a6)
add a0, a0, a4
addi a2, a2, -1
bnez a2, 1b
ret
.align 2
.global mtvec_handler
mtvec_handler:
csrr s1, mcause
csrr s3, mstatus
bgez s1, fail # not an interrupt
addi s2, s2, 1
li t5, DEV
beqz s4, 1f
andi t6, s2, 31 # re-arm after 20..51 cycles
addi t6, t6, 20
slli t6, t6, 8
ori t6, t6, TIMER
sw t6, 0(t5)
mret
1:
sw zero, 0(t5)
mret
RVTEST_CODE_END
.data
RVTEST_DATA_BEGIN
TEST_DATA
buf: .fill 32, 4, 0
RVTEST_DATA_END
+19
View File
@@ -0,0 +1,19 @@
/* riscv-tests image: everything from address 0 (the core's reset vector);
tests/nmi.S puts its NMI handler at 8000h, tb_cpu.v's NMI_VEC. */
OUTPUT_ARCH( "riscv" )
ENTRY(_start)
SECTIONS
{
. = 0x0;
.text.init : { *(.text.init) }
. = ALIGN(0x1000);
.tohost : { *(.tohost) }
. = ALIGN(0x1000);
.text : { *(.text) }
. = ALIGN(0x1000);
.data : { *(.data) }
.bss : { *(.bss) }
. = 0x8000;
.nmi : { KEEP(*(.nmi)) }
_end = .;
}
+89
View File
@@ -0,0 +1,89 @@
# Resumable NMI: one from the nmi line, then single steps from nmi_step.
# The handler is at 8000h (tb_cpu.v's NMI_VEC); the test device is FFFFFF00.
#include "riscv_test.h"
#include "test_macros.h"
#define DEV -256
#define NMI 16
#define STEP 32
#define RAISE(lines, delay) li t0, ((delay) << 8) | (lines); sw t0, DEV(zero)
RVTEST_RV32M
RVTEST_CODE_BEGIN
li s2, 0
# NMIE starts set
li TESTNUM, 2
csrr t0, CSR_MNSTATUS; andi t0, t0, MNSTATUS_NMIE; beqz t0, fail
# an NMI from the line: handler runs once, mnret comes back here
li TESTNUM, 3
li s4, 0 # handler mode: drop the line
RAISE(NMI, 10)
li t1, 1000
1: bnez s2, 2f; addi t1, t1, -1; bnez t1, 1b; j fail
2: li t0, 1; bne s2, t0, fail
csrr t0, CSR_MNSTATUS; andi t0, t0, MNSTATUS_NMIE; beqz t0, fail
andi t0, s5, MNSTATUS_NMIE; bnez t0, fail # clear inside the handler
# NMIs are masked while NMIE is clear
li TESTNUM, 4
csrci CSR_MNSTATUS, MNSTATUS_NMIE
RAISE(NMI, 0)
li t1, 50
1: addi t1, t1, -1; bnez t1, 1b
li t0, 1; bne s2, t0, fail
csrsi CSR_MNSTATUS, MNSTATUS_NMIE
li t1, 50
1: addi t1, t1, -1; bnez t1, 1b
li t0, 2; bne s2, t0, fail
# single steps: the handler logs mnepc and stops after four
li TESTNUM, 5
li s2, 0
li s4, 1 # handler mode: log, stop after 4
la s6, log
RAISE(STEP, 0)
step0:
addi a0, x0, 1
step1:
addi a0, a0, 1
step2:
addi a0, a0, 1
step3:
addi a0, a0, 1
addi a0, a0, 1
li t0, 4; bne s2, t0, fail
li t0, 5; bne a0, t0, fail # every instruction ran once
# the stops were after the RAISE's store, then after each instruction
la s6, log
la t0, step0; lw t1, 0(s6); bne t0, t1, fail
la t0, step1; lw t1, 4(s6); bne t0, t1, fail
la t0, step2; lw t1, 8(s6); bne t0, t1, fail
la t0, step3; lw t1, 12(s6); bne t0, t1, fail
TEST_PASSFAIL
.section .nmi, "ax"
nmi_handler:
csrr s5, CSR_MNSTATUS
addi s2, s2, 1
bnez s4, 1f
sw zero, DEV(zero) # drop the line
.word 0x70200073 # mnret
1:
csrr t5, CSR_MNEPC
sw t5, 0(s6)
addi s6, s6, 4
li t5, 4
bne s2, t5, 2f
sw zero, DEV(zero) # stop stepping
2:
.word 0x70200073 # mnret
.data
RVTEST_DATA_BEGIN
TEST_DATA
log: .fill 8, 4, 0
RVTEST_DATA_END