打印

matlab输出mif文件

[复制链接]
2168|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
星星之火红|  楼主 | 2012-10-16 20:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
星星之火红|  楼主 | 2012-10-16 20:43 | 只看该作者
做 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 中间一贴就行了。

使用特权

评论回复
板凳
星星之火红|  楼主 | 2012-10-16 20:43 | 只看该作者
下边举例说明 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 文件就可以了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

101

主题

1782

帖子

22

粉丝