4 位加法器减法器 Verilog 代码错误

4-bit adder subtractor Verilog code errors

我想用 Verilog 代码做一个 4 位加法器减法器,但我的代码中有一些我无法弄清楚的问题。我不确定测试平台或 Verilog 是否有误。有人可以帮我吗?另外,当我尝试模拟它时,它会出现加载错误。

我的 Verilog 代码:

module addsubparameter (A, B, OP, C_out, Sum);

input A,B;
input OP;
output C_out;
output Sum;

wire C_out, Sum;
reg assigning;

always@(OP)
begin
if (OP == 0)
    assigning = A + B + OP;
else
    assigning = A + (~B + 1) + OP;
end

assign {C_out, Sum} = assigning;

endmodule

module adder (a, b, op, cout, sum);
parameter size = 4 ;

input [3:0] a, b;
output [3:0] sum;

input op;
output cout;
wire [2:0] c;

genvar i;
generate
for (i = 0; i < size; i = i + 1) begin: adder
    if (i == 0)
        addsubparameter (a[i], b[i], op, sum[i], c[i]);
    else if (i == 3) 
        addsubparameter (a[i], b[i], c[i-1], cout, sum[i]);
    else
        addsubparameter (a[i], b[i], c[i-1], sum[i], c[i]);
end 
endgenerate
endmodule

这是我的测试平台:

module addsub_tb();

reg [3:0] a;
reg [3:0] b;
reg op;

wire [3:0] sum;
wire cout;

adder DUT (a,b,op,sum,cout);

initial begin
    a = 4'b1010; b = 4'b1100; op = 1'b0; #100;
    a = 4'b1111; b = 4'b1011; op = 1'b1; #100;
    a = 4'b1010; b = 4'b1010; op = 1'b0; #100;
end
      
endmodule

您的模拟器应该生成错误 and/or 警告消息,因为您有语法错误。如果没有,请在 edaplayground 上注册一个免费帐户,在那里您可以访问多个模拟器,这些模拟器会产生有用的消息。

您需要添加实例名称。例如,我在下面的行中添加了 i0

    addsubparameter i0 (a[i], b[i], op, sum[i], c[i]);

您的端口连接宽度不匹配,这表示连接错误。当您使用按位置连接时,这是一种常见的错误类型。您错误地将 sum 信号连接到 cout 端口,反之亦然。您应该改用按名称连接。例如,更改:

adder DUT (a,b,op,sum,cout);

至:

adder dut (
    .a     (a),
    .b     (b),
    .op    (op),
    .cout  (cout),
    .sum   (sum)
);

对所有实例使用此编码风格。

您不会收到模拟警告,但您可能会收到有关敏感度列表不完整的综合警告。变化:

always@(OP)

至:

always @*