Professional Documents
Culture Documents
System Verilog Classes
System Verilog Classes
System Verilog Classes
CLASSES
Introduction
Futurewiz
www.futurewiz.co.in
Example1
class rectangle;
int length, width; //class properties
Futurewiz
www.futurewiz.co.in
Example2
class person;
string name, address; //class properties
int number;
Futurewiz
www.futurewiz.co.in
Example3
class packet;
bit [7:0] data; //class property
task display();
$display(“data is %d”, data);
endtask
endclass
Futurewiz
www.futurewiz.co.in
Objects
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
initial begin
p1=new(); p2=new(); p3=new(); p4=new();
p4.display;
p4.randomize;
p4.display;
end
Futurewiz
www.futurewiz.co.in
This
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
Futurewiz
www.futurewiz.co.in
Example1
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
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
Futurewiz
www.futurewiz.co.in
Example4
Futurewiz
www.futurewiz.co.in
Example5
Futurewiz
www.futurewiz.co.in
Example6
endclass end
Futurewiz
www.futurewiz.co.in
Example2
Futurewiz
www.futurewiz.co.in
Example3
endclass
Now length and width are accessible to both rectangle and
square Futurewiz
www.futurewiz.co.in
Lifetime in Class
Futurewiz
www.futurewiz.co.in
Lifetime in Class
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
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;
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
Futurewiz
www.futurewiz.co.in
Functions and Tasks
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