Friday, March 02, 2012

Arrays Examples

Arrays - Examples

Example for Arrays.

a) Ethernet

(i) Example using Packed and unpacked arrays

module packed_unpacked_data();
bit [7:0] sfd = 8'hAB;
reg sfd1 [7:0] = '{1,0,1,0,1,0,1,1};
initial begin
$display("example of packed and unpacked array\n");
$display("sfd[0]= %b", sfd[0]);
$display ("sfd1[0] = %b", sfd1[0]);
$display ("sfd = %b\n", sfd);
#1 $finish;
end
endmodule

Output in VCS:

Example of packed and unpacked array
sfd[0]= 1
sfd1[0] = 1
sfd = 10101011

(ii) Example of multidimensional array

module multidimensional_array();
reg [7:0] preamble [0:6] = '{8'hAA,8'hAA,8'hAA,8'hAA,8'hAA,8'hAA,8'hAA};
reg [7:0] dest_src_addr [0:1] [0:5] = '{'{8'h0,8'h1,8'h2,8'h3,8'h4,8'h5},'{8'h6,8'h7,8'h8,8'h9,8'hA,8'hB}};
reg [15:0] len_type [];
initial begin
$display("example of multidimensional array\n");
$display ("preamble[0]= %b", preamble[0]);
$display ("preamble[1][0]= %b", preamble[1][0]);
$display ("dest_src_addr[0][1]= %b", dest_src_addr[0][1]);
$display ("dest_src_addr[1][1]= %b\n", dest_src_addr[1][1]);
len_type = new[2];
for ( int i = 0; i < 2; i ++)
begin
len_type[i] = i;
end
#1 $finish;
end
endmodule

Output in VCS :

Example of multidimensional array
preamble[0]= 10101010
preamble[1][0]= 0
dest_src_addr[0][1]= 00000001
dest_src_addr[1][1]= 00000111

(iii) Example of dynamic array

module dynamic_array();
reg [15:0] len_type [];
initial begin
len_type = new[2];
for ( int i = 0; i < 2; i ++)
begin
len_type[i] = i;
end
$display ("example of dynamic array\n");
$display ("length/type=%0d %0d",len_type[0],len_type[1]);
$display ("Current array size is %d",len_type.size());
$display ("Deleting the array");
len_type.delete();
$display ("Current array size after deleting is %d\n",len_type.size());
#1 $finish;
end
endmodule

Output in VCS :

Example of dynamic array
length/type=0 1
Current array size is 2
Deleting the array
Current array size after deleting is 0

(iv) Example of array methods

module array_methods();
int data [0:45] = '{46{5}};
int data_mem [$];
initial begin
$display ("example of array methods\n");
data_mem = data.min;
$display("Min size element is %0d",data_mem.pop_front());
data_mem = data.max;
$display("Max size element is %0d",data_mem.pop_front());
$display("Sum of array %0d",data.sum);
$display("Product of array %0d\n",data.product);
#1 $finish;
end
endmodule

Output in VCS :

Example of array methods
Min size element is 5
Max size element is 5
Sum of array 230
Product of array 1493286249

(v) Example of array reduction methods

module array_reduction_methods();
int data [0:45] = '{46{5}};
int data2[ string ];
string s;
initial begin
$display ("Example of array reduction methods\n");
$display("XOR of array\t= %0d",data.xor);
$display("AND of array\t= %0d",data.and);
$display("OR of array\t= %0d\n",data.or);
data2[ "welcome" ] = 1;
data2[ "to the" ] = 2;
data2[ "world of ETHERNET" ] = 3;
if(data2.first( s ) )
do
$display("data2\t: %s = %0d",s,data2[s]);
while ( data2.next( s ) );
data2.delete( "welcome" );
$display("data2 after deleting = %s\t",s,data2[s]);
#1 $finish;
end
endmodule

Output in VCS :

Example of array reduction methods
XOR of array 0
AND of array 5
OR of array 5
data2 : to the= 2
data2 : welcome= 1
data2 : world of ETHERNET= 3
data2 after deleting= world of ETHERNET 3

(vi) Example using arrays

module array_literal;
int preamble [1:7]='{7{10}};
int sfd [1:2]='{10,11};
int dest_addr [1:6]='{5,6,3,2,7,8};
int src_addr [1:6]='{2,8,4,1,7,5};
int len_type [1:2]='{default:0};
int fcs [1:4]='{4,3,6,7};
initial
begin
$display("preamble=%0p sfd=%0p dest_addr=%0p src_addr=%0p len_type=%0p fcs=%0p ", preamble,sfd,dest_addr,src_addr,len_type,fcs);
end
endmodule

Output in questasim

run -all
# preamble=10 10 10 10 10 10 10
sfd=10 11
dest_addr=5 6 3 2 7 8
src_addr=2 8 4 1 7 5
len_type=0 0
fcs=4 3 6 7


b) Sonet


(i) Example using Arrays

