module tb2lf; // testbench for the 8-bit unsigned Ladner-Fischer adder // exhaustive checking of all 256*256*2 possible cases reg [7:0] a, b; // 8-bit operands reg c0; // carry input wire [7:0] s; // 8-bit sum output wire c8; // carry output reg [8:0] check; // 9-bit value used to check correctess integer i, j, k; // loop variables integer num_correct; // counter to keep track of the number correct integer num_wrong; // counter to keep track of the number wrong // instantiate the 8-bit Ladner-Fischer adder ladner_fischer lf1(a, b, c0, s, c8); // exhaustive checking initial begin // initialize the counter variables num_correct = 0; num_wrong = 0; // loop through all possible cases and record the results for (i = 0; i < 256; i = i + 1) begin a = i; for (j = 0; j < 256; j = j + 1) begin b = j; for (k = 0; k < 2; k = k + 1) begin c0 = k; check = a + b + c0; // compute and check the product #10 if ({c8, s} == check) num_correct = num_correct + 1; else num_wrong = num_wrong + 1; // following line is for debugging // $display($time, " %d + %d + %d = %d (%d)", a, b, c0, {c8, s}, check); end end end // print the final counter values $display("num_correct = %d, num_wrong = %d", num_correct, num_wrong); end endmodule