VHDL函数中可变长度unsigned定义报错(ISE 14.7)求助
ISE 14.7中VHDL动态位宽unsigned函数的编译问题解决
在维护旧系统使用ISE 14.7时,编译原除法取整函数会报错Constant value expected for expression (31 - num_bits),原因是ISE 14.7遵循VHDL-93标准,不允许在子程序内使用非静态表达式(即编译时无法确定值的表达式,比如输入参数或信号属性)定义局部变量的位宽。
原函数意图是将输入值除以2的num_bits次幂并完成取整操作,以下是适配ISE 14.7的修改方案:
适配方案:静态位宽临时变量+结果截取
通过声明一个覆盖最大可能位宽的静态临时变量,避免动态位宽的变量声明,最后根据实际计算的位宽截取结果返回:
function my_round(data_in : in unsigned; num_bits : in integer) return unsigned is -- 根据实际系统中data_in的最大位宽调整此处的静态范围,示例为32位(31 downto 0) variable temp : unsigned(31 downto 0); -- 计算最终结果的位宽 constant result_width : integer := data_in'length - num_bits; begin -- 初始化临时变量,避免未赋值的位干扰计算 temp := (others => '0'); -- 将输入值的高位部分复制到临时变量的对应位置 temp(result_width - 1 downto 0) := data_in(data_in'left downto num_bits); -- 执行取整判断:当高位不全为1且被截断的最低位为1时,结果加1 if my_and_reduce(temp(result_width - 1 downto 0)) = '0' and data_in(num_bits-1) = '1' then temp(result_width - 1 downto 0) := temp(result_width - 1 downto 0) + 1; end if; -- 返回截取后的正确位宽结果 return temp(result_width - 1 downto 0); end my_round;
关键说明
- 静态临时变量的位宽需要根据系统中
data_in的实际最大位宽设定,确保能覆盖所有可能的输入情况。 result_width通过输入参数的属性计算得到,用于最终结果的截取,保证返回值的位宽符合需求。- 临时变量初始化全0是为了避免未使用的高位影响
my_and_reduce的判断结果。
内容的提问来源于stack exchange,提问作者Jim Clay
相关产品推荐
相关产品推荐

