Search code examples
filepackageconstantsvhdlreadline

VHDL How to assign outputs from file as constants in package?


I want to read from a text file big arrays of integers and assign these arrays as constants in my package. How am I going to do this? I have made a process that reads from the file and the output is the array that I want:

       P1: process
       file vec_file: text open read_mode is "mytext";
       variable iline: line;
       variable data_read: integer;
       variable x: integer := 0;
       begin

          while not endfile (vec_file) loop
            readline (vec_file, iline);
            read(iline,data_read);
            wait until rising_edge(clk); 
            output(x) <= data_read;
            x := x + 1;

          end loop;
          wait until rising_edge(clk); 
          wait;
      END process P1;

But how am I going to assign this array to my package as a constant? Instead of the process, should I make a function in the package body? Thanks in advance!


Solution

  • Initialization of the constant array in the package through a function that reads a file, can be done as shown below:

    package pkg is
      type output_t is array(0 to 9) of integer;  -- Just change size
      constant output : output_t;  -- Value assign is deferred
    end package;
    
    library std;
    use std.textio.all;
    package body pkg is
    
      -- Entries in output without value in file are assigned to 0
      impure function output_init return output_t is
        file vec_file: text open read_mode is "mytext";
        variable iline: line;
        variable data_read: integer;
        variable x: integer := 0;
        variable res_t : output_t := (others => 0);
      begin
        while not endfile (vec_file) loop
          readline (vec_file, iline);
          read(iline,data_read);
          res_t(x) := data_read;
          x := x + 1;
        end loop;
        return res_t;
      end function;
    
      constant output : output_t := output_init;
    
    end package body;