The document explains the casting process in SystemVerilog, which converts data types for compatibility, highlighting two types: static and dynamic casting. Static casting is checked at compile time and is used for fixed data types, while dynamic casting is checked at runtime and allows for more flexible assignments using the $cast method. Examples of both casting types are provided, demonstrating their syntax and functionality in various scenarios.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Casting 1
The document explains the casting process in SystemVerilog, which converts data types for compatibility, highlighting two types: static and dynamic casting. Static casting is checked at compile time and is used for fixed data types, while dynamic casting is checked at runtime and allows for more flexible assignments using the $cast method. Examples of both casting types are provided, demonstrating their syntax and functionality in various scenarios.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
Casting
• Casting is a process of converting from one data type into
another data type for compatibility. • In SystemVerilog, 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 cannot 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 SystemVerilog 1.Static Casting 2.Dynamic Casting Static Casting
• As the name suggests, static casting is only applicable
to fixed data types. It does not apply to the Object- Oriented programming concept. Syntax -: <data_type>'(value or variable or expression) • It converts values or variables from one data type to another data type. • Static casting is checked during compile time. So, there will not be any run time error. • Ex-: real to int, int to real, a string to int, expression in casting, concatenation in casting is used. Dynamic Casting
• Dynamic casting is used to cast the assigned values to the
variables that might not be ordinarily valid. The $cast is the system method. • The $cast can be either function or task • Syntax -: function int $cast(destination, source); task $cast(destination, source); • In both $cast as a function or task, $cast will try to assign source value or expression to the destination variable. If dynamic casting fails due to incompatible assignments, the destination variable will remain unchanged. The only difference between $cast as a function and as a task is mentioned. • $cast as a task: If $cast fails, it will cause a runtime error. • $cast as a function: As $cast as a function returns 1 for legal casting, otherwise it returns 0. But run time error will not occur for failed casting and the destination variable will be unchanged. • Will see example • 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 • parent_class = child_class; //allowed • child_class = parent_class; //not-allowed module dynamic_cast_example; typedef enum {DIODE=10, BJT=100, MOSFET=250, FINFET=300} devices; initial begin devices dev;
if($cast(dev, 250 + 50))
$display("A: Device used = %s",dev.name()); else $display("A: Dynamic casting failed");
if($cast(dev, 250 + 20))
$display("B: Device used = %s",dev.name()); else $display("B: Dynamic casting failed"); end endmodule Static casting example module casting_example; string name; int num[3]; real r_num; initial begin name = "A"; r_num = 2.8;
num[0] = int'(name); //Takes ascii value for string
$display("casting from string to int: num[0] = %0d", num[0]);
$display("casting from real to int: num[1] = %0d", num[1]); $display("casting from int to real: r_num = %0f", r_num); $display("casting an expression from real to int: num[2] = %0d", num[2]); end endmodule Int to string and string to int module test_vars(); int a,b; int myInt; string String1, OtherString2, abc; initial begin // int myInt = 32'h1200d2d5; myInt= 96; a = 10; abc ="BMW"; $display("myInt = %h", myInt); // first cast $sformat(String1, myInt); // second cast Continue ……….. OtherString2 = string'(myInt); $display("String1 = %h, OtherString2 = %h", String1, OtherString2); $display("String2 = %d, OtherString2 = %d", String1, OtherString2); $display("string = %0s, myotheer = %0s",String1,OtherString2); $sformat(String1, a); OtherString2 = string'(a); $display("String1 = %h, myOtherString2 = %h", String1, OtherString2);
b = int'(abc); $disply("-------------"); $display("a = %h, b = %s", OtherString2,b); end endmodule : test_vars • class A; int m=10; int n=20; function new(); m=30; n=40; endfunction function void display(); $display("m=%0d n=%0d",m,n); endfunction endclass class B extends A; int o=50,p=60; function new(); o=70; p=80; endfunction function void display(); $display("o=%0d p=%0d",o,p); endfunction endclass • module tb(); A tr1; B tr2; B tr3; initial begin //tr1=new(); tr2=new(); //tr1.display(); tr2.display(); tr1=tr2; tr1.display(); tr2.display(); /*$cast(tr3,tr1); tr1.display(); tr3.display(); */ /*tr3=tr1; tr1.display(); tr3.display();*/ $cast(tr3,tr2); tr2.display(); tr3.display(); end endmodule • 1) take a spi_transaction class contain properties like clk, cs, and display the properites • 2) create a spi_transaction_extended class by using spi_transaction, it contains properties like 32-bits of miso, mosi. and assign some values. and display • 3) by using parent handle I need to access the child properties, display both parent and child data. • 4) take one more handle for child class c2, then assign child to parent and print the data. • 5) take a string and int, convert string to int and int to strig, and display the string and int variables. • 6) take a local, protected properties and access from outside of the base class and print the values.