```////////////////////////////////////////////////////////////////////////////////
///
/// Solution to LSU EE 3755 Spring 2002 Homework 3
///

// Assignment:http://www.ee.lsu.edu/ee3755/2002/hw03.html

////////////////////////////////////////////////////////////////////////////////
/// Problem 1
///

input [7:0] a, b;
output [8:0] sum;

wire         p0, g0, p1, g1;

assign       sum[8] = g0 & p1 | g1;

endmodule

////////////////////////////////////////////////////////////////////////////////
/// Problem 2
///

// Common problem:  Signals omitted from event control in always.
//  For example, in the second always in the module below the
//  following is an easy mistake to make (g0 missing):
//      always @( p0 or p1 or g1 )

input [3:0] a, b;
input       cin;
output [3:0] sum;
output       P, G;
reg          P, G;

wire         p0, g0, p1, g1;

reg          c0;

always @( cin or p0 or g0 ) c0 = cin & p0 | g0;

always @( p0 or p1 or g0 or g1 )
begin
P = p0 & p1;
G = g0 & p1 | g1;
end

endmodule

////////////////////////////////////////////////////////////////////////////////
/// Problem 3
///

// Common mistake: P and G must not be computed using a carry out.

input [1:0] a, b;
input       cin;
output [1:0] sum;
output       P, G;

wire         c0, c1;

bfa bfa0(sum[0],c0,a[0],b[0],cin);
bfa bfa1(sum[1],c1,a[1],b[1],c0);

wire       p0 = a[0] | b[0];
wire       p1 = a[1] | b[1];
wire       g0 = a[0] & b[0];
wire       g1 = a[1] & b[1];

assign     P = p0 & p1;
assign     G = g0 & p1 | g1;

endmodule

////////////////////////////////////////////////////////////////////////////////
/// Module used in the solution.
///

module bfa(sum,cout,a,b,cin);
input a,b,cin;
output sum,cout;

assign sum = a ^ b ^ cin;
assign cout = a & b | b & cin | a & cin;

endmodule

////////////////////////////////////////////////////////////////////////////////
/// Testbench
///

parameter stop_on_error = 1;
parameter max_errs = 10;

wire [8:0] sum;
reg [7:0]  a, b;

integer    i, err;

initial begin

err = 0;

for(i=0; i<='hffff; i=i+1) begin
{a,b} = i;
#1;

if( shadow_sum !== sum ) begin

err = err + 1;

if( err < max_errs )
\$display("FAIL: Wrong sum: 0x%h + 0x%h = 0x%h (correct) != 0x%h",

if( err == max_errs )
\$display("Maximum error message count reached.");

if( stop_on_error ) \$stop;

end

end

\$display("Tests completed.");
if( err == 0 )
\$display("PASS: No errors detected.");
else
\$display("FAIL: %d errors.\n",err);

end

endmodule```