0% found this document useful (0 votes)
12 views38 pages

SV_Arrays_Structures

The document provides an overview of various data structures in programming, including arrays, structures, unions, dynamic arrays, associative arrays, and queues. It explains the definitions, advantages, and differences between these data types, along with examples of their usage and manipulation methods. Key concepts such as packed and unpacked structures, enumerated data types, and array operations are also discussed.

Uploaded by

brajeshkp7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views38 pages

SV_Arrays_Structures

The document provides an overview of various data structures in programming, including arrays, structures, unions, dynamic arrays, associative arrays, and queues. It explains the definitions, advantages, and differences between these data types, along with examples of their usage and manipulation methods. Key concepts such as packed and unpacked structures, enumerated data types, and array operations are also discussed.

Uploaded by

brajeshkp7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Padmanaban K

Arrays, Structures and Unions

1
Packed and Unpacked Arrays

2
Unpacked Array to Packed Array

3
Multidimensional Arrays

4
Array Replication

5
Array with default values

6
Array with default values

7
Enumerated Example

1
-1
9

46327
46071**slid e
Enumerated Data type

9
Enumerated Data type

10
Enumerated Data type

11
Enumerated Data type

12
Enumerated Data type

13
Do while Construct

14
Definition of Structures (1)

A structure is a data type that represents a collection of data types to


allow convenient grouping of signals

Definition of Structures Referencing Structures

1
-4

60935
Definition of Structures (2)

Advantages
— Enables a higher level of abstraction
— Easy to interface with other languages
— Can improve simulation performance

Structures Arrays
Can have members of different data Require all elements to be of the
types same type and size
Members are referenced by name Elements of an array are referenced
by index

1
-5

Structures are synthesizable

60935
Unpacked and Packed Structures

Unpacked Structures Packed Structures


By default, a structure is unpacked The keyword packed is used to declare a
packed structure
Unpacked means that the members of the Packed means that the structure stores all
structure are stored as independent variables members as contiguous bits in a specific
or constants order
The layout of the storage varies from one A packed structure is stored as a vector, with
software to another the first member occupying the left-most field

Members of an unpacked structure can be of All members of a packed structure must be


integral or non-integral data types integral data types

1
-6

60935
Packed Structures Example

1
-7

60935
60943**slid e
Definition of Unions (1)

A union is an aggregate data type like structure but uses the same memory for
all its members

Only one member of the union can be used at a time

Defining a Union Referencing a Union

1
-1
3

60935
Definition of Unions (2)

Have the same advantages as structures

Unions Structures
Use the same memory for all members Use different memory for each member
More efficient than structures when its Can access more than one member at
members are not required to be the same time, unlike unions
accessed at the same time

Not supported by Verilog

1
-1
4

60935
63270**slid e
Dynamic Arrays

Array size allocated at runtime with constructor


— Out-of-bounds write ignored
— Out-of-bounds read returns '0' for 2-state arrays, 'x' for 4-state arrays
— Multiple dimensions supported

Pre-defined methods to work with dynamic arrays


— new[] – used to set or change the size of the array
— size() – returns the current size of the array
— delete() – clears all the elements

1
-3
7

46327
60757**slid e
Dynamic arrays

• A dynamic array is one dimension of an unpacked array


whose size can be set or changed at run time.
• The space for a dynamic array doesn’t exist until the array
is explicitly created at run time.
• SystemVerilog provides set of function to work with
dynamic arrays .
• new[]:This operator is used to set or change the size of the
array.
• Size[]: This method returns the current size of the array.
• delete[]:This method clears all the elements yielding an
empty array (zero size).

22
22
Dynamic arrays
logic 1FA1[2];
logic lDA1[];
initial
begin
$display("lDA1 size = %0d",lDA1.size);
lFA1[1]=1;
lFA1[0]=0;
lDA1=lFA1;
$display("lDA1[1] = %0d", lDA1[1]);
$display("lDA1[0] = %0d", lDA1[0]);
$display("lDA1 size = %0d",lDA1.size);
end
endprogram

23
Associative Arrays

• Dynamic arrays are useful for dealing with contiguous


