创建包含可由VHDL / modelsim读取的REAL值的文件

时间:2014-03-17 13:56:55

标签: io vhdl modelsim

我想做什么

我希望在python或matlab中创建一个脚本,该脚本可以创建VHDL / modelsim作为real值文件读取的文件。

到目前为止我做了什么

我已经编写了一个小型VHDL实体,可以创建real个值的文件,这样我就可以找到它使用的格式。它看起来像这样:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;

library std;
use std.textio.all;

entity read_arr is
end entity read_arr;

architecture reader of read_arr is

  type t_real_arr is array (0 to 1023) of real;
  signal real_arr : t_real_arr := (others => 0.0);
  signal start    : std_logic := '0';
  signal filled   : std_logic := '0';
  type t_real_file is file of t_real_arr;
  file real_file : t_real_file;

begin  -- architecture reader

  start <= '0', '1' after 10 ns;

  fill_arr : process (start) is
    variable real_fill : real := -10.0;
  begin  -- process fill_arr
    if rising_edge(start) then
      for i in real_arr'range loop
        real_arr(i) <= real_fill;
        real_fill := real_fill + 0.25;
      end loop;  -- i
      filled <= '1';
    end if;
  end process fill_arr;

  wr_arr : process (filled) is
    variable filename : string (1 to 10) := "realvc.dat";
    variable status : file_open_status := status_error;
  begin  -- process fill_arr
    if rising_edge(filled) then
      file_open(status, real_file, filename, write_mode);
      write(real_file, real_arr);
      file_close(real_file);
    end if;
  end process wr_arr;

end architecture reader;

它的作用是填充1024个元素的数组,其中real值从-10.0开始并以0.25为单位递增。然后它将此数据写入二进制文件realvc.dat。可以看到文件的内容(使用十六进制编辑器查看)in this gist 很容易看出modelsim使用64位来存储每个real值,但我还没有弄清楚它是什么样的格式。它不是双精度的。

问题

有谁知道什么样的数据格式以及我如何用脚本语言(如python或matlab)重新创建它?

1 个答案:

答案 0 :(得分:1)

标准VHDL包std.textio可以读取/写入real 文本格式到文本文件,因此像Python这样的脚本语言可以 轻松访问它。从而避免了二进制格式,从而提高了可移植性。

类似于问题代码的示例,首先编写然后读取real数组std.textio

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

library std;
use std.textio.all;

entity test_arr is
end entity test_arr;

architecture test of test_arr is

begin

  process is

    type     real_arr_t is array (0 to 1023) of real;
    variable real_arr_v  : real_arr_t := (others => 0.0);
    variable real_fill_v : real := -10.0;

    procedure real_arr_write(variable real_arr : in real_arr_t) is
      file txt_file   : text;
      variable real_v : real;
      variable line_v : line;
    begin
      file_open(txt_file, "real.txt", write_mode);
      for idx in real_arr'range loop
        write(line_v, real_arr(idx));
        writeline(txt_file, line_v);
      end loop;
      file_close(txt_file);
    end procedure;

    procedure real_arr_read(variable real_arr : out real_arr_t) is
      file txt_file   : text;
      variable line_v : line;
      variable good_v : boolean;
    begin
      file_open(txt_file, "real.txt", read_mode);
      for idx in real_arr'range loop
        readline(txt_file, line_v);
        read(line_v, real_arr(idx), good_v);
        assert good_v report "Failed convert to real of: " & line_v.all severity FAILURE;
      end loop;
      file_close(txt_file);
    end procedure;

  begin

    -- Make and write real array to text file
    for i in real_arr_v'range loop
      real_arr_v(i) := real_fill_v;
      real_fill_v   := real_fill_v + 0.25;
    end loop;
    real_arr_write(real_arr_v);

    -- Read real array from text file
    real_arr_read(real_arr_v);

    -- End
    wait;

  end process;

end architecture test;

“real.txt”文件中的real值为:

-1.000000e+001
-9.750000e+000
-9.500000e+000
-9.250000e+000

Python 3可以使用'{:e}'.format(x)创建这样的字符串,并使用float(s)从此格式转换。