为什么`to_unsigned(0,4)> = -1`在运行时评估为`FALSE`?
使用下面的测试代码:为什么`to_unsigned(0,4)> = -1`在运行时评估为`FALSE`?
library ieee;
use ieee.numeric_std.all;
architecture sim of tb is
begin
process is
begin
for c in -1 to 1 loop
assert to_unsigned(0, 4) >= c report "Fails: 0 >= " & integer'image(c) severity NOTE;
end loop;
wait;
end process;
end architecture;
显示了使用的ModelSim 10.5A输出:
Loading work.tb(sim)
** Note: Fails: 0 >= -1
Time: 0 ns Iteration: 0 Instance: /tb
** Note: Fails: 0 >= 1
Time: 0 ns Iteration: 0 Instance: /tb
所以有效to_unsigned(0, 4) >= -1
评估,以FALSE
,而这并不是在运行时报道当我用for
循环。为什么是这样?
注意,如果我写to_unsigned(0, 4) >= -1
不使用for
循环用于获取在运行时-1
值,则ModelSim的编译器将报告-1(类型std.STANDARD.NATURAL),该“值超出范围0到2147483647 “。
TL/DR:无论您在哪里获得对Modelsim的技术支持,都可以问这个问题,然后用他们的回答更新问题。
快速重写和交叉校验与(一般相当严格和准确)ghdl模拟器:
library ieee;
use ieee.numeric_std.all;
entity tb is
end tb;
architecture sim of tb is
begin
process is
begin
for c in -1 to 1 loop
assert to_unsigned(0, 4)
>=
c
report "Fails: 0 >= " & integer'image(c) severity NOTE;
end loop;
wait;
end process;
end architecture;
ghdl -r tb
./tb:error: bound check failure at tb.vhd:14
./tb:error: simulation failed
示出了结合的检查错误恰恰在c
评价这是不内的合法范围为无符号或自然。
所以问题是,无论是ghdl还是Modelsim挑选不合适的>=
运算符?
numeric_std的来源仅显示两个>=
运算符定义,其中第一个参数是unsigned
。 (该文件是“版权2008”,从而为VHDL-2008标准。)
-- Id: C.19
function ">=" (L, R : UNRESOLVED_UNSIGNED) return BOOLEAN;
-- Result subtype: BOOLEAN
-- Result: Computes "L >= R" where L and R are UNRESOLVED_UNSIGNED vectors possibly
-- of different lengths.
-- Id: C.23
function ">=" (L : UNRESOLVED_UNSIGNED; R : NATURAL) return BOOLEAN;
-- Result subtype: BOOLEAN
-- Result: Computes "L >= R" where L is an UNRESOLVED_UNSIGNED vector and
-- R is a nonnegative INTEGER.
均未允许符号量(签名或整数)作为第二个参数。
因此,您可能需要查看安装中的numeric_std
的源代码,以查看它是否为不同的修订版本,其中>=
运算符允许混合的Unsigned和Integer数据类型。我怀疑它是否存在:它会很危险地容易出现这种误解。
如果没有这样的操作员,Modelsim在这里是宽容的;是否有编译选项来启用更严格的合规性?
如果你认为这是不必要的迂腐,考虑一下:
c := -1;
if to_unsigned(0, 4) >= c then
emergency_stop;
end if;
IEEE标准2076个至08年9.3.4函数调用,第5(部分):*函数调用的评价包括评价在调用中指定的实际参数表达式以及与函数形式参数关联的默认表达式的评估,这些表达式没有与它们相关的实际参数。在这两种情况下,结果值都应属于关联形式参数的子类型。 ... *表示一个强制性要求(1.3.1第4段),尽管Modelsim为了性能目的而选择它。 – user1155120
@BrianDrummond:在我的'numeric_std'包中没有额外的'> =',如果我在模块或包中创建了自己的类似函数,那么会立即报告负值。因此,它看起来像ModelSim针对'numeric_std'所做的性能优化,正如@ user1155120所示。我不会试图将它交给Mentor,因为几乎不可能为ModelSim启动一个支持案例;至少我没有得到必要的关注。 – EquipDev