|
|
|
|
|
|
|
|
|
|
|
|
Class Extensions
|
|
|
This section covers the following features of class. This are same as what we find in SystemVerilog/C++. If you have background in C++, then this is very simple. |
|
|
|
|
|
- Inheritance
- Polymorphism
- Overridden Members
- Virtual Class
|
|
|
|
|
|
Inheritance
|
|
|
Vera's OOP implementation provides the capability of inheriting from a base class and extending its capabilities within a subclass. This concept is called inheritance. When one inherits from a class into another, the original class definition is not changed, however the new subclass contains all the properties and methods of the base class and then can optionally add additional properties and methods. |
|
|
|
|
|
Polymorphism
|
|
|
Polymorphism allows the redefining of methods for derived classes while enforcing a common interface. To achieve polymorphism the 'virtual' identifier must be used when defining the base class and method(s) within that class. A virtual class is a class which serves as a template for the construction of derived classes. One cannot create an instance of a virtual class. |
|
|
|
|
|
|
|
|
|
|
|
Overridden Members
|
|
|
To override a method means that given a base class with a method, we can define a subclass which extends from that base class and then provide a new definition for the given method. By default, the subclass inherits the base class implementation of the method but should the programmer decide to change that definition by overriding it - simply listing a new version of that method, then the new version of the method will be used instead of the parent¿s. Unlike virtual methods, polymorphism does not apply, in other words should a base class handle be used to access the subclass, the base class implementation of the method will be called NOT the subclass implementation. Similar to method overriding, we can override properties of a class. When doing such the new definition of the property is valid for the subclass. |
|
|
|
|
|
super
|
|
|
The super keyword is used from within a derived class to refer to properties of the parent class. It is necessary to use super when the property of the derived class has been overridden, and cannot be accessed directly. |
|
|
|
|
|
Virtual Class
|
|
|
Like in C++, in Vera we can create virtual class for abstract class, this abstract class can contain abstract methods(virtual methods). Virtual methods provide prototypes for subroutines, all of the information generally found on the first line of a method declaration: the encapsulation criteria, the type and number of arguments, and the return type if it is needed. Later, when subclasses override virtual methods, they must follow the prototype exactly. Thus, all versions of the virtual method will look identical in all subclasses. |
|
|
|
|
|
Methods of normal classes can also be declared virtual. In this case, the method must have a body. If the method does have a body, then the class can be instantiated, as can its subclasses. However, if the subclass overrides the virtual method, then the new method must exactly match the superclass¿s prototype. |
|
|
|
|
|
We offen call this abstract virtual class as base objects. |
|
|
|
|
|
Code : class extension
|
|
|
|
|
|
1 // Virtual class for body of any driver
2 virtual class verif {
3 // This starts all the threads
4 virtual task startSim();
5 // This stops all the threads
6 virtual task stopSim();
7 // This prints all the stats
8 virtual task printStats();
9 // This check if driver is done or not
10 virtual function bit isDone () {
11 isDone = 0;
12 }
13 // set the driver config
14 virtual task setConfig(integer item, integer value);
15 virtual function integer getConfig(integer item) {
16 getConfig = 32'hDEAD_BEAF;
17 }
18 }
19 // ethernet inherits verif
20 class ethernet extends verif {
21 integer min_frame_size;
22 integer max_frame_size;
23 task new() {
24 min_frame_size = 32'h40;
25 max_frame_size = 32'h200;
26 }
27 task startSim() {
28 printf("Starting Simulation\n");
29 }
30 task stopSim() {
31 printf("Stopping Simulation\n");
32 }
33 task printStats() {
34 printf("Sent normal frames %d\n",100);
35 printf("Sent runt frames %d\n",1);
36 printf("Sent oversize frames %d\n",1);
37 }
38 function bit isDone() {
39 isDone = 1;
40 }
41 task setConfig(integer item, integer value) {
42 case(item) {
43 0 : min_frame_size = value;
44 1 : max_frame_size = value;
45 }
46 }
47 function integer getConfig(integer item) {
48 case(item) {
49 0 : getConfig = min_frame_size;
50 1 : getConfig = max_frame_size;
51 default : {
52 printf("Calling super.setConfig\n");
53 getConfig = super.getConfig(item);
54 }
55 }
56 }
57 }
58
59 class ethernet2 extends ethernet {
60 integer min_ipg;
61 task new() {
62 min_ipg = 32'hc;
63 }
64 task setConfig(integer item, integer value) {
65 case(item) {
66 2 : min_ipg = value;
67 default : {
68 printf("Calling super.setConfig\n");
69 super.setConfig(item,value);
70 }
71 }
72 }
73 function integer getConfig(integer item) {
74 case(item) {
75 2 : getConfig = min_ipg;
76 default : {
77 printf("Calling super.setConfig\n");
78 getConfig = super.getConfig(item);
79 }
80 }
81 }
82 }
83
84 program class_extension {
85 ethernet2 eth = new();
86 eth.setConfig(0,32'h100);
87 eth.setConfig(2,32'h24);
88 printf ("Value of min_frame is %0x\n", eth.getConfig(0));
89 printf ("Value of max_frame is %0x\n", eth.getConfig(1));
90 printf ("Value of min_ipg is %0x\n", eth.getConfig(2));
91 printf ("Value of unknown is %0x\n", eth.getConfig(3));
92
93 eth.startSim();
94 while (eth.isDone() == 0) {
95 delay(1);
96 }
97 eth.stopSim();
98 eth.printStats();
99 }
You could download file class_extension.vr here
|
|
|
|
|
|
Simulation : class extension
|
|
|
|
|
|
Calling super.setConfig
Calling super.setConfig
Value of min_frame is 100
Calling super.setConfig
Value of max_frame is 200
Value of min_ipg is 24
Calling super.setConfig
Calling super.setConfig
Value of unknown is deadbeaf
Starting Simulation
Stopping Simulation
Sent normal frames 100
Sent runt frames 1
Sent oversize frames 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|