`timescale 1ns/1ns module multi_pipe_tb; reg clk; reg rst_n; reg [3:0] mul_a; reg [3:0] mul_b; wire [7:0] mul_out; wire signed [7:0] perfect = mul_a*mul_b; // Instantiate the DUT (Design Under Test) multi_pipe_4bit #(.size(4)) dut ( .clk(clk), .rst_n(rst_n), .mul_a(mul_a), .mul_b(mul_b), .mul_out(mul_out) ); // Generate clock always #5 clk = ~clk; integer fail_count =0; integer i=0; // additional check integer hk, hl, herr = 0; integer hbad [2:4]; reg [7:0] hexp [0:261]; reg [7:0] hgot [0:261]; initial begin // Initialize inputs clk = 0; rst_n = 0; mul_a = 4'b0; mul_b = 4'b0; // Wait for a few clock cycles for reset to settle #10; // Apply reset rst_n = 1; // Perform test case for (i = 0; i < 100; i = i + 1) begin mul_a = $random; mul_b = $random; #10; // without pipeline fail_count = (perfect == mul_out)? fail_count+1:fail_count; #20; // $display("%d, %d, %d, %d", mul_a, mul_b, perfect, mul_out); fail_count = (perfect == mul_out)? fail_count:fail_count+1; end // Additional checks, after the original 100 checks (stimulus on falling edges): // H1 the registers are updated on every rising edge (no enable): with new operands every cycle (all 256 // pairs), mul_out must follow the products with one constant latency (2, 3 or 4 cycles accepted). // H2 mul_out is cleared on the falling edge of rst_n: asserted between clock edges mid-run, mul_out is 0 // 1 ns later and stays 0 while rst_n is low. for (hk = 0; hk < 262; hk = hk + 1) begin if (hk < 256) begin mul_a = hk[7:4]; mul_b = hk[3:0] ^ hk[7:4]; end else begin mul_a = 4'd15; mul_b = 4'd15; end hexp[hk] = mul_a * mul_b; #10; // rising edge in between, now at the next falling edge hgot[hk] = mul_out; // value after hk+1 rising edges since operands hexp[0] were applied end for (hl = 2; hl <= 4; hl = hl + 1) begin hbad[hl] = 0; for (hk = hl - 1; hk < 262; hk = hk + 1) if (hgot[hk] !== hexp[hk - hl + 1]) hbad[hl] = hbad[hl] + 1; end if (hbad[2] != 0 && hbad[3] != 0 && hbad[4] != 0) begin herr = herr + 1; $display("HCHK stream: mul_out does not follow the products with a constant latency (mismatches L2=%0d L3=%0d L4=%0d)", hbad[2], hbad[3], hbad[4]); end // H2: mul_out is 225 here; assert reset 3 ns after a falling edge #3 rst_n = 0; #1; if (mul_out !== 8'd0) begin herr = herr + 1; $display("HCHK reset: mul_out=%0d 1 ns after rst_n fell (expected 0)", mul_out); end for (hk = 0; hk < 3; hk = hk + 1) begin #10; if (mul_out !== 8'd0) begin herr = herr + 1; $display("HCHK reset: mul_out=%0d with rst_n low (expected 0)", mul_out); end end rst_n = 1; fail_count = fail_count + herr; if (fail_count == 0) begin $display("===========Your Design Passed==========="); end else begin $display("===========Test completed with %d / 100 failures===========", fail_count); end $finish; end endmodule