查看: 937|回复: 0
打印 上一主题 下一主题

MIF文件生成器(发现坛里没有就转过来)

[复制链接]
跳转到指定楼层
沙发
发表于 2016-4-20 22:09:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
MIF File Generator Utility
What is a .MIF File?
A .MIF (Memory Initialization File) file is used to preload the EAB's in Altera's FLEX devices, like the Altera FLEX 10K family. When you instantiate a ROM or RAM in a schematic or an AHDL file, you have the option to specify a .MIF file for preloading the EAB with some default data.

Look-Up Table
You may use the EAB's as look-up tables. For instance, you could use the EAB's for storing digital waveforms. A typical digital waveform is the digital representation of a sine or cosine waveform.

MIFGEN.EXE, The .MIF File Generator Utility
The MIFGEN utility will generate .MIF files for you with sine or cosine digital waveforms. You have the option to specify several parameters, like amplitude, offset, angle range and so on. The MIFGEN utility is a stand-alone 32-bit executable running under Windows NT or Windows 95.



.MIF sample file
Here is a sample output of the MIFGEN utility, using the above parameters:

-- Memory Initialization File
-- Generated by .MIF File Generator Utility v1.2
-- by Rune Baeverrud

-- Angle Range 0-360 degrees (quadrant 1-2-3-4)
-- Function type    : Cosine
-- Options          : Normal
-- Peak Amplitude   : 127,00
-- Offset           : 0
-- Number of Samples: 256
-- SNR:             : SNR: 49,87dB over i = 0 to 255

DEPTH = 256;
WIDTH = 8;

ADDRESS_RADIX = DEC;
DATA_RADIX = DEC;

CONTENT
  BEGIN
    0 : 127;
    1 : 127;
    2 : 127;
    3 : 127;
    4 : 126;
    5 : 126;
    ...
    ... (some lines removed here)
    ...  
    250 : 126;
    251 : 126;
    252 : 126;
    253 : 127;
    254 : 127;
    255 : 127;
  END;


Parameter Description and Explanation
Parameter Name Description/Explanation
Peak Amplitude A sine or cosine (with no weigth) has a value in the range [-1,1]. The sine or cosine generated will be multiplied by the Peak Amplitude to generate an intermediate floating point representation of the waveform in the range [-Peak Amplitude, Peak Amplitude]. This value can be viewed in the 3rd column of the table ("cos(i)" or "sin(i)"). The floating point value is then rounded or quantized to the nearest whole integer - this is in the 4th column of the table ("Rounded").
Offset The Offset value is then added to Rounded, and the result can be viewed in the 5th column of the table ("w/Offset").
Number of Samples The Number of Samples parameter gives the number of entries in the .MIF file. In the on-screen table, the number of samples is actually one more, so that a complete quadrant, half or circle can be more easily verified.
Number of Bits The Number of Bits parameter is used for two purposes. First, the WIDTH parameter has to be specified in the .MIF file. Second, if the "w/Offset" column contains negative values, the waveform is assumed to be bipolar and it will be represented in 2's complement. Since the .MIF file itself does not accept negative values, the number -3 is converted to 2^(Number of Bits) - 3. Assuming Number of Bits is 8, the new value will be 2^8 - 3 = 256 - 3 = 253. This is the value appearing in the last column, which is also the value written to the .MIF file.
Angle Range The Angle Range parameter specifies the angle range of which the Number of Samples will be applied. The angle range is evenly divided by Number of Samples from 0 degrees up to 90, 180 or 360 degrees.
Function Function specifies if you want to generate a sine or cosine within the angle range.
Options Options lets you specify one of the following options, which apply to the angle range:
Normal. The angle range starts at 0 degrees and ends at, but does not include, the end angle (90, 180 or 360 degrees). For example, the cosine is computed as cos ((Angle Range) * i / Number Of Samples).
Include End Angle. The angle range starts at 0 degrees and stops at, and also includes, the end angle (90, 180 or 360 degrees). For example, the cosine is computed as cos ((Angle Range) * i / (Number Of Samples-1) ). A drawback is that there are 255 samples only per quadrant instead of 256, making it less suitable for a phase modulator. (256 divides nicely into 128, 64, 32, 16 etc. for accurate phase offsets).
Symmetry. The angle range starts at 0 degrees and stops at 90, 180 or 360 degrees, but does not contain the start or the end angle. This method generates a symmetric look-up table. For example, the cosine is computed as cos ((Angle Range) * (i+0.5) / Number Of Samples). A drawback is that the angle has a small offset error - the advantage is that it is very easy to implement a sine/cosine oscillator.


