Matlab与Modelsim的联合仿真(转)

实验室做FPGA算法的基本流程:matlab进行功能层的仿真,然后再用matlab做算法层的仿真。Matlab功能之强大在于基本所有的数字信号处理在其内部都有相应的函数能够实现,功能层的仿真十分的简单。算法层的仿真验证目的在于对我们所设计的算法是否可行,即是否能够实现我们所需的功能。算法层代码的细化程度与自身设计习惯有关,原则在于自己通过算法层的matlab程序能够写出Verilog程序即可。当算法层结果与功能层的完全一样时我们就认为算法仿真完成,我们设计的算法没有问题。接下来就是用QuartusII+ModelSim编写Verilog代码并仿真。当我们将Verilog的仿真结果与matlab仿真的两层的结果比对后符合我们的条件则完成了我们的算法实现。(算法实现与算法优化不同)

在两者仿真结果的比对测试中,需要用到最多的就是matlab与modelsim直接的联合仿真。及两者之间的数据传递。

1.matlab产生数据供modelsim使用。

A.matlab将数据写入txt。

fid = fopen('data_in.txt','w');

for i = 1:1:752

    fprintf(fid,'%d\n',data_bits(i));

end

fclose(fid);

B.modelsim在testbench中读入txt

reg  data_matlab[0:751];

initial  $readmemh("E:/MATLAB/BCH/data_in.txt",data_matlab);

    2.modelsim产生数据供matlab使用。

    A.modelsim将数据写入txt。

integer    w_file;

initial    w_file = $fopen("data_out.txt");

…………

always @ (posedge clk)

begin

    if(en_out == 1)

    begin

       $fdisplay(w_file,"%d",data_out);

    end

end

B.matlab读入数据。

b = load('D:\FPGA\BCH_Coder\simulation\modelsim\data_out.txt');

直接load。或者:

fid = fopen('data_out.txt','r');

for i = 1 : 256;

    num(i) = fscanf(fid, '%x', 1);

end

fclose(fid);

posted on 2010-12-01 09:49  忧郁古城  阅读(651)  评论(0)    收藏  举报

导航