Lecture5 System Verilog Advanced Datatypes PDF
Lecture5 System Verilog Advanced Datatypes PDF
Index
Mixed Arrays
Queues
Structures
Enumerated Types
Strings
1
1/25/16
Mixed Arrays
3 2 1 0
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
2
1/25/16
0 1
3 2 1 0 3 2 1 0
2 1 0 2 1 0 2 1 0 2 1 0 2 1 0 2 1 0 2 1 0 2 1 0
0
1 1
logic [3:0][2:0]mixed_array[0:1][0:2]
Dynamic Arrays
3
1/25/16
Dynamic Arrays
What if you dont know the size of the array before run time?
Array size is provided at run-time and not compile-time
Array types Array name
Can be allocated and resized during simulation
new[] operator is used to allocate space, passing the number of entries type array_name[];
array_name=new[number of elements];
Can also pass name of an array to copy values
Dynamic Arrays are used when the size of the array is not known before
runtime
7
Dynamic Arrays
int dyn[], d2[]; Declare dynamic arrays
initial begin
dyn=new[5]; Allocate 5 elements
foreach (dyn[j]) dyn[j]=j; Initialize the array
d2=dyn; Copy a dynamic array
d2[0]=5; Modify the copy
$display(dyn[0],d2[0]); Displays 0 and 5
dyn=new[20](dyn); Allocate 20 ints and copy old values
Allocate 100 ints.
dyn=new[100];
Delete all elements dyn
dyn.delete();
end dyn 0 0
0 0 1 1
dyn d2 d2 1 1 2 2
0 0 0 0 0 5 2 2 3 3
1 1 1 1 1 1 dyn[0]=0
d2[0]=5 3 3 4 4
2 2 2 2 2 2 4 4
3 3 3 3 3 3
4 4 4 4 4 4 19 99
4
1/25/16
Queues
Queues
Push and pop operations are efficient Queue type Queue name
5
1/25/16
Queues
int j=7;
int b[$]={3,4}; Declare the queue and initialize it
int q[$]={0,2,5}; Declare the queue and initialize it
initial begin
q.insert(1,j); {0,7,2,5} Insert j at position 1
q.insert(3,4); {0,7,2,4,5}Insert 4 at position 3
{0,2,4,5} Delete element at position
q.delete(1);
q.push_front(6) {6,0,2,4,5}
j=q.pop_back; {6,0,2,4} j=5
q.push_back(8); {6,0,2,4,8}
j=q.pop_front; {0,2,4,8} j=6
end
j=5 j=6
b q q q q q
q q q
3 0 6 0 6 0 6 0 0
0 0 0 0 0 0 0 0 0
4 2 0 1 0 1 0 1 2
1 1 1 7 1 7 1 2 1
2 5 2 2 2 2 2 2 4
2 2 2 2 2 4 2
4 4 3 4 3 8
3 5 3 4 3 5 3 3
4 5 4 5 4 8 1
1
Associative Arrays
6
1/25/16
Associative Arrays
Dynamically allocated, non-contiguous elements
Accessed with integer, or string index, single dimension
type name[*];
Great for sparse arrays with wide ranging index
Dynamic Arrays are used when not all elements of an array are going to be
13 used
Associative Arrays
assoc assoc assoc
0 0 1 1
1 1 1 1 2 2 logic [63:0] assoc[*],
2 2 2 4 4 idx=1,i,j;
2
repeat(64)
3 3 8 8 begin
4 4 4 4 16 16 assoc[idx]=idx;
idx=idx<<1;
5 5
end
6 6 i=assoc[first];
j=assoc.next;
7 7
8 8 8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16
14
16 16 16
7
1/25/16
Array Methods
Array Methods
Array reduction methods (Can be used on unpacked arrays: fixed,
dynamic, queue, associative)
usum, product, and, or and xor
ua.sum of a single bit array returns a single bit value unless stored in a wider
value or compared to a wider value
bit on[10]
int summ;
initial begin
foreach (on[i])
on[i]=i; //0,1,0,1,0,1,0,1,0,1
$display(on.sum) //1
summ=on.sum; //summ=5
end
16
8
1/25/16
Array Methods
Array locator methods int q[$] = {1,3,5,7}, tq[$];
int d[] = {9,1,8,3,4};
u sort, rsort, reverse, find_first, find_first_with_index int f[6] = {1,6,2,6,8,6};
ufind_first, find_first_with_index
18
9
1/25/16
Array Methods
Array Sorting and Ordering
u sort, rsort, reverse, shuffle
19
Fixed-size Arrays:
u Size known at compile time
u Contiguous data
u Multi-dimensional arrays
Dynamic Arrays:
u Size unknown at compile time
u Contiguous data
Queues:
u FIFO/Stack
Associative Arrays:
u Sparse data memories
20
10
1/25/16
Structures
Structures
uA structure is a collection of variables and/or constants
that can be accessed separately or as a whole
Structure struct {
uAllows logical groups of signals to be combined variables
together (example: control signals of a bus protocol) variables
} struct_name
struct name
struct {
int a,b;
logic [7:0] opcode;
logic [23:0] address;
bit error;
} Instruction_Word
Instruction_Word.address=24hF00000;
Instruction_Word.opcode=8b00001111;
22
11
1/25/16
Structures
struct packed {
logic valid;
logic [7:0] tag;
logic [31:0] data;
} data_word
23
Packed Structures
Packed structures
The members of a packed structure can be referenced by either the name of the
member or by part select of the vector
data_word.tag = 8hf0;
data_word [39:32]=8hf0;
24
12
1/25/16
Thank You
13