# Configuration object casting problem in the hierarchical tb

**URL:** <https://verificationacademy.com/forums/t/configuration-object-casting-problem-in-the-hierarchical-tb/31943>\
**Category:** UVM\
**Tags:** UVM, configuration, uvm\_env, UVM-uvm\_test, cast\
**Created:** [June 13, 2016, 6:36pm UTC](https://verificationacademy.com/forums/t/configuration-object-casting-problem-in-the-hierarchical-tb/31943 "2016-06-13T18:36:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vaino](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@Vaino](https://verificationacademy.com/forums/u/Vaino)\
**Post date:** [June 13, 2016, 6:36pm UTC](https://verificationacademy.com/forums/t/configuration-object-casting-problem-in-the-hierarchical-tb/31943/1 "2016-06-13T18:36:18Z")

</div>

Hi,

I have the following configuration object hierarchy (a-b-c) in which every subclass brings some new additional properties:

```verilog
class cfg_a extends uvm_object;
endclass : cfg_a
class cfg_b extends cfg_a;
endclass : cfg_b
class cfg_c extends cfg_b;
endclass : cfg_c

```

The same amount of layers (a, b, c) is also applied to the UVM test and env hierarchy as follows:

```verilog
// UVM test hierarchy (a-b-c)
class test_base_a extends uvm_test;
  cfg_a cfg_a_inst;
  ...
  function void build_phase(uvm_phase phase)
    ...
    uvm_config_db#(uvm_object)::set(null, "*", "cfg", cfg_a_inst);
  endfunction
endclass : test_base_a
class test_base_b extends test_base_a;
  cfg_b cfg_b_inst;
  ...
  cfg_a_inst = cfg_b_inst;
  ...
endclass : test_base_b
class test_base_c extends test_base_b;
  cfg_c cfg_c_inst;
  ...
  cfg_a_inst = cfg_c_inst;
  ...  
endclass : test_base_c

// UVM env hierarchy (a-b-c)
class env_base_a extends uvm_test;
  cfg_a cfg_a_inst;
  uvm_object cfg_inst;
  ...
  function void build_phase(uvm_phase phase)
    ...
    uvm_config_db#(uvm_object)::get(null, "*", "cfg", cfg_inst);
    $cast(cfg_a_inst, cfg_inst);
  endfunction
endclass : env_base_a
class env_base_b extends env_base_a;
  cfg_b cfg_b_inst; // cfg_b_inst is used to configure layer-b specific agents.
  ...
  $cast(cfg_b_inst, cfg_inst);
  if (cfg_b_inst.has_some_agent) ...
  ...
endclass : env_base_b
class env_base_c extends env_base_b;
  cfg_c cfg_c_inst; // Layer-c specific configuration.
  ...
  $cast(cfg_c_inst, cfg_inst);
  ...  
endclass : env_base_c

```

If I run test in the layer-c (test\_base\_c), which in turn instantiates env\_base\_c and passes cfg\_c object handle to config\_db, how can I perform configuration object cast in the env\_base\_b which is inherited? In that case, **$cast(cfg\_b\_inst, cfg\_inst);** won’t work because cfg\_inst object is not assignment compatible with cfg\_b\_inst, right? How should I overcome this issue, any proposals? I am open for better ideas.

Thanks!

-ilia

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 13, 2016, 9:05pm UTC](https://verificationacademy.com/forums/t/configuration-object-casting-problem-in-the-hierarchical-tb/31943/2 "2016-06-13T21:05:37Z")

</div>

_In reply to [ilia](https://verificationacademy.com/forums/t/configuration-object-casting-problem-in-the-hierarchical-tb/31943):_  
You didn’t show when any cfg objects are constructed, and when you called super.build\_phase().

The thing to do here is use a factory override before calling super.build(). Then the $cast will work because the object retireved by config\_db get() will be of the correct type.

```verilog
class test_base_a extends uvm_test;
  cfg_a cfg_inst;
  ...
  function void build_phase(uvm_phase phase)
    cfg_inst = cfg_a::type_id::create("cfg_inst);
    uvm_config_db#(uvm_object)::set(null, "*", "cfg", cfg_inst);
  endfunction
endclass : test_base_a
class test_base_b extends test_base_a;
  function void build_phase(uvm_phase phase)
    set_type_override_by_type( cfg_a::get_type(), cfg_b::get_type() );
    super.build_phase(phase);
endclass : test_base_b

```

This assumes env is also being overridden in sync with the cfg and test.
