Z-80 computer with Dartmouth-style BASIC as the terminal's console

SW15 switches the terminal from the PC's serial line to a Z-80 running at
90 MHz from its own clock, with 64 KB of block RAM preloaded with BASIC.

- rtl/z80_cpu.v: Z-80 built for speed (one clock per memory access, next
  byte read during decode), full documented instruction set plus the
  common undocumented forms; no interrupts.
- rtl/z80_system.v: RAM, console/timer/7-segment ports, toggle handshakes
  to the terminal clock.
- basic/: BASIC interpreter in C for SDCC, start-up code, prebuilt image.
- tools/z80emu.py, tools/cputest.py, scripts/cpusim.sh: reference emulator
  and random tests comparing the CPU after every instruction.
- SW14 feeds the PC's UART into the Z-80 for pasting programs; BTND
  resets the Z-80 only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-27 20:25:41 -08:00
1 parent 1e4e2101b8
commit 59d5e617f2
20 files changed
+68756 -48

No files matched your search

+123 -3
View File
@@ -10,6 +10,9 @@ serial terminal with ANSI colour:
- **Serial line**: the micro-USB **PROG/UART** port (the FTDI chip), 8N1. Bytes
received there are drawn on the screen. Keystrokes are sent out there.
It also contains a **Z-80 computer running Dartmouth-style BASIC**. Flip SW15 up
and the terminal becomes its console (see [Z-80 BASIC](#z-80-basic)).
Plug the micro-USB into a PC and open the serial port (`/dev/ttyUSB1` is usually
the UART; `ttyUSB0` is JTAG). For example:
@@ -68,12 +71,15 @@ Set SW6 up so Backspace sends DEL, which the `basys3` entry expects.
| SW7 | Enter sends CR LF instead of CR |
| SW8 | Smooth scrolling (the host can also turn it on with `ESC[?4h`) |
| SW9 | Fast smooth scrolling: 15 lines/s instead of 7.5 |
| BTNC | Reset (clears screen, shows banner) |
| SW14 | Z-80 mode: text from the PC's serial port also goes to the Z-80 (for pasting programs) |
| SW15 | Z-80 mode: the terminal is the Z-80's console |
| BTNC | Reset (clears screen, shows banner, restarts the Z-80) |
| BTNU | Clear screen |
| BTND | Reset the Z-80 only |
| LED7:0 | Last byte received |
| LED11:8 | VT220 keyboard LEDs L4..L1 (`ESC[n q`) |
| LED12 / 13 / 14 / 15 | Bell / Caps Lock / UART RX activity / keyboard activity |
| 7-segment | Left: last keyboard scan code. Right: last byte sent. |
| 7-segment | Left: last keyboard scan code. Right: last byte sent. In Z-80 mode: whatever BASIC sends to ports 33/32 (`OUT 32,n`). |
## Terminal features
@@ -130,6 +136,107 @@ Set SW6 up so Backspace sends DEL, which the `basys3` entry expects.
non-UTF-8 locale such as `LANG=C`, or ASCII-only output, for clean line drawing.
- No 132-column mode, double-width/height lines, soft fonts, or printer port.
## Z-80 BASIC
Flip **SW15 up**. The Z-80 runs all the time; while the switch is down it simply
waits at its next character of output, so flipping back and forth loses
nothing. The first time, the BASIC banner and `READY` appear.
```
10 FOR I = 1 TO 10
20 PRINT I, SQR(I)
30 NEXT I
RUN
```
Type numbered lines to store a program, or a statement without a number to run
it at once (`PRINT 2^10`). Ctrl-C stops a running program; Backspace and Ctrl-U
edit the line being typed.
- **Commands**: `RUN [line]`, `LIST [a][-b]`, `NEW` (also `SCRATCH`).
- **Statements**:
- Assignment and output: `LET` (optional), `PRINT` (`,` for 15-column zones,
`;` to join, `TAB(n)`), `INPUT ["prompt";] vars`
- Control: `IF expr THEN line|statement`, `GOTO`, `GOSUB`/`RETURN`,
`ON expr GOTO|GOSUB list`, `FOR v = a TO b [STEP s]` / `NEXT [v]`, `END`,
`STOP`
- Data: `DIM`, `DEF FNx(v) = expr`, `READ`/`DATA`/`RESTORE`, `REM`
- Machine access: `RANDOMIZE`, `POKE addr,v`, `OUT port,v`
- Several statements can share a line, separated by `:`.
- **Variables**:
- Numbers: a letter or a letter and digit (`X`, `A1`), single-precision
floating point.
- Arrays: one letter, one or two subscripts from 0, 10 by default.
- Strings: `A$`..`Z$`, up to 79 characters.
- **Operators**: `+ - * / ^`, `= <> < > <= >=`, `AND OR NOT`.
- **Functions**:
- Numeric: `SIN COS TAN ATN EXP LOG ABS SQR INT RND SGN`, `FNx`
- Strings: `LEN VAL ASC`, `CHR$ STR$ LEFT$ RIGHT$ MID$`
- Machine: `PEEK(addr)`, `INP(port)`, `TIM` (seconds since reset)
- Spaces are optional, as at Dartmouth: `FORI=1TO10` works.
- `DATA` takes the rest of its line.
About 22 KB of program space is free. To paste a program from the PC, flip SW14
up too (stop the getty first, or it will talk to BASIC), then paste into
`picocom`. The terminal holds the PC back with XON/XOFF while BASIC catches up.
### The Z-80
`rtl/z80_cpu.v` is a Z-80 written for speed rather than cycle accuracy:
- Every memory access takes one clock from block RAM.
- The CPU reads the next byte while it decodes the current one, so a
register-to-register instruction takes 1 clock, `LD r,n` 2, `LD r,(HL)` 3,
`JP` 4, `CALL` 6, `RET` 5.
- `LDIR` moves a byte every 3 clocks.
- BASIC averages about 2.7 clocks per instruction. At 90 MHz that is about 33
million instructions a second, against roughly 0.6 million for a 4 MHz Z-80.
- It runs at 90 MHz from its own clock (an MMCM), the fastest that meets
timing. The limit is the path from the RAM through opcode decoding into the
registers.
- It has the documented instruction set plus the undocumented IXH/IXL/IYH/IYL
forms, `SLL` and the DDCB register copies, with the undocumented X/Y flags.
- There are no interrupts and HALT stops it until reset.
Memory is 64 KB of block RAM loaded with BASIC at configuration. Writes below
8000h are ignored, so the interpreter can't be damaged. BASIC's data and the
stack fill the top half.
| Port | In | Out |
|---|---|---|
| 00h | bit 0: key waiting, bit 1: console ready | |
| 01h | key | character to the screen |
| 10h–13h | millisecond counter (reading 10h latches the rest) | |
| 20h / 21h | | 7-segment display, right / left byte |
BASIC is C (`basic/basic.c`) compiled with SDCC (`sudo apt install sdcc`):
```bash
basic/build.sh # -> basic/basic.mem, loaded by the bitstream build
```
The same source builds natively for trying the interpreter on a PC
(`gcc basic/basic.c -lm`).
### Testing the CPU
`tools/z80emu.py` is an independent Z-80 emulator in Python.
`scripts/cpusim.sh` runs an image on the Verilog CPU in xsim and on the
emulator, then compares every register after every instruction and the RAM at
the end.
```bash
cd build/cputest && python3 ../../tools/cputest.py 1 1000 && cd ../..
scripts/cpusim.sh build/cputest/cputest.mem
```
- **Random tests**: `tools/cputest.py <seed>` writes 1000 blocks. Each loads
random registers, then runs one random instruction from the whole instruction
set (prefixes, memory forms and block moves included). 50 seeds pass.
- **BASIC**: `scripts/cpusim.sh basic/basic.mem input.txt` boots BASIC and runs
a program typed from `input.txt`. That is 1.1 million instructions, all
identical.
## Build and program
Requires Vivado. The scripts use the newest install in `~/vivado/<version>/Vivado`;
@@ -140,6 +247,13 @@ license, so use 2025.2 (`export VIVADO=~/vivado/2025.2/Vivado`).
./scripts/build.sh # bitstream -> build/basys3_terminal.bit (+ .mcs)
```
The Z-80 clock is `100 MHz × CPU_MULT / CPU_DIV`. The default is 9 / 10, which
gives 90 MHz, the fastest that meets timing. To try another speed:
```bash
CPU_MULT=8.5 CPU_DIV=10 ./scripts/build.sh
```
```bash
./scripts/build.sh program # build and load into the FPGA (until power-off)
```
@@ -162,6 +276,8 @@ older boards, Macronix `mx25l3273f` on newer ones).
scroll, then checks the screen buffer.
- It checks VGA sync timing and saves two frames, `sim/work/frame_1.png` and
`sim/work/frame_2.png` (the second is taken mid-scroll).
- It switches to Z-80 mode, types `PRINT 2+2` and a short program over the
UART, and checks BASIC's output on the screen.
```bash
VIVADO=~/vivado/2025.2/Vivado ./scripts/sim.sh
@@ -174,7 +290,11 @@ which Vivado needs on newer distros.
| Path | Contents |
|---|---|
| `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, flow control, banner, LEDs |
| `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, flow control, banner, LEDs, Z-80 clock |
| `rtl/z80_cpu.v` | Z-80 CPU |
| `rtl/z80_system.v` | Z-80 RAM, console and timer ports, clock-domain handshakes |
| `basic/` | BASIC interpreter (C), start-up code, build script, `basic.mem` |
| `tools/z80emu.py`, `tools/cputest.py`, `scripts/cpusim.sh` | CPU test bench: reference emulator, random tests |
| `rtl/term_ctrl.v` | VT220 engine (parser, modes, attributes, copy/fill engine, smooth scroll) |
| `rtl/text_display.v` | VGA timing, colour palette, character RAM, font lookup |
| `rtl/ps2_keyboard.v` | PS/2 receiver and scan code set 2 to ASCII / VT220 keys |