逻辑设计已经变得如此复杂,即便是块之间的通信也必须分割成独立的实体。接口可以看作一捆智能的连线。
1. 使用端口连接:
//*********************************使用端口连接***************************//
//仲裁器
module arb_with_port(
output logic [1:0] grant ,
input logic [1:0] request ,
input bit rst,clk );//端口
always @(posedge arb_if.clk or posedge arbif.rst)
begin
if(arbif.rst)
arb_if.grant <= '0;
else if (arbif.request[0])
arb_if.grant <= 2'b01;
else if (arbif.request[1])
arb_if.grant <= 2'b10;
else
arb_if.grant <= '0;
end
endmodule
//测试平台
module test_with_port(
input bit clk,
input logic [1:0] grant ,
input logic [1:0] request ,
input bit rst );//端口
initial begin
@(posedge arbif.clk); //表示每个clk上升沿做某事
arbif.request <= 2'b01;
$display("@%0t:Drove req = 01",$time);
repeat (2) @(posedge arbif.clk); //延迟2个时钟周期
if(arbif.grant != 2'b01)
$display("@%0t:Error:grant != 2'b01",$time);
$finish;
end
endmodule
//顶层
module top;
logic [1:0] grant,request;
bit clk ;
always #50 clk = ~clk;
arb_with_port a1(grant,request,rst,clk);//实例化模块,并连接接口
test_with_port b1(grant,request,rst,clk);
endmodule
2. 使用接口简化连接
//***************************使用接口简化连接****************************//
//接口
interface arb_if(input bit clk);
logic [1:0] grant,request;
bit rst ;
endinterface
//仲裁器
module arb_with_arbif(arb_if arbif);
always @(posedge arb_if.clk or posedge arbif.rst)
begin
if(arbif.rst)
arb_if.grant <= '0;
else if (arbif.request[0])
arb_if.grant <= 2'b01;
else if (arbif.request[1])
arb_if.grant <= 2'b10;
else
arb_if.grant <= '0;
end
endmodule
//测试平台
module test_with_ifc(arb_if arbif);
initial begin
@(posedge arbif.clk); //表示每个clk上升沿做某事
arbif.request <= 2'b01;
$display("@%0t:Drove req = 01",$time);
repeat (2) @(posedge arbif.clk); //延迟2个时钟周期
if(arbif.grant != 2'b01)
$display("@%0t:Error:grant != 2'b01",$time);
$finish;
end
endmodule
//顶层
module top;
bit clk;
always #50 clk = ~clk;
arb_if arbif(clk);//实例化模块,并连接接口
arb_with_arbif a1(arbif);
test_with_ifc b1(arbif);
endmodule : top
3. 使用modport将接口中的信号分组
上述例子在接口中使用了点对点的无信号方向的连接方式,下例在接口中使用modport结构将信号分组并指定方向。
//**********************使用modport将接口中的信号分组***********************//
//上述例子在接口中使用了点对点的无信号方向的连接方式
//下例在接口中使用modport结构将信号分组并指定方向
//接口
interface arb_if(input bit clk);
logic [1:0] grant,request;
bit rst ;
modport DUT (input clk,rst,request,
output grant ) ;
modport TEST (input clk,grant,
output rst,request ) ;
modport MONITOR (input clk,rst,request,grant);//MONITOR语句使测试平台能够把monitor模块连接到接口
endinterface
//仲裁器
module arb_with_arbif(arb_if arbif);
always @(posedge arb_if.clk or posedge arbif.rst)
begin
if(arbif.rst)
arb_if.grant <= '0;
else if (arbif.request[0])
arb_if.grant <= 2'b01;
else if (arbif.request[1])
arb_if.grant <= 2'b10;
else
arb_if.grant <= '0;
end
endmodule
//测试平台
module test_with_ifc(arb_if arbif);
initial begin
@(posedge arbif.clk); //表示每个clk上升沿做某事
arbif.request <= 2'b01;
$display("@%0t:Drove req = 01",$time);
repeat (2) @(posedge arbif.clk); //延迟2个时钟周期
if(arbif.grant != 2'b01)
$display("@%0t:Error:grant != 2'b01",$time);
$finish;
end
endmodule
//顶层
module top;
logic [1:0] grant,request;
bit clk;
always #50 clk = ~clk;
arb_if arbif(clk) ;//实例化模块,并连接接口
arb_with_mp a1(arbif.DUT) ;
test_with_mp b1(arbif.TEST);
endmodule : top