module tb9; // testbench for the 4-bit by 4-bit Baugh-Wooley signed multiplier // exhaustive checking of all 256 possible cases reg [3:0] x, y; // 4-bit inputs (to be chosen randomly) integer xval, yval; // numerical values of inputs x and y wire [7:0] p; // 8-bit output of the multiplier circuit integer pval; // numerical value of the product integer check; // value used to check correctness integer i, j; // 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 4-bit by 4-bit Baugh-Wooley signed multiplier mult4bw mult_instance(x, y, p); // exhaustive simulation of all 256 possible cases 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 < 16; i = i + 1) begin x = i; xval = -x[3]*8 + x[2:0]; for (j = 0; j < 16; j = j + 1) begin y = j; yval = -y[3]*8 + y[2:0]; check = xval * yval; // compute and check the product #10 pval = -p[7]*128 + p[6:0]; if (pval == check) num_correct = num_correct + 1; else num_wrong = num_wrong + 1; // following line is commented out, but is useful for debugging // $display($time, " %d * %d = %d (%d)", xval, yval, pval, check); end end // print the final counter values $display("num_correct = %d, num_wrong = %d", num_correct, num_wrong); end endmodule