Module 2: UVM Foundations | Beginner UVM

Copy, Compare, Print, and Record

Learn how UVM objects can be inspected, duplicated, compared, and recorded using the field automation introduced in Lesson 15.

Learning objectives

  • Explain what print(), copy(), compare(), and record() do.
  • Understand how field automation connects object fields to these operations.
  • Use copy() and compare() with UVM transactions.
  • Use print() and sprint() to debug transaction contents.
  • Understand the difference between copying an object and assigning an object handle.
  • Recognize common mistakes when fields are missing from automation.
  • Understand the basic purpose of transaction recording.

Why these operations matter

Verification code constantly needs to answer simple questions:

  • What is inside this transaction?
  • Is this transaction equal to the expected transaction?
  • Can I make a separate copy of this transaction?
  • Can I record this transaction so I can inspect it later?

UVM provides standard object methods for these jobs:

Operation Main purpose
print() Display the contents of an object.
sprint() Return the formatted contents as a string.
copy() Copy the contents of one UVM object into another.
compare() Compare two UVM objects using their registered fields.
record() Record transaction information for supported UVM transaction recording.
Mental model: Lesson 15 taught UVM which fields belong to the object. This lesson uses that information to perform useful operations on those fields.

Step 1: Start with a UVM object

We will use a simple packet throughout this lesson.

class packet extends uvm_sequence_item;


rand bit [7:0]  addr;
rand bit        write;
rand bit [31:0] data;

`uvm_object_utils_begin(packet)
    `uvm_field_int(addr,  UVM_DEFAULT)
`uvm_field_int(write, UVM_DEFAULT)
    `uvm_field_int(data,  UVM_DEFAULT)
`uvm_object_utils_end

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

endclass

Notice that the fields are registered with the field automation macros.

That registration is important because UVM can now use the field information when performing operations such as printing, copying, and comparing.

Remember: rand controls SystemVerilog randomization. The uvm_field_* macros tell UVM which fields participate in field automation. These are different mechanisms.

Step 2: Print an object

One of the most useful debugging operations is printing a transaction.

packet pkt;


pkt = packet::type_id::create("pkt");

pkt.addr  = 8'h20;
pkt.write = 1;
pkt.data  = 32'h1234_5678;

pkt.print();

print() asks the object to display its contents using UVM's object printing facilities.

Because the fields were registered, UVM knows about addr, write, and data.

This is extremely useful when debugging a driver, sequence, monitor, or scoreboard:

`uvm_info("PKT_DEBUG",
      pkt.sprint(),
      UVM_MEDIUM)

The difference is simple:

  • print() directly prints the object using the UVM printer.
  • sprint() returns the formatted representation as a string.
Debugging tip: When a transaction looks wrong, print it before changing the driver or scoreboard. The transaction contents often reveal the problem immediately.

Step 3: Copy an object

Suppose we have an original packet and want another packet with the same field values.

packet pkt1;


packet pkt2;

pkt1 = packet::type_id::create("pkt1");
pkt2 = packet::type_id::create("pkt2");

pkt1.addr  = 8'h40;
pkt1.write = 1;
pkt1.data  = 32'hABCD_1234;

pkt2.copy(pkt1);

After the call to copy(), the registered field values from pkt1 are copied into pkt2.

Conceptually:

pkt1


├── addr  = 8'h40
├── write = 1
└── data  = 32'hABCD_1234


    copy()


pkt2
├── addr  = 8'h40
├── write = 1
└── data  = 32'hABCD_1234

The two variables still refer to different objects.

Important: copy() copies object contents. It does not make the two variables point to the same object.

Step 4: Copying is different from handle assignment

This distinction is critical in SystemVerilog.

packet pkt1;


packet pkt2;

pkt1 = packet::type_id::create("pkt1");

pkt2 = pkt1;

Here, pkt2 receives the same object handle as pkt1.

There is still only one packet object.

pkt1 ─────┐
      │
      ▼
   packet
      ▲
      │


pkt2 ─────┘

Changing the object through either handle affects the same object.

With copy(), the destination is a separate object:

pkt1 ─────► packet A


pkt2 ─────► packet B

Step 5: Compare two objects

Verification often needs to answer:

"Does the actual transaction match the expected transaction?"

UVM provides compare() for this purpose.

packet expected;


packet actual;

expected = packet::type_id::create("expected");
actual   = packet::type_id::create("actual");

expected.addr  = 8'h10;
expected.write = 1;
expected.data  = 32'hAAAA_5555;

actual.addr  = 8'h10;
actual.write = 1;
actual.data  = 32'hAAAA_5555;

if (actual.compare(expected))
`uvm_info("COMPARE", "Packets match", UVM_LOW)
else
  `uvm_error("COMPARE", "Packets do not match")

If the registered fields match according to the comparison policy, compare() returns true. Otherwise, it returns false.

This makes comparison useful for scoreboards and other checking components.

Think like a checker: the expected packet represents what should happen. The actual packet represents what was observed. Comparison tells you whether the two agree.

Step 6: See what happens when a field is different

Change one field in the actual packet:

actual.data = 32'hAAAA_0000;


if (actual.compare(expected))
`uvm_info("COMPARE", "Packets match", UVM_LOW)
else
  `uvm_error("COMPARE", "Packets do not match")

Now the comparison should fail because the registered data field is different.

This is one reason field automation is valuable: you do not need to manually write comparison code for every basic field in the object.

Important: comparison behavior depends on the UVM comparison policy and field flags. For beginner code, focus on the idea that registered fields participate in the operation.

