Monday 2 April 2018

How to create array with random but unique values in system verilog

In verification environment, sometimes need to generate random but unique values for stimulus.
There are different ways to generate random and unique values. Although System Verilog provides "unique" keyword to generate unique values in randomization.

Below is the simple example to generate data array with the random but unique values.

class test_randomize;
  rand bit [7:0] data[];
  rand bit [7:0] len;
 
  constraint arr_len {
    data.size() == len;
  }
  
  constraint unique_arr_val {
    unique{data};  
  }
 
  function new();
    $display("Class test_randomize is created.");
  endfunction
 
  function void display();
    foreach(data[i]) begin
      $display("data[%0d]=%0h", i, data[i]);
    end
  endfunction
 
endclass
module top();
 
  initial begin
    test_randomize t1 = new();
    t1.randomize();
    t1. display();
  end
endmodule