Verilog仿真文件testbench编写样例

合集下载

verilogtestbench写法

verilogtestbench写法

verilogtestbench写法Verilog测试平台(testbench)技术(⼀) 收藏对设计进⾏功能仿真和时序仿真时,需要给待测模块提供激励输⼊。

对于由Verilog语⾔描述的设计模块,最好的⽅法⾃然同样是⽤Verilog语⾔对待测模块施加激励和检测模块的输出响应。

实际应⽤中,Verilog测试平台(testben ch)就是⽤来提供上述功能的。

Verilog测试平台是⼀个例化的待测(MUT)Verilog 模块,给它施加激励并观测其输出。

由于测试平台是⽤Verilog语⾔描述的,因此可以应⽤到不同的仿真环境中。

待测模块和与之对应的测试平台组成⼀个仿真模型,应⽤这个模型可以在不同的测试环境中⽤相同的激励对待测模块进⾏调试。

下⾯就对不同电路类型分别介绍verilog测试平台的语⾔结构。

⼀、测试平台1.组合电路测试设计组合电路的测试平台时,待测模块及其功能决定了激烈的选择与测试次数。

对于⼀个已有的待测模块,测试平台中需要声明与待测模块输⼊输出端⼝对应的变量。

与输⼊端⼝相连接的变量定义为reg,与输出端⼝相连接的变量定义为wire,例化时将测试平台中声明的变量与待测模块的输⼊输出端⼝相关联。

使⽤initial语句控制程序运⾏,initial语句是⼀种过程结构,在initial块中可使⽤延迟控制语句来控制initial块中的程序流动。

这⾥对⼀个简单的算术逻辑单元(ALU)为例进⾏测试,下⾯是该单元Verilog 描述。

/***************************************************************** ********///多动能ALU的Verilog代码'timescale 1ns/100psmodule alu_4bit(a,b,f,oe,y,p,ov,a_gt_b,a_eg_b,a_lt_b);input [3:0] a,b;input [1:0] f;input oe;input [3:0] y;output p,ov,a_gt_b,a_eg_b,a_lt_b; reg [4:0] im_y;always @(a or b or f)beginov=1'b0;im_y=0;case(f)2'b00:beginim_y=a+b;if(im_y>5'b01111)ov=1'b1;end2'b01:beginim_y=a-b;if(im_y>5'b01111)ov=1'b1;end2'b10:im_y[3:0]=a&b;2'b11:im_y[3:0]=a^b;default:im_y[3:0]=4'b0000;endcaseendalways @(a or b)beginif(a>b){a_gt_b,a_ge_b,a_lt_b}=3'b100;else if(a{a_gt_b,a_ge_b,a_lt_b}=3'b001;else{a_gt_b,a_ge_b,a_lt_b}=3'b010;endassign p=^im_y[3:0];assign y=oe?im_y[3:0]:4'bz;endmodule/***************************************************************** ********/模块alu_4bit是四功能的算术逻辑单元,输⼊包括数据信号a、b和功能信号f,输出包括数据信号y和ALU⽣成的奇偶校验信号p、溢出信号ov及⽐较信号。

Verilog仿真文件testbench编写样例

Verilog仿真文件testbench编写样例

