Unsupported Index type for an associative array in an interactive constraint

Hi, Guys

When using vsim, I encountered an error with an associative array where a class is used as the index during constraint randomization. Could you clarify whether this issue is caused by the tool, my code, or my understanding?

Part of the code:

class KeyClass;
  rand bit [31:0] id;
endclass

class ConstrainedAssocArrayClass;
  // Associative array with class keys
  rand bit [31:0] assoc_array[KeyClass];

  // Constraint to limit values
  constraint value_limit {
    foreach (assoc_array[key]) {
      assoc_array[key] < 32'd100; // Values must be less than 100
    }
  }

The LRM says

  • A constraint may be any expression containing operands of integral or real types only
  • Subexpressions of a constraint may include operands that are neither integral nor real if the subexpression
    contains no random variables and the result of the subexpression has an integral or real type (e.g., an equality operator with both sides having string operands).

Hi, @dave_59

Thank you for your response!

Is my understanding correct? The reason class types indices are not supported is that they do not have a fixed value range and cannot be enumerated, which makes it impossible for the solver to parse and solve such dynamic types. On the other hand, other types like user-defined data types or strings can be enumerated, and that’s why they are allowed in constraint expressions.

If that’s the case, does it mean that using a wildcard as an index is also not allowed in constraint expressions?

Best regards,
Yilou

The constraint expression is required to be an integral expression.
When you write ::

constraint value_limit {
    foreach (assoc_array[key]) {
      assoc_array[key] < 32'd100; //  Not an integral expression !!
    }
  }

Each element of assoc_array[key] is a class type whereas assoc_array[key].id is an integral type. Your intention is to constraint the property ‘id’ for each object

  constraint value_limit {
    foreach (assoc_array[key]) {
      assoc_array[key].id < 32'd100; // Now it's an integral expression
    }
  }

I don’t understand.
The class is the index of the associative array, not the data type of the associative array.

These are two separate issues. The way the LRM is written, you can’t use an associative array in a constraint with a class as in index type because the expression used as an index is not integral or real (reals are not allowed as associative array index types regardless of being in a constraint). There is no reason other than an oversight preventing this as long as the index is a non-random state variable.
Note that constraints cannot construct or destroy associative array elements.

The issue with not being allowed to iterate with a wildcard index is also regardless of being in a constraint.