用matlab生成mif文件

module rom_ip(
        clk,data,
        rst_n
);
input                        clk;
input                        rst_n;
output    [7:0]            data;

reg        [7:0]            add;
rom_ip_mif    rom_ip_mif_inst (
    .address ( add ),
    .clock ( clk ),
    .q ( data )
    );

always @(posedge clk or negedge rst_n)
    if(!rst_n)begin
        add <= 8‘d0;
    end
    else if(add == 8‘hff)
        add <= 8‘d0;
    else begin
        add <= add + 1‘b1;
    end

endmodule

`timescale 1ns/1ns

module    tb_romip();

reg                clk;
reg                rst_n;
wire    [7:0]        data;

rom_ip   U1(
        .clk(clk),
        .data(data),
        .rst_n(rst_n)
);

initial begin
    clk = 1;
    rst_n = 0;
    #20;
    rst_n = 1;
    #2000;
    $stop;
end

always #10 clk =~ clk;

endmodule
a=0:255
miffile(‘rom_ipp.mif‘,a,8,256)
这是在matlab中调用的miffile函数来生成mif文件
‘’单引号里面为所生成文件的名字  注:所生成的文件在matlab所编译的文件夹内
a  为产生的数据
8为位宽
256为深度
代码内容为
function miffile(filename,var,width,depth)
%       function miffile(filename,var,width,depth)
%       It creates a ‘mif‘ file called filename,which be written with var.
%       The ‘mif‘ file is a kind of file formats which is uesed in Altera‘s
%       EDA tool,like maxplus II ,quartus II,to initialize the memory
%       models,just like cam,rom,ram.
%       Using this function,you can easily produce the ‘mif‘ file written
%       with all kinds of your data.
%       If the size of ‘var‘ is shorter than ‘depth‘,0 will be written for the
%       lefts.If the size of ‘var‘ is greater than ‘depth‘,than only ‘depth‘ former
%       data of ‘var‘ will be written;
%       the radix of address and data is hex
%       filename --the name of the file to be created,eg,"a.mif",string;
%       var ----the data to be writed to the file, can be 3D or less ,int or other fittable;
%       width --the word size of the data,width>=1,int;
%       depth --the number of the data to be writed,int;
%
%       because matlab read the matrix is colum first,if you want to write
%       the ‘var‘ data in row first mode, just set var to var‘;
%
%       example:
%             a=uint8(rand(16,16)*256);
%             miffile(‘randnum.mif‘,a,8,256);

if(nargin~=4) %% be tired to do more inupts check!
    error(‘Need 4 parameters! Use help miffile for help!‘);
end, 

fh=fopen(filename,‘w+‘);
fprintf(fh,‘--Created by xxxx.\r\n‘);
fprintf(fh,‘[email protected]\r\n‘);
fprintf(fh,‘--%s.\r\n‘,datestr(now));
fprintf(fh,‘WIDTH=%d;\r\n‘,width);
fprintf(fh,‘DEPTH=%d;\r\n‘,depth);
fprintf(fh,‘ADDRESS_RADIX=HEX;\r\n‘);
fprintf(fh,‘DATA_RADIX=HEX;\r\n‘);
fprintf(fh,‘CONTENT BEGIN\r\n‘);
%%%%%%
%%%%%%
var=rem(var,2^width);%% clip to fit the width;
[sx,sy,sz]=size(var);%% can only fit 3D or less;
value=var(1,1,1);
sametotal=1;
idepth=0;
addrlen=1;
temp=16;
while(temp<depth) %%decide the length of addr
       temp=temp*16;
       addrlen=addrlen+1;
end,
datalen=1;
while(temp<width) %%decide the length of data
       temp=temp*16;
       datalen=datalen+1;
end,
for k=1:sz,
    for j=1:sy,
        for i=1:sx,
            if(~((i==1 ) &&( j==1) &&( k==1)))
               if(idepth<depth),
                  idepth=idepth+1;
                if(value==var(i,j,k))
                    sametotal=sametotal+1;
                    continue;
                else

                        if(sametotal==1)
                           fprintf(fh,[‘\t%‘ num2str(addrlen) ‘X:%‘ num2str(datalen) ‘X;\r\n‘],idepth-1,value);
                        else
                           fprintf(fh,[‘\t[%‘ num2str(addrlen) ‘X..%‘ num2str(addrlen) ‘X]:%‘ num2str(datalen) ‘X;\r\n‘],idepth-sametotal,idepth-1,value);
                        end,
                       sametotal=1;
                       value=var(i,j,k);
                end,
                    else
                 break;

                end,
            end,
        end,
    end,
end,
if(idepth<depth)
             if(sametotal==1)
               fprintf(fh,[‘\t%‘ num2str(addrlen) ‘X:%‘ num2str(datalen) ‘X;\r\n‘],idepth,value);
              else
                 fprintf(fh,[‘\t[%‘ num2str(addrlen) ‘X..%‘ num2str(addrlen) ‘X]:%‘ num2str(datalen) ‘X;\r\n‘],idepth-sametotal+1,idepth,value);
              end,
end,
if(idepth<depth-1)
    if(idepth==(depth-2))
        fprintf(fh,[‘\t%‘ num2str(addrlen) ‘X:%‘ num2str(datalen) ‘X;\r\n‘],idepth+1,0);
    else
        fprintf(fh,[‘\t[%‘ num2str(addrlen) ‘X..%‘ num2str(addrlen) ‘X]:%‘ num2str(datalen) ‘X;\r\n‘],idepth+1,depth-1,0);
    end,
end,
%%%%%%%%%%
%%%%%%%%%%
fprintf(fh,‘END;\r\n‘);
fclose(fh);
时间: 2024-08-04 04:29:13

用matlab生成mif文件的相关文章

matlab生成exe文件

matlab生成exe文件 1 首先要将\MATLAB701\toolbox\compiler\deploy\win32 中MCRinstaller.exe 安装到该计算机上(7.0 以前的版本是mglinstaller.exe),若你的matlab是安装版本的,这个工具可能已经安装过了,就不用安装了:否则需要安装.2 在确定安装好MatlabCompiler后,还需要对Compiler 进行适当的配置,方法是在Matlab 命令窗口输入:Mbuild –setup然后根据提示执行相应的操作,使

生成mif文件的几种方法总结

mif文件就是存储器初始化文件,即memory initialization file,用来配置RAM或ROM中的数据.生成QuartusII11.0可用的mif文件,有如下几种方式: 方法1:利用Quartus自带的mif编辑器 优点:对于小容量RAM可以快速方便的完成mif文件的编辑工作,不需要第三方软件的编辑: 缺点:一旦数据量过大,一个一个的输入会使人崩溃: 使用方法:在quartus中,[file]/[new],选择Memory  Initialization file,弹出如下窗口:

Matlab生成mif格式波形文件

Altera ROM支持HEX和mif格式文件,生成代码如下: 注意: mif  文件都是ASCⅡ码,负数应该是用反码补码处理的,否则负数会处理为0.另外可以用波形生成工具Guagle_wave生成mif波形文件 clc; clear; close all; width = 10; depth = 2^width; t = linspace(0,6.28,depth); sin_val = sin(t); %cos_val = cos(t); sin_val = fix(sin_val*(2^w

matlab 生成mat文件

生成MAT文件 假如你的矩阵A.B都已赋值,现在需要将A,B矩阵保存成mat格式文件,执行如下命令:save mydata A B 就会把A B矩阵数据保存在yourdata.dat文件中了 保存指定数据为MAT文件:假如我们只想保存A矩阵,那么,我们可以用如下命令:save mydata2 A 回车 这样就可以将矩阵A单独保存起来了 保存所有数据为MAT文件:如果有多个矩阵,我们都需要将其保存,如果我们这样写,save mydata2 A B C D....这样写会很麻烦,我们直接用下面命令便

matlab生成HEX文件

HEX文件格式不赘述,写里直接放上代码.请批评改正. 1 %%convert a sinal data into hex file format 2 % data format:16bit 3 % signal length: less than 2^24-1 4 % author: Yang Li [email protected] 5 % data:2015.01.27 6 7 clear all; 8 close all; 9 clc; 10 11 %% fixed point or bi

asp.net下调用Matlab生成动态链接库

对于这次论文项目,最后在写一篇关于工程的博客,那就是在asp.net下调用matlab生成的dll动态链接库.至今关于matlab,c/c++(opencv),c#(asp.net)我总共写了4篇配置的博客,配置问题搞了我们差不多2周多的时间,各种编译不通过,各种链接出问题,各种头疼,甚至都有放弃asp.net下建工程,转用matlab的gui来实现前端界面的念头了.但是在队友的强烈要求下,我不得不硬着头皮搞下这配置问题,最终总结出这4篇配置博客.其中艰辛,恐怕连队友都不甚了解. 当然在实验室同

FPGA学习笔记之mif文件生成方法总结

方法1:利用Quartus自带的mif编辑器 优点:对于小容量RAM可以快速方便的完成mif文件的编辑工作,不需要第三方软件的编辑: 缺点:一旦数据量过大,一个一个的输入会使人崩溃: 使用方法:在quartus中,[file]/[new],选择Memory  Initialization file,弹出如下窗口: Number of words:可寻址的存储单元数,对于8bit地址线,此处选择256: words size:存储单元宽度,8bit: 然后点击“OK”. 在表格中输入初始化数据:

mif文件生成方法

mif文件就是存储器初始化文件,即memory initialization file,用来配置RAM或ROM中的数据.常见生成方法: Quartus自带的mif编辑器生成 mif软件生成 高级编程语言生成 前两种方法都有一定的缺陷,主要介绍第三种方法,按照mif文件格式,使用高级编程语言(Matlab.C)编写生成. 注意: mif文件都是ASCⅡ码,负数应该是用补码处理,否则负数在mif文件中会变成0. Matlab参考代码如下: clc;clear;close all; width = 1

fpga rom 初始化mif文件生成

mif文件的格式 width= depth= address_radix= data_radix= content begin 00:    ; 01:   ; 02:   ; .... end; 所以只需要用matlab生成中间部分的数,然后添加头尾格式就可以了,当然文件后缀一定得是.mif matlab例子 clcclear alln=[1:1000];ratio=0.125;%ratio means fo/fs;sintemp=sin(2*pi*ratio*n);sinround=roun