Can I declare one time not multiple time for frequently used syntax?

Hi All,

When I use the virtual interface for each class, I declared multiple time like as the below. it seems very inconvenience. Is there any way just one time declare

virtual my_interface vintf;

and

uvm_config_db#(virtual my_interface)::get(this, "", "vintf_my_interface", vintf)

to handle into the multiple class?
I just thought that they can move into my_pkg, so I declare it at once. but It’s not possible. because “virtual my_interface vintf” and uvm_config_db#(virtual my_interface)::get(this, “”, “vintf_my_interface”, vintf) are should be in a class right?

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


class my_driver extends uvm_driver;
int c;
`uvm_component_utils(my_driver)

virtual my_interface vintf;

function new(string name = "", uvm_component parent);
    super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);
if(! uvm_config_db#(virtual my_interface)::get(this, "", "vintf_my_interface", vintf))
`uvm_error("", "!!uvm_config_db get fail!!!!");
endfunction

task run_phase(uvm_phase phase);
vintf.a = 6;
vintf.b = 7;
//c = vintf.c;
//$display(" %d, %d, %d",vintf.a, vintf.b, c);
$display(" %d, %d, %d",vintf.a, vintf.b, vintf.c);

endtask

endclass
class my_env extends uvm_env;

`uvm_component_utils(my_env)

virtual my_interface vintf;
my_driver p_driver;

function new (string name ="", uvm_component parent);
super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);
p_driver = my_driver::type_id::create("p_driver", this);
if(! uvm_config_db#(virtual my_interface)::get(this, "", "vintf_my_interface", vintf))
`uvm_error("", "!!uvm_config_db get fail!!!!");
endfunction

task run_phase(uvm_phase phase);
vintf.a = 8;
vintf.b = 9;
//c = vintf.c;
//$display(" %d, %d, %d",vintf.a, vintf.b, c);
$display(" %d, %d, %d",vintf.a, vintf.b, vintf.c);
endtask

endclass

In reply to UVM_LOVE:

You should do the get on the config_db where you are need this interface. Looking to your env you do nothing with the virtual interface. But the env is a good place to perform the get to retrieve the virtual interface from the config_db, because from there you can connect this to your driver and your monitor. Then you have to make the get only ones.
BTW the get on the config_db should be made in the connect_phase. Then you can be sure all components are existing.

the connect-_phase could look like:

function void connect_phase();
  if(! uvm_config_db#(virtual my_interface)::get(this, "", "vintf_my_interface", vintf))
     `uvm_error("", "!!uvm_config_db get fail!!!!")
  p_driver.vintf = vintf;
  p_monitor.vintf = vintf;
endfunction

In reply to chr_sue:

In reply to UVM_LOVE:
You should do the get on the config_db where you are need this interface. Looking to your env you do nothing with the virtual interface. But the env is a good place to perform the get to retrieve the virtual interface from the config_db, because from there you can connect this to your driver and your monitor. Then you have to make the get only ones.
BTW the get on the config_db should be made in the connect_phase. Then you can be sure all components are existing.
the connect-_phase could look like:

function void connect_phase();
if(! uvm_config_db#(virtual my_interface)::get(this, "", "vintf_my_interface", vintf))
`uvm_error("", "!!uvm_config_db get fail!!!!")
p_driver.vintf = vintf;
p_monitor.vintf = vintf;
endfunction

Dear @chr_sue Thanks! But I’ve got some question.

Q1)
What If I got the uvm_test class then Do I need it into the uvm_test class instead uvm_env class?
I’m trying to understand your recommend and implement as the below,

Q2)
But I’ve got error

  p_driver.vintf = vintf;
               |
ncelab: *E,CUVUNF (./testbench.sv,81|15): Hierarchical name component lookup failed at 'p_driver'.

As I understand from your answer, I can connect all virtual interface at top level class.
So I declared it into uvm_test class. If I right why do I got the error? What am I supposed to do to resolve this problem?

In reply to UVM_LOVE:

You get the error because your path is incomplete. The test class instantiates the env.
The path is then

p_env.p_driver.vintf

In reply to chr_sue:

In reply to UVM_LOVE:
You get the error because your path is incomplete. The test class instantiates the env.
The path is then

p_env.p_driver.vintf

Yup it works.
BTW, should I have to use it into uvm_env class or can I use it in uvm_test class?
because generally one uvm_test can has multiple uvm_env, so I think if I use them(such as virtual interfaces) into uvm_env, it will has more something efficient as your recommends, but not sure whether I am correctly do understand your intention or not.

In reply to UVM_LOVE:

My recommendation is to make this in the env, because multiple envs in the test are commonly different envs with different virtual interfaces.