collections of variables whose number changes
dynamically.
• When the size of the collection is unknown or the data
space is sparse , an associative array is a better option.
• Associative arrays do not have any storage allocated until
it is used, and the index expressions is not restricted to
integral expressions, but can be of any type.
• An associative array implements a lookup table of its
elements of its declared type.
• The data type to be used as an index serves as the lookup
key ,and imposes an ordering.

24
24
Associative arrays
module m;
bit [2:0] AA1[*];
int int1;
logic [7:0] log1;
initial begin
int1 = 27;
log1 = 42;
AA1[456] = 3'b101;
AA1[int1] = 3'b000; // index is 27
AA1[log1] = 3'b111; // index is 42
end
endmodule
25
Associative Arrays
module asso_test;
bit [31:0]mem[*];
bit [31:0]i;
initial
begin
mem[7]=18;
mem[21]=20;
mem[83]=99;
mem[100]=989;
mem[157]=87;
mem[200]=111;

$display("%0d",mem.num);
$display("%0d",mem[12]);
$display("%0d",mem[200]);
mem.first(i);
$display("first index is %0d",i);

26
Associative Arrays
mem.next(i);
$display("next index of dirst index is %0d",i);

mem.last(i);
$display("Last index is %0d",i);

mem.prev(i);
$display("second last index is %0d",i);

mem.delete(i);
$display("deleted index is %0d",i);

$display("%0d",mem.num);

$display("%p",mem);

27
Associative Arrays
if(mem.exists(i))
$display("element found at %0d",i);

else
$display("element not found");

$display("%p",mem.max);

$display("%p",mem.min);

mem.delete();

$display("%p",mem);

end
endmodule

28
Queues (1)

A queue is a variable-size, collection of elements similar to a


one-dimensional unpacked array that grows and shrinks automatically

A queue supports constant-time access to its elements

Insertion/removal at the beginning or end of the queue

Queues are declared like unpacked arrays, but specifying $ as the


array size

1
-4
0

46327
46344**slid e
Queues (2)

The maximum size of a queue can be limited by specifying its optional


right bound (last index)

All elements into a queue are indexed by an integral iterator The first

iterator is 0 and the last iterator is $

Queues are very useful in modeling FIFO and LIFO buffers

1
-4
1

46327
88975**slid e
Queues: Predefined Methods

Method Description
insert() Inserts the given item at the specified index position
delete() Deletes the item at the specified index
pop_front() Removes and returns the first element of the queue
pop_back() Removes and returns the last element of the queue
push_front() Inserts the given element at the front of the queue
push_back() Inserts the given element at the end of the queue
size() Returns the number of items in the queue. If the queue is empty, it returns 0

1
-4
3

46327
60759**slid e
Queues Example (1)

Queue is defined using [$]

1
-4
4

46327
Queues Example (2)

The same results can be obtained via opposite push and pop methods

1
-4
5

46327
46346**slid e
Queue

34
Array Manipulations
module arrays_test;
int q[$]={0,7,9,5,11,11};
int d[]='{0,7,9,5,11,11};
int f[6]='{0,7,9,5,11,11};

initial
begin
$display("%d",q.and);
$display("%d",d.and);
$display("%d",f.and);
$display("%d",q.or);
$display("%d",d.or);
$display("%d",f.or);
$display("%d",q.xor);
$display("%d",d.xor);
$display("%d",f.xor);

35
Array Manipulations
$display("%d",q.sum);
$display("%d",d.sum);
$display("%d",f.sum);
$display("%d",q.product);
$display("%d",d.product);
$display("%d",f.product);
q.sort;
$display("%p",q);
f.sort;
$display("%p",f);
d.sort;
$display("%p",d);
q.rsort;

36
Array Manipulations
$display("%p",q);
f.rsort;
$display("%p",f);
d.rsort;
$display("%p",d);
q.shuffle;
$display("%p",q);
f.shuffle;
$display("%p",f);
d.shuffle;
$display("%p",d);
q.reverse;
$display("%p",q);
f.reverse;
$display("%p",f);
d.reverse;

37
Array Manipulations
$display("%p",d);
$display("%p",q.unique);
$display("%p",f.unique);
$display("%p",d.unique);
$display("%p",q.max);
$display("%p",f.max);
$display("%p",d.max);
$display("%p",q.min);
$display("%p",f.min);
$display("%p",d.min);

end

endmodule

38

You might also like