Skip to main content

Object-Oriented Programming in SystemVerilog

· loading · loading · ·
Course Verification SystemVerilog SystemVerilog OOP Object-Oriented Programming Verification Classes
Course Verification SystemVerilog
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
SystemVerilog Design Series - This article is part of a series.
Part 25: This Article

🧩 Object-Oriented Programming in SystemVerilog
#

SystemVerilog extends Verilog by adding object-oriented programming (OOP) features, making it easier to write reusable, scalable, and maintainable testbenches. OOP is especially valuable in verification environments where complex behaviors and test scenarios need to be modeled.


📌 Why Object-Oriented Programming?
#

Traditional Verilog was purely procedural, making it challenging to:

  • Reuse code effectively.
  • Manage complex verification components.
  • Implement advanced testbench architectures like UVM.

SystemVerilog’s OOP support bridges this gap, enabling verification engineers to:

  • Create reusable testbench components (drivers, monitors, scoreboards).
  • Leverage inheritance and polymorphism to build flexible architectures.
  • Encapsulate data and functionality for better maintainability.

🔍 Key OOP Concepts in SystemVerilog
#

📦 Classes
#

Classes are the cornerstone of OOP in SystemVerilog:

  • A class is a variable.
  • Define data (variables) and behavior (methods) together.
  • Can be instantiated dynamically using new().
  • Support encapsulation and reuse.

Example:

class Packet;
  rand bit [7:0] data;
  function void print();
    $display("Data = %0h", data);
  endfunction
endclass

🧬 Inheritance
#

SystemVerilog allows classes to inherit properties and methods from other classes:

  • Use the extends keyword.
  • Derived classes can override or extend base class behavior.

Example:

class BasePacket;
  bit valid;
endclass

class DataPacket extends BasePacket;
  bit [31:0] payload;
endclass

🔄 Polymorphism
#

Polymorphism allows a base class handle to reference derived class objects:

  • Supports flexible coding.
  • Enables dynamic method dispatch with virtual and override.

Example:

virtual class Transaction;
  virtual function void execute();
endclass

class ReadTransaction extends Transaction;
  function void execute();
    $display("Executing Read Transaction");
  endfunction
endclass

Transaction t;
t = new ReadTransaction();
t.execute(); // Executes derived class method

🔒 Encapsulation
#

Encapsulation groups data and behavior together, providing controlled access:

  • Use local and protected keywords to hide internal data from outside access.
  • Provides modularity and maintainability.

Example:

class SecurePacket;
  local bit [7:0] secretData;
  function void setData(bit [7:0] d);
    secretData = d;
  endfunction
  function bit [7:0] getData();
    return secretData;
  endfunction
endclass

🧩 What is a Class Object?
#

In SystemVerilog, a class object is a dynamically created instance of a class, constructed using the new() keyword. A class itself is just a template or blueprint; an object is the actual runtime entity created from that class.

Example:

class Packet;
  bit [7:0] data;
endclass

Packet pkt;
initial begin
  pkt = new(); // Creates a Packet object from the class
end

In this example, Packet is a class, and pkt is a class object.


🏷️ Class Variable or Type?
#

  • Packet is a class type, similar to how int or bit are types.
  • pkt is a class variable (a reference to a class object).

In summary:

  • Type: The class definition itself (Packet).
  • Variable: A handle that references a specific object (pkt).

📦 Class Variables and Methods
#

A class typically contains two key elements: 1️⃣ Variables (Properties): Hold object-specific data. 2️⃣ Methods: Define the object’s behaviors.

Example:

class Packet;
  // Class variable (property)
  bit [7:0] data;

  // Class method
  function void print();
    $display("Data = %0h", data);
  endfunction
endclass
  • data: An object-specific property.
  • print(): A method that operates on that object.

Each object maintains its own copy of the variables, and methods provide access to manipulate them.


🔄 Class vs. Object
#

ConceptDescription
ClassThe blueprint or definition of data and behavior.
ObjectA specific, runtime instance of the class in memory.

Example:

  • Packet ➔ Class (template).
  • pkt = new(); ➔ Object (instantiated at runtime).

🔑 Accessing Class Members
#

Use the dot operator (.) to access an object’s members.

Example:

pkt.data = 8'hAA; // Accessing the 'data' member
pkt.print();      // Calling the 'print' method

This enables direct and intuitive access to object-specific data and methods.


🚀 Best Practices
#

✅ Use classes to build reusable, modular testbench components. ✅ Use inheritance and polymorphism to build flexible architectures (e.g., UVM). ✅ Encapsulate data to prevent unintended access and improve maintainability. ✅ Document your class hierarchies and relationships.


📖 Conclusion
#

Object-oriented programming in SystemVerilog transforms testbench design by providing modularity, flexibility, and maintainability. Mastering these concepts is essential for building scalable and reusable verification environments.

// This simple testbench demonstrates OOP concepts in SystemVerilog
module tb_oop_demo;

  // Base class: Packet
  class Packet;
    rand bit [7:0] data;
    function void print();
      $display("[Packet] Data = %0h", data);
    endfunction
  endclass

  // Derived class: ExtendedPacket with additional field
  class ExtendedPacket extends Packet;
    rand bit [7:0] id;
    function void print();
      $display("[ExtendedPacket] ID = %0h, Data = %0h", id, data);
    endfunction
  endclass

  // Testbench body
  initial begin
    Packet p;
    ExtendedPacket ep;

    // Base class instance
    p = new();
    p.data = $urandom_range(0, 255);
    p.print();

    // Derived class instance
    ep = new();
    ep.data = $urandom_range(0, 255);
    ep.id = $urandom_range(0, 255);
    ep.print();

    // Polymorphism demonstration
    p = ep;  // base handle points to derived object
    p.print();  // calls derived print() method

    #10 $finish;
  end

endmodule

SystemVerilog Design Series - This article is part of a series.
Part 25: This Article

Related

Advanced OOP in SystemVerilog: Aggregation, Inheritance, and More
· loading · loading
Course Verification SystemVerilog SystemVerilog OOP Object-Oriented Programming Verification Classes
Course Verification SystemVerilog
Advanced OOP in SystemVerilog: Constructors, Handles, and Static Members
· loading · loading
Course Verification SystemVerilog SystemVerilog OOP Object-Oriented Programming Verification Classes
Course Verification SystemVerilog
Polymorphism and Virtuality in SystemVerilog
· loading · loading
Course Verification SystemVerilog SystemVerilog OOP Object-Oriented Programming Verification Virtuality
Course Verification SystemVerilog
Mailboxes in SystemVerilog
· loading · loading
Course Verification SystemVerilog SystemVerilog IPC Mailboxes Verification Synchronization
Course Verification SystemVerilog
Named Events in SystemVerilog
· loading · loading
Course Verification SystemVerilog SystemVerilog IPC Named Events Verification Synchronization
Course Verification SystemVerilog
Semaphores in SystemVerilog
· loading · loading
Course Verification SystemVerilog SystemVerilog IPC Semaphores Verification Synchronization
Course Verification SystemVerilog