如何在verilog中将数字转换为二进制补码?
我正在尝试用 verilog 设计一个 4 位加法器减法器。 这只是我用 verilog 编写的第二件事,而且我还不知道所有正确的语法。 这是我迄今为止拥有的模块:
module Question3(carryin, X, Y, Z, S, carryout, overflow);
parameter n = 4;
input carryin, Z;
input [n-1:0]X, Y;
output reg [n-1:0]S;
output reg carryout, overflow;
if(Z==0)
begin
Y = not(y) + 4'b0001;
end
always @(X, Y, carryin)
begin
{carryout, S} = X + Y + carryin;
overflow = carryout ^ X[n-1]^Y[n-1]^S[n-1];
end
endmodule
我的编译器(xilinx 10.1)不断显示“if 附近的语法错误”。 我尝试了许多不同的转换方法,包括仅使用以 Y 作为参数的 Case,然后检查所有可能的 4 位组合,并将它们转换为二进制补码。
Z 决定加法器是否执行减法或加法。 如果是0,就意味着减法,我想把y转成补码,然后做正则加法就可以了。 我确信加法器的其余部分是正确的,我只是不知道我尝试转换的部分出了什么问题。
I am trying to design a 4-bit adder subtracter in verilog. This is only the second thing I have ever written in verilog, and I don't know all the correct syntax yet. This is the module I have so far:
module Question3(carryin, X, Y, Z, S, carryout, overflow);
parameter n = 4;
input carryin, Z;
input [n-1:0]X, Y;
output reg [n-1:0]S;
output reg carryout, overflow;
if(Z==0)
begin
Y = not(y) + 4'b0001;
end
always @(X, Y, carryin)
begin
{carryout, S} = X + Y + carryin;
overflow = carryout ^ X[n-1]^Y[n-1]^S[n-1];
end
endmodule
My compiler (xilinx 10.1), keeps saying "Syntax error near if." I have tried many different ways of doing the conversion, including just using a Case that takes Y as an argument, then checks all the possible 4-bit combinations, and converts them to two's complement.
Z is what determines if the adder does subtraction or addition. If it's 0, it means subtraction, and I want to convert y to two's complement, then just do regular addition. I'm sure the rest of the adder is correct, I just do not know what is wrong with the part where I'm trying to convert.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几点很重要。
原始。 综合工具可以更自由地以这种方式优化您的代码。
A couple of important points.
primitive. The synthesis tool has more freedom to optimize your code this way.
您在“Y = not(y) + 4'b0001;”中使用小写“y”
此外,您使用的添加物超出了您的需要。 XY 与 NOT(NOT(X)+Y) 相同。
You are using a lower case "y" in "Y = not(y) + 4'b0001;"
Also, you're using more additions than you need to. X-Y is the same thing as NOT(NOT(X)+Y).
将 if 语句放在初始块中
http://www.asic-world.com/verilog/vbehave1.html
put the if statement within an initial block
http://www.asic-world.com/verilog/vbehave1.html