Tutorials

CONSTRAINT EXPRESSION

Constraint Expressions

A constraint_expression is any Openvera expression or one of the constraint specific
operators, -> (Implication) and dist.

Set Membership

A set membership is a list of expressions or a range.This operator searches for the existences of the value in the specified expression or range and returns 1 if it is existing.

EXAMPLE:
class set_mem{
rand integer Var;
constraint range { Var in {0,1,{50,60},{90,100}}; }
printf("%0d__",Var);
}
}

program set_mem_p{
set_mem obj=new();
repeat(10)
void = obj.randomize();
}

RESULTS:

1__51__93__1__0__56__0__94__1__0__

If you want to define a range which is outside the set,use nagation.
EXAMPLE:
class set_mem{
rand bit [0:2] Var;
constraint range { !( Var in {0,1,5,6};)}
printf("%0d__",Var);
}
}

program set_mem_p{
set_mem obj=new();
repeat(10)
void = obj.randomize();
}

RESULTS:

2__7__2__4__7__3__3__4__2__7__

Weighted Distribution

There are two types of distribution operators.
The := operator assigns the specified weight to the item or, if the item is a range, to every value in the range.
The :/ operator assigns the specified weight to the item or, if the item is a range, to the range as a whole. If
there are n values in the range, the weight of each value is range_weight / n.

Var dist { 10 := 1; 20 := 2 ; 30 := 2 }

The probabulity of having 10,20,30 is in the ration of 1,2,2 respectively.

Var dist { 10 := 1; 20 := 2 ; [30:32] := 2 }

The probabulity of having 10,20,30,31,32 is in the ration of 1,2,2,2,2 respectively.
If you use the := operator each element of the range has the assigned weight.
If you want to weight for the whole group,use :/ and the weight is distributed equally for each element in that group.

Var dist { 10 := 1; 20 := 2 ; [30:32] :/ 2 }

The probabulity of having 10,20,30,31,32 is in the ration of 1,2,2/3,2/3,2/3 respectively.

To demonstrate the distribution property,hear is an example.

EXAMPLE:
class Dist{
rand integer Var;
constraint range { Var dist { {0,1} := 50 , {2,7} := 50 }; }
}

program Dist_p{
Dist obj;
integer count_0, count_1, count_2, count_3, count_4, count_5, count_6, count_7;
integer count_0_1 ,count_2_7 ;
obj=new();
count_0 = 0;count_1 = 0;count_2 = 0;count_3 = 0;
count_4 = 0;count_5 = 0;count_6 = 0;count_7 = 0;
count_0_1 = 0;count_2_7 = 0;

for(int i=0; i< 10000; i++)
if( obj.randomize())
{
if( obj.Var == 0) count_0 ++;
else if( obj.Var == 1) count_1 ++;
else if( obj.Var == 2) count_2 ++;
else if( obj.Var == 3) count_3 ++;
else if( obj.Var == 4) count_4 ++;
else if( obj.Var == 5) count_5 ++;
else if( obj.Var == 6) count_6 ++;
else if( obj.Var == 7) count_7 ++;

if( obj.Var inside {0,1} ) count_0_1 ++;
else if( obj.Var inside {[2:7]} ) count_2_7 ++;
}

printf(" count_0 = %0d , count_1 = %0d, count_2 = %0d, count_3 = %0d, count_4 = %0d, count_5 = %0d, count_6 = %0d, count_7= %0d ",count_0, count_1, count_2, count_3, count_4, count_5, count_6, count_7);
printf(" count_0_1 = %0d ;count_2_7 = %0d ",count_0_1,count_2_7);
}

RESULTS:

count_0 = 2450 , count_1 = 2420, count_2 = 1114, count_3 = 1071, count_4 = 871, count_5 = 812, count_6 = 621, count_7= 641
count_0_1 = 4870 ;count_2_7 = 5130

Now change the constraint to

constraint range { Var dist { {0,1} :/ 50 , {2,7} :/ 50 }; }

EXAMPLE:
class Dist{
rand integer Var;
constraint range { Var dist { {0,1} :/ 50 , {2,7} :/ 50 }; }
}

program Dist_p{
Dist obj;
integer count_0, count_1, count_2, count_3, count_4, count_5, count_6, count_7;
integer count_0_1 ,count_2_7 ;
obj=new();
count_0 = 0;count_1 = 0;count_2 = 0;count_3 = 0;
count_4 = 0;count_5 = 0;count_6 = 0;count_7 = 0;
count_0_1 = 0;count_2_7 = 0;

for(int i=0; i< 10000; i++)
if( obj.randomize())
{
if( obj.Var == 0) count_0 ++;
else if( obj.Var == 1) count_1 ++;
else if( obj.Var == 2) count_2 ++;
else if( obj.Var == 3) count_3 ++;
else if( obj.Var == 4) count_4 ++;
else if( obj.Var == 5) count_5 ++;
else if( obj.Var == 6) count_6 ++;
else if( obj.Var == 7) count_7 ++;

if( obj.Var in {0,1} ) count_0_1 ++;
else if( obj.Var in {2,7}} ) count_2_7 ++;
}

printf(" count_0 = %0d , count_1 = %0d, count_2 = %0d, count_3 = %0d, count_4 = %0d, count_5 = %0d, count_6 = %0d, count_7= %0d ",count_0, count_1, count_2, count_3, count_4, count_5, count_6, count_7);
printf(" count_0_1 = %0d ;count_2_7 = %0d ",count_0_1,count_2_7);
}
RESULTS:

count_0 = 1245 , count_1 = 1242, count_2 = 1211, count_3 = 1271, count_4 = 1287, count_5 = 1212, count_6 = 1221, count_7= 1241
count_0_1 = 2487 ;count_2_7 = 7513

Both the reults show,how may times each value occured.

NOTE:If no wait is specified for items,the default weight is 1.
Weight 0 is also allowed.
NOTE:Variable declared as randc are not allowed int dist.

Implication

Implication operator can be used to predirect a relation.The syntax is expression -> constraint set.
If the expression is true,then the constraint solver should satisfy the constraint set.
The booliean equvalant of a -> b is (!a || b).

rand bit a;
rand bit [3:0] b;
constraint c { (a == 0) -> (b == 1); }

The probabulity of a = 0 is 1/2**5,as bidirectional constraints are solved at once,the solver pics the random value from the possible set of {a,b} which has 2**5 solutions.

EXAMPLE:
class impli{
rand bit Var;
rand bit [3:0] b;
constraint c { (Var == 0) => (b == 1); }
}

program impli_p{
impli obj;
integer count_0 ,count_1,i ;
obj=new();
count_0 = 0;count_1 = 0;
for(i=0; i< 10000; i++)
{
obj = new();
if( obj.randomize())
{
if( obj.Var == 0 ) count_0 ++;
else if( obj.Var == 1 ) count_1 ++;
}}
printf(" count_0 = %0d;count_1 = %0d;",count_0 ,count_1);

}
RESULTS:

count_0 = 2047;count_1 = 7953

If..Else

Just like implication, if...else style constraints are bidirectional.Above example applies hear too.

EXAMPLE:
class if_else{
rand bit a;
rand bit [3:0] b;
constraint c { if(a == 0) (b == 1); }
}

program if_else_p{
if_else obj;
integer count_0 ,count_1 ;
obj=new();
count_0 = 0;count_1 = 0;
for(int i=0; i< 10000; i++)
{
obj = new();
if( obj.randomize())
{
if( obj.Var == 0 ) count_0 ++;
else if( obj.Var == 1 ) count_1 ++;
}}
printf(" count_0 = %0d;count_1 = %0d;",count_0 ,count_1);
}
RESULTS

count_0 = 2047;count_1 = 7953

Iterative

Iterative constraints allow Constraining individual fixed-size, dynamic, associative, or queue elements.
foreach construct specifies iteration over the elements of array.

EXAMPLE:
class Eth_pkt{
constraint size_c { Payload.size() inside {[46:1500]}; }
constraint element_c { foreach ( Payload[ i ] ) Payload[ i ] inside {[50:100]}; }
}

program iterative{
Eth_pkt obj;
obj = new();
for(int i=0;i< 10000;i++)
{
if(obj.randomize())
printf(" RANDOMIZATION DONE ");
}}
 << PREVIOUS PAGE TOP NEXT PAGE >>