SystemVerilog之interface
interface一組信號的集合(net,wire,port),里面除了可以聲明信號,還可以聲明常量(constant)、變量(variable)、參數(shù)(parameter)、函數(shù)(function)、任務(wù)(task)以及過程代碼塊(initial和always) 和連續(xù)賦值語句(assign)。
其中interface中的函數(shù)和任務(wù)可以用來產(chǎn)生drive communication的進程
過程代碼塊和連續(xù)賦值語句可以編寫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的接口實例被例化了并且每個接口實例的第一個參數(shù)都被修改為100,這11個接口實例分別為scalar1和一個包含10個接口實例的數(shù)組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); endmodule25.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
最簡單的接口是線網(wǎng)的集合,當接口被用作模塊的端口的時候,其中的線網(wǎng)被解讀為ref和inout類型。
下面的例子中顯示,如果在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 interfacememMod mem(.*);//implicit port connectionscpuMod 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 interfacelogic 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 interfacelogic 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 interfacememMod mem(.*,.a(sb_intf));//partial implicit port connectionscpuMod cpu(.*,.a(sb_intf));//partial implicit port connections endmodule?25.4? 接口里面的端口
簡單接口的一個局限在于其中的線網(wǎng)只被用于連接具有相同線網(wǎng)的端口。如果要共享外部線網(wǎng)或者變量,那么接口端口聲明是必須的。位于端口列表里面的線網(wǎng)和位于接口里面的線網(wǎng)的區(qū)別是只有端口列表里面的線網(wǎng)能夠通過名稱或位置關(guān)聯(lián)的方式與外部進行連接,接口端口聲明的方式和模塊一樣。
interface I1 (input a,output b, inout c);wire d; endinterface這里,線網(wǎng)a,b和c可以單獨地連接到接口上,并與其它的接口共享。
下面的例子顯示如何指定一個有 input端口的接口,以及在兩個接口實例之間共享同一根線網(wǎng)。?
interface simple_bus(input logic clk);//define the interface with input portlogic 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 interfacelogic avail;always @(posedge a.clk)//the clk signal from the interfacea.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 interfacesimple_bus sb_intf2(clk);//instantiate the interfacememMod mem1(.a(sb_intf1));//reference simple_bus 1 to memory 1cpuMod cpu1(.b(sb_intf1));memMod mem2(.a(sb_intf2));//reference simple_bus 2 to memory 2cpuMod cpu2(.b(sb_intf2)); endmodule25.5 Modports
總結(jié)
以上是生活随笔為你收集整理的SystemVerilog之interface的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件耦合的分类及解决方法
- 下一篇: 2017-2018-2 20179306