Files
FPGA-Neural/sim/crc32_tb.v
T
micheleandClaude Sonnet 5 97a21be240 feat: flash boot/persistence subsystem (SPI master, copy engine, CRC32 slot catalog)
Adds FPGA-exclusive access to the onboard W25Q128JV SPI NOR flash for
weights/bias/network persistence, layered as spi_flash_master (raw SPI,
USRMCLK-driven) -> flash_copy_engine (flash<->PSRAM streaming, erase-
before-write, Page Program loop) -> flash_slot_manager (16-slot catalog
with CRC32), exposed via 8 new SPI opcodes (0x40-0x47). Fixes two
pre-existing bugs found during bring-up: a psram_controller.v request
lost during power-up, and a one-cycle-pulse race in the PSRAM arbiter
request handshake. Full simulation + real Yosys/nextpnr-ecp5 synthesis
verification (0 errors, Fmax 66.68MHz) in WORKLOG.md and
docs/FPGA-Neural-Flash-Subsystem-Verification.md.

Also updates docs/pinout to reflect the 56-signal real .lpf (3 new
flash pins) and documents the WRITE_RAM/READ_RAM host backpressure
risk found while testing this subsystem.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013xXuuRUWZScuo1DeYJxs3v
2026-09-04 08:37:19 +02:00

94 lines
3.3 KiB
Verilog

`timescale 1ns/1ps
// ================================================================
// CRC32_BYTE TESTBENCH
//
// Oracle: tools/flash_catalog/oracle.py's crc32() (Python stdlib
// zlib.crc32) -- an independent implementation, not derived from
// rtl/crc32.v. Expected values below were generated by running that
// script (see its own __main__ block and WORKLOG.md's F4 entry for
// the exact invocation), not by reading rtl/crc32.v's algorithm and
// re-deriving them by hand.
// ================================================================
module tb;
reg [31:0] crc_in;
reg [7:0] data;
wire [31:0] crc_out;
crc32_byte dut (.crc_in(crc_in), .data(data), .crc_out(crc_out));
integer errors;
integer i;
reg [31:0] crc;
task automatic check(input [31:0] got, input [31:0] exp, input [255:0] label);
begin
if (got !== exp) begin
$display("FAIL: %0s got=%08h exp=%08h", label, got, exp);
errors = errors + 1;
end else begin
$display("PASS: %0s = %08h", label, got);
end
end
endtask
initial begin
errors = 0;
// TEST 1: CRC32 of the empty message is, by definition,
// 0x00000000 (init 0xFFFFFFFF XOR final 0xFFFFFFFF, no bytes
// processed) -- a fixed, well-known property of this exact
// algorithm, not something derived from either implementation.
check(32'hFFFFFFFF ^ 32'hFFFFFFFF, 32'h00000000, "TEST1 empty-message identity");
// TEST 2: single byte 0x00. Oracle (Python):
// >>> zlib.crc32(bytes([0x00])) -> 0xd202ef8d
crc = 32'hFFFFFFFF;
#1 crc_in = crc; data = 8'h00; #1;
crc = crc_out;
check(crc ^ 32'hFFFFFFFF, 32'hd202ef8d, "TEST2 single byte 0x00");
// TEST 3: ASCII "123456789" (the standard CRC32 check-value
// vector quoted by every CRC32 reference table, e.g.
// Rocksoft's "check value" for CRC-32/ISO-HDLC = 0xCBF43926
// -- an independent, textbook-published constant, not
// computed by either this RTL or the Python oracle).
begin : t3
reg [7:0] msg [0:8];
msg[0]=8'h31; msg[1]=8'h32; msg[2]=8'h33; msg[3]=8'h34; msg[4]=8'h35;
msg[5]=8'h36; msg[6]=8'h37; msg[7]=8'h38; msg[8]=8'h39;
crc = 32'hFFFFFFFF;
for (i = 0; i < 9; i = i + 1) begin
crc_in = crc; data = msg[i]; #1;
crc = crc_out;
#1;
end
check(crc ^ 32'hFFFFFFFF, 32'hCBF43926, "TEST3 \"123456789\" textbook check-value");
end
// TEST 4: 32-byte payload 0x10..0x2F, matching
// tools/flash_catalog/oracle.py's __main__ self-check output
// exactly (run: `python3 tools/flash_catalog/oracle.py`) --
// oracle printed CRC32 = 0x3b8cfe40 for this exact payload.
begin : t4
crc = 32'hFFFFFFFF;
for (i = 0; i < 32; i = i + 1) begin
crc_in = crc; data = 8'h10 + i[7:0]; #1;
crc = crc_out;
#1;
end
check(crc ^ 32'hFFFFFFFF, 32'h3b8cfe40, "TEST4 32B payload vs Python oracle.py");
end
if (errors == 0)
$display("ALL TESTS PASSED");
else
$display("FAILED: %0d error(s)", errors);
$finish;
end
endmodule