Hi Tricky,
Thanks for your response. With a bit of playing I managed to get your code working and I have used it in my simulation. I had to declare the function as impure and I put in a check for the end of file for a bit of safety. This is what i ended up with:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.numeric_std.all;
LIBRARY STD;
USE STD.textio.all;
LIBRARY work;
ENTITY test_program_file_io IS
GENERIC
(
filename : string := "prog_file.txt";
data_width : integer := 8;
addr_width : integer := 15
);
PORT
(
address : IN std_logic_vector(addr_width-1 DOWNTO 0);
rd : IN std_logic;
rddata : OUT std_logic_vector(data_width-1 DOWNTO 0)
);
END ENTITY;
ARCHITECTURE sim OF test_program_file_io IS
CONSTANT N : integer := (2**addr_width);
TYPE rom_t IS ARRAY (0 TO N-1) OF std_logic_vector(7 DOWNTO 0);
IMPURE FUNCTION init_rom RETURN rom_t IS
FILE rom_file : text OPEN read_mode IS filename;
VARIABLE ret : rom_t;
VARIABLE l : line;
BEGIN
FOR i IN 0 TO N-1 LOOP
IF(NOT ENDFILE(rom_file)) THEN
readline(rom_file, l);
hread(l, ret(i));
END IF;
END LOOP;
RETURN ret;
END FUNCTION init_rom;
CONSTANT ROM : rom_t := init_rom;
BEGIN
PROCESS(rd, address)
BEGIN
IF(rd='1')THEN
rddata <= ROM(to_integer(unsigned(address)));
END IF;
END PROCESS;
END ARCHITECTURE;
--- Quote Start ---
technically correct, but you included the non-standard library std_logic_textio which is a 3rd party library that gave you the ability to read/write directly to std_logic/std_logic_vector with VHDL 1993. The functionality of this package is included in std_logic_1164 in VHDL 2008.
--- Quote End ---
Yes you are right, I was working with vhdl-1993 but updated my simulation to use vhdl-2008 so I could use the hread and cut a corner. I missed removing that library.
--- Quote Start ---
Is there any reason you're making an asynchronous rom? and not a synchronous one?
--- Quote End ---
Not really, the application might not be the best. I was trying to improve my simulation skills and using it like a ROM I could connect it in my work simulation. My aim was to be able to read in a selected line from a input text file so I can dump a hex program to a text file and load it into my simulation sequentially. I also wanted to be able to start from somewhere other than the start of the file.
After playing I think that you are right and initializing an array is the simplest path.
Thanks
James