问题描述
我想要的功能是添加 A、B 和 cin(其中 A 和 B 是 64 位,cin 是 1 位)。所以实际总和(实际结果)可以是 64 位甚至 65 位,对吗?所以我希望输出“sum”为 64 位,然后进位输出“cout”将保存结果的最高有效位。尝试通过 assign
语句执行此操作,但我想这不是您的操作方式,因为它会出错。还有什么办法吗?
module addsub(A,B,cin,sum,cout);
input [63:0] A,B;
input cin;
output reg [63:0] sum;
output cout;
reg [64:0] actualsum; // the actual result of the addition,which I want to then "split" into cout & sum
always @(A or B or cin) begin
actualsum = A + B + cin;
sum <= actualsum[63:0];
cout <= actualsum[64];
end
endmodule
解决方法
我得到的编译错误是由于对 cout
的程序赋值(在 always
块内)。要解决此问题,您可以将 cout
声明为 reg
。
良好的编码实践建议您对组合逻辑使用阻塞赋值 (=
) 而不是非阻塞赋值 (<=
)。
一种更简单、更传统的编码方式是:
module addsub (
input [63:0] A,B,input cin,output [63:0] sum,output cout
);
assign {cout,sum} = A + B + cin;
endmodule