MIF_Gen – A Matlab Utility

matlab_mif_gen

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.

  1.  %——————————————-
  2.  % Generate Parameters
  3.  ram_size    = 256;     % In words
  4.  word_size   = 16;      % Can be 8, 16, 32, etc. 
  5.  %——————————————-
  6.  % generate vector with random values within 
  7.  % a defined range
  8.  min_v = 100;
  9.  max_v = 20000;
  10.  % Check values and calculate range
  11.  if (min_v < 0)
  12.      min_v = 0;
  13.  end
  14.  max_val = 2^word_size-1;
  15.  if (max_v > max_val)
  16.      max_v = max_val;
  17.  end
  18.  range_v = max_v – min_v;
  19.  values = (randi(range_v – 1, ram_size, 1)) + min_v;
  20.  %——————————————-
  21.  % Open mif file for write
  22.  fileID = fopen(‘init_mem.mif’, ‘w’);
  23.  fprintf( fileID, ‘%s\n’, ‘– Generated by mif_gen Matlab script’);
  24.  fprintf( fileID, ‘%s\n\n’, ‘– FPGA’er – https://fpgaer.wordpress.com’);
  25.  fprintf( fileID, ‘%s%d%s\n’, ‘WIDTH=’, word_size, ‘;’);
  26.  fprintf( fileID, ‘%s%d%s\n\n’, ‘DEPTH=’, ram_size, ‘;’);
  27.  fprintf( fileID, ‘%s\n’, ‘ADDRESS_RADIX=HEX;’);
  28.  fprintf( fileID, ‘%s\n\n’, ‘DATA_RADIX=HEX;’);
  29.  fprintf( fileID, ‘%s\n’, ‘CONTENT BEGIN’);
  30.  % data format
  31.  fdata = int2str(word_size/4);
  32.  faddr = int2str(log2(ram_size)/4);
  33.  format_str = strcat(‘    %0’, faddr, ‘X  :  %0’, fdata, ‘X;\n’);
  34.  % write values to file
  35.  idx = 0;
  36.  for ii=1:ram_size
  37.      fprintf( fileID, format_str, idx, values(idx+1));
  38.      idx = idx+1;
  39.  end
  40.  fprintf( fileID, ‘%s\n’, ‘END;’);
  41.  fclose( fileID);
  42.  %——————————————-
  43.  % Convert to HEX
  44.  cmd = ‘D:\altera_lite\15.1\quartus\bin64\mif2hex keys.mif init_mem.hex’;
  45.  system(cmd);

Lines 26 to 33 generate the Intel-Altera .mif file header. Lines 36 to 38 define the format for writing the data rows with the fprintf command, based on the memory size and width.

Lines 41 to 47 actually put the values on each line of the file.

Lines 51 and 52 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 installation.

You can download this utility at Github


fpgaer1_1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s