Can I check the length of the input text file?

In my VHDL project, my input will be extracted from a text file containing n bits 1 and 0. I try to make this as general as possible. I am familiar with how to read and write in a text file using test-bench, but I do not know how to check its length.

My code usually takes 64 bits as input, passes it through all the blocks, and generates output. If the remaining bit length is less than 64, then it passes through a specific block.

Say a text file contains 1000 bits. 15 x 64 = 960. 960 bits will go through all the blocks, the remaining 40 will go through a specific block. It looks straightforward, but to perform such operations I need to know the length of the text file. If someone can help, it will be very helpful.

+4
source share
1 answer

The length of the VHDL data structure should be taken into account, not the length of the file, since it is implementation-specific and not VHDL.

If the bit is in one long line, which should be divided into 64-bit fragments with the remainder, then the whole line can be read in VHDL type lineand reading from this line std_logic_vectorcan then depend on the remaining bits (characters) in the line.

The following is sample code:

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;

Btw, to answer the question about the "length of the input text file", then the length of the characters can be determined by reading as many characters from the file as possible, for example, with a code like:

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;
+3
source

All Articles