module fulladd4(sum, c_out, a, b, c_in); // Inputs and outputs output [3:0] sum; output c_out; input [3:0] a,b; input c_in; // Internal wires wire p0,g0, p1,g1, p2,g2, p3,g3; wire c4, c3, c2, c1; // compute the p for each stage assign p0 = a[0] ^ b[0], p1 = a[1] ^ b[1], p2 = a[2] ^ b[2], p3 = a[3] ^ b[3]; // compute the g for each stage assign #3 g0 = a[0] & b[0], g1 = a[1] & b[1], g2 = a[2] & b[2], g3 = a[3] & b[3]; // compute the carry for each stage // Note that c_in is equivalent c0 in the arithmetic equation for // carry lookhead computation assign #3 c1 = g0 | (p0 & c_in), c2 = g1 | (p1 & g0) | (p1 & p0 & c_in), c3 = g2 | (p2 & g1) | (p2 & p1 & g0) | (p2 & p1 & p0 & c_in), c4 = g3 | (p3 & g2) | (p3 & p2 & g1) | (p3 & p2 & p1 & g0) | (p3 & p2 & p1 & p0 & c_in); // Compute Sum assign #3 sum[0] = p0 ^ c_in, sum[1] = p1 ^ c1, sum[2] = p2 ^ c2, sum[3] = p3 ^ c3; // Assign carry output assign c_out = c4; endmodule // Define the stimulus (top level module) module stimulus; // Set up variables reg [3:0] A, B; reg C_IN; wire [3:0] SUM; wire C_OUT; // Instantiate the 4-bit full adder. call it FA1_4 fulladd4 FA1_4(SUM, C_OUT, A, B, C_IN); // Setup the monitoring for the signal values initial begin $monitor($time," A= %b, B=%b, C_IN= %b,, C_OUT= %b, SUM= %b\n", A, B, C_IN, C_OUT, SUM); end // Stimulate inputs initial begin A = 4'd0; B = 4'd0; C_IN = 1'b0; #5 A = 4'd3; B = 4'd4; #5 A = 4'd2; B = 4'd8; #5 A = 4'd9; B = 4'd9; #5 A = 4'd10; B = 4'd15; #5 A = 4'd10; B = 4'd5; C_IN = 1'b1; end endmodule