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

9
在调试Vivado中用户定义的物理类型处理时(阅读更多),我发现了从实数到整数的类型转换有不同的行为。
以下是我的示例代码:
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;

虚拟触发器用于防止某些工具在设计为空时出现错误。

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似乎使用“四舍五入”模式,并且它处理类型转换的包容性范围检查。因此,我必须使用“整数(-0.5)”而不是“自然数(-0.5)”。
[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似乎使用了“向无穷取整”的模式,并且它处理类型转换时没有进行范围检查。因此,也许naturall(..)只是integer(..)的别名。
注释行:constant int_2 : INTEGER := natural(-0.5);不会报错。 GHDL 0.29: GHDL 0.29在naturall(..)中不进行范围检查。 我知道这已经过时了,但由于0.31版本对我有敌意,我无法确定是否已经修复了这个问题。 GHDL 0.31: 稍后我将介绍结果。GHDL拒绝分析我的代码,因为:
Top_PhysicalTest_Simple.vhdl:29:14: file std_logic_1164.v93 has changed and must be reanalysed 我的问题:
  • VHDL定义了舍入模式吗?如果是,那么是哪一个?
  • 如果没有定义模式,应该如何处理舍入?

看起来你在0.31的基础上安装了另一个ghdl版本?如果你从源代码构建,那么进行干净的安装(可能是到不同的位置)或者使用“make install”命令可以解决标准库版本问题。 - user1818839
1
ghdl-0.32 ... ./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 - user1818839
抱歉搞混了:0.29版本在Debian上进行了测试(标准软件包),0.31版本是我的Windows机器(mcode版本)。安装和编译0.32版本仍然是一个任务 :) - Paebbels
ghdl -a top_phystest_simple.vhdl(完成)。ghdl --version GHDL 0.31(20140108)[Dunoon edition](在Mac OS X上,询问Brian Dunoon的含义)。您还可以在Debian上找到下载地址-http://sourceforge.net/projects/ghdl-updates/files/Builds/ghdl-0.31/Debian/. - user1155120
@DavidKoontz 问题是 ghdl -e ... 打印出了什么 :) 我尝试在我的Linux虚拟机上安装ghdl 0.31,但存在软件包版本不匹配的问题。Debian 8.0(测试版)libgnat-4.9,ghdl需要4.6,但这是另一天的问题... - Paebbels
显示剩余2条评论
2个回答

10

来自IEEE Std 1076-2002第7.3.5节“类型转换”

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 和/或 TRUNC)。


0
(因为无法在行内发布评论,所以将此作为答案发布...)
以下是使用预构建的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 拒绝分析我的代码"

如果您在 Windows mcode 构建的 0.31 上遇到库的问题,请尝试卸载该机器上任何 0.29 或更早版本的 GHDL NSIS 安装程序。 还要确保您按照 0.31 Windows INSTALL 中描述的整个设置过程运行,特别是 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\

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接