Step 7: A forgotten field can cause a debugging problem

Consider this object:

class packet extends uvm_sequence_item;


bit [7:0]  addr;
bit        write;
bit [31:0] data;

`uvm_object_utils_begin(packet)
    `uvm_field_int(addr, UVM_DEFAULT)
`uvm_field_int(write, UVM_DEFAULT)
    // data was forgotten
  `uvm_object_utils_end

endclass

The data variable still exists. However, it is not registered for field automation.

That means you should not assume that operations relying on field automation will automatically process data.

Step 8: Record transactions

UVM also supports transaction recording so transaction activity can be captured by a compatible recording mechanism and examined later.

The basic idea is:

Stimulus


↓
Driver
↓
DUT
↓
Monitor
↓
Transaction
↓
Recording / Debug Information

A transaction may contain useful information such as an address, operation, data value, and timing-related transaction information.

Recording can help answer questions such as:

  • Which transaction happened before a failure?
  • What values did the monitor observe?
  • What transaction activity occurred around an error?

The exact recording backend and visualization depend on the simulator and UVM recording configuration, so beginners should focus on the purpose rather than a specific waveform or database format.

Rule of thumb: printing is usually the quickest debugging tool. Recording becomes more useful when you need persistent transaction history and tool-based analysis.

Step 9: The four operations together

These operations solve different verification problems:

Question Operation
What values are in this transaction? print() / sprint()
How do I make another object with these values? copy()
Does this transaction match that one? compare()
How can I preserve transaction activity for later analysis? record()

These operations are especially useful for transaction-based verification environments.

Step 10: Where these operations appear in a real testbench

Imagine a scoreboard receiving an actual transaction from a monitor.

Monitor


│
│ actual transaction
▼
Scoreboard
│
├── get expected transaction
│
├── compare expected vs actual
│
└── report mismatch

The scoreboard may use compare() to determine whether the DUT behaved correctly.

During debugging, you might use:

`uvm_info("EXPECTED", expected.sprint(), UVM_MEDIUM)


`uvm_info("ACTUAL",   actual.sprint(),   UVM_MEDIUM)

Now you can see both sides of the comparison.

Verification habit: when a comparison fails, inspect both the expected and actual transactions. Do not immediately assume the DUT is wrong—the stimulus, monitor, or reference model may also be the source of the mismatch.

Step 11: A useful debugging pattern

A simple transaction debugging pattern is:

expected.print();


actual.print();

if (!actual.compare(expected)) begin
`uvm_error("MISMATCH",
"Actual transaction does not match expected transaction")
end

This gives you three useful pieces of information:

  1. What did we expect?
  2. What did we observe?
  3. Did they match?

This pattern becomes especially valuable later when building scoreboards.

Step 12: Important limitation of field automation

Field automation is convenient, but it is not magic.

For simple integral fields, the behavior is straightforward. More complex objects may require more careful handling, especially when you have:

  • nested objects
  • dynamic arrays
  • queues
  • associative arrays
  • special comparison rules
  • fields that should intentionally be ignored

For advanced cases, explicit do_copy(), do_compare(), do_print(), or related methods may provide more control.

For beginners: do not try to memorize every customization method yet. First become comfortable with the standard operations and field automation. Advanced customization can be added when your transaction design requires it.

Try it yourself

Create a bus_transaction with these fields:

bit [7:0]  addr;


bit        write;
bit [31:0] data;
  1. Extend uvm_sequence_item.
  2. Add a constructor.
  3. Register the class with uvm_object_utils_begin/end.
  4. Register all three fields with uvm_field_int.
  5. Create two transactions using the factory.
  6. Assign values to the first transaction.
  7. Print the first transaction using print().
  8. Copy it into the second transaction using copy().
  9. Print the second transaction.
  10. Compare the two transactions using compare().
  11. Change one field in the second transaction and compare them again.
Challenge

Add a field called debug_id. Register it and verify that changing it can affect comparison.

Then remove it from the field automation list and observe the difference.

Quick check

  1. What does print() do?
    It displays the contents of a UVM object.
  2. What is the difference between sprint() and print()?
    sprint() returns the formatted object contents as a string, while print() uses the UVM printer to display them.
  3. What does copy() do?
    It copies object contents into a destination UVM object.
  4. What does pkt2 = pkt1 do?
    It copies the object handle, so both variables refer to the same object.
  5. What does compare() answer?
    It determines whether two UVM objects match according to their comparison policy and registered fields.
  6. Why does field registration matter?
    Field automation provides UVM with information about which fields participate in supported automated operations.
  7. What is transaction recording used for?
    It can preserve transaction activity for later debugging and analysis through a supported recording mechanism.

Key takeaways

  • print() and sprint() are valuable for transaction debugging.
  • copy() copies object contents into another UVM object.
  • pkt2 = pkt1 copies a handle rather than creating a separate object.
  • compare() is useful for checking expected versus actual transactions.
  • record() supports transaction recording for later analysis.
  • Field automation connects registered fields to many standard UVM object operations.
  • A field that is not registered should not be assumed to participate in field automation.
  • Advanced transaction designs may require custom UVM methods instead of relying only on automation.

What’s next?

You now know how UVM objects can be inspected, copied, compared, and recorded.

Next, we move to one of the most important UVM concepts: the UVM Factory.

You have already seen code such as:

packet::type_id::create("pkt")

In Lesson 17, you will learn what that factory call actually does, why UVM uses a factory, and how the factory enables reusable testbench components to be replaced without rewriting the code that creates them.