System Verilog
CLASSES
Introduction
System Verilog introduces an object-oriented class data
type.
Classes allow objects to be dynamically created, deleted,
assigned, and accessed via object handles.
A class is a type that includes data and subroutines
(functions and tasks) that operate on that data.
class’s data is referred to as class properties, and its
subroutines are called methods.
Futurewiz
www.futurewiz.co.in
Example1
class rectangle;
int length, width; //class properties
function int area(); //class method
return length * width;
endfunction
function int perimeter(); //class method
return 2*(length + width);
endfunction
endclass
Futurewiz
www.futurewiz.co.in
Example2
class person;
string name, address; //class properties
int number;
function void set_name(string user); //class method
name=user;
endfunction
endclass
Futurewiz
www.futurewiz.co.in
Example3
class packet;
bit [7:0] data; //class property
tast randomize(); //class method
data=$random;
endtask
task display();
$display(“data is %d”, data);
endtask
endclass
Futurewiz
www.futurewiz.co.in
Objects
A class defines a data type. An object is an instance of that
class.
An object is created by defining a handler of class type and
then calling new function which allocates memory to the
object.
packet p; //p is handler to class packet
initial
p=new(); //Object is constructed
If objects are not created then handler points to null.
Futurewiz
www.futurewiz.co.in
Default Constructor
new() is a default constructor which allocates memory and
initializes class variables for an object.
rectangle rec;
initial begin
rec=new; //memory allocated to length and width
int a, p;
rec.set_size(3, 5);
a=rec.area;
p=rec.perimeter; end
Futurewiz
www.futurewiz.co.in
Constructor
User can define customized constructers by writing there
own new function inside a class.
The new method is defined as a function with no return
type.
It is also possible to pass arguments to the constructor,
which allows run-time customization of an object.
function new (int x=0, y=0);
length=x;
width=y;
endfunction Futurewiz
www.futurewiz.co.in
Example
class rectangle; rectangle r1, r2, r3;
int lenght, width; int a1, a3, p1;
function new(int
x=1,y=1);..... initial begin
function int area(); r1=new(3, 5);
............. r2=new(4);
function int perimeter(); a1=r1.area;
.......... p1=r2.perimeter;
endclass
a3=r3.area; //error r3 is null
Result: a1=15 p1=10 end
Futurewiz
www.futurewiz.co.in
Parameterized Class
class packet #(number=10, type dtype= bit);
dtype data [number];
function void randomize1();
foreach(data[i]) data[i]=$random;
endfunction
function void display();
foreach(data[i]) $display(“data[%0d”]=%0d”, i, data[i]);
endfunction
endclass
Futurewiz
www.futurewiz.co.in
Parameterized Class
packet p1; //number=10, dtype=bit
packet #(20)p2; //number=20, dtype=bit
packet #( .dtype(int)) p3; //number=10, dtype=int
packet #(30, bit [3:0])p4; //number=30, dtype=bit [3:0]
initial begin
p1=new(); p2=new(); p3=new(); p4=new();
p4.display;
p4.randomize;
p4.display;
end
Futurewiz
www.futurewiz.co.in
This
The this keyword is used to unambiguously refer to class
properties or methods of the current instance.
The this keyword shall only be used within non-static
class methods, otherwise an error shall be issued.
class example;
Now a is property of class as well as
int a;
argument of function new.
function new(int a);
a=a; SV will look in local scope to resolve
endfunction reference to a, which in this case is
endclass subroutine argument.
Futurewiz
www.futurewiz.co.in
This
To solve this issue this keyword is used which now refers
to property a in current class instance.
class example; example x, y;
int a; initial begin
function new(int a); x=new(5);
this.a=a; y=new(3);
endfunction $display(x.a);
endclass $display(y.a);
end
Futurewiz
www.futurewiz.co.in
Fundamental Principles of OOP
Encapsulation
o It’s a concept that binds together the data and functions
that manipulate the data.
o Encapsulation keeps both data and function safe from
outside world i.e. data hiding.
Abstraction
o Abstraction is the concept of moving the focus from the
details and concrete implementation of things, to the
types of things, the operations available thus making
the programming simpler, more general.
Futurewiz
www.futurewiz.co.in
Fundamental Principles of OOP
Inheritance
o New classes are created by inheriting properties and
method defined in an existing class.
o Existing class is called the base class(parent class),
and the new class is referred to as
the derived class(child class).
Polymorphism
o polymorphism means having many forms.
o A member function will cause a different function to be
executed depending on the type of object that invokes
the function. Futurewiz
www.futurewiz.co.in
Inheritance
Inheritance allows user to create classes which are
derived from other classes.
The derived class (child class) inherits all the properties
and methods defined in base class (parent class).
Additional properties and methods can be defined in child
class.
properties and methods defined in base class can be
overridden by redefining them in child class. This
phenomenon is called as overriding.
Futurewiz
www.futurewiz.co.in
Example1
class parent; class child extends parent;
int a, b; int c;
task display(); task print();
$display(“Parent $display(“Child Class”);
Class”); endtask
endtask endclass
endclass
Futurewiz
www.futurewiz.co.in
Example1
parent p; parent p child c
child c; a=4 ; a=3 ;
initial begin b=0; b=0;
p=new; c=0;
c=new;
c.print;
Child Class
c.display;
Parent
c.a=3; Class
p.a=4;
end
Futurewiz
www.futurewiz.co.in
Example2
class parent; class child extends
int a, b; parent;
task display(); int a;
$display(“Parent task display();
Class”); $display(“Child Class”);
endtask endtask
endclass endclass
Display method and property a is overridden in child
class
Futurewiz
www.futurewiz.co.in
Example2
parent p;
parent p child c
child c;
a=2 ; a=7 ;
initial begin b=0; b=0;
p=new; c=0;
c=new;
c.display;
p.display; Child Class
Parent
c.a=7;
Class
p.a=2;
end
Futurewiz
www.futurewiz.co.in
Example3
A super keyword can be used to access properties and
methods defined in parent class from a child class.
class parent; class child extends
int a; parent;
task display(); int a, b;
$display(“Parent task display();
Class”); $display(“Child Class”);
endtask super.display;
endclass $display(super.a);
endtask
endclass Futurewiz
www.futurewiz.co.in
Example3
parent p;
parent p child c
child c;
a=5 ; a=6 ;
initial begin b=0;
p=new;
c=new;
p.a=5; Parent
Class
c.a=6;
Child Class
p.display; Parent
c.display; Class
end 0
Futurewiz
www.futurewiz.co.in
Inheritance
Every time when child object is created, constructer of
parent (super.new) is called first implicitly.
If a new function has been defined in parent which
accepts a set of arguments and arguments don’t have
default values. In such a case super.new has to be
explicitly specified with required arguments.
It is because of this reason that child class is able to
access properties and methods defined in parent class.
Futurewiz
www.futurewiz.co.in
Example4
class parent; class child extends
function new(); parent;
$display(“Parent function new();
Class”); $display(“Child Class”);
endfunction endfunction
endclass endclass
initial begin
Parent
child c;
Class
c=new; Child Class
end
Futurewiz
www.futurewiz.co.in
Example5
class parent; class child extends
function new(string parent;
str); function new();
$display(str); $display(“Child Class”);
endfunction endfunction
endclass endclass
initial begin
child c;
Error super.new is not
c=new;
called
end
Futurewiz
www.futurewiz.co.in
Example6
class parent; class child extends
function new(string str=“ parent;
”); function new();
$display(str); $display(“Child Class”);
endfunction endfunction
endclass endclass
initial begin
child c; Child Class
c=new;
end No error, parent constructor has default
value
Futurewiz
www.futurewiz.co.in
Example7
class parent; class child extends
function new(string parent;
str); function new();
$display(str); super.new(“Parent
endfunction Class”);
endclass $display(“Child Class”);
initial begin endfunction
child c; endclass
c=new; Parent
Class
end
Child Class
Futurewiz
www.futurewiz.co.in
Example8
class rectangle; class square extends
int length, width; rectangle;
int size;
function new(int x, y); function new(int size);
this.length=x; this.size=size;
this.width=y; super.new(size, size);
endfunction endfunction
function int area(int x, y);.... endclass
function int perimeter(int x, square sq=
y);... new(5);
sq.area;
endclass sq.perimeter;
Futurewiz
www.futurewiz.co.in
Encapsulation
Till now classes have members which were accessible to
rest of the program. However in many situation such
functionality are not desired.
Example: In cars we are not concerned by how engine
works but we focus on how to control it.
System Verilog provides various ways of hiding class
members:
o local keyword will ensure that the members are
available only to the method of the same class.
o protected keyword is similar to local keyword but
members can also be accessed by child class.Futurewiz
www.futurewiz.co.in
Example1
class rectangle; rectangle rec;
int length, width;
initial begin
function new(int x, y); rec=new(2, 3);
this.length=x; rec.area;
this.width=y; rec.length=5; //length is
endfunction modified
rec.area;
function int area; ….
endclass end
Futurewiz
www.futurewiz.co.in
Example2
class rectangle; rectangle rec;
local int length, width;
initial begin
function new(int x, y); rec=new(2, 3);
this.length=x; rec.area;
this.width=y; rec.length=5; //error
endfunction rec.area;
function int area; ….
end
endclass
Futurewiz
www.futurewiz.co.in
Example3
class rectangle; class square extends rectangle;
local int length, width; function new (int x);
function new(int x, y); super.new(x, x);
this.length=x; endfunction
this.width=y; endclass
endfunction
square sq;
function int area; …. initial sq=new(3);
endclass Error length and width are local to class
rectangle
Futurewiz
www.futurewiz.co.in
Example4
class rectangle; class square extends rectangle;
protected int length, width; function new (int x);
super.new(x, x);
function new(int x, y);
endfunction
this.length=x;
endclass
this.width=y;
endfunction square sq;
initial sq=new(3);
function int area; ….
endclass
Now length and width are accessible to both rectangle and
square Futurewiz
www.futurewiz.co.in
Lifetime in Class
By default properties and methods defined inside a class
have automatic lifetime.
Memory to properties are allocated dynamically when a
new instance of the class is created.
User can define properties and methods as static. A static
property is a class variable that is associated with the
class, rather than an instance of the class.
Futurewiz
www.futurewiz.co.in
Lifetime in Class
Memory to static properties and methods are allocated
during elaboration time.
Scope resolution operator ( :: ) can be used to access
static property and methods defined inside a class.
Static properties and methods can be accessed without
creating any instance of a class.
Futurewiz
www.futurewiz.co.in
Example1
packet p1, p2, p3;
class packet;
static int id; initial begin
int val; //default: automatic p1=new; $display(p1.id, p1.val);
p2=new; $display(p2.id, p2.val);
function new();
p3=new; $display(p3.id, p3.val);
id++;
p2.id=7; p2.val=3;
val++;
$display(p1.id, p1.val);
endfunction
$display(p2.id, p2.val);
endclass $display(packet :: id);
end
Futurewiz
www.futurewiz.co.in
Example1
Result :
p1.id= 1 p1.val=1
p2.id= 2 p2.val=1
p3.id= 3 p3.val=1
p1.id= 7 p1.val=1
p2.id= 7 p2.val=3
packet :: 7
Futurewiz
www.futurewiz.co.in
Example2
initial begin
class packet;
packet:: id=3;
static int id;
$display(packet::id);
int val; //default: automatic
packet p1;
function new(); p1=new;
id=id+1; $display(packet::id);
val=val+1; end
endfunction
Result
endclass id=3; id=4;
Futurewiz
www.futurewiz.co.in
Functions and Tasks
Task and Functions defined inside class have automatic
lifetime for their arguments and variables.
A static keyword can be added after Task/Function to
make arguments and variables static in nature.
A function prototype can be declared inside a class and
body can be defined outside class with help of extern
keyword.
Futurewiz
www.futurewiz.co.in
Example1
class test;
initial begin
task increment; test t1;
Result
int i; t1=new; :
i++; t1.increment; i=1
$display(“i=%d”, i); t1.increment; i=1
endtask t1.increment; i=1
end
endclass
Futurewiz
www.futurewiz.co.in
Example2
class test;
initial begin
task increment;
test t1;
static int x; Result:
t1=new;
int y; x=1 y=1
t1.increment;
x++; y++; x=2 y=1
t1.increment;
$display(“x=%d y=%d”, x, x=3 y=1
y); t1.increment;
endtask end
endclass
Futurewiz
www.futurewiz.co.in
Example3
class test;
task static increment; initial begin
int x; test t1; Result:
int y; t1=new; x=1 y=1
x++; y++; t1.increment; x=2 y=2
$display(“x=%d y=%d”, x, t1.increment; x=3 y=3
y); t1.increment;
endtask end
endclass
Futurewiz
www.futurewiz.co.in
Example4
class test;
initial begin
task static increment;
test t1; Result:
int x;
t1=new; x=1 y=1
automatic int y;
t1.increment; x=2 y=1
x++; y++;
t1.increment; x=3 y=1
$display(“x=%d y=%d”, x,
y); t1.increment;
endtask end
endclass
Futurewiz
www.futurewiz.co.in
Example5
function rectangle :: new(int
class rectangle; x, y);
local int length, width; this.length=x;
this.widht=y;
extern function new(int x, y);
endfunction
extern function int area();
endclass function int rectangle::area();
return length*width;
endfunction
Futurewiz
www.futurewiz.co.in
Functions and Tasks
Functions and Tasks can be local as well as protected.
Functions and Tasks can also be declared as static. The lifetime of
variables inside static methods are automatic by default.
Memory to static methods are allocated during elaboration time.
A static methods can only access static members of a class.
A static method can be called without creating instance of a class.
They can be accessed by scope resolution operator(::).
Futurewiz
www.futurewiz.co.in
Example1
class test;
int i;
initial begin
local function void increment; test t1;
i++; $display(“i=%0d”, i); t1=new; Result
endtask t1.inc; :
t1.inc; i=1
function void inc;
//t1.increment; will give
increment; i=2
//compilation error
endfunction
end
endclass
Futurewiz
www.futurewiz.co.in
Example2
class test;
initial begin
static function int add(int x, $display(test::add(3,2));
y); $display(test::add(1,1));
int i; end
i++;
$display(“i=%0d”, i); Result:
return x + y; 5 i=1
endfunction 2 i=1
endclass
Futurewiz
www.futurewiz.co.in
Example3
class test;
initial begin
int i;
$display(test::add(3,2));
static function int add(int x, $display(test::add(1,1));
y); end
i++;
$display(“i=%0d”, i);
return x + y; Result :
endfunction Error, Static function cannot access
non-static class properties
endclass
Futurewiz
www.futurewiz.co.in
Example4
class test;
initial begin
static int i;
$display(test::add(3,2));
static function int add(int x, $display(test::add(1,1));
y); end
i++;
$display(“i=%0d”, i); Result:
return x + y; 5 i=1
endfunction 2 i=2
endclass
Futurewiz
www.futurewiz.co.in