Many times I find myself in the need of generating data for testing. We need data for verification, either done on simulation or on the real target.

One easy way to test our system is to generate data vectors on RAM. Altera RAM IP includes the ability to initialize RAM contents during power-up by means of a .hex file.

One problem of the .hex file format is that it is quite unreadable for humans. Altera came to our rescue with the .mif format, which is text based and very easy to understand.

The application I present below initializes a memory (generating an .hex file). The size and width of the memory are parameters. The Matlab application generates both a init_mem.mif and a init_mem.hex file.

The RAM parameters are configured on lines 17 and 18.

Then the user can choose a range for values to be generated. Line 37 generates random values within the range chosen.

Lines 43 to 50 generate Altera .mif header. Lines 53 to 55 define the format for writing the data rows with the fprintf command, based on the memory size and width.

Lines 58 to 62 actually put the values on each line of the file.

Lines 72 and 73 generate the .hex file via a system call using Altera’s mif2hex utility. You will need to change the path on line 72 to the actual location of your Quartus mif2hex.exe file. It has to be under bin or bin64 on your Quartus installation directory.

As usual, the full source is available here.