entity Memory is
generic(
memDepth:integer:=16;
memWidth:integer:=16;
fileName: string:="~\test.txt"
);
port(
CLK:in STD_LOGIC;
readEn:in STD_LOGIC;
memAddress:in integer range 0 to memWidth-1;
dataOut:out STD_LOGIC_VECTOR(memWidth-1 downto 0)
);
end Memory;
architecture Behavioral of Memory is
type mem is array(memDepth-1 downto 0) of STD_LOGIC_VECTOR(memWidth-1 downto 0);
function loadRom(fileName: string) return mem is
file toBeRead: TEXT open read_mode is fileName;
variable inLine: line;
variable temp: STD_LOGIC_VECTOR(memWidth-1 downto 0):= (others=>'0');
variable index: integer:=0;
variable endOfFile: STD_LOGIC;
variable ROM: mem;
begin
for i in mem'range loop
if not(endfile(toBeRead)) then
readline(toBeRead, inLine);
read(inLine, temp);
ROM(index):=temp;
index:=index+1;
else
endOfFile:='1';
end if;
end loop;
return ROM;
end loadRom;
constant ROM: mem := loadRom(fileName);
begin
process(CLK)
begin
if (CLK'event and CLK='1')then
if(readEn='1') then
dataOut<=ROM(memAddress);
end if;
end if;
end process;
end Behavioral;