Verilog floating point to binary conversion

女生的网名这么多〃 提交于 2019-12-13 04:59:15

问题


I am trying convert a signed floating number in Verilog to a signed 24 bit binary value. For example

-0.0065 would become: 24'b100000000110101001111110
0.0901 would become: 24'b000001011100010000110010

Is this correct? Thank you


回答1:


Taking the fractional decimal 0.0901 and converting to a fixed-point value with 2 integer bits and 22 fractional bits (24 bit total):

Ruby syntax used for the maths (you can cut and paste into irb (interactive ruby) a command line tool):

i = (0.0901 * 2**20) # 377906.7904
# truncat to integer value
i = i.to_i # 377906

# Convert to string using binary (base 2)
i.to_s(2) # "1011100010000110010"

To add the leading zeros (24 bit length), right justify and pad with 0's

i.to_s(2).rjust(24, '0') # "000001011100010000110010"

# Convert to Hex (base 16)
i.to_s(16) # "5c432"

Signed numbers are a bit more problematic, easiest way is to calculate positive value then perform twos complement :

(0.0065 * 2**22).to_i.to_s(2).rjust(24, '0')
=> "000000000110101001111110"

Twos complement

"000000000110101001111110"
"111111111001010110000001" # Ones complement (bit invert)
"111111111001010110000001" + 1 
"111111111001010110000010" #Twos complement

You had 24'b100000000110101001111110 which is just the positive number with the MSB set to 1 which is not how signed numbers normally work. The format you have used is Sign Magnitude, but you can not just feed that into a multiplier (as per your previous question).

NB: I have also skipped over the quantisation effect of converting to fixed point. Your coefficient when scaled by your fractional bit was 377906.7904. but we just take the integer part giving you an error of 0.7904 which will may effect your filter performance.



来源:https://stackoverflow.com/questions/27161159/verilog-floating-point-to-binary-conversion

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