Verilog 仿真文件testbench编写样例`timescale 1ns/100psmodule testbench;localparam DATA_WIDTH = 32;localparam CLK_100_PERIOD = 5;localparam CLK_200_PERIOD = 2.5;localparam SIM_TIME = 150000;localparam ;localparam ;reg clk_100, clk_200;wire clk;assign clk = clk_100;alwaysbeginclk_100 = 0;forever #CLK_100_PERIOD clk_100 = ~clk_100;endalwaysbeginclk_200 = 0;forever #CLK_200_PERIOD clk_200 = ~clk_200;endreg rstn;integer fp_testin;integer fp_matlab_out;integer fp_sim_out;integer fp_outdiff;reg signed [DATA_WIDTH/2-1:0] matlab_in_re, matlab_in_im;reg signed [DATA_WIDTH/2-1:0] matlab_out_re, matlab_out_im;reg signed [DATA_WIDTH/2-1:0] matlab_diff_re, matlab_diff_im;reg signed [DATA_WIDTH/2-1:0] matlab_diff_re2, matlab_diff_im2;reg signed [DATA_WIDTH/2-1:0] max_diff_re, max_diff_im;initial beginmax_diff_re = 0;max_diff_im = 0;rstn = 0;#500rstn = 1;#SIM_TIMEsim_finish();$stop();endtask sim_finish;beginif(fp_testin!=0)$fclose(fp_testin);if(fp_matlab_out!=0)$fclose(fp_matlab_out);if(fp_sim_out)$fclose(fp_sim_out);if(fp_outdiff!=0)$fclose(fp_outdiff);endendtaskinitialbeginfp_testin = 0;fp_testin=$fopen(&quot;txt_file/input_data.txt&quot;,&quot; r&quot;);if(fp_testin==0)begin$display(&quot;input_data.txt open failed!&quot;); sim_finish();$stop();endelse begin$fscanf(fp_testin,&quot;%d, %d\n&quot;,matlab_in_re,matlab_in_im); endfp_matlab_out = 0;fp_matlab_out =$fopen(&quot;txt_file/matlab_out.txt&quot;,&quot; r&quot;);if(fp_matlab_out==0)begin$display(&quot;fp_matlab_out.txt openfailed!&quot;);sim_finish();$stop();endelse begin$fscanf(fp_matlab_out,&quot;%d, %d\n&quot;,matlab _out_re,matlab_out_im);endfp_sim_out = 0;fp_sim_out =$fopen(&quot;txt_file/modelsim_out.txt&quot;,&quo t;w&quot;);if(fp_sim_out == 0)begin$display(&quot;modelsim_out_re.txt openfailed!&quot;);sim_finish();$stop();endfp_outdiff = 0;fp_outdiff =$fopen(&quot;text_file/outdiff.txt&quot;,&quot;w& quot;);if(fp_outdiff==0)begin$display(&quot;outdiff.txt open failed!&quot;); sim_finish();$stop();endendalways @(posedge clk)beginif(stest_wvalid && stest_wready) //ready to changebeginif(~$feof(fp_testin))$fscanf(fp_testin,&quot;%d, %d\n&quot;,matlab_in_re,matlab_in_im); endelsebeginmatlab_in_re <= matlab_in_re;matlab_in_im <= matlab_in_im;endendalways @(posedge clk_100)beginif(mfc_wready && mfc_wvalid)beginmatlab_diff_re <= mfc_wdata_re - matlab_out_re; matlab_diff_im <= mfc_wdata_im - matlab_out_im; matlab_diff_re2 <= matlab_out_re - mfc_wdata_re ; matlab_diff_im2 <= matlab_out_im - mfc_wdata_im ; if(max_diff_re < matlab_diff_re)beginmax_diff_re <= matlab_diff_re;$display(&quot;max_diff_re:%dmax_diff_im:%d\n&quot;,max_diff_re,max_diff_im); endelse if(max_diff_re < matlab_diff_re2)beginmax_diff_re <= matlab_diff_re2;$display(&quot;max_diff_re:%dmax_diff_im:%d\n&quot;,max_diff_re,max_diff_im); endif(max_diff_im < matlab_diff_im)beginmax_diff_im <= matlab_diff_im;$display(&quot;max_diff_re:%dmax_diff_im:%d\n&quot;,max_diff_re,max_diff_im); endelse if(max_diff_im < matlab_diff_im2)beginmax_diff_im <= matlab_diff_im2;$display(&quot;max_diff_re:%dmax_diff_im:%d\n&quot;,max_diff_re,max_diff_im); end$fscanf(fp_matlab_out,&quot;%d, %d\n&quot;,matlab _out_re,matlab_out_im);$fwrite(fp_sim_out, &quot;%d, %d\n&quot;,mfc_wdata_re,mfc_wdata_im);$fwrite(fp_outdiff,&quot;%d, %d\n&quot;,matlab_diff_re,matlab_diff_i m);endendendmodule。

编写高效率的testbench

编写高效率的testbench

编写高效率的testbench简介:由于设计的规模越来越大也越来越复杂,数字设计的验证已经成为一个日益困难和繁琐的任务。

验证工程师们依靠一些验证工具和方法来应付这个挑战。

对于几百万门的大型设计,工程师们一般使用一套形式验证(formal verification)工具。

然而对于一些小型的设计,设计工程师常常发现用带有testbench的HDL仿真器就可以很好地进行验证。

Testbench已经成为一个验证高级语言(HLL --High-Level Language) 设计的标准方法。

通常testbench完成如下的任务:1.实例化需要测试的设计(DUT);2.通过对DUT模型加载测试向量来仿真设计;3.将输出结果到终端或波形窗口中加以视觉检视;4.另外,将实际结果和预期结果进行比较。

通常testbench用工业标准的VHDL或Verilog硬件描述语言来编写。

Testbench调用功能设计,然后进行仿真。

复杂的testbench完成一些附加的功能—例如它们包含一些逻辑来选择产生合适的设计激励或比较实际结果和预期结果。

后续的章节描述了一个仔细构建的testbench的结构,并且提供了一个自动比较实际结果与预期结果的进行自我检查的testbench例子。

图1给出了一个如上所描述步骤的标准HDL验证流程。

由于testbench使用VHDL或Verilog来描述,testbench的验证过程可以根据不同的平台或不同的软件工具实现。

由于VHDL或Verilog是公开的通用标准,使用VHDL或Verilog编写的testbench以后也可以毫无困难地重用(reuse)。

图1使用Testbench的HDL验证流程构建TestbenchTestbench用VHDL或Verilog来编写。

由于testbench只用来进行仿真,它们没有那些适用于综合的RTL语言子集的语法约束限制,而是所有的行为结构都可以使用。

因而testbench可以编写的更为通用,使得它们可以更容易维护。

组合-时序逻辑电路Verilog-Testbench代码_带仿真代码和波形_

组合-时序逻辑电路Verilog-Testbench代码_带仿真代码和波形_

1组合逻辑电路--基本门电路1.1基本门电路1.1.1结构化描述方式代码如下View Code1 module logics2 (3 input iA,4 input iB,5 output oAnd,6 output oOr,7 output oNot8 );910 and and_inst(oAnd,iA,iB);11 or or_inst(oOr,iA,iB);12 not not_inst(oNot,iA);1314 endmodule最底层的是门级原语and or not RTL级视图testbench如下View Code1 `timescale 1 ns/ 1 ns2 module logics_tb();34 reg ia;5 reg ib;67 wire oAnd;8 wire oOr;9 wire oNot;1011 initial12 begin13 ia=0;14 #40 ia=1;15 #40 ia=0;16 #40 ia=1;17 #40 ia=0;18 end1920 initial21 begin22 ib=0;23 #40 ib=0;24 #40 ib=1;25 #40 ib=1;26 #40 ib=0;27 end2829 logics logics_inst30 (31 .iA(ia),32 .iB(ib),33 .oAnd(oAnd),34 .oOr(oOr),35 .oNot(oNot)36 );3738 endmoduleRTL级仿真图形如下GATE级仿真图如下可见RTL级仿真是理想的,GATE级仿真考虑了延迟和信号开始的不确定。

