Skip navigation.
Home

SystemVerilog Dynamic Array File IO

It is possible to combine file IO with dynamic arrays to load data from a file into a dynamically sized array. This snippet demonstrates how to open a file and read the contents into a dynamically sized array.

module systemverilog_dynamic_file_io;
int file,value,cnt=0,value_array[];            // dynamic array and variables.
initial begin
  file = $fopen("hexdata.dat","r");            // open file.
  while ($fscanf(file,"%x",value) != -1) begin // attempt to read a value from file.
    value_array = new[++cnt](value_array);
    value_array[cnt-1] = value;
  end
  foreach(value_array[ii])                     // display the values.
    $display("value_array[%d]=%x",ii,value_array[ii]);
  $stop;
end
endmodule

file ptr

I would check that the file pointer is not NULL before the while loop, else the thing just loops forever.... (atleast on VCS)