suryagunukula's picture
Initial release
035592e
Raw History Blame Contribute Delete
3.92 kB
`timescale 1ns/1ns
`define width 8
`define TESTFILE "test_data.dat"
module booth4_mul_tb () ;
reg signed [`width-1:0] a, b;
reg clk, reset;
wire signed [2*`width-1:0] p;
wire rdy;
integer total, err;
integer i, s, fp, numtests;
wire signed [2*`width-1:0] ans = a*b;
multi_booth_8bit dut( .clk(clk),
.reset(reset),
.a(a),
.b(b),
.p(p),
.rdy(rdy));
// Set up 10ns clock
always #5 clk = ~clk;
task apply_and_check;
input [`width-1:0] ain;
input [`width-1:0] bin;
begin
// Set the inputs
a = ain;
b = bin;
// Reset the DUT for one clock cycle
reset = 1;
@(posedge clk);
// Remove reset
#1 reset = 0;
while (rdy == 0) begin
@(posedge clk); // Wait for one clock cycle
end
if (p == ans) begin
// $display($time, " Passed %d * %d = %d", a, b, p);
end else begin
// $display($time, " Fail %d * %d: %d instead of %d", a, b, p, ans);
err = err + 1;
end
total = total + 1;
end
endtask // apply_and_check
// ---------------- Additional checks ----------------
integer hk, herr = 0, hj;
reg signed [15:0] hexp;
task h_capture_hold;
input signed [7:0] a0, b0; // operands present while reset is high
input signed [7:0] a1, b1; // operands driven after reset falls
begin
a = a0; b = b0;
hexp = a0 * b0;
reset = 1;
@(posedge clk);
#1 reset = 0;
#1 a = a1; b = b1;
hj = 0;
while (rdy == 0 && hj < 200) begin @(posedge clk); hj = hj + 1; end
#1;
if (rdy !== 1'b1 || p !== hexp) begin
herr = herr + 1;
$display("HCHK capture: a=%0d b=%0d loaded under reset, then a=%0d b=%0d: rdy=%b p=%0d expected %0d",
a0, b0, a1, b1, rdy, p, hexp);
end
for (hj = 0; hj < 8; hj = hj + 1) begin
@(posedge clk); #1;
if (p !== hexp) begin
herr = herr + 1;
$display("HCHK hold: %0d cycles after rdy p=%0d expected %0d", hj + 1, p, hexp);
end
end
end
endtask
initial begin
clk = 1;
total = 0;
err = 0;
// Get all inputs from file: 1st line has number of inputs
fp = $fopen(`TESTFILE, "r");
s = $fscanf(fp, "%d\n", numtests);
// Sequences of values pumped through DUT
for (i=0; i<numtests; i=i+1) begin
s = $fscanf(fp, "%d %d\n", a, b);
apply_and_check(a, b);
end
// Additional checks, after the original vectors:
// H1 operands are loaded while reset is high (multiplier <= a, multiplicand <= b): changing a/b after
// reset falls must not change the product.
// H2 p only changes while ctr < 16: p keeps the product for 8 more cycles after rdy.
// H3 signed operands (the spec sign-extends a and b): the extreme corners plus 40 random pairs.
h_capture_hold(8'sd77, -8'sd93, -8'sd5, 8'sd121);
h_capture_hold(-8'sd128, -8'sd128, 8'sd3, 8'sd3);
apply_and_check(-8'sd128, -8'sd128);
apply_and_check(-8'sd128, 8'sd127);
apply_and_check(8'sd127, -8'sd128);
apply_and_check(8'sd127, 8'sd127);
for (hk = 0; hk < 40; hk = hk + 1) apply_and_check($random, $random);
err = err + herr;
if (err > 0) begin
$display("=========== Failed ===========");
end else begin
$display("===========Your Design Passed===========");
end
$finish;
end
endmodule