Home System Verilog Constructs SV Arrays
Main Menu
Home
System Verilog
    - Constructs
        -- Introduction
        -- Data Types
        -- Type Casting
        -- SV Arrays
        -- Assoc Arrays
        -- Dynamic Array
        -- Queues
        -- Operators
        -- Procedural statements
        -- Classes
        -- OOP Concepts
        -- Inter process communication
        -- Randomization
        -- Interface
        -- Clocking block
        -- Conststraits
        -- Coverage
        -- DPI
        -- Regular expression
        -- Files
    - SV Classes
    - Functional Coverage SV
    - Examples
    - Tools
    - Links
    - Books
    - Interview Questions SV
Verilog
Methodologies
Open Vera
Digital Concepts
Verification Basics
Protocols
Scripting
Articles
Videos
Interview Questions
Computer Architechture
C and C++
Blog/Article
AsicGuru Blog
Tags Cloud
Ads
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
System Verilog Arrays
Share This Articale:

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

  • Fixed Size Arrays 
  • Dynamic Arrays
  • Queues
  • Associative arrays

1. Fixed Size Arrays : (data_type name [constant];)

Fixed size arrays are fast and static.In fixed size arrays

  1. Multiple dimentions supported
  2. Out of bound writes are ignored. Out of bound returns x even for 2 state data types
  3. Arrays data are stored in 32 bit words

Examples   :

int twoD1[0:7][0:23];  // 2D array

int twoD2[8][24];      // same as above

twoD1 = twoD2;         // Array copy
if (twoD1==twoD2)...     // Array compare

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 :

  1. Similar to fixed size arrays but size can be given in the run time
  2. Dynamic arrays can have only single dimention and it can not be packed
  3. Out of bound error in dynamic arrays causes run time error.

Examples :

int d[], b[];         // Two dynamic arrays
d = new[5];           // Make array with 5 elements

foreach (d[j])        // Initialize
   d[j] = j;

b = d;                // Copy a dynamic array
b[0] = 5;

$display(d[0],b[0]);  // See both values (0 & 5)

d = new[20](d);       // Expand and copy

d = new[100];         // Allocate 100 new integers
                              // Old values are lost


d.delete();           // Delete all elements

3. Queues ( data_type name [$]; ) :

In queues size is flexible. It can change easily

  1. Variable size array with automatic sizing, single dimension
  2. Many searching, sorting, and insertion methods
  3. Constant time to read, write, and insert at front & back
  4. Out of bounds access causes run-time error

Examples :

int q[$] = {0,1,3,6};

int j = 2, b[$] = {4,5};


q.insert(2, j);                           // {0,1,2,3,6}  Insert before s[2]
q.insert(4, b);                          // {0,1,2,3,4,5,6} Insert whole queue
q.delete(1);                            // {0,2,3,4,5,6}                  // {0,2,3,4,5,6} Delete element #1
q.push_front(7);                      // {7,0,2,3,4,5,6} Insert at front  

j = q.pop_back();                    // {7,0,2,3,4,5}      j = 6
q.push_back(8);                      // {7,0,2,3,4,5,8} Insert at back
j = q.pop_front();                    // {0,2,3,4,5,8} j = 7
$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 :

  1. Dynamically allocated, non-contiguous elements
  2. Accessed with integer, or string index, single dimension
  3. Great for sparse arrays with wide ranging index
  4. Array functions: exists, first, last, next, prev

int aa[*], i;

// Printing Full Array

foreach (aa[i])

     $display (i, aa[i]);  

Go to next section for more details..


This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start


Prev << Type Casting

Next >> Assoc Arrays

 
Login/Register
Register
Sign In
Login with :-
gmail-yahoo-twitter-facebook
| | |  
  •  
  • Bookmark
    ADS