1.1.2采用流描述方法代码如下View Code1 module logics2 (3 input iA,4 input iB,5 output oAnd,6 output oOr,7 output oNot8 );910 assign oAnd=iA&iB;11 assign oOr=iA|iB;12 assign oNot=~iA;1314 endmoduleRTL级视图,仿真图形同上。

Verilog-testbench的写法

Verilog-testbench的写法

数字集成电路设计入门--从HDL到版图于敦山北大微电子学系第十五章Verilog Test Bench使用简介学习内容:•用一个复杂的test bench复习设计的组织与仿真•建立test bench通常使用的编码风格及方法设计组织虚线表示编译时检测输入文件是否存在及可读并允许生成输出文件。

test bench 组织stimulus要验证的设计简单的test bench•简单的test bench 向要验证的设计提供向量,人工验证输出。

•复杂的test bench 是自检测的,其结果自动验证。

复杂的test bench激励验证结果要验证的设计并行块•fork…join块在测试文件中很常用。

他们的并行特性使用户可以说明绝对时间,并且可以并行的执行复杂的过程结构,如循环或任务。

module inline_ tb;reg [7: 0] data_ bus;// instance of DUTinitial forkdata_bus = 8'b00;Time | data_ bus0 | 8’b0000_0000 10 | 8’b0100_0101 30 | 8’b0100_0110 40 | 8’b0100_0111 45 | 8’b1000_1110#10 data_bus = 8'h45;#20 repeat (10) #10 data_bus = data_bus + 1;#25 repeat (5) #20 data_bus = data_bus<< 1;#140 data_bus = 8'h0f;joinendmodule上面的两个repeat循环从不同时间开始,并行执行。

