Wednesday, March 20, 2013

Example of multidimensional array


The following code is a best example for declaring a multi-dimensional array. It gives you an idea on how to declare a one dimensional and a two dimensional array. The code is based on system verilog which allows you to declare an array without specifying the length (in the following code reg [15:0] len_type []). 


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 :
Example of multidimensional array
preamble[0]= 10101010
preamble[1][0]= 0
dest_src_addr[0][1]= 00000001
dest_src_addr[1][1]= 00000111

No comments: