2. Introduction
SystemVerilog provides an object-oriented class data abstraction.
- Objects can be dynamically created, deleted, assigned and accessed by
object handles
- Object handles provide a pointer-like mechanism
- OOP introduces the notion of a class, which is a collection of data and methods that is
dependent on object activity
4. class
● A Class defines a collection of Data members.
● Class contains variables referred as Class Properties.
● Classes contains Subroutines (Tasks/Functions) referred as Class Methods.
● Both Properties and Methods are treated as Members of the Class.
● Classes are constructed Dynamically to create Class Objects.
● A Class Variable stores Class Handle.
● A Class Objects are accessed via Class Handles.
● Class declaration does not occupy any memory, it only creates a new type.
5. Class Example
Class packet;
//properties
int a;
int pay;
//Methods
function new();
Return pay;
endfunction
endclass
Class packet;
//properties
int a;
int pay;
//Methods
function new(int a); //passing arguments
a =a; or this.a = a;
endfunction
endclass
6. Object Creation
● Every class has a built-in method called new()
which is must to call to create an object of that
class type.
● The new() method is also known as the
“constructor”
● Constructing an object allocates the space in the
memory needed to hold the properties of an object
● We can also define arguments to pass to the
constructor
,module top;
Class A;
task display();
$display(“AB”);
endtask
endtask
Initial begin
A h1; //A h1 = new();
h1 = new(); //object creation or instance
h1.display();
end endmodule
7. Null Object Handles
● Uninitialized object handles are set
to null by default
● Uninitialized objects are detected by
comparing the handle to null
● Accessing a property of
uninitialized objects results in a
runtime error
Module top;
Class packet;
Int count;
Endclass
Initial begin
Packet pkt;
If (pkt == null)
$display(“packet handle ‘pkt’ is null”);
$display(“count=%0d”, pkt.count);
End endmodule
Output : packet handle ‘pkt’ is null
Count ncsim : *E, TRNULLID : NULL point dereference
8. Continued…
module top;
Class A;
virtual function void display(); //with virtual
$display(“AB”);
Endtask
Endclass
Class B extends A;
task display();
$display(“CD”);
endtask endclass
Initial
Begin
A p;
B c;
c = new();
p = c;
c= null;
c.display();
p.display(); End
Endmodule
10. Super
● The super keyword is used within a derived class to refer to
overridden members of parent class
11. Continued…
module top;
Class A;
int i;
function new(int i);
this.i = 20;
Endfunction endclass
Class B extends A;
int i;
function new(int i);
super.new(i);
this.i = i;
Endfunction endclass
initial begin
B b_h;
b_h = new(10);
$display(“%p”, b_h); p->20 c-> 10
end
endmodule
O/P : ?
12. Continued…
module top;
Class A;
int i;
function new(int i);
i = 20; //this.i = 20;
Endfunction endclass
Class B extends A;
int i;
function new(int i);
super.new(i);
this.i = i;
Endfunction endclass
initial begin
B b_h;
b_h = new(10);
$display(“%p”, b_h);
end
endmodule
O/P : ?
13. Continued…
module top;
Class A;
task display();
$display(“AB”);
Endtask
Endclass
Class B extends A;
task display();
$display(“CD”);
endtask endclass
Initial
Begin
A p;
B c;
p = new();
c = new();
p.display(); -> AB
c.display(); -> CD
End
Endmodule O/P ?
14. Continued…
module top;
Class A;
int i;
function new(int i=10);
this.i = i;
Endfunction endclass
Class B extends A;
int i;
function new();
i = 20;
Endfunction endclass
initial begin
B b_h;
b_h = new();
$display(“%p”, b_h);
end
endmodule
O/P : ?
15. Continued…
module top;
Class A;
int i;
function new(int i=10);
i = 10;
Endfunction endclass
Class B extends A;
int i;
function new();
i = 20;
Endfunction endclass
initial begin
B b_h;
b_h = new();
$display(“%p”, b_h);
end
Endmodule
O/P : ?
16. Polymorphism
A) It is an object-oriented programming language feature that allows a specific routine to use variables of
different types at different times
B) It is based on the concept that a base class pointer can be used to reference any of the derived class objects
Rules :
1. Parent and child methods should have the same name
2. Have to declare parent method as virtual, For child is not mandatory.
3. Have to do the assignment like p=c ((mandatory)
NOTE : Without assignment It cannot override the child methods and it will varry with the with & without
virtual keyword
17. Continued…
Module top;
Class A;
Virtual task display();
$display(“AB”);
Endtask
Endclass
Class B extends A;
Task display();
$display(“CD”);
Endtask
endclass
Initial
Begin
A p;
B c;
p = new();
c = new();
p = c;
with virtual
p.display(); -> CD
c.display(); -> CD
End
Endmodule
18. Continued…
(a) Without virtual
h1 = new(); h2 = new();
h1.display(); h2.display();
O/P ?
(a) With virtual
h1 = new(); h2 = new();
h1.display(); h2.display();
O/P ?
C Without virtual
h1 = new(); h2 = new();
h1 = h2;
h1.display(); h2.display();
O/P ?
D With virtual
h1 = new(); h2 = new();
h1 = h2;
h1.display(); h2.display();
O/P ?
19. Continued…
Module top;
Class A;
Virtual task display();
$display(“AB”);
Endtask
Endclass
Class B extends A;
Task display();
// super.new();
$display(“CD”);
Endtask endclass
Initial
Begin
A p;
B c;
p = new();
c = new();
p = c;
p.display();
c.display();
End
Endmodule
20. Continued…
(a) Without virtual
h1 = new(); h2 = new();
h1.display(); h2.display();
O/P ?
(a) With virtual
h1 = new(); h2 = new();
h1.display(); h2.display();
O/P ?
C Without virtual
h1 = new(); h2 = new();
h1 = h2;
h1.display(); h2.display();
O/P ?
D With virtual
h1 = new(); h2 = new();
h1 = h2;
h1.display(); h2.display();
O/P ?
21. Continued…
(a) Without virtual
super.new(); //In child class
h1 = new(); h2 = new();
h1.display(); h2.display();
O/P ?
(a) With virtual
super.new(); //In child class
h1 = new(); h2 = new();
h1.display(); h2.display();
O/P ?
C Without virtual
super.new(); //In child class
h1 = new(); h2 = new();
h1 = h2;
h1.display(); h2.display();
O/P ?
D With virtual
super.new(); //In child class
h1 = new(); h2 = new();
h1 = h2;
h1.display(); h2.display();
O/P ?
22. Continued...
● You have a class
- Decalred handles like p1 to p100
- Created instances for p1 to p5
- Now you have to calculate the how many instances are created without
counting
- Either implement a logic to calculate the how many instances created or is
there any other way to find ?