Files
Basys3RiscV/README.md
T
brad ArantandClaude Opus 5.5 71fd403294 Add a single-precision FPU (the F extension)
rtl/rv32_fpu.v: IEEE 754 single precision with all five rounding modes,
subnormals, the exception flags and the canonical NaN. Multiply-add
(DSPs, exact product, 77-bit sum) with add, subtract and multiply as its
special cases; divide and square root a bit a clock; one normalise-and-
round back end. 12 clocks for add/mul/fma, 36 for div/sqrt. About 3000
LUTs and 2 DSPs; the system still meets 90 MHz.

rv32_cpu.v: float registers, FLW/FSW, OP-FP and the R4 instructions,
fflags/frm/fcsr, mstatus.FS (Initial at reset), misa F.

tools/f32.py is an exact model (checked against x86 hardware) used by
scripts/fputest.sh (random vectors, every instruction and rounding mode)
and by tools/rv32emu.py, which now runs RV32IMF. cputest.sh adds rv32uf.

BASIC and the games are built for rv32imf/ilp32f (BASIC 36.4 -> 28.7 KB,
SIN about twice as fast); setjmp saves fs0-fs11; sqrtf is FSQRT.S. The
monitor saves and restores f0-f31 and fcsr, shows them (F), sets them
(R F1=...), and its disassembler and assembler know every F instruction
(checked against objdump and GNU as). Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:29:08 -08:00

115 lines
6.3 KiB
Markdown
Raw 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.
The screen starts at 132 columns by 64 rows (8×16 font); `ESC [?3l` switches
to 80×32 in a font twice the size and `ESC [?3h` back (for a PC using the
terminal, `TERM=basys3-w` and `stty rows 64 cols 132`, from
`terminfo/basys3.ti`).
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`)
RV32IMF in machine mode: the base integer set, multiply/divide,
single-precision floating point (`rtl/rv32_fpu.v`: IEEE 754 with all five
rounding modes, subnormals and the exception flags), 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.
Floating-point add, subtract, multiply and the fused multiply-adds take 12
clocks, divide and square root 36. It runs at 90 MHz (the Z-80's clock) and
takes about 5,300 LUTs (3,000 of them the FPU), 1,900 flip-flops and 6 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. BASIC and the games are compiled for the FPU
(`rv32imf`, `ilp32f`); its arithmetic is IEEE single precision, 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/fputest.sh` | `rtl/rv32_fpu.v` against `tools/f32.py`, an exact model of RISC-V's single precision (itself checked against the PC's FPU), on random vectors: every F instruction, every rounding mode, special values, subnormals and near-cancellations |
| `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `rv32uf`, `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 RV32IMF 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.