Memory Initialization in VHDL
Memory Initialization in VHDL
F P G A G AT E
FOLLOW ME
In this post we will illustrate different methods of memory initialization in Subscribe Now
Initialization from text file :
A text file can be used to initialize the memory in VHDL, for this purpose
library textio is used. A function is written before declaring the memory
signal, this function reads the text file line by line, converts each value in
the line to std_logic_vector type and use it to initialize the memory. the
following code shows an example of initializing a memory with from text
file
- Text file
011000000001
000100000000
111100000000
011100000001
001000000000
011100000000
110000000110
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
- VHDL code
--------------------------------------------------------------
-- fpgagate.com: FPGA Projects, VHDL Tutorials, VHDL projects
-- Memory initialization in VHDL
--------------------------------------------------------------
library IEEE;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use ieee.std_logic_textio.all;
use std.textio.all;
entity mem_array is
generic( DATA_WIDTH: integer := 12;
ADDR_WIDTH: integer := 8;
INIT_FILE: string := "inst_mem.txt");
------------------------------------
-- function to initialize memory content
function init_memory_wfile(mif_file_name : in string) return MEMORY_ARRA
file mif_file : text open read_mode is mif_file_name;
variable mif_line : line;
variable temp_bv : bit_vector(DATA_WIDTH-1 downto 0);
variable temp_mem : MEMORY_ARRAY;
begin
for i in MEMORY_ARRAY'range loop
readline(mif_file, mif_line);
read(mif_line, temp_bv);
temp_mem(i) := to_stdlogicvector(temp_bv);
end loop;
return temp_mem;
end function;
-------------------------------------
signal Memory: MEMORY_ARRAY := init_memory_wfile(INIT_FILE);
begin
process(clk) is
begin
if rising_edge(clk) then
if we='1' then
Memory(to_integer(unsigned(ADDR))) <= DATAIN;
end if;
OUTPUT <= Memory(to_integer(unsigned(ADDR)));
end if;
end process;
end behavioral;
Initialization using a mathematical function :
In this initialization method the memory arry is initialized using a
mathematical function, such as sine, cosine, linear .... etc.
a function should first be written before declaring the memory signal, this
function shall describe the required mathematical formula that will be used
to fill the memory locations. The following code shows an example of
using a linear function to initialize the memory.
--------------------------------------------------------------
-- fpgagate.com: FPGA Projects, VHDL Tutorials, VHDL projects
-- Memory Initialization in VHDL
--------------------------------------------------------------
library IEEE;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity mem_array2 is
generic( DATA_WIDTH: integer := 12;
ADDR_WIDTH: integer := 8);
port(ADDR: in std_logic_vector(ADDR_WIDTH-1 downto 0);
DATAIN : in std_logic_vector(DATA_WIDTH-1 downto 0);
CLK : in std_logic;
WE : in std_logic;
OUTPUT : out std_logic_vector(DATA_WIDTH-1 downto 0));
end mem_array2;
end behavioral;
0 comments
Leave a message...
E m b e d d e d s y s t e m s L a b
Subscribe Now