Saturday, March 03, 2012

Random Constraints - Examples

Random Constraints - Examples

Example for Randomization

(a) Ethernet

(i) Example

program eth_pktrand;
class eth_frame_task;
rand integer len;
rand bit [1500:0] data [];
// Constraint the members
constraint legal {
len >= 46;
len <= 100;
data.size() == len;
}
task print();
begin
integer i =0;
$write("Size of frame is %0d\n",len);
if (data.size() > 46)
begin
$write("data is ");
for (i=46; i < len; i++)
begin
$write(" %x",data[i]);
end
// $write("\n");
end
end
endtask
endclass
initial
begin
eth_frame_task frame = new();
integer k = 0;
// Print frame before randomize
$write("-------------------------------\n");
frame.print();
$write("-------------------------------\n");
for (k = 0 ; k < 10; k++)
begin
if (frame.randomize())
begin
// Print frame after randomize
frame.print();
end
else
begin
$write("Failed to randomize frame\n");
end
$write("-------------------------------\n");
end
end
endprogram

Output in VCS

-------------------------------
Size of frame is x
-------------------------------
Size of frame is 64
data isdata is 1c13d1b2303b3a6d9bb1f8957195e8762b1572c704a642f34ad41b4bd34400
a711100cd9ba741ef2519fefa4f5dbf2edfbed89d80fd2ea74230cdc7a77aa591c76faf
8dee48ab19fa5ed3627ba2a2e55ab185562d93878977ad76bf2ad17b05f5acffb300d7
2e441ae4c61fbc2f9a058e1b2bc4089e81c8369b984eab66299b9f2c4dea36d3705844
8a4c261d1f8ca8e86a6a7535c2fe4f5a799e3984dcd6eb5b5ce4ca9e9c84fcce5c6cea45
f3180aff51562711abfcf968d9edb04 16a617d63ad3039974a1b918ee675a5722dd336b6fc809bd4ca60b168f770c30caf151e1f1
d323ceb01cf528e5fe2a601fd2fdc367096622fb3c9a3db56e054e6457d36429c95bf310
9bcfe1d1c17c7e0a8228eedb755e360b82f02e7d40c7a507308406ba9b7bf266426d167
752f7aaafa21e63fe3b61b6d577dd71b3d87f052940a2a727b1637d4d8c2c0a3aa5c8b4
ee4867696700fefadc9629ca90525ca6127fae669f01510efe94be729013825d2fab633ba
d42e8c346618b8b
-- O/P continues Likewise

(ii) Example

module Ethernet_Data;
class Eth_data;
rand bit [55:0] preamble;
rand bit [7:0] SFD;
constraint BITS {SFD[7:6] == 2'b11; }; // SFD of last two bits is one
rand logic [63:0] packet;
function logic header();
packet = { preamble,SFD};
$display("\nHeader of start of packet %b", packet);
return(packet);
endfunction
endclass
Eth_data obj_Eth_data = new(); // The object of the class Eth_data
initial
begin
obj_Eth_data.randomize(); // randomize the object of class Eth_Data
obj_Eth_data.header();
$display("\nEnd of simulation");
#20 $finish;
end
endmodule

Output in VCS

Header of start of packet 1101010010110100000110110110110101111110011110011010011011101001
End of simulation




(b) Sonet

(i) Example

program rand_example;
class path_overhead;
rand bit [7:0] POH;
rand bit [15:0] data;
constraint c {POH[1:0] ==2'b0;}
endclass

path_overhead path1;
//IF THE RANDOMIZATION IS ONE.
initial
begin
path1 = new();
repeat (10)
begin
if (path1.randomize() ==1)
$display ( "POH = %h data = %h \n",path1.POH,path1.data);
else
$display ( "Randomization failed. \n");
$display ( "----------------------------------------\n");
end
end
// IF THE RANDOMIZATION IS ZERO.

class path_oh;
rand bit [7:0] POH_FRAME;
rand bit [15:0] data;
constraint p {POH_FRAME[1:0] == 2'b0;}
endclass
path_oh path2;
initial
begin
path2 = new();
repeat (5)
begin
if (path2.randomize() ==0)
$display ( "POH_FRAME =%h data =%h \n",path2.POH_FRAME,path2.data);
else
$display ( "Randomization failed. \n");
end
end
endprogram

Output in VCS

POH = f4 data = 9ea3
----------------------------------------
POH = 74 data = b1b8
----------------------------------------
POH = fc data = 59f3
----------------------------------------
POH = 90 data = 5d6d
----------------------------------------
POH = dc data = 57aa
----------------------------------------
POH = 5c data = 9278
----------------------------------------
POH = 38 data = e47f
----------------------------------------
POH = 3c data = a262
----------------------------------------
POH = 54 data = f98d
----------------------------------------
POH = 50 data = 8599
----------------------------------------
Randomization failed.
Randomization failed.
Randomization failed.
Randomization failed.
Randomization failed

(ii) Example

program pre_post_randomize;
class sonet_data; //SONET Overheads clubbed into a class
rand bit [7:0] section_oh_A1 ;
rand bit [7:0] section_oh_A2 ;
rand bit [7:0] line_oh_H1 ;
rand bit [7:0] section_oh_B1 ;
rand bit [7:0] line_oh_B2 ;
rand bit [7:0] section_oh_J0 ;
rand bit [7:0] path_oh_J1 ;
constraint section_A1A2 {
section_oh_A1 =='hf6 ;
section_oh_A2 =='h28 ;
}
constraint line_B1B2 {
section_oh_B1 inside {[8'h0:8'hff]} ;
line_oh_B2 inside {[8'h0:8'hff]} ;
}
constraint line_J0J1 {
section_oh_J0 inside {[8'h0:8'hff]} ;
path_oh_J1 inside {[8'h0:8'hff]} ;
}
function void pre_randomize();
begin
$write("pre_randomize : Value of A1: %h,A2: %h,B1: %h,B2 :%h,J0 :%h,J1 :%h\n",section_oh_A1,section_oh_A2,section_oh_B1,line_oh_B2,section_oh_J0,path_oh_J1);
end
endfunction
function void post_randomize();
begin
$write("post_randomize : Value of A1: %h,A2: %h,B1: %h,B2 :%h,J0 :%h,J1 :%h\n",section_oh_A1,section_oh_A2,section_oh_B1,line_oh_B2,section_oh_J0,path_oh_J1 );
end
endfunction
endclass
sonet_data sonet_data_new = new();
initial
begin
repeat(4)
sonet_data_new.randomize();
end
endprogram

Output in VCS

pre_randomize : Value of A1: 00,A2: 00,B1: 00,B2 :00,J0 :00,J1 :00
post_randomize : Value of A1: f6,A2: 28,B1: 57,B2 :30,J0 :b9,J1 :d5
pre_randomize : Value of A1: f6,A2: 28,B1: 57,B2 :30,J0 :b9,J1 :d5
post_randomize : Value of A1: f6,A2: 28,B1: 02,B2 :19,J0 :79,J1 :ff
pre_randomize : Value of A1: f6,A2: 28,B1: 02,B2 :19,J0 :79,J1 :ff
post_randomize : Value of A1: f6,A2: 28,B1: ca,B2 :11,J0 :e8,J1 :76
pre_randomize : Value of A1: f6,A2: 28,B1: ca,B2 :11,J0 :e8,J1 :76
post_randomize : Value of A1: f6,A2: 28,B1: 94,B2 :93,J0 :ac,J1 :4c

No comments:

Post a Comment

Popular Posts