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

+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
"""Run Crystal Cavern in tools/rv32emu.py on a model of the board
(tools/boardemu.py) and take screenshots.
python3 test-cavern.py [--shots DIR] [--play]
It checks the title screen, measures the Z-80 instructions each frame takes
(a 60 Hz frame has about 550,000 at 90 MHz), checks that walls stop the miner,
that crystals score and that the bats leave the roost, and with --play lets
an autopilot gather every crystal in the cave by breadth-first search. The
autopilot can't be caught (it keeps the miner shielded), so this checks the
maze and the rules, not its skill.
"""
import argparse
import os
import sys
from collections import deque
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ESC = 1, 2, 4, 8, 16, 128
MW, MH = 28, 29
OPEN = (0, 2, 3, 6)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="let the autopilot clear a cave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "cavern.ihx"))
b = Board(mem, code_end=hi)
font = load_font() if a.shots else None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "cavern.sym")).items()}
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def s16(n, k=0):
v = b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
return v - 65536 if v & 0x8000 else v
def u8(n, k=0):
return b.m[sym[n] + k]
def maze(x, y):
return b.m[sym["_maze"] + y * MW + (x % MW)] if 0 <= y < MH else 1
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "C R Y S T A L C A V E R N" in "\n".join(screen()))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(3, FIRE)
b.run_frames(110) # GET READY
check("game starts", "CAVE 1" in screen()[0] and "LIVES 3" in screen()[0])
b.m[sym["_shield"]] = 1
left0 = u8("_left")
c0 = b.steps
worst = 0
for f in range(120):
c1 = b.steps
b.run_frames(1, LEFT)
worst = max(worst, b.steps - c1)
print("%.0f instructions a frame on average, %d at most (%.0f%%)" %
((b.steps - c0) / 120, worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
check("crystals are gathered and score", u8("_left") < left0 and s16("_score") > 0)
x0 = s16("_mx")
b.run_frames(20, LEFT)
check("a wall stops the miner", s16("_mx") == x0 or (s16("_mx") & 7) == 4)
check("the bats leave the roost", sum(1 for k in range(4) if u8("_bstate", k) == 2) >= 2)
if a.shots: b.shot(os.path.join(a.shots, "2-chase.png"), font)
if a.play:
frame = 0
want = LEFT
while frame < 30000 and b.pc != 0:
b.m[sym["_shield"]] = 1
mx, my = s16("_mx"), s16("_my")
if (mx & 7) == 4 and (my & 7) == 4:
start = (mx >> 3, my >> 3)
prev = {start: None}
q = deque([start])
goal = None
while q:
p = q.popleft()
if maze(*p) in (2, 3) and p != start:
goal = p
break
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = ((p[0] + dx) % MW, p[1] + dy)
if n not in prev and maze(*n) in OPEN:
prev[n] = p; q.append(n)
if goal:
while prev[goal] != start:
goal = prev[goal]
dx = (goal[0] - start[0] + MW // 2) % MW - MW // 2
dy = goal[1] - start[1]
want = RIGHT if dx > 0 else LEFT if dx < 0 else DOWN if dy > 0 else UP
b.run_frames(1, want)
frame += 1
if frame == 900 and a.shots: b.shot(os.path.join(a.shots, "3-midgame.png"), font)
if "CAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot gathered every crystal in %d frames" % frame, "CAVE 2" in screen()[0])
b.run_frames(110) # the next cave's GET READY
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(screen()))
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()