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
    }
  }