Monday, February 27, 2017

What is exact difference between $cast as a task and function?

This terminology originally comes from Verilog where a function call always had a return value and could only be used as part of an expression, and a task call never had a return value and could only be used as a simple statement. Now System Verilog allows functions defined with no return value (a void return type), and allows functions with return values to be called as simple statements, and the return value is just thrown away.

So when we say $cast called as a function, we mean it is called as part of an expression

if ($cast(a_handle, b_handle)) something;

and when $cast is called as a task, it is just a simple statement.

$cast(a_handle, b_handle);

The only difference between the two is when the cast fails. Called as a task, it generates a run-time error, which would be consider a testing error.

When called as a function, it never generates an error, it just returns 1 for success and 0 for unsuccessful. You would use a $cast as a function when you have a class handle and want to know what the type of the class object is. Normally, you have a base class variable and want to try casting it to an extended class variable type.

No comments:

Post a Comment