Passing data through constructor

Is the fixed prototype requirement of the factory essentially is meant to discourage passing arguments through the constructor ?

Sometimes it is very handy to pass an integer through constructor. I am not able to achieve the same through parameters. I think they are fixed at compile time.

program test;

  import uvm_pkg::*;

  class compA #(int unsigned id = 10) extends uvm_component;
   
    `uvm_component_param_utils(compA#)
    //`uvm_component_param_utils(compA#(id))

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

    function void display();
      $display("ID = %0d", id);
    endfunction

  endclass

  class env extends uvm_test;

  `uvm_component_utils(env)

    compA compa_h;

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

    function void build();
      compa_h = compA#(20)::type_id::create("compa_h", this);
      compa_h.display();
    endfunction

 endclass 

  initial begin
    run_test(); 
  end

endprogram

What is the recommended way to pass information during construction of an object ?

That’s what the config class is for. See Configuration | Verification Academy

In reply to dave_59:

Thats useful.

Although if one needs to set ID’s for sequences while they get created. It may be easier to actually pass it through the constructor. Instead now, i have to call a function that sets the ID after I create the sequence.

Ex:

lane_seq lane_seq_h[NUM_LANES];

foreach(lane_seq_h[i])
if(lane_seq_h[i] == ACTIVE)
begin
lane_seq_h[i] = lane_seq::type_id::create(“lane_seq_h”);
lane_seq_h[i].set_lane_id(i);
end

In reply to sohels:

I think you will find that in the long run that using the factory with the configuration mechanism is easier to write test without having to modify a chain of constructor arguments when you need to add a new ID or whatever to the sequence.

Hello! I have a similar problem, and I am still not quite getting it. Check out my driver class below. I want to use it to connect to a DUT with the same interface multiple times – specifically a mutli-port memory port memory controller with several clients. Therefore, I will have multiple independent, but identical, virtual interfaces instantiated at the top level, shown below. It is the following line in the driver class that bothers me: assert (get_config_object(“vif_c0”, obj, 0));

I need to be able to get the config object using the correct name “vif_c0”, “vif_c1”, … “vif_cN”. How should I do this?

Thanks in advance!
Aaron


** From top level, instantiate virtual interfaced between driver and DUT


generate
// Construct the virtual interfaces…
for (genvar i = 0; i < `NUM_MEM_CLIENTS; i++)
initial vifc [i] = new (difc[i]);
endgenerate

initial begin
string my_vif_name;

for (integer i = 0; i < `NUM_MEM_CLIENTS; i++) begin
  my_vif_name = $sformatf("vif_c%0d", i);
  `ovm_info("top.sv; virtual interface names", my_vif_name, OVM_MEDIUM)
  set_config_object("*", my_vif_name, vifc[i], 0);
end // for loop

 vif_server = new(dif_server);
 set_config_object("*", "vif_s", vif_server, 0);   
 `ovm_info("top.sv", "... run_server_test() ...", OVM_MEDIUM)
  
 run_test(); 

end


** Driver Class


class tb_driver_mem_client extends ovm_driver #(trans_item_mem_client);
`ovm_component_utils(tb_driver_mem_client)

virtual dut_if_client vif_client;

function new (string name, ovm_component parent);
super.new(name, parent);
endfunction

function void build ();
super.build();
assign_vi;
endfunction

function void assign_vi;
ovm_object obj;
dut_if_client_wrapper w;
//---------------------------------------
assert (get_config_object(“vif_c0”, obj, 0));
assert ($cast(w, obj));
vif_client = w.vif;
endfunction

extern task run();

extern task initialize (trans_item_mem_client tx);

extern task drive_item (trans_item_mem_client tx);

endclass

You can use a static int and increment it in constructor. then use the same to get the appropriate interface.

static int id_static = -1;
int id;

function new (string name, ovm_component parent);
super.new(name, parent);
 id_static++;
 id = id_static;
endfunction

function void assign_vi;
..
assert (get_config_object($psprintf("vif_c%0d", id), obj, 0));
..
endfunction

Hope this helps

worked perfectly, thank you!