struct data type :
Struct data type is defined as collection of data of different data types . we can declare more than one variable with different data type of each variable. “struct” is a keyword for declaring the data type structure . The variables with different data types in the structure can be accessed through a handle/object .
–> we will declare an object (or) handle for the structure and thorugh the handle we will access the variables using ” . (dot) ” operator.
–> Struct is a subset to a class , but the difference between struct and class is “struct is static one where as class is a dynamic one” .
–> A structure can be assigned as a whole, and passed to or from a function or task as a whole.
–>Each member of a struct has its own memory space. The total size of the struct is the sum of the sizes of its members.
Syntax :
struct {
// variable declarations
} handle_name;
Example :
struct {
bit [3:0]addr;
byte data;
int sum;
time crt_time ;
} packet ;
In the above example a structure is defined with different variables and each variable is of different data type. Size of struct is sum of all sizes of variables (4(addr)+8(data)+32(sum)+64(crt_time) = 108 bits).
typedef :
typedef is a user-defined data type in System Verilog. System Verilog provides a facility to user for creating user defined data type.
Syntax : typedef data_type type_identifier;
typedef : keyword for declaring user defined data type .
data_type : It is a data type which user want to define .
type_identifier : it is an identifier for the data type of user.
Example :
typedef int sum;
sum total; //total is a int type varibale as sum is type_identifier of int type.
In the above example , we declared a type_identifier “sum” which is of int data type . If we want to declare more variables as “int” data type ,we can use type_identifier “sum” instead of “int” . we can declare any variable using “sum” as int type.
enum data type :
enum data type is defined as list of values provided for a single variable . Based on the requirement we can assign any value from the list of values to the variable at any point of time.
–> If no data type is declared by default it will take int data type.
–> list of values are called as enumerated values and the values associated to enumerated values are called numerical values.
Syntax : enum {list of values } variable_name ;
Example-1 : enum { 10,20,30,40} emp_id;
emp_id = 40;
emp_id = 50;
Example-2 : enum { white ,black ,red } colour;
colour = red ;
–> Each value in the list of values will have numerical values . In the above example -1 ,the numerical value of first value – 10 is ” 0 ” . for second value- 20 numerical value will be “1” and so on…..
In the second example the numerical value of “white” is 0 and numerical value of red is 2.
enum values in numerical epressions :
–> The enum values can be used in numerical expressions for performing some operations like addition , subtraction, multiplication etc….
–> During the execution of operations the numerical value associated to enumerated value will be used for calculation.
Example :
typedef enum { red, green, blue, yellow, white, black } Colors;
Colors col;
integer a, b;
a = blue * 3; // here numerical value of blue is 2 and hence a will get value 6.
col = yellow;
b = col + green; // b will get addition of numerical values of yellow(3) &green(1)
// b =3+1 =4 .
methods of enum data type :
System Verilog provide some inbuilt methods for enum data type which are as follows :
1.first ( ) :
–> first( ) method will return the enumerated value of the first element in the list of elements.
Syntax : handle.first( );
Example :
typedef enum {black,white,green}colour;
colour clr;
clr=colour.first( ); // clr will get black
2.last ( ) :
–> last( ) method will return the enumerated value of the last element in the list of elements.
Syntax : handle.last( );
Example :
typedef enum {black,white,green}colour;
colour clr;
clr =colour.last( ); // clr will get green
3.next ( ) :
–> next( ) method will return the enumerated value of the nth element in the list of elements.
Syntax : handle.next( );
Example :
enum {black,white,green}colour;
colour clr;
clr=white;
clr =colour.next( ); // clr will get green
4.prev ( ) :
–> prev( ) method will return the enumerated value of the (n-1)th element for the element which we are checking.
Syntax : handle_name.prev( );
Example :
enum {black,white,green}colour;
colour clr;
clr=white;
clr =colour.prev( ); // clr will get black
5.num ( ) :
–> num( ) method will return the total number of elements in the list.
Syntax : handle_name.num( );
Example :
enum {black,white,green}colour;
colour clr;
int no;
no =colour.num( ); // no will get value as 3