Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Thursday, 16 April 2015

Pass unpack array between two modules

module module1();
  bit dyn_ary[11];
  initial begin
    foreach (dyn_ary[i]) begin
      dyn_ary[i] = $urandom_range(10, 12323);
    end
 
    #40;
    foreach (dyn_ary[i]) begin
      $display ($time,,"module1 : dyn_ary[%0d]=%0h", i, dyn_ary[i]);
    end
  end
endmodule : module1


module module2 (input bit dyn_array[11]);
  initial begin
    #100;
    foreach (dyn_array[i]) begin
      $display($time,,"module2 : dyn_array[%0d]=%0h", i, dyn_array[i]);
    end
  end
endmodule : module2
 

module top_module();
  module1 M1 ();
  module2 M2 (.dyn_array(M1.dyn_ary));
endmodule : top_module


Output:
 40 module1 : dyn_ary[0]=0
 40 module1 : dyn_ary[1]=0
 40 module1 : dyn_ary[2]=1
 40 module1 : dyn_ary[3]=0
 40 module1 : dyn_ary[4]=1
 40 module1 : dyn_ary[5]=1
 40 module1 : dyn_ary[6]=0
 40 module1 : dyn_ary[7]=1
 40 module1 : dyn_ary[8]=0
 40 module1 : dyn_ary[9]=1
 40 module1 : dyn_ary[10]=1
100 module2 : dyn_array[0]=0
100 module2 : dyn_array[1]=0
100 module2 : dyn_array[2]=1
100 module2 : dyn_array[3]=0
100 module2 : dyn_array[4]=1
100 module2 : dyn_array[5]=1
100 module2 : dyn_array[6]=0
100 module2 : dyn_array[7]=1
100 module2 : dyn_array[8]=0
100 module2 : dyn_array[9]=1
100 module2 : dyn_array[10]=1

Sunday, 29 March 2015

Difference between Dynamic Array and Assosicate Array in SystemVerilog

With a regular array, you must specify its size when you declare it

bit my_array[10];
With a dynamic array you can allocate the size of the array during runtime (hence the term "dynamic").
bit my_dynamic_array[];  // Note size is not specified
...
my_dynamic_array = new[size];  // size is determined at run-time


Also, the array can be "re-sized" at a later point:
my_dynamic_array = new[new_size](my_dynamic_array);

In this case, new memory is allocated, and the old array values are copied into the new memory, giving the effect of resizing the array.

The main characteristic of an associative array is that the index type can be any type - you are not restricted to just integer values. For example, you can use a string as the index to look up an "associated" value.

bit my_assoc_array[string];  // Array stores bits, using a string as an index
...
my_assoc_array["testname"] = 1;  //Stores a 1 into the array element indexed by the value "testname"
$display(my_assoc_array["testname"]); // Displays 1

An associative array is also "dynamic", in the sense that it does not have a pre-determined size. However, you do not have to allocate the size - it grows as you add more elements into it.


Another answer is:

    Dynamic arrays are useful for dealing with contiguous collections of variables whose number changes dynamically.
    e.g.            int array[];
    When the size of the collection is unknown or the data space is sparse (scattered-throw in various random directions.), an associative array is a better option. In associative array, it uses the transaction names as the keys in associative array.
   e.g.            int array[string];

Different types of Arrays in SystemVerilog

Dynamic Array: 
  • We use dynamic array when we have no idea about the size of the array during compile time and we have to allocate its size for storage during run time.
  • We basically use this array when we have to store a contiguous or Sequential collection of data.
  • The array indexing should be always integer type.
  • To allocate size of a dynamic array, we have to use new[] operator.
            Example: 
               int my_array [];
               initial
                   begin
                         my_array = new[4];                              //Allocated 4 elements 
                    end 
  • To resize a dynamic array, we have to do as follows:
             Example:               
               initial 
                  begin 
                       my_array = new[18](my_array);     //Resize the Array and Copy 
                  end 

  • To know the size of the array, we have to use size() operator.
             Example: 
              my_array.size();                    //Returns the current size of the array, my_array as an integer. 

  • To delete an element from a dynamic array, we have to use delete() operator.
             Example: 
              initial 
                  begin 
                       my_array.delete();    //All the elements of array, my_array will be deleted. 
                   end