When you have entered your parameters, press the Generate Data button and the on-screen table will be filled with the appropriate values for your inspection. There is also an SNR (Signal-to-Noise Ratio) readout, based on the Signal Power / Quantization Noise Power. If you are satisfied with the result, press the Generate .MIF File button to create the .MIF file itself. You will be asked to supply a name for the new file.

MIF File Generator Utility.zipourdev_624512P9DS3K.zip(文件大小:152K) (原文件名:MIF File Generator Utility.zip)
做 FPGA设计时经常要调用芯片内存。 特别对于 ROM, 内存的初始化就显得比较重要。
当然你完全可以手工在 QUARTUS II 打开 mif文件的表格里或是在 EXCEL 中逐个输入,几
十项(字)或是近百项(字)你还可以接受,如果上千项或是更多呢?估计能累的人吐血!  
一般内存的初始化数据都是有规律的,符合一定的函数,我们完全可以用 MATLAB 来
完成(如果没规律只能逐项手工输入了)。
首先,我们应该知道*.mif 文件的格式。它是文本格式。随便打开一个 mif 文件,你会
发现它具有如下的格式:

-- Copyright (C) 1991-2007 Altera Corporation
-- Your use of Altera Corporation's design tools, logic functions  
-- and other software and tools, and its AMPP partner logic  
-- functions, and any output files from any of the foregoing  
-- (including device programming or simulation files), and any  
-- associated documentation or information are expressly subject  
-- to the terms and conditions of the Altera Program License  
-- Subscription Agreement, Altera MegaCore Function License  
-- Agreement, or other applicable license agreement, including,  
-- without limitation, that your use is for the sole purpose of  
-- programming logic devices manufactured by Altera and sold by  
-- Altera or its authorized distributors.  Please refer to the  
-- applicable agreement for further details.

-- Quartus II generated Memory Initialization File (.mif)

WIDTH=8;
DEPTH=256;

ADDRESS_RADIX=UNS;
DATA_RADIX=UNS;

CONTENT BEGIN
0:127;
    1:126;
2:126;
……
    ……
253:126;
254:126;
255:127;
END;

格式很简单吧。首先根据需要设置每个字的位宽 WIDTH 和总字数 DEPTH。然后设置地址
和数据的进制基数 ADDRESS_RADIX、DATA_RADIX,建议大家不要修改,就使用无符号
数(UNS) 。然后用 MATLAB 生成需要的数据(按上边的格式,注意中间“: ” ,最后“;” ) ,
往 CONTENT BEGIN和 END中间一贴就行了。

下边举例说明 MATLAB程序的写法,希望对大家有用。

%the walue of cosine
function data=makedata
index = linspace(0,2*pi,2^8);                       
cos_value = cos(index);                                   
cos_value = cos_value * (2^7 -1 );            
cos_value = fix(cos_value);
cos_value =abs(cos_value);
for i=65:192
    cos_value(i)=cos_value(i)+128;
end
%///////////////////////////////////////上边的用来生成数据, 下边的用于控制格式输出 (对大家有用的)  
number=[0:255];
for i=1:256
    comer(i)=':';
end
for i=1:256
    semi(i)=';';
end
data=[number; comer; cos_value; semi];
fid=fopen('d:\data.txt','w');
fprintf(fid, '%d%c%d%c\n', data);
fclose(fid);

在 D盘下找到 data.txt 文件,用写字板打开 mif文件,将 data.txt 中的内容贴到
CONTENT BEGIN和 END中间,然后保存就可以了。

对于 hex 文件,更简单。大家生成mif文件后,用 QUARTUS II 打开,然后另存为 hex
文件就可以了。

用MATLAB 生成.mif、.hex(QUARTUS II)文件简介.pdfourdev_624514CO3BS5.pdf(文件大小:64K) (原文件名:用MATLAB 生成.mif、.hex(QUARTUS II)文件简介.pdf)
用 MATLAB 生成*.mif、*.hex(QUARTUS II)文件简介(v1.1)
以前写过一个“用 MATLAB 生成*.mif、*.hex(QUARTUS II)文件简介” ,由于时间和
水平原因很粗糙,并且有一些错误。现在修改一下,升级到 v1.1。以前的称为 v1.0 吧。

做 FPGA设计时经常要调用芯片内存。 特别对于 ROM, 内存的初始化就显得比较重要。
当然你完全可以手工在 QUARTUS II 打开 mif文件的表格里逐个输入, 几十项或许你还可以
接受,但上千项估计能累的人吐血!
一般内存的初始化数据都是有规律的,符合一定的函数,我们完全可以用 MATLAB 来
完成(如果没规律只能逐项手工输入了)。

