System Verilog Classes
System Verilog Classes
CLASSES
ROHIT KHANNA
Introduction
class rectangle;
int lenght, width; //class properties
class person;
string name, address; //class properties
int number;
class packet;
bit [7:0] data; //class property
task display();
$display(“data is %d”, data);
endtask
endclass
System Verilog Class
Objects
rectangle rec;
rec=new; //memory allocated to length and width
initial begin
int a, p;
rec.set_size(3, 5);
a=rec.area;
p=rec.perimeter; end
System Verilog Class
Constructor
initial begin
p1=new(); p2=new(); p3=new(); p4=new();
p4.display;
p4.randomize;
p4.display;
end
System Verilog Class
This
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.
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.
The derived class (child class) inherits all the properties and
methods defined in base class (parent class).
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 Class
c.a=7;
p.a=2;
end
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
Child Class
c.a=6;
Parent Class
p.display; 5
c.display;
end
initial begin
Parent Class
child c;
Child Class
c=new;
end
initial begin
child c;
Error super.new is not called
c=new;
end
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
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++; $display(packet::id);
val=++val; end
endfunction
Result
endclass id=3; id=4;
class test;
initial begin
task increment; test t1;
Result:
int i; t1=new;
i=1
i++; t1.increment;
i=1
$display(“i=%d”, i); t1.increment;
i=1
endtask t1.increment;
end
endclass
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, y); x=3 y=1
t1.increment;
endtask
end
endclass
class test;
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
class test;
int i; initial begin
local function void increment; test t1;
Result:
i++; $display(“i=%0d”, i); t1=new;
i=1
endtask t1.inc;
i=2
t1.inc;
function void inc;
//t1.increment; will give
increment;
//compilation error
endfunction
end
endclass
System Verilog Class
Example2
class test;
initial begin
static function int add(int x, y); $display(test::add(3,2));
int i; $display(test::add(1,1));
i++; end
$display(“i=%0d”, i);
return x + y; Result:
endfunction 5 i=1
2 i=1
endclass
class test;
initial begin
int i;
$display(test::add(3,2));
static function int add(int x, y); $display(test::add(1,1));
i++; end
$display(“i=%0d”, i);
return x + y;
endfunction Result :
Error, Static function cannot access non-
endclass static class properties
class test;
initial begin
static int i;
$display(test::add(3,2));
static function int add(int x, y); $display(test::add(1,1));
i++; end
$display(“i=%0d”, i);
return x + y; Result:
endfunction 5 i=1
2 i=2
endclass
endclass
Result :
I am shape I don’t know perimeter
I am rectangle Perimeter= 10
I am triangle Perimeter= 6
I am triangle Perimeter= 6
I am square Perimeter= 16
I am rectangle Perimeter= 10
initial begin
parent p1; child c1;
c1=new;
$cast(p1, c1); // checks run-time casting errors
//p1=c1; //checks compile time casting errors
//Properties and virtual methods in parent class points to one
//defined in child class
p1.d1; p1.d2;
$display(“p1.a=%0d”, p1.a); c1.a=9;
$display(“p1.a=%0d”, p1.a);
end
System Verilog Class
Example2
The class exists simply as a base class from which other classes
can be derived.
endclass
endclass
endclass
class Node;
Node left, right;
//properties and methods for Node
endclass
class timestat;
time start_time, end_time;
class packet;
int data[7:0];
task packet :: transmit();
timestat t;
t.start;
function new; //do some operation
t=new; t.end;
endfunction endtask
endclass
This allows for the compiler to read a file from beginning to end
without concern for the positioning of the class declaration.
module test;
class packet;
timestat t; Compilation error class
//definitions timestat is not defined.
endclass
module test;
typedef class timestat;
class packet;
timestat t; typedef allows compiler to
//definitions process packet class before
endclass timestat class.
class timestat;
//definitions
enclass
endmodule
System Verilog Class
Copy
When using new to copy objects, the user define new constructer
is not called. New function just copies the value of variables and
object handle.
class pkt;
bit addr [15:0];
class timestat;
bit [7:0] data;
time start_time, end_time; int id; static int count;
endclass timestat t;
function new();
id=count++;
t=new;
endfunction
endclass
This user defined copy function should copy the content of class
handle, not handle itself.
class pkt;
bit addr [15:0];
bit [7:0] data;
int id; static int count; function pkt pkt :: copy;
copy=new;
timestat t; copy.addr=this.addr;
copy.data=this.data;
function new(); endfunction
id=count++;
t=new;
endfunction
extern function pkt copy;
endclass
System Verilog Class
Example
endclass
semaphore smp;
int got=0;
initial
initial begin begin
smp=new(5); #8 smp.get(2);
#5 smp.get(3); #7 smp.put(2);
#6 smp.get(1); got=got +1; end
#2 if(smp.try_get(3)) got=got +1; got=1 at 15
end
module test;
mailbox mb; //typeless Mailbox
string s, int i;
initial begin
mb=new(); //Unbound Mailbox
$monitor(“s=%s and i=%0d at time=%0d”, s, i, $time);
fork gen_data;
rec_data;
join end
endmodule
System Verilog Class
Example
task rec_data;
task gen_data;
#1 mb.peek(s);
mb.put(“Hello”);
#2 mb.get(s);
#3 mb.put(7);
#2 mb.get(i);
#4 mb.put(“Test”);
#1 mb.peek(s);
#3 mb.put(3);
#2 void’( mb.try_get(s));
#3 mb.put(“Hi”);
#1 void’(mb.try_get(i));
#2 mb.put(9);
#4 mb.get(s);
endtask
#2 mb.get(i);
endtask
Result:
# s= and i=0 at time=0
# s=Hello and i=0 at time=1
# s=Hello and i=7 at time=5
# s=Test and i=7 at time=7
# s=Test and i=3 at time=16
module test;
mailbox #(int) mb; //Parameterized Mailbox
int i;
initial begin
mb=new(3); //bound mailbox
$monitor(“i=%0d at %0d”, i ,$time);
fork gen_data;
rec_data;
join end
endmodule
System Verilog Class
Example
Result:
# i=0 at time=0
# i=1 at time=1
# i=7 at time=8
# i=4 at time=10
# i=3 at time=11
# i=2 at time=13
# i=5 at time=17