如何在VHDL中读取一行中的元素?

Omn*_*ous 5 file-io vhdl

我正在尝试使用 VHDL 读取可能具有不同格式的文件。我知道您应该使用以下两行代码一次读取一行,读取该行中的各个元素。

readline(file, aline);
read(aline, element);
Run Code Online (Sandbox Code Playgroud)

然而我的问题是会read(aline, element)返回什么element?如果该行为空,它会返回什么?如果我使用它 5 次并且我的行只有 4 个字符,它会返回什么?

我想知道的原因是,如果我正在读取有效数据之间具有任意数量空格的文件,我如何解析这些有效数据?

该文件包含由任意数量的空格(任意数量的空格、制表符或换行符)分隔的 ASCII 字符。如果该行以 # 开头,则该行是注释,应被忽略。

除了这些注释之外,文件的第一部分包含的字符仅为可变大小的字母或数字组合。换句话说:

readline(file, aline);
read(aline, element);
Run Code Online (Sandbox Code Playgroud)

然而,文件的大部分(在读取一定数量的字之后)将是纯粹的任意长度的数字,并由任意数量的空格分隔。换句话说,文件的第二部分是这样的:

123           ABC   12ABB3 
Run Code Online (Sandbox Code Playgroud)

我必须能够单独解析这些数字(并如此解释它们)。

Mat*_*lor 4

正如评论中提到的,除了和版本之外,和中的所有read过程都会跳过前导空格(因为空格与任何其他字符一样是一个字符)。std.textioieee.std_logic_textiocharacterstring

您可以像这样测试line变量(缓冲区)是否为空:

if L'length > 0 then
Run Code Online (Sandbox Code Playgroud)

L你的变量在哪里line。还有一组read带有额外状态输出的重载过程:

procedure read (L    : inout LINE;
                VALUE: out   <type> ;
                GOOD : out   BOOLEAN);
Run Code Online (Sandbox Code Playgroud)

额外的输出 - GOOD- 是true读取是否成功false。这些的优点是,如果读取不成功,模拟不会停止(与常规程序一样)。另外,对于 中的版本std.textio,如果读取不成功,则读取是非破坏性的(即您尝试读取的任何内容都保留在缓冲区中)。然而,中的版本并非如此ieee.std_logic_textio

如果您确实不知道要读取什么格式,您可以将整行读入字符串,如下所示:

variable S : string(1 to <some big number>);
...
readline(F, L);
assert L'length < S'length;  -- make sure S is big enough
S := (others => ' '); -- make sure that the previous line is overwritten
if L'length > 0 then
  read(L, S(1 to L'length);
end if;
Run Code Online (Sandbox Code Playgroud)

该行L现在位于字符串中S。然后您可以编写一些代码来解析它。您可能会发现type 属性 'value很有用。这会将字符串转换为某种类型,例如

variable I : integer;
...
I := integer'value(S(12 to 14));
Run Code Online (Sandbox Code Playgroud)

会将 integer 设置I为 string 的元素 12 到 14 中包含的值S

另一种方法,如下面 user1155120 所建议的,是查看缓冲区中的值,例如

if L'length > 0 then  -- check that the L isn't empty, otherwise the next line blows up
  if L.all(1) = '#' then
    -- the first character of the line is a '#' so the line must be a comment
Run Code Online (Sandbox Code Playgroud)