lec5 data types
TRANSCRIPT
-
7/25/2019 Lec5 Data Types
1/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 1
-
7/25/2019 Lec5 Data Types
2/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 2
Topics
Dynamic Arrays
Queues
Associative Arrays
Array Methods
Structures
Enumerated Types
-
7/25/2019 Lec5 Data Types
3/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 3
Dynamic Arrays
Dynamic Arrays
! Array size is provided at run-time and not compile-time
! Can be allocated and resized during simulation
! Declared with empty subscripts [ ]
! new[]operator is used to allocate space, passing the number of entries! Can also pass name of an array to copy values
! Fixed size arrays can be assigned to dynamic arrays as long as they have the same base
type
! $size function returns the size of a fixed-size or dynamic array
What if you dont know thesize of the array beforerun time?
data_typename_of_dynamic_array[];name_of_dynamic_array=new[number of elements]
Syntax for dynamic arrays
-
7/25/2019 Lec5 Data Types
4/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 4
Dynamic Arrays
Dynamic Arrays
intdyn[], d2[];
initialbegindyn=new[5];foreach(dyn[j]) dyn[j]=j;d2=dyn;
d2[0]=5;$display(dyn[0],d2[0]);dyn=new[20](dyn);dyn=new[100];
dyn.delete();end
Isreg [7:0][3:0] b_reg_array []; legal?
Isreg [][3:0] b_reg_array [4];legal?
Using dynamic arrays
-
7/25/2019 Lec5 Data Types
5/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 5
Dynamic Arrays
Dynamic Arrays! Can use several built in routines such as deleteand size
!
You can make assignments between fixed arrays and dynamic arrays as longas they have the same:
! Base type
!And same number of elements
-
7/25/2019 Lec5 Data Types
6/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 6
Queues
Can provide easy sorting and searching
Allocates extra space to quickly add extra elements
Does not need the new[ ] operator
Push and pop operations are efficient
Can add and remove elements from anywhere
Can copy contents of fixed or dynamic arrays to the queue
int j=1b[$]={3,4},q[$]={0,2,5};
initial beingq.insert(1,j);q.insert(3,b);
q.delete(1);
q.push_front(6)j=q.pop_back;q.push_back(8);j=q.pop_front;
end
type name [$];
-
7/25/2019 Lec5 Data Types
7/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 7
Associative Arrays
Use for sparse memories
! Dynamically allocated, non-contiguous elements
!Accessed with integer, or string index, single dimension
! Great for sparse arrays with wide ranging index
type name [*];
All memory
allocated, even
unused elements
Associative arrayStandard array
Unused elements
dont use memory
logic [63:0] assoc[*], idx=1;
repeat(64) beginassoc[idx]=idx;idx=idx
-
7/25/2019 Lec5 Data Types
8/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 8
Associative Arrays
Operations
! foreachis used with associative arrays
! Reading from an element that has not been allocated results in a x for 4-stateand a 0 for 2-state
! first, next, prev, delete, existsfunctions can be used
-
7/25/2019 Lec5 Data Types
9/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 9
Array Methods
Array reduction methods (Can be used on unpacked arrays: fixed,dynamic, queue, associative)
! sum, product, and, orand xor
!
a.sum of a single bit array returns a single bit value unless stored in a widervalue 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=5end
-
7/25/2019 Lec5 Data Types
10/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 10
Array Methods
Array locator methods
! sort, rsort, reverse, find_first, find_first_with_index
int q[$] = {1,3,5,7}, tq[$];
int d[] = {9,1,8,3,4};
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}
-
7/25/2019 Lec5 Data Types
11/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 11
Array Methods
Array locator methods
! find_first, find_first_with_index
int d[] = {9,1,8,3,4,4}, tq[$];
tq = d.find with(item > 3);
tq = d.find_index with (item>99);
tq = d.find_first_index with (item==8);tq = d.find_last_index with (item==4);
{9,8,4,4}
{}
{2}{5}
-
7/25/2019 Lec5 Data Types
12/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 12
Array Methods
Array Sorting and Ordering
! sort, rsort, reverse, shuffle
int d[] = {9,1,8,3,4,4},
d.reverse();
d.sort();d.rsort();
d.shuffle();
{4,4,3,8,1,9}
{1,3,4,4,8,9}
{9,8,4,4,3,1}{}
-
7/25/2019 Lec5 Data Types
13/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 13
Array Usage Recommendations
Fixed-size Arrays:! Size known at compile time
! Contiguous data
! Multi-dimensional arrays
Dynamic Arrays: ! Size unknown at compile time
! Contiguous data
Queues: ! FIFO/Stack
Associative Arrays:
! Sparse data memories! Index by number or string
-
7/25/2019 Lec5 Data Types
14/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 14
Structures
Structures can be created using the structstatement
!A structure is a collection of variables and/or constants that can beaccessed separately or as a whole
!Allows logical groups of signals to be combined together (example:control signals of a bus protocol)
!The entire collection can be referenced using the name of thestructure
struct {int a,b;
logic [7:0] opcode;logic [23:0] address;
bit error;
} Instruction_Word
Instruction_Word.address=24hF00000;
-
7/25/2019 Lec5 Data Types
15/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 15
Structures
Packed structures
! Structures are unpacked by default
!Layout varies from tool to tool
! Structures can be declared as packed structures using packedkeyword
!Packed structure stores all elements as contiguous bits
!First member of the structure is the left-most field of the vector
struct packed {
logic valid;
logic [7:0] tag;logic [31:0] data;
} data_word
valid datatag
40 39 31 0
-
7/25/2019 Lec5 Data Types
16/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 16
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
! Operations on packed structures
!Assigning values to a packed structure
!Logical operations that can be performed on vectors can beperformed on packed structures
data_word.tag = 8hf0;
data_word [39:32]=8hf0;
data_word = {1,8hff, 1024}
-
7/25/2019 Lec5 Data Types
17/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 17
Enumerated Types
Enumerated types! Provide a way to declare an abstract variable that can have a specific
list of valid values.
! Each value is identified by a user-defined name or label
! Make a program more readable
! Can use routines first, last, next, prevetc.
enum{red, green, blue} RGB
-
7/25/2019 Lec5 Data Types
18/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 18
Enumerated Types
Enumerated type values
! By default the labels of an enumare int! The first label in the enum list is represented with a value 0, the second label with 1 and so on
! SystemVerilog allows the value for each label to be explicitly declared
! Label values must be unique
! If each label is unspecified, the value representing each label will be incremented by 1 from theprevious label
enum{red, green, blue} RGB
0 12
enum{ONE =1 , FIVE =5, TEN =10} state;
enum{A=1, B, C, X=24, Y, Z} list1;
enum{A=1, B, C, D=3} list2; error
1 2 3 24 25 26
-
7/25/2019 Lec5 Data Types
19/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 19
Enumerated Types
Base type of enumerated types
! By default the labels of an enumare int
! SystemVerilog allows base type to be explicitly defined
enumbit {TRUE, FALSE} Boolean;
enum logic [1:0]
{WAITE=3b001,LOAD=3b010,READY=3b100} state;
error!
enumlogic [1:0] {WAITE, LOAD, READY} state;
Enum value size! Assigned label values should match the size of base type
! Assigned label values should match the base types
enumlogic [2:0]{WAITE=3b001,
LOAD=3b010,READY=3b100} state;
-
7/25/2019 Lec5 Data Types
20/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 20
Enumerated Types
Enum value size
! Cannot have more labels in the enumerated list than the base type size can represent
! It is legal to assign x or z to a four state type!
The label next to the x or z assigned label should have an explicitly defined value
enumlogic {A=1b0, B, C} list3; error!
enumlogic {ON=1b1, OFF=1bz}out;
enumlogic [1:0] {WAITE,
ERR=2bxx,LOAD,READY} state;
error!
-
7/25/2019 Lec5 Data Types
21/21
2009, Dr. Meeta Yadav, www4.ncsu.edu/~myadav/Research 21
Operators
+ - * /
arithmetic ~
bitwise negation
% modulus division & bitwise and++ -- increment, decrement &~ bitwise nand> >= <
shift right
?:
conditional (ternary)
{} concatenation Assignment:
= += -= *= /= %= = &= |= ^= ~&= ~|= ~^=