How to define sum result's width?

落花浮王杯 提交于 2019-12-25 04:24:01

问题


I have few unsigned, 8bits-wide number that i need to add/subtract together. Below the example:

 h_tmp <= signed(r4(calc_cnt - 2) + r4(calc_cnt - 1) + r4(calc_cnt) + 
                                        r4(calc_cnt + 1) + r4(calc_cnt + 2) - 
                                        r2(calc_cnt - 2) - r2(calc_cnt - 1) - r2(calc_cnt) -
                                        r2(calc_cnt + 1) - r2(calc_cnt + 2));

I know that a 13 bit wide result is ok for the numbers that I have, so i defined h_tmp as a signed (12 downto 0). Now, after synthesis I have the following warning

Width mismatch. <h_tmp> has a width of 13 bits but assigned expression is 8-bit wide.

It seems that the synthesiser inferred a 8bits-wide result of the calculation, what have I done incorrectly?


回答1:


Assuming the addition/subtraction (+/-) is based on the ieee.numeric_std package (or ieee.std_logic_arith and ieee.std_logic_unsigned), the result length of an addition/subtraction is that of the longest argument.

So if all your arguments in the addition/subtraction chain are 8 bits long, then all the additions are made as 8-bit additions/subtractions, even through you assign to a 13-bit result.

So start the addition/subtraction chain with resize of the first argument to the length of the result, like below for ieee.numeric_std package:

h_tmp <= signed(resize(r4(calc_cnt - 2), h_tmp'length) + r4(calc_cnt - 1) ...

For ieee.std_logic_arith package use conv_unsigned.



来源:https://stackoverflow.com/questions/28382730/how-to-define-sum-results-width

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