Arrays in system verilog : An array is a collection of variables, all of the same type, and accessed using the same name plus one or more indices. reg [7:0] r1 [1:256]; // [7:0] is the vector width, [1:256] is the array size SystemVerilog uses the term packed array to refer to the dimensions declared before the object name (what Verilog refers to as the vector width). The term unpacked array is used to refer to the dimensions declared after the object name. bit [7:0] c1; // packed array real u[7:0]; // unpacked array System verilog enhances the arrays by allowing multiple dimentions.System verilog have following type arrays 1.Fixed Size Arrays 2.Dynamic Arrays 3.Queues Associative arrays1. Fixed Size Arrays : (data_type name [constant];) Fixed size arrays are fast and static.In fixed size arraysMultiple dimentions supportedOut of bound writes are ignored. Out of bound returns x even for 2 state data typesArrays data are stored in 32 bit wordsExamples :view sourceprint? 1.int twoD1[0:7][0:23]; // 2D array2. 3.int twoD2[8][24]; // same as above4. 5.twoD1 = twoD2; // Array copy6.if (twoD1==twoD2)... // Array compare7. 8.bit [3:0][7:0] bytes [0:2]; // 3 entries of packed 4 bytes 2. Dynamic Arrays (data_type name [ ]) :Dynamic arrays are fast and variable size is possible with a call to new () function. In dynamic size array :Similar to fixed size arrays but size can be given in the run timeDynamic arrays can have only single dimention and it can not be packedOut of bound error in dynamic arrays causes run time error.Examples :view sourceprint?01.int d[], b[]; // Two dynamic arrays02.d = new[5]; // Make array with 5 elements03. 04.foreach (d[j]) // Initialize05.d[j] = j;06. 07.b = d; // Copy a dynamic array08.b[0] = 5;09. 10.$display(d[0],b[0]); // See both values (0 & 5)11. 12.d = new[20](d); // Expand and copy13. 14.d = new[100]; // Allocate 100 new integers15.// Old values are lost16. 17. 18.d.delete(); // Delete all elements 3. Queues ( data_type name [$]; ) :In queues size is flexible. It can change easilyVariable size array with automatic sizing, single dimensionMany searching, sorting, and insertion methodsConstant time to read, write, and insert at front & backOut of bounds access causes run-time errorExamples :view sourceprint?01.int q[$] = {0,1,3,6};02. 03.int j = 2, b[$] = {4,5};04. 05. 06.q.insert(2, j); // {0,1,2,3,6} Insert before s[2]07.q.insert(4, b); // {0,1,2,3,4,5,6} Insert whole queue08.q.delete(1); // {0,2,3,4,5,6} // {0,2,3,4,5,6} Delete element #109.q.push_front(7); // {7,0,2,3,4,5,6} Insert at front 10. 11.j = q.pop_back(); // {7,0,2,3,4,5} j = 612.q.push_back(8); // {7,0,2,3,4,5,8} Insert at back13.j = q.pop_front(); // {0,2,3,4,5,8} j = 714.$display(q.size); // “6” Traversing :foreach (q[i]) $display(q[i]);4. Associative Arrays ( DataType name [*]; ) :Associative arrays are generally used for sparse memories. Associative arrays are :Dynamically allocated, non-contiguous elementsAccessed with integer, or string index, single dimensionGreat for sparse arrays with wide ranging indexArray functions: exists, first, last, next, prevint aa[*], i;// Printing Full Arrayforeach (aa[i]) $display (i, aa[i]); |