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

+102 -9
View File
@@ -11,7 +11,7 @@ and keyboard belong to:
| Switch | Down | Up |
|---|---|---|
| SW15 | Terminal for the PC's serial port | Console of the Z-80 |
| SW14 | — | Also pass text from the PC's serial port to the Z-80 (for loading programs) |
| SW14 | — | Link the PC's serial port to the Z-80 as well: what the PC sends is typed in, and what BASIC prints is also sent to the PC |
Flip **SW15 up**. The first time, BASIC greets you:
@@ -66,10 +66,10 @@ case, except inside quotes.
| `LIST 100` | List line 100 |
| `LIST 100-200` | List lines 100 to 200 (`LIST -200` and `LIST 100-` work too) |
| `NEW` or `SCRATCH` | Erase the program and all variables |
There is no `SAVE` or `LOAD`: the board has no storage for programs. Keep them
as text files on the PC and send them in (section 10). `LIST` shows the program
on the screen, so you can copy it from a PC terminal.
| `SAVE "NAME"` | Keep the program in the board's flash (section 10) |
| `LOAD "NAME"` | Bring a saved program back |
| `RUN "NAME"` | Load a saved program and run it |
| `DIR` / `DEL "NAME"` | List the saved programs / delete one |
## 2. Program lines
@@ -482,6 +482,8 @@ BASIC's own data. Poke with care.
| 48, 49, 50 | — | graphics X (low byte, high bit), Y |
| 51 | colour at X, Y | plot a pixel at X, Y and move right |
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour |
| 64 | bit 0: flash byte in progress; bit 1: last flash command refused | 1 selects the flash and starts a command, 0 ends it |
| 65 | the byte the flash sent back during the last `OUT 65` | a byte to the flash |
```
10 FOR I = 0 TO 255
@@ -514,7 +516,91 @@ Constants in a program are converted from text every time they are used, so a
loop runs a little faster if a constant used inside it is put into a variable
first.
## 10. Loading programs from a PC
## 10. Saving programs
### In the board's flash
The board's configuration flash has room for programs next to the bitstream it
boots from. They stay there when the power is off.
| Command | Does |
|---|---|
| `SAVE "NAME"` | Write the program to flash, replacing any saved program with that name |
| `LOAD "NAME"` | Erase the program in memory and read the saved one in its place |
| `RUN "NAME"` | `LOAD` it, then run it |
| `DIR` | List the saved programs with their sizes in bytes, and the free space |
| `DEL "NAME"` | Delete a saved program |
```
SAVE "SPIRO"
DIR
SPIRO 212
1 FILE, 1780K FREE
```
- Names are 1 to 8 letters and digits; lower case becomes upper case. The name
can be any string expression, so `SAVE N$` works.
- Up to 64 programs, 1,784 KB in all. A program is saved as its listing, in 4 KB
blocks; one can use at most 8 blocks (32 KB of listing).
- Variables aren't saved, and `LOAD` clears them.
- Each block is read back and checked after it is written.
- If the power goes off while saving, you keep the old version of that program:
the new copy is written to free space first and the list of programs is
switched over to it last.
The flash also holds the bitstream the board starts from. The hardware refuses
to erase or write anything below the area kept for programs, so no program can
stop the board from starting, even one that sends commands to the flash itself
with `OUT` (ports 64 and 65, section 9).
## 11. Programs on the PC
Programs can also be kept as plain text files on the PC, one numbered line per
line. SW14 links the PC's serial port to the Z-80: what the PC sends is typed
into BASIC, and what BASIC prints is also sent back to the PC. The tools need:
1. **SW15** and **SW14** up.
2. Nothing else using the serial port. A login getty on it is worse than in the
way: it takes BASIC's output for login names and its "Login incorrect"
replies are typed back into BASIC, round and round. Keep it off until the
next reboot with:
```
sudo systemctl mask --runtime serial-getty@ttyUSB1
sudo systemctl stop serial-getty@ttyUSB1
```
If the port is then only open to root (`ls -l /dev/ttyUSB1`), the getty left
it that way; `sudo chmod 666 /dev/ttyUSB1` opens it again.
3. The baud rate set by SW1:0 (SW0 up is 9600).
**Sending a program to the board:**
```
tools/send-bas.py basic/examples/graphics-demo.bas --baud 9600 --run
```
This sends `NEW`, then the program, then `RUN`. Leave out `--run` to only load
it, and add `--keep` to merge it into the program already there. Pasting into a
terminal program such as `picocom` works too.
**Copying the program in memory to the PC:**
```
tools/get-bas.py myprog.bas --baud 9600
```
**Running a command and seeing the answer on the PC:**
```
tools/get-bas.py --baud 9600 --cmd 'DIR' --cmd 'LOAD "SPIRO"' --cmd 'LIST'
```
`--cmd` can be repeated; each one waits for `READY` (up to `--timeout`
seconds, 30 by default). Program lines typed this way get no `READY`, so they
take the full timeout each; use `send-bas.py` for those.
When you're done, flip SW14 back down, and bring the getty back with
`sudo systemctl unmask --runtime serial-getty@ttyUSB1` (a reboot does it too).
Don't do that with SW14 up.
Keep programs as plain text files with one numbered line per line. To type one
into the board:
@@ -536,7 +622,7 @@ into the board:
Pasting into a terminal program such as `picocom` works too.
## 11. Error messages
## 12. Error messages
An error stops the program and reports the line, for example
`?DIVISION BY ZERO IN 120`.
@@ -557,10 +643,16 @@ An error stops the program and reports the line, for example
| `UNDEFINED FUNCTION` | `FNx` used before its `DEF` |
| `FOR NESTED TOO DEEPLY` | More than 16 loops open |
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |
| `FILE NOT FOUND` | `LOAD`, `RUN` or `DEL` of a name not in `DIR` |
| `BAD FILE NAME` | A name that is empty, longer than 8 or not just letters and digits |
| `DISK FULL` | Not enough free flash for the program |
| `TOO MANY FILES` | 64 programs are saved already |
| `PROGRAM TOO BIG TO SAVE` | The listing is over 32 KB |
| `FLASH ERROR` | The flash didn't answer, or read back wrong after writing |
| `BREAK IN n` | You pressed Ctrl-C |
| `STOP IN n` | The program ran `STOP` |
## 12. Example programs
## 13. Example programs
### Times table
@@ -604,7 +696,7 @@ An error stops the program and reports the line, for example
bars, circles, a burst of lines, sine and cosine curves, random rectangles, a
bouncing ball and a Mandelbrot set, then prints how long it all took.
## 13. Limits at a glance
## 14. Limits at a glance
| Item | Limit |
|---|---|
@@ -618,3 +710,4 @@ bouncing ball and a Mandelbrot set, then prints how long it all took.
| FOR nesting / GOSUB nesting | 16 / 32 |
| Graphics | 320 × 240, 16 colours |
| Text | 80 × 30 |
| Saved programs | 64, 1,784 KB in all, 32 KB of listing each |