Interfaces and Clocking Blocks
Learn how SystemVerilog groups DUT signals into reusable interfaces and how clocking blocks make testbench timing explicit and predictable.
Learning objectives
- Explain why SystemVerilog interfaces are useful in verification.
- Create an interface that groups related DUT signals.
- Understand how modports control access to interface signals.
- Explain the purpose of a clocking block.
- Use clocking block input and output directions correctly.
- Understand clocking block skew and why sampling and driving timing matter.
- Recognize how interfaces and clocking blocks connect to future UVM drivers and monitors.
Step 1: Why verification needs interfaces
A DUT rarely has only one signal. A protocol may contain a clock, reset, address, data, control signals, valid/ready handshakes, status signals, and many other signals.
Without an interface, a testbench component would need to receive many individual signal connections.
input logic clk,
input logic reset_n,
output logic valid,
output logic [7:0] addr,
output logic [31:0] data,
input logic ready
As the protocol grows, this becomes difficult to maintain.
A SystemVerilog interface allows related signals to be grouped into one reusable object-like connection.
Step 2: Create a simple interface
Consider a simple bus with a clock, reset, address, data, and read/write control.
interface bus_if(input logic clk);
logic reset_n;
logic valid;
logic write;
logic [7:0] addr;
logic [31:0] data;
logic ready;
endinterface
The interface groups all of the bus signals together.
The clock is passed into the interface through the interface declaration:
interface bus_if(input logic clk);
The interface can now be instantiated in the testbench.
Step 3: Connect the interface to the DUT
The interface instance can be connected to the DUT signals.
module tb;
logic clk;
bus_if bus(clk);
my_dut dut (
.clk (clk),
.reset_n (bus.reset_n),
.valid (bus.valid),
.write (bus.write),
.addr (bus.addr),
.data (bus.data),
.ready (bus.ready)
);
endmodule
The important point is that the testbench now has one object,
bus, containing the protocol signals.
Instead of passing every signal independently to a verification component, the interface can later be passed as a single connection.
Step 4: Why interfaces improve testbenches
Interfaces provide several important benefits:
- Grouping: Related signals are kept together.
- Reusability: The same interface can be used by different testbenches.
- Maintainability: Protocol changes can be handled in one place.
- Cleaner connections: Components can work with a protocol interface instead of dozens of individual signals.
- Timing organization: Clocking blocks can be placed inside the interface.
Step 5: What is a modport?
An interface contains signals, but different users of the interface may need different access directions.
For example, a driver may drive addr, data,
and valid, while a monitor only needs to observe them.
A modport defines how a particular user is allowed to access the interface.
interface bus_if(input logic clk);
logic reset_n;
logic valid;
logic write;
logic [7:0] addr;
logic [31:0] data;
logic ready;
modport DUT (
input clk,
input reset_n,
input valid,
input write,
input addr,
input data,
output ready
);
modport DRIVER (
input clk,
input reset_n,
input ready,
output valid,
output write,
output addr,
output data
);
modport MONITOR (
input clk,
input reset_n,
input valid,
input write,
input addr,
input data,
input ready
);
endinterface
The same interface can therefore provide different views for different users.
Step 6: Why modports matter in verification
Think about three different participants:
- The DUT consumes and produces protocol signals.
- The driver generates stimulus.
- The monitor observes what happened.
These participants should not necessarily have identical access to every signal.
driver -> bus_if.DRIVER
monitor -> bus_if.MONITOR
DUT -> bus_if.DUT
Step 7: The timing problem
Grouping signals solves one problem, but verification has another important problem: timing.
Suppose the DUT samples valid, addr, and
data on the rising edge of clk.
always_ff @(posedge clk) begin
if (valid && ready) begin
// DUT samples transaction
end
end
When should the testbench drive those signals?
When should the monitor sample them?
If the testbench and DUT access the same signals at nearly the same simulation time, race conditions can occur.
Step 8: What is a clocking block?
A clocking block defines when a testbench samples and drives signals relative to a clock.
This allows the testbench to describe its timing relationship with the DUT explicitly.
clocking cb @(posedge clk);
input ready;
input reset_n;
output valid;
output write;
output addr;
output data;
endclocking
The clocking block is synchronized to the rising edge of
clk.
The signals are then accessed through the clocking block rather than directly through the interface when using clocking-block semantics.
Step 9: Clocking block inputs and outputs
The direction inside a clocking block describes the relationship between the testbench and the clocking block.
clocking cb @(posedge clk);
input ready;
input reset_n;
output valid;
output addr;
output data;
endclocking
In this example:
-
inputsignals are sampled by the testbench through the clocking block. -
outputsignals are driven by the testbench through the clocking block.
Step 10: Clocking block skew
A clocking block can specify when signals are sampled or driven relative to the clock event.
clocking cb @(posedge clk);
input #1step ready;
output #1step valid;
endclocking
The skew controls the timing relationship between the clock event and the signal access performed through the clocking block.
You do not need to memorize every skew rule yet. The important concept is:
Step 11: Use a clocking block in a driver-style task
Consider a simple driver operation.
task drive_transfer(
bit write,
bit [7:0] addr,
bit [31:0] data
);
@(bus.cb);
bus.cb.valid <= 1;
bus.cb.write <= write;
bus.cb.addr <= addr;
bus.cb.data <= data;
endtask
The driver waits for the clocking event and then drives the signals through the clocking block.
This is much clearer than scattering arbitrary delays throughout the testbench.
Step 12: Use a clocking block in a monitor
A monitor can also use the clocking block to sample signals.
task monitor_transfer();
@(bus.cb);
if (bus.cb.valid && bus.cb.ready) begin
$display(
"Observed addr=%0h data=%0h",
bus.cb.addr,
bus.cb.data
);
end
endtask
The monitor waits for the clocking event and samples the protocol through the clocking block.
This creates a clean separation:
- Interface: defines the protocol signals.
- Clocking block: defines the testbench timing view.
- Driver: produces stimulus.
- Monitor: observes protocol activity.
Step 13: Put the clocking block inside the interface
A common verification pattern is to keep the clocking block inside the interface itself.
interface bus_if(input logic clk);
logic reset_n;
logic valid;
logic write;
logic [7:0] addr;
logic [31:0] data;
logic ready;
clocking cb @(posedge clk);
input reset_n;
input ready;
output valid;
output write;
output addr;
output data;
endclocking
endinterface
Now the interface contains both:
- The protocol signals.
- The timing definition used by the testbench.
This is a powerful abstraction for verification environments.
Step 14: Interface vs. clocking block
These two concepts are related, but they solve different problems.
| Concept | Main purpose |
|---|---|
| Interface | Groups related DUT and testbench signals. |
| Modport | Defines a particular user's access directions. |
| Clocking block | Defines testbench sampling and driving timing. |
| Clock | Provides the timing reference for synchronous behavior. |
Interface = what signals exist
Modport = who can access them
Clocking block = when the testbench interacts with them
Step 15: A complete small example
Put the concepts together in a small bus interface.
interface bus_if(input logic clk);
logic reset_n;
logic valid;
logic write;
logic [7:0] addr;
logic [31:0] data;
logic ready;
clocking cb @(posedge clk);
input #1step reset_n;
input #1step ready;
output #1step valid;
output #1step write;
output #1step addr;
output #1step data;
endclocking
modport DRIVER (
clocking cb
);
modport MONITOR (
clocking cb
);
endinterface
The driver and monitor now have a clearly defined timing-oriented view of the same interface.
This pattern is particularly useful when building reusable verification components.
Step 16: How this connects to UVM
You have not learned UVM drivers and monitors yet, but you now have the SystemVerilog foundation needed to understand them.
Later in the course, a UVM driver will receive a transaction object and convert it into signal-level activity.
sequence item
|
v
UVM driver
|
v
virtual interface
|
v
clocking block
|
v
DUT signals
A monitor works in the opposite direction:
DUT signals
|
v
clocking block
|
v
UVM monitor
|
v
transaction object
Step 17: Common mistakes
Mistake 1: Treating an interface as a UVM component
An interface is a SystemVerilog construct. It is not a
uvm_component and does not participate in UVM's
component hierarchy.
Mistake 2: Confusing interface directions with clocking directions
A module port might say input or output
from the DUT's perspective. A clocking block describes the
testbench's interaction with those signals.
Mistake 3: Ignoring timing
A monitor that samples a signal at the wrong time can observe incorrect or unstable values even when the DUT itself is correct.
Mistake 4: Using arbitrary delays everywhere
Code such as #5 may appear to work, but it often hides
the real protocol timing. Prefer synchronization to meaningful
events such as clock edges and protocol handshakes.
Mistake 5: Making the interface too generic
An interface should represent a meaningful protocol or connection. Avoid creating one giant interface containing unrelated signals from the entire DUT.
Step 18: Debugging timing problems
When a verification environment produces unexpected results, inspect the waveform before changing the driver or monitor.
Ask these questions:
- At which clock edge should the DUT sample the transaction?
- When does the driver change the signals?
- When does the monitor sample the signals?
- Are the clocking block directions correct?
- Is the input/output skew appropriate?
- Is the monitor observing the same transaction that the DUT received?
Try it yourself
Build a simple SystemVerilog interface for a valid/ready protocol.
- Create signals for
clk,reset_n,valid,ready, anddata. - Create a clocking block synchronized to
posedge clk. - Declare
validanddataas clocking-block outputs. - Declare
readyandreset_nas clocking-block inputs. - Create a driver-style task that waits for the clocking event and drives a data value.
- Create a monitor-style task that waits for the clocking event and prints the data when
valid && readyis true. - Run the testbench and inspect the waveform.
Bonus: Add DRIVER and MONITOR modports and explain what access each modport provides.
Quick check
- What problem does a SystemVerilog interface solve?
- What is the purpose of a modport?
- What problem does a clocking block solve?
- What does
@(posedge clk)synchronize to? - What is the difference between an interface and a clocking block?
- Why can a testbench have a race condition even when its logic is correct?
- In a clocking block, what does an
inputsignal represent from the testbench perspective? - Why are clocking blocks useful for monitors and drivers?
Show answers
- It groups related signals into a reusable protocol connection.
- It defines a particular user's access directions for an interface.
- It defines when the testbench samples and drives signals relative to a clock.
-
The rising edge of
clk. - The interface groups and provides the signals; the clocking block defines the testbench's timing relationship with those signals.
- The testbench and DUT may access signals at conflicting simulation times or scheduling regions.
- It is a signal that the testbench samples through the clocking block.
- They provide a consistent and explicit timing model for driving and sampling protocol signals.
Key takeaways
- An interface groups related DUT and testbench signals into a reusable connection.
- A modport provides a specific access view of an interface.
- A clocking block defines how the testbench samples and drives signals relative to a clock.
- Correct timing is essential for reliable verification and avoiding race conditions.
- Drivers typically drive DUT signals, while monitors observe them.
- Interfaces and clocking blocks are SystemVerilog constructs; they are not UVM components.
- Understanding interfaces and timing is essential before learning UVM drivers, monitors, and virtual interfaces.