EE / ECE students
Final-year and master's students preparing for VLSI internships and campus placements in design or verification.
Software engineers
Developers moving into hardware who want the foundations explained from first principles.
QA & test engineers
Test automation people heading into design verification — the same stimulus-and-check mindset applied to silicon.
FPGA & embedded engineers
Engineers who write some HDL already and want the rules and habits that verification teams expect.
RTL designers
Designers who want to move to verification and think like the person checking their code.
Career returners
Engineers refreshing fundamentals before interviews after time away from hands-on work.
Prerequisites
| You need | Level | Where to get it |
|---|---|---|
| SystemVerilog types, queues, mailboxes | Comfortable | Area 03 |
| Tasks, functions, automatic | Comfortable | Area 03 · Module 3 |
| Object-oriented programming (any language) | Helpful, not required | This area starts from zero |
Where this area sits in the chip flow — and the roles that use it
| Role | How you use this area on the job | Typical first title |
|---|---|---|
| Design verification (DV) | Write sequence items, drivers, monitors and scoreboards as classes; debug copy and override bugs | DV Engineer / Verification Engineer I |
| RTL design | Read class-based testbenches to reproduce failures | ASIC Design Engineer / RTL Engineer |
| FPGA | Build reusable class-based testbenches for IP blocks | FPGA Engineer |
| DFT | Model patterns and expected responses as objects | DFT Engineer |
| Post-silicon validation | Reuse verification classes in post-silicon test content | Validation Engineer |
- Write a transaction class with a constructor, methods and convert2string.
- Explain handles vs objects and avoid null-handle crashes.
- Implement deep copy and field-wise compare.
- Extend classes, override methods and call super.
- Use virtual methods for polymorphism and $cast safely.
- Use static members, parameterised classes and typedef class.
- Apply the blueprint pattern to change generated stimulus.
- Fix a shallow-copy bug and a missing-virtual bug in a real testbench.
| Tool | What it does | Used in this area for | Cost |
|---|---|---|---|
| Verilator (5.x) | Fast simulator that compiles Verilog/SystemVerilog to C++; also a strict linter | Every example and the lab (make) | Free |
| GTKWave or Surfer | Waveform viewers for .vcd / .fst dumps | Watching the byte stream into the checker | Free |
| VS Code + Verilog extension | Editor with syntax highlighting and lint on save | Writing code | Free |
| EDA Playground | Browser-based simulators, including commercial ones, after a free sign-up | Commercial simulators with full class support | Free |
| make and git | Run builds with one command; track your changes | Lab Makefile; saving your work | Free |
Commercial simulators you will meet at work
| Simulator | Vendor | Notes |
|---|---|---|
| VCS | Synopsys | Widely used for SystemVerilog/UVM regressions |
| Xcelium | Cadence | Successor of Incisive / NC-Sim |
| Questa | Siemens EDA | Successor of ModelSim; starter editions ship with FPGA tools |
Verilator 5 supports classes, inheritance, virtual methods, parameterised classes and mailboxes used here. Randomisation with constraints (Area 05) is where commercial simulators matter most.
Recommended: WSL 2 (Ubuntu inside Windows). You get the same commands as Linux and as most companies' compute farms.
- Turn on WSL and install UbuntuOpen PowerShell as Administrator, run the command, then restart.
wsl --install
After the restart an Ubuntu window opens: choose a user name and password. - Install the toolsIn the Ubuntu window:
sudo apt update sudo apt install -y iverilog verilator gtkwave make git
- Install VS Code and connect it to WSLInstall VS Code from code.visualstudio.com, add the WSL extension, then type
code .in Ubuntu to open the current folder. - Add Verilog supportInstall the Verilog-HDL/SystemVerilog/Bluespec SystemVerilog extension (publisher mshr-h).
- Check it works
iverilog -V | head -1 verilator --version
Install the native Icarus Verilog for Windows build (installer from bleyer.org/icarus, which also bundles GTKWave) and tick "Add executable folder to the user PATH". Use EDA Playground for Verilator.
- Install Apple's command-line tools
xcode-select --install
- Install HomebrewFollow the one-line command on brew.sh, then reopen Terminal.
- Install the simulators
brew install icarus-verilog verilator make git
- Install a waveform viewerDownload Surfer from surfer-project.org (native on Apple silicon), or try
brew install --cask gtkwave. - EditorInstall VS Code and the Verilog-HDL/SystemVerilog/Bluespec SystemVerilog extension.
- Check it works
iverilog -V | head -1 verilator --version
- Ubuntu / Debian
sudo apt update sudo apt install -y iverilog verilator gtkwave make git
- Fedora
sudo dnf install -y iverilog verilator gtkwave make git
- Arch
sudo pacman -S iverilog verilator gtkwave make git
- Check the versions
iverilog -V | head -1 # 11 or newer verilator --version # 5.x recommended
- Open EDA PlaygroundGo to edaplayground.com and create a free account (simulation needs a login).
- Paste the codeDesign on the right, testbench on the left.
- Choose a simulatorUnder Tools & Simulators pick a SystemVerilog-capable simulator (one of the commercial ones listed); tick Open EPWave after run for waveforms.
- RunPress Run; the log shows your
$displayoutput.
Queues, classes, mailboxes and fork timing need Verilator 5.0 or newer with --timing. Ubuntu 24.04 and current Homebrew ship 5.x; check with verilator --version. On an older system, use EDA Playground or build Verilator from source (verilator.org → Installation). Icarus Verilog does not support associative arrays or most class features.
- Make a folder and a file called
first.svclass packet; bit [7:0] addr; function new(bit [7:0] a = 0); addr = a; endfunction virtual function string kind(); return "packet"; endfunction endclass class bad_packet extends packet; function new(bit [7:0] a = 0); super.new(a); endfunction virtual function string kind(); return "bad_packet"; endfunction endclass module first; packet p1, p2, p3; bad_packet b; initial begin p1 = new(8'h10); p2 = p1; // same object p2.addr = 8'h20; $display("p1.addr = %h (changed through p2)", p1.addr); p3 = new p1; // shallow copy: a new object p3.addr = 8'h30; $display("p1.addr = %h, p3.addr = %h", p1.addr, p3.addr); b = new(8'h40); p1 = b; // parent handle, child object $display("p1.kind() = %s", p1.kind()); $finish; end endmodule
- Compile it
verilator --binary --timing first.sv
- Run it
./obj_dir/Vfirst
Expected output:p1.addr = 20 (changed through p2) p1.addr = 20, p3.addr = 30 p1.kind() = bad_packet - first.sv:26: Verilog $finish
- What to noticeLine 1:
p2 = p1made two names for one object. Line 2:new p1made a separate object. Line 3: apackethandle ran the child'skind()because it isvirtual.
| Session | Do this | Time |
|---|---|---|
| Day 1 | Install Verilator 5 · first class · Module 1 (classes, handles); handles playground | 4 h |
| Day 2 | Module 2 (shallow vs deep copy, clone, compare) | 3.5 h |
| Day 3 | Module 3 (inheritance, virtual, $cast, access control); virtual toggle | 4 h |
| Day 4 | Module 4 (static, parameterised classes, generator pattern) | 3 h |
| Day 5 | Lab: find and fix both class bugs · area test | 3.5 h |
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
A class is a blueprint
A class describes a kind of thing: what data it holds and what it can do. An object is one thing built from that description.
A house blueprint versus the houses built from it: one plan, many houses, each with its own furniture.
class Packet; bit [7:0] id; bit [7:0] len; function void show(); $display("id %0d len %0d", id, len); endfunction endclass
Every transaction, driver, monitor and scoreboard in UVM is a class.
Building an object with new()
Declaring a class variable doesn't build anything. Calling new() builds the object; a constructor sets its starting values.
Owning the blueprint doesn't give you a house — you have to build one.
class Packet; bit [7:0] id, len; function new(int i = 0); id = i; len = 4; endfunction endclass Packet p; // only a handle, no object yet p = new(7); // object built: id 7, len 4
The constructor new().
Forgetting new() is the number-one cause of “null object access” crashes.
Handles point to objects
A class variable is a handle: it points to an object. Copying a handle copies the pointer, not the object.
A TV remote: two remotes can control the same TV. A remote with no TV (null) does nothing — and pressing it crashes.
p1 = new(1);p2 = p1;— no new object.p2.len = 9;p1.lenis now 9 as well.
Shared handles are the root of the most confusing scoreboard bugs — Module 2 shows the fix.
Packet a = new(1); Packet b = a; b = new(2); b.len = 5; What is a.id and a.len?
b = new(2) points b at a new object, so a is untouched: a.id = 1, a.len = 4.this, packages and cleaning up
this means “my own” property when a name is ambiguous. Packages collect related classes so files can share them. Objects with no handle pointing at them are freed automatically.Saying “my bag” in a room full of bags; a shared toolbox for the team; rubbish collected when nobody wants it.
function new(int len); this.len = len; endfunction package pkt_pkg; class Packet; … endclass endpackage import pkt_pkg::*;
Lab 04 puts its classes in pkt_pkg.sv, the same way real testbenches are organised.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Class
- A user-defined type that bundles data (properties) and behaviour (methods).
- Object
- One instance of a class, created with
new(). - Handle
- A variable that refers to an object (like a pointer you cannot do arithmetic on).
nulluntil assigned. - Property / method
- A variable / function or task that belongs to the class.
- Constructor
- The special function
new()that initialises an object. - this
- Handle to the current object inside its own methods.
- Garbage collection
- Objects with no handles left are freed automatically.
- package
- A named scope that holds classes and types so every file shares them:
import pkg::*;
class packet; bit [7:0] addr; byte payload[$]; bit [7:0] csum; function new(bit [7:0] addr = 0); this.addr = addr; // this. separates the property from the argument endfunction function bit [7:0] calc_csum(); bit [7:0] s = addr; foreach (payload[i]) s ^= payload[i]; return s; endfunction endclass
Try it: handles and objects
| Code | What happens |
|---|---|
packet p; | A handle, value null — no object yet |
p = new(); | An object is built; p refers to it |
q = p; | Copies the handle: two names, one object |
p = null; | p lets go; the object lives while q refers to it |
p.calc_csum() with p null | Fatal null-handle error — the most common class crash |
Calling a method on a handle that was never constructed · thinking q = p copies data · comparing handles with == when you meant to compare contents · declaring classes inside each test file instead of one package.
Interview corner
Q. Handle vs object? A. The object is the data built by new(); the handle is a reference to it. Several handles can refer to one object.
Q. What happens to an object with no handles? A. It is garbage-collected automatically.
Key takeaways
- A class is a blueprint;
new()builds an object; a handle refers to it. - Assigning handles copies the reference, not the data.
- Put classes in a package and import it everywhere.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
Copying the handle versus copying the object
p2 = p1 copies the handle (same object). p2 = new p1 builds a new object with the same field values.Sharing a link to a document versus making your own copy of the file.
| Code | Objects | Change p2.len — does p1 change? |
|---|---|---|
p2 = p1; | 1 | yes |
p2 = new p1; | 2 | no |
Whenever a component keeps a transaction for later, it needs its own copy.
Shallow copy: nested objects are still shared
new p1 copies each field. If a field is itself a handle (to a header object, say), only that handle is copied — both packets still point to the same header.Photocopying a folder that contains a shortcut: both folders now hold a shortcut to the same photo.
Lab 04's planted bug is in packet::copy(): it shares the header instead of cloning it, so changing one packet's header silently changes another's.
Deep copy: copy everything
A deep copy also copies the nested objects. You write it yourself, usually as copy() or clone().
Copying the folder and the photo the shortcut points to.
function Packet clone(); clone = new this; // shallow copy of the fields clone.hdr = new this.hdr; // then copy the nested object endfunction
UVM gives every transaction copy(), clone() and compare(); you'll implement exactly this idea.
p2 = new p1; then p2.hdr.kind = 3. Does p1.hdr.kind change? And after p2 = p1.clone()?
With new p1 the header is shared → yes, p1 changes. With a deep clone() → no.
Why scoreboards need deep copies
A monitor often reuses one object, filling it with each new transaction. If the scoreboard keeps only the handle, the stored “expected” values change under it.
Writing every visitor's name on the same sticky note and filing the note — at the end every file shows the last visitor.
- Monitor fills
twith packet 1 and sends the handle. - Scoreboard stores the handle in its queue.
- Monitor overwrites
twith packet 2. - Stored “packet 1” now reads as packet 2 → false pass or false fail.
- Fix: store
t.clone().
This bug makes tests pass when the design is broken — the most dangerous kind.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Shallow copy
p2 = new p1: a new object with every field copied; handle fields still point at the same nested objects.- Deep copy
- A copy where nested objects are copied too, so nothing is shared.
- clone()
- Convention: a method that returns a new object that is a copy of this one.
- compare()
- Convention: a method that compares field values, not handles.
- convert2string()
- Convention: returns a printable summary; UVM uses the same name.
class header; bit [7:0] kind, seq; function header clone(); clone = new(); clone.kind = kind; clone.seq = seq; endfunction endclass class packet; header hdr; byte payload[$]; function new(); hdr = new(); endfunction function void copy(packet rhs); // deep payload = rhs.payload; // queue: copied by value hdr = rhs.hdr.clone(); // object: needs its own copy endfunction function bit compare(packet rhs); return hdr.kind == rhs.hdr.kind && hdr.seq == rhs.hdr.seq && payload == rhs.payload; endfunction endclass
| Field type | Shallow copy (new p) | Deep copy |
|---|---|---|
| bit, int, enum, string | value copied | value copied |
| queue, dynamic array | value copied | value copied |
| class handle | handle copied — shared object | object cloned |
| mailbox, event, semaphore handle | shared | usually shared on purpose |
The scoreboard keeps the expected transaction for a while. If it shares a nested object with something the generator keeps changing, the expected value changes underneath it — exactly the bug in this area's lab.
Copying a handle when you meant to copy the object · cloning only the top level of a nested structure · comparing with == on handles · forgetting to copy a field added later (keep copy and compare next to the field list).
Interview corner
Q. What does new p1 copy? A. Every property; handle properties are copied as handles, so nested objects are shared.
Q. How does UVM handle copying? A. copy() calls the do_copy() hook you implement — a deep copy written field by field.
Key takeaways
- Shallow copies share nested objects; deep copies don't.
- Write clone, copy and compare for every transaction class.
- The scoreboard's copy must be deep.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
extends: build on an existing class
A child class extends a parent: it gets all the parent's properties and methods, and can add or change some.
A smartphone is a phone with extra features: it can still make calls.
class BadPacket extends Packet; bit corrupt = 1; // added endclass
Error-injection tests are child classes of normal transactions — no copy-pasting.
super: call the parent's version
Inside a child, super. calls the parent's method, so you extend it instead of rewriting it.
Following the family recipe, then adding your own spice.
function void show(); super.show(); // id and len $display("corrupt %0d", corrupt); // plus the new field endfunction
super.new() when the parent's needs arguments.Keeps base behaviour in one place, so a fix there reaches every child.
virtual methods: the object decides
A parent handle can point to a child object. If the method is virtual, calling it runs the child's version. Without virtual, the handle's type decides.
A universal remote's “play” button: it does the right thing for whichever device it's pointed at.
Packet p = BadPacket_obj;— parent handle, child object.p.show();showisvirtual→ BadPacket's show runs ✓shownot virtual → Packet's show runs, and the corrupt field is never printed ✗
Lab 04 has a missing virtual on calc_csum(): a bad_csum_packet held in a packet handle still computes the good checksum, so the error test injects no error at all.
Packet's calc() is not virtual; BadPacket overrides it. With Packet p = new_bad;, which calc() runs for p.calc()?
Without virtual, the handle type decides → Packet's calc().
$cast: checked down-casting
Going from a parent handle back to a child handle needs $cast, which checks that the object really is that child.
Checking a parcel's label before signing for it.
BadPacket b; if (!$cast(b, p)) $error("not a BadPacket"); // returns 0 instead of crashing
$cast).UVM hands you parent-type handles everywhere; $cast is how you get the real type back safely.
local and protected
local members are visible only inside the class; protected also inside its children.A diary (local), a family album (protected), a newspaper (public).
| Keyword | Visible in the class | In children | Outside |
|---|---|---|---|
| (none) | yes | yes | yes |
protected | yes | yes | no |
local | yes | no | no |
Hiding internals stops tests from poking fields they shouldn't — fewer accidental dependencies.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Inheritance
class child extends parent: the child gets every property and method of the parent.- Override
- A child method with the same name and signature as a parent method.
- super
- Refers to the parent's members:
super.new(),super.calc_csum(). - virtual method
- A method whose call is resolved by the object's real type, not the handle's type.
- Polymorphism
- One handle type, many behaviours: a
packethandle can run a child's override. - Upcast / downcast
- Child handle → parent handle (always legal) / parent → child (checked with
$cast). - Abstract class
virtual class: cannot be constructed; children implement itspure virtualmethods.
class bad_csum_packet extends packet; function new(); super.new(); endfunction virtual function bit [7:0] calc_csum(); return super.calc_csum() ^ 8'h01; // wrong by one bit on purpose endfunction endclass
virtual in the base class, the handle's declared type wins and the override is silently ignored.Try it: virtual or not
Casting
packet p; bad_csum_packet b = new(); p = b; // upcast: always legal if (!$cast(b, p)) $error("p is not a bad_csum_packet"); // downcast: checked
Access control
| Keyword | Visible to | Use for |
|---|---|---|
| (none) | Everyone | Most testbench fields |
protected | The class and its children | Internals children may need |
local | This class only | True internals |
const | Everyone, set once | IDs, configuration fixed at construction |
Forgetting virtual on the base method · not calling super.new() when the parent constructor takes arguments · downcasting without checking $cast's result · changing an override's argument list (it no longer overrides).
Interview corner
Q. Why must base-class methods be virtual in UVM? A. So tests can swap in child classes (factory overrides) and the framework calls their versions through base handles.
Q. When does $cast fail? A. When the object isn't actually the target type or one of its descendants.
Key takeaways
- Children inherit everything and may override.
virtualmakes the object's real type choose the method.- Upcast freely; downcast only through a checked
$cast.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
static: one value shared by all objects
A static property belongs to the class, not to each object, so every object sees the same one.
A ticket dispenser at a deli counter: everyone takes the next number from the same machine.
class Packet; static int count = 0; int id; function new(); id = count++; endfunction // 0, 1, 2 … endclass
Unique IDs on every transaction make logs and scoreboards traceable.
Three Packets are created with the class above. What are their ids, and what is Packet::count?
ids 0, 1, 2; count = 3.
Classes with type parameters
A class can take a type as a parameter, so one piece of code works for ints, packets, or anything else.
One storage box design that comes in “for shoes” and “for books” versions.
class Stack #(type T = int); T items[$]; function void push(T x); items.push_back(x); endfunction function T pop(); return items.pop_back(); endfunction endclass Stack #(Packet) ps = new();
UVM's FIFOs, ports and sequencers are all parameterised by your transaction type.
The blueprint generator
A generator keeps one blueprint object. Each time it needs a new transaction it clones the blueprint, randomises the copy and sends the copy.
A cookie cutter: the same cutter, a fresh cookie each time.
blueprint = new();— or a child class, to change behaviour.- Loop:
t = blueprint.clone(); t.randomize(); mb.put(t); - Swap in
blueprint = BadPacketand every generated packet is a BadPacket — no generator code changes.
The blueprint pattern (the idea behind UVM's factory).
It shows why both deep copies and virtual methods matter: the lab's generator relies on both.
typedef class and extern
typedef class lets two classes refer to each other before both are defined. extern lets you declare a method in the class and write its body later.A table of contents that lists a chapter before you reach it.
typedef class Driver; // “Driver exists, details later” class Packet; extern function void show(); endclass function void Packet::show(); … endfunction
Keeps class declarations short and readable in big testbenches.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- static property
- One copy shared by every object of the class:
static int count; - static method
- Called on the class without an object:
packet::get_count(). - Parameterised class
- A class with type or value parameters:
class sb #(type T = packet); - typedef class
- Forward declaration so two classes can refer to each other.
- extern method
- Method declared in the class, body written outside with
packet::calc_csum. - Blueprint (template) pattern
- A generator copies a template object; swapping the template changes what is generated.
class packet; static int unsigned count; // shared by all packets int unsigned id; function new(); id = count++; endfunction endclass class scoreboard #(type T = packet); // parameterised by type T expected[$]; function void add(T t); expected.push_back(t); endfunction endclass scoreboard #(packet) sb = new();
The lab's testbench, as a picture
| Pre-UVM (this area) | UVM (Areas 08–12) |
|---|---|
| Transaction class | uvm_sequence_item |
| copy / compare / convert2string | do_copy / do_compare / convert2string |
| Generator + blueprint | Sequence + factory override |
| Mailbox | TLM FIFO / analysis port |
| Scoreboard queue | uvm_scoreboard with analysis imports |
Using a static counter for something that should be per-object · instantiating a parameterised class with the wrong type and casting around it · forgetting typedef class for mutually-referencing classes.
Interview corner
Q. What is the factory pattern in UVM, in OOP terms? A. A registry that constructs objects by type name, so a test can substitute a child class without editing the code that creates it.
Q. Why parameterise a class by type? A. One implementation (sequencer, FIFO, scoreboard) works for every transaction type.
Key takeaways
- Static members are per class, not per object.
- Type parameters make components reusable.
- The blueprint pattern is the factory idea in miniature.
After p2 = p1; p2.addr = 5; what is p1.addr?
What is a handle's value before new()?
null.Which fields does new p1 share with p1?
A base method isn't virtual and a child overrides it. Which runs through a base handle?
When does $cast(child, base) fail?
What does static int count mean in a class?
How do you change every generated packet without editing the generator?
In Lab 04, what fixes bug 1?
packet::copy clone the header instead of copying the handle.Explain shallow vs deep copy with an example.
new p shares the header; a deep copy clones it so changing one doesn't change the other.What is polymorphism and why does UVM rely on it?
Difference between local and protected?
What is an abstract class?
virtual class with pure virtual methods; it can't be constructed, only extended.Why put classes in a package?
Can you override a constructor?
new() and should call super.new() first.What does a parameterised class give you?
How would you debug a scoreboard reporting mismatches on every packet with the same sequence number?
| You see | Why | Fix |
|---|---|---|
| Fatal: null pointer / null handle | A method called before new() | Construct every handle; check with if (p == null) |
| Override never runs | Base method not virtual, or the signature differs | Add virtual in the base; match argument types exactly |
| Every scoreboard entry shows the same values | Shallow copy shares a nested object | Deep copy with clone() |
$cast error at run time | Object is not the target type | Check the factory/blueprint; use if (!$cast(...)) |
| Duplicate class declaration errors | Class defined in several files | Define it once in a package; import it |
| Resource | Why read it |
|---|---|
| Chris Spear & Greg Tumbush, SystemVerilog for Verification — OOP chapters | Classes, copying and polymorphism for testbenches |
| IEEE 1800 — Classes chapter | Exact rules for handles, virtual methods and casting |
| Accellera UVM 1.2 / IEEE 1800.2 User's Guide | Where these OOP patterns lead next |
| Verilator manual — class support | What the free simulator supports |
Lab 04 · Class-based packet generator and checksum checker
The DUT, csum_checker.v, is correct. Both bugs are in pkt_pkg.sv. tb_csum.sv has a generator, a driver and a scoreboard.
- Run
make: the scoreboard reports mismatches even on good packets — and every one shows the same sequence number. - Bug 1: find which field of
packet::copystill points at the template's object; fix it withheader::clone. - Re-run: good packets pass, but bad packets are no longer flagged.
- Bug 2: the generator calls
calc_csumthrough apackethandle — what must the base method be for the override to run? Fix and re-run.
Done when you see PASS: 200 packets, 40 bad checksums flagged.