Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartus initializing RAM

Tags:

memory

ram

vhdl

I made an entity in which quartus successfully recognizes RAM, and instantiates a RAM megafunction for it. It would be nice if I could initialize that RAM from a file. I found tutorials for making such file (.mif file). Now that I have created that file, i don't know how to make quartus initialize that module. Any help is appreciated.

Here is my RAM entity:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity RAM is
    port (
        clk: in std_logic;
        we: in std_logic;
        data_in: in std_logic_vector (7 downto 0);
        read_addr: in integer range 0 to 65535;
        write_addr: in integer range 0 to 65535;
        data_out: out std_logic_vector (7 downto 0)
    );
end entity RAM;

architecture RAM_arch of RAM is
type memory is array (65535 downto 0) of std_logic_vector (7 downto 0);
signal content: memory;     
begin
    process(clk)
    begin
        if (RISING_EDGE(clk)) then
            if (we = '1') then
                content(write_addr) <= data_in;
            end if;
            data_out <= content(read_addr);
        end if;
    end process;
end architecture;
like image 632
Dejan Avatar asked Jun 25 '26 12:06

Dejan


1 Answers

Possibly the best way to initialise the memory is to ... put an initialisation clause on the memory variable. There may be Quartus-specific ways to load .MIF files, but this is probably simpler, definitely more portable (to Xilinx for example), and more flexible because you get to define the file format, you don't have to generate .mif files.

Given the following code:

type memory is array (65535 downto 0) of std_logic_vector (7 downto 0);
signal content: memory; 

you could simply write

type memory is array (65535 downto 0) of std_logic_vector (7 downto 0);
signal content: memory := init_my_RAM(filename => "ram_contents.txt"); 

Now it is possible but unlikely that Quartus doesn't support initialisation this way, so we can test it by writing a simple init_my_ram function ignoring the actual file contents:

function init_my_ram (filename : string) return memory is
variable f : file;
variable m : memory;
begin
   file_open(f, filename, read_mode);
   for i in memory'range loop
      m(i) := X"55";
   end loop;
   file_close(f);
   return m;
end init_my_ram;

Because the function call is an initialiser, and called at elaboration time when the design is synthesised, this is all synthesisable.

If this compiles and Quartus generates a memory full of X"55", you are good to go, to parse whatever file format you want, in the init_my_ram function. (Binary files are harder, and the reader code may not be so portable between tools, but not impossible).

The .MIF approach has one potential advantage though : you can update just the memory contents without requiring another synthesis/place and route cycle.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!