1.  使用 MATLAB 直接生成*.mif文件
首先,我们应该知道*.mif 文件的格式。它是文本格式。随便打开一个 mif 文件,你会
发现它具有如下的格式:

-- Copyright (C) 1991-2008 Altera Corporation
-- Your use of Altera Corporation's design tools, logic functions  
-- and other software and tools, and its AMPP partner logic  
-- functions, and any output files from any of the foregoing  
-- (including device programming or simulation files), and any  
-- associated documentation or information are expressly subject  
-- to the terms and conditions of the Altera Program License  
-- Subscription Agreement, Altera MegaCore Function License  
-- Agreement, or other applicable license agreement, including,  
-- without limitation, that your use is for the sole purpose of  
-- programming logic devices manufactured by Altera and sold by  
-- Altera or its authorized distributors.  Please refer to the  
-- applicable agreement for further details.

-- Quartus II generated Memory Initialization File (.mif)

WIDTH=8;
DEPTH=256;

ADDRESS_RADIX=UNS;
DATA_RADIX=DEC;

CONTENT BEGIN
[0..3]  :   127;
[4..6]  :   126;
[7..8]  :   125;
9    :   124;
10   :   123;
……
……
[253..255]  :   127;
END;

下边笔者来说明用 MATLAB 产生所需*.mif文件方法。
由于笔者的语文水平仅脱盲,如果空洞讲解很表述清楚并且会很枯燥。所以笔者仍使用一个
例子来说明。对[0,2*pi) (包括 0,不包括 2*pi)256 点采样,每点用 8 位有符号数表示。
生成*.mif文件的 MATLAB 代码如下:
clear all;
close all;
clc;
index=linspace(0,2*pi,2^8+1);  %由于linspace函数包括前后两个边界点,所以多加一点除去
%cos(2*pi)的值
cos_val=fix((2^7-1)*cos(index)+0.5);  %求COS值、倍扩成8位有符号数、fix是去尾求整数,
%加0.5变成四舍五入求整数
fid=fopen('E:\…\cosine.mif','w');  %将cosine.mif文件创建到您老的QUARTUS工程目录里。 当
%然你也可创建到指定的目录,然后拷贝到QUARTUS工程目录。
fprintf(fid,'WIDTH=8;\n');   %指定每个数值的字宽(本工程用8位)
fprintf(fid,'DEPTH=256;\n');   %指定数值的个数,即ROM的深度(本工程用256个)
fprintf(fid,'ADDRESS_RADIX=UNS;\n');   %指定地址的数制(UNS:无符号数。推荐UNS)  
fprintf(fid,'DATA_RADIX=DEC;\n'); %指定数据基数 (DEC: 十进制 (有符号数) , 推荐DEC)  
fprintf(fid,'CONTENT BEGIN\n');   %固定格式
for j=1:256            %数据段数据
    i=j-1;
    fprintf(fid,'%3d',i);
    fprintf(fid,' : ');
    fprintf(fid,'%3d',cos_val(j));
    fprintf(fid,';\n');
end
fprintf(fid,'END;\n');   %固定格式
fclose(fid);

运行此 MATLAB 程序,得到 cosine.mif 即可使用。

用MATLAB生成.mif、.hex(QUARTUS II)文件简介(v1.1).pdfourdev_624515VKREOH.pdf(文件大小:135K) (原文件名:用MATLAB生成.mif、.hex(QUARTUS II)文件简介(v1.1).pdf)
【基于MATLAB生成Intel HEX文件】http://www.matlabsky.com/thread-675-1-1.html

MATLAB生成*.mif、*.hex2008-08-31 03:06做FPGA设计时经常要调用芯片内存。特别对于ROM,内存的初始化就显得比较重要。当然你完全可以手工在QUARTUS II打开mif文件的表格里或是在EXCEL中逐个输入,几十项(字)或是近百项(字)你还可以接受,如果上千项或是更多呢?估计能累的人吐血!

一般内存的初始化数据都是有规律的,符合一定的函数,我们完全可以用MATLAB来完成(如果没规律只能逐项手工输入了)。

首先,我们应该知道*.mif文件的格式。它是文本格式。随便打开一个mif文件,你会发现它具有如下的格式:


-- Copyright (C) 1991-2007 Altera Corporation
-- Your use of Altera Corporation's design tools, logic functions
-- and other software and tools, and its AMPP partner logic
-- functions, and any output files from any of the foregoing
-- (including device programming or simulation files), and any
-- associated documentation or information are expressly subject
-- to the terms and conditions of the Altera Program ******
-- Subscription Agreement, Altera MegaCore Function ******
-- Agreement, or other applicable ****** agreement, including,
-- without limitation, that your use is for the sole purpose of
-- programming logic devices manufactured by Altera and sold by
-- Altera or its authorized distributors. Please refer to the
-- applicable agreement for further details.

