More Reading from Files

In addition to reading data, it's also possible to read commands from files. This will be discussed in this section by extending the already introduced file_read.vhd.

The new version will read hex data and will also understand a command: #count. Each time it is found in the input file the file reader shall shall count from 1 to 5 in binary format and present that count on the output port. Unfortunately the file I/O of VHDL is not very sophisticated. It's not allowed to read a string from a file where the string is longer than the number of characters in that line.

Here is what needs to be done in order to read variable length strings from an input file:


        readline(stimulus, l);
        s := (others => ' ');
        for i in s'range loop
           read(l, c, in_string);
           s(i) := c;
           if not in_string then -- found end of line 
              exit;
           end if;
        end loop;
The read function will return false for in_string once the last character of the line has been read.

The above function has been placed in txt_util.vhd and named str_read(stimulus, s). The length of s determines the maximum number of characters in a line which can be evaluated.

Using this function the following code will implement the set task:

        while not endfile(stimulus) loop
          str_read(stimulus, s);

          if s(1 to 6) = "#count" then -- check for command "count" 
             for i in 1 to 5 loop
                 Y <= conv_std_logic_vector(i,5);
                 wait until CLK = '1';
             end loop;
            else
               -- if it's not a command -> process data normally 
               Y <= to_std_logic_vector(s to 5);
               wait until CLK = '1';
          end if;

        end loop;

        print("I@FILE_READ: reached end of "& stim_file);
        EOG <= '1';

        wait;

Note that the appropriate sub-section of the string s needs to be compared with #count as comparing different length strings will always yield the result false.

Here is an input file making use of the #count-command.

        00010
        00011
        #count
        11100
        1UXZW
        HL111
        11111
The resulting waveforms are thus:

Below are the files which have been simulated in this section:

txt_util.vhd file_read2.vhd sim2.cmd tb_file_read2.vhd
txt_util.vhd file_read2.vhd sim2.cmd tb_file_read2.vhd


previous Index next