/ovm-2.0.1/src/base/ovm_factory.svh

http://camlet.googlecode.com/ · SystemVerilog · 282 lines · 113 code · 61 blank · 108 comment · 2 complexity · fe466cc867ad051911e46ab6076b2c5a MD5 · raw file

  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. `ifndef OVM_FACTORY_SVH
  22. `define OVM_FACTORY_SVH
  23. typedef class ovm_object;
  24. typedef class ovm_component;
  25. //------------------------------------------------------------------------------
  26. //
  27. // CLASS: ovm_object_wrapper (OVM)
  28. //
  29. //------------------------------------------------------------------------------
  30. // To register with a factory, a class must create a wrapper which implements
  31. // either the create_object (for generic data object) or create_component (for
  32. // hierarchical elements).
  33. virtual class ovm_object_wrapper;
  34. virtual function ovm_object create_object (string name="");
  35. return null;
  36. endfunction
  37. virtual function ovm_component create_component (string name,
  38. ovm_component parent);
  39. return null;
  40. endfunction
  41. // Subtypes must specify the type that it creates
  42. pure virtual function string get_type_name();
  43. endclass
  44. //------------------------------------------------------------------------------
  45. //
  46. // CLASS: ovm_factory_override
  47. //
  48. //------------------------------------------------------------------------------
  49. class ovm_factory_override;
  50. string full_inst_path;
  51. string orig_type_name;
  52. string ovrd_type_name;
  53. ovm_object_wrapper orig_type;
  54. ovm_object_wrapper ovrd_type;
  55. function new (string full_inst_path="",
  56. string orig_type_name="",
  57. ovm_object_wrapper orig_type=null,
  58. ovm_object_wrapper ovrd_type);
  59. assert (ovrd_type != null);
  60. this.full_inst_path= full_inst_path;
  61. this.orig_type_name = orig_type == null ? orig_type_name : orig_type.get_type_name();
  62. this.orig_type = orig_type;
  63. this.ovrd_type_name = ovrd_type.get_type_name();
  64. this.ovrd_type = ovrd_type;
  65. endfunction
  66. endclass
  67. //------------------------------------------------------------------------------
  68. //
  69. // CLASS: ovm_factory
  70. //
  71. //------------------------------------------------------------------------------
  72. //
  73. // Type-based factory configuration (preferred)
  74. //
  75. //
  76. // String-based factory configuration
  77. //
  78. // set_type_override
  79. //
  80. // Configures the factory to produce an object of type 'override_type_name'
  81. // when a request is made using 'requested_type_name', which often is the
  82. // base type but can be any arbitrary string.
  83. //
  84. // set_type_override("driver", "my_driver")
  85. //
  86. // This says: "When a request for 'driver' is made, produce and return an
  87. // object whose type name is 'my_driver'." Overrides are recursive. That is:
  88. //
  89. // set_type_override("my_driver", "special_driver")
  90. //
  91. // This says: "When a request for 'driver' or 'my_driver' is made, produce
  92. // and return an object whose type name is 'special_driver'."
  93. //
  94. // This says: "When a request for 'driver' or 'my_driver' is made, produce
  95. // and return an object whose type name is 'special_driver'."
  96. //
  97. // set_inst_override
  98. //
  99. // Configures the factory to produce an object of type 'override_type_name'
  100. // when a request is made using 'requested_type_name' *and*
  101. // {parent_inst_path,".",name}
  102. // matches the 'inst_path' string. The 'inst_path' string allows overrides
  103. // to occur on an instance basis. It may contain wildcards so as to apply to
  104. // more than one context. The 'requested_type_name' is often the base type
  105. // but can be any arbitrary string.
  106. //
  107. // set_inst_override("top.a1.master", "some_monitor", "really_special_monitor")
  108. // set_inst_override("top.*.master", "some_monitor", "special_monitor")
  109. // set_type_override("some_monitor", "basic_monitor")
  110. //
  111. // This says: "When a request for 'some_monitor' is made, produce
  112. // 'really_special_monitor' when the context is 'top.a1.master',
  113. // 'special_monitor' when the context is 'top.*.master', and 'basic_monitor'
  114. // in every other context." Note how the order goes from most specific to
  115. // most general. That's important. A type override can be expressed as
  116. // an instance override. The following is equivalent to, although less
  117. // efficient than, the type override above:
  118. //
  119. // set_inst_override("*", "some_monitor", "basic_monitor")
  120. //
  121. // Because the requested_type_name does not necessarily represent the type
  122. // name of an actual class, the factory *must* be configured to map it to some
  123. // concreate type, else a run-time error will occur when a request is made using
  124. // 'some_monitor' as the original_type_name.
  125. //
  126. //
  127. // create_object
  128. // create_component
  129. // Methods for creating objects
  130. //
  131. // auto_register
  132. // Method for registering an object creator. This is called automatically
  133. // by the `ovm_*_utils macros
  134. //
  135. //------------------------------------------------------------------------------
  136. class ovm_factory;
  137. extern `_protected function new ();
  138. extern static function ovm_factory get();
  139. extern function void set_inst_override_by_type(ovm_object_wrapper original_type,
  140. ovm_object_wrapper override_type,
  141. string full_inst_path);
  142. extern function void set_type_override_by_type(ovm_object_wrapper original_type,
  143. ovm_object_wrapper override_type,
  144. bit replace=1);
  145. extern function ovm_object create_object_by_type (ovm_object_wrapper requested_type,
  146. string parent_inst_path="",
  147. string name="");
  148. extern function ovm_component create_component_by_type(ovm_object_wrapper requested_type,
  149. string parent_inst_path="",
  150. string name,
  151. ovm_component parent);
  152. extern
  153. function ovm_object_wrapper find_override_by_type (ovm_object_wrapper requested_type,
  154. string full_inst_path);
  155. extern function void debug_create_by_type (ovm_object_wrapper requested_type,
  156. string parent_inst_path="",
  157. string name="");
  158. extern function void set_inst_override_by_name(string original_type_name,
  159. string override_type_name,
  160. string full_inst_path);
  161. extern function void set_type_override_by_name(string original_type_name,
  162. string override_type_name,
  163. bit replace=1);
  164. extern function ovm_object create_object_by_name (string requested_type_name,
  165. string parent_inst_path="",
  166. string name="");
  167. extern function ovm_component create_component_by_name(string requested_type_name,
  168. string parent_inst_path="",
  169. string name,
  170. ovm_component parent);
  171. extern
  172. function ovm_object_wrapper find_override_by_name (string requested_type_name,
  173. string full_inst_path);
  174. extern
  175. function ovm_object_wrapper find_by_name (string type_name);
  176. extern function void debug_create_by_name (string requested_type_name,
  177. string parent_inst_path="",
  178. string name="");
  179. extern function void print (int all_types=1);
  180. extern function void register (ovm_object_wrapper obj);
  181. //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  182. // name-based static methods - deprecated
  183. extern static function void set_type_override (string original_type_name,
  184. string override_type_name,
  185. bit replace=1);
  186. extern static function void set_inst_override(string full_inst_path,
  187. string original_type_name,
  188. string override_type_name);
  189. extern static function ovm_object create_object (string requested_type_name,
  190. string parent_inst_path="",
  191. string name="");
  192. extern static function ovm_component create_component (string requested_type_name,
  193. string parent_inst_path="",
  194. string name,
  195. ovm_component parent);
  196. extern static function void print_override_info(string requested_type_name,
  197. string parent_inst_path="",
  198. string name="");
  199. extern static function void print_all_overrides(int all_types=0);
  200. extern static function void auto_register (ovm_object_wrapper obj);
  201. //----------------------------------------------------------------------------
  202. // PRIVATE MEMBERS
  203. extern protected function void m_debug_create (string requested_type_name,
  204. ovm_object_wrapper requested_type,
  205. string parent_inst_path,
  206. string name);
  207. extern protected function void m_debug_display (string requested_type_name,
  208. ovm_object_wrapper result,
  209. string full_inst_path);
  210. static local ovm_factory m_inst;
  211. protected bit m_types[ovm_object_wrapper];
  212. protected bit m_lookup_strs[string];
  213. protected ovm_object_wrapper m_type_names[string];
  214. protected ovm_factory_override m_type_overrides[$];
  215. protected ovm_factory_override m_inst_overrides[$];
  216. local ovm_factory_override m_override_info[$];
  217. local static bit m_debug_pass;
  218. endclass
  219. // our singleton factory; it is initialized upon first call to ovm_factory::get
  220. `const ovm_factory factory = ovm_factory::get();
  221. `endif // OVM_FACTORY_SVH