• 沒有找到結果。

tag is 0 for Invalid, 1 for Valid

7.4 Packed and unpacked arrays

SystemVerilog supports both packed arrays and unpacked arrays of data. The term packed array is used to refer to the dimensions declared before the data identifier name. The term unpacked array is used to refer to the dimensions declared after the data identifier name.

bit [7:0] c1; // packed array of scalar bit types real u [7:0]; // unpacked array of real types

A one-dimensional packed array is often referred to as a vector (see 6.9). Multidimensional packed arrays are also allowed.

Unpacked arrays may be fixed-size arrays (see 7.4.2), dynamic arrays (see 7.5), associative arrays (see 7.8), or queues (see 7.10). Unpacked arrays are formed from any data type, including other packed or unpacked arrays (see 7.4.5).

7.4.1 Packed arrays

A packed array is a mechanism for subdividing a vector into subfields, which can be conveniently accessed as array elements. Consequently, a packed array is guaranteed to be represented as a contiguous set of bits.

An unpacked array may or may not be so represented. A packed array differs from an unpacked array in that, when a packed array appears as a primary, it is treated as a single vector.

If a packed array is declared as signed, then the array viewed as a single vector shall be signed. The individual elements of the array are unsigned unless they are of a named type declared as signed. A part-select of a packed array shall be unsigned.

Packed arrays allow arbitrary length integer types; therefore, a 48-bit integer can be made up of 48 bits.

These integers can then be used for 48-bit arithmetic. The maximum size of a packed array can be limited, but shall be at least 65 536 (216) bits.

Packed arrays can be made of only the single bit data types (bit, logic, reg), enumerated types, and recursively other packed arrays and packed structures.

Integer types with predefined widths shall not have packed array dimensions declared. These types are byte, shortint, int, longint, integer, and time. Although an integer type with a predefined width n is not a packed array, it matches (see 6.22), and can be selected from as if it were, a packed array type with a single [n-1:0] dimension.

byte c2; // same as bit signed [7:0] c2;

integer i1; // same as logic signed [31:0] i1;

7.4.2 Unpacked arrays

Unpacked arrays can be made of any data type. Arrays whose elements are themselves arrays are declared as multidimensional arrays (see 7.4.5). Unpacked arrays shall be declared by specifying the element address range(s) after the declared identifier.

Elements of net arrays can be used in the same fashion as a scalar or vector net. Net arrays are useful for connecting to ports of module instances inside loop generate constructs (see 27.4).

Each fixed-size dimension shall be represented by an address range, such as [1:1024], or a single positive number to specify the size of a fixed-size unpacked array, as in C. In other words, [size] becomes the same as [0:size-1].

VERIFICATION LANGUAGE

The following examples declare equivalent size two-dimensional fixed-size arrays of int variables:

int Array[0:7][0:31]; // array declaration using ranges int Array[8][32]; // array declaration using sizes

The expressions that specify an address range shall be constant integer expressions. The value of the constant expression can be a positive integer, a negative integer, or zero. It shall be illegal for them to contain any unknown (x) or high-impedance bits.

Implementations may limit the maximum size of an array, but they shall allow at least 16 777 216 (224) elements.

7.4.3 Operations on arrays

The following operations can be performed on all arrays, packed or unpacked. The examples provided with these rules assume that A and B are arrays of the same shape and type.

— Reading and writing the array, e.g., A = B

— Reading and writing a slice of the array, e.g., A[i:j] = B[i:j]

— Reading and writing a variable slice of the array, e.g., A[x+:c] = B[y+:c]

— Reading and writing an element of the array, e.g., A[i] = B[i]

— Equality operations on the array or slice of the array, e.g., A==B, A[i:j] != B[i:j]

The following operations can be performed on packed arrays, but not on unpacked arrays. The examples provided with these rules assume that A is an array.

— Assignment from an integer, e.g., A = 8'b11111111;

— Treatment as an integer in an expression, e.g., (A + 3)

If an unpacked array is declared as signed, then this applies to the individual elements of the array because the whole array cannot be viewed as a single vector.

See 7.6 for rules for assigning to packed and unpacked arrays.

7.4.4 Memories

A one-dimensional array with elements of types reg, logic, or bit is also called a memory. Memory arrays can be used to model read-only memories (ROMs), random access memories (RAMs), and register files. An element of the packed dimension in the array is known as a memory element or word.