象这样的特殊的激励集在单个的begin…end块中将很难实现。

50 | 8’b1000_1111 60 | 8’b1001_0000 65 | 8’b0010_0000 70 | 8’b0010_0001 80 | 8’b0010_0010 85 | 8’b0100_0100 90 | 8’b0100_0101 100 | 8’b0100_0110 105 | 8’b1000_1100 110 | 8’b1000_1101 120 | 8’b1000_1110 125 | 8’b0001_1100 140 | 8’b0000_1111包含文件•包含文件用于读入代码的重复部分或公共数据。

Verilog中inout类型的数据的使用和testbench仿真写法

Verilog中inout类型的数据的使用和testbench仿真写法

Verilog inout 双向口使用和仿真芯片外部引脚很多都使用inout类型的,为的是节省管腿。

一般信号线用做总线等双向数据传输的时候就要用到INOUT类型了。

就是一个端口同时做输入和输出。

inout在具体实现上一般用三态门来实现。

三态门的第三个状态就是高阻' Z'。

当inout端口不输出时,将三态门置高阻。

这样信号就不会因为两端同时输出而出错了,更详细的内容可以搜索一下三态门tri-state的资料.1 使用inout类型数据,可以用如下写法:inout data_inout;input data_in;reg data_reg;//data_inout的映象寄存器reg link_data;assign data_inout=link_data?data_reg:1’bz;//link_data控制三态门//对于data_reg,可以通过组合逻辑或者时序逻辑根据data_in对其赋值.通过控制link_data的高低电平,从而设置data_inout是输出数据还是处于高阻态,如果处于高阻态,则此时当作输入端口使用.link_data可以通过相关电路来控制.2 编写测试模块时,对于inout类型的端口,需要定义成wire类型变量,而其它输入端口都定义成reg类型,这两者是有区别的.当上面例子中的data_inout用作输入时,需要赋值给data_inout,其余情况可以断开.此时可以用assign语句实现:assign data_inout=link?data_in_t:1’bz;其中的link ,data_in_t是reg类型变量,在测试模块中赋值.另外,可以设置一个输出端口观察data_inout用作输出的情况:Wire data_out;Assign data_out_t=(!link)?data_inout:1’bz;else,in RTLinout use in top module(PAD)dont use inout(tri) in sub module也就是说,在内部模块最好不要出现inout,如果确实需要,那么用两个port实现,到顶层的时候再用三态实现。

Testbench写法总结


