// Full Adder module fa(sum, cout, a, b, cin); output sum, cout; input a, b, cin; wire aorb, gen, prop; xor(sum, a, b, cin); // sum and(gen, a, b); // generate or(aorb, a, b); // propogate and(prop, aorb, cin); or(cout, gen, prop); // cout endmodule // ripple carry ADD, 8-bit module add8(sum, a, b, cin); output [7:0] sum; input [7:0] a, b; input cin; wire [7:0] cout; fa fa0(sum[0], cout[0], a[0], b[0], cin), fa1(sum[1], cout[1], a[1], b[1], cout[0]), fa2(sum[2], cout[2], a[2], b[2], cout[1]), fa3(sum[3], cout[3], a[3], b[3], cout[2]), fa4(sum[4], cout[4], a[4], b[4], cout[3]), fa5(sum[5], cout[5], a[5], b[5], cout[4]), fa6(sum[6], cout[6], a[6], b[6], cout[5]), fa7(sum[7], cout[7], a[7], b[7], cout[6]); endmodule // random 8-bit value via LCG module rand8(r, seed); output [7:0] r; input [7:0] seed; wire [7:0] seed32; add8 one(seed32, {seed[4:0],3'b000}, {seed[5:0],2'b00}, 1'b0), two(r, seed32, seed, 1'b1); endmodule // REFerence random 8-bit value via LCG module refrand8(r, seed); output reg [7:0] r; input [7:0] seed; always @* begin r = ((13 * seed) + 1); end endmodule // TEST BENCH module testbench; reg [7:0] seed; wire [7:0] v, vref; integer correct = 0; integer failed = 0; rand8 uut(v, seed); refrand8 oracle(vref, seed); initial begin seed = 0; repeat (256) #1 begin if (v != vref) begin $display("Wrong: seed=%d should give %d, but got %d", seed, vref, v); failed = failed + 1; end else begin correct = correct + 1; end #1 seed = vref; end $display("All cases tested; %d correct, %d failed", correct, failed); end endmodule