module array_element;
byte b[0:3] = '{1,2,3,4};
string s[] = {"SOH", "POH" ,"TOH"};
typedef enum {H1_value, H2_value, H3_value} pointer_value;
pointer_value p;
initial
begin
int x,y,z;
s.reverse; // REVERSE THE STRING OF THE FRAME
for (int k =0;k <3; k++)
$write (" %s \n",s[k]);
$display ("********************************\n");
s = new[3];
$display("The size is \n");
$display ("%d \n",s.size);
$display("************************************\n");
p = p.first();
$display("The first frame is %s",p.name()); // DISPLAY THE FIRST FRAME
$display("************************************\n");
p = p.last();
$display("The last frame is %s",p.name());// DISPLAY THE LAST FRAME
$display("************************************\n");
x= b.sum ;
$display("Value of x is %d",x);// ADDING THE VALUE
y =b.product;// MULTIPLY THE VALUE
$display("Value of y is %d",y);
$display ("************************************\n");
z = b.xor with(item +4);// XORING THE VALUE
$display ("Value of z is %d",z);
end
endmodule

Output in VCS

TOH
POH
SOH
********************************
The size is 3
************************************
The first frame is H1_value
************************************
The last frame is H3_value
************************************
Value of x is 10
Value of y is 24
************************************
Value of z is 12

(ii) Example using Queue Methods

module payload;
bit [7:0]buffer;
bit [7:0] Line_oh[$]={32'd87,32'd88,32'd99,32'd100,32'd99,32'd88,32'd99,32'd100};
int output_q_loh[$];
bit [7:0] Path_oh[$]={32'd12,32'd13,32'd14,32'd10,32'd9,32'd8,32'd15,32'd18};
bit [7:0] output_q_poh[$]; //------------------finding the elements in POH queue
initial
begin
output_q_poh = Path_oh.find(x) with (x>12);
for (int k=0;k<=output_q_poh.size;k++)
$write("POH:%d\n",output_q_poh[k]);
$display("***************************************"); //-----------finding the elements in LOH queue
output_q_loh =Line_oh.find_index with (item =='d99);
for( int k=0; k< output_q_loh.size;k++)
$write("LOH:%d\n",output_q_loh[k]);
$display("***************************************"); //-------------delete the elements from a queue
Line_oh.delete(0); //--------deletes the element at 0th position
foreach(Line_oh[i])
$display("After deleting the first element\nLine_oh[%d]=%d",i,Line_oh[i]);
$display("***************************************"); //----------insert element in the front
Line_oh.push_front('d66);
foreach(Line_oh[i])
$display("After pushing the first element \nLine_oh[%d]=%d",i,Line_oh[i]);
$display("***************************************");
buffer=Line_oh.pop_front(); foreach(Line_oh[i])
$display("After popping the first element \nLine_oh[%d]=%d",i,Line_oh[i]);
$display("***************************************");
Line_oh.insert(3,'d109);
foreach(Line_oh[i])
$display("After inserting the first element \nLine_oh[%d]=%d",i,Line_oh[i]);
$display("***************************************");
end
endmodule

Output in VCS

POH: 13
POH: 14
POH: 15
POH: 18
POH: 0
***************************************
LOH: 2
LOH: 4
LOH: 6
***************************************
After deleting the first element
Line_oh[ 0]= 88
After deleting the first element
Line_oh[ 1]= 99
After deleting the first element
Line_oh[ 2]=100
After deleting the first element
Line_oh[ 3]= 99
After deleting the first element
Line_oh[ 4]= 88
After deleting the first element
Line_oh[ 5]= 99 After deleting the first element
Line_oh[ 6]=100
***************************************
After pushing the first element
Line_oh[ 0]= 66
After pushing the first element
Line_oh[ 1]= 88
After pushing the first element
Line_oh[ 2]= 99
After pushing the first element
Line_oh[ 3]=100
After pushing the first element
Line_oh[ 4]= 99
After pushing the first element
Line_oh[ 5]= 88
After pushing the first element
Line_oh[ 6]= 99
After pushing the first element
Line_oh[ 7]=100
***************************************
After popping the first element
Line_oh[ 0]= 88
After popping the first element
Line_oh[ 1]= 99
After popping the first element
Line_oh[ 2]=100
After popping the first element
Line_oh[ 3]= 99
After popping the first element
Line_oh[ 4]= 88
After popping the first element
Line_oh[ 5]= 99
After popping the first element
Line_oh[ 6]=100
***************************************
After inserting the first element
Line_oh[ 0]= 88
After inserting the first element
Line_oh[ 1]= 99
After inserting the first element
Line_oh[ 2]=100
After inserting the first element
Line_oh[ 3]=109
After inserting the first element
Line_oh[ 4]= 99
After inserting the first element
Line_oh[ 5]= 88
After inserting the first element
Line_oh[ 6]= 99
After inserting the first element
Line_oh[ 7]=100
***************************************

(iii) Example using arrays

module packet();
initial
begin
byte TOH[9][3];
foreach (TOH[i,j])
TOH[i][j]=i*3+j;
foreach (TOH[i])
begin
$write ("%d:",i);
foreach(TOH[i,j])
$write ("%d",TOH[i][j]);
$display;
end
end
endmodule

Output in VCS

0: 0 1 2
1: 3 4 5
2: 6 7 8
3: 9 10 11
4: 12 13 14
5: 15 16 17
6: 18 19 20
7: 21 22 23
8: 24 25 26


No comments:

Post a Comment

Popular Posts