Indexing, slicing, and querying functions of arrays

Jan. 10, 2024 || Item:2.3.Indexing, slicing and querying functions of arrays

An expression can select part of a packed array, or any integer type, which is assumed to be numbered down to 0.

SystemVerilog uses the term “part select” to refer to a selection of one or more contiguous bits of a single dimension packed array. This is consistent with the usage of the term “part select” in Verilog.

reg [63:0] data;

reg [7:0] byte2;

byte2 = data[23:16]; // an 8-bit part select from data

SystemVerilog uses the term “slice” to refer to a selection of one or more contiguous elements of an array. Verilog only permits a single element of an array to be selected, and does not have a term for this selection.

An single element of a packed or unpacked array can be selected using an indexed name.

bit [3:0] [7:0] j; // j is a packed array

byte k;

k = j[2]; // select a single 8-bit element from j

One or more contiguous elements can be selected using a slice name. A slice name of a packed array is a packed array. A slice name of an unpacked array is an unpacked array.

bit busA [7:0] [31:0] ;  // unpacked array of 8 32-bit vectors

int busB [1:0];          // unpacked array of 2 integers

busB = busA[7:6];        // select a slice from busA

The size of the part select or slice must be constant, but the position can be variable. The syntax of Verilog-2001 is used.

int i = bitvec[j +: k]; // k must be constant.

int a[x:y], b[y:z], e;

a = {b[c -: d], e}; // d must be constant

Slices of an array can only apply to one dimension, but other dimensions can have single index values in an expression.

SystemVerilog provides new system functions to return information about an array. These are: $left, $right, $low, $high, $increment, $size, and $dimensions



Comments:

Leave a comment: