SystemVerilog Casting

Casting

In Manufacturing, Casting is a process in which liquid metal is converted into the desired object. Similarly, SystemVerilog casting means the conversion of one data type to another datatype.  During value or variable assignment to a variable, it is required to assign value or variable of the same data type. Some situations need assignment of different data type, in such situations, it is necessary to convert data type and assign. Otherwise, the assignment of different data type results in a compilation error. The method of data type conversion is called casting.
In systemVerilog, there are two types of casting,

  • Static casting
  • Dynamic casting

Static casting

  • SystemVerilog static casting is not applicable to OOP
  • Static casting converts one data type to another compatible data types  (example string to int)
  • As the name says ‘Static’, the conversion data type is fixed
  • Static casting will be checked during compilation, so there won’t be run-time checking and error reporting
  • Casting is applicable to value, variable or to an expression
  • A data type can be changed by using a cast ( ‘ ) operation
  • The vale/variable/expression to be cast must be enclosed in parentheses or within concatenation or replication braces

Static casting example

In the below example,
the real type is converted into int type. i.e multiplication of two real numbers results in real value, the result is converted into int and then assigned to a variable of int type.
Note: the casting is applied to expression here.

module casting;
  
  real r_a;
  int  i_a;
  
  initial begin
    
    r_a = (2.1 * 3.2);
    
    //real to integer conversion
    i_a = int'(2.1 * 3.2); //or i_a = int'(r_a);
    
    $display("real value is %f",r_a);
    $display("int  value is %d",i_a);
  end
endmodule

Simulator Output

real value is 6.720000
int  value is           7

Click to execute on

Dynamic casting

  • Dynamic casting is used to, safely cast a super-class pointer (reference) into a subclass pointer (reference) in a class hierarchy
  • Dynamic casting will be checked during run time, an attempt to cast an object to an incompatible object will result in a run-time error
  • Dynamic casting is done using the $cast(destination, source) method
  • With $cast compatibility of the assignment will not be checked during compile time, it will be checked during run-time

Let’s see how we can use the casting,

It is always legal to assign a child class variable to a variable of a class higher in the inheritance tree (parent class).
parent_class = child_class; //allowed

It is never legal to directly assign a super-class (parent class) variable to a variable of one of its subclasses (child class).
 child_class  = parent_class; //not-allowed

However, it is legal to assign a super-class (parent class) handle to a subclass (child class) variable if the super-class (parent class) handle refers to an object of the given subclass(child class).
parent_class = child_class ;
child_class  = parent_class; //allowed because parent_class is pointing to child_class.

Though parent_class is pointing to the child_class, we will get a compilation error saying its not compatible type for the assignment.

This we can over come by make use of $cast method, i.e,

$cast(child_class,parent_class);

Why is it called as dynamic casting?

In the above parent class assignment with child class example. type of parent class is changing dynamically i.e on declaration it is of parent class type, on child class assignment it is of child class type.
Parent class handle during $cast execution is considered for the assignment, so it referred to as dynamic casting.

Dynamic Casting examples

assigning child class handle to parent class handle

class parent_class;
  bit [31:0] addr;
  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;
  function display();
    super.display();
    $display("Data = %0d",data);
  endfunction
endclass

module inheritence;
  initial begin
    parent_class p=new();
    child_class  c=new();
    c.addr = 10;
    c.data = 20;
    p = c;        //assigning child class handle to parent class handle
    c.display();
  end
endmodule

Simulator Output

Addr = 10
Data = 20

Click to execute on

assigning parent class handle to child class handle

This assignment is invalid, it leads to a compilation error.

class parent_class;
  bit [31:0] addr;
  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;

  function display();
    super.display();
    $display("Data = %0d",data);
  endfunction
endclass

module inheritence;
  initial begin
    parent_class p=new();
    child_class  c=new();
    c.addr = 10;
    c.data = 20;
    c = p;        //assigning child class handle to parent class handle
    c.display();
  end
endmodule

Simulator Output

"c = p;"
Expression 'p' on rhs is not a class or a compatible class and hence cannot
be assigned to a class handle on lhs.
Please make sure that the lhs and rhs expressions are compatible.

Click to execute on

assigning parent class handle to child class handle

assigning parent class handle (which is pointing to child class handle) to child class handle, leads to compile error.

class parent_class;
  bit [31:0] addr;

  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;
  function display();
    super.display();
    $display("Data = %0d",data);
  endfunction
endclass

module inheritence;
  initial begin
    parent_class p;
    child_class  c=new();
    child_class  c1;
    c.addr = 10;
    c.data = 20;
    p  = c;        //p is pointing to child class handle c.
    c1 = p;        //type check fails during compile time.
    c1.display();
  end
endmodule

Simulator Output

"c1 = p;"
Expression 'p' on rhs is not a class or a compatible class and hence cannot
be assigned to a class handle on lhs.
Please make sure that the lhs and rhs expressions are compatible.

Click to execute on

Use of $cast or casting

In the above example, assigning parent class handle (which is pointing to child class handle) to child class handle is valid but compilation error is observed.
During the compile time, as the handle of p is of parent class type which leads to compile error.

With the use of $cast(), type check during compile time can be skipped.

class parent_class;
  bit [31:0] addr;

  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;

  function display();
    super.display();
    $display("Data = %0d",data);
  endfunction
endclass

module inheritence;
  initial begin
    parent_class p;
    child_class  c=new();
    child_class  c1;
    c.addr = 10;
    c.data = 20;

    p = c;         //p is pointing to child class handle c.
    $cast(c1,p);   //with the use of $cast, type chek will occur during runtime

    c1.display();
  end
endmodule

Simulator Output

Addr = 10
Data = 20

Click to execute on

❮ Previous Next ❯