绿皮书:接口部分代码总结

逻辑设计已经变得如此复杂,即便是块之间的通信也必须分割成独立的实体。接口可以看作一捆智能的连线。

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值