Module 1: SystemVerilog for UVM | Beginner UVM

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.

Simple rule: A class is a blueprint. An object is an instance created from that blueprint.

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 address
  • data — 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.

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:

  • addr
  • data
  • write

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
Object-oriented idea: A class can keep related data and behavior together.

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.

Remember: Constructors are useful when an object needs a known initial state.

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 ───────┘

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
Rule: Every call to 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.

Why this matters in verification: Verification components frequently need to pass transaction objects between different parts of a testbench.

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.

This is the beginning of transaction-based verification. UVM will build much more structure around this same basic idea.

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.

UVM connection: The UVM classes you will use later are built on the same SystemVerilog class and object concepts you are learning here.

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 bits
  • data — 32 bits
  • write — 1 bit

Then:

  1. Create a transaction object.
  2. Set addr to 8'h20.
  3. Set data to 32'hABCD_1234.
  4. Set write to 1.
  5. Print all three values.
Challenge: Create two transaction objects. Give each object a different address. Change the address of the first object and verify that the second object does not change.

Quick knowledge check

Question 1

What is a SystemVerilog class?

  1. A hardware interface
  2. A blueprint for creating objects
  3. A simulation process
  4. A clocking block
Answer: B — A class is a blueprint for creating objects.

Question 2

What does new() do?

  1. Deletes an object
  2. Creates an object
  3. Randomizes an object
  4. Starts a UVM phase
Answer: B — new() constructs an object.

Question 3

What happens in this code?

pkt2 = pkt1;
  1. A completely new packet is created
  2. The packet is deleted
  3. The handle is copied
  4. The packet is randomized
Answer: C — The handle is copied, so both handles refer to the same object.

Question 4

How do you create two independent packet objects?

  1. pkt2 = pkt1;
  2. pkt1 = pkt2;
  3. Call new() for each object
  4. Use copy() automatically
Answer: C — Each independent object must be separately constructed.

Common beginner mistakes

1. Forgetting new()
packet pkt;


pkt.addr = 8'h10;

The handle exists, but no packet object has been created yet.

2. Assuming handle assignment copies the object
pkt2 = pkt1;

This copies the handle. Both handles refer to the same object.

3. Creating only one object when two are needed

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.