8.randomization 1up
8.randomization 1up
Why Randomize?
Solution is Constrained Random Testcases (CRT) which randomize the input stimulus
What to Randomize?
• Device configurations
Try different device configurations
For instance for a switch verification try different configurations of
input and output ports
• Environment configurations
Randomize the entire environment that the device operates in
The number of objects
Object configurations
For instance randomize the number of PCI busses connected to a
particular device
• Delays
Use random legal delays specified by the protocols
Check for clock jitter sensitivity
Do not try to violate set up and hold requirements
• Overview
Randomization enables users to automatically generate random input
stimulus for functional verification
SystemVerilog enables user to specify random constrained (legal)
values
Random constraints should be specified using OOP
• rand keyword
Random variables are declared with the rand keyword
Their values are uniformly distributed over the specified range
If unconstrained the variable shall assign any value in the specified range
with equal probability
rand bit [7:0] y; 8-bit unsigned integer with the range 0 to 255
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
• randc keyword
Random cyclic variables are declared with the randc keyword
They cycle through all the values in a random permutation of their
declared range
Can only be of the type bit or enum
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
class Bus
rand bit[15:0] addr; random variable
randc bit[31:0] data; random cyclic: the random solver
constraint range1 {addr>1024; addr<16384;} will not repeat a permutation of
endclass values
Example: Simple class with random variables constraining the random variables
to values between 1024 and
16384
Bus bus=new;
repeat (50) begin
if(bus.randomize()==1)
$display(bus.addr, bus.data);
else
$display (“Randomization failed”);
end
Example: randomize() function example
In the example below randomize() fails sometimes and succeeds sometimes, why?
constraint_identifier constraint_block
Error!!!
rand bit[15:0] addr;
class Bus
bit[15:0] addr;
randc bit[31:0] data;
constraint range1 {addr>1024; addr<16384;}
endclass
Example: Simple constraint example
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
Example: Constraint example with inside operator
©2008, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 15
Constraint Details: Quiz
Declare constraint blocks c1,c2,c3 and c4 so that variable x, a and v
get the values shown:
x takes any values described in the constraint_block :
• x is: 3,5,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31,32
• x is: 3,5,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31,32,
between y and 2y, z
rand integer x,y,z;
constraint c1 {x inside {3,5,[9:15],[24:32];}
constraint c1 {x inside {3,5,[9:15],[24:32],[y:2*y],z};}
• Another example
rand int src, dst;
costraint c1 {
src dist {0:=40, [1:3]:=60};
dst dist {0:=40, [1:3]:/60};
}
Example: Weighted random distribution with dist
x!=200;
x dist {100:=1, 300:=5}
• Bidirectional Constraints
Constraint blocks are not procedural but declarative
All active at one time
Solution b c d
rand logic [15:0] b,c,d;
costraint c1 { A 26 26 27
b<d;
c==b; B 26 26 28
d<30; C 26 26 29
c>=25;
} D 27 27 28
Example: Bidirectional constraints E 27 27 29
F 28 28 29
Solutions for Bidirectional Constraints
if(mode==small)
len<10; mode == small -> len<10;
else if (mode==large) mode == large -> len>100
len>100;
Example: if…else example Example: Equivalent implication example
Solution x y Probability
A 0 0 1/8
B 0 1 1/8
class Unconstrained;
rand bit x; C 0 2 1/8
rand bit [1:0] y;
D 0 3 1/8
endclass
E 1 0 1/8
Example: Class with unconstrained random variables
F 1 1 1/8
There are 8 possible solutions and G 1 2 1/8
because there are no constraints each
has the same probability H 1 3 1/8
Solutions for Unconstrained class
class SolveBefore;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve y before x;
}
endclass
Example: Class with implication and solve…before
Solution y x Probability
class SolveBefore;
A 0 0 1/8
rand bit x;
rand bit [1:0] y; B 0 1 1/8
constraint c_xy {
C 1 0 0
(x==0)->y==0;
solve y before x; D 1 1 1/4
}
E 2 0 0
endclass
F 2 1 1/4
Example: Class with implication and solve…before
G 3 0 0
H 3 1 1/4
Solutions for solve y before x constraint
Solution x y Probability
class Imp4; A 0 0 1/2
rand bit x;
rand bit [1:0] y; B 0 1 0
constraint c_xy { C 0 2 0
(x==0)->y==0;
solve x before y; D 0 3 0
} E 1 0 1/8
endclass
F 1 1 1/8
Example: Class with implication and solve…before
G 1 2 1/8
H 1 3 1/8
Solutions for solve x before y constraint
class C;
rand byte A[4];
constraint C1{ foreach (A[i]) A[i]inside {2,4,8,16};}
constraint C2{ foreach (A[j]) A[j]> 2*j;}
endclass
3 3 2 1 0
4 5:1
foreach (A[i,j,k])…
4
i iterates from 0 to 1
j iterates from 0 to 2 foreach (B[q,r,,s])…
k iterates from 0 to 3
q iterates from 5 to 1
r iterates from 0 to 3
s iterates from 2 to 1
class B;
rand int x,y;
constraint C{x<=F(y);} Forces y to be solved before x
constraint D{ y inside {2,4,8};}
endclass
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
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
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();
class Packet;
rand integer src, dst;
constraint filter{src>2*dst;}
endclass