[SV]SystemVerilog Modport

                                              Modport

       SystemVerilog Modport. The Modport groups and specifies the port directions to the wires/signals declared within the interface. modports are declared inside the interface with the keyword modport.

  • By specifying the port directions, modport provides access restrictions
  • The keyword modport indicates that the directions are declared as if inside the module
  • Modport wire declared with input is not allowed to drive or assign, any attempt to drive leads to a compilation error
  • The Interface can have any number of modports, the wire declared in the interface can be grouped in many modports
  • Modpports can have, input, inout, output, and ref

 

一、Declaring modport

       Below code shows the declaration of modport inside the interface. It has two modports with the name driver and monitor.

interface my_intf;
  logic a;
  logic b;
   
  //modport delcaration
  modport driver  (input a, output b);
  modport monitor (input a, input  b);
 
endinterface

(1)driver modport

       It groups the signals a and b with the access direction input and output respectively.

(2)monitor modport

       It groups the signals a and b with the access restricted to input for both the signals. As the monitor need only monitoring the signals, driving access is restricted by specifying direction as input.

二、Accessing Modport

       wires declared in the modport are accessed as,

interface_name.modport_name.wire;

三、Modport example

(1)Use of modport

       This example is a continuation of a virtual interface example.In the below example, driver modport is defined with a, b as outputs and c as output.In the env file signals are accessed using interface.modport.<signal>.

//Defining modport in interface
interface intf();
   
  //declaring the signals
  logic [3:0] a;
  logic [3:0] b;
  logic [6:0] c;
   
  modport driver (output a, b, input c);
   
endinterface
 
//driving using modort
//run task
task run;
  vif.driver.a = 6;
  vif.driver.b = 4;
   
  $display("Value of a = %0d, b = %0d",vif.driver.a,vif.driver.b);
  #5;
  $display("Sum of a and b, c = %0d",vif.driver.c);
  $finish;
endtask

Simulator Output 

Value of a = 6, b = 4
Sum of a and b, c = 10

(2)Driving modport signal declared with input

       In the below example, the modport signals defined with the direction as input are driven in the run task, which leads to a compilation error.

//Defining modport in interface with the direction inut
interface intf();
   
  //declaring the signals
  logic [3:0] a;
  logic [3:0] b;
  logic [6:0] c;
   
  modport driver (input a, b, c);
   
endinterface
 
//driving using modort
//run task
task run;
  vif.driver.a = 6;
  vif.driver.b = 4;
   
  $display("Value of a = %0d, b = %0d",vif.driver.a,vif.driver.b);
  #5;
  $display("Sum of a and b, c = %0d",vif.driver.c);
  $finish;
endtask

Simulator Output 

Error-[MPCBD] Modport port cannot be driven
environment.sv, 18
$unit, “vif.driver.a”
Port ‘a’ of modport ‘driver’ has been restricted as an input port. Input
ports cannot be driven.

Error-[MPCBD] Modport port cannot be driven
environment.sv, 19
$unit, “vif.driver.b”
Port ‘b’ of modport ‘driver’ has been restricted as an input port. Input
ports cannot be driven.

发布了145 篇原创文章 · 获赞 81 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105125218
今日推荐