Constraining dynamic array size with reusable policy class

In reply to dave_59:

Hi Juhi/Dave,

I temporarily updated the example with rand on the item as follows:

class policy_base#(type ITEM=int);
  
  rand ITEM item;
  
  virtual function void set_item(ITEM item);
    this.item = item;
  endfunction
  
endclass

The same error persists, however. This may be since it is the item which is randomized, not the policy object. The policy object is not randomizing the item, only its rand variables. I should mention that other rand variables can be constrained without rand on item. It is just size constraints that error.

But as Juhi mentioned there is a workaround. Seems a soft or upper bound constraint in the item itself is required by the simulator. I’ve updated the example (https://www.edaplayground.com/x/EPL_) with a new c_data constraint as follows and the simulation runs without error:

class txn;

  rand logic [31:0] addr;
  rand logic [31:0] data[];
  
  rand policy_base#(txn) policy[$];

  constraint c_data {
    data.size < 4;
  }

  function void pre_randomize;
    foreach(policy[i]) policy[i].set_item(this);
  endfunction

endclass

Seems odd that this constraint is required by the simulator. For sure it is a good practice anyways, but if the policy object is applied, as in this case, I’m surprised the constraint solver couldn’t solve it.

Thanks to both of you.