Calculations with Real Numbers, Verilog HDL

懵懂的女人 提交于 2020-01-04 06:33:52

问题


I noticed that Verilog rounds my real number results into integer results. For example when I look at simulator, it shows the result of 17/2 as 9. What should I do? Is there anyway to define something like a: output real reg [11:0] output_value ? Or is it something that has to be done by simulator settings?

Simulation only (no synthesis). Example:
x defined as a signed input and output_value defined as output reg.

output_value = ((x >>> 1) + x) + 5; 

If x=+1 then output value has to be: 13/2=6.5.

However when I simulate I see output_value = 6.


回答1:


Code would help, but I suspect your not dividing reals at all. 17 and 2 are integers, and so a simple statement like that will do integer division.

17   / 2   = 8 (not 9, always rounds towards 0)
17.0 / 2.0 = 8.5

In your second case

output_value = ((x >>> 1) + x) + 5

If x is 1, x >>> 1 is 0, not 0.5 because you've just gone off the bottom of the word.

output_value = ((1 >>> 1) + 1) + 5 = 0 + 1 + 5 = 6

There's nothing special about verilog here. This is true for the majority of languages.



来源:https://stackoverflow.com/questions/9745989/calculations-with-real-numbers-verilog-hdl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!