VHDL 中的类型转换:实数到整数 - 是否指定了舍入模式?

Type conversion in VHDL: real to integer - Is the rounding mode specified?

在调试 Vivado 中用户定义的物理类型的处理时 (read more),我发现了从实数到整数的类型转换的不同行为。

这是我的示例代码:

library IEEE;
use     IEEE.STD_LOGIC_1164.ALL;
--use     IEEE.MATH_REAL.all;

entity Top_PhysicalTest_Simple is
  port (
    Clock : in STD_LOGIC;
    Input : in STD_LOGIC;
    Output : out STD_LOGIC
  );
end;

architecture top of Top_PhysicalTest_Simple is
  constant int_1     : INTEGER  := natural(0.5);
  constant int_2     : INTEGER  := integer(-0.5);
--  constant int_2     : INTEGER  := natural(-0.5);
begin
  assert FALSE report "16 - int_1 (natural(0.5)):  " & INTEGER'image(int_1) severity note;
  assert FALSE report "17 - int_2 (natural(-0.5)): " & INTEGER'image(int_2) severity note;

  Output <= Input when rising_edge(Clock);
end;

dummy flip flop是用来防止一些工具报错空设计的。

XST 14.7:

Elaborating entity <Top_PhysicalTest_Simple> (architecture <top>) from library <work>.
Note: "16 - int_1 (natural(0.5)):  1"
Note: "17 - int_2 (natural(-0.5)): 0"

XST 似乎使用 round up 模式,它处理类型转换包含范围检查。 所以我必须使用 integer(-0.5) 而不是 natural(-0.5).

Vivado 2014.4:

[Synth 8-63] RTL assertion: "16 - int_1 (natural(0.5)):  1" ["D:/Temp/PhysicalTest_Vivado2014.4/vhdl/Top_PhysicalTest_Simple.vhdl":80]
[Synth 8-63] RTL assertion: "17 - int_2 (natural(-0.5)): -1" ["D:/Temp/PhysicalTest_Vivado2014.4/vhdl/Top_PhysicalTest_Simple.vhdl":81]

Synth 似乎使用模式 舍入到无穷大 并且它处理类型转换而不进行范围检查。所以也许 natural(..) 只是 integer(..).
的别名 注释行:constant int_2 : INTEGER := natural(-0.5); 不会引发错误。

GHDL 0.29:

GHDL 0.29 在 natural(..) 中不进行范围检查。 我知道它已经过时了,但由于 0.31 讨厌我,所以我不知道这是否已经修复。

GHDL 0.31:

稍后我会展示结果。 GHDL 拒绝分析我的代码,因为:
Top_PhysicalTest_Simple.vhdl:29:14: 文件 std_logic_1164.v93 已更改,必须重新分析

我的问题:

来自 IEEE Std 1076-2002 第 7.3.5 节 "Type conversions"

The conversion of a floating point value to an integer type rounds to
the nearest integer; if the value is halfway between two integers,
rounding may be up or down.

如果您想要其他东西,IEEE.MATH_REAL 中的函数可能会有一些用处(特别是 CEILFLOOR and/or TRUNC)。

( post 将此作为答案,因为我无法 post 内联评论... )

以下是使用预构建的 ghdl-0.31-mcode-win32 的结果:

  C:\brian\jobs\ghdl_test\paebbels>md work.ghd

  C:\brian\jobs\ghdl_test\paebbels>ghdl -a --workdir=work.ghd Top_PhysicalTest_Simple.vhd

  C:\brian\jobs\ghdl_test\paebbels>ghdl -r --workdir=work.ghd Top_PhysicalTest_Simple
  Top_PhysicalTest_Simple.vhd:18:3:@0ms:(assertion note): 16 - int_1 (natural(0.5)):  1
  Top_PhysicalTest_Simple.vhd:19:3:@0ms:(assertion note): 17 - int_2 (natural(-0.5)): -1

“0.31 是我的 Windows 机器(mcode 版本)” "GHDL refuses to analyse my code "

如果您在 0.31 的 Windows mcode 版本上遇到库问题,请尝试卸载该机器上任何 0.29 或更早版本的 GHDL NSIS 安装程序。 还要确保 运行 按照 0.31 Windows 安装中所述完成整个设置过程,特别是 reanalyze_libraries.bat

这是用于上述测试的版本:

  C:\brian\jobs\ghdl_test\paebbels>ghdl -v
  GHDL 0.31 (20140108) [Dunoon edition] + ghdl-0.31-mcode-win32.patch
  Compiled with GNAT Version: GPL 2013 (20130314)
  mcode code generator
  Written by Tristan Gingold.

  Copyright (C) 2003 - 2014 Tristan Gingold.
  GHDL is free software, covered by the GNU General Public License.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

以及库路径信息:

  C:\brian\jobs\ghdl_test\paebbels>ghdl --dispconfig
  command line prefix (--PREFIX): (not set)
  environment prefix (GHDL_PREFIX): C:\Ghdl\ghdl-0.31-mcode-win32\lib
  default prefix: C:\Ghdl\ghdl-0.31-mcode-win32\lib
  actual prefix: C:\Ghdl\ghdl-0.31-mcode-win32\lib
  command_name: C:\Ghdl\ghdl-0.31-mcode-win32\bin\ghdl.exe
  default library pathes:
  C:\Ghdl\ghdl-0.31-mcode-win32\lib\v93\std\
  C:\Ghdl\ghdl-0.31-mcode-win32\lib\v93\ieee\