interface一组信号的集合(net,wire,port),里面除了可以声明信号,还可以声明常量(constant)、变量(variable)、参数(parameter)、函数(function)、任务(task)以及过程代码块(initial和always) 和连续赋值语句(assign)。
其中interface中的函数和任务可以用来产生drive communication的进程
过程代码块和连续赋值语句可以编写protocal checker、function coverage、asserction等
interface的完整定义语法可以参考systemverilog(1800-2012).pdf Syntax25-1
interface myinterface;
...
endinterface[:myinterface]
interface 可以像module一样例化:例如
myinterface #(100) scalar1(), vector[9:0]();
在这个例子里面,有11个类型为myinterface的接口实例被例化了并且每个接口实例的第一个参数都被修改为100,这11个接口实例分别为scalar1和一个包含10个接口实例的数组vector[9:0]。
接口可以单独声明,也可以在module里面声明和实例化,但是module不能在interface里面声明和实例化。
25.3.1 不使用interface的例子
module memMod(input logic req,
logic clk,
logic start,
logic [1:0] mode,
logic [7:0] addr,
inout wire[7:0] data,
output bit gnt,
bit rdy);
logic avail;
...
endmodule
module cpuMod(input logic clk,
logic gnt,
logic rdy,
inout wire[7:0] data,
output logic req,
logic start,
logic [7:0] addr,
logic [1:0] mode);
...
endmodule
module top;
logic req,gnt,start,rdy;
logic clk=0;
logic [1:0] mode;
logic [7:0] addr;
logic [7:0] data;
memMod mem(req,clk,start,mode,addr,data,gnt,rdy);
cpuMod cpu(clk,gnt,rdy,data,req,start,addr,mode);
endmodule
25.3.2 使用interface的例子
The simplest form of a SystemVerilog interface is a bundled collection of variables or nets. When an
interface is referenced as a port, the variables and nets in it are assumed to have ref and inout access,
respectively. The following interface example shows the basic syntax for defining, instanti
最简单的接口是线网的集合,当接口被用作模块的端口的时候,其中的线网被解读为ref和inout类型。
interface simple_bus;
logic req,gnt;
logic [7:0] addr,data;
logic [1:0] mode;
logic start,rdy;
endinterface:simple_bus
module memMod(simple_bus a,//access the simple_bus interface
input logic clk);
logic avail;
//when memMode is instantiated in module top,a.req is the req signal in the sb_intf
//instance of the 'simple_bus' interface
always @(posedge clk) a.gnt <= a.req & avail;
endmodule
module cpuMod(simple_bus b,input logic clk);
...
endmodule
module top;
logic clk=0;
simple_bus sb_intf();//instantiate the interface
memMod mem(sb_intf,clk);//connect the interface to the module instance
cpuMod cpu(.b(sb_intf),.clk(clk));//either by position or by name
endmodule
下面的例子中显示,如果在memMode和cpuMod的模块头里面,使用相同的接口实例名(sb_intf)例化simple_bus的时候,那么可以在top模块中采用隐式的端口连接例化memMod和cpuMode。如下所示:
module memMod(simple_bus sb_intf,input logic clk);
...
endmodule
module cpuMod(simple_bus sb_intf,input logic clk);
...
endmodule
module top;
logic clk=0;
simple_bus sb_intf();//instantiate the interface
memMod mem(.*);//implicit port connections
cpuMod cpu(.*);//implicit port connections
endmodule
25.3.3 通用接口
模块定义的头部声明可以使用未定义的接口作为占位符,并在模块被例化的时候选择。这里把未定义的接口称为通用接口。
通用接口引用只能使用ANSI 风格声明(参见23.2.2.2),不能使用non-ANSI风格声明(参见23.2.2.1)
在模块定义中指定通用接口引用的示例如下:
//memMod and cpuMod can use any interface
module memMod(interface a,input logic clk);
...
endmodule
module cpuMod(interface b,input logic clk);
...
endmodule
interface simple_bus;//define the interface
logic req,gnt;
logic [7:0] addr,data;
logic [1:0] mode;
logic start,rdy;
endinterface:simple_bus
module top;
logic clk = 0;
simple_bus sb_intf();//instantiate the interface
//Reference the sb_intf instance of the simple_bus
//interface from the generic interfaces of the memMod and cpuMod modules
memMod mem(.a(sb_intf),.clk(clk));
cpuMod cpu(.a(sb_intf),.clk(clk));
endmodule
通用接口只能按名称显式引用,不能使用隐式引用。如下例所示:
//memMod and cpuMod can use any interface
module memMod(interface a,input logic clk);
...
endmodule
module cpuMod(interface b,input logic clk);
...
endmodule
interface simple_bus;//define the interface
logic req,gnt;
logic [7:0] addr,data;
logic [1:0] mode;
logic start,rdy;
endinterface:simple_bus
module top;
logic clk = 0;
simple_bus sb_intf();//instantiate the interface
memMod mem(.*,.a(sb_intf));//partial implicit port connections
cpuMod cpu(.*,.a(sb_intf));//partial implicit port connections
endmodule
25.4 接口里面的端口
简单接口的一个局限在于其中的线网只被用于连接具有相同线网的端口。如果要共享外部线网或者变量,那么接口端口声明是必须的。位于端口列表里面的线网和位于接口里面的线网的区别是只有端口列表里面的线网能够通过名称或位置关联的方式与外部进行连接,接口端口声明的方式和模块一样。
interface I1 (input a,output b, inout c);
wire d;
endinterface
这里,线网a,b和c可以单独地连接到接口上,并与其它的接口共享。
下面的例子显示如何指定一个有 input端口的接口,以及在两个接口实例之间共享同一根线网。
interface simple_bus(input logic clk);//define the interface with input port
logic req,gnt;
logic [7:0] addr,data;
logic [1:0] mode;
logic start,rdy;
endinterface:simple_bus
module memMod (simple_bus a);//uses just the interface
logic avail;
always @(posedge a.clk)//the clk signal from the interface
a.gnt <= a.req & avail;//a.req is in the 'simple_bus' interface
endmodule
module cpuMod(simple_bus b);
...
endmodule
module top;
logic clk = 0;
simple_bus sb_intf1(clk);//instantiate the interface
simple_bus sb_intf2(clk);//instantiate the interface
memMod mem1(.a(sb_intf1));//reference simple_bus 1 to memory 1
cpuMod cpu1(.b(sb_intf1));
memMod mem2(.a(sb_intf2));//reference simple_bus 2 to memory 2
cpuMod cpu2(.b(sb_intf2));
endmodule
25.5 Modports