// tb_sum8.sv // Self-checking testbench: drives the same random inputs into sum8_chain and // sum8_pipe and checks each output against a reference sum, allowing for the // different latencies (2 and 4 cycles). Inputs include all-ones vectors so // the widest carry is tested. `timescale 1ns/1ps module tb_sum8; localparam int W = 16; localparam int N = 1000; logic clk, rst, in_valid; logic [7:0][W-1:0] x; logic vc, vp; logic [W+2:0] yc, yp; logic [W+2:0] exp_c [$]; logic [W+2:0] exp_p [$]; logic [W+2:0] ref_sum, e; int errors, n_c, n_p; integer seed; sum8_chain #(.W(W)) u_chain (.clk(clk), .rst(rst), .in_valid(in_valid), .x(x), .out_valid(vc), .y(yc)); sum8_pipe #(.W(W)) u_pipe (.clk(clk), .rst(rst), .in_valid(in_valid), .x(x), .out_valid(vp), .y(yp)); initial begin clk = 1'b0; forever #5 clk = ~clk; end // Monitor both outputs. initial begin errors = 0; n_c = 0; n_p = 0; forever begin @(posedge clk); #1; if (vc) begin e = exp_c.pop_front(); if (yc !== e) begin $display("ERROR chain: y=%0d expected %0d", yc, e); errors = errors + 1; end n_c = n_c + 1; end if (vp) begin e = exp_p.pop_front(); if (yp !== e) begin $display("ERROR pipe: y=%0d expected %0d", yp, e); errors = errors + 1; end n_p = n_p + 1; end end end initial begin seed = 11; // fixed seed: the run is repeatable rst = 1'b1; in_valid = 1'b0; x = '0; repeat (3) @(posedge clk); @(negedge clk); rst = 1'b0; for (int n = 0; n < N; n++) begin ref_sum = '0; for (int i = 0; i < 8; i++) begin x[i] = (n % 50 == 0) ? {W{1'b1}} : W'($random(seed)); ref_sum = ref_sum + (W+3)'(x[i]); end in_valid = (($unsigned($random(seed)) % 5) != 0); if (in_valid) begin exp_c.push_back(ref_sum); exp_p.push_back(ref_sum); end @(negedge clk); end in_valid = 1'b0; repeat (6) @(negedge clk); if (errors == 0 && n_c > 0 && n_c == n_p && exp_c.size() == 0 && exp_p.size() == 0) begin $display("PASS: %0d sums checked on each design", n_c); end else begin $display("FAIL: %0d errors, chain %0d, pipe %0d", errors, n_c, n_p); end $finish; end endmodule