Modports in SV
Previously, we saw what interfaces are and why they are useful. Modports are a part of an interface that helps define the direction of signals. But we can also define the direction of signals within the interface, so what is the need for modports? In this article, we will see how modports help us organize signals in complex designs as well as testbenches.
What are modports?
Modports can be considered as a sub-entity of interface which defines a particular direction to the signals. In an interface, there can be many modports and all modports can define different directions to the signal.
Syntax
Need for modports
An interface is an entity that helps connect testbenches to designs or different modules of a design. For example, let's consider the simple scenario of connecting a testbench to a design. Any inputs in the design need to be driven by the testbench. So, the signals that are inputs for the design are outputs for the testbench. However, inside an interface, the same signal cannot take on two different directions. This is where modports become helpful. We can define two modports inside the interface. One will define the directions with respect to the design, and the other will define the directions with respect to the testbench.
In later articles, we will see that complex testbenches often have drivers and monitors. For a driver to drive signals, the direction would be output, but a monitor, as its name suggests, needs to monitor the signals and thus all the signals will be inputs in this case. This is another scenario where modports are helpful.
This is the main reason why we logic as a data type for signals inside. If we used reg or wire, then it would be difficult to make it behave as both input and output without worrying about the driver load issue.
Example
Output
# Sampled signals at t = 10
# rw = x, addr = x, data_in= x, data_out = x
# Sampled signals at t = 30
# rw = 1, addr = a, data_in= 2f, data_out = x
# Sampled signals at t = 50
# rw = 1, addr = 3, data_in= 11, data_out = x
# Sampled signals at t = 70
# rw = 0, addr = 3, data_in= 11, data_out = 11
# Sampled signals at t = 90
# rw = 0, addr = 3, data_in= 11, data_out = 11