Home / Learn Verilog / Classes (Testbench OOP)

Classes (Testbench OOP)

Objects for verification.

Quick summaryObjects for verification.

How to think about this

Class partsproperties: data fieldsmethods: functions/tasksconstructor: new()
Class parts

The basic idea

Classes bundle data and methods. They are used in testbenches (not in synthesizable RTL) to model transactions and components.

Why this comes up

Basis of UVM and constrained-random verification.

The mechanics

class ... endclass, new(), handles.

Where it is used

  • Everyday RTL design
  • FPGA and ASIC projects
  • Testbenches and debugging

Try it

A "transaction" with plain regsVerilog: edit and run

A class would hold addr, data, write together as one object.

Transaction class
class Packet;\n  rand bit [7:0] addr, data;\n  rand bit write;\n  function void show();\n    $display("%s addr=%h data=%h", write ? "WRITE":"READ", addr, data);\n  endfunction\nendclass\n\nPacket p = new();\ninitial begin p.randomize(); p.show(); end
Careful:This simulator runs the core of SystemVerilog (logic, always_ff, always_comb, enum, typedef, int, loops with int, break/continue, inside, immediate assert). Features shown only in the grey code box need a full simulator (Icarus -g2012, Verilator, Questa, VCS, Xcelium).

Quick check

1. Classes are mainly for...