KEMBAR78
SystemVerilog Randomization Guide | PDF | Inheritance (Object Oriented Programming) | Programming Paradigms
0% found this document useful (0 votes)
73 views49 pages

SystemVerilog Randomization Guide

This document discusses constrained randomization in SystemVerilog. It provides examples of using $random, $urandom_range, and the randomize() function to generate random numbers within specified ranges. It also discusses randomizing properties of class objects using rand and randc variables, and controlling randomization with rand_mode. Pre_randomize and post_randomize functions allow adding functionality before and after randomization.

Uploaded by

Srinivas Aluvala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views49 pages

SystemVerilog Randomization Guide

This document discusses constrained randomization in SystemVerilog. It provides examples of using $random, $urandom_range, and the randomize() function to generate random numbers within specified ranges. It also discusses randomizing properties of class objects using rand and randc variables, and controlling randomization with rand_mode. Pre_randomize and post_randomize functions allow adding functionality before and after randomization.

Uploaded by

Srinivas Aluvala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

System Verilog

RANDOMIZATION
Verilog Constrained Randomization

Futurewiz
www.futurewiz.co.in
Random in range

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
Futurewiz
www.futurewiz.co.in
Random in range

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

Futurewiz
www.futurewiz.co.in
Weighted Random numbers

module test;
integer a, count=0;

always if(count< 10) #2 count=count+1; else #2 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
Futurewiz
www.futurewiz.co.in
SV Constrained Randomization

Futurewiz
www.futurewiz.co.in
$urandom_range

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

Futurewiz
www.futurewiz.co.in
Randomize function

 SV provides scope randomize function which is used to


randomize variables present in current scope.

 randomize() function can accept any number of variables


which have to be randomized as an arguments.

 This function returns true if randomization was successful


else false.

 User can also provide inline constraints to control range of


random values.
Futurewiz
www.futurewiz.co.in
Randomize function

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
Futurewiz
www.futurewiz.co.in
Randomize function with constraint

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
Futurewiz
www.futurewiz.co.in
Result

# num=19
# num=15
# num=11
# num=13
# num=15
# num=14
# num=16
# num=15
# num=17
# num=15
# num=11
# num=15
Futurewiz
www.futurewiz.co.in
Randomize Object Properties

 In SV properties (variables) inside a class can also be


randomized.

 Variables declared with rand and randc are only considered


for randomization.

 A class built-in randomize function is used to randomized


rand and randc variables.

 User can also specify constraint blocks to constrain random


value generation.
Futurewiz
www.futurewiz.co.in
rand vs randc

 Variables defined with rand keyword, distribute values uniformly.

rand bit [1:0] num1;


num1: 3, 2 , 0, 3, 0, 1, 2, 1, 3

 Variables defined with randc keyword, distribute values in a cyclic


fashion without any repetition within an iteration.

randc bit [1:0] num2;


num2: 3, 2, 0, 1
0, 2, 1, 3
1, 3, 0, 2

Futurewiz
www.futurewiz.co.in
Example1

program test; class sample;


sample sm; rand int num1;
initial begin
int num2;
sm=new;
repeat(20) endclass
assert(sm.randomize()) //assert checks randomization status
$display(“num1=%0d num2=%0d”, sm.num1, sm.num2);
end
endprogram

num1 is randomized num2 remains untouched

Futurewiz
www.futurewiz.co.in
Result

# 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
Futurewiz
www.futurewiz.co.in
pre_randomize and post_randomize

 Every class contains pre_randomize and post_randomize functions


which are evoked every time randomize function is called.

 When randomize function is called, it first evokes pre_randomize and


then randomization is done.

 post_randomize function is only called if randomization was successful.

 pre_randomize and post_randomize functions can be written in a class


to offer user defined functionality before and after randomization.

Futurewiz
www.futurewiz.co.in
Example1

class packet; program test;


rand bit [7:0] data; packet pkt;

function void pre_randomize; initial begin


$display(“Pre-Randomize”); pkt=new;
endfunction repeat(5) begin
void'(pkt.randomize);
function void post_randomize;
$display(pkt.data);
$display(“Post-Randomize”);
end
endfunction
end
endclass endprogram
Futurewiz
www.futurewiz.co.in
Result

# 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

Futurewiz
www.futurewiz.co.in
Example2

class A; class B extends A;

function void pre_randomize; function void pre_randomize;


