Applying unique to random variables

Unique keyword works on unpacked arrays, queues and dynamic arrays. With associative arrays, the behavior seems to be tool dependent. On edaplayground, it works with synopsys but the cadence one errors out. I could not locate the exact behavior in the LRM. Can somebody help out?

In reply to kernalmode1:

Unpacked arrays include fixed sized, dynamic-sized, queues and associative arrays. I could not reproduce the error you did not show. Note that you cannot randomly size an associative array.

In reply to dave_59:

My apologies.

Here is the link to edaplayground - https://www.edaplayground.com/x/5mNA

Code is here-

module test;
  class A;
    rand int unsigned aa[int];
    constraint c1 {
      aa.size() == 10;
      unique{aa};
      foreach(aa[i]) {
        aa[i] < 10;
      }
    }
    function void disp();
      $display("%p", aa);
    endfunction
  endclass
   initial begin
     A a;
     a = new();
     assert(a.randomize());
     a.disp();
   end
endmodule

Output with Synopsys tool-
'{0x0:'h5, 0x1:'h7, 0x2:'h8, 0x3:'h1, 0x4:'h6, 0x5:'h9, 0x6:'h2, 0x7:'h3, 0x8:'h4, 0x9:'h0}

Error with Cadence tool-
aa.size() == 10; (./testbench.sv,5)
ncsim: *E,ASRTST (./testbench.sv,18): (time 0 FS) Assertion test.unmblk1.__assert_1 has failed

In reply to kernalmode1:
Your problem has nothing to do with unique constraints. The problem is as I noted above, the LRM does not allow size constraints on an associative arrays. (See section 18.4 Random Variables). What would be the behavior if the array was already populated with random index values?

In reply to dave_59:

Hi Dave ,

Is there any way I can use Unique Constraint on Packed Arrays ?

Example ::



rand bit [2:0 ] b; // Need 8 Unique Values 

constraint UNIQ { unique { b  } ; } 



This just Makes sure that 2 Consecutive Values are not same .

I get one way would be taking a Queue and pushing values inside into it in post_randomize() and adding another constraint of

 ! ( b inside { q } ; ) 

and in post_randomize



 if ( queue.size() == 8 )
    queue.delete();


Regards,
AGIS

In reply to Etrx91:

The unique constraint needs more than one integral value, otherwise it does nothing, Why not use randc for this?

In reply to dave_59:

Thank you for pointing out the section in LRM. Agreed that associative arrays prepopulated work the same way.