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/endcorrectly. - Register integer, enum, object, and string fields for UVM automation.
- Understand what
UVM_DEFAULTmeans 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.
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. |
`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)
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.
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.
randcontrols whether SystemVerilog can randomize the field.uvm_field_inttells UVM about the field for automation.
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.
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
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.
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.
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:
- Data — defines the state of the object.
- Registration — tells UVM about the type and its automated fields.
- 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.
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:
- Extend
uvm_object. - Add the correct constructor.
- Register the class with
uvm_object_utils_begin/end. - Register the three integral fields with
uvm_field_int. - Register
descriptionwithuvm_field_string. - Use
UVM_DEFAULTfor each field. - Create the object and print it.
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?
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
Question 3: Which macro would you use for a string?
`uvm_field_string.
Question 4: Does a field become random because it uses a UVM field macro?
rand and constraints.
Question 5: What happens if you forget to register a field?
Key takeaways
- Field automation tells UVM which object fields should participate in supported automated operations.
-
`uvm_object_utilsregisters a type, while`uvm_object_utils_begin/endprovides field registration. - Use the field macro that matches the field's data type.
-
uvm_field_intis commonly used for integral fields. -
uvm_field_stringis used for strings. -
uvm_field_enumis used for enumerated values. -
uvm_field_objectis used for nested UVM objects. -
UVM_DEFAULTis 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.