Setting a variable inside a class

I am trying to achieve the following and am not sure how to do it. Any suggestions would be helpful.

  1. I have a base sequence item as follows:

   class base_seq_item extends uvm_sequence_item;
     `uvm_object_utils(base_seq_item);

      bit init_zero;
      bit init_rand;

     function new(string name="base_seq_item");
       init_zero = 0;
       init_rand = 0;
     endfunction
   endclass
   
  1. I have a bunch of other sequence items that extend from the above base_seq_item with each extended class having its own variables. Here’s an example extended sequence item class

    class ext_seq_item extends base_seq_item;
      `uvm_object_utils(ext_seq_item);

      rand logic [5:0] data;

      function new(string name="ext_seq_item");
        super.new(name);

        if(init_zero)
          data = 0;

        if(init_rand)
          data = $urandom();
      endfunction
    endclass  
    
  1. The extended class constructor uses the variables init_zero and init_rand to set the initial value of the sequence item.

  2. I would like to know what is a way to set these variables (init_zero/init_rand) in the testbench (may be in build_phase) so that I can every sequence item object created sees this change. Is there any way to set this before creating the objects (afaik this is impossible in OOPs, but just checking)

You could make them static, but I am guessing you are looking for other possibilities.

In reply to KillSteal:

Making it static would result in the value being the same across all the child classes (in my case the extended sequence items). I would like to set different initial values (either 0/random) in different extended sequence item classes.

In reply to tpan:

This is what the uvm_config_db is for. You can put this in the base_seq_item constructor since the name will be overridden.

 function new(string name="base_seq_item");
       if (uvm_config_db::get(null,name,"init_zero",init_zero)) ; 
           else
         init_zero = 0;
       if (uvm_config_db::get(null,name,"init_rand",init_rand)) ; 
           else
         init_rand = 0;
     endfunction

In reply to dave_59:

Thanks for the response, Dave. I don’t quite get how can I use this. Let’s say I want for seq_item1 I want to set init_zero to 1 and for seq_item2 I want to set init_rand to 1, how can I do it (let’s say I want to do this from my test class)