Skip to main content

Module 3 — Object-Oriented Programming

VLSI Verification  •  SystemVerilog Series

Module 3 — Object-Oriented Programming

The OOP foundation that makes UVM possible: classes, objects, constructors, inheritance, polymorphism, encapsulation, virtual methods, abstract classes, static members, and this/super — with testbench-flavored examples throughout.

🧩 Intermediate → Advanced ⏱ ~30 min read 💻 Code Examples Included

26Class

A class is a user-defined type bundling data (properties) and behavior (methods) together — SystemVerilog's core OOP building block, and the foundation of every UVM component and transaction.

SystemVerilog
class packet; // properties (member variables) rand bit [7:0] addr; rand bit [31:0] data; bit valid; // methods (member functions/tasks) function void display(); $display("addr=%0h data=%0h valid=%0b", addr, data, valid); endfunction function void set_valid(bit v); valid = v; endfunction endclass
📘 Why Classes Matter Here

Every UVM transaction (uvm_sequence_item), driver, monitor, agent, and scoreboard is written as a SystemVerilog class. Understanding raw classes is a prerequisite for understanding any UVM code.

27Object

An object is a runtime instance of a class, created with the new() constructor call. Class variables hold a handle (reference) to the object, not the object itself.

SystemVerilog
packet pkt1; // handle, currently null -- no object yet packet pkt2; pkt1 = new(); // object created, pkt1 now references it pkt2 = pkt1; // pkt2 references the SAME object as pkt1 (shallow copy of handle) pkt2.addr = 8'hFF; $display(pkt1.addr); // prints FF -- pkt1 and pkt2 point to the same object! // To get an independent copy, use .copy() (if defined) or new object + field copy packet pkt3; pkt3 = new pkt1; // shallow copy constructor -- new object, fields copied // Checking for a valid handle if (pkt1 == null) $display("handle not yet pointing to an object");
⚠️ Handle vs Object — Common Interview Trap

Assigning one class variable to another (pkt2 = pkt1) copies the handle, not the object — both variables now point to the same underlying object in memory. This is functionally similar to reference/pointer semantics in C++/Java, and is a frequent source of confusing testbench bugs when engineers expect value-copy behavior.

28Constructor

The new() method is the constructor — automatically called when an object is created, used to initialize properties. Every class has an implicit default constructor unless one is explicitly defined.

SystemVerilog
class packet; bit [7:0] addr; bit [31:0] data; string name; // explicit constructor with default argument values function new(string name = "pkt", bit[7:0] addr = 0); this.name = name; // 'this' disambiguates member vs argument this.addr = addr; data = 32'h0; endfunction endclass // Usage packet p1 = new(); // uses defaults: name="pkt", addr=0 packet p2 = new("my_pkt", 8'hA0); // custom args // Derived-class constructors MUST call super.new() (implicitly or explicitly) class ext_packet extends packet; bit crc_en; function new(string name = "ext_pkt"); super.new(name); // must be the FIRST statement if explicit crc_en = 1; endfunction endclass
💡 Rule

If you don't explicitly call super.new() in a derived class's constructor, SystemVerilog implicitly calls the base class's zero-argument new() for you first. If the base constructor requires arguments, you must call super.new(...) explicitly, and it must be the first statement.

29Inheritance

Inheritance lets a class (derived/child) reuse and extend the properties and methods of another class (base/parent) via the extends keyword — the mechanism behind every UVM class hierarchy (e.g., your driver extends uvm_driver).

SystemVerilog
// Base class class transaction; rand bit [31:0] addr; rand bit [31:0] data; function void print(); $display("[BASE] addr=%0h data=%0h", addr, data); endfunction endclass // Derived class -- inherits addr, data, print(), adds its own members class axi_transaction extends transaction; rand bit [2:0] burst_len; rand bit is_write; // method overriding -- redefines print() for this subclass function void print(); super.print(); // call the base class version too $display("[AXI] burst_len=%0d is_write=%0b", burst_len, is_write); endfunction endclass // Usage axi_transaction axi_tr = new(); axi_tr.addr = 32'h1000; // inherited property axi_tr.burst_len = 3'd4; // own property axi_tr.print(); // calls overridden version, which also calls base version
📘 Single Inheritance Only

SystemVerilog supports only single inheritance — a class can extend just one base class. Multiple inheritance is achieved indirectly through interface classes (a class can implement several interface classes even while extending only one base class).

30Polymorphism

Polymorphism means a base-class handle can reference a derived-class object, and calling a (virtual) method through that handle invokes the derived class's overridden version — enabling generic code that works across a whole class family.

