首页 > 解决方案 > 4 位加减器 Verilog 代码错误

问题描述

我正在尝试在 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

标签: verilogtest-bench

解决方案


您的模拟器应该生成错误和/或警告消息,因为您有语法错误。如果没有,请在 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 @*

推荐阅读