Module 410 min

Object-Oriented Basics

Classes and objects - the foundation UVM is built on

Why this matters

UVM, the industry-standard verification framework, is built entirely on classes. If classes feel shaky, UVM will feel like magic you cannot control. Spend real time here; it pays off in every later lesson.

Class and object - the blueprint and the thing

A class is a blueprint. An object is an actual thing built from that blueprint. The blueprint for a house is one document; you can build many houses from it. Same with classes: one class, many objects.

systemverilog
class Packet;
  bit [7:0] addr;
  bit [7:0] data;

  // a function that belongs to the class
  function void print();
    $display("addr=%0d data=%0d", addr, data);
  endfunction
endclass

// Using it:
Packet p;            // a handle (empty for now)
p = new();           // build an actual object
p.addr = 8'h10;
p.data = 8'hFF;
p.print();           // addr=16 data=255
  • Packet p declares a handle - a name that can point at an object.
  • new() actually creates the object in memory.
  • p.addr reaches inside the object to its data.
  • p.print() calls a function that belongs to the object.

Inheritance - build on what exists

You can make a new class that is a specialized version of an existing one. It gets everything the parent had, and you add or change what is different. A "long packet" is still a packet, plus extra.

systemverilog
class LongPacket extends Packet;   // inherits addr, data, print
  bit [7:0] length;                // adds something new
endclass

Why verification loves classes

  • A transaction (one unit of stimulus, like a bus request) is naturally an object.
  • You can randomize an object to get thousands of varied transactions.
  • You can build a generic component once and reuse it across projects by extending it.
Watch out

The handle and the object are different things. Packet p; alone gives you an empty handle pointing at nothing (null). You must call new() before using it, or the simulator errors with a null-handle access. Forgetting new() is the number-one beginner mistake in SystemVerilog OOP.