Jump to content

Unique array elements without rand or randc

Recommended Posts

I have a variable logic [31:0] id which is not declared as rand or randc. I need different id's each time into an array logic [31:0] id_array [16].

logic [31:0] id;
logic [31:0] id_array [16];
foreach(id_array) begin
id_array = id;

In the above code, there is a possibility of getting duplicate ids in the array. How do I change the code to get unique ids in the array ?


Share this post

Link to post
Share on other sites

Write a constraint for that. The idea is, for each element in the array, you iterate through all the elements of the array. If the index don't match, the make sure the values are not equal.

This is O(n^2) complexity, there could be a better way of accomplishing the same as well.  

constraint c_unique_array_value {

  foreach(id_array) { 

      foreach(id_array[j]) {

          if(i != j) id_array != id_array[j];




Share this post

Link to post
Share on other sites

I think UNIQUE KEYWORD does well , I have used it with Synopsys VCS, a quick example ::-> http://www.edaplayground.com/x/6AHE


Not sure about cadence incisive, sometime back raised CCR for enhancement for this feature , not sure whether it is available.


But yes, this unique keyword does much beautification to my code. ;)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now