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:
1 parent
dc7c7c22c5
commit
29a15bf7c7
15 files changed
+6907
-6056
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user