Home System Verilog Constructs Dynamic Array
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
Dynamic Arrays in system verilog
Share This Articale:

Dynamic Arrays :

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.

 

 

module example;

        reg [31:0] regArray_1[];
        reg [31:0] regArray_2[];
        reg [31:0] regArray_3[];
        reg [31:0] regArray_[];

        int i;

        initial
        begin
                regArray_1 = new [5];
                regArray_3 = new [3];
                regArray_ = new [10];

                // initialize values for regArray_1
                for ( i = 0 ; i < regArray_1.size(); i ++ )
                        regArray_1[i] = i;

                // copy regArray_1 to regArray_2
                regArray_2 = new[regArray_1.size()] (regArray_1);

                // initialize values for regArray_3
                for ( i = 0 ; i < regArray_2.size(); i ++ )
                        $display ("%d: %d \n" , i, regArray_2[i]);

                $display ("regArray_3");
                for ( i = 0 ; i < regArray_3.size(); i ++ )
                        $display ("%d: %d \n" , i, regArray_3[i]);

                // Display the result of the transformation
                $display ("Transformed regArray_1, with reduced size");
                for ( i = 0 ; i < regArray_1.size(); i ++ )
                        $display ("%d: %d \n" , i, regArray_1[i]);

                // copying 'regArray_' to 'regArray_1'.
                regArray_1 = new[10](regArray_);
                $display ("regArray_");
                for ( i = 0 ; i < regArray_.size(); i ++ )
                        $display ("%d: %d \n" , i, regArray_[i]);

                // Display the result of the transformation
                $display ("Transformed regArray_1, with increased size");
                for ( i = 0 ; i < regArray_1.size(); i ++ )
                        $display ("%d: %d \n" , i, regArray_1[i]);

                regArray_1.delete(); // Deleting the elements of regArray_1
                // Display the size of the array after deletion
                $display ("Size of the regArray_1 after deletion = %d",
                regArray_1.size());

                #100 $finish;
        end
endmodule

Output :

# Copy of old regArray_1
#           0:          0
#
#           1:          1
#
#           2:          2
#
#           3:          3
#
#           4:          4
#
# regArray_3
#           0:          1
#
#           1:          1
#
#           2:          1
#
# Transformed regArray_1, with reduced size
#           0:          1
#
#           1:          1
#
#           2:          1
#
#           3:          x
#
#           4:          x
#
#           5:          x
#
#           6:          x
#
#           7:          x
#
#           8:          x
#
#           9:          x
#
# regArray_
#           0:          0
#
#           1:          0
#
#           2:          0
#           2:          0
#
#           3:          0
#
#           4:          0
#
#           5:          0
#
#           6:          0
#
#           7:          0
#
#           8:          0
#
#           9:          0
#
# Transformed regArray_1, with increased size
#           0:          0
#
#           1:          0
#
#           2:          0
#
#           3:          0
#
#           4:          0
#
#           5:          0
#
#           6:          0
#
#           7:          0
#
#           8:          0
#
#           9:          0
#

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


Prev << Assoc Arrays

Next >> Queues

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