outer_port_tb_wire,inner_port_tb_wire);
end
else
begin
$display("\n **** time=%t ****",$time);
$display("ERROR! out_en=%d",out_en_tb);
$display("ERROR! outer_port_tb_wire != inner_port_tb_wire" );
$display("ERROR! outer_port_tb_wire=%d, inner_port_tb_wire=%d",
outer_port_tb_wire,inner_port_tb_wire);
end
end
endmodule
验证该双向端口的testbench结构如图2所示。
这是一个self-checking testbench,可以自动检查仿真结果是否正确,并在Modelsim控制台上打印出提示信息。图中Monitor完成信号采样、结果自动比较的功能。
testbench的工作过程为
1)out_en=1时,双向端口处于输出状态,testbench给inner_port_tb_reg信号赋值,然后读取outer_port_tb_wire的值,如果两者一致,双向端口工作正常。
module tb();
reg[7:0] inner_port_tb_reg;
wire[7:0] inner_port_tb_wire;
reg[7:0] outer_port_tb_reg;
wire[7:0] outer_port_tb_wire;

(verilog和vhdl)Testbench编程指南

(verilog和vhdl)Testbench编程指南TestBench编程指南如今数字设计的规模变得越来越庞大,设计的复杂程度也越来越高,这就使得设计的验证变得越来越困难,而且费时费力。

为了应对这种挑战,验证工程师依靠各种验证工具和方法。

对于大型设计,如几百万门的设计,通常采用一整套正式的验证工具。

然而,对于小一些的设计,设计工程师发现往往采用带TestBench的HDL仿真工具是最好的途径。

TestBench已经变成验证高级语言设计的一种标准的方法。

通常,TestBench执行以下任务:z例化设计,使其可测试(DUT-design under test);z通过将测试向量应用到模型来仿真例化后的可测试的设计;z将结果输出到终端,或者输出波形窗口;z将真实的结果和期望的结果进行比较;一般,TestBench采用工业标准的VHDL或者Verilog硬件描述语言来编写。

TestBench调用功能设计,然后仿真。

复杂的测试文件执行附加功能――例如,他们包含逻辑以决定合适的设计激励或者比较真实的结果和期望的结果。

以下章节将讨论一个组织良好的测试文件的组成,以及例举了一个带有自检的测试文件(自动将真实的结果和预期的结果进行比较)。

下图是一个标准的HDL验证的流程。

自从测试文件可以用VHDL或者Verilog编写以来,测试验证流程就可以在平台和供应商的工具交叉进行。

同时,由于VHDL和Verilog都是标准的公用的语言,所以用VHDL或者是Verilog描述的验证可以很简单的被再使用。

图1. HDL验证流程测试文件构成:测试文件可以采用VHDL或者Verilog语言编写。

由于测试文件只是用来仿真的,他们就不被用于综合的RTL语言子集的语法所约束。

相反,所有行为结构都可以被使用。

这样,测试文件可以被写的更通用,更易于维护。

所有的测试文件都包含以下基本内容,如表1。

如上所属,测试文件经常同时包含附加功能,如结果的可视化显示和内建错误检测。

Testbench文件编写纪要(Verilog)

Testbench⽂件编写纪要(Verilog)之前在使⽤Verilog做FPGA项⽬中、以及其他⼀些不同的场合下,零散的写过⼀些练⼿性质的testbench⽂件,开始⼏次写的时候,每次都会因为⼀些基本的东西没记住、写的很不熟练,后⾯写的时候稍微熟练了⼀点、但是整体编写下来⽐较零碎不成体系,所以在这⾥简要记录⼀下⼀般情况下、针对⼩型的verilog模块进⾏测试时所需要使⽤到的testbench⽂件的编写要点。

本⽂主要参考了在⽹上找到的Lattice公司的“A Verilog HDL Test Bench Primer”⼿册中的有关内容。

谢谢!模块实例化、reg&wire声明、initial和always块的使⽤需要测试的模块(Verilog-module)被称为DUT(Design Under Test),在testbench中需要对⼀个或者多个DUT进⾏实例化。

Testbench中的顶层module不需要定义输⼊和输出。

Testbench中连接到DUT instance的输⼊的为reg类型、连接到DUT instance的输出的为wire类型。

对于DUT的inout类型变量,在testbench中需要分别使⽤reg、wire类型的变量进⾏调⽤。

