繁体   English   中英

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

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

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

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

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

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

至:

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

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

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

always@(OP)

至:

always @*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM