Randomization Ppt

21
RANDOMIZATION

Transcript of Randomization Ppt

Page 1: Randomization Ppt

RANDOMIZATION

Page 2: Randomization Ppt

Why we need to go for randomization ?

• A directed test finds the bugs you think are there, but a CRT(constrained

random test) finds bugs you never thought about.

• These are processed by solver that generates random values that meets the

constraints

• We can restrict the test scenarios to those that are both valid and of interest

by using constraints.

• System Verilog allows users to specify constraints.

Page 3: Randomization Ppt

Constraint blocks• The values of random variables determined using constraint expressions that are

declared using constraint blocks.

• Constraint block name should be unique within a class

Syntax:

[static] constraint <constraint_identifier> <constraint_block>

constraint_identifier name of the constraint block

• Using constraint_identifier we can enable or disable the constraints.

Note:

• Operators with side effects like ++ and -- not allowed.

• randc variables cannot be specified in ordering constraints .

Page 4: Randomization Ppt

• Constraints can be declared outside a class declaration.

Eg:

// class declaration

class xypair;

rand integer x,y;

constraint c;

end class

constraint xypair :: c { x<y; } //external constraint body declaration

Page 5: Randomization Ppt

Set membership • Constraints supports integer value sets and set membership operator.

• !(expression inside {set}) denotes expression lies outside the set.

• Absent any other constraints, all values have an equal probability of being chosen by

the inside operator

Example:

1. rand integer x,y,z;

