3 Ways to Generate an Array with Unique Elements Using SystemVerilog Constraints

Hi guys, what I would like to share with you today is how to use SystemVerilog constraints to generate an array with unique elements. There are three ways that I can think of. The first one is to use the uniqueness constraints in SystemVerilog syntax, the other two ways are to use array iterators to do the trick.

 

  1. Using Uniqueness Constraints
  2. This is the most straightforward way to do it. SystemVerilog introduces this in RFM 18.5.5, a group of variables can be constrained using unique constraint so that no two members of the group have the same value after randomization.

    Let’s assume that we have a dynamic array with size unknown, and we would like to constrain the size between 10 and 15. So we can just write our code as follows:

     

    program unique_array;

      class C1;
        rand bit [4:0] A [];

        //constrain A's size between 10 to 15
        constraint c1 {
          A.size insisde {[10:15]};
        }

        //use unique to generate unique elements in A
        constraint c2 {
          unique{A};
        }

      endclass

      initial begin
        C1 C1_h = new();

        repeat(10) begin
          if(C1_h.randomize())
            $display("A is %p", C1_h.A);
          end

      end

    endprogram

     

  3. Using Two Loop Iterators
  4. This idea is to use two loop iterators. If the indexes of two iterators are not the same, the elements should also be different. Since we have two nested loops, the running time is O(n^2). The code is listed below:

    program unique_array;

      class C2;
        rand bit [4:0] A [];

        //constrain A's size between 10 to 15
        constraint c1 {
          A.size insisde {[10:15]};
        }

        //use iterators and conditions to generate unique elements
        constraint c2 {
          foreach(A[i])
            foreach(A[j])
              if(i!=j)
                A[i] != A[j];
        }

      endclass

      initial begin
        C2 C2_h = new();

        repeat(10) begin
          if(C2_h.randomize())
            $display("A is %p", C2_h.A);
        end

      end

    endprogram

     

  5. Generate a(n) Ascending/Descending Array and Shuffle It.

We can also generate an ascending or a descending array and shuffle the array in the post_randomize() function. This will guarantee us to have an array with unique elements. Since we only loop around the array once, the running time is O(n), which is signficantly lower than the previous method. I am going to code an ascending array to illustrate this.

 

program unique_array;

  class C3;
    rand bit [4:0] A [];

    //constrain A's size between 10 to 15
    constraint c1 {
      A.size insisde {[10:15]};
    }

    //use iterators and conditions to generate unique elements
    constraint c2 {
      foreach(A[k])
        if(k < A.size -1) //prevent out-of-bounds
          A[k+1] > A[k]; //generate an ascending array
    }

     //shuffle all the elements
     function post_randomize();
       A.shuffle();
     endfunction

  endclass

  initial begin
    C3 C3_h = new();

    repeat(10) begin
      if(C3_h.randomize())
        $display("A is %p", C3_h.A);
    end

  end

endprogram

 

Thank you for reading. This is all I have for how to generate a dynamic array with unique elements. Leave me a comment below if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *