How to use unique in generating array of classes?

Hi,

Assume I have this class :

class my_classe extends uvm_object;
        rand int unsigned port_idx;
        rand int unsigned port_num;
    endclass
    

and in my test I have :


    rand my_classe ex_class[6]
    

and I want this array of classes (ex_class) to be generated in a way that all the port_idx fields in all the classes are unique - The port_idx in ex_class[0] is not the same as in ex_class[1,2,3,4,5] and so on,

And I don’t want to use other arrays for the generation, just the “unique” property.

How should I write such a constraint? Is it possible to use the unique constraint when it is not the case of an array of native types (bit, int…)

Thank you :)

In reply to snognog:
The unique constraint requires a list of integral values, or arrays of integral values. If you insist on using the unique constraint, then the only way do do this is listing the elements individually.

constraint ui {ex_class[0].port_idx, ex_class[1].port_idx, ex_class[2].port_idx, ex_class[3].port_idx, ex_class[4].port_idx, ex_class[5].port_idx };

Otherwise you’ll need to use foreach

In reply to dave_59:

Hi Dave.

Thank you for your answer.

The thing is, consider the size of the array could be a define, ARR_SIZE, this use of unique could be problematic.

What would you suggest to do then ?

:)

Thank you,
Noga

In reply to snognog:

There’s no way to write a constraint using the unique construct given the requirements you want. You can use a foreach loop;

foreach (ex_class[i1]) foreach (ex_class[i2]) i1!=i2 -> ex_class[i1].port_idx != ex_class[i2].port_idx;