logic [7:0] mema [0:255]; // declares a memory array of 256 8-bit // elements. The array indices are 0 to 255 mema[5] = 0; // Write to word at address 5

data = mema[addr]; // Read word at address indexed by addr 7.4.5 Multidimensional arrays

A multidimensional array is an array of arrays. Multidimensional arrays can be declared by including multiple dimensions in a single declaration. The dimensions preceding the identifier set the packed dimensions. The dimensions following the identifier set the unpacked dimensions.

bit [3:0] [7:0] joe [1:10]; // 10 elements of 4 8-bit bytes // (each element packed into 32 bits) can be used as follows:

joe[9] = joe[8] + 1; // 4 byte add

joe[7][3:2] = joe[6][1:0]; // 2 byte copy

In a multidimensional declaration, the dimensions declared following the type and before the name ([3:0][7:0] in the preceding declaration) vary more rapidly than the dimensions following the name ([1:10] in the preceding declaration). When referenced, the packed dimensions ([3:0], [7:0]) follow the unpacked dimensions ([1:10]).

In a list of dimensions, the rightmost one varies most rapidly, as in C. However, a packed dimension varies more rapidly than an unpacked one.

bit [1:10] v1 [1:5]; // 1 to 10 varies most rapidly; compatible with memory arrays

bit v2 [1:5] [1:10]; // 1 to 10 varies most rapidly, compatible with C bit [1:5] [1:10] v3 ; // 1 to 10 varies most rapidly

bit [1:5] [1:6] v4 [1:7] [1:8]; // 1 to 6 varies most rapidly, followed by // 1 to 5, then 1 to 8 and then 1 to 7 Multiple packed dimensions can also be defined in stages with typedef.

typedef bit [1:5] bsix;

bsix [1:10] v5; // 1 to 5 varies most rapidly

Multiple unpacked dimensions can also be defined in stages with typedef.

typedef bsix mem_type [0:3]; // array of four 'bsix' elements mem_type ba [0:7]; // array of eight 'mem_type' elements

A subarray is an array that is an element of another array. As in the C language, subarrays are referenced by omitting indices for one or more array dimensions, always omitting the ones that vary most rapidly.

Omitting indices for all the dimensions references the entire array.

int A[2][3][4], B[2][3][4], C[5][4];

...

A[0][2] = B[1][1]; // assign a subarray composed of four ints A[1] = B[0]; // assign a subarray composed of three arrays of

// four ints each

A = B; // assign an entire array

A[0][1] = C[4]; // assign compatible subarray of four ints

A comma-separated list of array declarations can be specified. All arrays in the list shall have the same data type and the same packed array dimensions.

bit [7:0] [31:0] v7 [1:5] [1:10], v8 [0:255]; // two arrays declared

VERIFICATION LANGUAGE

7.4.6 Indexing and slicing of arrays

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

The term part-select refers to a selection of one or more contiguous bits of a single-dimension packed array.

logic [63:0] data;

logic [7:0] byte2;

byte2 = data[23:16]; // an 8-bit part-select from data The term slice refers to a selection of one or more contiguous elements of an array.

NOTE—IEEE Std 1364-2005 only permitted a single element of an array to be selected.

A 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 signed [31:0] busA [7:0] ; // unpacked array of 8 32-bit vectors int busB [1:0]; // unpacked array of 2 integers busB = busA[7:6]; // select a 2-vector slice from busA The size of the part-select or slice shall be constant, but the position can be variable.

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.

If an index expression is out of bounds or if any bit in the index expression is x or z, then the index shall be invalid. Reading from an unpacked array of any kind with an invalid index shall return the value specified in Table 7-1. Writing to an array with an invalid index shall perform no operation, with the exceptions of writing to element [$+1] of a queue (described in 7.10.1) and creating a new element of an associative array (described in 7.8.6). Implementations may issue a warning if an invalid index occurs for a read or write operation on an array.

Access to a packed array with an invalid index is described in 11.5.1.

See 11.5.1 and 11.5.2 for more information on vector and array element selecting and slicing.

Table 7-1—Value read from a nonexistent array entry

Type of array Value read

4-state integral type 'X 2-state integral type '0