Expression of this type cannot be used to index the array

Hello All !

Is it not possible to cycle through an associative array during randomization ? For the below code, get the following error.


module assoc_array_cycling;
 class config;
  rand bit [7:0]my_pts[$];
 endclass: config

 class aac;
  config   cfg;

  bit[7:0] agt_pts[string][$] = '{"alu0":'{0:'h11}, "cpu0":'{0:'h11, 1:'h22}};

  function perform_opr;
    cfg = new();

    assert(cfg.randomize with { 
           cfg.my_pts.size() == agt_pts["cpu0"].size();

           foreach(agt_pts["cpu0"][z])
             my_pts[z] == agt_pts["cpu0"][z];

    }) else $display("Randomization failed");
  endfunction: perform_opr
 endclass: aac 

 initial begin
   aac aac_inst = new();
 
   aac_inst.perform_opr();
 end
endmodule: assoc_array_cycling

Error Message:
Error-[TCF-IIE] Illegal index expression
assoc_array_cycling.sv, 19
“this.my_pts[z]”
Expression of this type cannot be used to index the array

Can anyone clarify the same !

In reply to desperadorocks:

my_pts = '{'h11, 'h22} // code display



module assoc_array_cycling;
 class config;
  rand bit [7:0]my_pts[]; // using dynamic array instead of queue
 endclass: config
 
 class aac;
  config   cfg;
 
  bit[7:0] agt_pts[string][$] = '{"alu0":'{'h11}, "cpu0":'{'h11,'h22}}; // your assignment is wrong
 
  function perform_opr;
    cfg = new();
    
    assert(cfg.randomize with { 
           cfg.my_pts.size() == agt_pts["cpu0"].size();
 
      foreach(my_pts[z]) // walking dynamic array
             my_pts[z] == agt_pts["cpu0"][z];
 
    }) else $display("Randomization failed");

    $display("my_pts = %p", cfg.my_pts);
  endfunction: perform_opr
 endclass: aac 
 
 initial begin
   aac aac_inst = new();
 
   aac_inst.perform_opr();
 end
endmodule: assoc_array_cycling


In reply to javatea:

Hello Javatea,

Thanks for your response. This is a basic example to represent my bigger issue. In that scenario, I can’t edit the my_pts code from queue to an dynamic array bcoz its a vip from a different team.

With that said, and even after changing the assignment code in the agt_pts I face the same issue. Aren’t there any other way to solve this out ? or is it not possible at all ?

In reply to desperadorocks:

The below code seems working.



class config_;
  rand bit [7:0]my_pts[$];
endclass: config_
 
class aac;
  config_   cfg; 
  bit[7:0] agt_pts[string][$] = '{"alu0":'{0:'h11}, "cpu0":'{0:'h11, 1:'h22}};
 
  function void perform_opr;
    cfg = new();
    assert(cfg.randomize with {
      cfg.my_pts.size() == agt_pts["cpu0"].size(); 
      //foreach(agt_pts["cpu0"][z])
      foreach(cfg.my_pts[z]) cfg.my_pts[z] == agt_pts["cpu0"][z];
    }) else $display("Randomization failed");
    
    foreach(cfg.my_pts[ii]) $display("my_pts[%0d] : %0h",ii,cfg.my_pts[ii]);
  endfunction: perform_opr
 endclass: aac
    
module assoc_array_cycling;
 aac aac_inst;
 initial begin
   aac_inst = new(); 
   aac_inst.perform_opr();
 end
endmodule: assoc_array_cycling

Output:

ncsim> run
my_pts[0] : 11
my_pts[1] : 22

In reply to karan_18:

Hello Karan !

THanks for your inputs. Had the reverse working at my end as well. But was wondering if cycleling through the assoc array during randomization isn’t possible at all?

In reply to desperadorocks:

The syntax is not legal, though simulators other than the one you are using support it. You are only allowed one set of 's in a foreach loop. Normally you would do there following to skip of iterating one of the dimensions:

foreach(agt_pts[,z])

But that doesn’t work when the z dimension is dynamically sized.

A few other non-standard code fragments

  • config is a reserved keyword
  • You cannot use an assignment pattern with named indexes to assign to a queue. Use array concatenation
bit[7:0] agt_pts[string][$] = '{"alu0":{h11}, "cpu0":{h11,'h22}};

  • You cannot initialize an implicitly static variable(aac_inst) in its declaration. People mistakenly think it gets initialized as part of entering the procedural block it in.

In reply to dave_59:

THanks Dave for your inputs ! And thanks to all !