Here is a sample solutions to Assignment 0: Unsigned Saturation Subtraction.
// 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 // Unsigned SATuration SUBtract, 8-bit // uses a 9-bit add module satsub8(t, a, b); output [7:0] t; input [7:0] a, b; wire [7:0] notb, sum, cout; wire sum8, cout8; assign notb = {(!b[7]), ~b}; fa fa0(sum[0], cout[0], a[0], notb[0], 1), fa1(sum[1], cout[1], a[1], notb[1], cout[0]), fa2(sum[2], cout[2], a[2], notb[2], cout[1]), fa3(sum[3], cout[3], a[3], notb[3], cout[2]), fa4(sum[4], cout[4], a[4], notb[4], cout[3]), fa5(sum[5], cout[5], a[5], notb[5], cout[4]), fa6(sum[6], cout[6], a[6], notb[6], cout[5]), fa7(sum[7], cout[7], a[7], notb[7], cout[6]), fa8(sum8, cout8, 0, 1, cout[7]); assign t = (cout8 ? sum : 0); endmodule // REFerence unsigned SATuration SUBtract, 8-bit module refsatsub8(t, a, b); output [7:0] t; input [7:0] a, b; assign t = ((a < b) ? 0 : (a - b)); endmodule // TEST BENCH module testbench; reg [7:0] a, b; wire [7:0] s, sref; integer correct = 0; integer failed = 0; satsub8 uut(s, a, b); refsatsub8 oracle(sref, a, b); initial begin a=0; repeat (256) begin b=0; repeat (256) #1 begin if (s != sref) begin $display("Wrong: %d-%d=%d, but got %d", a, b, sref, s); failed = failed + 1; end else begin correct = correct + 1; end b = b + 1; end a = a + 1; end $display("All cases tested; %d correct, %d failed", correct, failed); end endmodule
The C program that generated this page was written by Hank Dietz using the CGIC library to implement the CGI interface.
Advanced Computer Architecture.