这真的不应该这么难。
我想阅读原始的 64 位 IEEE 754 double-precision floating-point文件中的数据,并在 std_logic_vector(63 downto 0)
中使用它。我正在使用 ModelSim ALTERA 10.1b。
我尝试将原始二进制数据读入 64 位向量:
type double_file is file of std_logic_vector(63 downto 0);
file infile1: double_file open read_mode is "input1.bin";
variable input1 : std_logic_vector(63 downto 0) := (others => '0');
read(infile1, input1);
但这行不通。显然,ModelSim 试图将输入数据的每个 byte 解释为 std_logic
('U'
, 'Z'
, '-'
等)。
不过,我可以成功地将数据读入真实的
变量:
type real_file is file of real;
file infile1: real_file open read_mode is "input1.bin";
variable input1 : real;
read(infile1, input1);
但此时,我无法弄清楚如何将该真实
变量转换为std_logic_vector(63 downto 0)
。几乎所有 Google results只是说“你不能这样做;real
是不可合成的”。我完全理解 - 这只是为了模拟。
最佳答案
关键是 ieee.float_pkg
.
首先,您使用 to_float
将 real
转换为 float :
variable in1_r : real;
variable in1_f : float64;
in1_f := to_float(in1_r, in1_f); -- in1_f passed for sizing
然后,您只需将 float64
转换为 slv:
variable in1_slv : std_logic_vector(63 downto 0);
in1_slv := to_std_logic_vector( in1_f );
这也可以用一行代码来完成,消除中间的 float64
:
in1_slv <= to_std_logic_vector( to_float(in1_r, float64'high, -float64'low) );
关键是 to_float
需要知道目标大小。由于我们没有中间 float64
值,我们可以直接传递 exponent_width
和 fraction_width
参数,使用 的定义float64
。查看 to_float64
的定义有帮助。
关于floating-point - 将 real 转换为 IEEE double std_logic_vector(63 downto 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17780101/