Warning: Non-existent associative array entry. Returning default value


typedef enum {A,B,C,D} sometype_t;

class txn
   rand int array[sometype_t] = '{default:0, A:1, B:2, C:3, D:4}; 
   ...
endclass

initial begin
   txn.randomize();
end

From LRM 7.9.11 “If a default value is specified, then reading a nonexistent element shall yield the specified default value, and
no warning shall be issued.” I have a default value specified, yet I am getting 4 warnings that say:

** Warning: (vsim-3829) Non-existent associative array entry. Returning default value.

How can I get rid of the warnings?

In reply to ce_2015:

For an associative array, we can’t add/remove items in randomize()
I guess you want to do the following:
the first time .randomize() will return default lookup table.
but then if you randomize again, it will randomize existed element in this associative array.


class txn;
  typedef enum bit [3:0] {A,B,C,D} sometype_t;   
  rand int array[sometype_t] = '{default:0}; 

  function void post_randomize(); // first randomize will set to default vaule
    if (array.num()==0) begin
       array = '{A:1, B:2, C:3, D:4};
    end
  endfunction
  
endclass

In reply to javatea:

I’m not trying to add anything to the array in randomize. I want an initialized array before I call randomize().

Isn’t there a way to initialize the associative array without having to call randomize (and rely on the post_randomize function)?

In reply to ce_2015:

Actually, I try this and it works fine in local and even SystemVerilog Randomization - EDA Playground
maybe check your tool first. It’s valid syntax.


class txn;
  typedef enum bit [3:0] {A,B,C,D} sometype_t;   
  rand int array[sometype_t] = '{default:0, A:1, B:2, C:3, D:4};   
endclass

module automatic test;
  
  function void run;
    txn tmp = new();
    $display("template : %p", tmp); // '{array:'{A:1, B:2, C:3, D:4} }

    tmp.randomize();
    $display("template : %p", tmp); // '{array:'{A:727460974, B:-2124444300, C:-816347820, D:2075578760} }
  endfunction
  
  initial run();
  
endmodule

In reply to javatea:

In reply to ce_2015:
maybe check your tool first.

I was afraid this would be the case…