0% found this document useful (0 votes)
94 views

Lecture5 System Verilog Advanced Datatypes PDF

Uploaded by

sureshchandra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Lecture5 System Verilog Advanced Datatypes PDF

Uploaded by

sureshchandra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1/25/16

SystemVerilog Advanced Datatypes

Engineering Design Institute


Engineering Design Institute

Index

Mixed Arrays

Queues

Structures

Enumerated Types

Strings

1
1/25/16

Mixed Arrays

Multidimensional Packed Arrays: Mixed Arrays

Common to have combination of packed and unpacked


array
type [dim1][dim2] array_name [dim];
array_packed is an unpacked array of three packed
elements
Packed Unpacked
which dimension is unpacked? Array types
dimensions
Name of the array dimension

bit [3:0] [7:0] array_packed [0:2];

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

Mixed Arrays: Indexing


When indexing mixed arrays, unpacked dimensions are referenced first from the left-most to the right-most dimension
Packed dimensions are referenced second from the left-most dimension to the right-most dimension

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]

mixed_array [0] [1] [2] [1] = 1b1;


5

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

Declared with empty subscripts [ ]

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

$size function returns the size of a fixed-size or dynamic array


Number of
Array name
elements

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

Dynamic Arrays can be resized during simulation

4
1/25/16

Queues

Queues

Can provide easy sorting and searching

Allocates extra space to quickly add extra elements type name[$];

Does not need the new[ ] operator

Push and pop operations are efficient Queue type Queue name

Can add and remove elements from anywhere

Can copy contents of fixed or dynamic arrays to the queue

Provide inbuilt ability to perform several functions


1
0

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

Array type Array name

logic [63:0] assoc[*], idx=1;


repeat(64) begin
assoc[idx]=idx;
idx=idx<<1;
end

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};

$display(q.sum, q.product); // 16 105


tq = q.min(); // {1}
tq = q.max(); // {7}
tq = f.unique; // {1,6,2,8}
tq = d.find with (item > 3); // {9,8,4}
tq = d.find_index with (item > 3); // {0,2,4}

q[$] tq[$] d[ ] f[6] q[$] tq[$] tq[$] tq[$] tq[$]


0 1 0 9 0 1 0 1 0 1 0 7 0 1 0 9
1 3 1 1 1 6 1 3 q.min=1 q.max=7 1 6 1 8
2 5 2 8 2 2 2 5 2 2 2 4
3 7 3 3 3 6 3 7 3 8
4 4 4 8 q.sum=16
d.find with (item>3)
f.unique
5 6 q.product=105
17

Array Methods: Example

ufind_first, find_first_with_index

int d[] = {9,1,8,3,4,4}, tq[$];


tq = d.find with(item > 3); {9,8,4,4}
tq = d.find_index with (item>99); {}
tq = d.find_first_index with (item==8); {2}
tq = d.find_last_index with (item==4); {5}

18

9
1/25/16

Array Methods
Array Sorting and Ordering
u sort, rsort, reverse, shuffle

int d[] = {9,1,8,3,4,4},


d.reverse(); {4,4,3,8,1,9}
d.sort(); {1,3,4,4,8,9}
d.rsort(); {9,8,4,4,3,1}
d.shuffle(); {9,4,3,8,1,4}

19

Array Usage Recommendations

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

u Index by number or string

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

uStructures are unpacked by default Structure


Layout varies from tool to tool variables struct packed {
variables
uStructures can be declared as packed structures using packed } struct_name
keyword
Packed structure stores all elements as contiguous bits
First member of the structure is the left-most field of the vector struct name

struct packed {
logic valid;
logic [7:0] tag;
logic [31:0] data;
} data_word

valid tag data


40 39 31 0

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;

u Operations on packed structures

Assigning values to a packed structure

data_word = {1,8hff, 1024}

Logical operations that can be performed on vectors can be performed on packed


structures

24

12
1/25/16

Thank You

13

You might also like