【发布时间】:2018-09-07 22:01:52
【问题描述】:
我有这个定义:
subtype sample_t is signed(SAMPLE_WIDTH-1 downto 0);
现在在代码中我想将信号设置为 sample_t 的最大值除以 2:
signal max_sample : sample_t;
max_sample <= to_signed(max_sample'<some attribute>/2,max_sample'LENGTH);
我查看了属性 sample_t'HIGH 和 sample_t'RIGHT 但它们似乎返回了数组的最高下标。
从此列表中:https://www.csee.umbc.edu/portal/help/VHDL/attribute.html
T'HIGH is the highest value of type T.
A'HIGH is the highest subscript of array A or constrained array type.
如何在sample_t上使用第一个定义?
其中一位评论者的建议:
max_sample <= (max_sample'LEFT => '0', others => '1');
有效。但是这个:
max_sample <= (max_sample'LEFT => '0', others => '1')/2;
因“OTHERS 是不受约束的目标的非法聚合选择”而失败。
为什么会出现这个错误?
【问题讨论】:
-
谢谢,如果您将评论添加为回复,我可以将问题标记为已回答。
-
@DinneBosman:你不需要除法,你可以简单地利用二进制数的位置特性。如果最大值是 (sign=0)11...1,那么(那个数字是奇数)一半落在 (sign=0)01...1 或 (sign=0)10...0 之间
-
您也可以完全依赖 sample_t 声明,允许 max_sample 为常量
constant max_sample: sample_t := SHIFT_RIGHT(sample_t'(sample_t'LEFT => '0', others => '1'), 1);或constant max_sample: sample_t := sample_t'(sample_t'LEFT => '0', others => '1')/2;。 -
注意:除以 2 与右移 1 相同...可能会提示您如何简化事情。