Lec8 Randomization
Lec8 Randomization
Randomization
Topics
Randomization Overview
Why Randomize?
Random testing detects the bugs you did not expect in your
design
What to Randomize?
Device configurations
What to Randomize?
Environment configurations
What to Randomize?
What to Randomize?
Delays
Randomization in SystemVerilog
Overview
Randomization in SystemVerilog
rand keyword
The probability of the same value repeating on successive calls to randomize is 1/256
If unconstrained y is assigned any value in the range 0 to 255 with equal probability
The probability of a value occurring on simultaneous calls to randomize is 1/256
Randomization in SystemVerilog
randc keyword
randc randomly iterates over all the values in the range and no value is
repeated within an iteration
When the iteration finishes, a new iteration automatically starts
y can take values 0,1,2,3
initial permutation 0 -> 3 -> 2 -> 1
next permutation
next permutation
Randomization in SystemVerilog
The Bus class models a simplified bus with two random variables:
addr and data
The range1 constraint specifies the limits for the values of addr
class Bus
rand bit[15:0] addr;
randc bit[31:0] data;
constraint range1 {addr>1024; addr<16384;}
endclass
random variable
random cyclic: the random solver
will not repeat a permutation of
values
Randomization in SystemVerilog
randomize()function
Randomization in SystemVerilog
Constraint Details
Constraint Blocks
In the example below randomize() fails sometimes and succeeds sometimes, why?
Error!!!
rand bit[15:0] addr;
constraint_identifier
constraint_block
class Bus
bit[15:0] addr;
randc bit[31:0] data;
constraint range1 {addr>1024; addr<16384;}
endclass
Constraint Details
Simple expressions
There can be only one relational operator (<, <=, ==, >= or >)
For multiple variables use multiple expressions
right way
class Bus
rand bit[15:0] a,b,c;
constraint c1 { 0 < a;
a < b;
b < c;}
endclass
class Bus
rand bit[15:0] addr;
randc bit[31:0] data;
constraint range1
{
addr inside {[0:100],[1024:16384]};
data > 1000;
data < 10000;
}
endclass
Weighted Distribution
right way
rand integer a;
constraint c1 {a dist {0:=1, 1:=5}};
Weighted Distribution
Another example
rand int src, dst;
costraint c1 {
src dist {0:=40, [1:3]:=60};
dst dist {0:=40, [1:3]:/60};
}
src
weight
dst
weight
40/220
40/100
60/220
20/100
60/220
20/100
60/220
20/100
Bidirectional Constraints
Bidirectional Constraints
Solution
26
26
27
26
26
28
26
26
29
27
27
28
27
27
29
28
28
29
Conditional Constraints
if(mode==small)
len<10;
else if (mode==large)
len>100;
Solution Probabilities
Unconstrained
Probability of distribution
class Unconstrained;
rand bit x;
rand bit [1:0] y;
endclass
Solution
Probability
1/8
1/8
1/8
1/8
1/8
1/8
1/8
1/8
Solution Probabilities
Implication
class Imp1;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
}
endclass
Solution
Probability
1/2
1/8
1/8
1/8
1
3
Solutions for Imp1 class
1/8
Solution Probabilities
class Imp_Bid;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
y>0;
(x==0)->y==0;
}
endclass
Solution
Probability
1/3
1/3
1
3
Solutions for Imp_Bid class
1/3
Solution Probabilities
Solvebefore
Solvebefore does not change the solution space but changes the
probability of the results
class SolveBefore;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve y before x;
}
endclass
Solution Probabilities
class SolveBefore;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve y before x;
}
endclass
Unconstrained
Probability
1/8
1/8
1/8
1/8
1/8
1/8
1/4
1/8
1/8
1/4
1/8
1/8
1/4
Solution
Possibility
Probability
Solution Probabilities
Solvebefore
Solvebefore does not change the solution space but changes the
probability of the results
class SolveBefore;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve y before x;
}
endclass
Solution
Probability
1/8
1/8
1/4
1/4
1/4
Solution Probabilities
Solvebefore
class Imp4;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve x before y;
}
endclass
Solution
Probability
1/2
1/8
1/8
1/8
H
1
3
1/8
Solutions for solve x before y constraint
Constraints
Iterative constraints
Meaning
Description
OFF
Sets the specified variable to inactive so that they are not randomized on
subsequent calls to randomize() method
ON
class Packet;
rand integer src, dst;
endclass
int r;
Packet packet_a=new;
packet_a.rand_mode(0);
packet_a.src.rand_mode(1);
r=packet_a.dst.rand_mode();
Controlling Constraints
Meaning
Description
OFF
ON
class Packet;
rand integer src, dst;
constraint filter{src>2*dst;}
endclass
function integer toggle_rand (Packet p);
if (p.filter.constraint_mode()==1)
p.filter.constraint_mode(0);
else
p.filter.constraint_mode(1);
toggle_rand=p.randomize();
endfunction
Thank You
Constraints
Iterative constraints
bit [3:0][2:1] B [5:1][4]
int A [2][3][4];
1
3
2
4
5:1
foreach (A[i,j,k])
4
i iterates from 0 to 1
j iterates from 0 to 2
k iterates from 0 to 3
foreach (B[q,r,,s])
q iterates from 5 to 1
r iterates from 0 to 3
s iterates from 2 to 1
33
Constraints
Functions in constraints
For instance computing the sum of ones in a packed array uses a loop
Without the loop the loop will have to be unrolled
34
Constraints
Constraint guards
They are not logical relations that have to be satisfied by the constraint solver
Prevents the solver from generating evaluation errors
Constants
State variables
Object handle comparisons
The constraint will fail on the last element due to a non existent handle in the linked list
class SList;
rand int n;
constraint sort{n<next.n;}
endclass
if statement acts as a constraint guard
class SList;
rand int n;
constraint sort{if(next!=null)n<next.n;}
endclass
2011, Dr. Meeta Yadav, www.asicverification.org
35
Constraints
Constraint guards
Case
a.x
b.x
Constraint
!0
error
x+y=10
error
always error
!0
!0
10
20
36
Constraints
Constraint guards
class D
int x;
endclass
class C;
rand int x,y;
D a,b;
constraint C1{(x<y && a.x > b.x && a.x==5) -> x+y==10;}
endclass
Case
a.x
b.x
Constraint
!0
error
error
error
always error
!0
!0
37