System Verilog Classes and Objects
A class is a blueprint or schematic which has its own behavior and works the same way for different instantiations. Any variable which is called or “instantiated” with the blueprint of this class is called an object of this class. Object Orientation revolves around classes and objects. It’s like solar system where the sun is OOPS and planets are class, objects, subclass, class variables, subclass variables etc.
Explaining by example – We all know about int command in C. The command is like
int x = 0 ; int y = 1 ;
So somewhere there is a class with definition “int” and x and y here are objects instantiated with “int” class. So each object has access to the properties of int and no interference takes place within the objects, i.e. here x and y do not have interference although both have the same class. It is like you and you friend buy different biscuits from the same shop. Now each class can have a subclass. This forms a hierarchy.
The basic class definition is like this:
class <class_name>;
///Properties
///Method
endclass: <class_name>
Now an object referring class here is like
<class_name> x = new ();
The new keyword is the built in command that instantiates an object of the class and allocates a memory block for the new object x irrespective of whether an object already exists or not. It allows the object created (here “X”) to access the properties of the class and is passed argument less by default.
Now Myclass x = new ; // instantiates an object named x. Constructor is called by compiler.
Myclass x = new( ); //No-argument Constructor called by the user.
Thus when the user explicitly calls the no-argument constructor the compiler doesn’t pose an issue or error here instead checks the class for any other similar constructor.
Here the new constructor is not returning any value however is returning the properties of the class to the object i.e. <class_name> as well as the address where the new object has been created.
NOTE- If you won’t define a constructor it gets created by the compiler.
For example: int x = new int( ) in Java. X gets access to the properties of int.
One can also define his/her own constructor. It won’t override the default as a constructor is not a method. It cannot be overridden.
An example in Model Sim
class myclass;
task abc();
$display("Hello World Class myclass has been incarnated");
endtask: abc
endclass: myclass
module main;
myclass my = new(); //my object created
initial begin
my.abc(); //my gets class properties thus calls abc
end
endmodule
The Output is :
Q. What if another new command is used with same object name ?
An object's memory can be deallocated by using the null command.
my = null;
In the above example we have used task in the class. Similarly functions can also be used inside class.
Classes can have their own variables. These variables are of local scope until declared as static inside the class. Each instance of this class gets a separate variable and storage.
An example of multiple objects with same class using class variables and functions
class myclass;
int a,b;
function abc();
$display("Result is ",a+b);
endfunction: abc
endclass: myclass
module main;
myclass my = new();
myclass my1 = new;
initial begin
my.a = 10;
my.b = 5;
my1.a = 6;
my1.b = 8;
my.abc();
my1.abc();
end
endmodule
Here is the Output:
Example with task as well as functions
class myclass;
int a,b;
function abc();
$display("Result of function is ",a+b);
endfunction: abc
task def;
$display("Result of task is",a-b);
endtask: def
endclass: myclass
module main;
myclass my = new();
myclass my1 = new;
initial begin
my.a = 10;
my.b = 5;
my1.a = 6;
my1.b = 8;
my.abc();
my1.def();
end
endmodule
Here is the Output:
Multiple Class declaration
System Verilog supports multiple class declaration too. Each class has their own separate instance which has further separate variables.
For example
class myclass;
int a,b;
function abc();
$display("Result of class A is ",a+b);
endfunction: abc
endclass: myclass
class yourclass;
int a,b;
task def;
$display("Result of class B is",a*b);
endtask:def
endclass:yourclass
module main;
myclass my = new();
yourclass my1 = new;
initial begin
my.a = 10;
my.b = 5;
my1.a = 6;
my1.b = 8;
my.abc();
my1.def();
end
endmodule
We have created two classes named myclass and yourclass. A and b here are the instances of the myclass and your class respectively.
The Output
System Verilog Nested Class
A class called within a class itself is called the nested class. The class which holds the nested class is the outer class.
class Outer;
int a,b;
function abc();
$display("Result of Outer is ",a+b);
endfunction: abc
endclass: Outer
class Inner;
task pain;
$display ("Result of inner class is",6/2);
endtask:pain
endclass:Inner
class Outer_2;
int a,b;
task def;
$display("Result of Outer_2 is",a*b);
endtask:def
Inner ok = new;
endclass:Outer_2
module main;
Outer my = new();
Outer_2 my1 = new;
initial begin
my.a = 10;
my.b = 5;
my1.a = 6;
my1.b = 8;
my.abc();
my1.def();
my1.ok.pain();
end
endmodule
The Output is :
Here class Outer_2 calls the class Inner and then "ok" object is created. Remember a class cannot instantiate a function and call a function i.e. if we place my1.ok.pain() just below the line Inner ok = new then it will throw an error. It is because only the outer module can call class task's and functions of any class including inner class as well. Try this in your code.
Another example
class Outer_inner;
task oi;
$display("Result of Outer_inner is ",24/2);
endtask:oi
endclass: Outer_inner
class Outer;
int a,b;
function abc();
$display("Result of Outer is ",a+b);
endfunction: abc
Outer_inner ox = new();
endclass: Outer
class Inner;
task pain;
$display ("Result of inner class is",6/2);
endtask:pain
endclass:Inner
class Outer_2;
int a,b;
task def;
$display("Result of Outer_2 is",a*b);
endtask:def
Inner ok = new;
endclass:Outer_2
module main;
Outer my = new();
Outer_2 my1 = new;
initial begin
my.a = 10;
my.b = 5;
my1.a = 6;
my1.b = 8;
my.abc();
my.ox.oi();
my1.def();
my1.ok.pain();
end
endmodule
The Output is:
Here the class Outer_inner is called inside the class Outer. It is very important to note that Outer_inner has no access to the properties and variables of Outer class.
Try replacing this $display("Result of Outer_inner is ",24/2);with this $display("Result of Outer_inner is ",a/2);and you will get an error because Outer_inner has no permission to access Outer variables.
No comments:
Post a Comment