Tuesday, January 26, 2016

Data type in SV - Part 3 : Arrays and Queues

Array :
Arrays hold a fixed number of equally-sized data elements. 
Individual elements are accessed by index using a consecutive range of integers. 
Some type of arrays allows to access individual elements using non consecutive values of any data types.
can be classified as fixed-sized/static arrays whose size cannot change once their declaration is done, or dynamic arrays, which can be resized. 

A.Fixed-Size Arrays : 
size is fixed

int low_high[0:15]   //16 ints 0 .... 15
int ary[16]   //16 ints 0 .... 15, in C style

Packed Array :
reg [0:10] ary  //Packed array, ary is a variable here ,
wire [31:0][1:0] ary  //2D Packed array, ary is a variable here ,

reg [31:0] ary1 [0:255] //unpacked array of 256 registers 
reg [31:0] ary1 [256] // register is packed 32 bit wide

 storage of an unpacked array : 


Array methods :
1. array querying method // $left. $right, $low, $high, $increment, $size
2. array locator method
2. array reduction method  //sum, product, or, and ,xor
3. array ordering method  // reverse, sort(in ascending order), rsort(in descending order), shuffle
4. array index querying            



B. Dynamic Array : 
1.It is an unpacked array
2. size can be set during run time .
3. this is done basically to avoid wasting of memory
4. It is used to deal with continuous collection of variables

integer ary1[] ; // declaration
ary1 = new [10] ; // allocation of memory

ary1 = new[10] ('{4,5,6,7,8,9})  // initialization ... here array elements are 4,5,6,7,8,9,9,0,0,0,0
ary1 = new[15] ary2   // resizing of ary1

if we want to copy the elements from different array

ary2 = new [100] ary1   //prev. ary1 elements will be reserved


C.Assosiative Arrays : when the size of collection is unknown or the data space is sparse, associatove array is better option,
it ensures faster access .

int ary [*]  ; //wildcard index (any integral type)
int ary[string];  //string index
int ary[class];   //class index
int ary[integer]; // integer ,,

here string, class or integer is called "keytype"
ary // array name
int // data type

No need to use new method for mem declaration ins case of associative array
methods : num/size, delete, exists, first, last, next, prev


4.Queues : 
1.variable size
2. ordered collection of homogeneous element
3. can be passed to tasks/functions as ref or non-ref arguments

int q[$] = {1,2,3} ;
int q[$]   // queue declaration
int e, pos ;
e = q[0]  // first element
e = q[$]  // last element

p = q //copy

Methods : size, insert, popfront, pop=back, push-front, push-back

Dynamic array of queues :
typedef int q[$] ;
q ary[]   ; // array of queues


Queues of queue :
typedef int q[$] ;
q p[$] ;


COMPARISON OF ARRAYS   // from testbench.in

Static Array 
Size should be known at compilation time. 
Time require to access any element is less. 
if not all elements used by the application, then memory is wasted.
Not good for sparse memory or when the size changes. 
Good for contagious data. 


Associative Array 
No need of size information at compile time. 
Time require to access an element increases with size of the array.
Compact memory usage for sparse arrays. 
User don't need to keep track of size. It is automatically resized. 
Good inbuilt methods for Manipulating and analyzing the content. 

Dynamic Array
No need of size information at compile time. 
To set the size or resize, the size should be provided at runtime. 
Performance to access elements is same as Static arrays. 
Good for contagious data. 
Memory usage is very good, as the size can be changed dynamically. 



Queues 
No need of size information at compile time. 
Performance to access elements is same as Static arrays. 
User doesn't need to provide size information to change the size. It is automatically resized. 
Rich set of inbuilt methods for Manipulating and analyzing the content. 
Useful in self-checking modules. Very easy to work with out of order transactions. 
Inbuilt methods for sum of elements, sorting all the elements. 
Searching for elements is very easy even with complex expressions.
Useful to model FIFO or LIFO. 




If your code accidently tries to read from an out-of-bounds address, System-
Verilog will return the default value for the array element type. That just means
that an array of 4-state types, such as logic , will return X’s, whereas an array of
2-state types, such as int or bit , will return 0. This applies for all array types –
fi xed, dynamic, associative, or queue, and also if your address has an X or Z. An
undriven net is Z.If your code accidently tries to read from an out-of-bounds address, System-
Verilog will return the default value for the array element type. That just means
that an array of 4-state types, such as logic , will return X’s, whereas an array of
2-state types, such as int or bit , will return 0. This applies for all array types –
fi xed, dynamic, associative, or queue, and also if your address has an X or Z. An
undriven net is Z.


No comments:

Post a Comment

Ethernet and more

Ethernet is a protocol under IEEE 802.33 standard User Datagram Protocol (UDP) UDP is a connectionless transport protocol. I...