`timescale 1ns/1ns `define clk_period 20 module tb_multi_pipe(); reg [7:0] mul_a; reg [7:0] mul_b; reg mul_en_in; reg clk; reg rst_n; wire mul_en_out; wire [15:0] mul_out; reg [15:0] expected_product; multi_pipe_8bit u1( .clk(clk), .rst_n(rst_n), .mul_a(mul_a), .mul_b(mul_b), .mul_en_in(mul_en_in), .mul_en_out(mul_en_out), .mul_out(mul_out) ); initial clk = 1; always # 10 clk = ~clk; integer error = 0; // ---------------- Additional checks ---------------- integer herr = 0, hpush = 0, hpop = 0, hk; reg hon = 0, hmon = 0; reg [15:0] hq [0:255]; always @(posedge rst_n) hon = 1; always @(negedge clk) if (hon && mul_en_out === 1'b0 && mul_out !== 16'd0) begin herr = herr + 1; if (herr < 10) $display("HCHK idle: mul_en_out=0 but mul_out=%0d at %0t", mul_out, $time); end always @(posedge clk) if (hmon && mul_en_in === 1'b1) begin hq[hpush] = mul_a * mul_b; hpush = hpush + 1; end always @(negedge clk) if (hmon && mul_en_out === 1'b1) begin if (hpop >= hpush || mul_out !== hq[hpop]) begin herr = herr + 1; $display("HCHK stream: result #%0d got %0d expected %0d", hpop, mul_out, hq[hpop]); end hpop = hpop + 1; end task h_stream; begin @(negedge clk); mul_en_in = 0; repeat (20) @(negedge clk); hmon = 1; for (hk = 0; hk < 160; hk = hk + 1) begin mul_a = $random; mul_b = $random; mul_en_in = (hk < 40) ? 1'b1 : $random; @(negedge clk); end mul_en_in = 0; repeat (20) @(negedge clk); hmon = 0; if (hpop != hpush) begin herr = herr + 1; $display("HCHK stream: %0d operand pairs accepted but %0d mul_en_out=1 cycles", hpush, hpop); end end endtask initial begin rst_n = 0; mul_a = 0; mul_b = 0; mul_en_in = 0; #(`clk_period*200+1); rst_n = 1; #(`clk_period*10); mul_a = 'd35; mul_b = 'd20; mul_en_in = 'd1; repeat (100) begin mul_en_in = 0; #(`clk_period*20); rst_n = 1; #(`clk_period*10); mul_a = $random; mul_b = $random; mul_en_in = 'd1; #(`clk_period); expected_product = mul_a * mul_b; #(`clk_period); error = ((mul_en_out==1) && (mul_out == expected_product)) ? error+1 : error; while (mul_en_out == 0) begin @(posedge clk); // Wait for one clock cycle end // //========it can be changed depending on your pipelining processing latency // #(`clk_period*4); // //========it can be changed depending on your pipelining processing latency error = (mul_out != expected_product) ? error+1 : error; // $display("mul_a = %d, mul_b = %d, mul_out = %d, expected = %d", mul_a, mul_b, mul_out, expected_product); end // Additional checks, after the original 100 checks: // H1 "Otherwise, mul_out is set to 0": whenever mul_en_out is 0, mul_out is 0 (monitored at every falling // clock edge from the first reset release to the end of the run, original stimulus included). // H2 pipelining: 160 cycles of new random operands every cycle with a random mul_en_in pattern; every // mul_en_out=1 cycle must show the product of the next accepted (mul_en_in=1) operand pair, in order. h_stream; error = error + herr; if (error == 0) begin $display("===========Your Design Passed==========="); end else begin $display("===========Test completed with %d /100 failures===========", error); end $finish; end endmodule