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

+82
View File
@@ -0,0 +1,82 @@
`timescale 1ns / 1ps
// Behavioural SPI flash (mode 0) with the commands the Z-80 uses:
// 03 read, 05 status, 06/04 write enable/disable, 9F ID (Macronix MX25L3273F),
// 02 page program, 20 4 KB erase. Reports any program or erase below PROTECT.
module spiflash_model #(
parameter PROTECT = 24'h240000
) (
input cs_n,
input sck,
input mosi,
output reg miso = 1'b1
);
reg [7:0] mem [0:(1 << 22) - 1];
reg [7:0] in_sh, out_sh, cmd;
reg [7:0] pagebuf [0:255];
reg [23:0] addr;
reg [2:0] bitn;
integer nbytes, ndata, i;
reg wel = 1'b0;
realtime busy_until = 0;
integer violations = 0, programs = 0, erases = 0;
initial for (i = 0; i < (1 << 22); i = i + 1) mem[i] = 8'hFF;
always @(negedge cs_n) begin
bitn = 0; nbytes = 0; ndata = 0; out_sh = 8'hFF;
end
always @(posedge sck) if (!cs_n) begin
in_sh = {in_sh[6:0], mosi};
bitn = bitn + 1;
if (bitn == 0) begin // a whole byte
if (nbytes == 0) cmd = in_sh;
else if (nbytes <= 3) addr = {addr[15:0], in_sh};
else if (cmd == 8'h02 && ndata < 256) begin pagebuf[ndata] = in_sh; ndata = ndata + 1; end
nbytes = nbytes + 1;
case (cmd)
8'h03: out_sh = nbytes >= 4 ? mem[addr + nbytes - 4] : 8'hFF;
8'h05: out_sh = {6'd0, wel, $realtime < busy_until};
8'h9F: out_sh = nbytes == 1 ? 8'hC2 : nbytes == 2 ? 8'h20 : 8'h16;
default: out_sh = 8'hFF;
endcase
end
end
always @(negedge sck) if (!cs_n) begin
miso = out_sh[7];
out_sh = {out_sh[6:0], 1'b1};
end
task busy_for(input integer ns);
busy_until = $realtime + ns;
endtask
always @(posedge cs_n) if (bitn == 0) begin
case (cmd)
8'h06: wel = 1'b1;
8'h04: wel = 1'b0;
8'h02: if (wel && nbytes >= 5) begin
if (addr < PROTECT) begin
$display("FLASH VIOLATION: program at %h", addr); violations = violations + 1;
end
for (i = 0; i < ndata; i = i + 1)
mem[{addr[23:8], addr[7:0] + i[7:0]}] = mem[{addr[23:8], addr[7:0] + i[7:0]}] & pagebuf[i];
programs = programs + 1;
wel = 1'b0;
busy_for(3000);
end
8'h20: if (wel && nbytes == 4) begin
if (addr < PROTECT) begin
$display("FLASH VIOLATION: erase at %h", addr); violations = violations + 1;
end
for (i = 0; i < 4096; i = i + 1) mem[{addr[23:12], 12'd0} + i] = 8'hFF;
erases = erases + 1;
wel = 1'b0;
busy_for(20000);
end
default: ;
endcase
end
endmodule
+24 -1
View File
@@ -14,13 +14,18 @@ module tb_terminal;
wire rstx, hs, vs;
wire [3:0] r, g, b;
wire [15:0] led;
wire qcs;
wire [3:0] qdb;
basys3_terminal #(.FONT_FILE(`FONT_FILE), .MEM_FILE(`MEM_FILE)) dut (
.clk(clk), .btnC(btnC), .btnU(btnU), .btnD(btnD), .sw(sw), .led(led), .seg(), .dp(), .an(),
.vgaRed(r), .vgaGreen(g), .vgaBlue(b), .Hsync(hs), .Vsync(vs),
.RsRx(rsrx), .RsTx(rstx), .PS2Clk(ps2c), .PS2Data(ps2d)
.RsRx(rsrx), .RsTx(rstx), .PS2Clk(ps2c), .PS2Data(ps2d),
.QspiCSn(qcs), .QspiDB(qdb)
);
spiflash_model flash (.cs_n(qcs), .sck(dut.fl_sck), .mosi(qdb[0]), .miso(qdb[1]));
localparam BIT_NS = 1_000_000_000 / 115200;
integer errors = 0;
@@ -280,6 +285,24 @@ module tb_terminal;
end
capture_frame("frame_3.ppm");
// ---- files in the flash, and the bitstream protection
uart_str({"CLS:NEW\01510 PRINT \"SAVED\";6*7\015SAVE \"TEST1\"\015NEW\015",
"LOAD \"TEST1\"\015RUN\015DIR\015"});
#20_000_000;
expect_row(13, "SAVED 42");
expect_row(17, "TEST1 21");
expect_row(18, "1 FILE, 1780K FREE");
uart_str("OUT 64,1:OUT 65,6:OUT 64,0:OUT 64,1:OUT 65,32:OUT 65,0:OUT 65,0:OUT 65,0:OUT 64,0\015");
#3_000_000;
if (flash.programs > 0 && flash.erases > 0 && flash.violations == 0)
$display("PASS flash: %0d programs, %0d erases, bitstream untouched",
flash.programs, flash.erases);
else begin
$display("FAIL flash: %0d programs, %0d erases, %0d violations",
flash.programs, flash.erases, flash.violations);
errors = errors + 1;
end
dump_screen;
if (errors == 0) $display("ALL TESTS PASSED");
else $display("%0d TESTS FAILED", errors);