Saturday, March 03, 2012

Randomization Methods

Randomization Methods

(i)randomize()

Variables in an object are randomized using the randomize() class method. Every class has a built-in randomize() virtual method, declared as:

virtual function int randomize();

The randomize() method is a virtual function that generates random values for all the active random variables in the object, subject to the active constraints.

The randomize() method returns 1 if it successfully sets all the random variables and objects to valid values, otherwise it returns 0.

Example: class SimpleSum;
rand bit [7:0] x, y, z;
constraint c {z == x + y;}
endclass

(ii)pre_randomize() and post_randomize():

Every class contains built-in pre_randomize() and post_randomize() functions, that are automatically called by randomize() before and after computing new random values.

The built-in definition for pre_randomize() is:

function void pre_randomize;
if (super) super.pre_randomize(); // test super to see if the
// object handle exists
// Optional programming before randomization goes here
endfunction

The built-in definition for post_randomize() is:

function void post_randomize;
if (super) super.post_randomize(); // test super to see if the
// object handle exists
// Optional programming after randomization goes here
endfunction

//example for pre post randomization

program pre_post_rand;
class eth_pkt ;
rand integer length ;
endclass
class my_pkt extends eth_pkt ;
function void pre_randomize() ;
super.pre_randomize() ;
$display("before randomize length = %h" , length ) ;
endfunction
function void post_randomize() ;
super.post_randomize() ;
$display("After randomization length =%h", length ) ;
endfunction
endclass
my_pkt m_pkt ;
initial begin
m_pkt = new() ;
m_pkt.randomize() ;
end
endprogram

(iii)Disabling random variables with rand_mode():

The rand_mode() method can be used to control whether a random variable is active or inactive. When a random variable is inactive, it is treated the same as if it had not been declared rand or randc. Inactive variables are not randomized by the randomize() method, and their values are treated as state variables by the solver.

All random variables are initially active.

The syntax for the rand_mode() method is:

task object[.random_variable]::rand_mode( bit on_off );

// example to explain rand mode operation

program rand_mod ;
class eth_pkt ;
rand integer length ;
rand bit [47:0] dst_addr ;
constraint valid_len
{
length >=5 ; length <= 10 ;
}
task print () ;
$display("the length is %h" , length) ;
endtask
endclass
eth_pkt pkt ;
initial begin
pkt = new () ;
pkt.length.rand_mode(1) ; // rand mode is on u can make it 1 or 0
if( pkt.randomize() == 1 )
begin
$display("randomization success") ;
pkt.print ();
end
else
$display("randomization fail") ;
end
endprogram

(iv)Controlling constraints with constraint_mode():

The constraint_mode() method can be used to control whether a constraint is active or inactive. When a constraint is inactive, it is not considered by the randomize() method. All constraints are initially active.

The syntax for the constraint_mode() method is:

task object[.constraint_identifier]::constraint_mode( bit on_off );

// example to explain constraint mode operation

program cnst_mod ;
class Eth_pkt ;
rand integer length ;
constraint valid_len
{
length >=5 ; length <= 10 ;
}
task print () ;
$display("the length is %h" , length) ;
endtask
endclass
Eth_pkt pkt ;
initial begin
pkt = new () ;
pkt.valid_len.constraint_mode (0) ; // constraint mode is off u can make it 1 or 0
if( pkt.randomize() == 1 )
begin
$display("randomization success") ;
pkt.print () ;
end
else
$display("randomization fail") ;
end
endprogram


No comments:

Post a Comment

Popular Posts