MatLab中生成QuartusII ROM的初始化文件的主要代码
文章来源:不详 作者:佚名
--------------------------------------------------------------------------------
该文章讲述了MatLab中生成QuartusII ROM的初始化文件的主要代码.
Matlab生成mif文件的主要代码如下,其中比较需要注意的是,是要将radix定义为UNS,即无符号整型。
下面是主要代码
function makemif(num)
width = 16;
depth = 105;
N = 0:1:104;
fidc = fopen('thres.mif','wt');
fprintf(fidc,'width=%d;n',width);
fprintf(fidc,'depth=%d;n',depth);
fprintf(fidc,'address_radix=UNS;n');
fprintf(fidc,'data_radix=UNS;n');
fprintf(fidc,'Content Beginn');
for i = 1:depth
fprintf(fidc,'%d:%d;n',(i-1),num(i));
end
fprintf(fidc,'end;');
fclose(fidc);
end
%num=[1:105];
%makemif(num);运行前先建立“thres.mif”空文档,运行这两行程序后就写完成了thres文件
在Modelsim仿真中不能使用mif,只能使用hex文件,这个就比较简单了,直接使用QuartusII打开,然后另存为hex文件类型。
——————————————————————————————————————————————————————
再给出一个例子:
首先,我们应该知道*.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%cn',
data);
fclose(fid);
在D盘下找到data.txt文件,用写字板打开mif文件,将data.txt中的内容贴到CONTENT
BEGIN和END中间,然后保存就可以了。
————————————————————————————————————————————————————————————
首先在Quartus
II中打开ROM数据文件编辑窗口,即选择File|New命令,在New对话框中切换到Other Files选项卡,再选择Memory
Initialization
File选项,单击OK按钮后产生ROM数据文件大小选择窗口。这里采用64点8位数据的情况,可选ROM的数据数Number为64,数据宽Word
size取8位。单击OK按钮,将出现如图所示的空mif数据表格,表格中的数据为10进制形式,任一数据(如第三行的99)对应的地址为左列数与顶行数之和)。将波形数据填入此表中,完成后在File菜单中单击Save
as命令,保存此数据文件。在这里不妨取名为romd.mif。