Saturday, March 03, 2012

Constraint Blocks

Constraint Blocks

The values of random variables are determined using constraint expressions that are declared using constraint blocks. Constraint blocks are class members, like tasks, functions, and variables. Constraint block names must be unique within a class.

(i)External constraint blocks:

Constraint block bodies can be declared outside a class declaration, just like external task and function bodies:

// class declaration
class XYPair;
rand integer x, y;
constraint c;
endclass
// external constraint body declaration
constraint XYPair::c { x < y; }

(ii)Inheritence

Constraints follow the same general rules for inheritance as class variables, tasks, and functions:

A constraint in a derived class that uses the same name as a constraint in its parent classes overrides the base class constraints. For example:

class A;rand integer x; constraint c { x < 0; }endclass
class B extends A;constraint c { x > 0; }endclass

(iii)Set Membership

Constraints support integer value sets and the set membership operator.

Absent any other constraints, all values (either single values or value ranges), have an equal probability of being chosen by the inside operator.

The negated form of the inside operator denotes that expression lies outside the set:

!(expression inside { set }).
For example:
rand integer x, y, z;
constraint c1 {x inside {3, 5, [9:15], [24:32], [y:2*y], z};}

// set membership constraint block

program set_membership ;
class eth_pkt ;
rand bit [47:0] src_addr ;
rand bit [47:0] dst_addr ;
constraint valid { src_addr inside { [48'h00_00_00_00_00_00 : 48'haa_aa_aa_aa_aa_aa ] } ;
(! dst_addr inside {[48'h00_00_00_00_00_00 : 48'h00_00_11_11_11_11 ] } ) ; }
endclass
eth_pkt pkt ;
initial begin
pkt = new () ;
pkt.randomize() ;
$display("The SRC_ADDR = %h and DST_ADDR =%h", pkt.src_addr , pkt.dst_addr ) ;
end
endprogram

(iv)Implication

Constraints provide two constructs for declaring conditional (predicated) relations: implication and if...else.

The implication operator ( -> ) can be used to declare an expression that implies a constraint.

mode == small -> len < 10;
mode == large -> len > 100;

In this example, the value of mode implies that the value of len shall be constrained to less than 10 (mode == small), greater than 100 (mode == large), or unconstrained (mode != small and mode != large).

// implication operator inside constraint block

program implication_ex ;
typedef enum { SMALL ,LARGE } mode_type ;
class eth_pkt ;
rand integer length ;
rand mode_type mode_typ ;
constraint valid_len
{
mode_typ==SMALL -> length<=10 ;
mode_typ ==LARGE -> length >=100 ;
}
endclass
eth_pkt pkt ;
initial begin
pkt = new ;
pkt.randomize() ;
$display("The length =%h" ,pkt.length ) ;
end
endprogram

(v)Inline constraints

In SystemVerilog additional constraint are declared using randomize with construct.

These are called inline constraints.

The following example illustrate the usage of inline constraint

program inlin_cst ;
class eth_pkt ;
rand bit [47:0 ] dst_addr ;
rand bit [47:0 ] src_addr ;
constraint valid_dst
{
dst_addr >=128 ;
src_addr <= 127 ;
}
task print() ;
begin
$display("The SOURCE ADDR = %h and DST_ADDR = %h" , src_addr ,dst_addr ) ;
end
endtask
endclass
eth_pkt pkt ;
initial begin
pkt = new () ;
pkt.randomize();
pkt.print();
pkt.randomize() with { src_addr <=90 ; dst_addr >= 120 ; } ;
pkt.print () ;
end
endprogram

(vi)If-Else

if-else: we can use if else statements inside the constraint block.

The below example will show the example of using if-else construct inside the constraint block

// if-else statement in constraint block

program if_else ;
class eth_pkt ;
typedef enum { SHORT , LARGE } frame_type ;
rand frame_type frame_typ ;
rand byte frame_size ;
constraint valid
{
if (frame_typ == SHORT )
{
frame_size <= 64 ; }
else
{
frame_size >= 1518 ; }
}
endclass
eth_pkt pkt ;
initial begin
pkt = new ;
pkt.randomize() ;
$display("The frame Size = %h " , pkt.frame_size ) ;
end
endprogram

(vii)Distribution

Set membership construct implicitly express uniform distribution in a set.

In many cases non uniform distributions are required.

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.

The following example illustrate the use of := distribution operator.

program distribution ;
class my_class ;
rand integer x, y, z;
constraint valid_range { x dist {100 := 1, 200 := 2, 300 :=5} ; }
endclass
my_class obj ;
initial begin
obj = new ;
obj.randomize() ;
$display(" The value of x is %h " , obj.x) ;
end
endprogram

Example for distribution inside constraint block :/

program distribution ;
class my_class ;
rand integer x, y, z;
constraint valid_range { x dist {100 :/ 1, 200 :/ 2, 300 :/ 5} ; }
endclass
my_class obj ;
initial begin
obj = new ;
obj.randomize() ;
$display(" The value of x is %h " , obj.x) ;
end
endprogram

(viii)Foreach:

foreach loop can be used inside the constraint block.

The following example shows the use of foreach loop inside the constraint block.

// foreach loop inside the constraint block

program foreach_loop ;
class my_class ;
rand byte A[] ;
int j = 40 ;
constraint C1 { foreach (A[i]) A[i] inside { 2,4,8,16} ; }
constraint C2 { foreach (A[i]) A[i] > 2 * j ; }
endclass
my_class obj ;
initial begin
obj = new ;
if( obj.randomize() == 1)
$display("randomization Success" ) ;
else
$display("randomization Fail") ;
end
endprogram

(ix) Variable Ordering:

The constraint provides the mechanism variable ordering.

The fallowing example shows the use of variable ordering inside the constraint block.

// example for variable ordering inside constraint block

program var_order ;
class my_class ;
rand bit S ;
rand bit [31:0] A ;
constraint C1 { S == 1 -> A == 0 ; }
constraint C2 { solve S before A ; }
endclass
my_class obj ;
initial begin
obj = new ;
obj.randomize() ;
$display("The Value of A = %h " , obj.A ) ;
end
endprogram

(x)Functions inside constraint block:

Functions can be used inside the constraint block.

The following example explains about the use of functions inside the constraint block.

program fun_const ;
class my_class ;
rand int x , y ;
function f(y) ;
y = y+1 ;
endfunction
constraint c1 { x == f(y) ; } // where f(y) is function
constraint c2 { y inside { 2,4,8 } ; }
endclass
my_class obj ;
initial begin
obj = new ;
obj.randomize() ;
$display("The value of x = %h and y =%h" , obj.x , obj.y) ;
end
endprogram


No comments:

Post a Comment

Popular Posts