例如,对于下⾯这样⼀个待测试module:module bidir_infer (DATA, READ_WRITE);input READ_WRITE ;inout [1:0] DATA ;reg [1:0] LATCH_OUT ;always @ (READ_WRITE or DATA) beginif (READ_WRITE == 1)LATCH_OUT <= DATA;endassign DATA = (READ_WRITE == 1) ? 2'bZ : LATCH_OUT;endmodule为其设计的testbench⽂件可以是:module test_bidir_ver;reg read_writet;reg [1:0] data_in;wire [1:0] datat, data_out;bidir_infer uut (datat, read_writet);assign datat = (read_writet == 1) ? data_in : 2'bZ;assign data_out = (read_writet == 0) ? datat : 2'bZ;initial beginread_writet = 1;data_in = 11;#50 read_writet = 0;endendmodule和普通的Verilog模块中⼀样、使⽤assign对wire类型的变量进⾏赋值。

4.2.Verilog Testbench与仿真

Verilog Testbench与仿真Verilog Testbench与仿真为通过软件验证Verilog语言设计实例的逻辑功能,需要编写Testbench,也称为测试模块,并通过仿真软件ModelSim进行仿真。

1.Testbench功能产生模拟激励(波形);将产生的激励加入到被测试模块;将输出响应与期望进行比较。

