Verilog 4位加法器返回错误结果

Verilog 4位加法器返回错误结果,verilog,Verilog,我试着用4个全加器做一个4位加法器,但结果总是不正确。当a=3,b=8时,我得到的和是9,而不是11。我看不出有错。有人能找出我做错了什么吗 您的某些连接错误。您正在将c_o[0]连接到full_adder的sum端口(与c_o[2:1]和c_out相同)。按位置实例化时,端口顺序很重要。最快的修复方法是使用: module adder4( output[3:0] sum, output c_out, // carry out input[3:0] a, b, // opera

我试着用4个全加器做一个4位加法器,但结果总是不正确。当a=3,b=8时,我得到的和是9,而不是11。我看不出有错。有人能找出我做错了什么吗

您的某些连接错误。您正在将
c_o[0]
连接到
full_adder
sum
端口(与
c_o[2:1]
c_out
相同)。按位置实例化时,端口顺序很重要。最快的修复方法是使用:

module adder4(
output[3:0] sum,
output c_out,       // carry out
input[3:0] a, b,    // operands
input c_in);        // carry in

wire [2:0] c_o;
full_adder fa1(c_o[0],sum[0],a[0],b[0],c_in);
full_adder fa2(c_o[1],sum[1],a[1],b[1],c_o[0]);
full_adder fa3(c_o[2],sum[2],a[2],b[2],c_o[1]);
full_adder fa4(c_out,sum[3],a[3],b[3],c_o[2]);

endmodule

module full_adder(
    output sum,
    output c_out,   // carry out
    input a,
    input b,
    input c_in);    // carry in

wire sum1;
wire c_in1;
wire c_out2;
    half_adder ha1(sum1,c_in1,a,b);
    half_adder ha2(sum,c_out2,sum1,c_in);
    or(c_out,c_out2,c_in1);
endmodule


module half_adder(
    output sum,
    output c_out,   // carry out
    input a,
    input b);

assign sum=a^b;
assign c_out=a&&b;

endmodule
通常,通过名称而不是位置来实例化是更好的做法。显然,这更详细,但它确实有助于避免您的接线错误类型:

module full_adder(
    output c_out,   // carry out
    output sum,

您还可以利用实例阵列:

full_adder fa1 (.c_out(c_o[0]) , .sum(sum[0]), .a(a[0]), .b(b[0]), .c_in(c_in));
full_adder fa[3:0] (
    .c_out  ({c_out, c_o}),
    .sum    (sum),
    .a      (a),
    .b      (b),
    .c_in   ({c_o, c_in})
);