SystemVerilog
class transaction; virtual function void print(); $display("base transaction"); endfunction endclass class read_txn extends transaction; virtual function void print(); $display("read transaction"); endfunction endclass class write_txn extends transaction; virtual function void print(); $display("write transaction"); endfunction endclass // Polymorphic array — base handles, mixed derived objects transaction queue[$]; queue.push_back(read_txn::new()); queue.push_back(write_txn::new()); foreach (queue[i]) queue[i].print(); // prints "read transaction" then "write transaction" // -- correct override runs even though the array // is typed as the BASE class 'transaction'
💡 Where UVM Uses This

The UVM factory (uvm_factory) and sequencer/driver communication rely entirely on polymorphism: a driver's seq_item_port handles items through the base uvm_sequence_item type, but calls the correctly overridden methods of whatever derived transaction was actually generated.

31Encapsulation

Encapsulation restricts direct access to a class's internal data using access qualifiers, exposing controlled interaction through public methods instead — protecting invariants and hiding implementation details.

SystemVerilog
class fifo_model; local bit [7:0] mem [0:15]; // local: only THIS class can access local int wr_ptr, rd_ptr; protected int depth; // protected: this class + derived classes function new(); wr_ptr = 0; rd_ptr = 0; depth = 16; endfunction // public methods -- the only sanctioned way to touch mem[] function void push(bit[7:0] val); mem[wr_ptr] = val; wr_ptr = (wr_ptr + 1) % depth; endfunction function bit[7:0] pop(); bit[7:0] val = mem[rd_ptr]; rd_ptr = (rd_ptr + 1) % depth; return val; endfunction endclass fifo_model f = new(); f.push(8'hAA); // OK -- public method // f.mem[0] = 8'h00; // ILLEGAL -- 'mem' is local, inaccessible outside the class
QualifierAccessible From
(default, public)Anywhere the handle is visible
protectedThe declaring class and any classes that extend it
localOnly the declaring class itself (not even derived classes)
⚠️ local vs protected

A common interview trap: local members are not inherited-accessible — even a subclass cannot touch them directly. Use protected instead if derived classes need direct access.

32Virtual Methods

A virtual method enables true polymorphic dispatch — without it, calling an overridden method through a base-class handle would incorrectly run the base class's version instead of the derived one.

SystemVerilog
class base; function void non_virtual_show(); $display("base non-virtual"); endfunction virtual function void virtual_show(); $display("base virtual"); endfunction endclass class derived extends base; function void non_virtual_show(); $display("derived non-virtual"); endfunction virtual function void virtual_show(); $display("derived virtual"); endfunction endclass base b; derived d = new(); b = d; // base handle pointing to a derived object b.non_virtual_show(); // prints "base non-virtual" -- WRONG method (no virtual) b.virtual_show(); // prints "derived virtual" -- CORRECT (virtual dispatch)
🚫 Interview Classic

This exact base-handle/derived-object scenario is one of the most frequently asked SystemVerilog OOP interview questions. Without virtual, the method call resolves based on the static (declared) type of the handle; with virtual, it resolves based on the dynamic (actual object) type. Always mark methods virtual in a base class if you expect subclasses to override them.

33Abstract Classes

