Files
FPGA-Neural/hardware/v1/sim/layer_sequencer_bug005_zero_layers_tb.v
micheleandClaude Sonnet 5 dc0b331d3e feat(v2): scaffold hardware/v1 frozen baseline + M1 Neural Processor
Begins the V2 Neural Multiprocessor / Dataflow architecture per
docs/v2-description.md, per explicit user request to freeze V1 and
start V2 development, copying from V1 what's needed.

Scaffold:
- hardware/v1/: byte-exact, read-only copy of the current V1 codebase
  (rtl, testbenches, tools, constraints, a representative subset of
  synthesis results, and reference docs) -- verified identical via
  diff/cmp against the live top-level tree before being made
  filesystem-read-only. The live top-level tree is untouched and
  remains the project's "production" V1 (see hardware/v1/README.md
  and hardware/v2/logs/decisions.log DEC-0001 for why copy-not-move).
- hardware/v2/: mandatory structure (rtl/sim/constraints/synthesis/
  reports/scripts/logs/docs) plus the full logging system required by
  the spec (development/architecture/simulation/synthesis/timing/
  benchmark/decisions/experiments/errors.log).

M1 -- Neural Processor (hardware/v2/rtl/neural_processor.v):
- 8-stage pipelined perceptron unit (P_IN=8): input align, 8
  multipliers, 3-level adder tree, accumulator, bias+activation, INT8
  saturation. Genuine 1-tile/cycle throughput, not just a wider
  combinational datapath.
- 7-state FSM (NP_IDLE..NP_ERROR per docs/v2-description.md §6, with
  4 baseline states merged into NP_WAIT_OPERANDS -- see
  decisions.log DEC-0002); valid/ready/data/last stream interfaces
  per §7.
- Bit-exact vs the frozen hardware/v1/rtl/neuron_parallel.v + mac8.v
  + mac_unit.v: 7/7 tests pass (hardware/v2/sim/tb_neural_processor.v),
  covering regular/mixed-sign/extreme-INT8 vectors, both activations,
  a zero-idle-gap back-to-back-tiles throughput check, and an 8-tile
  job -- verified with Verilator (see below for why).
- Real synthesis + place&route (Yosys + nextpnr-ecp5): 0 CHECK
  problems, Fmax 183.12 MHz at ACC_WIDTH=32 (PASS at 80MHz, ~3x V1's
  isolated PARALLEL=8 Fmax of 61.71 MHz) and 176.21 MHz at ACC_WIDTH=24
  (a user-requested comparison experiment, also bit-exact-verified;
  see experiments.log EXP-0001/EXP-0002 and benchmark.log).

Three real bugs found and resolved during M1 development (full
diagnostic record in errors.log):
- Two independent, reproducible Icarus Verilog v13.0 scheduling
  defects (ERR-0001, ERR-0002) that silently produced wrong simulation
  results for standard sequential Verilog -- confirmed via Verilator
  5.050 giving correct results on the same minimal repros. Verilator
  is now the trusted simulator for hardware/v2/ (decisions.log
  DEC-0004); Icarus's affected protocol-violation check was removed
  from the RTL and deferred architecturally to the Neural Director
  (DEC-0003) rather than chased further.
- One real RTL bug (ERR-0003): last0 wasn't gated like valid0,
  letting a "last tile" tag leak into the pipeline ahead of its
  actual valid tile on back-to-back jobs. Fixed and verified.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013xXuuRUWZScuo1DeYJxs3v
2026-09-05 14:06:53 +02:00

130 lines
5.3 KiB
Verilog

