matlab读mif文件,关于QuartusII里面调用MATLAB里生成的mif文件的一些问题(转) |
您所在的位置:网站首页 › rom文件怎么打开 › matlab读mif文件,关于QuartusII里面调用MATLAB里生成的mif文件的一些问题(转) |
关于QuartusII里面调用MATLAB里生成的mif文件的一些问题(转) 最近做DDS正弦信号发生器,需要用到MATLAB生成一个正弦信号的ROM(MIF文件)。首先在MATLAB里面建立一个M-File,程序如下: depth=4096; %存储单元数; widths=12; %数据宽度为12位; N=0:1: 4096; s=sin(pi*N/2048); %计算0~pi/2的Sin值; fidc=fopen('dds.mif','wt'); %以"wt"的形式打开,\n为换行 % 写入 dds.mif % fprintf(fidc,'depth=%d;\n',depth); fprintf(fidc,'width=%d;\n',widths); fprintf(fidc,'address_radix=dec;\n'); fprintf(fidc,'data_radix = dec;\n'); fprintf(fidc,'Content Begin\n'); for(x=1:depth) fprintf(fidc,'%d:%d;\n',x-1, round(2047*sin(pi*(x-1)/ 2048)+2048)); end fprintf(fidc,'end;'); fclose(fidc); 再点RUN就生成所要的MIF文件了,保存在C:\MATLAB7\work 当然程序是老师写的,本人对这东东一无所知,不怕读者笑话。(也正是因为对这个程序的茫然不知,导致后面严重的错误。) 然后在QuartusII调用一个12位宽,4096位深度的LPM_ROM,指定路径C:\MATLAB7\work,我已为大功告成。 可接下来问题来了,在QII里面编译时出现如下错误: Error: Data at line 6 exceeds the specified width (12) in the Memory Initialization File "C:/MATLAB7/work/dds.mif" Error: Memory Initialization File or Hexadecimal (Intel-Format) File C:/MATLAB7/work/dds.mif contains illegal syntax at line 6 Error: Can't read Memory Initialization File or Hexadecimal (Intel-Format) File C:/MATLAB7/work/dds1.mif for ROM instance . If the file exists, it is not in correct format. Error: Can't elaborate user hierarchy "lpm_rom0:inst|altsyncram:altsyncram_component|altsyncram_3c81:auto_generated" Error: Quartus II Analysis & Synthesis was unsuccessful. 4 errors, 1 warning 第一个错误是说我生成的MIF文件第六行数据已经超过的指定的数据位宽。后面的大概就是QII里面的一些VHDL语法错误了(我认为,因为英文不好)。 可是我想不通的是调用LPM_ROM明明是12位宽,4096的深度怎么会不对呢?问老师说不知道,且说MATLAB程序没问题。于是本人只好百度一下了,结果找到一篇这样的文章: 用MATLAB生成*.mif、*.hex(QUARTUS II)文件简介 做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程序的写法,希望对大家有用。 %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文件就可以了。 --------------------------------- 我注意到 下面这两行: ADDRESS_RADIX=UNS; DATA_RADIX=UNS; 和老师的程序有点出入,于是把MATLAB生成的MIF文件里面的DEC改成UNS,就是把有符号10进制改成无符号十进制的整数,再重新调用LPM_ROM,编译就顺利通过了: *博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |