如何检测verilog中的溢出?
所以我的目标是在添加两个 64 位变量 A 和 B 时检测溢出。我尝试了一些方法但没有成功。起初我做了:
if ((A[63])== B[63]==1)
Overflow=1;
那不起作用,所以我做了:
if(A != (ALU_Result-B))
Overflow=1;
ALU_Result 是我的结果顺便说一句。我认为它保存的值将是第一个 64 位,因此这个方程将给出标志,但这也不起作用。如何创建旗帜?另外,如果发生溢出,那么进位是否自动为1?
So my goal is to detect overflow when adding two 64-bit variables A and B. I tried a couple things that didn't work out. At first I did:
if ((A[63])== B[63]==1)
Overflow=1;
That didn't work so I did:
if(A != (ALU_Result-B))
Overflow=1;
ALU_Result is my result btw. I thought that the value it held will be the first 64 bit so this equation would give the flag but this didn't work either. How do I create the flag? Also, if there is overflow, then is the carry automatically a 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于无符号数,当总和小于任一操作数时,就会发生溢出。
对于有符号(二进制补码)数,当两个操作数均为负但总和为正时,或者两个操作数均为正且总和为负时,就会发生溢出。
此 Verilog 示例对于无符号情况使用 8 位数字,对于有符号情况使用 32 位数字。数字的大小并不重要。
使用操作数的符号和结果检查溢出,如上面详述,并在下面的代码中说明。
Verilog ex在测试平台中添加无符号、无符号数字并检测溢出的过程:
测试平台运行结果如下:
For unsigned numbers, overflow occurs when the sum is less than either of the operands.
For signed (two's complement) numbers, overflow occurs when both operands are negative, but the sum is positive, or if both operands are positive and the sum is negative.
This Verilog example uses 8 bit numbers for the unsigned case and 32 bit numbers for the signed case. It does not matter what size the numbers are.
Check for overflow using sign of the operands and result as detailed above, and illustrated in the code below.
Verilog ex in a testbench process of adding unsigned, unsigned numbers and detecting overflow:
Testbench run results here:
最直接的方法是使用一个临时变量,该变量有一个额外的位,这样总和就不会溢出。我已经展示了如何处理无符号数字。通过检查正溢出和负溢出,可以很容易地将其扩展到有符号数。综合工具应该足够智能,可以简化此逻辑,而无需进行任何位操作。
对无符号数执行此操作的另一种方法是直接使用该额外位作为溢出标志。此选项不会剪切您的结果,但我不确定您是否关心这一点:
The most direct way to do this is to have a temporary variable which has an extra bit so the sum does not overflow. I have shown how to handle this for unsigned numbers. It is easy enough to expand this to signed numbers by checking for both positive and negative overflow. Synthesis tools should be smart enough to simplify this logic without you doing any bit manipulation
Another way to do it for unsigned numbers is to directly use that extra bit as the overflow flag. This options doesn't clip your result, but I am not sure you care about that: