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

+43 -4
View File
@@ -13,7 +13,8 @@
// SW7 Enter sends CR LF instead of CR
// SW8 smooth scrolling (also turned on by ESC [ ? 4 h)
// SW9 fast smooth scrolling
// SW14 in Z-80 mode, bytes from the PC's UART also go to the Z-80 (paste programs)
// SW14 in Z-80 mode, link the PC's UART too: bytes from the PC also go to the
// Z-80, and the Z-80's output is copied to the PC
// SW15 Z-80 mode
// BTNC reset BTNU clear screen BTND reset the Z-80 only
// LED7:0 last byte received LED11:8 VT220 LEDs L4..L1 (DECLL)
@@ -49,7 +50,9 @@ module basys3_terminal #(
input RsRx,
output RsTx,
input PS2Clk,
input PS2Data
input PS2Data,
output QspiCSn,
inout [3:0] QspiDB
);
localparam [7:0] XON = 8'h11, XOFF = 8'h13;
@@ -156,6 +159,19 @@ module basys3_terminal #(
.rd(zin_rd), .dout(zin), .empty(zin_empty), .full(zin_full), .count(zin_count)
);
// Z-80 output copied to the PC when linked
wire [7:0] mq;
wire mq_empty;
wire [10:0] mq_count;
reg mq_rd;
wire z80_out; // a Z-80 byte is going to the screen
wire [7:0] z80_out_byte;
fifo #(.W(8), .A(10)) mirror_fifo (
.clk(clk), .rst(rst), .din(z80_out_byte), .wr(z80_out && z80_link),
.rd(mq_rd), .dout(mq), .empty(mq_empty), .full(), .count(mq_count)
);
wire [10:0] tq_count;
wire [10:0] flow_count = z80_mode ? zin_count : tq_count;
reg xoff_sent;
@@ -164,6 +180,7 @@ module basys3_terminal #(
tx_start <= 1'b0;
kq_rd <= 1'b0;
rq_rd <= 1'b0;
mq_rd <= 1'b0;
if (rst) begin
from_resp <= 1'b0;
flow_pend <= 1'b0;
@@ -191,6 +208,10 @@ module basys3_terminal #(
tx_start <= 1'b1;
kq_rd <= !from_resp;
rq_rd <= from_resp;
end else if (z80_mode && !mq_empty && !mq_rd) begin
tx_byte <= mq;
tx_start <= 1'b1;
mq_rd <= 1'b1;
end
end
end
@@ -268,6 +289,7 @@ module basys3_terminal #(
always @(posedge clk) ztx_req_s <= {ztx_req_s[0], ztx_req};
wire [15:0] z80_seg;
wire fl_sck;
wire [16:0] gfx_addr;
wire [3:0] gfx_pix;
@@ -276,9 +298,22 @@ module basys3_terminal #(
.rx_req(zrx_req), .rx_data(zrx_data), .rx_ack(zrx_ack),
.tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack),
.seg_val(z80_seg), .halted(),
.vclk(clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix)
.vclk(clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1])
);
// The flash clock is the configuration clock pin, reached through STARTUPE2.
// DQ2/DQ3 (write protect, hold) stay high.
assign QspiDB[1] = 1'bz;
assign QspiDB[3:2] = 2'b11;
`ifndef SIMULATION
STARTUPE2 #(.PROG_USR("FALSE"), .SIM_CCLK_FREQ(0.0)) startup (
.CFGCLK(), .CFGMCLK(), .EOS(), .PREQ(),
.CLK(1'b0), .GSR(1'b0), .GTS(1'b0), .KEYCLEARB(1'b1), .PACK(1'b0),
.USRCCLKO(fl_sck), .USRCCLKTS(1'b0), .USRDONEO(1'b1), .USRDONETS(1'b1)
);
`endif
// terminal input: Z-80 output or received bytes, local echo, banner
reg echo_pend;
reg [7:0] echo_byte;
@@ -286,6 +321,10 @@ module basys3_terminal #(
reg tin_wr;
wire tin_full;
assign z80_out = z80_mode && ztx_req_s[1] != ztx_ack && !tin_wr && tq_count < 1000 &&
(!z80_link || mq_count < 1000);
assign z80_out_byte = ztx_data;
always @(posedge clk) begin
tin_wr <= 1'b0;
if (kq_rd && sw_r[2] && !z80_mode) begin
@@ -296,7 +335,7 @@ module basys3_terminal #(
echo_pend <= 1'b0;
ban_i <= z80_mode ? 8'hFF : 8'd199;
end else if (z80_mode) begin
if (ztx_req_s[1] != ztx_ack && !tin_wr && tq_count < 1000) begin
if (z80_out) begin
tin <= ztx_data;
tin_wr <= 1'b1;
ztx_ack <= ~ztx_ack;
+100
View File
@@ -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
+20 -2
View File
@@ -12,6 +12,7 @@
// 30 out graphics X bits 7:0 31 out X bit 8 32 out Y
// 33 out plot colour at (X,Y), then X+1 in colour at (X,Y)
// 34 out fill the whole graphics screen in bit 0 = fill busy
// 40 SPI flash control, 41 SPI flash data (see flash_spi.v)
//
// Graphics are 320x240 in 16 colours (0 = transparent), shown over the text.
// Writes below ROM_TOP are ignored so a crashed program cannot damage BASIC.
@@ -35,7 +36,12 @@ module z80_system #(
// graphics layer read port, on the video clock
input vclk,
input [16:0] gfx_vaddr,
output reg [3:0] gfx_vpix
output reg [3:0] gfx_vpix,
// configuration flash (SPI)
output fl_cs_n,
output fl_sck,
output fl_mosi,
input fl_miso
);
wire [15:0] ma, io_addr;
wire we, io_rd, io_wr;
@@ -105,6 +111,15 @@ module z80_system #(
end
end
// --------------------------------------------------------------- flash
wire [7:0] fl_ctl_q, fl_dat_q;
flash_spi flash (
.clk(clk), .rst(rst),
.wr_ctl(io_wr && io_addr[7:0] == 8'h40), .wr_dat(io_wr && io_addr[7:0] == 8'h41),
.wdata(io_wd), .ctl_q(fl_ctl_q), .dat_q(fl_dat_q),
.cs_n(fl_cs_n), .sck(fl_sck), .mosi(fl_mosi), .miso(fl_miso)
);
// millisecond timer
localparam MS_DIV = CPU_HZ / 1000;
reg [31:0] ms, ms_latch;
@@ -120,12 +135,14 @@ module z80_system #(
always @* case (io_addr[7:0])
8'h00: io_din = {6'd0, tx_ready, rx_avail};
8'h01: io_din = rx_data;
8'h10: io_din = ms_latch[7:0];
8'h10: io_din = ms[7:0]; // sampled on the same edge that fills ms_latch
8'h11: io_din = ms_latch[15:8];
8'h12: io_din = ms_latch[23:16];
8'h13: io_din = ms_latch[31:24];
8'h33: io_din = {4'd0, gfx_q};
8'h34: io_din = {7'd0, fill_busy};
8'h40: io_din = fl_ctl_q;
8'h41: io_din = fl_dat_q;
default: io_din = 8'hFF;
endcase
@@ -134,6 +151,7 @@ module z80_system #(
rx_ack <= rx_req_s[1];
tx_req <= tx_ack_s[1];
seg_val <= 16'h2080;
ms_latch <= 32'd0;
end else begin
if (io_rd && io_addr[7:0] == 8'h01 && rx_avail) rx_ack <= ~rx_ack;
if (io_rd && io_addr[7:0] == 8'h10) ms_latch <= ms;