ovm_factory.sv

Go to the documentation of this file.
00001 // $Id: ovm__factory_8sv-source.html,v 1.1 2008/10/07 21:54:43 alex.marin Exp $
00002 //----------------------------------------------------------------------
00003 //   Copyright 2007-2008 Mentor Graphics Corporation
00004 //   Copyright 2007-2008 Cadence Design Systems, Inc.
00005 //   All Rights Reserved Worldwide
00006 //
00007 //   Licensed under the Apache License, Version 2.0 (the
00008 //   "License"); you may not use this file except in
00009 //   compliance with the License.  You may obtain a copy of
00010 //   the License at
00011 //
00012 //       http://www.apache.org/licenses/LICENSE-2.0
00013 //
00014 //   Unless required by applicable law or agreed to in
00015 //   writing, software distributed under the License is
00016 //   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
00017 //   CONDITIONS OF ANY KIND, either express or implied.  See
00018 //   the License for the specific language governing
00019 //   permissions and limitations under the License.
00020 //----------------------------------------------------------------------
00021 
00022 `include "base/ovm_factory.svh"
00023 `include "base/ovm_object.svh"
00024 
00025 
00026 // create
00027 // ------
00028 
00029 function ovm_object ovm_factory::create (string name="");
00030   ovm_factory f; f=new(name);
00031   return f; 
00032 endfunction
00033 
00034 // new
00035 // ---
00036 
00037 function ovm_factory::new (string name="");
00038     super.new (name);
00039     if(inst==null)
00040       inst = this;
00041 endfunction
00042 
00043 
00044 // auto_register (static)
00045 // -------------
00046 
00047 function void ovm_factory::auto_register (ovm_object_wrapper obj);
00048 
00049   if (inst==null) begin
00050     inst = new ("ovm_factory_inst");
00051   end
00052   inst.auto_register_type(obj);
00053 
00054 endfunction
00055 
00056 
00057 // auto_register_type
00058 // ------------------
00059 
00060 function void ovm_factory::auto_register_type (ovm_object_wrapper obj);
00061 
00062   if (types.exists(obj.get_type_name())) begin
00063     ovm_report_warning("TPRGED", $psprintf("Type '%0s' already registered with factory. Support for same type names in different scopes not implemented.", obj.get_type_name()));
00064     return;
00065   end
00066 
00067   types[obj.get_type_name()] = obj;
00068   
00069 endfunction
00070 
00071 // print_all_overrides
00072 // -------------------
00073 
00074 function void  ovm_factory::print_all_overrides (bit all_types=0);
00075   if(inst==null) inst = new;
00076   inst.m_print_all_overrides(all_types);
00077 endfunction
00078 
00079 function void  ovm_factory::m_print_all_overrides (bit all_types=0);
00080   ovm_factory_override t_ov[$];
00081   string no_ov[$];
00082   string key;
00083 
00084   if(type_overrides.first(key)) begin
00085     do begin
00086       t_ov.push_back(type_overrides[key]);
00087     end while(type_overrides.next(key));
00088   end
00089   if(all_types && types.first(key)) begin
00090     do begin
00091       if(!type_overrides.exists(key) && 
00092          !ovm_is_match("ovm_*", types[key].get_type_name()) )
00093       begin
00094         no_ov.push_back(types[key].get_type_name());
00095       end
00096     end while(types.next(key));
00097   end
00098 
00099   $display("#### Factory Overrides");
00100 
00101   if(!t_ov.size() && !inst_overrides.size()) begin
00102     $display("  No overrides exist in the factory");
00103   end
00104 
00105   if(inst_overrides.size()) begin
00106     string s;
00107     $display("Instance Overrides");
00108     $display("  Requested Type Name       Instance Path                       Override Type");
00109     $display("  -------------------------------------------------------------------------------------");
00110     for(int i=0; i<inst_overrides.size(); ++i) begin
00111       s = {"  ",inst_overrides[i].req_type_name};
00112       if(s.len() > 25) begin 
00113         s = s.substr(0,24);
00114         s[24] = "+";
00115       end
00116       else 
00117         for(int i=s.len(); i<=25; ++i) 
00118           s = {s," "};
00119       $write(s);
00120       s = {"  ",inst_overrides[i].inst_path};
00121       if(s.len() > 35) begin 
00122         s = s.substr(0,34);
00123         s[34] = "+";
00124       end
00125       else 
00126         for(int i=s.len(); i<=35; ++i) 
00127           s = {s," "};
00128       $write(s);
00129       s = {"  ", inst_overrides[i].type_name};
00130       if(s.len() > 25) begin 
00131         s = s.substr(0,24);
00132         s[24] = "+";
00133       end
00134       else 
00135         for(int i=s.len(); i<=25; ++i) 
00136           s = {s," "};
00137       $display(s);
00138     end
00139   end
00140   else if(t_ov.size()) begin
00141     $display("No Instance Overrides");
00142   end
00143   $display("");
00144   if(t_ov.size()) begin
00145     string s;
00146     $display("Type Overrides");
00147     $display("  Requested Type Name       Override Type");
00148     $display("  --------------------------------------------------");
00149     for(int i=0; i<t_ov.size(); ++i) begin
00150       s = {"  ",t_ov[i].req_type_name};
00151       if(s.len() > 25) begin 
00152         s = s.substr(0,24);
00153         s[24] = "+";
00154       end
00155       else 
00156         for(int i=s.len(); i<=25; ++i) 
00157           s = {s," "};
00158       $write(s);
00159       s = {"  ", t_ov[i].type_name};
00160       if(s.len() > 25) begin 
00161         s = s.substr(0,24);
00162         s[24] = "+";
00163       end
00164       else 
00165         for(int i=s.len(); i<=25; ++i) 
00166           s = {s," "};
00167       $display(s);
00168     end
00169   end
00170   if(no_ov.size()) begin
00171     $display("Additional Types in the factory (no type override)");
00172     $display("  Type Name");
00173     $display("  -------------------------");
00174     for(int i=0; i<no_ov.size(); ++i) begin
00175       $display("  ", no_ov[i]);
00176     end
00177   end
00178   $display("####");
00179 endfunction
00180 
00181 // print_override_info
00182 // -------------------
00183 
00184 function void  ovm_factory::print_override_info (string lookup_str,
00185                                                  string inst_path="",
00186                                                  string inst_name="");
00187   if(inst==null) inst = new;
00188   inst.m_print_override_info(lookup_str, inst_path, inst_name);
00189 endfunction
00190 
00191 function void  ovm_factory::m_print_override_info (string lookup_str,
00192                                                  string inst_path,
00193                                                  string inst_name);
00194  
00195   ovm_factory_override i_ov[$];
00196   ovm_factory_override t_ov;
00197   bit match;
00198   bit first_inst;
00199   
00200   for (int index=0; index < inst_overrides.size(); index++) begin
00201     if (inst_name != "") begin
00202       if (ovm_is_match(inst_overrides[index].inst_path,
00203                    {inst_path,".",inst_name}))
00204         i_ov.push_back(inst_overrides[index]);
00205     end
00206     else if (ovm_is_match(inst_overrides[index].inst_path, {inst_path})) begin
00207         i_ov.push_back(inst_overrides[index]);
00208     end
00209   end 
00210 
00211   t_ov = type_overrides[type_overrides[lookup_str].type_name];
00212 
00213   first_inst = 0;
00214 
00215   $display("#### Factory creation information");
00216   $display("   Type name (lookup string): ", lookup_str);
00217   $display("   Instance path            : ", inst_path);
00218   $display("   Instance name            : ", inst_name);
00219   for(int i=0; i<i_ov.size(); ++i) begin
00220     if (ovm_is_match(i_ov[i].req_type_name, lookup_str)) 
00221       match = 1; 
00222     else
00223       match = 0;
00224     $write("   Instance override");
00225     if(match==0) $write(" (ignored because required type does ",
00226                         "not match requested type for the instance path)\n");
00227     else if(first_inst==1) $write(" (disabled do to previous instance override)\n");
00228     else $write(" (enabled)\n");
00229     $display("      Required type   -- ", i_ov[i].req_type_name);
00230     $display("      Override inst   -- ", i_ov[i].inst_path);
00231     $display("      Override type   -- ", i_ov[i].type_name);
00232     if(match)
00233       first_inst = 1;
00234   end
00235   if(type_overrides[lookup_str].type_name != lookup_str) begin
00236     $write("   Type override");
00237     if(first_inst) $write(" (disabled do to instance override)\n");
00238     else $write(" (enabled)\n");
00239     $display("      Required type -- ", lookup_str);
00240     $display("      Override type -- ", type_overrides[lookup_str].type_name);
00241   end
00242   $write("   Base type requested: ", lookup_str);
00243   if(i_ov.size()) $write(" (disabled do to instance override)\n");
00244   else if(type_overrides[lookup_str].type_name != lookup_str)
00245     $write(" (disabled do to type override)\n");
00246   $display("####");
00247 endfunction
00248 
00249 
00250 // override_type
00251 // -------------
00252 
00253 function void ovm_factory::override_type (string lookup_str,
00254                                           string type_name,
00255                                           string inst_path,
00256                                           bit replace=1);
00257   
00258   ovm_factory_override tmp;
00259 
00260   // check that type is registered with the factory
00261   if (!types.exists(type_name)) begin
00262     if (inst_path == "")
00263       ovm_report_fatal("TYPNTF", $psprintf("Cannot register type override '%0s' because the type it's supposed to produce, '%0s', is not registered with the factory.", lookup_str, type_name));
00264     else
00265       ovm_report_fatal("TYPNTF", $psprintf("Cannot register instance override '%0s' for instance '%s' because the type it's supposed to produce, '%0s', is not registered with the factory.", lookup_str, inst_path, type_name));
00266     return;
00267   end
00268 
00269   // check if an instance override
00270   if (inst_path != "") begin
00271     tmp = new(inst_path, lookup_str, type_name);
00272     inst_overrides.push_back(tmp);
00273     return;
00274   end
00275 
00276   // check if the lookup string already exists
00277   if (type_overrides.exists(lookup_str)) begin
00278 
00279     if (replace==1) begin
00280       m_sc.scratch1 = type_overrides[lookup_str].type_name;
00281     end
00282     else if (replace==0) begin
00283       m_sc.scratch1 = type_overrides[lookup_str].type_name;
00284       ovm_report_warning("TPREGD", $psprintf("Lookup string '%0s' already registered to produce '%0s'.  Set replace argument to replace existing entry.", lookup_str, m_sc.scratch1));
00285       return;
00286     end
00287   end
00288   else begin
00289   end
00290 
00291   tmp = new("", lookup_str, type_name);
00292 
00293   type_overrides[lookup_str] = tmp;
00294 
00295 endfunction
00296 
00297 
00298 // find_override
00299 // -------------
00300 
00301 function ovm_object_wrapper ovm_factory::find_override(string lookup_str,
00302                                                        string inst_path,
00303                                                        string inst_name);
00304 
00305   // First see if any instance-specific override; return first match
00306   for (int index=0; index < inst_overrides.size(); index++) begin
00307 
00308     if (ovm_is_match(inst_overrides[index].req_type_name, lookup_str)) begin
00309 
00310       if (inst_name != "") begin
00311 
00312         if (ovm_is_match(inst_overrides[index].inst_path,
00313                      {inst_path,".",inst_name}))
00314           return types[inst_overrides[index].type_name];
00315 
00316       end
00317       else if (ovm_is_match(inst_overrides[index].inst_path, {inst_path})) begin
00318 
00319         return types[inst_overrides[index].type_name];
00320 
00321       end
00322     end
00323   end 
00324 
00325   // Next, lookup simple overrides queue
00326   if (type_overrides.exists(lookup_str)) begin
00327     return types[type_overrides[lookup_str].type_name];
00328   end
00329 
00330   // No override found
00331   return null;
00332 
00333 endfunction
00334 
00335 
00336 // create_type
00337 // -----------
00338 
00339 function ovm_object ovm_factory::create_type (string     lookup_str,  
00340                                               string     inst_path="",  
00341                                               string     name=""); 
00342 
00343   ovm_object_wrapper wrapper;
00344 
00345   wrapper = find_override(lookup_str, inst_path, name);
00346 
00347   // no override exists, so use lookup_str directly
00348   if (wrapper==null) begin
00349     if(!types.exists(lookup_str)) begin
00350       ovm_report_warning("BDTYP",$psprintf("Cannot create an object based on lookup string '%0s'.  This type is not registered with the factory.", lookup_str));
00351       return null;
00352     end
00353     wrapper = types[lookup_str];
00354   end
00355 
00356   return wrapper.create_object(name);
00357 
00358 endfunction
00359 
00360 
00361 // create_component_type
00362 // ---------------------
00363 
00364 function ovm_component ovm_factory::create_component_type (string     lookup_str,  
00365                                               string     inst_path="",  
00366                                               string     name, 
00367                                               ovm_component   parent);
00368 
00369   ovm_object_wrapper wrapper;
00370 
00371   // no override exists, so use lookup_str directly
00372   wrapper = find_override(lookup_str, inst_path, name);
00373   if (wrapper == null) begin
00374     if(!types.exists(lookup_str)) begin 
00375       ovm_report_warning("BDTYP", $psprintf("Cannot create an object based on lookup string '%0s'.  This type is not registered with the factory.", lookup_str));
00376       return null;
00377     end
00378     else begin
00379       wrapper = types[lookup_str];
00380     end
00381   end
00382 
00383   return wrapper.create_component(name, parent);
00384 
00385 endfunction
00386 
00387 
00388 // create_object (static)
00389 // -------------
00390 
00391 function ovm_object ovm_factory::create_object (string     lookup_str,  
00392                                                 string     inst_path="", 
00393                                                 string     name="");
00394 
00395   if (inst==null)
00396     inst = new ("ovm_factory_inst");
00397 
00398   return inst.create_type(lookup_str, inst_path, name);
00399 
00400 endfunction
00401 
00402 
00403 // create_component (static)
00404 // ----------------
00405 
00406 function ovm_component ovm_factory::create_component (string lookup_str,  
00407                                             string   inst_path="", 
00408                                             string   name, 
00409                                             ovm_component parent);
00410 
00411   if (inst==null)
00412     inst = new ("ovm_factory_inst");
00413 
00414   create_component = inst.create_component_type(lookup_str, inst_path, name, parent);
00415 
00416 endfunction
00417 
00418 
00419 // set_type_override (static)
00420 // -----------------
00421 
00422 function void ovm_factory::set_type_override (string lookup_str,
00423                                               string type_name,
00424                                               bit replace=1);
00425   if (inst==null)
00426     inst = new ("ovm_factory_inst");
00427 
00428   inst.override_type (lookup_str, type_name, "", replace);
00429 
00430 endfunction
00431 
00432 
00433 // set_inst_override (static)
00434 // -----------------
00435 
00436 function void ovm_factory::set_inst_override (string inst_path,
00437                                               string lookup_str,
00438                                               string type_name);
00439 
00440   if (inst==null)
00441     inst = new ("ovm_factory_inst");
00442 
00443   inst.override_type (lookup_str, type_name, inst_path, 0);
00444 
00445 endfunction
00446 

Intelligent Design Verification
Intelligent Design Verification
Project: OVM, Revision: 1.1.0
Copyright (c) 2008 Intelligent Design Verification.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included here:
http://www.intelligentdv.com/licenses/fdl.txt
doxygen
Doxygen Version: 1.4.6
Mon Sep 29 14:20:12 2008
Find a documentation bug? Report bugs to: bugs.intelligentdv.com Project: DoxygenFilterSV