是否可以检查输入文本文件的长度?

Is it possible to check the length of an input text file?

在我的 VHDL 项目中,我的输入将从包含 n 位 1 和 0 的文本文件中提取。我试图让它尽可能通用。我熟悉如何使用测试台读写文本文件,但我不知道如何检查它的长度。

我的代码通常采用 64 位作为输入,将它们传递给所有块并生成输出。如果剩余位长度小于 64,则它通过特定块。

假设文本文件包含 1000 位。 15 x 64 = 960。960 位将通过所有块,其余 40 位将通过特定块。这看起来很简单,但为了让我做这样的操作,我需要知道文本文件的长度。如果有人可以提供帮助,那将是非常有益的。

应考虑 VHDL 数据结构长度,而不是文件长度,因为这是特定于实现而不是指定的 VHDL。

如果这些位在一个长字符串中,要被分割成 64 位的片段并留余数,那么整个字符串可以读入 VHDL line 类型,并从该行读取std_logic_vector 类型然后可以取决于行中的剩余位(字符)。

下面是这样做的代码示例:

library ieee;
use std.textio.all;
use ieee.std_logic_textio.all;  -- Synopsys package; required for VHDL-2002 only

architecture syn of tb is
begin
  process is
    variable myl_v : line;
    file txt_file : text;
    variable slv_v : std_logic_vector(63 downto 0);
  begin
    file_open(txt_file, "input.txt", read_mode);
    readline(txt_file, myl_v);
    while myl_v'length > 0 loop
      if myl_v'length >= slv_v'length then  -- Full slv_v
        report "Full...: " & myl_v.all(1 to slv_v'length);
        read(myl_v, slv_v);
      else  -- Reduced slv_v
        report "Reduced: " & myl_v.all(1 to myl_v'length);
        read(myl_v, slv_v(myl_v'length - 1 downto 0));  -- Place reduced at LSBs
      end if;
    end loop;
    file_close(txt_file);
    wait;
  end process;
end architecture;

顺便说一下,要回答 "length of an input text file" 的问题,那么可以通过从文件中读取尽可能多的字符来确定字符长度,例如使用如下代码:

impure function file_length_in_characters(filename : string) return natural is
  type char_file_t is file of character;
  file char_file : char_file_t;
  variable char_v : character;
  variable res_v : natural;
begin
  res_v := 0;
  file_open(char_file, filename, read_mode);
  while not endfile(char_file) loop
    read(char_file, char_v);
    res_v := res_v + 1;
  end loop;
  file_close(char_file);
  return res_v;
end function;