Skip to main content

Initializing M4K blocks for RAM. (For Altera FPGA users)

Hello Everybody.

It seems you are having problem about how to initialize on-board memory using Megafunction Library and M4K blocks. (I am using Quartus II 10.1 SP1, but procees will be similar all other versions)

So here are the steps of the solution:

Start Quartus II and open new project.

 Open the MegaFunction Wizard

Select the 'Create New megaFunction' Option
Select the Memory Compiler from list

Select RAM 1 Port and give name to output file

After clicking Next, select the no. of bits per block (In this case its 4 bits)

Select no. of words or blocks of 4-bits you want (In this case 32 blocks, I use 'blocks' though 'words' is proper term)

Select the memory block type as M4K (It doesnt matter if you keep that Auto)

Click Next. Dont change anything on this page

Now select the second option if you want to enter data in memory before programming

To create hex file keep the MegaFunction window as it is and click on the File>New>Intel-hex File in Quartus II window

Then make the proper changes in words and wordsize field

Add whatever data you want in the respective blocks (It will only take a 4 bit no. as we have set the word size as 4)

Save the file be any relevant name you want and then open the MegaFunction window and select the file

Click Next till you get the this window and select the Instantiation file option

Click finish and then open the Instantiation and Component file from File>Open>(Your destination of project)>*_inst.vhd and *.cmp

Then add the Code as follows and make necessary changes to the Port Map field as per your project specifications

If you want to, then you can follow the process rather can use the process with my SRAM interface example project. It will make you more perfect. You can check necessary changes required for my project in .vhd file I attached. Mail me your querries or suggestions on

Enjoy programming...!!


  1. Thanks a lot! I was having such troubles with initializing RAM/ROM. And figured out where I was making the mistake.

  2. Thank you so much!
    very useful
    Good Luck.
    Q.M from Afghanistan


Post a Comment

Popular posts from this blog

RTC on FPGA with manual set synthesizeble VHDL code.

Hey there, cheers to VHDL. I successfully added manual set feature in my previous RTC project. I am not going to copy whole code here. I am just adding the links of .vhd files. I also added the pre-compiled .sof file only for DE1 boards. There is a very small bug that while clicking the set button to enable min and hr set, it increments the hr or min value unintentionally. Else everything is working great.

(Input freq 24MHz., top-level entity DigiClock)

VHDL code for Clock Divider

This is a clock divider code, just set the max-count value as per your requirenment.

For ex. If I want 1Hz freq. set the max count to i/p freq value viz.
1sec = 1Hz
Then, to get time period of 1sec i.e. 1 Hz frequency set max-count to 240000 as shown below:

1sec  =  24000000  -- for i/p frequency of 24 MHz.

To get your desired frequency just calculate the maxcount with the formula given below:

max_count = 24000000 * (1/your required frequency)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48libraryIEEE; useIEEE.STD_LOGIC_1164.ALL; useIEEE.numeric_std.all; entityclk_divisPort ( Clk :instd_logic; rst :instd_logic; op :outstd_logic ); endclk_div; architecturebehavioralofclk_divisconstant max_count :natural:=24000000; -- I used 24MHz clockbeginprocess(Clk,rst) variable count :naturalrange0to max_count; beginif rst ='0'…