Module 5: Configuration and Extension | Intermediate UVM

UVM Callbacks

Learn how callbacks add controlled, reusable extension points to UVM components without modifying the component's core behavior.

Learning objectives

  • Explain why UVM callbacks are useful for extending reusable verification components.
  • Distinguish callback hooks from factory overrides and configuration settings.
  • Implement and register a callback for a driver or other UVM component.
  • Validate that the callback executes at the intended point in the component lifecycle.

Step 1: Understand the concept

A reusable UVM component should define its normal behavior without knowing every special action that a particular test may need. Callbacks provide controlled extension points where additional test-specific behavior can be inserted without rewriting the component.

A callback is different from a factory override. A factory override replaces the implementation of a component or object. A callback keeps the original component and adds behavior at predefined hook points.

Think of a callback as a hook: the driver still owns the normal transaction-driving algorithm, while the callback can observe or modify selected behavior before or after that algorithm reaches a defined hook.

When should you use callbacks?

  • When a reusable component needs a small number of controlled extension points.
  • When different tests need optional behavior without creating many derived component classes.
  • When you want test-specific behavior while keeping the base agent reusable.
  • When the extension belongs naturally at a particular lifecycle or transaction boundary.

Avoid callbacks when they make the execution flow difficult to understand. If a behavior is fundamental to the component's identity, a normal derived class or factory override may be clearer.

Step 2: Study the example

The following example adds a callback hook to a driver. The callback can inspect a transaction immediately before the driver sends it.

class driver_cb extends uvm_callback;


`uvm_object_utils(driver_cb)

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

virtual function void before_drive(packet item);
endfunction
endclass

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

function new(string name, uvm_component parent);
super.new(name, parent);
endfunction

task run_phase(uvm_phase phase);
packet item;


forever begin
  seq_item_port.get_next_item(item);

  `uvm_do_callbacks(packet_driver, driver_cb,
                    before_drive(item))

  drive_packet(item);

  seq_item_port.item_done();
end


endtask

task drive_packet(packet item);
// Normal DUT interface driving occurs here.
endtask
endclass

The important design boundary is before_drive(). The driver does not need to know what the callback will do. It simply invokes the registered callbacks at the defined extension point.

Registering a test-specific callback

class error_injection_cb extends driver_cb;


`uvm_object_utils(error_injection_cb)

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

virtual function void before_drive(packet item);
if (item.inject_error)
item.data ^= 'h1;
endfunction
endclass

error_injection_cb cb;

function void build_phase(uvm_phase phase);
super.build_phase(phase);

cb = error_injection_cb::type_id::create("cb");

uvm_callbacks#(packet_driver, driver_cb)::add(env.agent.driver, cb);
endfunction

Here the reusable driver remains unchanged. The test adds an error_injection_cb object and registers it with the driver. When the driver reaches before_drive(), the callback can apply the test-specific behavior.

Debugging tip: if the callback appears not to run, verify three things first: the component registered the callback type, the callback object was added to the correct component instance, and the component actually reaches the callback invocation point.

Step 3: Trace the execution flow

Do not start by memorizing the callback macros. First understand the sequence of events:

  1. The sequence creates or supplies a transaction.
  2. The driver retrieves the transaction.
  3. The driver reaches its defined callback hook.
  4. Registered callback objects receive the hook invocation.
  5. The driver continues with its normal behavior.
  6. The monitor observes the resulting DUT activity.

This separation is valuable because the callback modifies the extension point, not the driver's fundamental responsibility.

Callback versus factory override

Technique Primary purpose
Configuration Supply values or settings that change component behavior.
Factory override Replace one implementation with another implementation.
Callback Add optional behavior at a predefined extension point.
Common mistake: putting major component behavior inside callbacks simply because callbacks are available. Keep the main algorithm in the reusable component and reserve callbacks for focused extensions such as logging, error injection, protocol variation, or test-specific observation.

Step 4: Build it incrementally

  1. Create a base callback class with one clearly named hook.
  2. Register the callback type with the target UVM component.
  3. Add the callback invocation at a meaningful extension point.
  4. Create a derived callback that performs one small test-specific action.
  5. Register the callback instance from the test.
  6. Run a deterministic transaction and verify that the callback executes.
  7. Only after that, experiment with multiple callbacks or more complex behavior.

A useful validation message is more informative than simply printing "callback called." Include the transaction ID or another field that proves the callback observed the expected transaction.

virtual function void before_drive(packet item);


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

Try it yourself

Extend the driver callback example with a before_drive() callback that selectively modifies one transaction.

  1. Add an inject_error field to the transaction.
  2. Create a callback that changes one data field only when inject_error is set.
  3. Register the callback from the test.
  4. Send one normal transaction and one error-injection transaction.
  5. Use a UVM report or scoreboard result to prove that only the selected transaction was modified.

Challenge: register two callbacks and give each one a distinct diagnostic message. Determine the order in which they execute and document the result.

Key takeaways

  • Callbacks provide controlled extension points without replacing the reusable UVM component.
  • Use callbacks for focused, optional behavior rather than moving the component's core algorithm into callback code.
  • Factory overrides replace implementations; configuration supplies settings; callbacks extend predefined behavior.
  • Always verify that the callback is registered with the correct component instance and invoked at the intended hook.
  • Good callback designs make specialized tests possible while keeping the base verification environment reusable.