Saturday, March 03, 2012

Random Variables

Random Variables

Class variables can be declared random using the rand and randc type-modifier keywords. Arrays can be declared rand or randc, in which case all of their member elements are treated as rand or randc.

(i)rand modifier:

Variables declared with the rand keyword are standard random variables. Their values are uniformly distributed over their range. For example:

rand bit [7:0] y;

This is an 8-bit unsigned integer with a range of 0 to 255. If unconstrained, this variable shall be assigned any value in the range 0 to 255 with equal probability. In this example, the probability of the same value repeating on successive calls to randomize is 1/256.

program Kacper_example ;
typedef enum { UNICAST, MULTICAST,BROADCAST } packet_typ ;
typedef enum { DATA_FRAME ,CONTROL , JUMBO } frame_type ;
class Ethernet_packet ;
rand bit [47:0] dst_addr ;
rand integer length ;
rand packet_typ pkt_typ ;
rand frame_type frame_typ ;
randc bit [1:0] num_of_trans ;
rand bit [7:0] payload [ ] ;
constraint dst_addr_typ
{
if ( frame_typ == DATA_FRAME )
{
( pkt_typ == UNICAST ) -> { dst_addr[41:40] == 2'b00 } ;
( pkt_typ == MULTICAST) -> { dst_addr[41:40] == 2'b01 ; }
( pkt_typ == BROADCAST) -> { dst_addr[41:40] == 48'hff_ff_ff_ff_ff_ff ; }
}
}
constraint legel_lenght
{
length >= 5 ; length <= 10 ; payload.size() == length ;
}
task print () ;
begin
integer i = 0 ;
$display("the Size of frame is = %16h" , length ) ;
if(payload.size()>0)
begin
$display ("THE PAYLOAD IS ") ;
for (i=0 ;i< length ;i++)
begin
$display("%h",payload[i]) ;
end
$display(" No of transaction = %16h " , num_of_trans ) ;
end
end
endtask
endclass
Ethernet_packet pkt ;
initial begin
pkt = new () ;
pkt.randomize() ;
pkt.print() ;
end
endprogram

(ii)randc modifier:

Variables declared with the randc keyword are random-cyclic variables that cycle through all the values in a random permutation of their declared range. Random-cyclic variables can only be of type bit or enumerated types, and can be limited to a maximum size.

To understand randc, consider a 2-bit random variable y:

randc bit [1:0] y;


No comments:

Post a Comment

Popular Posts