Associative Array: 
  • It is also allocated during run time.
  • This is the array, where data stored in random fashion.
  • It is used when we don’t have to allocate contiguous collection of data, or data in a proper sequence or index.
  • In associative array, the index itself associates the data. So it is called so.
  • Indexing is not regular, can be accessed using indexing like integer or string type or any scalar. 
Example: 
my_array ["name"];           // “name”, Index type is a string
my_array[address];           // address, Index type is an integer (here address is an integer).
my_array[my_class];       // my_class, Index type is a class.
my_array[s_array];          // s_array, Index type is an array.
  • It is better to use associative array, when size of the array is unknown & data space is random or irregular or sparse.
  • Following are the methods associated with Associative array. 
          num() — returns the number of entries in the Associative array 
Eg: my_array.num() 
first() — assigns the value of the first index in the Associative array to the given index variable Eg:my_array.first(i); 
last() — assigns the value of the last index in the Associative array to the given index variable Eg:my_array.last(i); 
next() — assigns the value of the next index in the Associative array to the given index variable Eg:my_array.next(i); 
prev() — assigns the value of the previous index in the Associative array to the given index variable Eg:my_array.prev(i); 
delete() — removes all the elements in the Associative array. 
Eg: my_array.delete(i); If the index is specified, then the element at the specified index “i”is deleted 
exists() — checks if element exists at the specified index “i” in the Associative arrayEg:my_array.exists(i);



 Queue: 
  • Queue is a variable size, ordered collection of Homogenous Data.
  • It is flexible, as it is variable in size and analogous to an 1-dimensional Unpacked array that can shrink & grow automatically and can be of size zero.
  • The main advantage of queue over dynamic array is that, we don’t need new[] operatorto allocate storage space for a queue.
  • The other advantages of queue over dynamic array is that we can manipulate the queue using various queue methods like: push, pop, delete, insert, size.
Example: 
module test_example ; 
int my_queue[$] = { 1, 2, 3 }; 
string s_queue [$] = {"first","second","third","fourth"}; 
string store;
initial 
begin
// Use of the size() method/operator 
$display("\n size() operator used"); 
for (int i = 0 ; i < my_queue.size(); i++ ) 
$display (my_queue[i]); 
$display("\n\n Elements of s_queue is :"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of insert() method/operator 
s_queue.insert(1,"next"); // Previous element 1 is now turned to element 2. 
s_queue.insert(2,"somewhere"); 
$display("\n\n insert() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of delete() method/operator 
s_queue.delete(1); // delete the element 1 
s_queue.delete(3); // delete the element 3 
$display("\n\n delete() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(string_queue[i]," "); 
// Use of pop_front() method/operator (it deletes the front of the queue) 
store = s_queue.pop_front(); 
$display("\n\n pop_front() operator used"); 
$display(" %s",store); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of pop_back() method/operator (it deletes the back of the queue) 
store= s_queue.pop_back(); 
$display("\n\n pop_back() operator used"); 
$display(" %s",store); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of push_front() and push_back() method/operator 
s_queue.push_front("in-front"); 
s_queue.push_back("in-back"); 
$display("\n\n push_front() and push_back() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," \n "); 
end 
endmodule
 
Result: 
size() operator used 
1 
2 
3 
 
Elements of s_queue[$] is: 
first second third fourth 
 
insert() operator used 
first next somewhere second third fourth 
 
delete() operator used 
first somewhere second fourth 
 
pop_front() operator used 
first 
somewhere second fourth 
 
pop_back() operator used 
fourth 
somewhere second 
 
push_front() and push_back() operator used 
in-front 
somewhere 
second 
in-back