Thursday, March 10, 2011

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


Website is being updated with new UI!

Hi E veryone , Pardon me. It took me very long to get back on managing this website. You all during some part of a time in your life, you ...