```

// Problem 1

output sum, cout;
input  a, b, cin;

reg [3:0] sum;
reg       cout;

wire [3:0] a, b;
wire       cin;
reg [4:0]  fullsum;

always @( a or b or cin ) {cout,sum} = a + b + cin;

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

wire [7:0] sum;
wire       cout;

wire [7:0] a, b;
wire       cin;

wire       cbetween;

// Named ports are used for readability.

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

initial begin:I
integer i;

for(i=0; i<'h20000; i=i+1) begin
{cin,a,b} = i;
#1;
if( {cout,sum} != a + b + cin ) begin
\$display("Error: %d + %d + %d =?= %d %d\n",
a,b,cin,cout,sum);
\$stop;
end

end

end

// Problem 3

module acc(sum,amt,reset,clk);
output sum;
input  amt, reset, clk;

reg [31:0] sum;
wire [31:0] amt;
wire        reset, clk;
reg [31:0]  nextsum;

always @( clk or posedge reset)
if( reset ) begin
nextsum = 0; sum = 0;
end else if( clk === 1 )
nextsum = amt + sum;
else sum = nextsum;

endmodule // acc

module testacc();

reg clk, reset;
wire [31:0] sum;
reg [31:0]  amt;

acc acc1(sum,amt,reset,clk);

initial begin:I
integer i,r;
clk = 0;

for(i=0; i<1000; i=i+1) begin

#( 2 + ( \$random & 31 ) );

if( reset ) begin
if( \$random & 1 ) reset = 0;
end else begin
if( ! ( \$random & 15 ) ) reset = 1;
end

amt = \$random >> 5;

end // for (i=0; i<1000; i=i+1)

\$stop;

end

always #10 clk = ~clk;

always @( posedge reset ) begin shadowout = 0; shadownext = 0; end

always @( posedge clk ) if( !reset ) shadownext = shadownext + amt;

initial begin
#1;
forever @( shadowout or sum ) #1
if( shadowout !== sum ) begin \$display("Error"); \$stop; end
end

endmodule // testacc

// Problem 4a

// This is the code given in the problem.

module delays();

integer a;

initial begin

a = 1;
#1;
a = 2;
#1;
a = 3;
#1;
a <= 4;
#1;
a <= 5;
#1;
#3 a = a+1;
#1;
a = #3 a+1;
#1;
a <= #3 a+1;
#1;
a = a+1;
end

endmodule // delays

// Problem 4b

// Original code:

module prob4b();

initial begin:I

integer i, c;
integer a[0:1000];

i = 0;

while( i < 1000 ) begin:LOOP

if( a[i] == c ) disable LOOP;

i = i + 1;

end

end

endmodule

// Solution

module prob4bsol();

initial begin:I

integer i, c;
integer a[0:1000];

i = 0;

begin:LOOP
while( i < 1000 ) begin

if( a[i] == c ) disable LOOP;

i = i + 1;

end
end

end

endmodule

```