Files
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

105 lines
3.9 KiB
Verilog

`timescale 1ns/1ps
// ================================================================
// MAC_UNIT TESTBENCH (certification campaign, aspect C.1)
//
// rtl/mac_unit.v had NO dedicated unit-level testbench before this
// (docs/validation/00-inventario.md §0.4/§0.5): only indirect coverage
// through neuron_parallel_tb.v and friends, where acc_in is always
// hardwired to 0 by mac8.v and a bug isolated to this module would only
// surface if it happened to propagate visibly through the full layer.
//
// Oracle: tools/validation/mac_oracle.py, an independent Python
// reimplementation (two's complement from first principles, not a
// transcription of this RTL) -- see that file's own header. Vectors are
// pre-generated (tools/validation/mac_unit_vectors_*.hex) rather than
// computed at Verilog runtime, so the comparison at each step is purely
// mechanical (no chance of Verilog-side arithmetic accidentally
// re-deriving the same bug the oracle exists to catch).
//
// Coverage: EXHAUSTIVE on (x,w) at DATA_WIDTH=8 -- all 256*256=65536
// combinations, acc_in=0 (matches mac8.v's real usage). Plus a second,
// separate file exercising the module's own full port contract
// (acc_out = acc_in + product for ANY acc_in, not just the acc_in=0 case
// this design happens to use today) across boundary/random acc_in values
// including the four true product-magnitude corners
// (-128*-128, 127*127, -128*127, 127*-128).
// ================================================================
module tb;
localparam DATA_WIDTH = 8;
localparam ACC_WIDTH = 32;
reg signed [DATA_WIDTH-1:0] x;
reg signed [DATA_WIDTH-1:0] w;
reg signed [ACC_WIDTH-1:0] acc_in;
wire signed [ACC_WIDTH-1:0] acc_out;
mac_unit #(.DATA_WIDTH(DATA_WIDTH), .ACC_WIDTH(ACC_WIDTH)) dut (
.x(x), .w(w), .acc_in(acc_in), .acc_out(acc_out)
);
// 80-bit packed vector: x[79:72] w[71:64] acc_in[63:32] expected[31:0]
reg [79:0] exhaustive_vec [0:65535];
reg [79:0] boundary_vec [0:485];
integer i;
integer errors;
integer checked;
reg [7:0] vx;
reg [7:0] vw;
reg [31:0] vacc_in;
reg [31:0] vexpected;
task automatic check_one(input [79:0] packed_vec);
begin
vx = packed_vec[79:72];
vw = packed_vec[71:64];
vacc_in = packed_vec[63:32];
vexpected = packed_vec[31:0];
x = vx;
w = vw;
acc_in = vacc_in;
#1;
checked = checked + 1;
if (acc_out !== $signed(vexpected)) begin
errors = errors + 1;
if (errors <= 20) begin
$display("MISMATCH: x=%0d w=%0d acc_in=%0d -> got=%0d expected=%0d",
$signed(vx), $signed(vw), $signed(vacc_in), acc_out, $signed(vexpected));
end
end
end
endtask
initial begin
errors = 0;
checked = 0;
$readmemh("tools/validation/mac_unit_vectors_exhaustive.hex", exhaustive_vec);
$readmemh("tools/validation/mac_unit_vectors_boundary_accin.hex", boundary_vec);
$display("--- TEST 1: exhaustive (x,w), acc_in=0 -- 65536 combinations ---");
for (i = 0; i < 65536; i = i + 1) begin
check_one(exhaustive_vec[i]);
end
$display(" checked %0d, errors so far %0d", checked, errors);
$display("--- TEST 2: full port contract, boundary/random acc_in -- 486 vectors ---");
for (i = 0; i < 486; i = i + 1) begin
check_one(boundary_vec[i]);
end
$display(" checked %0d total, errors %0d", checked, errors);
if (errors == 0) begin
$display("ALL TESTS PASSED (%0d vectors, 0 mismatches against independent Python oracle)", checked);
end else begin
$display("FAILED: %0d/%0d vectors mismatched", errors, checked);
end
$finish;
end
endmodule