-- Quartus II generated Memory Initialization File (.mif)

WIDTH=8;
DEPTH=256;

ADDRESS_RADIX=UNS;
DATA_RADIX=UNS;

CONTENT BEGIN
       0:127;
       1:126;
       2:126;
       ……
       ……
       253:126;
       254:126;
       255:127;
END;

格式很简单吧。首先根据需要设置每个字的位宽WIDTH和总字数DEPTH。然后设置地址和数据的进制基数ADDRESS_RADIX、DATA_RADIX,建议大家不要修改,就使用无符号数(UNS)。然后用MATLAB生成需要的数据(按上边的格式,注意中间“:”,最后“;”),往CONTENT BEGIN和END中间一贴就行了。

下边举例说明MATLAB程序的写法,希望对大家有用

function data=makedata
%使用MATLAB生成MIF & HEX2文件
%see also http://www.matlabsky.com
%2009.1.10
%
index = linspace(0,2*pi,2^8);     
cos_value = cos(index);   
cos_value = cos_value * (2^7 -1 );   
cos_value = fix(cos_value);
cos_value =abs(cos_value);
for i=65:192
    cos_value(i)=cos_value(i)+128;
end

%上边的用来生成数据,下边的用于控制格式输出(对大家有用的)

number=[0:255];
for i=1:256
    comer(i)=':';
end
for i=1:256
    semi(i)=';';
end
data=[number; comer; cos_value; semi];
fid=fopen('d:\mif_data_matlabsky.txt','w');
fprintf(fid, '%d%c%d%c\n', data);
fclose(fid);

在d:\下找到mif_data_matlabsky.txt'文件,用写字板打开mif文件,将data.txt中的内容贴到CONTENT BEGIN和END中间,然后保存就可以了。或者将CONTENT BEGIN和END两边的内容分别贴于data.txt文件的前后,然后保存为.mif文件。

对于hex文件,更简单。大家生成mif文件后,用QUARTUS II打开,然后另存为hex
在Quartus II中快速更新FPGA片上存储器初始值的一种方法
http://www.61eda.com/Services/soft/softs/200801/310.html

最近在调试中遇到了这样一个问题:一组参数预先存储在FPGA的片上存储器内;在系统运行过程中部分参数会被读出,修改后写回;为了调试,需要经常变换参数的初始化值。
    实现存储器中数值的初始化比较容易,在生成RAM模块时指定一个初始化文件(.hex或.mif)即可。

    实现部分参数的动态修改也容易,设计一个状态机在需要的时候写入该RAM即可。

    为了调试,更新初始化数值就难了些,可供选择的方法有这样几种:

1. 采用In-system Memory Content Editor,通过Import File的方式把修改后的初始化文件更新到RAM中。

2. 采用Virtual JTAG定制一个RAM写入接口,连接到双端口RAM的一个空闲端口用于调试,双端口RAM的另一个端口用作正常使用。

3. 修改初始化文件,把初始化文件更新到编程文件中,通过重新下载更新到RAM中。

    第一种方法原理上没有任何问题,在MegaWizard Plug-in Manager中选择Allow In-system Memory Content Editor...即可。但是,在实际应用中,只有单端口ROM可以采用该方法;单端口RAM采用该方法可以生成代码,但是不能正确编译,总是报出一些奇怪的错误;此外。双端口的ROM和RAM都不支持In-system Memory Content Editor。

    第二种方法在本质上与第一种方法相同,在实际应用中也能正确编译,唯一的缺点是需要对Virtual JTAG有较深入的认识,需要手工编写一些代码,还要编写TCL脚本。

    第三种方法可以不修改代码,也不需要执行全编译,只要在Quartus II集成开发环境中执行两个简单的操作即可。下面就是具体的步骤:

1. Processing -〉Update Memory Initialization File

2. Processing -〉Start -〉Start Assembler

    上述操作执行完毕,把Assembler生成的编程文件下载到FPGA中,修改后的初始化文件就更新到片上存储器中了。

    该方法的原理是,在Update Memory Initialization File过程中,只更新db文件夹中与RAM初始化内容相关的文件,其余文件不更新,所以不需要执行全编译;更新后执行Assembler,把新的初始化内容更新到编程文件中去。该方法类似于软件编译过程中的部分编译和重新链接。

    该方法只适用于静态更新RAM内容,如果需要对RAM进行动态调试,还是用第二种方法才行。




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表