interface 和 module是一样的, 都是静态的变量, 也就是在程序开始时, 内存中就有了其实例.
interface Rx_if (input logic clk);
logic [7:0] data;
logic soc, en, clav, rclk;
clocking cb @(posedge clk);
output data, soc, clav;
input en;
endclocking : cb
modport DUT (output en, rclk,
input data, soc, clav);
modport TB (clocking cb);
endinterface : Rx_if
...
...
program automatic test(Rx_if.TB Rx[4],
Tx_if.TB Tx[4],
output logic rst);
........
Driver drv[4]; //实例化了4个 Driver 对象,每个 Driver对象带有1个实例化的虚接口
.........
initial begin
virtual Rx_if.TB vRx_t=Rx;
//创建一组虚接口,由于这里定义了virtual,所以实例化的时候可以有Rx[].
for (int i=0; i<4; i++) begin
drv[i] = new(...., vRx[i]);
end
rst <= 1;
repeat (10) @Rx[0].cb;
rst <= 0;
for (int i=0; i<4; i++) begin
drv[i].run(5, driver_done); //发送
.......
end
..........
endprogram : test
最后在顶层:
module top;
logic clk, rst;
Rx_if Rx[4] (clk);
,,,,
atm_router a1 (Rx[0], Rx[1], Rx[2], Rx[3], Tx[0], Tx[1], Tx[2], Tx[3], clk, rst);
test t1 (Rx, Tx, rst);
initial begin
clk = 0;
forever #20 clk = !clk;
end
endmodule : top
定义一个interface,且实例化多个后,如果没有定义virtual,则在任何一个实例中修改了某个信号值,在其他实例中都会受到影响。如果定义了virtual,则每个实例独立。如果该interface只有一个实例,可用可不用virtual,有多个实例,需要virtual。
再举个例子:8位计数器
`timescale 1ns/1ns
interface X_if (input logic clk);
logic [7:0] din, dout;
logic reset_l, load;
clocking cb @(posedge clk);
output din, load;
input dout;
endclocking
always @cb //接口里面也可以带子程序,断言,initial,always块等代码。
$strobe("@ : %m: dout= , din= , load= , reset= ",
$time, dout, din, load, reset_l);
modport DUT (input clk, din, reset_l, load,
output dout);
modport TB (clocking cb, output reset_l);
endinterface
// Simple 8-bit counter with load and active-low reset
`timescale 1ns/1ns
module DUT(X_if.DUT xi);
logic [7:0] count;
assign xi.dout = count; //们想要输出的结果就是计数器
always @(posedge xi.clk or negedge xi.reset_l)
begin
if (!xi.reset_l) count = 0;
else if (xi.load) count = xi.din;
else count++;
end
endmodule
////////////////////////////////
`timescale 1ns/1ns
program automatic test();
parameter NUM_XI = 2; // Number of interface instances
typedef virtual X_if.TB vXi_t;
vXi_t vxi[NUM_XI]; //虚接口数组
class Driver; //在测试程序中定义类
vXi_t xi;
int id;
function new(vXi_t xi, int id);
this.xi = xi;
this.id = id;
endfunction
task reset;
fork
begin
$display("@ : %m: Start reset [ ]", $time, id);
// Reset the device
xi.reset_l <= 1;
xi.cb.load <= 0;
xi.cb.din <= 0;
@(xi.cb)
xi.reset_l <= 0;
@(xi.cb)
xi.reset_l <= 1;
$display("@ : %m: End reset [ ]", $time, id);
end
join_none
endtask
task load;
fork
begin
$display("@ : %m: Start load [ ]", $time, id);
xi.cb.load <= 1;
xi.cb.din <= id + 10;
xi.cb.load <= 0;
repeat (5) @(xi.cb);
$display("@ : %m: End load [ ]", $time, id);
end
join_none
endtask
endclass
Driver driver[];
initial begin
// Connect the local virtual interfaces to the top
$display("Test.v: There are NUM_XI = interfaces", NUM_XI);
if (NUM_XI <= 0) $finish;
driver = new[NUM_XI]; //创建driver, 每个DUT 要对应一个driver
vxi = top.xi;
//XMR跨模块连接。这种是推荐做法,就不用带参数了program automatic test(X_if xi[NUM_XI]); 了。
//注意这里其实是把top模块中生成的xi[]数组的句柄传过来的
for (int i=0; i《NUM_XI; i++)
begin
driver[i] = new(vxi[i], i);
driver[i].reset;
end
foreach (driver[i])
driver[i].load;
repeat (10) @(vxi[0].cb);
$display("@ : Test completed", $time);
$finish;
end
endprogram
////////////////////////////////////////////////////////
`timescale 1ns/1ns
parameter NUM_XI = 2; // Number of interface instances
module top;
// Clock generator
bit clk;
initial forever #20 clk = !clk;
X_if xi [NUM_XI] (clk); // Instantiate N Xi interfaces
// Generate N DUT instances
generate
for (genvar i=0; i《NUM_XI; i++)
begin : dut
DUT d (xi[i]);
end
endgenerate
// Instantiate the testbench, overriding the parameter with number of instances
test tb();
endmodule : top