File size: 3,143 Bytes
035592e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | module tb_multi_16bit;
reg clk;
reg rst_n;
reg start;
reg [15:0] ain;
reg [15:0] bin;
wire [31:0] yout;
wire done;
integer i; // Declare the loop variable here
integer fail_count = 0; // Declare a variable to count the failures
integer timeout; // Declare a timeout counter here
reg [31:0] expected_product; // Declare a variable to store the expected product
integer hk, herr = 0; // additional check
reg [31:0] hexp; // additional check
// Instantiate the module
multi_16bit uut (
.clk(clk),
.rst_n(rst_n),
.start(start),
.ain(ain),
.bin(bin),
.yout(yout),
.done(done)
);
// Clock generation
always begin
#5 clk = ~clk;
end
// Randomize inputs and check output
initial begin
clk = 0; // Initialize clock
rst_n = 1; // De-assert reset
start = 0; // Initialize start
// Perform reset
rst_n = 0;
for (i = 0; i < 100; i = i + 1) begin
#100;
rst_n = 1;
#50;
ain = $random;
bin = $random;
#50;
start = 1; // Start the operation
while(done !== 1) begin
#10;
end
expected_product = ain * bin;
if (done == 1) begin
fail_count = (yout == expected_product)? fail_count:fail_count+1;
end
start = 0; // Stop the operation
rst_n = 0;
#100;
end
// Additional checks, one more operation after the original 100 (stimulus changes on falling edges):
// H1 done_r is set when i == 16 and cleared when i == 17, and i stops at 17 while start stays high:
// done is high for exactly one cycle, and yout keeps the product after it (no further accumulation).
// H2 i counts from 0 by one per clock while start is high (operands stored at i == 0, done set at i == 16):
// done rises at the 17th rising edge after start goes high.
rst_n = 1;
ain = $random;
bin = $random;
#10;
start = 1;
hexp = ain * bin;
for (hk = 1; hk <= 22; hk = hk + 1) begin
#10; // falling edge after the hk-th rising edge with start high
if (hk < 17 && done !== 1'b0) begin
herr = herr + 1; $display("HCHK latency: done=%b after %0d edges (expected 0 before edge 17)", done, hk);
end
if (hk == 17 && (done !== 1'b1 || yout !== hexp)) begin
herr = herr + 1; $display("HCHK latency: after edge 17 done=%b yout=%h (expected done=1, yout=%h)", done, yout, hexp);
end
if (hk > 17 && done !== 1'b0) begin
herr = herr + 1; $display("HCHK done pulse: done=%b after edge %0d with start held (expected 0)", done, hk);
end
if (hk > 17 && yout !== hexp) begin
herr = herr + 1; $display("HCHK hold: yout=%h after edge %0d (expected %h)", yout, hk, hexp);
end
end
start = 0;
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
initial begin
#50000;
$finish;
end
endmodule |