$display(“B: Pre-Randomize”);
$display(“A: Pre-Randomize”);
endfunction
endfunction
function void post_randomize;
function void post_randomize; $display(“B: Post-Randomize”);
$display(“A: Post-Randomize”); endfunction
endfunction endclass

endclass

Futurewiz
www.futurewiz.co.in
Example2

program test;
Result
B b1; # B: Pre-
Randomize
initial begin # B: Post-
b1=new; Randomize
# B: Pre-
repeat(2)
Randomize
void'(b1.randomize); # B: Post-
end Randomize
endprogram
Pre-Randomize and Post-
Randomize of parent class are
overridden Futurewiz
www.futurewiz.co.in
Controlling Randomization

 Randomization nature of rand and randc variables can be turned on/off


dynamically.

 rand_mode method is used to change randomization status of rand and


randc variable.

 When used as a task, the argument determines the state of rand and
randc variables.

 When argument is 0 then randomization is disabled(turned-off), when


argument is 1 then randomization is enabled(turned-on).

Futurewiz
www.futurewiz.co.in
Example1

class packet; repeat(4) begin


rand bit [7:0] data; void'(pkt.randomize);
$display(pkt.data); end
endclass
pkt.rand_mode(0);
//Disabling Randomization
program test; repeat(3) begin
packet pkt; void'(pkt.randomize)
$display(pkt.data);
initial begin
end end
pkt=new;
endprogram
Futurewiz
www.futurewiz.co.in
Result

# 33
# 25
# 202
# 138
# 138
# 138
# 138

Futurewiz
www.futurewiz.co.in
Example3

class packet; repeat(10) if(pkt.randomize)


rand bit [7:0] data1; $display(pkt.data1, pkt.data2);
rand byte data2; pkt.data2.rand_mode(0);
//turn off for data2
endclass
repeat(10) if(pkt.randomize)
$display(pkt.data1, pkt.data2);
program test; pkt.data2.rand_mode(1);
packet pkt; repeat(10) if(pkt.randomize)
initial begin $display(pkt.data1, pkt.data2); end
pkt=new; endprogram

Futurewiz
www.futurewiz.co.in
Result

# 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 Futurewiz
www.futurewiz.co.in
Example4

class packet; repeat(10)


rand bit [7:0] data1; if(pkt.randomize)
byte data2; $display(pkt.data1, pkt.data2);
repeat(10)
endclass
if(pkt.randomize(data2))
program test; //will only randomize data2
packet pkt; $display(pkt.data1, pkt.data2);
initial begin end
pkt=new; endprogram

Futurewiz
www.futurewiz.co.in
Result

# 238 0
# 85 0
# 202 0
# 29 0
# 155 0
# 225 0
# 141 75
# 141 115
# 141 -24
# 141 111
# 141 -119

Futurewiz
www.futurewiz.co.in
Example5

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

Futurewiz
www.futurewiz.co.in
Example5

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

Futurewiz
www.futurewiz.co.in
Result

# 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

Futurewiz
www.futurewiz.co.in
Set Membership

 User can use inside operator to set membership in a


constraint block.

 Example: To limit address in range from 1 to 5, 7 to 11 and


to a set of values 15, 18, 25.

class packet;
rand int address;
constraint limit {address inside { [1:5], [7:11], 15, 18, 25
};}
endclass

Futurewiz
www.futurewiz.co.in
Set Membership

 A ! Operator can be used to exclude set of values


class packet;
rand int address;
constraint limit { !(address inside { 6, [12:14]} ) ;}
endclass

 Using arrays to set membership.


