System Verilog Randomization
System Verilog Randomization
RANDOMIZATION
ROHIT KHANNA
Why Randomize?
Device configuration
Environment configuration
Primary input data
Encapsulated input data
Protocol exceptions
Errors and violations
Delays
Test order
Seed for the random test
module test;
integer a, b, c;
initial
repeat(20) begin
a=$random % 10; //-9 to 9 (Random range)
b={$random} % 20; //0 to 19 (Random range)
c=$unsigned($random)%15; //0 to 14 (Random range)
#2; end
endmodule
module test;
integer a, b, c;
initial
repeat(20) begin
a=10 + {$random} % 6; //10 to 15 (positive range)
b=-5 - {$random} % 6; //-5 to -10 (negative range)
c =-5 + {$random} % 16; //-5 to 10 (mix range)
#2; end
endmodule
Positive Range:
result= min + {$random} % (max – min + 1);
Negative Range:
result= -min - {$random} % (max – min + 1);
Mix Range:
result= -min + {$random} % (max + min + 1);
//min is the magnitude of minimum number
//max is the magnitude of maximum number
module test;
integer a;
initial
repeat(20)
if ({$random} % 2)
#2 a=10 + {$random} % 6; //10 to 15
else
#2 a= 3 + {$random} % 5; // 3 to 7
endmodule
module test;
integer a, count=0;
initial repeat(20)
if (count<3)
#2 a=1 + {$random} % 9; //1 to 9
else
#2 a=11 + {$random} % 8; // 11 to 18 Higher weight
endmodule
module test;
reg sign; reg [7:0] exp;
reg [22:0] mantisa; real a;
while(index!=10) begin
Generate 10 unique random
numbers temp=$random;
begin: loop
integer rec [0:9]; for(i=0; i<index; i=i+1)
integer i, temp, num, index=0; if(rec[index]==temp)
disable loop;
initial begin rec[index]=temp;
$monitor(“num=%0d”, num); index=index + 1; num=temp;
#2; end
end end
System Verilog Randomization
Result
# num=303379748
# num=-1064739199
# num=-2071669239
# num=-1309649309
# num=112818957
# num=1189058957
# num=-1295874971
# num=-1992863214
# num=15983361
# num=114806029
while(index!=10) begin
Generate 10 unique random
numbers between 0 to 99 temp={$random} % 100;
begin: loop
integer rec [0:9]; for(i=0; i<index; i=i+1)
integer i, temp, rand, index=0; if(rec[index]==temp)
disable loop;
rec[index]=temp;
index=index + 1; rand=temp;
#2; end
end
System Verilog Randomization
Result
# num=48
# num=97
# num=57
# num=87
# num=57
# num=25
# num=82
# num=61
# num=29
module test;
integer num1, num2, seed;
initial
repeat(20) begin
num1=$dist_uniform (seed, 5, 15); //5 to 15
num2=$dist_uniform (seed, -5, 10); //-5 to 10
#2; end
endmodule
module test;
integer num1, num2, seed;
initial
repeat(20) begin
#2 num1=$urandom (seed); //Unsigned 32-bit
//Random Number
num2=$urandom;
end
endmodule
module test;
integer num1, num2 , num3;
initial
repeat(20) begin
#2 num1=$urandom_range(35, 20); //35:max to 20:min
num2=$urandom_range(9); //9:max to 0:min
num3=$urandom_range(10,15); //10:min to 15:max
end
endmodule
module test;
integer num1, num2;
initial
repeat(20) begin
if(randomize(num1, num2)) //Randomize num1 and num2
$display(“Randomization Successful”);
else $display(“Randomization Failed”);
#2 ; end
endmodule
module test;
integer num;
initial
repeat(20) begin
if(randomize(num) with {num>10; num<20;} )
$display(“Randomization Successful”);
//num should be between 10 and 20 Inline Constraint
#2 ; end
endmodule
# num=19
# num=15
# num=11
# num=13
# num=15
# num=14
# num=16
# num=15
# num=17
# num=15
# num=11
# num=15
Variables declared with rand and randc are only considered for
randomization.
# num1=-1884196597 num2=0
# num1=-326718039 num2=0
# num1=1452745934 num2=0
# num1=-2130312236 num2=0
# num1=1572468983 num2=0
# num1=131041957 num2=0
# num1=1115460554 num2=0
# num1=-818992270 num2=0
# num1=2000525113 num2=0
# num1=1547354947 num2=0
# num1=1196942489 num2=0
# num1=736230661 num2=0
program test;
class sample; sample sm;
typedef struct { randc int a; initial begin
bit [3:0] b; sm=new;
} st_t; repeat(20)
rand st_t st; assert(sm.randomize())
//rand is must to randomize $display(sm.st.a);
//int present inside structure end
endclass endprogram
# 14 # 0
# 4 # 0
# 9 # 0
# 6 # 0
# 5 # 0
# 15 # 0
# 4 # 0
# 13 # 0
# 1 # 0
# 8 # 0
# 9 # 0
# 14 # 0
System Verilog Randomization
Specifying Constraints
class sample1;
rand int num;
class sample3;
constraint c { num>10;
randc int num;
num<100; }
int Max, Min;
endclass
constraint c1 { num>Min; }
class sample2; constraint c2 { num<Max; }
randc bit [7:0] num; endclass
constraint c1 { num>10; }
constraint c2 { num<100; }
endclass
System Verilog Randomization
Example1
# 22 # 72
# 22 # 53
# 29 # 66
# 27 # 79
# 46 # 68
# 43 # 69
# 33 # 78
# 43 # 95
# 46 # 65
# 36 # 34
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Randomization Fails
# Pre-Randomize
# Post-Randomize # 33
# Pre-Randomize
# Post-Randomize # 25
# Pre-Randomize
# Post-Randomize # 202
# Pre-Randomize
# Post-Randomize # 138
# Pre-Randomize
# Post-Randomize # 15
program test;
Result
B b1; # B: Pre-Randomize
# B: Post-Randomize
initial begin # B: Pre-Randomize
b1=new; # B: Post-Randomize
repeat(2)
void'(b1.randomize);
Pre-Randomize and Post-Randomize
end
of parent class are overridden
endprogram
# 33
# 25
# 202
# 138
# 138
# 138
# 138
# 238 94
# 85 48
# 202 -92
# 29 38
# 155 48
# 225 -91
# 81 -66
# 232 -82
# 85 -112
# 141 -34
# 244 -34
# 32 -34
# 9 -34
System Verilog Randomization
Example4
# 238 0
# 85 0
# 202 0
# 29 0
# 155 0
# 225 0
# 141 75
# 141 115
# 141 -24
# 141 111
# 141 -119
class packet;
rand int data;
int Max, Min;
constraint c1{ data> Min; data<Max; }
constraint c2 { Max> Min; }
task set(int Min, Max);
this.Min=Min;
this.Max=Max;
endtask
endclass
initial begin
packet p1=new;
p1.set(5, 25);
repeat(5) if(p1.randomize)
$display(“Random value=%0d”, p1.data);
p1.set(35, 20);
repeat(5) if(p1.randomize)
$display(“Random value=%0d”, p1.data);
else $display(“Randomization Failed”);
end
# Random value=14
# Random value=18
# Random value=15
# Random value=16
# Random value=16
# Randomization Failed
# Randomization Failed
# Randomization Failed
# Randomization Failed
# Randomization Failed
# a1.data=12
# a1.data=7
# a1.data=15
# a1.data=6
# a1.data=9
# a2.data=13
# a2.data=13
# a2.data=6
# a2.data=2
# a2.data=15
# a2.data=13
# a2.data=13
# a2.data=6
# a2.data=2
# a2.data=15
# a1.data=12
# a1.data=7
# a1.data=15
# a1.data=6
# a1.data=9
# a1.data=5
# a1.data=7
# a1.data=12
# a1.data=13
# a1.data=5
# a2.data=5
# a2.data=7
# a2.data=12
# a2.data=13
# a2.data=5
class packet;
rand int length, data, address;
constraint len { length==address * 5};
endclass
class packet;
rand int address;
constraint limit {address inside { [1:5], [7:11], 15, 18, 25 };}
endclass
class packet;
rand int data;
constraint limit { ( (data==5) || (data==7) || (data==9) );}
endclass
There is a better way of providing such constraints:
class packet;
rand int data;
constraint limit { data inside { 5, 7, 9}; }
endclass
class packet;
rand int data;
constraint con { data dist { 0:=40, [1:4] :=60, [6:7]:=20 };
endclass
//Total weight= 40 + 60 + 60 + 60 + 60 + 20 + 20=320
data=3 weight=60/320=18.75%
data=0 weight=40/320=12.5%
data=4 weight=60/320=18.75%
data=1 weight=60/320=18.75%
data=6 weight=20/320=6.25%
data=2 weight=60/320=18.75%
data=7 weight=20/320=6.25%
class packet;
rand int data;
constraint con { data dist { 0:/20, [1:3] :/60, [6:7]:/20 };
endclass
//Total weight= 20 + 60 + 20=100
Solution x y Probability
S1 0 0 1/8
class Unconstrained;
S2 0 1 1/8
rand bit x;
// 0 or 1 S3 0 2 1/8
rand bit [1:0] y; S4 0 3 1/8
// 0, 1, 2, or 3 S5 1 0 1/8
endclass S6 1 1 1/8
S7 1 2 1/8
S8 1 3 1/8
Solution x y Probability
class Implication1; S1 0 0 1/2
rand bit x; S2 0 1 0
// 0 or 1
S3 0 2 0
rand bit [1:0] y;
S4 0 3 0
// 0, 1, 2, or 3
constraint c { S5 1 0 1/8
(x==0) -> (y==0); } S6 1 1 1/8
endclass S7 1 2 1/8
S8 1 3 1/8
Solution x y Probability
class Implication2; S1 0 0 0
rand bit x; S2 0 1 0
// 0 or 1
S3 0 2 0
rand bit [1:0] y;
S4 0 3 0
// 0, 1, 2, or 3
constraint c { S5 1 0 0
y>0; S6 1 1 1/3
(x==0) -> (y==0); } S7 1 2 1/3
endclass S8 1 3 1/3
When used with just handle, it controls all constraints for an object.
class Packet;
rand int length;
constraint c_short { length inside { [1:32] }; }
constraint c_long { length inside { [1000:1023]}; }
endclass
Packet p;
initial begin
p = new;
// Create a long packet by disabling short constraint
p.c_short.constraint_mode(0);
assert (p.randomize());
// Create a short packet by disabling all constraints
// then enabling only the short constraint
p.constraint_mode(0);
p.c_short.constraint_mode(1);
assert (p.randomize());
end
class Transaction;
rand bit [31:0] addr, data;
constraint c1 { addr inside { [0:100], [1000:2000] }; }
endclass
Transaction t;
initial begin
t = new(); // addr is 50-100, 1000-1500, data < 10
repeat(5)
assert(t.randomize() with { addr >= 50;
addr <= 1500;
data < 10;} );
module rand_sequence1();
initial begin
repeat(5) begin
randsequence( main ) //main is production
main : one two three ; //main contains one production list
one : {$write("one");}; //one two three are production items
two : {$write("two");};
three: {$display("three");};
endsequence
end
end
endmodule : rand_sequence1
module rand_sequence2();
initial begin
repeat(7) begin
randsequence( main ) //main contains three production list
main : one| two | three ; //one two three are production list
one : {$display("one"); }; //one list will be chosen randomly
two : {$display("two"); };
three: {$display("three"); };
endsequence
end
end
endmodule : rand_sequence2
# one
# one
# one
# one
# three
# one
# two
module rand_sequence3();
initial begin
repeat(50) begin
randsequence( main )
main : one:=5 | two:=2 | three:=3 ; //production list with weights
one : {$display("one");};
two : {$display("two");};
three: {$display("three");};
endsequence
end
end
endmodule : rand_sequence3
module rand_sequence4();
int one_1, two_2, three_3; bit on;
initial begin
repeat(100) begin
randsequence( main )
main : one three;
one : if(on) incr_one else incr_two;
incr_one : {one_1 ++; on=~on;};
incr_two : {two_2 ++; };
three: {three_3++;};
endsequence end end
endmodule : rand_sequence4
module rand_sequence5();
initial for (int i = 0 ; i < 10 ; i++)
randsequence( main )
main : case(i %3)
0 : zero;
1, 2 : non_zero;
default : def;
endcase
zero : {$display("zero");};
non_zero : {$display("non_zero");};
def : {$display("default");};
endsequence
endmodule : rand_sequence5
System Verilog Randomization