Sometime verification scenarios require creating sets of random instructions or addresses with no repeating values, usually represented as elements in a dynamic array. Earlier versions of SystemVerilog required you to use either nested foreach loops to constrain all combinations of array elements so that they would not be equal to each other. Or else repeatedly randomize one element at a time, and then constraining the next element to not be in the list of already generated values.
The new unique constraint (new feature of 1800-2012) lets you use one statement to constrain a set of variables or array elements to have unique values. When randomized, this class generates a set of ten unique values from 0 to 15.
class set_unique_val;
rand bit [3:0] data[10];
constraint uniq {
The new unique constraint (new feature of 1800-2012) lets you use one statement to constrain a set of variables or array elements to have unique values. When randomized, this class generates a set of ten unique values from 0 to 15.
class set_unique_val;
rand bit [3:0] data[10];
constraint uniq {
unique {data};
}
endclass : set_unique_val
You can also add other non-random variables to the set of unique values which has the effect of excluding the values of those variables from the set of unique values. When randomized, this class generates a set of ten unique values excluding the values 0, 5 and 15.
class set_unique_val;
rand bit [3:0] data[10];
const bit [3:0] excludes[] = {0, 15};
constraint uniq {
endclass : set_unique_val
You can also add other non-random variables to the set of unique values which has the effect of excluding the values of those variables from the set of unique values. When randomized, this class generates a set of ten unique values excluding the values 0, 5 and 15.
class set_unique_val;
rand bit [3:0] data[10];
const bit [3:0] excludes[] = {0, 15};
constraint uniq {
unique {data, excludes, 5};
}
endclass : set_unique_val
endclass : set_unique_val
How do you generate the same without using random and constraints?
ReplyDeleteWithout using Random variable and constraint, you can generate array of random unique values using below code, but it is not fully random.
Deleteint unsigned data[10];
initial begin
foreach (data[i]) begin
data[i] = i;
// or data[i] = i * i;
end
data.shuffle();
end
module for_each;
Deleteint unsigned a[10];
initial begin
$display();
foreach (a[i]) a[i] = i; // or data[i] = i * i;
a.shuffle();
foreach (a[i]) $display("valueof a = %0d" , i, a[i]);
end
endmodule
Hi, have you tried it with dynamic arrays.
ReplyDeleteFor dynamic arrays also the method remains same, but first you should create the dynamic array of desired size.
Deleteunique is not working......throughing error at unique keyword
ReplyDeleteHow to generate arry of sequence data.. Without Randomizing.
ReplyDeleteFor eg: I have one bit data I need to pass {1,0,1,1,0,1}
how can we generate unique array without using unique keyword?
ReplyDeleteIamlinkfeeder
ReplyDeleteIamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder