Files
Basys3RiscV/README.md
T
brad ArantandClaude Opus 5.5 5930b958b4 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>
2026-11-30 18:09:18 -08:00

104 lines
5.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Basys 3 RISC-V Computer
The [Basys 3 Z-80 computer](https://git.barant.com/barant/Basys3Terminal) with
the Z-80 replaced by a RISC-V processor written from scratch in Verilog. The
rest is the same machine: a VT220 terminal with ANSI colour on 1280×1024 VGA,
320×240 graphics in 16 colours with hardware scrolling and 32 sprites, a
three-voice sound chip (PmodAMP2 on JA), the keyboard on the USB host port
(with a keys-held port for games), files in the configuration flash,
Dartmouth-style BASIC, a machine-code monitor and five games.
Flip **SW15** up and the terminal is the computer's console. BASIC and the
monitor are described in [docs/basic-manual.md](docs/basic-manual.md); the
terminal itself (SW15 down) works exactly as in the Z-80 project, whose README
covers it.
## The processor (`rtl/rv32_cpu.v`)
RV32IM in machine mode: the base integer set, multiply/divide, CSRs, counters
(`cycle`, `time`, `instret`), `fence.i`, `ecall`/`ebreak`/`mret`/`wfi`, traps
and interrupts (`mtvec`, `mepc`, `mcause`, `mtval`; software, timer, external
and 16 local interrupt lines), two hardware triggers for execute, load and
store breakpoints, and a resumable NMI (after the Smrnmi extension: `mnepc`,
`mnstatus`, `mnret`) with a single-step input, for the monitor.
It is a multi-cycle design with the next instruction fetched while the current
one decodes: 2 clocks for most instructions, 3 for taken branches, jumps, loads
and stores, 4 for multiplies, 35 for divides; BASIC averages about 2.5. It
runs at 90 MHz (the Z-80's clock) and takes about 2,100 LUTs, 900 flip-flops
and 4 DSP slices.
## The machine (`rtl/rv32_system.v`)
| Addresses | |
|---|---|
| 0000_0000–0001_3FFF | RAM, 80 KB of block RAM, BASIC preloaded; writes below A000h (BASIC's code) are ignored |
| 0002_0000–0002_7FFF | the monitor's ROM (32 KB of block RAM); the NMI enters at 20000h, traps at 20004h |
| 0002_8000–0002_9FFF | the monitor's RAM (8 KB of distributed RAM, data only) |
| FFFF_FE00–FFFF_FEFF | 32-bit registers: milliseconds, microseconds, the timer interrupt's compare, breakpoints |
| FFFF_FF00–FFFF_FFFF | the Z-80 machine's I/O ports: port *N* at FFFF_FF00 + *N*, one instruction from `x0` (`sb a0,-255(zero)` is `OUT (1),A`) |
The peripherals are the Z-80 machine's, unchanged in function: `term_ctrl.v`,
`text_display.v`, `sprites.v`, `sound.v`, `flash_spi.v`, `uart.v`,
`ps2_keyboard.v`, `sevenseg.v`. To make room in block RAM for the wider
processor's memory, the text screen and the graphics framebuffer are each
split into a power-of-two array and a small one (Vivado builds an array of
16,896 or 76,800 entries as 32K or 96K deep); that saves 10.5 block RAMs and
changes nothing else.
Resources at 90 MHz: see `build/utilization.rpt` and `build/timing.rpt` after a
build.
## Software
All in C (plus a little assembly), compiled with GCC for `rv32im`:
- `basic/` — BASIC, ported from the Z-80 version: the same language, the same
manual. `lib/` has the few C library functions it needs (small string
functions, `setjmp`, and single-precision `sinf` … `sqrtf`), since newlib's
bring in its stdio and malloc. Floating point is IEEE single precision from
libgcc, correctly rounded (the Z-80's SDCC library wasn't, so the last digit
of a result can differ).
- `monitor/` — the machine-code monitor, with a RISC-V disassembler and a
two-pass assembler in place of the Z-80 ones.
- `games/` — The Iron Keep, Bastion Raid, Star Warden, Star Swarm and Crystal
Cavern, recompiled; they load at A200h (`CALL 41472`).
## Building
```bash
export VIVADO=~/vivado/2025.2/Vivado
```
```bash
basic/build.sh && monitor/build.sh && for g in keep raid warden swarm cavern; do games/build-game.sh $g; done
```
```bash
scripts/build.sh
```
`scripts/build.sh program` loads the bitstream over JTAG; `flash` writes it to
the configuration flash.
Games go onto the board as in the Z-80 project: `tools/send-hex.py
games/keep/keep.ihx --save KEEPGAME --baud 3000000`, then `tools/send-bas.py
games/keep/keep.bas` and `SAVE "KEEP"` in BASIC.
## Tests
| | |
|---|---|
| `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `rv32mi`, fetched into `third_party/`) and `tests/` (interrupts, the NMI and single step) on `rv32_cpu.v` in xsim, with random wait states on the data bus. Two riscv-tests don't apply: `ma_data` expects misaligned loads and stores to work (here they trap, as the spec allows) and `pmpaddr` needs PMP. |
| `scripts/sim.sh` | the whole design in xsim: the terminal, BASIC, graphics, sprites, scrolling, flash files, the monitor (step, breakpoint, Pause, memory files, the assembler, Intel HEX), 3 Mbaud. `scripts/sim.sh quick` skips the terminal and the monitor. |
| `monitor/test-disasm.py`, `test-asm.py` | the disassembler against GNU objdump on thousands of random instructions, and the assembler against GNU as |
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM and string arrays, and each game, on `tools/rv32emu.py` (a Python RV32IM emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. |
| `bench/` | the processor alone with 64 KB of RAM, for timing |
## Toolchain
- Vivado (2025.2 is the licensed install here).
- RISC-V GCC: Vivado ships one for MicroBlaze V (`~/vivado/*/gnu/riscv/lin/bin/riscv64-unknown-elf-gcc`,
GCC 13 with an `rv32im/ilp32` multilib), which `scripts/rv_env.sh` finds
if there is no `riscv64-unknown-elf-gcc` on the PATH. `RV_PREFIX` overrides.