使用 $cast 函数和任务在 SV 中进行动态转换

Dynamic casting in SV using $cast function and task

我们如何判断$cast的调用是一个函数还是一个任务。每个人的呼唤有何不同?我理解的一件事是,通过函数调用,我将能够使用 assert()。但除此之外,还有什么告诉我们调用是 $cast 函数还是 $cast 任务?在任何一种情况下,我们都会做类似 $cast(pkt, pkt1);

LRM 给出了 $cast 函数的语法

function int $cast( singular dest_var, singular source_exp );

以及 $cast 任务的

task $cast( singular dest_var, singular source_exp );

并继续解释

Use of $cast as either a task or a function determines how invalid assignments are handled.

When called as a task, $cast attempts to assign the source expression to the destination variable. If the assignment is invalid, a run-time error occurs, and the destination variable is left unchanged.

When called as a function, $cast attempts to assign the source expression to the destination variable and returns 1 if the cast is legal. If the cast fails, the function does not make the assignment and returns 0. When called as a function, no run-time error occurs, and the destination variable is left unchanged.

请说明。

您的评论是正确的:如果 $cast 用作表达式的一部分,则它被视为函数调用。该措辞源自 Verilog 术语,当时函数只能在表达式中使用,永远不能像任务调用那样作为简单语句存在。但是一旦 SystemVerilog 添加了具有 void return 类型的函数,这种措辞就不再适用了。