Module 5: Configuration and Extension | Intermediate UVM

Callback Extension Patterns

Learn how UVM callbacks add optional, test-specific behavior without modifying the reusable component that owns the main protocol flow.

Learning objectives

  • Explain how UVM callbacks provide controlled extension points in reusable components.
  • Distinguish a callback from a factory override and a configuration setting.
  • Implement and register a callback that modifies or observes component behavior.
  • Verify that the callback executes only when its intended extension point is reached.

Step 1: Understand the concept

A reusable UVM component should own its normal protocol behavior, while a test may occasionally need to add specialized behavior. For example, a test might want to inject an error, observe a transaction before it is driven, delay an operation, or collect additional debug information.

Callbacks provide explicit extension points for these cases. The component defines when an extension can run, while the callback class defines what extra behavior should happen there.

Think of a callback as a controlled hook: the reusable component continues to own the normal behavior, and the callback supplies optional behavior at a predefined point.

Callback vs. other UVM mechanisms

  • Configuration: changes data or settings used by a component.
  • Factory override: replaces the type of an object or component created through the factory.
  • Callback: keeps the original component but allows additional behavior at a defined execution point.

This makes callbacks useful when you want to preserve the reusable component's implementation while allowing selected tests to customize a specific operation.

Step 2: Study the callback structure

A callback design normally has three pieces: a callback base class, a component that invokes the callback, and a concrete callback registered by the test.

class driver_callback extends uvm_callback;


`uvm_object_utils(driver_callback)

function new(string name = "driver_callback");
super.new(name);
endfunction

virtual function void before_send(packet item);
endfunction
endclass

class packet_driver extends uvm_driver #(packet);
`uvm_component_utils(packet_driver)
  `uvm_register_cb(packet_driver, driver_callback)

task run_phase(uvm_phase phase);
packet item;


forever begin
  seq_item_port.get_next_item(item);

  `uvm_do_callbacks(packet_driver,
                    driver_callback,
                    before_send(item))

  drive_packet(item);
  seq_item_port.item_done();
end


endtask
endclass

The important design decision is the location of the callback invocation. Here, the callback runs after the driver receives the transaction but before drive_packet() performs the normal DUT-facing operation.

Add a concrete callback

A test can derive from the callback base class and implement only the behavior it needs.

class error_inject_callback extends driver_callback;


`uvm_object_utils(error_inject_callback)

function new(string name = "error_inject_callback");
super.new(name);
endfunction

virtual function void before_send(packet item);
if (item.inject_error)
item.data = ~item.data;
endfunction
endclass

The driver does not need to know that this particular test is injecting an error. It simply exposes the callback point and continues executing its normal flow.

Design rule: keep the callback focused. It should add behavior at the extension point rather than silently replacing the component's entire responsibility.

Step 3: Register the callback from the test

Defining a callback does not automatically make it active. The test must create and register it with the component instance that owns the callback mechanism.

function void build_phase(uvm_phase phase);


error_inject_callback cb;

super.build_phase(phase);

cb = error_inject_callback::type_id::create("cb");
uvm_callbacks#(packet_driver, driver_callback)::add(env.agent.driver, cb);
endfunction

After registration, the execution path becomes:

  1. The sequence produces a packet.
  2. The driver receives the packet.
  3. The driver invokes its registered callbacks.
  4. The concrete callback optionally changes or observes the packet.
  5. The driver performs its normal drive_packet() operation.

This separation is valuable because the reusable driver remains unchanged while the test controls whether the extension is active.

Step 4: Build it in small pieces

  1. Identify the extension point. Decide exactly where additional behavior belongs: before driving, after driving, on reset, or at another well-defined event.
  2. Define the callback interface. Keep the callback method narrow and pass only the information the extension actually needs.
  3. Implement a no-op callback first. Confirm that registering it does not change normal DUT behavior.
  4. Add one controlled behavior. For example, modify a transaction only when an explicit error-injection field is enabled.
  5. Register the callback from the test. Do not hide registration inside the reusable component.
  6. Verify both paths. Run one test with the callback disabled or unregistered and another with it active.
Common mistake: using callbacks as a general-purpose mechanism for changing component behavior everywhere. If a behavior is required for every test, it probably belongs in the component itself. If the entire implementation should be replaced, consider a factory override instead.

Step 5: Debug callback execution

When a callback appears not to work, verify the chain rather than immediately changing the callback code.

  1. Confirm the callback type matches the component's registered callback type.
  2. Confirm the callback instance was created successfully.
  3. Confirm it was registered with the correct component instance.
  4. Confirm the component actually reaches the callback invocation point.
  5. Add a temporary `uvm_info message inside the callback.
  6. Compare a run with the callback registered against a run without it.
virtual function void before_send(packet item);


`uvm_info("CALLBACK",
$sformatf("Callback invoked for packet id=%0d", item.id),
UVM_MEDIUM)

if (item.inject_error)
item.data = ~item.data;
endfunction

A useful debug message should identify the callback, the relevant transaction, and the condition that caused the extension to act.

Step 6: Connect callbacks to verification intent

Callbacks are most effective when they support a specific verification requirement. Common uses include:

  • Error injection: corrupt a selected transaction to verify DUT error handling.
  • Protocol stress: introduce controlled delays or unusual transaction conditions.
  • Additional observation: collect information without changing the main driver implementation.
  • Test-specific behavior: customize one test without creating a new driver subclass.

Do not use a callback merely because it is available. First identify the behavior that needs to vary, then choose the least complicated UVM extension mechanism that expresses that requirement clearly.

Try it yourself

Extend the driver callback example with controlled error injection.

  1. Add an inject_error field to the packet.
  2. Create a callback that modifies the packet only when that field is set.
  3. Register the callback from a dedicated test.
  4. Run one normal transaction and one error-injection transaction.
  5. Add a `uvm_info message proving that the callback executed.
  6. Verify at the scoreboard that the injected transaction produces the expected DUT response.

Challenge: add a second callback that records the packet ID without modifying the transaction. Register both callbacks and observe their behavior.

Key takeaways

  • Callbacks provide explicit extension points without requiring changes to the reusable component.
  • The component owns the callback interface and execution point; the test owns whether a concrete callback is registered.
  • Use callbacks for focused, test-specific extensions such as error injection, stress behavior, or additional observation.
  • Choose configuration, factory overrides, or callbacks based on the kind of variation you need.
  • Always verify callback registration and execution with observable evidence such as reports, transaction changes, scoreboard results, or assertions.