2. Testbench结构Module test_bench信号或变量定义声明initial、always产生激励信号例化待测试模块监控和比较输出响应endmodule组合逻辑的仿真//待测模块2选1数据选择器module mux21 (a,b,s,y);input a,b;input s;output y;assign y =(s==0)?a :b;endmodule//测试模块(testbench )`timescale 1ns / 1ps module mux21_tp;reg a,b,s;wire y;mux21 u0(.a(a),.b(b),.s(s),.y(y));initial begina=0;b=0;s=0;#5 s=1;#5 a=1;s=0;#5 s=1;#5 a=0;b=1;s=0;#5 s=1;#5 a=1;b=1;s=0;#5 s=1;end initial$monitor ($time,,,"a=%b b=%b s=%b y=%b",a,b,s,y);endmoduleTestbench待测模块wire y reg a,b,soutput y wire a,b,s//测试模块(test bench )`timescale 1ns / 1ps module mux21_tp;reg a,b,s;wire y;mux21 u0(.a(a),.b(b),.s(s),.y(y));initial begina=0;b=0;s=0;组合逻辑的仿真(1)Verilog HDL 仿真编译指令“`”开头,编译指定某种操作` timescale [时间的基准单位]/[时间的精度]`timescale 10ns / 1ns 缺省:`timescale 1ns / 1ns时间单位符号有s 、ms 、ns 、ps 和fs ,分别表示为秒、10-3s 、10-6s 、10-9s 、10-12s 、10-15s 。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Verilog 仿真文件testbench编写样例
`timescale 1ns/100ps
module testbench;
localparam DATA_WIDTH = 32;
localparam CLK_100_PERIOD = 5;
localparam CLK_200_PERIOD = 2.5;
localparam SIM_TIME = 150000;
localparam ;
localparam ;
reg clk_100, clk_200;
wire clk;
assign clk = clk_100;
always
begin
clk_100 = 0;
forever #CLK_100_PERIOD clk_100 = ~clk_100;
end
always
begin
clk_200 = 0;
forever #CLK_200_PERIOD clk_200 = ~clk_200;
end
reg rstn;
integer fp_testin;
integer fp_matlab_out;
integer fp_sim_out;
integer fp_outdiff;
reg signed [DATA_WIDTH/2-1:0] matlab_in_re, matlab_in_im;
reg signed [DATA_WIDTH/2-1:0] matlab_out_re, matlab_out_im;
reg signed [DATA_WIDTH/2-1:0] matlab_diff_re, matlab_diff_im;
reg signed [DATA_WIDTH/2-1:0] matlab_diff_re2, matlab_diff_im2;
reg signed [DATA_WIDTH/2-1:0] max_diff_re, max_diff_im;
initial begin
max_diff_re = 0;
max_diff_im = 0;
rstn = 0;
#500
rstn = 1;
#SIM_TIME
sim_finish();
$stop();
end
task sim_finish;
begin
if(fp_testin!=0)
$fclose(fp_testin);
if(fp_matlab_out!=0)
$fclose(fp_matlab_out);
if(fp_sim_out)
$fclose(fp_sim_out);
if(fp_outdiff!=0)
$fclose(fp_outdiff);
end
endtask
initial
begin
fp_testin = 0;
fp_testin=
$fopen(&quot;txt_file/input_data.txt&quot;,&quot; r&quot;);
if(fp_testin==0)
begin
$display(&quot;input_data.txt open failed!&quot;); sim_finish();
$stop();
end
else begin
$fscanf(fp_testin,
&quot;%d, %d\n&quot;,matlab_in_re,matlab_in_im); end
fp_matlab_out = 0;
fp_matlab_out =
$fopen(&quot;txt_file/matlab_out.txt&quot;,&quot; r&quot;);
if(fp_matlab_out==0)
begin
$display(&quot;fp_matlab_out.txt open
failed!&quot;);
sim_finish();
$stop();
end
else begin
$fscanf(fp_matlab_out,&quot;%d, %d\n&quot;,matlab _out_re,matlab_out_im);
end
fp_sim_out = 0;
fp_sim_out =
$fopen(&quot;txt_file/modelsim_out.txt&quot;,&quo t;w&quot;);
if(fp_sim_out == 0)
begin
$display(&quot;modelsim_out_re.txt open
failed!&quot;);
sim_finish();
$stop();
end
fp_outdiff = 0;
fp_outdiff =
$fopen(&quot;text_file/outdiff.txt&quot;,&quot;w& quot;);
if(fp_outdiff==0)
begin
$display(&quot;outdiff.txt open failed!&quot;); sim_finish();
$stop();
end
end
always @(posedge clk)
begin
if(stest_wvalid && stest_wready) //ready to change
begin
if(~$feof(fp_testin))
$fscanf(fp_testin,
&quot;%d, %d\n&quot;,matlab_in_re,matlab_in_im); end
else
begin
matlab_in_re <= matlab_in_re;
matlab_in_im <= matlab_in_im;
end
end
always @(posedge clk_100)
begin
if(mfc_wready && mfc_wvalid)
begin
matlab_diff_re <= mfc_wdata_re - matlab_out_re; matlab_diff_im <= mfc_wdata_im - matlab_out_im; matlab_diff_re2 <= matlab_out_re - mfc_wdata_re ; matlab_diff_im2 <= matlab_out_im - mfc_wdata_im ; if(max_diff_re < matlab_diff_re)
begin
max_diff_re <= matlab_diff_re;
$display(&quot;max_diff_re:%d
max_diff_im:%d\n&quot;,max_diff_re,max_diff_im); end
else if(max_diff_re < matlab_diff_re2)
begin
max_diff_re <= matlab_diff_re2;
$display(&quot;max_diff_re:%d
max_diff_im:%d\n&quot;,max_diff_re,max_diff_im); end
if(max_diff_im < matlab_diff_im)
begin
max_diff_im <= matlab_diff_im;
$display(&quot;max_diff_re:%d
max_diff_im:%d\n&quot;,max_diff_re,max_diff_im); end
else if(max_diff_im < matlab_diff_im2)
begin
max_diff_im <= matlab_diff_im2;
$display(&quot;max_diff_re:%d
max_diff_im:%d\n&quot;,max_diff_re,max_diff_im); end
$fscanf(fp_matlab_out,&quot;%d, %d\n&quot;,matlab _out_re,matlab_out_im);
$fwrite(fp_sim_out, &quot;%d, %d\n&quot;,
mfc_wdata_re,mfc_wdata_im);
$fwrite(fp_outdiff,
&quot;%d, %d\n&quot;,matlab_diff_re,matlab_diff_i m);
end
end
endmodule。

相关文档
最新文档