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

+12
View File
@@ -0,0 +1,12 @@
#!/bin/bash
# Build the bitstream; add "program" or "flash" to load it onto a connected board.
set -e
cd "$(dirname "$0")/.."
source scripts/vivado_env.sh
mkdir -p build
cd build
vivado -mode batch -nojournal -log build.log -source ../scripts/build.tcl
case "$1" in
program) vivado -mode batch -nojournal -log program.log -source ../scripts/program.tcl ;;
flash) vivado -mode batch -nojournal -log program.log -source ../scripts/program.tcl -tclargs flash ;;
esac
+37
View File
@@ -0,0 +1,37 @@
# Non-project Vivado build: vivado -mode batch -source scripts/build.tcl
# The CPU clock is 100 MHz * CPU_MULT / CPU_DIV (environment variables).
set root [file normalize [file dirname [info script]]/..]
set out $root/build
file mkdir $out
set mult [expr {[info exists ::env(CPU_MULT)] ? $::env(CPU_MULT) : 9.0}]
set div [expr {[info exists ::env(CPU_DIV)] ? $::env(CPU_DIV) : 10.0}]
set hz [expr {int(round(100e6 * $mult / $div))}]
puts "CPU clock: [expr {$hz / 1e6}] MHz"
read_verilog [glob $root/rtl/*.v]
read_mem $root/font/font.mem
read_mem $root/basic/basic.mem
read_mem $root/basic/basic-hi.mem
read_mem $root/monitor/monitor.mem
read_xdc $root/constraints/basys3.xdc
synth_design -top basys3_terminal -part xc7a35tcpg236-1 \
-generic CPU_MULT=$mult -generic CPU_DIV=$div -generic CPU_HZ=$hz
opt_design -directive Explore
place_design -directive ExtraTimingOpt
phys_opt_design -directive AggressiveExplore
route_design -directive Explore
phys_opt_design -directive AggressiveExplore
write_checkpoint -force $out/routed.dcp
report_utilization -file $out/utilization.rpt
report_timing_summary -file $out/timing.rpt
report_timing -max_paths 30 -nworst 1 -unique_pins -file $out/paths.rpt
write_bitstream -force $out/basys3_terminal.bit
write_cfgmem -force -format mcs -interface SPIx4 -size 4 \
-loadbit "up 0x0 $out/basys3_terminal.bit" $out/basys3_terminal.mcs
set wns [get_property SLACK [get_timing_paths -max_paths 1 -setup]]
puts "Worst setup slack: $wns ns"
if {$wns < 0} { error "timing not met" }
+52
View File
@@ -0,0 +1,52 @@
#!/bin/bash
# Build riscv-tests (rv32ui, rv32um, rv32mi) and our own tests in tests/, run
# them all on rtl/rv32_cpu.v in xsim. Fetches riscv-tests into third_party/.
# scripts/cputest.sh [name-filter]
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/rv_env.sh
RT=$ROOT/third_party/riscv-tests
if [ ! -d "$RT/isa" ]; then
git clone --recursive https://github.com/riscv-software-src/riscv-tests.git "$RT"
fi
# Not applicable to this core:
# ma_data assumes misaligned loads/stores work; here they trap (allowed)
# pmpaddr needs PMP
SKIP="rv32ui-ma_data rv32mi-pmpaddr"
OUT=$ROOT/build/tests
mkdir -p "$OUT"
LIST=$OUT/list.txt
: > "$LIST"
build() { # name source
local name=$1 src=$2
case " $SKIP " in *" $name "*) return ;; esac
[ -n "$FILTER" ] && [[ $name != *$FILTER* ]] && return
${RV_PREFIX}gcc $RV_ARCH -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -Wl,--build-id=none \
-I"$RT/env/p" -I"$RT/isa/macros/scalar" -I"$ROOT/tests" -T"$ROOT/tests/link.ld" \
"$src" -o "$OUT/$name.elf"
${RV_PREFIX}objcopy -O binary "$OUT/$name.elf" "$OUT/$name.bin"
python3 "$ROOT/tools/bin2mem.py" "$OUT/$name.bin" "$OUT/$name.mem"
local th=$(${RV_PREFIX}nm "$OUT/$name.elf" | awk '$3=="tohost"{print $1}')
echo "$OUT/$name.mem $th $name" >> "$LIST"
}
FILTER=$1
for suite in rv32ui rv32um rv32mi; do
for t in $(sed -n "/^${suite}_sc_tests/,/^\$/p" "$RT/isa/$suite/Makefrag" | grep -v = | tr -d '\\'); do
build "$suite-$t" "$RT/isa/$suite/$t.S"
done
done
for s in tests/*.S; do [ -e "$s" ] || continue; build "own-$(basename "$s" .S)" "$s"; done
echo "$(wc -l < "$LIST") tests built"
source scripts/vivado_env.sh
mkdir -p sim/cpu
cd sim/cpu
rm -f xsim.log
xvlog "$ROOT/rtl/rv32_cpu.v" "$ROOT/sim/tb_cpu.v" | grep -E "^(ERROR|WARNING)" || true
[ -d xsim.dir/work ] && ! grep -q ERROR xvlog.log || { echo "compile failed"; exit 1; }
xelab -debug off tb_cpu -s tb_cpu >/dev/null
xsim tb_cpu -R -testplusarg list="$LIST" | grep -E '^(PASS|FAIL|[0-9]+ passed)'
grep -q " passed, 0 failed" xsim.log
+43
View File
@@ -0,0 +1,43 @@
# Program the Basys 3 over its USB-JTAG port.
# vivado -mode batch -source scripts/program.tcl (SRAM, lost at power-off)
# vivado -mode batch -source scripts/program.tcl -tclargs flash (QSPI flash, survives power-off)
set root [file normalize [file dirname [info script]]/..]
set mode [expr {[llength $argv] > 0 ? [lindex $argv 0] : "sram"}]
open_hw_manager
connect_hw_server
open_hw_target
set dev [lindex [get_hw_devices xc7a35t*] 0]
current_hw_device $dev
refresh_hw_device -update_hw_probes false $dev
if {$mode eq "flash"} {
# Board revisions differ: older Basys 3s have Spansion flash, newer ones Macronix.
# Vivado checks the chip ID before erasing, so a wrong guess is harmless.
set done 0
foreach part {mx25l3273f-spi-x1_x2_x4 s25fl032p-spi-x1_x2_x4} {
create_hw_cfgmem -hw_device $dev [lindex [get_cfgmem_parts $part] 0]
set mem [get_property PROGRAM.HW_CFGMEM $dev]
set_property PROGRAM.FILES [list $root/build/basys3_terminal.mcs] $mem
set_property PROGRAM.ADDRESS_RANGE use_file $mem
set_property PROGRAM.ERASE 1 $mem
set_property PROGRAM.CFG_PROGRAM 1 $mem
set_property PROGRAM.VERIFY 1 $mem
create_hw_bitstream -hw_device $dev [get_property PROGRAM.HW_CFGMEM_BITFILE $dev]
program_hw_devices $dev
if {![catch {program_hw_cfgmem -hw_cfgmem $mem}]} {
set done 1
break
}
delete_hw_cfgmem $mem
}
if {!$done} { error "flash part not recognised" }
# reload the FPGA from flash (works when JP1 is set to QSPI)
boot_hw_device $dev
puts "Flash programmed; with JP1 on QSPI the board now boots from it."
} else {
set_property PROGRAM.FILE $root/build/basys3_terminal.bit $dev
program_hw_devices $dev
puts "FPGA configured."
}
close_hw_manager
+13
View File
@@ -0,0 +1,13 @@
# Sourced by the build scripts: finds a RISC-V GCC.
# RV_PREFIX overrides; otherwise a riscv64/riscv32-unknown-elf-gcc on PATH, else
# the one that ships with Vivado (for MicroBlaze V; it has the rv32im multilib).
if [ -z "$RV_PREFIX" ]; then
for p in riscv64-unknown-elf- riscv32-unknown-elf-; do
command -v ${p}gcc >/dev/null && { RV_PREFIX=$p; break; }
done
fi
if [ -z "$RV_PREFIX" ]; then
RV_PREFIX=$(ls -d "$HOME"/vivado/*/gnu/riscv/lin/bin 2>/dev/null | sort -V | tail -1)/riscv64-unknown-elf-
fi
[ -x "${RV_PREFIX}gcc" ] || command -v "${RV_PREFIX}gcc" >/dev/null || { echo "no RISC-V gcc found; set RV_PREFIX" >&2; exit 1; }
RV_ARCH="-march=rv32im_zicsr_zifencei -mabi=ilp32"
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
# Run the sound chip's testbench (sim/tb_sound.v) in Vivado's simulator.
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/vivado_env.sh
mkdir -p sim/work-sound
cd sim/work-sound
xvlog "$ROOT"/rtl/sound.v "$ROOT"/sim/tb_sound.v
xelab -debug typical tb_sound -s tb_sound
xsim tb_sound -R
Executable
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
# Run the whole-design testbench in Vivado's simulator; output lands in sim/work.
# scripts/sim.sh [quick] quick: only the computer, up to the monitor
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/vivado_env.sh
DEFS=""
[ "$1" = quick ] && DEFS="-d SKIP_TERMINAL -d STOP_BEFORE_MONITOR"
MON=$ROOT/monitor/monitor.mem
[ -e "$MON" ] || { mkdir -p sim/work; MON=$ROOT/sim/work/empty.mem; : > "$MON"; }
mkdir -p sim/work
cd sim/work
xvlog -d SIMULATION $DEFS -d FONT_FILE="\"$ROOT/font/font.mem\"" -d MEM_FILE="\"$ROOT/basic/basic.mem\"" \
-d MEM_HI_FILE="\"$ROOT/basic/basic-hi.mem\"" -d MON_FILE="\"$MON\"" \
"$ROOT"/rtl/*.v "$ROOT"/sim/spiflash_model.v "$ROOT"/sim/tb_terminal.v | grep -E '^(ERROR|WARNING)' || true
xelab -debug off tb_terminal -s tb_terminal | grep -E '^(ERROR|WARNING)' || true
xsim tb_terminal -R | grep -v -E '^(INFO|#|\*\*|Vivado|source|run|exit|Time:|Copyright)'
if command -v python3 >/dev/null; then
for f in frame_*.ppm; do
[ -e "$f" ] || continue
python3 -c "import sys; from PIL import Image; Image.open(sys.argv[1]).save(sys.argv[2])" \
"$f" "${f%.ppm}.png" && echo "wrote sim/work/${f%.ppm}.png"
done
fi
+13
View File
@@ -0,0 +1,13 @@
# Synthesis only, for resource counts: vivado -mode batch -source scripts/synth.tcl
set root [file normalize [file dirname [info script]]/..]
set out $root/build
file mkdir $out
read_verilog [glob $root/rtl/*.v]
read_mem $root/font/font.mem
read_mem $root/basic/basic.mem
read_mem $root/basic/basic-hi.mem
read_mem $root/monitor/monitor.mem
read_xdc $root/constraints/basys3.xdc
synth_design -top basys3_terminal -part xc7a35tcpg236-1
report_utilization -file $out/synth_util.rpt
report_utilization -hierarchical -hierarchical_depth 2 -file $out/synth_hier.rpt
+14
View File
@@ -0,0 +1,14 @@
# Sourced by build.sh and sim.sh: sets up Vivado's environment.
# VIVADO picks the install; default is the newest in ~/vivado/<version>/Vivado.
VIVADO=${VIVADO:-$(ls -d "$HOME"/vivado/*/Vivado 2>/dev/null | sort -V | tail -1)}
source "$VIVADO/settings64.sh"
# Vivado still links against ncurses 5; newer distros only ship 6, which works.
COMPAT_LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/build/compat-lib"
mkdir -p "$COMPAT_LIB"
for l in ncurses tinfo; do
if ! ldconfig -p | grep -q "lib$l.so.5" && [ -e /usr/lib/x86_64-linux-gnu/lib$l.so.6 ]; then
ln -sf /usr/lib/x86_64-linux-gnu/lib$l.so.6 "$COMPAT_LIB/lib$l.so.5"
fi
done
export LD_LIBRARY_PATH="$COMPAT_LIB${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"