Systemverilog Array Randomization

SystemVerilog Randomize Array


Most of the array usage application needs randomization of array. randomization is possible for array size as well as for array elements.

constrained randomization of array


  • It is possible to get the specific value on randomization, this can be achieve by writing/specifying the constraints.
  • During randomization, constraints of size are solved first, and then the elements constraints.

Array randomization is applicable to all the array types, Below section describes about examples on array randomization and using array methods in constrained randomization.


Fixed Size Array Randomization

In fixed size array, randomization is possible only for the array elements. as the size is fixed, it is not possible to change.

Generating random value for array elements.

In below example random values will be generated for array elements.

  1. Declare array as rand
  2. On randomization array will get random values

class fs_array;
  rand bit [3:0] array1[4];
  rand bit [7:0] array2[6];
  
  function void display();
    $display("array1 = %p",array1);
    $display("array2 = %p",array2);
  endfunction
endclass

program fixedsize_array_randomization;
  fs_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
  
endprogram 

 Simulator Output: 

array1 = '{'he, 'h4, 'h4, 'h8}
array2 = '{'h9b, 'h9a, 'h10, 'h5f, 'hde, 'h84}

Execute the above code on 

Generate unique elements in an array

In above example we have seen randomization with random values. Below example shows the randomization with unique values by using shuffle array method.
  1. Constrain array with element value same as index value 
  2. In post randomization shuffle the array, so that array will not have an incremental values.

class fs_array;
  rand bit [7:0] array1[6];
  
  constraint array_c { foreach(array1[i]) array1[i] == i;}
  
  function void post_randomize();
    array1.shuffle();  
  endfunction
  
  function void display();
    $display("array1 = %p",array1);
  endfunction
  
endclass

program fixedsize_array_randomization;
  fs_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

 Simulator Output: 

array1 = '{'h2, 'h1, 'h4, 'h0, 'h5, 'h3}

Execute the above code on 

array sum constraint 

In below example, array is randomized in such a way that sum of all the elements equals to 30. 

  1. declare array with rand.
  2. Constraint sum of array using array method sum().

class fs_array;
  rand bit [7:0] array1[6];
  
  constraint array_c { array1.sum() == 30;}
  
  function void display();
    $display("array1 = %p",array1);
  endfunction
  
endclass

program fixedsize_array_randomization;
  fs_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

 Simulator Output: 

array1 = '{'h7, 'h0, 'h17, 'h0, 'h0, 'h0}

Execute the above code on 

Array sum constraint with array elements

In previous example only sum of array elements is considered, array elements can take any value. But in below example array sum and also value of each element is constrained.

  1. declare array with rand
  2. Constraint sum of array using array method sum().
  3. Constraint array elements value.

class fs_array;
  rand bit [7:0] array1[6];
  
  constraint array_c { array1.sum() == 30;}
  constraint array_c_e { foreach(array1[i]) array1[i] > 2;}
  
  function void display();
    $display("array1 = %p",array1);
  endfunction
  
endclass

program fixedsize_array_randomization;
  fs_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

 Simulator Output: 

array1 = '{'h7, 'h3, 'hb, 'h3, 'h3, 'h3}

Execute the above code on 

Multi dimensional

Below example shows declaration and randomization of multi dimensional array with constraint.


cclass fs_array;
  rand bit [7:0] array1[3][4];
  
  constraint array_c { foreach(array1[i,j]) array1[i][j] == i*j;}
  
  function void display();
    $display("array1 = %p",array1);
  endfunction
  
endclass

program fixedsize_array_randomization;
  fs_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end 
endprogram 

 Simulator Output: 

array1 = '{'{'h0, 'h0, 'h0, 'h0}, '{'h0, 'h1, 'h2, 'h3}, '{'h0, 'h2, 'h4, 'h6}}

Execute the above code on 

Multi dimensional array with sum method

In below example sum of array elements is constrained.


cclass fs_array;
  rand bit [7:0] array1[3][4];
  
  constraint array_c { array1.sum() == 30;}
  
  function void display();
    $display("array1 = %p",array1);
  endfunction
  
endclass

program fixedsize_array_randomization;
  fs_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end 
endprogram 

 Simulator Output: 

array1 = '{'{'h0, 'h0, 'h16, 'h0}, '{'h0, 'h0, 'h0, 'h0}, '{'h0, 'h0, 'h8, 'h0}} 

Execute the above code on