Module 2: UVM Foundations | Beginner UVM

Field Automation Macros

Learn how UVM registers class fields for automation, how to choose the correct field macro, and how field automation reduces repetitive verification code.

Learning objectives

  • Explain why UVM provides field automation macros.
  • Understand the difference between type registration and field registration.
  • Use uvm_object_utils_begin/end correctly.
  • Register integer, enum, object, and string fields for UVM automation.
  • Understand what UVM_DEFAULT means at a high level.
  • Recognize when a field should not be registered for automation.
  • Use field automation to make UVM objects easier to inspect and manipulate.
  • Recognize common field-macro mistakes and debug them.

Step 1: Why does UVM need field automation?

In the previous lessons, you learned that UVM objects can contain many fields. For example, a transaction might contain an address, command, data value, byte enables, and control flags.

class packet extends uvm_object;


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

endclass

Imagine a transaction with 20 or 30 fields. If UVM had no field automation, you could end up writing repetitive code to tell UVM how to handle every field when printing, copying, comparing, packing, recording, and performing other object operations.

Field automation gives UVM information about the fields inside an object. Once the fields are registered, UVM can use that information for supported object operations.

Mental model: Field macros are a way of telling UVM: "These are the fields that belong to this object, and UVM may operate on them automatically."

Step 2: Type registration vs field registration

This distinction is very important.

In Lesson 13, you learned:

`uvm_object_utils(packet)

This registers the type with UVM. It does not describe the individual fields inside the object.

Field automation uses a different pattern:

`uvm_object_utils_begin(packet)


// Field registrations go here
`uvm_object_utils_end

You can think about the two patterns like this:

Macro Purpose
`uvm_object_utils Register the object type with the factory.
`uvm_object_utils_begin/end Register the object type and provide a place for field automation.
`uvm_field_int Register an integral field.
`uvm_field_enum Register an enum field.
`uvm_field_object Register a nested UVM object field.
`uvm_field_string Register a string field.
Common mistake: Do not confuse registering a class with registering its fields. `uvm_object_utils(packet) tells UVM about the type. Field macros tell UVM about the data inside that type.

Step 3: Register integer fields

The most common field macro for simple integral values is uvm_field_int.

Consider this packet:

class packet extends uvm_object;


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)
    `uvm_field_int(data,  UVM_DEFAULT)
`uvm_object_utils_end

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

endclass

Each uvm_field_int line tells UVM that the corresponding integral field is part of the object's automated data.

`uvm_field_int(addr, UVM_DEFAULT)

The first argument is the field. The second argument specifies the automation flags.

Step 4: What does UVM_DEFAULT mean?

You will frequently see:

UVM_DEFAULT

It is a predefined combination of UVM field automation flags. For beginner code, it is a useful default because it enables the normal field behavior without requiring you to learn every flag immediately.

For example:

`uvm_field_int(addr,  UVM_DEFAULT)


`uvm_field_int(data,  UVM_DEFAULT)
`uvm_field_int(write, UVM_DEFAULT)
Don't memorize every flag yet. First understand the field registration pattern. Advanced field flags are useful later when you need precise control over printing, copying, comparison, packing, or other operations.

The important idea is that the second argument controls how UVM should treat that field during its automated operations.

Step 5: Register different field types

Not every field is an integer. UVM provides different field macros for different kinds of data.

Integral fields

bit [7:0] addr;


`uvm_field_int(addr, UVM_DEFAULT)

String fields

string command;


`uvm_field_string(command, UVM_DEFAULT)

Enum fields

typedef enum {READ, WRITE} command_e;


command_e command;

`uvm_field_enum(command_e, command, UVM_DEFAULT)

Nested UVM objects

packet_header header;


`uvm_field_object(header, UVM_DEFAULT)

The exact macro you choose should match the type of data being registered.

Rule of thumb: Look at the field's SystemVerilog type first. Then choose the UVM field macro designed for that type.

Step 6: A complete packet example

Let's put the most common beginner pattern together.

class packet extends uvm_object;


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

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

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

endclass

Notice that randomization and field automation are separate concepts.

  • rand controls whether SystemVerilog can randomize the field.
  • uvm_field_int tells UVM about the field for automation.
Important: A field does not become random simply because it is registered with a UVM field macro. Randomization comes from the SystemVerilog rand keyword and constraints.

Step 7: What field automation can enable

Once fields are registered, UVM can use that field information for several standard object operations.

Depending on the operation and field flags, this can include:

  • Printing object contents
  • Copying object contents
  • Comparing object contents
  • Packing and unpacking data
  • Recording object data

For example, if a packet has ten fields, field automation can save you from manually writing ten separate assignments or comparisons for standard UVM operations.

Big idea: Field automation turns the structure of your class into information that UVM can use consistently across multiple object operations.

We will explore those operations in detail in the next lesson.

Step 8: Fields do not have to be automated

Not every class field should necessarily be included in automation.

Consider:

class packet extends uvm_object;


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

// Internal bookkeeping information
int debug_counter;

endclass

Perhaps debug_counter is internal bookkeeping and should not be treated as part of the transaction's meaningful data.

You can simply leave it out of the field macro list.

