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

driver modport

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

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

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

Click to execute on   

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.

Click to execute on   

❮ Previous Next ❯