Randomization Flashcards
Why CRT
Why CRT
-With large designs, challenging to write directed tests to cover all features
-Solution : Auto generated constrained-random tests(CRT)
-Pros :
*Efficient
*Detects unanticipated bugs
-Cons :
*Bringup complexity
**Directed tests have defined test suite, known input/output pair
**For CRT, we need to have a reference model, that predicts expected output for a random input)
CRT requirements
-Random variable
-Constraints to control random stimulus
-Reference model
-Seed to PRNG
Coverpoint outside class ??
class transaction;
rand bit[7:0] addr;
rand bit[31:0] data;
endclass
covergroup cg_tran; coverpoint tr.addr; endgroup module top; initial begin transaction tr; tr = new(); cg_tran = new(); for(int i=0;i<100;i++) begin tr.randomize(); cg_trans.sample(); end end endmodule : top
What is to be randomized
-Device configuration
*config combinations
-Environment configuration
*active masters/slaves combinations
-Primary input data
-Encapsulated input data
*Multilayer stimulus, nested simulus classes
-Delays
*Responses,clk jitter
-Protocol exceptions,errors,violations
*Injection of failures
Constraint and use
-If DUT’s valid stimulus cannot be a random value
-Constraints set rules to limit the random values to legal values
-A constraint is set of relational expressions that must be true for the chosen value of the variables.
Sample randomization value
class Packet;
rand bit [31:0] src, dst, data[8];
randc bit [7:0] kind;
constraint c {src > 10;
src < 15;}
endclass
Packet p;
initial begin
p = new();
assert (p.randomize())
else $fatal(0, “Packet::randomize failed”);
transmit(p);
end
Randomization failure
-Returns 0 for FAIL
-Returns 1 for SUCCESS
if(!p.randomize())
$fatal(0, “Packet::randomize failed”);
(or)
assert (p.randomize()) else $fatal(0, “Packet::randomize failed”);
(or)
assert (p.randomize());
How random values are generated?
-User writes constraints
-SV’s PRNG (which takes seed as input) generates random values
-Constraint random solver : Evaluates constraints and picks valid values from generated random values
NOTE : PRNG only generates 2-state values
-So when we declare 4-state random variable, only 2-state values are assigned
Nested random classes
-When we have nested random classes, parent class randomization calls sub-class randomization(if child class obj is declared as rand)
What can be randomized
-Fixed sized : integral/packed array/enum/struct/bit
-Dynamic sized : If len is not constrainted : empty array
If len is constrainted : array/queue is created and randomized
-String : Cannot be randomized
-Handles :
**Randomization cannot create objects. They have to created
-Handle/handle array/queue/Dynmic array
**Each inst has to be created/newed
**If declared rand, parent randomization, applies to handle
Constraint in non-random value
When randomize is called, it fails if var doesnt satisfy constraint condition
If I want add priority of random values
-We can use WEIGHTED DISTRIBUTION to choose few random values more frequently.
- := ( nth member wt == total wt )
- :/ ( nth member wt == total wt/n)
- Randc variables cannot be used with weighted distribution
-
int wt1=11,wt2=22,wt3=22,wt4=44;
constraint c{
val1 dist { 1:=wt1, [2:3]:=wt2; };
}
constraint c{
val2 dist { 1:/wt3, [2:3]:/wt4; };
}
How to generate values of specific value
class v_class;
unsigned int v_arr1[] = ‘{ 6,15,8,12 };
unsigned int v_arr2[] = ‘{ 120,130 };
unsigned int v_val;
constraint c {
v_val inside { [$:2], v_arr1, [100:150] };
!(v_val inside { v_arr2 });
}
endclass
Conditional contraints
- We can use implication/if conditions for enabling constraints
constraint c1{
(unaligned_addr==1) -> addr[1:0] inside {[1:3]};
}
constraint c1{
if(unaligned_addr==1)) {
addr[1:0] inside {[1:3]};
}
}
Prioritize constraints
- Constraints are procedural/bidirectionals
- All are active/evaluated in parallel, no sequential
- We can order them if needed, using :: solve <> before <>;
Probability of each value
rand bit x;
rand bit[1:0] y;
constraint c { (x==0) -> (y==0); }
- (x,y) :
(0,0) = 1/2
(1,0) = 1/8
(1,1) = 1/8
(1,2) = 1/8
(1,3) = 1/8
Probability of each value
rand bit x;
rand bit[1:0] y;
constraint c {
y > 0;
(x==0) -> (y==0);
}
- (x,y) :
(1,1) = 1/3
(1,2) = 1/3
(1,3) = 1/3
Probability of each value
rand bit x;
rand bit[1:0] y;
constraint c {
(x==0) -> (y==0);
solve x before y;
}
- (x,y) :
(0,0) = 1/2
(1,0) = 1/8
(1,1) = 1/8
(1,2) = 1/8
Probability of each value
rand bit x;
rand bit[1:0] y;
constraint c {
(x==0) -> (y==0);
solve y before x;
}
- (x,y) :
(0,0) = 1/8
(1,0) = 1/8
(1,1) = 1/4
(1,2) = 1/4
(1,3) = 1/4
Controlling randomness
-Enable/disable constraints
handle.constraint_mode(0); : OFF
handle.constraint_mode(1); : ON
handle.constraint.constraint_mode(0); : OFF
handle.constraint.constraint_mode(1); : ON
Randomization here ignored rules
-Disable variable randomness
obj.var.rand_mode(0) : OFF
obj.var.rand_mode(1) : ON
-Selective randomization
obj.randomize(var); // doesnt randomize remaining rand variables
-Check correctness of override
obj.randomize(null);
//doesnt randomize whole obj. Just check if rules hold good for current var values
Inline constraints
- Variables use scope of callee, not the caller
p.randomize() with { addr[1:0] == 0; ; data!=0; };
Pre/Post randomize
- function void pre_randomize() {};
- function void post_randomize() {};
Random number funcions
$random : 32-bit signed
$urandom : 32-bit unsigned
$urandom_range : number over range/max value if one is providedl
here min,max is inclusive. If no mix mentioned, 0 is default min
External constraint
-Like extern functions, we declare constraint name in cls
-User can define constraint on need basis
-If undefined, SV behavior based on compiler
class pkt;
logic[7:0] addr;
constraint ext_addr;
endclass
module top;
constraint pkt::ext_addr {
addr[1:0] == 0;
}
inital begin
pkt p1=new();
assert(p1.randomize());
end
endmodule
Inline vs external
-Inline : Pros
** Every invokation rules can be unique
** Flexible
-Inline : Cons
** No reusability, every randomization needs full inline constraint definition
-External : Pros
** Helps in extending exisiting constraint
** Reusable for all objs, post definition
Extended class constraints
-Incase constraint name matches parent class constraint name, base version is overwritten
Program to generate rand int list, whose sum does not exceed 1024
(recheck)
class d_class;
rand int unsigned d_arr[];
constraint cnt_c {
d_addr.size == 5;
(d_addr.sum < 35’d1024);
}
endclass
Program to generate a dyn arry, which has only 4 strbs
Generate a array on ascending numbers
class asc_num;
unsigned int u_arr[];
constraint u_arr_c {
u_arr.size() inside {[2:10]};
foreach(u_arr[i]) {
if(i>0) {
u_arr[i] > u_arr[i-1];
}
}
}
Generate a array of unique values
Generate a random configuration sequence
-Consider a case, where we want to generate a sequence as W/R/R/W/R/W
-Here we have to call same seq multuple times with different orderining.
-randc is not applicable as we do not want to sweep all values before repeatation
-Maintain history
**In post_randomization, capture the current handomization history
**Subsquent calls show randomize based on prev history
-Use randsequence:
**This doesnt even need class randomization
**We mention a scnerio in BNF
Example of random BNF sequence
initial begin randsequence(any_stream); any_stream : p1:=10 | p2:=30; p1 : { func_p1(); } | { func_p1();} | p1; p2 : { func_p2(); } | { func_p2();} | p2; endsequence end
Example of randcase
initial begin
int i;
randcase
10 : len=10;
20 : len=50;
endcase
end
Randcase vs Randsequence
-Randsequence : It supports re-entry., branches can loop
-Randcase : It doesnt support re-entry. Thrds do not overlap randsame
Explain a TB env whcih has rand config class