SV Constraint Challenge

If someone wants to try this:

In SystemVerilog, given an array of 100 random integers, write a constraint to ensure that the last four digits of each integer (i.e., the lower 4 decimal digits) are unique across all 100 integers.

Two possible solutions

(1) Using helper array

class Unique_BCD;
  
  rand int unsigned  arr[100];
  rand bit[15:0]     helper_arr[100];

  constraint BCD {        foreach( arr[i,j] )
                     ( j%4 == 0 ) -> arr[i][j+:4] inside {[0:9]}; 
                 }
 
  constraint Helper {    foreach( helper_arr[i] )
                         helper_arr[i] == arr[i][15:0] ;
                     }

  constraint unique_constrnt {  unique { helper_arr }; }  

endclass  

(2) Without using helper array

I haven’t figured the unique part

class Unique_BCD;
  
  rand int unsigned arr[100];
  
  constraint BCD {        foreach( arr[i,j] )
                     ( j%4 == 0 ) -> arr[i][j+:4] inside {[0:9]}; 
                 }
  
  // Need help with unique constraint
  constraint unique_constrnt {         foreach( arr[i] )
                                     unique { arr[i][15:0] } ; 
                              }  
endclass  

You need to be more specific.

Do you mean the decimal formatted representation of the binary 2’s complement integer needs to have unique digits? Or are you looking for unique binary coded decimal values?

@dave_59 For example: 127890, 349563, 440001, 327890, 110001, 440002, 349564

327890 and 110001 are invalid because last four digits are repeated.
440002 and 349564 are valid because last four digits are unique although first 2 digits are repeated.

You didn’t answer my question. I’m going to assume you meant the decimal representation of the binary number; not binary coded decimal. This constraint satisfies your requirements as written:

class A;
  rand int array[100];
  constraint C {
     foreach(array[i]) array[i] inside {[0:9999]};
     unique {array};
  }
endclass

Yes, I meant decimal representation of binary number.
Random number can be any number out of ~4billion and cannot constraint it to be between 0:9999

Please elaborate on the above 2 lines
I didn’t get why 327890 and 110001 are invalid ?
Binary representation of 327890 is 19’b1010000000011010010 whereas
binary representation of 110001 is 17’b11010110110110001

Because if you notice first 3 values are 127890, 349563, 440001, so subsequent values should not end with 7890 or 0001. It is confusing but was an interview question - I don’t understand the point of such questions because this is not we do as verification engineers on daily basis.

The point of these questions is not necessarily providing a correct answer, but knowing how to ask the right questions about the problem if you don’t understand it. You need to be able to do that on a daily basis.

The original question said nothing about the size of the integers needed. My answer provides random integers that meet the requirements given. Also, someone interpreted the question to mean binary coated decimal integers, but that was never clarified. That’s a question that needs to be answered.

1 Like

Hi Dave,
Assuming the requirement is for unique lower 4-BCDs, how can I achieve the uniqueness in (2) i.e without using helper array ?
My above attempt

  constraint unique_constrnt {         foreach( arr[i] )
                                     unique { arr[i][15:0] } ; 
                              }  

would unroll to

  constraint unique_constrnt {         
                                     unique { arr[0][15:0] } ; 
                                     unique { arr[0][15:0] } ; 
                                     unique { arr[1][15:0] } ; 
                                     unique { arr[2][15:0] } ; 
                                     unique { arr[3][15:0] } ;
                                     ....................... 
                                     unique { arr[99][15:0] } ; 
                              }  

I believe this wouldn’t constraint the lower 16-bits across all the 100 Indexes of ‘arr’ to be unique

I agree with your point. If I have made clarification on this, it would have been much easier. I assumed that the integers need to be greater than 10000 but that was not given in the question. Therefore, your answer is valid. And, if they wanted greater than 10000, then applying BCD to last 4 decimal digits would be have been easy to do.

Hi Dave,
I am not clear on the need to constraint each element within 0 to 9999.
If one were to keep each element unconstrained and directly use the unique constraint, wouldn’t that ensure that last 4 digits are unique indirectly.

Thanks in Advance

10123 and 20123 do not meet the requirements.