verilog - 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
解决方案
您的模拟器应该生成错误和/或警告消息,因为您有语法错误。如果没有,请在 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 @*
推荐阅读
- groovy - 在 JMeter 5.3 中,变量在函数中不起作用
- excel - 是否有可扩展的公式将数据块转换为垂直列表?
- html - 如何在IOS中禁用自动省略号
- php - 如何跳过 loop_shop_per_page 中不可用的产品?
- c# - BUG:使用“eml”文件附件(通过 Outlook 互操作 API)发送的电子邮件未正确编码
- c++ - c++中多个字符串中的元音数。输入与停用词“end”一样多的字符串,输出必须是每个字符串中的元音数
- java - 有没有办法将 HttpServletRequest 转换为 PageContext
- python - 问题索引从 xml 中提取的数据
- python - playwright._impl._api_types.Error:评估失败:检索 dataLayer 变量时的循环对象值 - Playwright-Python
- r - 如何使用 taskscheduleR 自动化 R 中的脚本?