module tb5; // testbench for signed addition and subtraction reg [3:0] a, b; // 4-bit inputs (to be chosen randomly) integer aval, bval; // numerical values of inputs a and b reg c; // carry input (to be used for subtraction) wire [4:0] s; // 5-bit output of the RCA circuit integer sval, dval; // numerical values of the sum and difference integer sum_check; // value used to check correctness of an addition integer dif_check; // value used to check correctness of a subtraction // instantiate the 4-bit ripple-carry adder RCA rca1(a, b, c, s); // simulation of 10 additions and 10 subtractions using random operands initial repeat (10) begin // get new operand values and compute the two check values a = $random; b = $random; c = 0; aval = -a[3]*8 + a[2:0]; bval = -b[3]*8 + b[2:0]; sum_check = aval + bval; dif_check = aval - bval; // compute and display the sum with its check value #10 sval = -s[3]*8 + s[2:0]; $display($time, " %d + %d = %d (%d)", aval, bval, sval, sum_check); // compute and display the difference with its check value b = ~b; c = 1; // one's complement of b plus 1 into the LSB #10 dval = -s[3]*8 + s[2:0]; $display($time, " %d - %d = %d (%d)", aval, bval, dval, dif_check); end endmodule