We are using policy classes as described in the 2015 DVCon US paper, “SystemVerilog Constraint Layering via Reusable Randomization Policy Classes” by John Dickol.
They work great. However we are running into a problem sizing a dynamic array.
Before doing t.randomize(), you need to assign t.data= new[1] which will solve this constraint error. Constraint failure has occurred because, data size in t was zero and in txn_policy it expects it to be of 1.
Thanks for your reply. I don’t think that’s it though. SV LRM section 18.4 states that an array will be “resized according to the size constraint”. The usefulness of the size constraint would be limited if you had to know the size of the array and call new before randomizing.
Also it is a good idea to place a constraint on the size of the array in case no policies get added. You can either use a soft constraint as suggested, or a maximum hard constraint.
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:
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.
I think their may be some ambiguities in the LRM. There seems to be a lack of consensus among the tools on EDAPlayground. You may want to discuss this with your tool vendor as this forum is not for discussing tool specific issues.