`timescale 1ns/1ps
// ================================================================
// C.5 / BUG-005 REGRESSION TEST: rtl/layer_sequencer.v with
// run_num_layers=0.
//
// BUG-005 (docs/validation/bugs.md), now FIXED: run_num_layers=0
// used to make layer_idx (a full 8-bit register) wrap the termination
// check to 255, running through all 256 possible layer indices and
// executing 256 fabricated "layers" from garbage PSRAM bytes far past
// the real, N_LAYERS-sized descriptor table -- confirmed via this
// exact test before the fix: 21761 cycles, layer_idx ending at 255.
//
// Fix (rtl/layer_sequencer.v, ST_IDLE): run_num_layers==0 is now an
// explicit, immediate no-op -- seq_done pulses without ever entering
// ST_READ_DESC, same convention as spi_engine.v's WRITE_RAM/READ_RAM
// len==0 guard. This test now ASSERTS that behavior (previously it
// only observed and reported, since the pre-fix outcome was the bug
// itself, not a pass/fail condition).
// ================================================================
//
// neuron_memory is NOT instantiated -- layer_sequencer only needs
// nm_busy/nm_done as far as its own control-flow is concerned, so a
// minimal fake responder (assert busy the cycle after nm_start, done
// one cycle later) is enough to observe how many layer iterations
// actually occur, without needing the full memory stack.
// ================================================================
module tb;
localparam ADDR_WIDTH = 23;
localparam DATA_WIDTH = 8;
localparam N_WIDTH = 8;
localparam N_LAYERS = 4;
reg clk, rst;
reg run_start;
reg [7:0] run_num_layers;
wire seq_busy, seq_done;
reg [ADDR_WIDTH-1:0] x_base, table_base, buf_a_base, buf_b_base;
wire [ADDR_WIDTH-1:0] nm_x_base, nm_w_base, nm_bias_addr;
wire [1:0] nm_activation;
wire [15:0] nm_n_inputs, nm_n_neurons;
wire nm_start;
reg nm_busy, nm_done;
reg signed [DATA_WIDTH*N_WIDTH-1:0] y_bus;
wire ram_req, ram_wr;
wire [ADDR_WIDTH-1:0] ram_addr;
wire signed [7:0] ram_wdata;
reg signed [7:0] ram_rdata;
reg ram_ready;
layer_sequencer #(
.ADDR_WIDTH(ADDR_WIDTH), .DATA_WIDTH(DATA_WIDTH), .N_WIDTH(N_WIDTH), .N_LAYERS(N_LAYERS)
) dut (
.clk(clk), .rst(rst),
.run_start(run_start), .run_num_layers(run_num_layers),
.seq_busy(seq_busy), .seq_done(seq_done),
.x_base(x_base), .table_base(table_base), .buf_a_base(buf_a_base), .buf_b_base(buf_b_base),
.nm_x_base(nm_x_base), .nm_w_base(nm_w_base), .nm_bias_addr(nm_bias_addr),
.nm_activation(nm_activation), .nm_n_inputs(nm_n_inputs), .nm_n_neurons(nm_n_neurons),
.nm_start(nm_start), .nm_busy(nm_busy), .nm_done(nm_done), .y_bus(y_bus),
.ram_req(ram_req), .ram_wr(ram_wr), .ram_addr(ram_addr), .ram_wdata(ram_wdata),
.ram_rdata(ram_rdata), .ram_ready(ram_ready)
);
initial begin clk = 0; forever #5 clk = ~clk; end
// Minimal always-1-cycle-latency RAM stub: any request completes
// next cycle, content is a fixed byte (irrelevant to this check --
// only iteration COUNT and eventual termination matter here).
always @(posedge clk) begin
ram_ready <= ram_req;
ram_rdata <= 8'sd0;
end
// Minimal fake neuron_memory: busy one cycle after start, done one
// cycle after that.
reg [1:0] nm_state;
always @(posedge clk) begin
if (rst) begin
nm_busy <= 0; nm_done <= 0; nm_state <= 0;
end else begin
nm_done <= 0;
case (nm_state)
0: if (nm_start) begin nm_busy <= 1; nm_state <= 1; end
1: begin nm_busy <= 0; nm_done <= 1; nm_state <= 0; end
endcase
end
end
integer watchdog;
initial begin
rst <= 1;
run_start <= 0; run_num_layers <= 0;
x_base <= 0; table_base <= 0; buf_a_base <= 0; buf_b_base <= 0;
y_bus <= 0;
repeat(3) @(posedge clk);
rst <= 0;
@(posedge clk);
$display("--- run_num_layers=0: must complete immediately, must NOT run through 256 garbage layers ---");
run_num_layers <= 8'd0;
run_start <= 1;
@(posedge clk);
run_start <= 0;
watchdog = 0;
while (!seq_done && watchdog < 200000) begin
@(posedge clk);
watchdog = watchdog + 1;
end
if (!seq_done) begin
$display("FAIL: run_num_layers=0 HANGS -- no seq_done in %0d cycles, seq_busy=%b, layer_idx=%0d (BUG-005 fix regressed)", watchdog, seq_busy, dut.layer_idx);
end else if (dut.layer_idx !== 8'd0) begin
$display("FAIL: seq_done reached after %0d cycles but layer_idx=%0d (expected 0 -- the sequencer entered the descriptor-read loop instead of taking the immediate no-op path, BUG-005 fix regressed)", watchdog, dut.layer_idx);
end else if (watchdog > 5) begin
$display("FAIL: seq_done reached in %0d cycles with layer_idx=0, but that is far more than the ~1-2 cycles an immediate no-op should take -- worth re-examining even though layer_idx itself looks correct", watchdog);
end else begin
$display("PASS: run_num_layers=0 completed as an immediate no-op in %0d cycle(s), layer_idx stayed 0 -- BUG-005 fix confirmed, no garbage layers executed", watchdog);
end
$finish;
end
endmodule