class packet;
int arr [ ]= `{ 5, 7, 11, 13, 19};
rand int address;
constraint limit { address inside { arr }; }
endclass

Futurewiz
www.futurewiz.co.in
Set Membership

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

Futurewiz
www.futurewiz.co.in
Weighted Distribution

 User can provide weights for random numbers to obtain


non-uniform distribution.

 := operator is used to assign same weight to all the values.

 :/ operator is used to distribute weight among all the values.

 dist operator is used to specify distribution.

 Weighted distribution does not work on randc variables.

 Example: constraint con { src dist { 0:=40, [1:3] :=60 };


dst dist { 0:/40 , [1:3] :/60 }; }
Futurewiz
www.futurewiz.co.in
Example1

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%

Futurewiz
www.futurewiz.co.in
Example2

class packet;
rand int data;
constraint con { data dist { 0:/20, [1:3] :/60, [6:7]:/20 }; }
endclass
//Total weight= 20 + 60 + 20=100

data=0 weight=20/100=20% data=3 weight=20/100=20%


data=1 weight=20/100=20% data=6 weight=10/100=10%
data=2 weight=20/100=20% data=7 weight=10/100=10%

Futurewiz
www.futurewiz.co.in
Example3

typedef enum {Red, Green, Blue} color;


class temp;
rand color col;
int redw=5, greenw=3, bluew=4;
constraint weight { col dist { Red:=redw,
Green:=greenw,
Blue:=bluew}; }
endclass

Futurewiz
www.futurewiz.co.in
Bidirectional Constraints

 Constraints are not procedural but declarative.

 All constraints should be active at same time.

rand bit [15:0] a, b, c; Solution a b c


constraint cp { a < c; S1 6 6 7
b == a; S2 6 6 8
c < 10; S3 6 6 9
b > 5; S4 7 7 8
} S5 7 7 9
S6 8 8 9
 Even though there is no direct constraint on lower value of
c, constraint on b restricts choices.
Futurewiz
www.futurewiz.co.in
Implication Constraints

constraint mode_c { if (mode == small)


len < 10;
else if (mode == large)
len > 100; }

Is equivalent to
constraint mode_c { (mode == small) -> len < 10;
(mode == large) -> len > 100; }
 If mode is small that implies length should be less than 10.
 If mode is large that implies length should be more than 100.
 Implication helps in creating case like blocks.

Futurewiz
www.futurewiz.co.in
Controlling Constraints

 Constraints can be turned on/off during runtime.

 constraint_mode() is used to achieve this capability.

 When used with handle.constraint, this method controls a single


constraint.

 When used with just handle, it controls all constraints for an object.

 To turn off constraint, 0 is passed as an argument to constraint_mode


and to turn on, 1 is passed as an argument.

Futurewiz
www.futurewiz.co.in
Example

class Packet;
rand int length;
constraint c_short { length inside { [1:32] }; }
constraint c_long { length inside { [1000:1023]};
}
endclass

Futurewiz
www.futurewiz.co.in
Example

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

Futurewiz
www.futurewiz.co.in
Inline Constraints

 New constraints can be added to existing constraints while


calling randomize function using randomize with.

 constraint_mode can be used disable any conflicting


constraints.
class Transaction;
rand bit [31:0] addr, data;
constraint c1 { addr inside { [0:100], [1000:2000] }; }
endclass

Futurewiz
www.futurewiz.co.in
Inline Constraints

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;} );

repeat(5) // force addr to a specific value, data > 10


assert(t.randomize() with { addr == 2000;
data > 10; } );
end
Futurewiz
www.futurewiz.co.in
Constraint in Inheritance

 Additional constraints can be provided in a subclass


class(child class). Child class object has to fulfill both the
constraints (parent and child).
class base; class child extends base;
rand int data; constraint limit2 { data > 50;
constraint limit1 { data> 0; }
data< 100; } endclass
endclass
Parent: 0 < data < 100 Child: 50 < data < 100

Futurewiz
www.futurewiz.co.in
Example2

class base; class child extends base;


rand int data; constraint limit2 { data ==
constraint limit1 { data> 0; 50;
data< 100; }
} endclass
endclass

Parent: 0 < data < 100 Child: data=50

Futurewiz
www.futurewiz.co.in
Example3

class base; class child extends base;


rand int data; constraint limit2 { data > 10;
constraint limit1 { data> 40; data< 30;
data< 50; }
} endclass
endclass

Parent: 40 < data < 50

Randomization Fails because both constraints are not satisfied

Futurewiz
www.futurewiz.co.in
Example 4

class base; class child extends base;


rand int data; rand int data;
constraint limit1 { data> 40; constraint limit2 { data > 10;
data< 50; data< 30;
} }
endclass endclass

Parent: 40 < data < 50 Child: 10 < data < 30

Parent data is different as compared to child data. Data Overridden

Futurewiz
www.futurewiz.co.in
Constraint in Inheritance

 Constraints are overridden in child class if they are defined


with same name as that present in parent class.
class base; class child extends base;
rand int data; constraint limit { data > 50;
constraint limit { data> 20; data < 90; }
data< 40; } endclass
endclass

Parent: 20 < data < 40 Child: 50 < data < 90

Constraints are overridden


Futurewiz
www.futurewiz.co.in

You might also like