Files
Basys3RiscV/sim/spiflash_model.v
T
brad ArantandClaude Opus 5.5 5930b958b4 Basys 3 RISC-V computer
The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:09:18 -08:00

83 lines
2.8 KiB
Verilog

`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