刚开始学习verilog时,我的testbench大概如下:
- // ------------------------------------------------------------
- // (C) 2010 Sivar http://www.cnblogs.com/Sivar/
- // Author : SivarH@gmail.com
- // Filename : code1.v
- // Created On : 2010-12-05 11:57
- // Description :
- //
- // ------------------------------------------------------------
- module test ;
- reg clk , rst ;
- reg [7:0] din ;
- reg en ;
- wire [7:0] dout ;
- initial
- begin
- clk=0;
- rst=0;
- en=0;
- din=8'b0;
- #10;
- rst=1;
- en=1;
- din=8'b1;
- #10 din=8'b2;
- .......
- end
- always
- begin
- #10 clk=~clk;
- end
-
- dut u1(clk,rst,en,din,dout);
- endmodule
复制代码
之后,知道一些coding style之类的,有一些改进:
- // ------------------------------------------------------------
- // (C) 2010 Sivar http://www.cnblogs.com/Sivar/
- // Author : SivarH@gmail.com
- // Filename : code2.v
- // Created On : 2010-12-05 12:02
- // Description :
- //
- // ------------------------------------------------------------
- `timescale 1ns/1ns
- module test ;
- reg clk , rst ;
- reg [7:0] din ;
- reg en ;
- wire [7:0] dout ;
- initial
- begin
- rst=0;
- en=0;
- din=8'b0;
- #10;
- rst=1;
- en=1;
- din=8'b1;
- #10 din=8'b2;
- .......
- end
- initial
- begin
- clk=0;
- forever #10 clk=~clk;
- end
-
- dut u1
- (.clk(clk),
- .rst(rst),
- .en(en),
- .din(din),
- .dout(dout)
- );
- endmodule
复制代码
再后来,学着将重复的操作封装起来:
- // ------------------------------------------------------------
- // (C) 2010 Sivar http://www.cnblogs.com/Sivar/
- // Author : SivarH@gmail.com
- // Filename : code3.v
- // Created On : 2010-12-05 12:05
- // Description :
- //
- // ------------------------------------------------------------
- `timescale 1ns/1ns
- module test ;
- reg clk , rst ;
- reg [7:0] din ;
- reg en ;
- wire [7:0] dout ;
- // sim
- initial
- begin
- reset ( 0 ) ;
- #20;
- reset ( 1 ) ;
- datain ( 1 ) ;
- datain ( 2 ) ;
- .......
- end
- // clk
- initial
- begin
- clk =0 ;
- forever #10 clk =~clk ;
- end
- // tasks
- task datain ;
- input [7:0] data ;
- @ ( posedge clk )
- din = data ;
- endtask
- task reset ;
- input rst_i ;
- rst = rst_i ;
- endtask
- dut u1
- (.clk(clk),
- .rst(rst),
- .en(en),
- .din(din),
- .dout(dout)
- );
- endmodule
复制代码 后来,学着划模块,clk_gen模块产生时钟,dut模块生成待测的DUT,log模块记录错误信息,test是测试平台顶层:
- // ------------------------------------------------------------
- // (C) 2010 Sivar http://www.cnblogs.com/Sivar/
- // Author : SivarH@gmail.com
- // Filename : test.v
- // Created On : 2010-12-05 12:18
- // Description :
- //
- // ------------------------------------------------------------
- `timescale 1ns/1ns
- module test ;
-
- initial
- begin
- ctrl.init ;
- log.init ;
- ctrl.reset ;
- repeat ( 10 )
- ctrl.datain ;
- end
- always @ ( ... )
- log.error("err @ %t" , $time ) ;
- ctrl ctrl ( ) ;
- log log ( ) ;
- clk_gen clkgen ( ... ) ;
- endmodule
复制代码
log模块如下:《设计与验证verilog HDL》
- // ------------------------------------------------------------
- // (C) 2010 Sivar http://www.cnblogs.com/Sivar/
- // Author : SivarH@gmail.com
- // Filename : log.v
- // Created On : 2010-12-05 12:25
- // Description :
- //
- // ------------------------------------------------------------
- `timescale 1ns/1ns
- module log ;
- integer cnt =
- 0 ;
- ...
- task error ;
- input [50*8:0] str ;
- begin
- $display ( "error : %s" , str ) ;
- $display ( "@time%0t" , $time ) ;
- cnt = cnt + 1 ;
- end
复制代码
之后会怎么样呢??
期待ing……
>>>> From Sivar's: http://www.cnblogs.com/Sivar Thanks for your reading.