Array-Querying System Functions
Last modified 2014-07-01
Sini
System Verilog provides some system functions to query about arrays. The return value of this system function is `int` and it can be applied to all arrays.
Different array-querying functions are
dimension
Returns the number of dimensions in the array and 1 for strings and simple bit vectors and 0 for any other type.
module arrayquery1;
typedef logic \[15:0\] memt \[1:63\];
var memt mem;
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
initial
begin
$display("Dimesion of mem : %0d", $dimensions(mem));
$display("Dimesion of myarr : %0d", $dimensions(myarr));
end
endmodule
/\*
ncsim> run
Dimesion of mem : 2
Dimesion of myarr : 4
\*/
Here there are two dimensions for mem i.e. one `packed` and one `unpacked` ( [15:0] and 1:63]).For myarr there are two `packed` and two `unpacked` dimensions. So total 4.
unpackeddimensions
Returns the number of unpacked dimensions of an array and 0 for any other type.
module arrayquery2;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("Unpacked dimesion of mem : %0d", $unpackeddimensions(memt));
$display("Unpacked dimesion of myarr : %0d", $unpackeddimensions(myarr));
end
endmodule
/\*
ncsim> run
Unpacked dimesion of mem : 1
Unpacked dimesion of myarr : 2
\*/
Here, one unpacked dimension for mem (i.e. 1:63) and 2 unpacked dimensions for myarr (2:0,3:0).
left
Returns the left declared bound of the dimension.
module arrayquery3;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("$left of mem : %0d", $left(mem));
$display("$left of myarr : %0d", $left(myarr));
end
endmodule
/\*
ncsim> run
$left of mem : 1
$left of myarr : 2
\*/
Here first unpacked dimension will be used. For mem, 1:63 is the unpacked dimension and left bound is 1. For myarr, 2:0 is the unpacked dimension and 2 will be the $left value.
right
Returns the right declared bound of the dimension.
module arrayquery4;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("$right of mem : %0d", $right(mem));
$display("$right of myarr : %0d", $right(myarr));
end
endmodule
/\*
ncsim> run
$right of mem : 63
$right of myarr : 0
ncsim: \*W,RNQUIE: Simulation is complete.
\*/
Just like $left, $right will be the right bound. So for mem, 1:63 is the dimesion and $left value is 1 and $right value is 63.For myarr $left is 2 and $right is 0.
low
Returns the lowest declared bound of the dimension.
module arrayquery5;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("$low of mem : %0d", $low(mem));
$display("$low of myarr : %0d", $low(myarr));
end
endmodule
/\*
ncsim> run
$low of mem : 1
$low of myarr : 0
\*/
Lowest declared bound of the dimension for mem is 1 and for myarr is 0.
high
returns the highest declared bound of the dimension.
module arrayquery6;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("$high of mem : %0d", $high(mem));
$display("$high of myarr : %0d", $high(myarr));
end
endmodule
/\*
ncsim> run
$high of mem : 63
$high of myarr : 2
ncsim: \*W,RNQUIE: Simulation is complete.
\*/
Highest declared bound of the dimension for mem is 63 and for myarr is 2.
increment
Returns 1 if the left bound is greater than or equal to the right bound and -1 otherwise.
module arrayquery7;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("$increment of mem : %0d", $increment(mem));
$display("$increment of myarr : %0d", $increment(myarr));
end
endmodule
/\*
ncsim> run
$increment of mem : -1
$increment of myarr : 1
\*/
For mem, left bound is less than right bound, so returned -1. For myarr, left bound is greater than right bound, so returned 1.
size
Returns the number of elements in the dimension.
module arrayquery8;
typedef logic \[15:0\] memt \[1:63\];
logic \[7:0\]\[15:0\] myarr \[2:0\]\[3:0\];
var memt mem;
initial
begin
$display("$size of mem : %0d", $size(mem));
$display("$size of myarr : %0d", $size(myarr));
end
endmodule
/\*
ncsim> run
$size of mem : 63
$size of myarr : 3
ncsim: \*W,RNQUIE: Simulation is complete.
\*/
Here, number of elements in mem is 63 (1:63 dimension) and myarr has 3 elements.