SV - Casting


What is Type-Casting ?.

Type casting is a way of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.

To cast variable of one data type to a variable of another datatype, the two must be compatible.


Importance of Casting

In System Verilog, a data type is essential to mention while declaring a variable and it can hold values of the same data type.

For example, the int data type can not hold real data type values. If we try doing so, it will lead to a compilation error. Casting helps to resolve this problem.

There are two types of casting in System Verilog :

  • Static Casting.
  • Dynamic Dynamic.
Static Casting has already been discussed as part of System-Verilog Basics and Constructs here : Static Casting


Dynamic Casting using $cast

It is always legal to assign a subclass variable to a variable of a class higher in the inheritance tree. For example, refer below code :

                          

It is never legal to directly assign a superclass variable to a variable of one of its subclasses. For example refer below example code :

                          

The above code will return an error at the time of compilation.

                          

However, it is legal to assign a superclass handle to a subclass variable if the superclass handle refers to an object of the given subclass.

To check whether for the above condition, the assignment is legal or not, the Dynamic Cast Function $cast is used.

The $cast can be called as either a task or a function.

Refer below synatx for using dynamic casting using $cast :

                          
  • Use of $cast as either a task or a function determines how invalid assignments are handled.
  • When called as a task, $cast attempts to assign the source expression to the destination variable. If the assignment is invalid, a run-time error occurs, and the destination variable is left unchanged.
  • When called as a function, $cast attempts to assign the source expression to the destination variable and returns 1 if the cast is legal. If the cast fails, the function does not make the assignment and returns 0. When called as a function, no run-time error occurs, and the destination variable is left unchanged.
NOTE : It is important to note that $cast performs a run-time check. No type checking is done by the compiler, except to check that the destination variable and source expression are singulars.

Also, Assignment of Extended class object to Base class object is allowed. It is Illegal to assign Base class object to Extended class.

Using $cast as a task
                          

Code Output :

As can be seen in the output log below, the second cast attemp to cast source 'bc2' to 'ec2' has resulted in a dynamic casting error.

                          
Using $cast as a function

Usage of $cast as a task limits further flexibility in running code, since if the castinf fails the simulation is terminated with an Error. This however can be mitigated by using $cast as a function and use the return value of $cast to take decision further. Refer below, the same code above, which has been modified to used $cast as function instead of task

                          

Code Output :

                          

Refer another example below for a better understanding dynamic casting :

The following class hierarchy is implemented in the example.

Could not load fig. Plz refresh
Implemented Class Hierarchy
                          

Code Output :