SystemVerilog学习(2)- interface搭建测试平台

SystemVerilog使用interface搭建测试平台

接口

SystemVerilog使用接口为块之间的通信建模,接口可以看做一捆智能的连线,接口包含了连接、同步两个甚至多个块之间的通信功能,他们连接了设计块和测试平台。

时钟、复位可以是接口声明中的一部分,也可以是一个独立的内部端口。

使用接口的优势:
(1)接口便于设计重用:当设计中有多组相同通信协议的总线连接时,应当考虑使用接口,如多组AXI4总线、AXIS总线;
(2)要增加一个新的信号时,只需要在接口中声明一次,不需要在其他模块中声明。

程序实例

以下程序演示了interface和task的使用方法,将接口定义在了tb中,实际项目中可以将接口的定义放在一个单独的 .sv文件。

adder.v

`timescale 1ns / 1ps

// Company: 
// Engineer: 
// 
// Create Date: 11/27/2020
// Author Name: Sniper
// Module Name: adder
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 


module adder(
    input clk,
    input rst_n,
    input [7:0] a,
    input [7:0] b,
    input write_en,
    output reg [8:0] p,
    output reg out_en
);

always@(posedge clk or negedge rst_n)
begin
	if(!rst_n)
    begin
		p <= 0;
        out_en <= 0;
    end
	else
    begin
        if(write_en)
        begin
            p <= a + b;
            out_en <= 1;
        end
        else
            out_en <= 0;
    end
end

endmodule

tb_adder.sv

`timescale 1ns / 1ps

// Company:
// Engineer:
//
// Create Date: 11/27/2020
// Author Name: Sniper
// Module Name: tb_adder
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//


interface adderInterface(input bit clk, input bit rst_n);
    //write in
    logic [7:0] a;
    logic [7:0] b;
    logic write_en;
    //output
    logic [8:0] p;
    logic out_en;

    //task
    task init;
    begin
        a = 0;
        b = 0;
        write_en = 0;
    end
    endtask

    task write(input [7:0] data_a, input [7:0] data_b);
    begin
        @(posedge clk);
        write_en <= 0;
        a <= data_a;
        b <= data_b;

        @(posedge clk);
        write_en <= 1;

        @(posedge clk);
        write_en <= 0;
    end
    endtask
    
    task catch;
    begin
        @(posedge clk);
        if(out_en)
        begin
            $display("Catch one output: %0d ", p);
        end
    end
    endtask
endinterface



module tb_adder;

//system signals
reg clk;
reg rst_n;

//interface
adderInterface adder_if(clk, rst_n);


initial
begin
    clk = 0;
    rst_n = 0;

    adder_if.init;

    repeat(10) @(posedge clk);
    rst_n <= 1;

    for(int i=0;i<8;i++)
        adder_if.write(2,i);

end

initial
begin
    forever adder_if.catch;
end


//clock
always #5 clk = ~clk;


//DUT
adder DUT
(
    .clk(clk),
    .rst_n(rst_n),
    .a(adder_if.a),
    .b(adder_if.b),
    .write_en(adder_if.write_en),
    .p(adder_if.p),
    .out_en(adder_if.out_en)
);

initial
begin
  $dumpfile("curve.vcd");
  $dumpvars(0,DUT);
end

initial #1000 $finish;

endmodule

运行结果

[IC@IC sim]$ vcs -R -sverilog ../rtl/adder.v ../bench/tb_adder.sv -l run.log

...

Catch one output: 2 
Catch one output: 3 
Catch one output: 4 
Catch one output: 5 
Catch one output: 6 
Catch one output: 7 
Catch one output: 8 
Catch one output: 9 
$finish called from file "../bench/tb_adder.sv", line 123.
$finish at simulation time              1000000
           V C S   S i m u l a t i o n   R e p o r t 
Time: 1000000 ps
CPU Time:      0.140 seconds;       Data structure size:   0.0Mb
...

[IC@IC sim]$ 

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/meng1506789/article/details/110223748
今日推荐