/ovm-2.0.1/src/base/ovm_factory.svh
SystemVerilog | 282 lines | 113 code | 61 blank | 108 comment | 2 complexity | fe466cc867ad051911e46ab6076b2c5a MD5 | raw file
Possible License(s): Apache-2.0
1// $Id: //dvt/vtech/dev/main/ovm/src/base/ovm_factory.svh#14 $ 2//---------------------------------------------------------------------- 3// Copyright 2007-2008 Mentor Graphics Corporation 4// Copyright 2007-2008 Cadence Design Systems, Inc. 5// All Rights Reserved Worldwide 6// 7// Licensed under the Apache License, Version 2.0 (the 8// "License"); you may not use this file except in 9// compliance with the License. You may obtain a copy of 10// the License at 11// 12// http://www.apache.org/licenses/LICENSE-2.0 13// 14// Unless required by applicable law or agreed to in 15// writing, software distributed under the License is 16// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 17// CONDITIONS OF ANY KIND, either express or implied. See 18// the License for the specific language governing 19// permissions and limitations under the License. 20//---------------------------------------------------------------------- 21 22`ifndef OVM_FACTORY_SVH 23`define OVM_FACTORY_SVH 24 25typedef class ovm_object; 26typedef class ovm_component; 27 28//------------------------------------------------------------------------------ 29// 30// CLASS: ovm_object_wrapper (OVM) 31// 32//------------------------------------------------------------------------------ 33 34// To register with a factory, a class must create a wrapper which implements 35// either the create_object (for generic data object) or create_component (for 36// hierarchical elements). 37 38virtual class ovm_object_wrapper; 39 40 virtual function ovm_object create_object (string name=""); 41 return null; 42 endfunction 43 44 virtual function ovm_component create_component (string name, 45 ovm_component parent); 46 return null; 47 endfunction 48 49 // Subtypes must specify the type that it creates 50 pure virtual function string get_type_name(); 51 52endclass 53 54 55//------------------------------------------------------------------------------ 56// 57// CLASS: ovm_factory_override 58// 59//------------------------------------------------------------------------------ 60 61class ovm_factory_override; 62 string full_inst_path; 63 string orig_type_name; 64 string ovrd_type_name; 65 ovm_object_wrapper orig_type; 66 ovm_object_wrapper ovrd_type; 67 function new (string full_inst_path="", 68 string orig_type_name="", 69 ovm_object_wrapper orig_type=null, 70 ovm_object_wrapper ovrd_type); 71 assert (ovrd_type != null); 72 this.full_inst_path= full_inst_path; 73 this.orig_type_name = orig_type == null ? orig_type_name : orig_type.get_type_name(); 74 this.orig_type = orig_type; 75 this.ovrd_type_name = ovrd_type.get_type_name(); 76 this.ovrd_type = ovrd_type; 77 endfunction 78endclass 79 80 81//------------------------------------------------------------------------------ 82// 83// CLASS: ovm_factory 84// 85//------------------------------------------------------------------------------ 86// 87// Type-based factory configuration (preferred) 88// 89// 90// String-based factory configuration 91// 92// set_type_override 93// 94// Configures the factory to produce an object of type 'override_type_name' 95// when a request is made using 'requested_type_name', which often is the 96// base type but can be any arbitrary string. 97// 98// set_type_override("driver", "my_driver") 99// 100// This says: "When a request for 'driver' is made, produce and return an 101// object whose type name is 'my_driver'." Overrides are recursive. That is: 102// 103// set_type_override("my_driver", "special_driver") 104// 105// This says: "When a request for 'driver' or 'my_driver' is made, produce 106// and return an object whose type name is 'special_driver'." 107// 108// This says: "When a request for 'driver' or 'my_driver' is made, produce 109// and return an object whose type name is 'special_driver'." 110// 111// set_inst_override 112// 113// Configures the factory to produce an object of type 'override_type_name' 114// when a request is made using 'requested_type_name' *and* 115// {parent_inst_path,".",name} 116// matches the 'inst_path' string. The 'inst_path' string allows overrides 117// to occur on an instance basis. It may contain wildcards so as to apply to 118// more than one context. The 'requested_type_name' is often the base type 119// but can be any arbitrary string. 120// 121// set_inst_override("top.a1.master", "some_monitor", "really_special_monitor") 122// set_inst_override("top.*.master", "some_monitor", "special_monitor") 123// set_type_override("some_monitor", "basic_monitor") 124// 125// This says: "When a request for 'some_monitor' is made, produce 126// 'really_special_monitor' when the context is 'top.a1.master', 127// 'special_monitor' when the context is 'top.*.master', and 'basic_monitor' 128// in every other context." Note how the order goes from most specific to 129// most general. That's important. A type override can be expressed as 130// an instance override. The following is equivalent to, although less 131// efficient than, the type override above: 132// 133// set_inst_override("*", "some_monitor", "basic_monitor") 134// 135// Because the requested_type_name does not necessarily represent the type 136// name of an actual class, the factory *must* be configured to map it to some 137// concreate type, else a run-time error will occur when a request is made using 138// 'some_monitor' as the original_type_name. 139// 140// 141// create_object 142// create_component 143// Methods for creating objects 144// 145// auto_register 146// Method for registering an object creator. This is called automatically 147// by the `ovm_*_utils macros 148// 149//------------------------------------------------------------------------------ 150 151class ovm_factory; 152 153 extern `_protected function new (); 154 155 extern static function ovm_factory get(); 156 157 158 extern function void set_inst_override_by_type(ovm_object_wrapper original_type, 159 ovm_object_wrapper override_type, 160 string full_inst_path); 161 162 extern function void set_type_override_by_type(ovm_object_wrapper original_type, 163 ovm_object_wrapper override_type, 164 bit replace=1); 165 166 extern function ovm_object create_object_by_type (ovm_object_wrapper requested_type, 167 string parent_inst_path="", 168 string name=""); 169 170 extern function ovm_component create_component_by_type(ovm_object_wrapper requested_type, 171 string parent_inst_path="", 172 string name, 173 ovm_component parent); 174 extern 175 function ovm_object_wrapper find_override_by_type (ovm_object_wrapper requested_type, 176 string full_inst_path); 177 178 extern function void debug_create_by_type (ovm_object_wrapper requested_type, 179 string parent_inst_path="", 180 string name=""); 181 182 183 extern function void set_inst_override_by_name(string original_type_name, 184 string override_type_name, 185 string full_inst_path); 186 187 extern function void set_type_override_by_name(string original_type_name, 188 string override_type_name, 189 bit replace=1); 190 191 extern function ovm_object create_object_by_name (string requested_type_name, 192 string parent_inst_path="", 193 string name=""); 194 195 extern function ovm_component create_component_by_name(string requested_type_name, 196 string parent_inst_path="", 197 string name, 198 ovm_component parent); 199 extern 200 function ovm_object_wrapper find_override_by_name (string requested_type_name, 201 string full_inst_path); 202 203 extern 204 function ovm_object_wrapper find_by_name (string type_name); 205 206 extern function void debug_create_by_name (string requested_type_name, 207 string parent_inst_path="", 208 string name=""); 209 210 211 extern function void print (int all_types=1); 212 213 214 extern function void register (ovm_object_wrapper obj); 215 216 217 218 //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ 219 220 // name-based static methods - deprecated 221 222 extern static function void set_type_override (string original_type_name, 223 string override_type_name, 224 bit replace=1); 225 226 extern static function void set_inst_override(string full_inst_path, 227 string original_type_name, 228 string override_type_name); 229 230 extern static function ovm_object create_object (string requested_type_name, 231 string parent_inst_path="", 232 string name=""); 233 234 extern static function ovm_component create_component (string requested_type_name, 235 string parent_inst_path="", 236 string name, 237 ovm_component parent); 238 239 240 extern static function void print_override_info(string requested_type_name, 241 string parent_inst_path="", 242 string name=""); 243 244 extern static function void print_all_overrides(int all_types=0); 245 246 extern static function void auto_register (ovm_object_wrapper obj); 247 248 249 //---------------------------------------------------------------------------- 250 // PRIVATE MEMBERS 251 252 extern protected function void m_debug_create (string requested_type_name, 253 ovm_object_wrapper requested_type, 254 string parent_inst_path, 255 string name); 256 257 extern protected function void m_debug_display (string requested_type_name, 258 ovm_object_wrapper result, 259 string full_inst_path); 260 static local ovm_factory m_inst; 261 262 protected bit m_types[ovm_object_wrapper]; 263 protected bit m_lookup_strs[string]; 264 protected ovm_object_wrapper m_type_names[string]; 265 266 protected ovm_factory_override m_type_overrides[$]; 267 protected ovm_factory_override m_inst_overrides[$]; 268 269 local ovm_factory_override m_override_info[$]; 270 local static bit m_debug_pass; 271 272endclass 273 274 275// our singleton factory; it is initialized upon first call to ovm_factory::get 276 277`const ovm_factory factory = ovm_factory::get(); 278 279 280 281`endif // OVM_FACTORY_SVH 282