1、用matlab产生mif文件
首先自己用matlab产生自己需要的数据,然后调用外部的miffile.m文件即可生成需要的mif文件,需要注意的是,产生自己数据的时候,
需要对数据取整,否则quartus识别不了科学计数法。
下面举个例子
这是需要的外部miffile.m文件,需要将该文件放在自己编程的目录下
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. '); fprintf(fh,'--xxxxx@126.com. '); fprintf(fh,'--%s. ',datestr(now)); fprintf(fh,'WIDTH=%d; ',width); fprintf(fh,'DEPTH=%d; ',depth); fprintf(fh,'ADDRESS_RADIX=HEX; '); fprintf(fh,'DATA_RADIX=HEX; '); fprintf(fh,'CONTENT BEGIN '); %%%%%% %%%%%% var=rem(var,2^width);%% clip to fit the width; [sx,sy,sz]=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,[' %' num2str(addrlen) 'X:%' num2str(datalen) 'X; '],idepth-1,value); else fprintf(fh,[' [%' num2str(addrlen) 'X..%' num2str(addrlen) 'X]:%' num2str(datalen) 'X; '],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,[' %' num2str(addrlen) 'X:%' num2str(datalen) 'X; '],idepth,value); else fprintf(fh,[' [%' num2str(addrlen) 'X..%' num2str(addrlen) 'X]:%' num2str(datalen) 'X; '],idepth-sametotal+1,idepth,value); end, end, if(idepth<depth-1) if(idepth==(depth-2)) fprintf(fh,[' %' num2str(addrlen) 'X:%' num2str(datalen) 'X; '],idepth+1,0); else fprintf(fh,[' [%' num2str(addrlen) 'X..%' num2str(addrlen) 'X]:%' num2str(datalen) 'X; '],idepth+1,depth-1,0); end, end, %%%%%%%%%% %%%%%%%%%% fprintf(fh,'END; '); fclose(fh);
产生一个1024X1024的sin函数mif文件,然后就可以在目录下方找到自己的mif文件。
n = 0 : 1023; N = 1023; y = 512 + 511*sin(2*pi*n/N) ; y = fix(y); plot(n,y); miffile('rom_sin_10X10.mif',y,10,1024)
2、再就是可以用网上有些人写好的软件产生mif文件
- 将图片转化成mif文件的BMP2MIF工具
- 产生方波,三角波,正弦波,锯齿波的Mif_Maker工具
- 字模产生工具PCtoLCD2002 配合bingo的C2mif软件生成字模
我把软件传到github上了,大家可以自行下载。
https://github.com/bxpgithub/fpga_tools