SystemVerilog Testbench学习总结(Lab1)

1、ntb_template -t router router.v 执行该命令会生成3个文件(命令中router.v是dut)

  a、router.if.vrh,包含信号端口的方向(相对于dut)、位宽,可将此信号加上类型(logic或者bit),去掉方向,作为interface中的信号声明。将此信号去掉位宽来作为interface中clocking模块中同步信号的声明;
  b、router.test_top 此文件中会产生仿真时钟信号,接口例化等信号,仿真时钟可直接拷贝到Test Harness File使用,例化拷贝后稍加修改即可使用;

2、interface
  a、在interface中,信号方向的指定:
    同步信号:in a clocking block
    异步信号:in a modport

  b、一个interface应包括:(below example)

 1 interface router_io(input bit clock);//interface中有同步信号,引入时钟
 2 信号声明<类型(logic或bit) + 位宽 + 信号名>
 3 logic reset_n;
 4 logic [15:0] din;
 5 ...
 6 
 7 //同步时钟模块,执行同步信号的驱动和采样
 8 clocking cb @(posedge clock)
 9 default input #1ns output #1ns //去除输入输出的抖动
10 //信号方向(相对于dut) + 信号名
11 output reset_n;
12 output din;
13 ...
14 endclocking:cb
15 
16 //使用modport将interface和test program 连接起来,在modport参数列表中,应包含前面锁创建的同步信号和潜在的异步信号。
17 //异步信号方向指定
18 modport TB(clocking cb,output reset_n);//需要注意,reset_n既是同步信号,也是异步信号
19 endinterface:router_io

3、Test Program File 测试程序:

 1 program automatic test(router.TB rtr_io);//将test program 和interface相连
 2 
 3 initial begin
 4 $display("This My first SV testbench");
 5 reset();
 6 end
 7 
 8 task reset();
 9 rtr_io.reset_n = 1'b0;//异步信号,阻塞赋值
10 rtr_io.cb.frame_n <= 1'b1;//同步信号,非阻塞赋值
11 rtr_io.cb.valid_n <= 1'b1;//同上
12 //阻塞赋值和非阻塞赋值是同时执行的,最后来到##2延时2个时钟周期后拉高reset_n
13 
14 ##2 rtr_io.cb.reset_n <= 1'b1;//reset_n拉低2个时钟周期然后拉高
15 repeat(15) @(router.cb);
16 endtask:reset
17 
18 endprogram:test

4、Test Harness File

 1 `timescale 1ns/100ps
 2 module router_test_top;
 3 //时钟生成可使用ntb_template产生文件中的时钟
 4 parameter simulation_cycle = 100;
 5 bit SystemClock;
 6 
 7 router_io top_io(SystemClock);//例化一个接口
 8 test t(top_io);//将接口和tb连接起来
 9 router dut(//将接口和dut连接起来
10 .reset_n    (top_io.reset_n),
11 .clock    (top_io.clock),
12 .din    (top_io.din),
13 ...
14 .frameo_n    (top_io.frameo_n)
15 );
16 
17 initial begin
18 $timeformat(-9,1,"ns",10);    //set time
19 SystemClock = 0;
20 forever begin
21 #(simulation_cycle/2)
22 SystemClock = ~SystemClock;
23 end
24 end
25 
26 endmodule

 

5、compile and simulation

  vcs -sverilog -fsdb router_test_top.sv test.sv router_io.sv router.v    //执行词句会生成可执行文件simv

  ./simv    //执行词句可产生仿真结果,并且产生需要保存的波形文件(下面介绍);

6、waveform dump
  第一种波形文件:

$vcdpluson;//加入此句vcdplus.vpd文件

 

  第二种波形文件:

$fsdbDumpfile("test.fsdb");//被保存的波形文件名命名
$fsdbDumpvars(0,router_test_top);//router_test_top即代表你要保存的波形是哪个文件中的信号;

 


posted @ 2016-08-08 00:00  冷冷北极  阅读(6472)  评论(0编辑  收藏  举报