How can I create an array of agents of same type?

I have to create a bunch of agents which are of same type. Can I create them as an array rather than copy pasting code again and again?

In reply to SharonBeaulah:

Of course you can do this.But it is not needed.
But you do not need to paste the code again and again because the agent class definition ius needed only once. You can reuse the agent code from another project by passing only a link to the source code.

In reply to chr_sue:

Sorry if my question wasn’t clear. I want to instantiate same agent multiple times based on a configuration variable in one environment. Can I have it as an array?

In reply to SharonBeaulah:

You sure can, and people do this all the time to represent multiple masters and slaves on a bus.

In reply to SharonBeaulah:

Do you mean something like this?

master_agent my_master_agent[num_agent];

foreach(my_master_agent)
begin
  my_master_agent[i] = master_agent::tpye_id::create(my_master_agent, this);
end

You can take num_agent from environment. You can use config_db or directly assign like “agent_inst.num_agent = 12”

In reply to hcglu:

[CLDEXT] Cannot set ‘wali[i_wali]’ as a child of ‘uvm_test_top.environment’, which already has a child by that name.

Getting above error.

I don’t want to use manual method as I have number of such agents and I want to access them using index which is a configuration variable.

Also I want to have multiple monitor bfms and driver bfms, but I’m not able to get them in a for loop.

In reply to SharonBeaulah:

Could you please show some code related to the error you get.

In reply to chr_sue:

Sure. My code file is very huge, just pasting the related lines…

typedef wali_agent #(
.DATA_WIDTH(WALI_DATA_WIDTH),
.ADDR_WIDTH(WALI_ADDR_WIDTH)
)
wali_agent_t;
wali_agent_t wali [N-1:0]; // N is a parameter

// In build_phase
foreach (wali[i_wali]) begin
wali[i_wali] = wali_agent_t::type_id::create(“wali”,this);
wali[i_wali].set_config(configuration.wali_config[i_wali]);
end

In reply to SharonBeaulah:

The first argument to create() is the instance name. Every child component needs a unique instance name. (Imagine your parents named you and your brother “Chris”!)

Here is how I would write the create loop. Your instance name string does not HAVE to match the handle name, but it makes debug easier. Put the index value in the string with $sformatf().

master_agent agt[num_agent];
foreach (my_master_agent)
  agt[i] = master_agent::type_id::create($sformatf("agt[%0d]", i), this);

Now you have instance names “agt[0]”, “agt[1]”, etc. I like to put square brackets around the index to prevent an issue with wildcards. In the uvm_config_db, if you specify “env.agt1*”, that will match “env.agt1”, “env.agt10”, “env.agt11”, etc.

In reply to chrisspear:

Little heads-up, I’m kind of new to this and sorry if it’s so basic.
I tried this one and this is what I’m getting. I’m using UVMF so most of the code is auto-generated.

UVM_FATAL @ 0.000ns: uvm_test_top.environment.wali[0].wali[0]_driver [DRV] BFM handle with interface_name is null

I tried adding them to BFM as well, but it’s looking for that particular instance name and it is not accepting it as the name of BFM because of the index.

In reply to SharonBeaulah:

Aha - UVM Framework, that helps! I always use “grep -r” (or the Emacs equivalent) to explore UVMF as there are many files.

That error message is from the driver’s connect_phase() checking if configuration.driver_bfm is null. The full error message is:

`uvm_fatal("DRV", $sformatf("BFM handle with interface_name %s is null",configuration.interface_name) );

It looks like the configuration.interface_name string is null. This is set in the agent configuration’s initialize() method. That string was passed in from the environment’s initialize() method. You can either grep for the call or set a breakpoint on the agent initialize() method and walk through the code. Also, set the VERBOSITY to UVM_DEBUG and look in the log file for a message starting with “Interfaces for the following uvm environment hierarchy”, which should be followed by a list of names.