fpga中的MIF文件
在FPGA设计中ROM的应用时比较常见的,在调用ROM时经常要加载mif文件,对于初学者,无论mif还是hex都是很令人疑惑的东西,这里就对mif文件的格式及其创建做一点简单的说明。mif在fpga设计中试memory initialization file 的缩写,中文意思就是存储器初始化文件。直接说就是给rom赋值的文件。先看一个简单的mif文件的内容(可以用记事本将mif文件打开,看到里面的代码):
DEPTH=256; %存储器的纵向容量,就是存多少个数据,本例中是256个
WIDTH=8 ; %存储器的横向宽度,就是每个数据多少位,8位宽
ADDRESS_RADIX=DEC ; %设置地址基值(实际就是地址用什么进制的数表示) 可以设为BIN(二进制),OCT(八进制),DEC(十进制),HEX(十六进制)
DATA_RADIX=DEC ;%设置数据基值同上
%数据区里的地址和数据值要和这里设置的值一致,即这里如果设置了
%DEC那么,数据区的地址和数据都要用十进制来表示。
CONTENT %开始数据区
BEGIN
0:0; %前面是地址,后面是数据,都是用十进制表示(上面的DEC)
1:1;
……%如果表示成这样:10;意思就是从0到255都是数据10.
255:255;
END; %结束
至于mif文件的创建,最简单的方法就是用记事本了。也可以在Quartus II 里新建,然后保存。
mif文件的使用,在bdf模块调用中找出lpm_ROM在有一项设置中要添加文件那里加入就可以用了。
---------------------------- 强烈关注。
为什么不贴个C程序出来。 function miffile(filename,var,width,depth)
% function miffile(filename,var,width,depth)
% It creates a 'mif' file called filename,which be written with var.
% The 'mif' file is a kind of file formats which is uesed in Altera's
% EDA tool,like maxplus II ,quartus II,to initialize the memory
% models,just like cam,rom,ram.
% Using this function,you can easily produce the 'mif' file written
% with all kinds of your data.
% If the size of 'var' is shorter than 'depth',0 will be written for the
% lefts.If the size of 'var' is greater than 'depth',than only 'depth' former
% data of 'var' will be written;
% the radix of address and data is hex
% filename --the name of the file to be created,eg,"a.mif",string;
% var ----the data to be writed to the file, can be 3D or less ,int or other fittable;
% width --the word size of the data,width>=1,int;
% depth --the number of the data to be writed,int;
%
% because matlab read the matrix is colum first,if you want to write
% the 'var' data in row first mode, just set var to var';
%
% example:
% a=uint8(rand(16,16)*256);
% miffile('randnum.mif',a,8,256);
if(nargin~=4) %% be tired to do more inupts check!
error('Need 4 parameters! Use help miffile for help!');
end,
fh=fopen(filename,'w+');
fprintf(fh,'--Created by xxxx.\r\n');
fprintf(fh,'--xxxxx@126.com.\r\n');
fprintf(fh,'--%s.\r\n',datestr(now));
fprintf(fh,'WIDTH=%d;\r\n',width);
fprintf(fh,'DEPTH=%d;\r\n',depth);
fprintf(fh,'ADDRESS_RADIX=HEX;\r\n');
fprintf(fh,'DATA_RADIX=HEX;\r\n');
fprintf(fh,'CONTENT BEGIN\r\n');
%%%%%%
%%%%%%
var=rem(var,2^width);%% clip to fit the width;
=size(var);%% can only fit 3D or less;
value=var(1,1,1);
sametotal=1;
idepth=0;
addrlen=1;
temp=16;
while(temp<depth) %%decide the length of addr
temp=temp*16;
addrlen=addrlen+1;
end,
datalen=1;
while(temp<width) %%decide the length of data
temp=temp*16;
datalen=datalen+1;
end,
for k=1:sz,
for j=1:sy,
for i=1:sx,
if(~((i==1 ) &&( j==1) &&( k==1)))
if(idepth<depth),
idepth=idepth+1;
if(value==var(i,j,k))
sametotal=sametotal+1;
continue;
else
if(sametotal==1)
fprintf(fh,['\t%' num2str(addrlen) 'X:%' num2str(datalen) 'X;\r\n'],idepth-1,value);
else
fprintf(fh,['\t[%' num2str(addrlen) 'X..%' num2str(addrlen) 'X]:%' num2str(datalen) 'X;\r\n'],idepth-sametotal,idepth-1,value);
end,
sametotal=1;
value=var(i,j,k);
end,
else
break;
end,
end,
end,
end,
end,
if(idepth<depth)
if(sametotal==1)
fprintf(fh,['\t%' num2str(addrlen) 'X:%' num2str(datalen) 'X;\r\n'],idepth,value);
else
fprintf(fh,['\t[%' num2str(addrlen) 'X..%' num2str(addrlen) 'X]:%' num2str(datalen) 'X;\r\n'],idepth-sametotal+1,idepth,value);
end,
end,
if(idepth<depth-1)
if(idepth==(depth-2))
fprintf(fh,['\t%' num2str(addrlen) 'X:%' num2str(datalen) 'X;\r\n'],idepth+1,0);
else
fprintf(fh,['\t[%' num2str(addrlen) 'X..%' num2str(addrlen) 'X]:%' num2str(datalen) 'X;\r\n'],idepth+1,depth-1,0);
end,
end,
%%%%%%%%%%
%%%%%%%%%%
fprintf(fh,'END;\r\n');
fclose(fh);
网上的生成的matlab源程序
页:
[1]