Classes and Objects
Learn the SystemVerilog class and object concepts that form the foundation of UVM.
Learning objectives
- Explain what a SystemVerilog class is.
- Explain what an object is and how it is created.
- Create classes with properties and methods.
- Use a constructor with
new(). - Understand the difference between an object and an object handle.
- Create multiple independent objects from the same class.
- Pass objects to functions and tasks.
- Explain why classes and objects are important in UVM.
Step 1: What is a class?
A class is a blueprint for creating objects. It defines the data an object can contain and the behavior that object can perform.
If you are familiar with hardware design, you can think of a class as a template. The template describes what something looks like, but the template itself is not one specific instance.
Example
class packet;
bit [7:0] addr;
bit [31:0] data;
endclass
We have created a class called packet.
The class contains two properties:
addr— an 8-bit addressdata— a 32-bit data value
However, we have not created a packet object yet.
Step 2: What is an object?
An object is an instance of a class. It is created from the class definition.
To create an object, first declare a class variable and then use
new().
class packet;
bit [7:0] addr;
bit [31:0] data;
endclass
module test;
packet pkt;
initial begin
pkt = new();
pkt.addr = 8'h10;
pkt.data = 32'h1234_ABCD;
$display("addr = %h", pkt.addr);
$display("data = %h", pkt.data);
end
endmodule
These two lines are especially important:
packet pkt;
pkt = new();
The first line declares a variable called pkt.
The second line creates the object.
new().
Step 3: Class properties
Data stored inside a class is called a property.
Properties describe the state of an object.
class packet;
bit [7:0] addr;
bit [31:0] data;
bit write;
endclass
A packet object now has three properties:
addrdatawrite
We can assign values to these properties through the object handle.
packet pkt;
initial begin
pkt = new();
pkt.addr = 8'h20;
pkt.data = 32'hCAFE_BABE;
pkt.write = 1;
end
The dot operator . is used to access members of an object.
pkt.addr
pkt.data
pkt.write Step 4: Class methods
A class can contain more than data. It can also contain methods.
A method is a function or task defined inside a class. Methods provide behavior for an object.
class packet;
bit [7:0] addr;
bit [31:0] data;
function void print();
$display("addr = %h", addr);
$display("data = %h", data);
endfunction
endclass
The print() method belongs to the packet class.
It can directly access the properties of the current object.
We can call the method using the object:
packet pkt;
initial begin
pkt = new();
pkt.addr = 8'h40;
pkt.data = 32'hDEAD_BEEF;
pkt.print();
end
Step 5: Constructors and new()
The new() method is used to construct an object.
It is called a constructor.
You can define your own constructor to initialize the object's properties.
class packet;
bit [7:0] addr;
bit [31:0] data;
function new();
addr = 0;
data = 0;
endfunction
endclass
Now when a packet is created, its properties are initialized.
packet pkt;
initial begin
pkt = new();
$display("addr = %h", pkt.addr);
$display("data = %h", pkt.data);
end
The output will show zero values because the constructor initialized both properties to zero.
Step 6: Object handles
Now we reach one of the most important concepts for understanding SystemVerilog and UVM: the object handle.
When you write:
packet pkt;
pkt is a handle that can refer to a packet object.
It is not the packet object itself.
The actual object is created when we call:
pkt = new();
You can visualize this as:
pkt
|
| refers to
v
+-------------------+
| packet object |
| ---------------------------------- |
| addr |
| data |
| +-------------------+ |
Step 7: What happens if two handles refer to one object?
Consider this example:
packet pkt1;
packet pkt2;
pkt1 = new();
pkt1.addr = 8'h10;
pkt2 = pkt1;
pkt2.addr = 8'h20;
What is the value of pkt1.addr?
The value is:
8'h20
Why?
Because pkt1 and pkt2 refer to the same object.
pkt1 ───────┐
│
▼
+-----------+
| packet |
|-----------|
| addr = 20 |
| data |
+-----------+
▲
│
pkt2 ───────┘
pkt2 = pkt1 does not create a second packet.
It copies the handle.
Step 8: Creating independent objects
If you want two independent packet objects, create them separately.
packet pkt1;
packet pkt2;
pkt1 = new();
pkt2 = new();
pkt1.addr = 8'h10;
pkt2.addr = 8'h20;
Now there are two different objects:
pkt1 ───────► packet object #1
addr = 10
pkt2 ───────► packet object #2
addr = 20
Changing one object does not change the other.
pkt1.addr = 8'h30;
The values are now:
pkt1.addr = 8'h30
pkt2.addr = 8'h20
new() creates a new object.
Step 9: Passing objects to functions
Objects can be passed to functions and tasks. This is extremely useful in verification environments.
class packet;
bit [7:0] addr;
bit [31:0] data;
endclass
function void print_packet(packet pkt);
$display("addr = %h", pkt.addr);
$display("data = %h", pkt.data);
endfunction
We can create a packet and pass it to the function:
packet pkt;
initial begin
pkt = new();
pkt.addr = 8'h50;
pkt.data = 32'h1234_5678;
print_packet(pkt);
end
The function receives a handle to the packet object and can access its properties.
Step 10: Build a simple verification transaction
Now let's use everything we have learned to model a simple bus operation.
Suppose our DUT supports a simple bus with:
- An address
- Data
- A read/write control
We can represent one bus operation as an object.
class bus_transaction;
bit [7:0] addr;
bit [31:0] data;
bit write;
function new();
addr = 0;
data = 0;
write = 0;
endfunction
function void print();
$display(
"addr=%h data=%h write=%b",
addr,
data,
write
);
endfunction
endclass
Create and use the transaction:
bus_transaction tr;
initial begin
tr = new();
tr.addr = 8'h10;
tr.data = 32'h1234_5678;
tr.write = 1;
tr.print();
end
We have now created a small object that represents a verification transaction.
Step 11: Why are classes important in UVM?
UVM is built heavily around SystemVerilog classes. Understanding classes now will make the UVM class hierarchy much easier to understand later.
In a UVM environment, objects will represent things such as:
- Transactions
- Sequences
- Configuration objects
- UVM components
- Tests
- Agents
- Scoreboards
- Monitors
- Drivers
For example, later in this course you will see classes such as:
class my_transaction extends uvm_sequence_item;
...
endclass
Do not worry about extends or
uvm_sequence_item yet.
Those concepts will be introduced in later lessons.
Class vs. Object
Keep this distinction clear:
| Concept | Meaning | Example |
|---|---|---|
| Class | Blueprint for creating objects | class packet; |
| Object | An instance created from a class | pkt = new(); |
| Property | Data stored in an object | pkt.addr |
| Method | Function or task belonging to a class | pkt.print() |
| Handle | A variable that refers to an object | packet pkt; |
| Constructor | Used to create and initialize an object | new() |
Try it yourself
Create a class called transaction.
Add these properties:
addr— 8 bitsdata— 32 bitswrite— 1 bit
Then:
- Create a transaction object.
- Set
addrto8'h20. - Set
datato32'hABCD_1234. - Set
writeto1. - Print all three values.
Quick knowledge check
Question 1
What is a SystemVerilog class?
- A hardware interface
- A blueprint for creating objects
- A simulation process
- A clocking block
Question 2
What does new() do?
- Deletes an object
- Creates an object
- Randomizes an object
- Starts a UVM phase
new() constructs an object.
Question 3
What happens in this code?
pkt2 = pkt1;
- A completely new packet is created
- The packet is deleted
- The handle is copied
- The packet is randomized
Question 4
How do you create two independent packet objects?
pkt2 = pkt1;pkt1 = pkt2;- Call
new()for each object - Use
copy()automatically
Common beginner mistakes
new()
packet pkt;
pkt.addr = 8'h10;
The handle exists, but no packet object has been created yet.
pkt2 = pkt1;
This copies the handle. Both handles refer to the same object.
If two transactions must be independent, create two objects
using two calls to new().
Key takeaways
- A class is a blueprint for creating objects.
- An object is an instance of a class.
- A class can contain properties and methods.
new()creates an object.- A class variable is a handle to an object.
- Assigning one handle to another does not create a new object.
- Calling
new()separately creates independent objects. - Objects can be passed to functions and tasks.
- Verification transactions can be represented as objects.
- UVM relies heavily on SystemVerilog classes and objects.
What's next?
You now know how SystemVerilog classes and objects work. Next, we will build on this foundation with one of the most important object-oriented concepts used throughout UVM: inheritance and polymorphism.
You will learn how one class can extend another class, override behavior, and allow UVM to work with objects through a common base type.