Chap 5 Basic OOP
Chap 5 Basic OOP
Why OOP
OOP terminology
Classes
Objects
Custom constructors
Static variables/methods
Classes within classes
Handles
Copying objects
5.1 Introduction
Why OOP?
Gains in productivity/maintainability/thoroughness
Strongly couples data with code that manipulates it.
Allows the testbench to be reused
Facilitates testing at the transaction level
SystemVerilog allows inheritance and polymorphism
Scenario
Functional
Command
Signal
Environment
Generator
Agent
Scoreboard
Checker
Driver
Assertions
Monitor
DUT
A complete house
House Address
Object
Class
Methods
Handle
Light switches
Properties
Dr. Meeta Yadiv, ASIC Verification, North Carolina State University Fall 2007
A complete
house
Address of a
house
Light
switches
Turn on/off
switches
Class
Programming element containing related groups of features
and functionality
Provides a template for building objects
Can be used as a data structure
Object
An object is an instance of a class
Handle
Type-safe pointer to an object can not be corrupted
Properties
Variables contained in the instance of the class
Methods
Tasks/functions (algorithms) that operate on the properties in
this instance of the class
Dr. Meeta Yadiv, ASIC Verification, North Carolina State University Fall 2007
OOP
Verilog
SystemVerilog
Block definition
module
class
Block instance
instance
object
Block name
instance name
handle
Data Types
Executable Code
Communication
between blocks
Properties:
Variables
behavioral blocks
Methods: tasks
(always, initial), and functions
tasks, functions
Ports or crosstask/function
module task calls
calls,
mailboxes,
semaphores,
etc.
Dr. Meeta Yadiv, ASIC Verification, North Carolina State University Fall 2007
properties or variables
method
method
endclass: Transaction
handle
class
creates an object of class Transaction
tr now points to the object
OR
Transaction tr = new();
Class in a package
package abc;
class Transaction;
// Class body
endclass
endpackage
program automatic test;
import abc::*;
Transaction tr;
// Test code
endprogram
10
Class Exercise
Create a class called MemTrans that contains the following
members:
An 8-bit data_in of logic type
A 4-bit address of logic type
A void function that prints out the value of data_in and
address
11
12
13
tr1
tr2
tr3
tr4
=
=
=
=
new(10);
new(, 6);
new(10, 6);
new(.a(10), .d(6));
Chapter 5 Copyright 2012 G. Tumbush, C. Spear v1.2
14
15
t1 = new();
t1 = null, t2=null
t2=null, t1
t2
t2
Object A
t1
Object A
t2
t1 = null;
Object A
Chapter 5 Copyright 2012 G. Tumbush, C. Spear v1.2
Object B
t1 = null
Object B
16
17
18
19
20
21
1) Transaction::count = 0d0
First id = 0d0, count = 0d1
Second id = 0d1, count = 0d2
2) Transaction::count = 0d2
Chapter 5 Copyright 2012 G. Tumbush, C. Spear v1.2
22
23
initial begin
Transaction::cfg = new(.num_trans(42));
end
24
25
class Transaction;
static Config cfg;
static int count = 0;
int id;
Config cfg;
initial begin
cfg = new(.num_trans(42));
Transaction::cfg = cfg;
Transaction::display_statics(); // Static method call
end
endprogram : top
26
27
28
29
class Scoping;
string oname;
function new(string new_oname);
oname = new_oname;
endfunction
endclass
30
31
32
class Statistics;
endclass
typedef class Statistics;
class Transaction;
Statistics stats;
endclass
class Statistics;
endclass
Chapter 5 Copyright 2012 G. Tumbush, C. Spear v1.2
33
gen
object of class
Transaction
Chapter 5 Copyright 2012 G. Tumbush, C. Spear v1.2
trans
35
36
37
38
endtask
39
src
id=0
stats
startT=42
src
id=0
stats
dst
id=0
stats
startT=42 96
dst.stats.StartT = 96;
Chapter 5 Copyright 2012 G. Tumbush, C. Spear v1.2
40
class Statistics;
time startT;
function Statistics copy();
copy = new();
copy.startT = startT;
endfunction
endclass
41
id=0
stats
src
id=0
stats
startT=42
dst
id=1
stats
startT=42
dst = src.copy();
src
id=0
stats
startT=42
dst
id=1
stats
startT=96
dst.stats.StartT = 96;
end
startT=42
42
Copy Exercise
For the following class create a copy function and demonstrate its
usage. Assume the Statistics class has its own copy function.
package automatic my_package;
class MemTrans;
bit [7:0] data_in;
bit [3:0] address;
Statistics stats;
function new();
data_in = 3;
address = 5;
stats = new();
endfunction
endclass;
endpackage
43
44
Environment
45