[go: up one dir, main page]

0% found this document useful (0 votes)
15 views5 pages

Casting Software

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Casting Software

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CASTING

In systemVerilog, there are two types of casting

1. Static casting

2. Dynamic casting

STATIC CASTING:

1. SystemVerilog static casting is not applicable to OOP

2. Static casting converts one data type to another data types (example string to int)

3. As the name says ‘Static’, the conversion data type is fixed

4. Static casting will be checked during compilation, so there won’t be run-time checking and error reporting

A data type can be changed by using a cast ( ‘ ) operation

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 a;

int b;

initial begin

a = (2.5 * 3.5);

//real to integer conversion

b = int'(2.5 * 3.5);

$display("real value is %f",a);

$display("int value is %d",b);

end

endmodule

Output:

real value is 8.750000

int value is 9
DYNAMIC CASTING:

1. Dynamic casting is used to, safely cast a parent_class handle into a child_class handle in a .class hierarchy

2. 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

3. Dynamic casting is done using the $cast(destination, source) method

4. With $cast compatibility of the assignment will not be checked during compile time, it will be checked during run-time

1. It is always legal to assign a child_class handle to a handle of a .class higher in the inheritance tree (parent_class).

parent_class = child_class; //allowed

2. It is never legal to directly assign a parent_class handle to a handle of one of its child_class

child_class = parent_class; //not_allowed

DYNAMIC CASTING EXAMPLES:

1 ASSIGNING CHILD CLASS HANDLE TO PARENT CLASS HANDLE

class parent_class;

bit [31:0] addr;

function void display();

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

endfunction

endclass

class child_class extends parent_class;

bit [31:0] data;

function void display();

super.display();

$display("Data = %0d",data);

endfunction

endclass

module top;

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

output:

Addr=10

Data=20

2 ASSIGNING PARENT CLASS HANDLE TO CHILD CLASS HANDLE:

class parent_class;

bit [31:0] addr;

function void display();

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

endfunction

endclass

class child_class extends parent_class;

bit [31:0] data;

function void display();

super.display();

$display("Data = %0d",data);

endfunction

endclass

module top;

initial begin

parent_class p;

child_class c=new();

child_class c1;

c.addr = 10;

c.data = 20;

p=c; //assigning parent class handle to child class handle

c1 = p; //type check fails during compile time.

c1.display();
end

endmodule

output:

Error: testbench.sv(24): (vlog-13216) Illegal assignment to type 'class testbench_sv_unit::child_class' from type 'class
testbench_sv_unit::parent_class': Types are not assignment compatible.

End time: 06:35:23 on Nov 27,2023, Elapsed time: 0:00:00

3 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 void display();

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

endfunction

endclass

class child_class extends parent_class;

bit [31:0] data;

function void display();

super.display();

$display("Data = %0d",data);

endfunction

endclass

module top;

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

output:

Addr = 10

Data = 20

You might also like