Guest
For some reason I am having problems with the output of this code for
the Carry-look-ahead and half adder. It took me some time to code it
and now I am having issues getting the correct wave outputs. Please
help me!! My test bench is include.
================================================================
//Verilog Code: 4-bit CLA adder (CLA.v)
module Add_half(sum, c_out, a, b);
input a, b;
output c_out, sum;
assign {cout,sum}= a + b;
endmodule
module cla4(c_output,c_in_0,g,p);
input c_in_0;
output [3:0] c_output; //declare 4-bit carry output
reg [3:0] c_output; //declare 4-bit carry output as reg for
always process
input [3:0] g; //declare 4-bit input (g)
input [3:0] p; //declare 4-bit input (p)
always @ (g or p) //start process, which starts soon as input
changes
begin
c_output[1] = g[0] + p[0]*c_in_0; //first equation,
c_output[2] = g[1] + p[1]* (g[0] + p[0]*c_in_0); //second
equation,
c_output[3] = g[2] + p[2]* (g[1] + p[1]* (g[0] +
p[0]*c_in_0) ); //third equation
end
endmodule
module TOP_MOD(a,b,s,in_bit);
input [3:0] a;
input [3:0] b;
input in_bit;
output [3:0] s; //final output values
wire [3:0] p;
wire [3:0] g;
wire [2:0] out_cla; //for the inputs of xor gates
Add_half U0 (.a(a[0]),.b(b[0]),.c_out(g[0]), .sum(p[0]));
Add_half U1 (.a(a[1]),.b(b[1]),.c_out(g[1]), .sum(p[1]));
Add_half U2 (.a(a[2]),.b(b[2]),.c_out(g[2]), .sum(p[2]));
Add_half U3 (.a(a[3]),.b(b[3]),.c_out(g[3]), .sum(p[3]));
cla4 U4(.c_output(out_cla), .c_in_0(in_bit), .g(g), .p(p));
assign s[0]= p[0]^in_bit;
assign s[1]= p[1]^out_cla[1];
assign s[2]= p[2]^out_cla[2];
assign s[3]= p[3]^out_cla[3];
endmodule
===============================================================
//Verilog Code: Testbench for CLA (CLA_tb.v)
module TB_CLA( );
parameter In_width_a = 4;
parameter In_width_b = 4;
parameter Out_width_s = 4;
reg[In_width_a-1:0]in_a_tb;
reg[In_width_b-1:0]in_b_tb;
reg c_in;
wire[Out_width_s-1:0]out_s_tb;
TOP_MOD U0(.a(in_a_tb),.b(in_b_tb),.s(out_s_tb),.in_bit(c_in));
initial
begin
in_a_tb = 4'b0000;
in_b_tb = 4'b0000;
c_in = 1'b0;
#50 in_a_tb = 4'b0001; in_b_tb = 4'b0000; c_in = 1'b0;
#50 in_a_tb = 4'b0011; in_b_tb = 4'b0010; c_in = 1'b1;
#50 in_a_tb = 4'b0001; in_b_tb = 4'b0000; c_in = 1'b0;
#50 in_a_tb = 4'b0111; in_b_tb = 4'b0000; c_in = 1'b1;
#50 in_a_tb = 4'b1111; in_b_tb = 4'b0001; c_in = 1'b0;
#50 in_a_tb = 4'b0101; in_b_tb = 4'b0010; c_in = 1'b1;
#50 $stop;
#20 $finish;
end
endmodule
================================================================
the Carry-look-ahead and half adder. It took me some time to code it
and now I am having issues getting the correct wave outputs. Please
help me!! My test bench is include.
================================================================
//Verilog Code: 4-bit CLA adder (CLA.v)
module Add_half(sum, c_out, a, b);
input a, b;
output c_out, sum;
assign {cout,sum}= a + b;
endmodule
module cla4(c_output,c_in_0,g,p);
input c_in_0;
output [3:0] c_output; //declare 4-bit carry output
reg [3:0] c_output; //declare 4-bit carry output as reg for
always process
input [3:0] g; //declare 4-bit input (g)
input [3:0] p; //declare 4-bit input (p)
always @ (g or p) //start process, which starts soon as input
changes
begin
c_output[1] = g[0] + p[0]*c_in_0; //first equation,
c_output[2] = g[1] + p[1]* (g[0] + p[0]*c_in_0); //second
equation,
c_output[3] = g[2] + p[2]* (g[1] + p[1]* (g[0] +
p[0]*c_in_0) ); //third equation
end
endmodule
module TOP_MOD(a,b,s,in_bit);
input [3:0] a;
input [3:0] b;
input in_bit;
output [3:0] s; //final output values
wire [3:0] p;
wire [3:0] g;
wire [2:0] out_cla; //for the inputs of xor gates
Add_half U0 (.a(a[0]),.b(b[0]),.c_out(g[0]), .sum(p[0]));
Add_half U1 (.a(a[1]),.b(b[1]),.c_out(g[1]), .sum(p[1]));
Add_half U2 (.a(a[2]),.b(b[2]),.c_out(g[2]), .sum(p[2]));
Add_half U3 (.a(a[3]),.b(b[3]),.c_out(g[3]), .sum(p[3]));
cla4 U4(.c_output(out_cla), .c_in_0(in_bit), .g(g), .p(p));
assign s[0]= p[0]^in_bit;
assign s[1]= p[1]^out_cla[1];
assign s[2]= p[2]^out_cla[2];
assign s[3]= p[3]^out_cla[3];
endmodule
===============================================================
//Verilog Code: Testbench for CLA (CLA_tb.v)
module TB_CLA( );
parameter In_width_a = 4;
parameter In_width_b = 4;
parameter Out_width_s = 4;
reg[In_width_a-1:0]in_a_tb;
reg[In_width_b-1:0]in_b_tb;
reg c_in;
wire[Out_width_s-1:0]out_s_tb;
TOP_MOD U0(.a(in_a_tb),.b(in_b_tb),.s(out_s_tb),.in_bit(c_in));
initial
begin
in_a_tb = 4'b0000;
in_b_tb = 4'b0000;
c_in = 1'b0;
#50 in_a_tb = 4'b0001; in_b_tb = 4'b0000; c_in = 1'b0;
#50 in_a_tb = 4'b0011; in_b_tb = 4'b0010; c_in = 1'b1;
#50 in_a_tb = 4'b0001; in_b_tb = 4'b0000; c_in = 1'b0;
#50 in_a_tb = 4'b0111; in_b_tb = 4'b0000; c_in = 1'b1;
#50 in_a_tb = 4'b1111; in_b_tb = 4'b0001; c_in = 1'b0;
#50 in_a_tb = 4'b0101; in_b_tb = 4'b0010; c_in = 1'b1;
#50 $stop;
#20 $finish;
end
endmodule
================================================================