Flash file system for BASIC, with bitstream write protection

BASIC gets SAVE, LOAD, RUN "NAME", DIR and DEL, storing program listings
in the configuration flash from 240000h (448 x 4 KB blocks, 64 files,
32 KB each). The directory is kept in two alternating blocks with a
sequence number and checksum, new data is written before the directory
switches to it, and every page is verified after programming.

rtl/flash_spi.v drives the flash through STARTUPE2 on ports 40h/41h. It
only passes read, status, WREN/WRDI, RDID, page program and 4 KB erase,
and abandons any program or erase below 240000h before the address
reaches the chip. Tested on the board: an erase at 0 and a write at
200000h were refused and left the flash unchanged.

With SW14 up, the Z-80's output is also copied to the PC's serial port,
and tools/get-bas.py uses that to LIST a program into a file or run
commands and print the replies.

Also fixes port 10h returning the previous millisecond latch (the first
read after reset was undefined). Simulation adds a model flash and a
save/load/run/protection test. Manual and README updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-28 08:33:52 -08:00
1 parent dc7c7c22c5
commit 29a15bf7c7
15 files changed
+6907 -6056

No files matched your search

+51 -10
View File
@@ -73,7 +73,7 @@ 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 |
| SW14 | Z-80 mode: text from the PC's serial port also goes to the Z-80 (for pasting programs) |
| SW14 | Z-80 mode: link the PC's serial port to the Z-80 too (PC input is typed into BASIC, BASIC's output is copied to the PC) |
| 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 |
@@ -156,6 +156,8 @@ 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`).
- **Files in flash**: `SAVE "NAME"`, `LOAD "NAME"`, `RUN "NAME"`, `DIR`,
`DEL "NAME"`.
- **Statements**:
- Assignment and output: `LET` (optional), `PRINT` (`,` for 15-column zones,
`;` to join, `TAB(n)`), `INPUT ["prompt";] vars`
@@ -192,17 +194,48 @@ drawn on top, and the graphics show through blank text.
statement and function, with examples, error messages and limits.
`docs/build-manual.sh` rebuilds the PDF from it.
**Loading programs from the PC:**
**Saving programs on the board:** BASIC keeps programs in the configuration
flash above the bitstream, from 240000h to the end of the 4 MB chip. There is
room for 64 programs, 1,784 KB in all and up to 32 KB of listing each, and they
survive power-off.
1. Flip SW14 and SW15 up.
2. Stop the getty: `sudo systemctl stop serial-getty@ttyUSB1`.
3. Send the program at the SW1:0 baud rate:
- **Layout:** the area is 448 blocks of 4 KB. Blocks 0 and 1 hold the
directory, written alternately with a sequence number and checksum, so a
power cut mid-save leaves the previous directory, and the previous version of
the program, in place. The other blocks hold program listings.
- **Bitstream protection:** the flash port (`rtl/flash_spi.v`) only passes
read, status, write enable/disable, ID, page program and 4 KB erase. It drops
chip select before the address of any program or erase below 240000h reaches
the chip, so nothing the Z-80 runs can damage the bitstream. Tested on the
board: an erase at 0 and a write at 200000h were both refused and left the
flash unchanged.
- **Reflashing keeps programs:** `scripts/build.sh flash` erases only the
bitstream's range (about 1.2 MB, compressed), so saved programs survive.
```bash
tools/send-bas.py basic/examples/graphics-demo.bas --baud 9600 --run
```
**Programs on the PC:** with SW14 and SW15 up, the PC's serial port is linked to
BASIC both ways. First stop the getty for this boot: it would read BASIC's
output as login names and type its replies back into BASIC, in a loop.
The terminal holds the PC back with XON/XOFF while BASIC catches up.
```bash
sudo systemctl mask --runtime serial-getty@ttyUSB1
sudo systemctl stop serial-getty@ttyUSB1
```
Then, at the SW1:0 baud rate:
```bash
tools/send-bas.py basic/examples/graphics-demo.bas --baud 9600 --run # type a program in
tools/get-bas.py myprog.bas --baud 9600 # LIST it into a file
tools/get-bas.py --baud 9600 --cmd 'DIR' --cmd 'SAVE "DEMO"' # run commands, show replies
```
If the port is left root-only (`crw-------`) after stopping the getty,
`sudo chmod 666 /dev/ttyUSB1` opens it. `sudo systemctl unmask --runtime
serial-getty@ttyUSB1` (or a reboot) brings the getty back; flip SW14 down
first.
The terminal holds the PC back with XON/XOFF while BASIC catches up, and the
Z-80 waits when the copy to the PC falls behind.
`basic/examples/graphics-demo.bas` shows off the graphics: colour bars,
circles, lines, curves, random shapes, a bouncing ball and a Mandelbrot set.
@@ -242,6 +275,8 @@ stack fill the top half.
| 30h, 31h, 32h | | graphics X low byte, X bit 8, Y |
| 33h | colour at X,Y | plot at X,Y, then X+1 |
| 34h | bit 0: fill busy | fill the whole graphics layer with a colour |
| 40h | bit 0: SPI byte busy, bit 1: command refused | 1 starts a flash command (chip select low), 0 ends it |
| 41h | byte received during the last transfer | send a byte to the flash |
BASIC is C (`basic/basic.c`) compiled with SDCC (`sudo apt install sdcc`):
@@ -312,6 +347,10 @@ older boards, Macronix `mx25l3273f` on newer ones).
`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.
- It draws graphics from BASIC and checks the pixels.
- Against a model flash (`sim/spiflash_model.v`) it saves, loads, runs and
lists a program, then tries to erase address 0 and checks the model saw no
write below 240000h.
```bash
VIVADO=~/vivado/2025.2/Vivado ./scripts/sim.sh
@@ -326,7 +365,9 @@ which Vivado needs on newer distros.
|---|---|
| `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 |
| `rtl/z80_system.v` | Z-80 RAM, console, timer, graphics and flash ports |
| `rtl/flash_spi.v` | SPI port to the configuration flash, with bitstream write protection |
| `tools/send-bas.py`, `tools/get-bas.py` | Send a BASIC program to the board / copy one back, run commands |
| `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) |