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
+100
@@ -0,0 +1,100 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// SPI port to the configuration flash for the Z-80, with write protection for
|
||||
// the bitstream.
|
||||
//
|
||||
// port 40h out: bit 0 = 1 opens a command, 0 ends it (chip select high)
|
||||
// in: bit 0 = byte in progress, bit 1 = command refused
|
||||
// port 41h out: send a byte (the reply byte is read back from 41h)
|
||||
//
|
||||
// The first byte of a command is checked before the chip is selected. Only
|
||||
// read (03), read status (05), write enable/disable (06/04), read ID (9F), page
|
||||
// program (02) and 4 KB sector erase (20) are allowed. Program and erase are
|
||||
// abandoned after the opcode, before any address byte reaches the flash,
|
||||
// unless the address is at or above PROTECT (a 64 KB boundary), so the
|
||||
// bitstream below it can never be changed by the Z-80.
|
||||
//
|
||||
// SCK is clk/4, SPI mode 0.
|
||||
module flash_spi #(
|
||||
parameter [7:0] PROTECT_HI = 8'h24 // first writable address = PROTECT_HI << 16
|
||||
) (
|
||||
input clk,
|
||||
input rst,
|
||||
input wr_ctl, // port 40h written
|
||||
input wr_dat, // port 41h written
|
||||
input [7:0] wdata,
|
||||
output [7:0] ctl_q, // port 40h read value
|
||||
output [7:0] dat_q, // port 41h read value
|
||||
output reg cs_n,
|
||||
output reg sck,
|
||||
output mosi,
|
||||
input miso
|
||||
);
|
||||
reg [7:0] sh;
|
||||
reg [3:0] nbits;
|
||||
reg [1:0] ph;
|
||||
reg busy, rbit;
|
||||
reg open_, refused, first, guarded;
|
||||
reg [1:0] nbytes;
|
||||
|
||||
assign mosi = sh[7];
|
||||
assign ctl_q = {6'd0, refused, busy};
|
||||
assign dat_q = sh;
|
||||
|
||||
(* IOB = "TRUE" *) reg miso_r;
|
||||
always @(posedge clk) miso_r <= miso;
|
||||
|
||||
task start(input [7:0] b, input [3:0] n);
|
||||
begin sh <= b; nbits <= n; ph <= 2'd0; busy <= 1'b1; end
|
||||
endtask
|
||||
|
||||
always @(posedge clk)
|
||||
if (rst) begin
|
||||
cs_n <= 1'b1; sck <= 1'b0; open_ <= 1'b0; refused <= 1'b0;
|
||||
start(8'hFF, 4'd8); // a few clocks so STARTUPE2 hands over CCLK
|
||||
end else begin
|
||||
if (busy) begin
|
||||
ph <= ph + 1'b1;
|
||||
case (ph)
|
||||
2'd1: sck <= 1'b1;
|
||||
2'd2: rbit <= miso_r;
|
||||
2'd3: begin
|
||||
sck <= 1'b0;
|
||||
sh <= {sh[6:0], rbit};
|
||||
nbits <= nbits - 1'b1;
|
||||
if (nbits == 4'd1) busy <= 1'b0;
|
||||
end
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
if (wr_ctl && !busy) begin
|
||||
open_ <= wdata[0];
|
||||
first <= 1'b1;
|
||||
nbytes <= 2'd0;
|
||||
refused <= 1'b0;
|
||||
if (!wdata[0]) cs_n <= 1'b1;
|
||||
end else if (wr_dat && !busy && open_) begin
|
||||
if (nbytes != 2'd3) nbytes <= nbytes + 1'b1;
|
||||
if (refused)
|
||||
sh <= 8'hFF;
|
||||
else if (first) begin
|
||||
first <= 1'b0;
|
||||
guarded <= wdata == 8'h02 || wdata == 8'h20;
|
||||
if (wdata == 8'h03 || wdata == 8'h05 || wdata == 8'h06 || wdata == 8'h04 ||
|
||||
wdata == 8'h9F || wdata == 8'h02 || wdata == 8'h20) begin
|
||||
cs_n <= 1'b0;
|
||||
start(wdata, 4'd8);
|
||||
end else begin
|
||||
refused <= 1'b1;
|
||||
sh <= 8'hFF;
|
||||
end
|
||||
end else if (nbytes == 2'd1 && guarded && wdata < PROTECT_HI) begin
|
||||
refused <= 1'b1; // incomplete command: the flash ignores it
|
||||
cs_n <= 1'b1;
|
||||
sh <= 8'hFF;
|
||||
end else
|
||||
start(wdata, 4'd8);
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
Reference in new issue
Block a user