SystemVerilog $cast: The Best Kept Secret For Enum Users
This topic will explain difference between static and dynamic casting in enum variable. The c is assigned with c1. last() which returns blue with index as 2. So, c will be 2. Learn how to use $cast with simple easy to understand code example and how to call it as a function/task and compile time errors and warnings when cast fails. The purpose of $cast is when you have handle stored in a base class variable, and that handle refers to an extended class object.
Dynamic casting is done using the $cast(destination, source) method; Static cast is a systemverilog feature that allows converting an expression from one data type to another at compile time. Here are some of the properties of static cast in systemverilog:. Use the static cast operator to convert the expression to enum type. Specifically it seems to be able to handle the enum being assigned to the inputs of the standard cell (which. Examples typedef enum {peter, john, mary} students_e; If (!$cast(std, 5+8)) // 13: Invalid cast, results in error. When you need to assign between two variables of different types, and the source might have a value incompatible with the destination, use $cast() to check the value.