constraint c1{x inside {3,5,[9:15],[24:32],[y:2*y];}

2. rand integer a,b,c;

constraint c2 {a inside {b , c} ; } //c2 represents a==b || a==c.

3. integer fives[0:3] = {2,4,6,8};

rand integer v;

constraint c3 {v inside fives ;}

Page 6: Randomization Ppt

Implication • Provides 2 constructs for declaring conditional relations

implication and if …else

• Implication can be used to declare an expression that implies a constraint

syntax:

• Constraint expression :: =expression constraint_set

constraint_set valid constraint or an unnamed constraint set

expression system verilog expression

• Boolean equivalent of ab is ( !a || b )

eg:

1. Mode == small len <10 // mode implies value of len constrained to less than 10

2. Mode == small len >100 // mode implies value of len constrained to greater than 100

Page 7: Randomization Ppt

If …else constraints• Syntax:

• Constraint_expression::= if(expression) constraint_set

(else constraint_set)

• This can be used to group multiple constraint sets.

Eg:

If (mode == small)

len < 10;

else if (mode == large)

len > 100;

• Like implication if..else is also bi-directional. In the above example value of

mode constraints len and value of len constraints mode.

Page 8: Randomization Ppt

• Here else part of if..else constraint declaration is optional.

• To avoid confusion this is resolved by always associating the else with

closest previous if that lacks else.

Eg:

If (mode != large)

if (mode == small)

len < 10;

else // else applies to preceding if

len > 100;

Page 9: Randomization Ppt

Iterative constraints

• Allows arrayed variables to be constrained in a parameterized manner

using loop variables and indexing expressions.

• Syntax:

Constraint_expression :: = foreach(array_identifier

[loop_variables]) constraint_set

Loop_variables:: = [index_variable_identifiers]

{,[index_variable_identifiers]}

• foreach construct specifies iteration over the elements of array.

Page 10: Randomization Ppt

Eg:

Class c;

Rand byte a[];

Constraints c1 {foreach (a[i]) a[i] inside {2,4,8,16}; }

//c1 constraints each element of a to be in set[2,4,8,16]

Constraints c2 {foreach(a[j]) a[j] > 2*j ;}

//c2 constraints each element of a to be greater than twice its

index

Page 11: Randomization Ppt

• The mapping of loop variables to array indexes is determined by the

dimension cardinality.

• // 1 2 3 3 4 1 2 dimension numbers

Int A [2] [3] [4]; bit [3:0] [2:1] B [5:1] [4];

foreach ( A [ i , j , k ] ) …

foreach ( B [ q , r , , s ] ) …

• In the above first foreach causes i to iterate from 0 to 1, j from 0 to 2,

and k from 0 to 3.

• The second foreach causes q to iterate from 5 to 1,r from 0 to 3 and s

from 2 to 1.

Page 12: Randomization Ppt

• Iterative constraints can include predicates.

Eg:

class c;

rand int A[];

constraint c1 { arr.size inside { [1:10] } ;}

//c1 constrains the size of array A to be between 1 and 10.

constraint c2 { foreach {A[k]} { k < A.size – 1} -> A[k+1] > A[k];}

//c2 constrains each array value to be greater than the preceding one

i.e., an array sorted in ascending order.

endclass

Page 13: Randomization Ppt

Variable orderingClass B;

rand bit s;

rand bit [31:0]d;

constraint c { s -> d == 0;}

endclass

//constraint c says “s implies d equals zero “

-> if s determines d ; both s & d are determined together but {s,d} combination

is true only for {1,0}. The probability of s is true is 1/233 practically zero

-> So by practical ordering on the evaluation of variables is specified using

solve keyword

Page 14: Randomization Ppt

Class B;

rand bit s;

rand bit [31:0]d;

constraint c { s -> d == 0;}

constraint order { solve s before d;}

endclass

• Constraints instructs solver to solve for s before solving for d

• It can be used to force selected corner cases to occur more frequently

than they would.

• Syntax:

• Constraint_block_item ::= solve identifier_list before identifier_list; |

constraint_expression.

Page 15: Randomization Ppt

Functions in constraints• Example to compute the number of 1’s in a packed array uses a loop

function int count_ones (bit [0 : 9] w);

for (count_ones = 0; w != 0 ; w = w>>1)

count_ones += w&1’b1;

endfunction

Such a function could be used to constrain other random variables to the

number of 1 bits:

constraint c1 { length == count_ones (v) ; }

• The ability to call functions,thus enhances the expressive power of constraint

language and reduces the likelihood of errors.

Page 16: Randomization Ppt

Restrictions:

• Functions that appear in constraint expressions cannot contain output

or ref arguments.

• Functions that appear in constraint expressions should be automatic.

• Functions that appear in constraints cannot modify the constraints

• Functions shall be called before constraints are solved, and their

return values shall be treated as state variables.

Page 17: Randomization Ppt

• $urandom:

• System function provides a mechanism for generating

pseudorandom numbers.

• Returns 32-bit random number each time it is called.

• Number shall be unsigned.

Syntax: Function int unsigned $urandom [ ];

• Eg: bit [64:1 ] addr;

• $urandom (254); //intialize generator

• Addr = { $urandom,$urandom}; // 64-bit random number.

• Note: $urandom similar to $random function but $urandom returns

unsigned numbers and automatically thread stable.

Page 18: Randomization Ppt

Constraint_mode():

• Used to enable or disable any named constraint block in an object.

• Eg:

bus. Word_align.constraint_mode(0); //disables word_align

bus. Word_align.constraint_mode(1); //enables word_align

Rand_mode():

• Used to enable or disable any random variable. when random variable

disables,it behaves in sameway the other non random variables.

Pre_randomize() & post_randomize():

• Used to perform operations immediately before or after

randomization.

Page 19: Randomization Ppt

• rand & randc

• Syntax:

class_property ::= { property_quantifier } data_declaration

property_quantifier ::= rand | randc

• Variables declared with the rand keyword are standard random

variable

Eg: rand bit [ 7:0 ] y;

• Variables declared with randc keyword are random-cyclic variables

that cycle through all the values in a random permutation of their

declared range.

• Eg: randc bit [ 1:0 ] y;

Page 20: Randomization Ppt
Page 21: Randomization Ppt

• Here variable y can take on the values 0,1,2,3 (range 0 to 3)

• Randomize computes an initial random permutation of the range

values of y, and then returns to those values in order on successive

calls.after it returns the last element of a permutation,it repeats the

process by computing a new random premutation.

• Randc randomly iterates over all the values in the range and that no

vlaue is repeated within an ireration.