Search code examples
vhdltest-bench

Using integers from a large single line text file for testbench


I have a file with a large number of integers, it looks like:

123 254 360 700 800 900 1000 354 778 897 663 554 888 776 654 655 231 900 777 666 667 776 887 991 555 888 778 666 111 2232 444 545 667 ...  

I have written a VHDL code (algorithm) to analyze this file which stores the several characteristics of this file. I want to write a test bench for this algorithm which takes a value (integer) at a time gives it to my algorithm as an input and moves on to next value.

At the end of the file, this test bench should take the output stored in the algorithm write the result in a separate text file.

The caveat here is that its a large single line file separated by spaces. I tried solutions like Read textfile in VHDL testbench but they are not suitable for single line file.


Solution

  • If you are not memory-limited you can just read the whole line at once and then parse it one integer at a time until the last integer. Example:

    use std.textio.all;
    
    entity foo is
    end entity foo;
    
    architecture arc of foo is
    begin
      process
        variable l: line;
        file f: text is "foo.txt";
        variable i: integer;
        variable good: boolean;
      begin
        readline(f, l); 
        loop
          read(l, i, good);
          exit when not good;
          report integer'image(i);
        end loop;
        wait;
      end process;
    end architecture arc;
    

    If your file is larger than what your available memory can afford, you'll have to split the readline in smaller chunks but here std.textio will not help and you will have to use lower level read procedures and custom file type of characters. But, personally, I would probably use some shell utilities to split the super-long line is shorter ones. Programming in VHDL is funny but when the problem to solve is too far from hardware, the fun is getting less significant.