Add a 640x480 two-colour graphics mode; start in white

The graphics memory doubles as 640x480 at a bit a pixel (port 38), four
pixels to an entry, plotted by read-modify-write, in two colours from
port 39 (0 transparent). BASIC gets SCREEN 0 / SCREEN 1 [,set[,clear]];
a plain fill (as every game does) returns to 320x240 and BASIC follows.
The text now starts in white (SW5:4 = 00; 10 is green). The README is
rewritten to stand on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-01 06:06:30 -08:00
1 parent 71fd403294
commit bda9bb67b6
13 files changed
+2985 -2680

No files matched your search

+139 -45
View File
@@ -1,22 +1,109 @@
# 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.
A complete home computer on a Digilent Basys 3 FPGA board, built around a
RISC-V processor written from scratch in Verilog:
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`).
- **RV32IMF processor at 90 MHz**: integer, multiply/divide and IEEE 754
single-precision floating point in hardware, with traps, interrupts, hardware
breakpoints and single step.
- **VT220 terminal** with ANSI colour on 1280×1024 VGA: 132 × 64 text, or 80 × 32
in a double-size font.
- **Graphics** over the text: 320 × 240 in 16 colours with hardware scrolling,
or 640 × 480 in two colours, plus 32 hardware sprites.
- **Sound**: a three-voice chip with noise and envelopes (PmodAMP2 on JA).
- **USB keyboard** on the board's USB host port, with a keys-held port for
games.
- **Files** saved in the board's configuration flash.
- **Software**: Dartmouth-style BASIC with graphics, sound and files, a
machine-code monitor with a RISC-V disassembler and assembler, and five
original 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 [BASIC manual](docs/basic-manual.md) ([PDF](docs/basic-manual.pdf))
describes the computer from the user's side: BASIC, graphics, sprites, sound,
files, the monitor and the games.
## Getting started
Download `basys3_terminal.bit` (or `basys3_terminal.mcs` to keep it in the
flash) from the [latest release](https://git.barant.com/barant/Basys3RiscV/releases),
or build it yourself (below).
1. Plug a VGA monitor, a USB keyboard, and optionally a PmodAMP2 into JA.
2. Load the bitstream with Vivado's Hardware Manager, or `scripts/build.sh
program` / `flash` from a checkout.
3. Flip **SW15 up**. The screen shows BASIC's banner and `READY`.
With SW15 down the board is a plain VT220 terminal on its USB serial port, for
a PC. The computer keeps running underneath, waiting at its next character of
output, so flipping back and forth loses nothing.
The games come in the release as `*.ihx` (the program) and `*.bas` (the
loader). From a checkout of this repository, on a PC connected to the board's
serial port:
```bash
tools/send-hex.py keep.ihx --save KEEPGAME --baud 3000000
```
```bash
tools/send-bas.py keep.bas --baud 3000000
```
then `SAVE "KEEP"` in BASIC, and `RUN "KEEP"` from then on.
## Controls
| Control | Function |
|---|---|
| SW1:0 | Serial speed: `00` 115200, `01` 9600, `10` 921600, `11` 3000000 |
| SW2 | Local echo |
| SW3 | Received LF also does CR |
| SW5:4 | Text colour at start-up: `00` white, `01` yellow, `10` green, `11` cyan |
| SW6 | Backspace sends DEL (`7F`) instead of BS (`08`) |
| SW7 | Enter sends CR LF instead of CR |
| SW8 | Smooth scrolling (also `ESC[?4h`) |
| SW9 | Fast smooth scrolling: 15 lines a second instead of 7.5 |
| SW14 | Computer mode: link the PC's serial port too (PC input is typed into BASIC, BASIC's output is copied to the PC) |
| SW15 | Computer mode: the terminal is the computer's console |
| BTNC | Reset everything |
| BTNU | Clear the screen |
| BTND | Restart the computer only (or Ctrl-Alt-Del in computer mode) |
| Pause key | Stop the program in the machine-code monitor |
| LED7:0 | Last byte received |
| LED11:8 | VT220 keyboard LEDs L4..L1 (`ESC[n q`) |
| LED12–15 | Bell, Caps Lock, serial activity, keyboard activity |
| 7-segment | In computer mode, whatever a program writes to ports 33/32 (`OUT 32,n`); otherwise the last scan code and the last byte sent |
## The terminal
The terminal understands what a VT220 does, plus ANSI colour:
- Cursor movement, save and restore, `IND`/`NEL`/`RI`; insert and delete of
characters and lines, erase, scroll, insert mode.
- Scroll regions, origin mode, deferred auto-wrap, tab stops.
- DEC special graphics (drawn with the CP437 line characters).
- Bold, underline, blink, reverse, concealed; 16 foreground and background
colours (256-colour and RGB codes are mapped to the nearest).
- Cursor and keypad application modes, cursor on/off, smooth scrolling, the
alternate screen, `DECCOLM` (`ESC[?3h` 132 × 64, `ESC[?3l` 80 × 32).
- Replies to `DA`, secondary `DA` and `DSR`/`CPR`; `RIS`, `DECSTR`,
`DECALN`.
- XON/XOFF flow control, so a PC can send at full speed.
For a Linux login on the board, install the terminfo entry and run a getty
on the board's port (the second of its two, usually `/dev/ttyUSB1`):
```bash
tic -x -o ~/.terminfo terminfo/basys3.ti
```
```bash
sudo /usr/sbin/agetty -L 115200 ttyUSB1 basys3-w
```
`basys3-w` is the 132 × 64 entry (`stty rows 64 cols 132`); `basys3` is
80 × 32. Set SW6 up so Backspace sends DEL. Text is drawn as CP437 bytes, with
no UTF-8 decoding, so use `LANG=C` for clean line drawing.
## The processor (`rtl/rv32_cpu.v`)
@@ -33,9 +120,8 @@ 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.
clocks, divide and square root 36. It runs at 90 MHz 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`)
@@ -45,34 +131,42 @@ slices.
| 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`) |
| FFFF_FF00–FFFF_FFFF | byte-wide I/O ports: port *N* at FFFF_FF00 + *N*, one instruction from `x0` (`sb a0,-255(zero)` writes port 1) |
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.
The ports (listed at the top of `rv32_system.v` and in the manual) cover the
console, the millisecond counter, the 7-segment display, graphics, sprites,
sound, the keys held down and the flash. The peripherals are `term_ctrl.v`
(the VT220), `text_display.v` (video), `sprites.v`, `sound.v`,
`flash_spi.v`, `uart.v`, `ps2_keyboard.v` and `sevenseg.v`.
Resources at 90 MHz: see `build/utilization.rpt` and `build/timing.rpt` after a
build.
The graphics memory is 76,800 four-bit entries: 320 × 240 pixels in 16
colours, or, with port 38 set, 640 × 480 pixels at one bit each (four to an
entry; a plot reads the entry and writes it back with one bit changed). To
fit the block RAM, the text screen and the graphics are each split into a
power-of-two array and a small one, because Vivado builds an array of 16,896
or 76,800 entries as 32K or 96K deep. That saves 10.5 block RAMs. The design
uses 49.5 of the XC7A35T's 50.
## Software
All in C (plus a little assembly), compiled with GCC for `rv32im`:
All in C (plus a little assembly), compiled with GCC:
- `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.
- `basic/` — BASIC, with single-precision arithmetic on the FPU (`rv32imf`,
`ilp32f`), correctly rounded. `lib/` has the few C library functions it
needs (small string functions, `setjmp`, and `sinf` … `sqrtf`), since
newlib's bring in its stdio and malloc.
- `monitor/` — the machine-code monitor: memory, registers (integer and
floating point), step, breakpoints, a RISC-V disassembler and a two-pass
assembler, Intel HEX in and out.
- `games/` — The Iron Keep, Bastion Raid, Star Warden, Star Swarm and Crystal
Cavern, recompiled; they load at A200h (`CALL 41472`).
Cavern; they load at A200h (`CALL 41472`).
| BASIC on the board | |
|---|---|
| `Y = SIN(X)` | 37 µs |
| `Y = EXP(X)` | 45 µs |
| `X = I * 1.5` | 26 µs |
| An empty `FOR` loop, each time round | 8.3 µs |
## Building
@@ -91,19 +185,15 @@ 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. |
| `scripts/sim.sh` | the whole design in xsim: the terminal, BASIC, both graphics modes, 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; `wide` checks the 132 × 64 start-up. |
| `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. |
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM, string arrays and graphics modes, 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
@@ -112,3 +202,7 @@ games/keep/keep.bas` and `SAVE "KEEP"` in BASIC.
- 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.
## License
MIT; see [LICENSE](LICENSE).