【问题标题】:4-bit adder subtractor Verilog code errors4 位加减器 Verilog 代码错误
【发布时间】:2021-07-24 14:02:02
【问题描述】:

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

【问题讨论】:

    标签: verilog test-bench


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多