UVM FACTORY Example Usage

Hi All,




 `include "uvm_macros.svh"
  import uvm_pkg::*;

class packet extends uvm_component;
  `uvm_component_utils(packet)
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
endclass

class new_packet extends uvm_component;
  `uvm_component_utils(new_packet)
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
endclass

class env extends uvm_component;
  `uvm_component_utils(env)
  packet p;
  new_packet np;
  uvm_factory factory;
  
  function new(string name, uvm_component parent);
    super.new(name, parent);
    factory  = uvm_factory::get();
  endfunction
  
  function void replace();
    p = packet::type_id::create("pkt", this);
    np = new_packet::type_id::create("new_pkt", this);
    $display("before overriding");
    p.print;
    np.print;
    
    set_type_override_by_type(packet::get_type(), 
                              new_packet::get_type());
    $display("after overriding");
    p.print;
    np.print;
    factory.print;
    uvm_top.print_topology();
    
    //know the return type
    
    `uvm_info("ENV", $sformatf("Factory returned cmp of type=%s, path=%s", p.get_type_name(), p.get_full_name()), UVM_LOW)
    
    `uvm_info("ENV", $sformatf("Factory returned cmp of type=%s, path=%s", np.get_type_name(), np.get_full_name()), UVM_LOW)
    
  endfunction
endclass
//endmodule

module tb;
  env obj_env;
  initial begin
    //obj_env = new();
    obj_env = env::type_id::create("obj_env", null);
    obj_env.replace();
  end
endmodule
```

I am trying to understand uvm_factory example, this the output of the code

========================

before overriding

Name Type Size Value

pkt packet - @1830


Name Type Size Value

new_pkt new_packet - @1861

after overriding

Name Type Size Value

pkt packet - @1830


Name Type Size Value

new_pkt new_packet - @1861

Type Overrides:

Requested Type Override Type


packet new_packet

UVM_INFO testbench.sv(47) @ 0: obj_env [ENV] Factory returned cmp of type=packet, path=obj_env.pkt
UVM_INFO testbench.sv(49) @ 0: obj_env [ENV] Factory returned cmp of type=new_packet, path=obj_env.new_pkt

==================

Before overriding and after overriding, I could see the same output.

Questions:

  1. How can I really validate that, override has taken place?

  2. I tried to pint the return types, but that did not help. How can I use the return types or something effectively to understand override concepts?

Kindly advice 1 and 2

Thank you

Several issues:

  • Factory overrides only affect items being created. Your code creates ‘p’ and ‘np’ and then sets a factory override. The override has no effect on the items that were already created.
  • If you override ‘packet’ with ‘new_packet’, you will be unable to assign a new_packet handle to the declaration of ‘p’ as they aren’t type compatible. Typically overrides are used for classes extended from a base class (i.e. ‘new_packet’ extends ‘packet’).

Thank you @cgales

I was curious to know is there any way to check the status of override.

Success or unsuccessful?