suryagunukula's picture
Initial release
035592e
Raw History Blame Contribute Delete
2.1 kB
`timescale 1ns / 1ps
module testbench;
reg [63:0] A; // Input A (64 bits)
reg [63:0] B; // Input B (64 bits)
wire [63:0] result; // Subtraction result (64 bits)
wire overflow; // Overflow signal
integer i; // Loop variable
integer error = 0; // Error count for failed tests
reg [63:0] diff; // fix: expected A - B (64-bit)
reg exp_ovf; // fix: expected overflow (sign rules of the spec)
// Instantiate the verified_sub_64bit module
sub_64bit uut (
.A(A),
.B(B),
.result(result),
.overflow(overflow)
);
// Randomize inputs and check output
initial begin
// fix: vectors 100..199 use full 64-bit operands so that overflow can occur
for (i = 0; i < 200; i = i + 1) begin
// Generate random 64-bit inputs
if (i < 100) begin
A = $random;
B = $random;
end
else begin
A = {$random, $random};
B = {$random, $random};
end
// Wait for the operation to complete
#10;
// Calculate expected result using system task
// $monitor("A = %d, B = %d, Expected Result = %d, Overflow = %b", A, B, A - B, overflow);
// Check the result of the subtraction
// fix: the old overflow term (unsigned A - B < 0) could never fire; overflow is now compared:
// operands of different signs and a result whose sign differs from A's (zero operands not graded).
diff = A - B;
exp_ovf = (A[63] != B[63]) && (diff[63] != A[63]);
if (result !== diff || (A != 0 && B != 0 && overflow !== exp_ovf)) begin
error = error + 1;
$display("Test failed: A = %d, B = %d, Expected Result = %d, Got = %d, Overflow = %b", A, B, A - B, result, overflow);
end
end
// Final test result summary
if (error == 0) begin
$display("=========== Your Design Passed ===========");
end
else begin
$display("=========== Test completed with %d /200 failures ===========", error);
end
$finish;
end
endmodule