Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

System Verilog Data-types

Overview
• Two-state: better performance, reduced memory usage
• Queues, dynamic and associative arrays: reduced memory usage,
built-in support for searching and sorting
• Classes and structures: support for abstract data structures
• Unions and packed structures: allow multiple views of the same
data
• Strings: built-in string support
• Enumerated types: code is easier to write and understand

Courtesy: - All Pictures and diagrams taken from Verification guide .com
The Logic Type
• The one thing in Verilog that always leaves new users scratching
their heads is the difference between a reg and a wire.
• A logic signal can be used anywhere a net is used, except that a
logic variable cannot be driven by multiple structural drivers, such
as when you are modeling a bidirectional bus.
2-State Data-types
Behavioral Data Types 
• Time is a 64-bit quantity that can be used in conjunction with the
$time system task to hold simulation time. Time is not supported for
synthesis and hence is used only for simulation purposes.
• Syntax: - time time_variable_name;
• An integer declares one or more variables of type integer. These
variables can hold values ranging from -2^31 to (2^31)-1. 
• Integer Syntax:     integer integer_variable_name;
• A real declaration declares one or more variables of type real. The
real variables are stored as 64-bit quantities, and store the real
values. Real numbers can be specified in either decimal notation (for
example, 14.72) or in scientific notation (for example, 39e8).
• Examples:
 integer a[0:64] ; // an array of 65 integer values.
   real      float_v ; // a variable to store real value.
Behavioral Data types
• Parameters represent constants, hence it is illegal to modify their
value at runtime.
• Parameters can be modified at compilation time to have values that
are different from those specified in the declaration assignment.
• This allows the customization of module instances. A parameter can
be modified with the defparam statement, or in the module instance
statement.
• Example: - parameter size = 16 ;
• Logic is the improved version of reg form Verilog to SystemVerilog,
so it can be driven by continuous assignments, gates, and modules in
addition to being a variable.
Behavioral Data types
Data-types
Arrays
Arrays
• Fixed size array: In fixed size array, array size will be constant
throughout the simulation, Once the array is declared no need to
create it. By default, the array will be initialized with value ‘0’.
• Single dimensional array:
int array1 [6]; //Compact declaration
int array2 [5:0]; // Verbose declaration
• Two-dimensional array:
int arr[2][3]; // This array has total 2*3 = 6 elements.
• Three-dimensional array:
int arr[2][2][2]; // This array has total 2*2*2 = 8 elements.
Two-dimensional array declaration
int array3 [2:0][3:0];
array1 = '{0,1,2,3,4,5};
array2 = '{0,1,2,3,4,5};
array3 = '{ '{0,1,2,3},'{4,5,6,7},'{8,9,10,11}};
Arrays
Packed Arrays
• bit [7:0] temp_var; // packed array of bit types.
• bit temp_var [7:0]; // unpacked array of real types.

• Packed arrays can be of single bit data types (reg, logic, bit),
enumerated types, and recursively packed arrays and packed structures.
• One dimensional packed array is referred to as a vector.
• Scalar: Scalar is 1-bit data object of reg/logic/bit declared without
specifying a range.
• A packed array is a mechanism for subdividing a vector into sub-fields,
which can be conveniently accessed as array elements.
• A packed array is guaranteed to be represented as a contiguous set of
bits.
Unpacked Arrays
• Unpacked arrays can be of any data type.
• Unpacked arrays shall be declared by specifying the element ranges
after the identifier name.
• An unpacked array may or may not be so represented as a
contiguous set of bits.
• bit [7:0] array4[2:0];
Example: Packed and Unpacked Array

Unpacked Array Declaration


Dynamic Array
• data_type array_name [ ];
• Dynamic array methods: -
– new[ ] –> allocates the storage.
– size( ) –> returns the current size of a dynamic array.
– delete( ) –> empties the array, resulting in a zero-sized array.
• d_array1 = new[10]; //dynamic array of 10 elements
• //Change the length of the array after declaration/initialization
• d_array1.delete; //delete array
Dynamic Array
Dynamic Array methods
Associative Array
• Associative array stores entries in a sparse matrix. Associative
arrays allocate the storage only when it is used, unless like in the
dynamic array we need to allocate memory before using it.
• In associative array index expression is not restricted to integral
expressions, but can be of any type.
• An associative array implements a lookup table of the elements of
its declared type. The data type to be used as an index serves as the
lookup key and imposes an ordering.
• When the size of the collection is unknown or the data space is
sparse, an associative array is a better option.
Associative Array Methods
• num() –> returns the number of entries in the associative array.
• delete(index) –> removes the entry at the specified index.ex: -
a_array.delete(index).
• exists(index) –> returns 1 if an element exists at the specified
index else returns 0.
• first(var) –> assigns the value of first index to the variable var.
• last(var) –> assigns the value of last index to the variable var.
• next(var) –> assigns the value of next index to the variable var.
• prev(var) –> assigns the value of previous index to the variable var.
Associative Array
Associative Array
Associative Array
Associative Array
Queues
A queue is a variable-size, ordered collection of homogeneous elements
like a dynamic array, queues can grow and shrink queue supports
adding and removing elements anywhere.

Queues are declared using the same syntax as unpacked arrays, but
specifying $ as the array size. In queue 0 represents the first, and $
representing the last entries.

A queue can be bounded or unbounded. bounded queue – queue with


the number of entries limited or queue size specified unbounded
queue – queue with unlimited entries or queue size not specified
Queues
Queue Methods


size() –> returns the number of items in the queue.


insert() –> inserts the given item at the specified index position

delete() –> deletes the item at the specified index position


push_front() –> inserts the given element at the front of the queue


push_back() –> inserts the given element at the end of the queue


pop_front() –> removes and returns the first element of the queue


pop_back() –> removes and returns the last element of the queue
Unbounded and Bounded Queue
Bounded and Unbounded Queue
Unbounded and Bounded Queue
Queue Methods
Queue Methods
Bounded and Unbounded Queue
• The number of entries of the bounded queue is limited, push_back
to the bounded queue (after the queue full condition) will not impact
any changes to the queue.
• push_front to the bounded queue (after the queue full condition) will
delete the last entry from queue and stores a new entry in the 0th
index of the queue.
Queue Methods
Accessing random element of Queue
Deleting random element of queue with index
Deleting complete queue
String
• A string data type is variable size, it is a dynamically allocated array
of bytes.
Enum Type
• An enumerated type defines a set of named values.
The simplest enumerated type declaration contains a list of constant
names and one or more variables.
• In the following example, colors are defined to be variable of the
unnamed enumerated int type that includes the members red, green,
blue, yellow, white, black.
• enum { red, green, blue, yellow, white, black } Colors;
•      The actual values are defaulted to integers starting at 0 and then
increase. In the above example by default variable will get the
default value of 0,1,2,3,4,5 respectively from red. 
• The values can be set for the names and also values can be set for
some of the names and not set for other names. A name without a
value is automatically assigned an increment of the value of the
previous name.
Enum Type
• In the following example value is set for red = 0, blue = 4, white =
10.
• Example:
enum { red=0, green, blue=4, yellow, white=10, black } Colors;
• green, yellow, black automatically assigned to the increment-value
of 1,5,11 respectively.
• In the below example yellow will get the increment-value of 5, the
value of white is set with 5. this will cause the syntax error.
• Example:
 enum { red=0, green=0, blue=4, yellow, white=5, black=6 } Colors
.
• A type name can be given so that the same type can be used in many
places.
 typedef enum {GOOD, BAD} pkt_type;
pkt_type  pkt_a; // named type.
Enum Methods
•  first()     — >   returns the value of the first member of the
enumeration.
•  last()     — >   returns the value of the last member of the
enumeration.
•  next()    — >   returns the value of next member of the enumeration.
          next(N)  — >   returns the value of next Nth member of the
enumeration.
•           prev()    — >   returns the value of previous member of the
enumeration.
•           prev(N)  — >  returns the value of previous Nth member of
the enumeration.
•           num()    — >   returns the number of elements in the given
enumeration.
•           name()  — >   returns the string representation of the given
enumeration value.
Example: Enum declaration error

The enum label ‘white’ has the value 5 which is duplicate


of enum label ‘yellow’ in the declared enum.
Example: Enum

Simulator Output: -
pkt_a is  GOOD packet
pkt_b is  BAD  packet
Simulator Output

first_set first color is red, Value = 0


first_set last color is black, Value = 11
second_set color is black, Value = 11
second_set color is yellow, Value = 5
second_set color is black, Value = 11
Number of members in alphabets is 7
Default First members in alphabets is a and value is 0
Next members in alphabets is b , value is 1
Last members in alphabets is g , value is 6
3rd members from last in alphabets is d , value is 3

You might also like