A virtual class (SystemVerilog's term for an abstract class) cannot be instantiated directly — it exists only to be extended, typically to enforce a common interface across all subclasses via pure virtual methods.

SystemVerilog
// Abstract base class virtual class shape; // pure virtual method -- no body, MUST be overridden by subclasses pure virtual function real area(); function void describe(); $display("This shape has area = %0.2f", area()); endfunction endclass // shape s = new(); // ILLEGAL -- cannot instantiate a virtual class class circle extends shape; real radius; function new(real r); radius = r; endfunction virtual function real area(); // must implement -- else circle is also abstract return 3.14159 * radius * radius; endfunction endclass circle c = new(2.0); c.describe(); // legal -- circle is a concrete (non-abstract) class
📘 UVM Connection

uvm_component and uvm_object rely heavily on this pattern — many UVM base classes declare pure virtual methods that your derived driver/monitor/scoreboard classes are required to implement (e.g., overriding run_phase).

34Static Members

static properties and methods belong to the class itself, not to any individual object — shared and visible across every instance, useful for counters, ID generators, or shared configuration.

SystemVerilog
class transaction; static int txn_count = 0; // shared across ALL objects of this class int id; // per-object (non-static) function new(); txn_count++; // increments the single shared counter id = txn_count; // each object gets a unique id endfunction // static method -- can be called WITHOUT an object handle static function int get_count(); return txn_count; endfunction endclass transaction t1 = new(); // txn_count = 1, t1.id = 1 transaction t2 = new(); // txn_count = 2, t2.id = 2 transaction t3 = new(); // txn_count = 3, t3.id = 3 $display(transaction::get_count()); // call via class scope resolution -- prints 3 $display(t1.txn_count); // also legal, still prints 3 (shared value)
💡 Interview Tip

Static methods can only directly access static properties — they have no implicit this handle since they aren't tied to any specific object, so they cannot touch non-static (instance) members without being given an explicit object handle.

35this and super

this refers to the current object instance (disambiguating member variables from same-named arguments); super refers to the immediate base-class portion of the current object, used to call overridden base-class methods or constructors.

SystemVerilog
class base_driver; int timeout; function new(int timeout = 100); this.timeout = timeout; // 'this.timeout' = member, 'timeout' = argument endfunction virtual function void drive(); $display("base drive, timeout=%0d", timeout); endfunction endclass class axi_driver extends base_driver; function new(int timeout = 200); super.new(timeout); // call base class constructor explicitly endfunction virtual function void drive(); super.drive(); // invoke the BASE class's drive() first $display("axi_driver: additional AXI-specific drive logic"); endfunction function void print_self(); $display("this handle = %s", this.get_name()); // example use of 'this' endfunction endclass
KeywordRefers ToCommon Use
thisThe current object instanceDisambiguate member vs. local/argument names; pass self as an argument
superThe immediate base-class part of the current objectCall base-class constructor/method that was overridden
⚠️ Placement Rule

If used, super.new(...) must be the first statement in a derived class's constructor — you cannot execute any other code before calling the base class constructor.

🎯 Module 3 Recap

  • A class bundles properties and methods; an object is a runtime instance accessed through a handle (reference), not stored by value.
  • The new() constructor initializes an object; derived classes must properly chain to super.new().
  • extends gives inheritance (single inheritance only); overriding + virtual gives true polymorphism.
  • local/protected qualifiers enforce encapsulation — local blocks even subclasses, protected allows them.
  • Mark base-class methods virtual whenever subclasses might override them — otherwise base-handle calls silently use the wrong (base) implementation.
  • virtual class + pure virtual methods create abstract base types that enforce a common interface, exactly like UVM's own base classes.
  • static members are shared across all objects of a class; static methods can't touch non-static members without an explicit handle.
  • this = current object; super = immediate base-class portion of the current object.

📚 Next up — Module 4: Randomization & Constraints (rand, randc, constraint blocks, randomize(), pre/post_randomize, and constraint solving order)

↑ Back to Table of Contents

Comments

Popular posts from this blog

Module 1 — SystemVerilog Basics

VLSI Verification  •  SystemVerilog Series Module 1 — SystemVerilog Basics A complete, example-driven walkthrough of SystemVerilog fundamentals: data types, variables vs nets, arrays of every flavor, strings, enums, structs, unions, and typedef — the building blocks every verification engineer needs before UVM. 🧩 Beginner → Intermediate ⏱ ~25 min read 💻 Code Examples Included 📑 Table of Contents Introduction to SystemVerilog Verilog vs SystemVerilog Data Types: 2-State vs 4-State Logic Variables and Nets Arrays: Packed vs Unpacked Dynamic Arrays Queues Associative Arrays Strings Enumerations Structures Unions Typedef 1 Introduction to SystemVerilog SystemVerilog (IEEE 1800) is a unified hardware description and verification language (HDVL). It extends Verilog (IEEE 1364) with powerful constructs for design (interfaces, always_comb/always_ff, enhanced d...

Clock Domain Crossing (CDC): A Complete Beginner's Guide to Metastability, Synchronizers & Safe Data Transfer

Clock Domain Crossing (CDC): A Complete Beginner's Guide to Metastability, Synchronizers & Safe Data Transfer Modern System-on-Chip (SoC) designs rarely operate with a single clock. Instead, different modules such as CPUs, GPUs, memory controllers, communication interfaces, and peripherals often run at different clock frequencies or even use completely independent clock sources . When data moves from one clock domain to another, timing uncertainty can cause serious problems such as metastability , data corruption , and unpredictable system behaviour . This challenge is known as Clock Domain Crossing (CDC) . CDC is one of the most important topics in ASIC Design , RTL Design , ASIC Verification , FPGA Design , and SoC Architecture . Every modern chip—from smartphones to automotive processors—uses CDC techniques to ensure reliable communication between multiple clock domains. In this article, we'll explore CDC fundamentals, metastability, synchronization techniques, asynchro...

GPU Memory System & Interconnect: How Data Travels Inside a Modern GPU

GPU Memory System & Interconnect: How Data Travels Inside a Modern GPU Modern GPUs deliver incredible computing power by processing thousands of operations simultaneously. While compute cores often receive the spotlight, the real performance of a GPU depends heavily on how efficiently data moves between memory and compute units. A GPU memory system is designed to provide extremely high bandwidth while keeping latency as low as possible. This is achieved through a collection of specialized hardware components working together, including the Memory Controller, HBM/GDDR memory, Crossbar, Network-on-Chip (NoC), DMA Engine, PCIe Interface, and NVLink. In this article, we'll explore each component in detail and understand how they collectively enable high-performance parallel computing. Why Does GPU Memory Matter? Every GPU application—from gaming and graphics rendering to AI model training and scientific simulations—requires continuous movement of enormous amounts of data. For examp...

AXI Verification Interview Questions and Real-World Debugging

AMBA Protocol Series · Part 10 of 10 AXI Verification Interview Questions and Real-World Debugging Common protocol violations, how to read them off a waveform, and a curated set of interview questions with concise, correct answers — the capstone of the core 10-day series. AMBA Protocol Series: From APB & AHB to AXI — A Complete Guide for VLSI Engineers This wraps up the core 10-day series. Everything here draws directly on Days 1–9 — if any answer below feels unfamiliar, that's a good signal for which earlier post to revisit. (If you want to go further, see the 20-day extended list at the end of this post.) In This Article Common Protocol Violations Debugging From a Waveform Interview Questions and Answers Series Recap Extending the Series 1. Common Protocol Violations VALID deasserted before handshake — a source drops VALID before READY ever went high (violates the Day 8 stabi...

Timing Basics in Static Timing Analysis (STA): Delay Models, Sequential Timing, and Wire Delays

Timing Basics in Static Timing Analysis (STA) Static Timing Analysis (STA) is one of the most important verification techniques used in modern VLSI design. Before a chip is manufactured, engineers must verify that every signal reaches its destination within the required clock period. Even if a design passes functional simulation, it may still fail after fabrication because of timing violations. Unlike simulation, Static Timing Analysis mathematically analyzes every timing path without applying input test vectors. This makes STA significantly faster and capable of analyzing millions of timing paths efficiently. Table of Contents What is Static Timing Analysis? Why STA is Required Propagation Delay Contamination Delay Cell Delay Models Combinational Gate Delay Sequential Timing Setup Time Hold Time Wire Delay RC Delay Model PVT Corners Worked Examples Interview Questions Key Takeaways What is Static Timing Analysis? Static Ti...

Cache Basics and Cache Mapping Techniques

  Cache Basics and Cache Mapping Techniques  Modern processors execute billions of instructions every second. However, processor speed has increased much faster than main memory (RAM) speed. This difference, known as the memory wall , creates a performance bottleneck because the CPU often has to wait for data from slower memory. To solve this problem, computer systems use cache memory —a small, high-speed memory located between the CPU and the main memory. In this article, we will understand the fundamentals of cache memory, the principle of locality, cache organization, and the three major cache mapping techniques. What is Cache Memory? Cache memory is a small, high-speed memory that stores copies of frequently accessed data from the main memory. Since accessing cache is much faster than accessing RAM, the CPU first checks whether the required data is available in the cache. If the data is found in the cache, it is called a cache hit . Otherwise, the CPU fetches the dat...

CMOS Power & Scaling Explained: Dynamic Power, Leakage Power, Short-Circuit Power & Technology Scaling

  CMOS Power & Scaling Explained: Dynamic Power, Leakage Power, Short-Circuit Power & Technology Scaling Day 6 | CMOS Design Fundamentals | The Silicon Stack As integrated circuits continue to become smaller, faster, and more powerful, power consumption has emerged as one of the biggest challenges in modern VLSI design. While early digital circuits primarily focused on functionality and speed, today's processors, AI accelerators, smartphones, and IoT devices must deliver high performance while operating within strict power and thermal limits. A modern System-on-Chip (SoC) may contain billions of CMOS transistors , each switching billions of times per second. Even a tiny amount of power consumed by each transistor adds up to significant energy consumption across the entire chip. Excessive power not only drains batteries but also generates heat, reduces reliability, and limits performance. Understanding where CMOS power comes from and how it can be reduced is therefore ...