|
|
|
|
|
|
|
|
|
|
|
|
Virtual Class
|
|
|
Like in C++, in Systemverilog 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 : virtual class
|
|
|
|
|
|
1 program class_virtual;
2 // Virtual class for body of any driver
3 virtual class verif;
4 // This starts all the threads
5 virtual task startSim();
6 endtask
7 // This stops all the threads
8 virtual task stopSim();
9 endtask
10 // This prints all the stats
11 virtual task printStats();
12 endtask
13 // This check if driver is done or not
14 virtual function bit isDone ();
15 begin
16 isDone = 0;
17 end
18 endfunction
19 // set the driver config
20 virtual task setConfig(integer item, integer value);
21 endtask
22 virtual function integer getConfig(integer item);
23 begin
24 getConfig = 32'hDEAD_BEAF;
25 end
26 endfunction
27 endclass
28 // ethernet inherits verif
29 class ethernet extends verif;
30 integer min_frame_size;
31 integer max_frame_size;
32 function new();
33 begin
34 min_frame_size = 32'h40;
35 max_frame_size = 32'h200;
36 end
37 endfunction
38 task startSim();
39 begin
40 $write("Starting Simulation\n");
41 end
42 endtask
43 task stopSim();
44 begin
45 $write("Stopping Simulation\n");
46 end
47 endtask
48 task printStats();
49 begin
50 $write("Sent normal frames %d\n",100);
51 $write("Sent runt frames %d\n",1);
52 $write("Sent oversize frames %d\n",1);
53 end
54 endtask
55 function bit isDone();
56 begin
57 isDone = 1;
58 end
59 endfunction
60 task setConfig(integer item, integer value);
61 begin
62 case(item)
63 0 : min_frame_size = value;
64 1 : max_frame_size = value;
65 endcase
66 end
67 endtask
68 function integer getConfig(integer item);
69 begin
70 case(item)
71 0 : getConfig = min_frame_size;
72 1 : getConfig = max_frame_size;
73 default : begin
74 $write("Calling super.setConfig\n");
75 getConfig = super.getConfig(item);
76 end
77 endcase
78 end
79 endfunction
80 endclass
81
82 class ethernet2 extends ethernet;
83 integer min_ipg;
84 function new();
85 begin
86 min_ipg = 32'hc;
87 end
88 endfunction
89 task setConfig(integer item, integer value);
90 begin
91 case(item)
92 2 : min_ipg = value;
93 default : begin
94 $write("Calling super.setConfig\n");
95 super.setConfig(item,value);
96 end
97 endcase
98 end
99 endtask
100 function integer getConfig(integer item);
101 begin
102 case(item)
103 2 : getConfig = min_ipg;
104 default : begin
105 $write("Calling super.setConfig\n");
106 getConfig = super.getConfig(item);
107 end
108 endcase
109 end
110 endfunction
111 endclass
112
113
114 initial begin
115 ethernet2 eth = new();
116 eth.setConfig(0,32'h100);
117 eth.setConfig(2,32'h24);
118 $write ("Value of min_frame is %0x\n", eth.getConfig(0));
119 $write ("Value of max_frame is %0x\n", eth.getConfig(1));
120 $write ("Value of min_ipg is %0x\n", eth.getConfig(2));
121 $write ("Value of unknown is %0x\n", eth.getConfig(3));
122
123 eth.startSim();
124 while (eth.isDone() == 0) begin
125 #1 ;
126 end
127 eth.stopSim();
128 eth.printStats();
129 end
130
131 endprogram
You could download file class_virtual.sv here
|
|
|
|
|
|
Simulation : virtual class
|
|
|
|
|
|
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
|
|