`uvm_object_utils_begin(packet)


`uvm_field_int(addr, UVM_DEFAULT)
  `uvm_field_int(data, UVM_DEFAULT)
`uvm_object_utils_end
Design principle: Automate the fields that represent the meaningful state of the object. Do not blindly register every variable in a class.

Step 9: A common mistake — forgetting a field

One of the most common field automation bugs is simply forgetting to register a field.

Suppose the class contains:

class packet extends uvm_object;


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

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

endclass

The write variable still exists. SystemVerilog can still read and write it. But UVM field automation does not know that it should operate on write.

This can produce confusing debugging results. You may look at an object and see a field in the class, but that field may not appear in automated operations.

Debugging rule: If a field is unexpectedly missing from an automated UVM operation, first check whether that field was registered.

Step 10: Another common mistake — wrong macro

The field macro should match the field type.

For example, a string should not be registered as an integer field.

string name;


// Wrong
`uvm_field_int(name, UVM_DEFAULT)

// Correct
`uvm_field_string(name, UVM_DEFAULT)

Similarly, enum fields use uvm_field_enum, and nested UVM objects use uvm_field_object.

Debugging habit: When field automation behaves unexpectedly, check both the field type and the field macro.

Step 11: Field automation and inheritance

UVM field automation is especially useful when classes are extended.

Imagine a base packet:

class packet extends uvm_object;


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

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

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

endclass

A specialized packet could add another field:

class read_packet extends packet;


bit [7:0] burst_length;

`uvm_object_utils_begin(read_packet)
    `uvm_field_int(burst_length, UVM_DEFAULT)
`uvm_object_utils_end

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

endclass

In real UVM code, inheritance and automation need to be designed carefully, especially when you want base-class fields and derived-class fields to participate consistently.

For now, the important lesson is that field automation is part of the class design, not an unrelated piece of syntax.

Step 12: The pattern to remember

For a UVM object with automated fields, learn this pattern:

class my_object extends uvm_object;


// Data
bit [7:0] addr;
bit [31:0] data;

// Type + field registration
`uvm_object_utils_begin(my_object)
    `uvm_field_int(addr, UVM_DEFAULT)
`uvm_field_int(data, UVM_DEFAULT)
  `uvm_object_utils_end

// Constructor
function new(string name = "my_object");
super.new(name);
endfunction

endclass

The three sections have different jobs:

  1. Data — defines the state of the object.
  2. Registration — tells UVM about the type and its automated fields.
  3. Constructor — initializes the UVM object base class.

Step 13: When should you use field automation?

Field automation is particularly useful for reusable UVM data objects such as:

  • Sequence items
  • Transaction objects
  • Configuration objects
  • Protocol-specific data objects
  • Other reusable verification data structures

However, field macros are not mandatory for every UVM object. You should use them when the standard automation they provide is useful for your object.

Engineering principle: Use automation because it makes the verification environment clearer, more maintainable, and less repetitive—not simply because a macro appears in every UVM example.

Try it yourself

Create a UVM object called bus_transaction with these fields:

bit [7:0]  addr;


bit [31:0] data;
bit        write;
string     description;

Then:

  1. Extend uvm_object.
  2. Add the correct constructor.
  3. Register the class with uvm_object_utils_begin/end.
  4. Register the three integral fields with uvm_field_int.
  5. Register description with uvm_field_string.
  6. Use UVM_DEFAULT for each field.
  7. Create the object and print it.
Challenge: Add a field called debug_id and intentionally leave it out of the field macro list. Think about why UVM automation would treat that field differently.

Quick check

Question 1: What is the purpose of a UVM field macro?

Answer: It tells UVM about a field so supported UVM object operations can process that field automatically.

Question 2: What is the difference between these two?

`uvm_object_utils(packet)
`uvm_object_utils_begin(packet)


`uvm_field_int(addr, UVM_DEFAULT)
`uvm_object_utils_end
Answer: The first registers the object type. The second registers the type and provides field automation information.

Question 3: Which macro would you use for a string?

Answer: `uvm_field_string.

Question 4: Does a field become random because it uses a UVM field macro?

Answer: No. Randomization is controlled by SystemVerilog features such as rand and constraints.

Question 5: What happens if you forget to register a field?

Answer: The field still exists as a SystemVerilog variable, but UVM field automation will not automatically process it.

Key takeaways

  • Field automation tells UVM which object fields should participate in supported automated operations.
  • `uvm_object_utils registers a type, while `uvm_object_utils_begin/end provides field registration.
  • Use the field macro that matches the field's data type.
  • uvm_field_int is commonly used for integral fields.
  • uvm_field_string is used for strings.
  • uvm_field_enum is used for enumerated values.
  • uvm_field_object is used for nested UVM objects.
  • UVM_DEFAULT is a useful starting point for field flags.
  • Not every class variable must be registered for automation.
  • Field automation is independent from SystemVerilog randomization.

What's next?

Now that UVM knows about the fields inside an object, we can use that information for practical object operations.

In the next lesson, you will learn how UVM objects can be printed, copied, compared, and recorded—and why these operations are essential when debugging transactions and verification environments.