<?xml version="1.0" encoding="gbk"?> <rss version="2.0"><channel> <title>定阅帖子更新</title> <link>http://www.broadkey.com.cn/XML.ASP</link><description>TEAM Board - 意得法电子</description> <copyright>TEAM 2.0.5 Release</copyright><generator>TEAM Board by TEAM5.Cn Studio</generator> <ttl>30</ttl><item><link>http://www.broadkey.com.cn/Thread.asp?tid=347 </link><title>关于QuartusII里面调用MATLAB里生成的mif文件的一些问题（转</title><author>dl11</author><pubDate>2009-12-09 10:36:51</pubDate><description><![CDATA[<div>最近做DDS正弦信号发生器，需要用到MATLAB生成一个正弦信号的ROM（MIF文件）。首先在MATLAB里面建立一个M-File，程序如下：</div>
<div>depth=4096;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %存储单元数;</div>
<div>widths=12;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %数据宽度为12位;</div>
<div>N=0:1: 4096;</div>
<div>s=sin(pi*N/2048);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %计算0~pi/2的Sin值;</div>
<div>fidc=fopen('dds.mif','wt');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %以&quot;wt&quot;的形式打开,\n为换行</div>
<div>% 写入 dds.mif %</div>
<div>fprintf(fidc,'depth=%d;\n',depth);</div>
<div>fprintf(fidc,'width=%d;\n',widths);</div>
<div>fprintf(fidc,'address_radix=<font color="#ff0000">dec</font>;\n');</div>
<div>fprintf(fidc,'data_radix = <font color="#ff0000">dec</font>;\n');</div>
<div>fprintf(fidc,'Content Begin\n');</div>
<div>for(x=1:depth)</div>
<div>fprintf(fidc,'%d:%d;\n',x-1, round(2047*sin(pi*(x-1)/ 2048)+2048));</div>
<div>&nbsp;</div>
<div>end</div>
<div>fprintf(fidc,'end;');</div>
<div>fclose(fidc);</div>
<div>再点RUN就生成所要的MIF文件了，保存在C:\MATLAB7\work</div>
<div>当然程序是老师写的，本人对这东东一无所知，不怕读者笑话。（也正是因为对这个程序的茫然不知，导致后面严重的错误。）</div>
<div>然后在QuartusII调用一个12位宽，4096位深度的LPM_ROM,指定路径<font size="5">C:\MATLAB7\work，我已为大功告成。</font></div>
<div><font size="5">可接下来问题来了，在QII里面编译时出现如下错误：</font></div>
<div>Error: Data at line 6 exceeds the specified width (12) in the Memory Initialization File &quot;C:/MATLAB7/work/dds.mif&quot;<br />
Error: Memory Initialization File or Hexadecimal (Intel-Format) File C:/MATLAB7/work/dds.mif contains illegal syntax at line 6<br />
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.<br />
Error: Can't elaborate user hierarchy &quot;lpm_rom0:inst|altsyncram:altsyncram_component|altsyncram_3c81:auto_generated&quot;<br />
Error: Quartus II Analysis &amp; Synthesis was unsuccessful. 4 errors, 1 warning</div>
<div>第一个错误是说我生成的MIF文件第六行数据已经超过的指定的数据位宽。后面的大概就是QII里面的一些VHDL语法错误了（我认为，因为英文不好）。</div>
<div>可是我想不通的是调用LPM_ROM明明是12位宽，4096的深度怎么会不对呢？问老师说不知道，且说MATLAB程序没问题。于是本人只好百度一下了，结果找到一篇这样的文章：</div>
<div style="text-align: center; line-height: 21.6pt">用MATLAB生成*.mif、*.hex（QUARTUS II）文件简介</div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 270pt"><a href="mailto:macro_qian@yahoo.com.cn"><font color="#0033cc">macro_qian@yahoo.com.cn</font></a></div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 270pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 24pt">做FPGA设计时经常要调用芯片内存。特别对于ROM，内存的初始化就显得比较重要。当然你完全可以手工在QUARTUS II打开mif文件的表格里或是在EXCEL中逐个输入，几十项（字）或是近百项（字）你还可以接受，如果上千项或是更多呢？估计能累的人吐血！</div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 24pt">一般内存的初始化数据都是有规律的，符合一定的函数，我们完全可以用MATLAB来完成(如果没规律只能逐项手工输入了)。</div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 24pt">首先，我们应该知道*.mif文件的格式。它是文本格式。随便打开一个mif文件，你会发现它具有如下的格式：</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">-- Copyright (C) 1991-2007 Altera Corporation</div>
<div style="text-align: center; line-height: 21.6pt">-- Your use of Altera Corporation's design tools, logic functions</div>
<div style="text-align: center; line-height: 21.6pt">-- and other software and tools, and its AMPP partner logic</div>
<div style="text-align: center; line-height: 21.6pt">-- functions, and any output files from any of the foregoing</div>
<div style="text-align: center; line-height: 21.6pt">-- (including device programming or simulation files), and any</div>
<div style="text-align: center; line-height: 21.6pt">-- associated documentation or information are expressly subject</div>
<div style="text-align: center; line-height: 21.6pt">-- to the terms and conditions of the Altera Program ******</div>
<div style="text-align: center; line-height: 21.6pt">-- Subscription Agreement, Altera MegaCore Function ******</div>
<div style="text-align: center; line-height: 21.6pt">-- Agreement, or other applicable ****** agreement, including,</div>
<div style="text-align: center; line-height: 21.6pt">-- without limitation, that your use is for the sole purpose of</div>
<div style="text-align: center; line-height: 21.6pt">-- programming logic devices manufactured by Altera and sold by</div>
<div style="text-align: center; line-height: 21.6pt">-- Altera or its authorized distributors.&nbsp; Please refer to the</div>
<div style="text-align: center; line-height: 21.6pt">-- applicable agreement for further details.</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">-- Quartus II generated Memory Initialization File (.mif)</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">WIDTH=8;</div>
<div style="text-align: center; line-height: 21.6pt">DEPTH=256;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt"><font color="#008000"><font size="1">ADDRESS_RADIX=UNS;</font></font></div>
<div style="text-align: center; line-height: 21.6pt"><font color="#008000"><font size="1">DATA_RADIX=UNS;</font></font></div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">CONTENT BEGIN</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0:127;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1:126;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2:126;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &hellip;&hellip;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp; &hellip;&hellip;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 253:126;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 254:126;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 255:127;</div>
<div style="text-align: center; line-height: 21.6pt">END;</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">格式很简单吧。首先根据需要设置每个字的位宽WIDTH和总字数DEPTH。然后设置地址和数据的进制基数ADDRESS_RADIX、DATA_RADIX，建议大家不要修改，就使用无符号数（UNS）。然后用MATLAB生成需要的数据（按上边的格式，注意中间&ldquo;：&rdquo;，最后&ldquo;<strong>;</strong>&rdquo;），往CONTENT BEGIN和END中间一贴就行了。</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">下边举例说明MATLAB程序的写法，希望对大家有用。</div>
<div style="text-align: left; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: left; line-height: 21.6pt">%the walue of cosine</div>
<div style="text-align: left; line-height: 21.6pt">function data=&quot;makedata&quot;</div>
<div style="text-align: left; line-height: 21.6pt">index = linspace(0,2*pi,2^8);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
<div style="text-align: left; line-height: 21.6pt">cos_value = cos(index);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
<div style="text-align: left; line-height: 21.6pt">cos_value = cos_value * (2^7 -1 );&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
<div style="text-align: left; line-height: 21.6pt">cos_value = fix(cos_value);</div>
<div style="text-align: left; line-height: 21.6pt">cos_value =abs(cos_value);</div>
<div style="text-align: left; line-height: 21.6pt">for i=&quot;65:192&quot;</div>
<div style="text-align: left; line-height: 21.6pt">&nbsp;&nbsp;&nbsp; cos_value(i)=cos_value(i)+128;</div>
<div style="text-align: left; line-height: 21.6pt">end</div>
<div style="text-align: center; line-height: 21.6pt">%///////////////////////////////////////上边的用来生成数据，下边的用于控制格式输出（对大家有用的）</div>
<div style="text-align: left; line-height: 21.6pt">number=[0:255];</div>
<div style="text-align: left; line-height: 21.6pt">for i=&quot;1:256&quot;</div>
<div style="text-align: left; line-height: 21.6pt">&nbsp;&nbsp;&nbsp; comer(i)=':';</div>
<div style="text-align: left; line-height: 21.6pt">end</div>
<div style="text-align: left; line-height: 21.6pt">for i=&quot;1:256&quot;</div>
<div style="text-align: left; line-height: 21.6pt">&nbsp;&nbsp;&nbsp; semi(i)=';';</div>
<div style="text-align: left; line-height: 21.6pt">end</div>
<div style="text-align: left; line-height: 21.6pt">data=[number; comer; cos_value; semi];</div>
<div style="text-align: left; line-height: 21.6pt">fid=fopen('d:\data.txt','w');</div>
<div style="text-align: left; line-height: 21.6pt">fprintf(fid, '%d%c%d%c\n', data);</div>
<div style="text-align: left; line-height: 21.6pt">fclose(fid);</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt">在D盘下找到data.txt文件，用写字板打开mif文件，将data.txt中的内容贴到CONTENT BEGIN和END中间，然后保存就可以了。</div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 24pt">对于hex文件，更简单。大家生成mif文件后，用QUARTUS II打开，然后另存为hex文件就可以了。</div>
<div style="text-align: center; line-height: 21.6pt; text-indent: 24pt">---------------------------------</div>
<div><font color="#2f485a">我注意到</font>&nbsp;下面这两行：</div>
<div style="text-align: center; line-height: 21.6pt"><font color="#008000"><font size="1">ADDRESS_RADIX=UNS;</font></font></div>
<div style="text-align: center; line-height: 21.6pt"><font color="#008000"><font size="1">DATA_RADIX=UNS;</font></font></div>
<div style="text-align: center; line-height: 21.6pt"><font color="#008000"><font size="1">和老师的程序有点出入，于是把MATLAB生成的MIF文件里面的DEC改成UNS,就是把有符号10进制改成无符号十进制的整数，再重新调用LPM_ROM，编译就顺利通过了:</font></font></div>
<div style="text-align: center; line-height: 21.6pt">&nbsp;</div>
<div style="text-align: center; line-height: 21.6pt"><font color="#008000"><font size="1">这里要感谢这位仁兄，所以也将自己的一点体会摆出来一起交流，有什么不对的地方还请大家批评指正。</font></font></div>]]></description></item></channel></rss>