数学建模社区-数学中国
标题:
用MATLAB 生成*.mif和*.hex(QUARTUS II)文件
[打印本页]
作者:
建不了的模。
时间:
2015-1-20 13:52
标题:
用MATLAB 生成*.mif和*.hex(QUARTUS II)文件
MATLAB生成*.mif、*.hex2008-08-31 03:06做FPGA设计时经常要调用芯片内存。特别对于ROM,内存的初始化就显得比较重要。当然你完全可以手工在QUARTUS II打开mif文件的表格里或是在EXCEL中逐个输入,几十项(字)或是近百项(字)你还可以接受,如果上千项或是更多呢?估计能累的人吐血!
) J7 }" p, y8 r( d J% j
/ K+ C; ^( W6 P4 @1 w1 \0 [( O
一般内存的初始化数据都是有规律的,符合一定的函数,我们完全可以用MATLAB来完成(如果没规律只能逐项手工输入了)。
* _" H" {: c; w) B' N; L4 q
' _4 J1 f* n4 h( ^$ E) ]1 j2 y0 ~$ b' p2 W
首先,我们应该知道*.mif文件的格式。它是文本格式。随便打开一个mif文件,你会发现它具有如下的格式:
3 T3 K, o H- R4 T7 z( e n
$ M1 r1 i+ x3 {9 k( o9 ~* a, Q L
z1 f- @! ~! R9 H" R, z
-- 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;
0 g6 j) h5 f" ~' w, @& q
格式很简单吧。首先根据需要设置每个字的位宽WIDTH和总字数DEPTH。然后设置地址和数据的进制基数ADDRESS_RADIX、DATA_RADIX,建议大家不要修改,就使用无符号数(UNS)。然后用MATLAB生成需要的数据(按上边的格式,注意中间“:”,最后“;”),往CONTENT BEGIN和END中间一贴就行了。
7 X- ] u: W9 w2 n8 q6 ?
& l3 I; {. Z- l4 R8 F
下边举例说明MATLAB程序的写法,希望对大家有用
4 J, j/ i, S1 V: x4 ]: Z; J6 [
( M1 L: [$ x: U
function data=makedata
%使用MATLAB生成MIF & HEX2文件
%see also
http://www.matlabsky.com
%2009.1.10
%
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:\mif_data_matlabsky.txt','w');
fprintf(fid, '%d%c%d%c\n', data);
fclose(fid);
% _+ b% g0 S- ^3 w: P$ w
在d:\下找到mif_data_matlabsky.txt'文件,用写字板打开mif文件,将data.txt中的内容贴到CONTENT BEGIN和END中间,然后保存就可以了。或者将CONTENT BEGIN和END两边的内容分别贴于data.txt文件的前后,然后保存为.mif文件。
% J$ l* @/ P; H& a
) V8 b( }# m: y8 T1 B. a/ R
对于hex文件,更简单。大家生成mif文件后,用QUARTUS II打开,然后另存为hex
% P6 D9 o! K# R T" }5 F# A: j
, q; `9 g! g0 Z/ C
5 F* `1 s# o% ^/ n% T! M0 i8 P
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5