在我的VHDL项目中,我的输入将从包含n位1和0的文本文件中提取。我正在尝试使其尽可能通用。我熟悉如何使用测试台对文本文件进行读写,但是我不知道如何检查其长度。
我的代码通常以64位作为输入,将它们传递通过所有块并生成输出。如果剩余位的长度小于64,则它将通过特定的块。
假设该文本文件包含1000位。15 x 64 =960。960位将通过所有块,其余40位将通过特定块。这看起来很简单,但是为了让我执行此类操作,我需要知道文本文件的长度。如果有人可以帮助,那将是非常有益的。
应该考虑VHDL数据结构的长度,而不是文件长度,因为这是实现特定的,而不是VHDL。
如果位在一个长字符串中,剩下的将被切成64位,那么整个字符串可以读取为VHDLline
类型,然后从该行读取到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;
顺便说一句,要回答“输入文本文件的长度”的问题,那么可以通过从文件中读取尽可能多的字符来确定字符长度,例如,使用以下代码:
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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句