module test_adder1;
reg [2:0]a,b;
reg [2:0]carry_in ;
wire [2:0]sum;
wire [2:0]carry_out;
adder1_behavorial A1(carry_out[0], sum[0], a[0], b[0], carry_in[0]);
adder1_behavorial A2(carry_out[1], sum[1], a[1], b[1], carry_in[1]);
adder1_behavorial A3(carry_out[2], sum[2], a[2], b[2], carry_in[2]);
initial
begin
carry_in = 0; a = 0; b = 0;
# 100 if ( carry_out != 0 | sum !== 0)
$display(" 0+0+0=00 sum is WRONG!");
else
$display(" 0+0+0=00 sum is RIGHT!");
carry_in = 0; a = 0; b = 1;
# 100 if ( carry_out != 0 | sum !== 1)
$display(" 0+0+1=01 sum is WRONG!");
else
$display(" 0+0+1=01 sum is RIGHT!");
carry_in = 0; a = 1; b = 0;
# 100 if ( carry_out != 0 | sum !== 1)
$display(" 0+1+0=01 sum is WRONG!");
else
$display(" 0+1+0=01 sum is RIGHT!");
carry_in = 0; a = 1; b = 1;
# 100 if ( carry_out != 1 | sum !== 0)
$display(" 0+1+1=10 sum is WRONG!");
else
$display(" 0+1+1=10 sum is RIGHT!");
carry_in = 1; a = 0; b = 0;
# 100 if ( carry_out != 1 | sum !== 0)
$display(" 1+0+0=01 sum is WRONG!");
else
$display(" 1+0+0=01 sum is RIGHT!");
carry_in = 1; a = 0; b = 1;
# 100 if ( carry_out != 1 | sum !== 0)
$display(" 1+0+1=10 sum is WRONG!");
else
$display(" 1+0+1=10 sum is RIGHT!");
carry_in = 1; a = 1; b = 0;
# 100 if ( carry_out != 1 | sum !== 0)
$display(" 1+1+0=10 sum is WRONG!");
else
$display(" 1+1+0=10 sum is RIGHT!");
carry_in = 1; a = 1; b = 1;
# 100 if ( carry_out != 1 | sum !== 1)
$display(" 1+1+1=11 sum is WRONG!");
else
$display(" 1+1+1=11 sum is RIGHT!");
$finish;
end
endmodule
module adder1_behavorial (carry_out, sum, a, b, carry_in);
input a, b, carry_in;
output carry_out, sum;
assign sum = (~a&b&~carry_in)|(~carry_in&a&~b)|(a&b&carry_in)|(~a&~b&carry_in);
assign carry_out = a&carry_in|a&b|b&carry_in;
endmodule
module fulladder (sum, c_out, a, b, c_in);
wire s1, c1, c2;
output sum;
output c_out;
input a, b, c_in;
assign{c_out,sum}=a+b+c_in;
endmodule
module adder3(sum, c_out, a, b, c_in);
wire [2:0] c;
output [2:0] sum;
output c_out;
input [2:0] a;
input [2:0] b;
input c_in;
fulladder fa1(sum[0], c[1], a[0], b[0], c_in) ;
fulladder fa2(sum[1], c[2], a[1], b[1], c[1]) ;
fulladder fa3(sum[2], c_out, a[2], b[2], c[2]) ;
endmodule
module main;
reg [2:0] a;
reg [2:0] b;
wire [2:0] sum;
wire c_out;
adder3 DUT (sum, c_out, a, b, 1'b0);
initial
begin
a = 4'b0101;
b = 4'b0000;
end
always #50 begin
b=b+1;
$monitor("%dns monitor: a=%d b=%d sum=%d", $stime, a, b, sum);
end
initial #2000 $finish;
endmodule