【SV】$cast的用法

  • Post author:
  • Post category:其他


在systemVerilog中经常见到$cast,让我们了解一下。

含义

父类站的高,子类在底下。

从父类向子类的转换称为

向下类型转换 (即 child_handle = father_handle,直接这样复制是不行的)


反之则称为向上类型转换(即 father_handle = handle_handle)


$cast 则是用于向下类型转换。



在向下类型转换时,只有当父类指针指向的对象和待转换的类型一致时,cast才会成功。

栗子

father  f;

child1 c1,c2;

f = c1;  //向上类型转换,f指向的对象是类型是child1的子类对象

$cast(c2,f);//cast成功,c2和c1是相同类型,f又指向c1.

应用场景

  • 由于多态的存在,父类的句柄可以指向父类,也可以指向子类。

  • 对于

    virtual

    method,由于SV支持的动态绑定,即支持子类method重载;SV根据

    对象的类型,

    而非句柄的类型来决定调用什么method;

  • 对于property和非virtual method,SV是根据handle本身的类型进行判断
class transaction;

  virtual function transaction copy(input transaction to);
    if(to==null)
       copy = new();
    else 
       copy = to;
  
    copy.src = this.src;
    return copy;

  endfunction

endclass


class badtr extends transaction;

  virtual function transaction copy(input transaction to);
    badtr bad;
    if(to==null)
       bad = new();
    else 
       $cast(bad,to);
    super.copy(bad);
  
    bad.bad_csm = this.bad_csm;
    return bad;

  endfunction

endclass



badtr b_tr1 = new();
badtr b_tr2 = new();
badtr b_tr3;

$cast(b_tr3,b_tr1.copy(b_tr2));

枚举类型的缺省类型为双状态int,可以使用简单的赋值表达式把枚举类型变量的值直接赋值给非枚举变量 如int.



但SV不允许在没有进行显示类型转换的情况下把int变量直接赋值给枚举变量。

SV要求显式的类型转换的目的在于让你意识到可能的数据越界情况。

typedef enum bit[1:0] {RED=0,BLUE,GREEN} COLOR_E;
COLOR_E color,c2;
int c;

initial begin
    color = BLUE;   // 赋值一个已知的合法的值
    c = color;      // 将枚举变量赋值给int,此时为 1
    c = c+1;        // int型变量递增
    if(!$cast(color,c))      // 将整型显示转换回枚举类型,如果越界会报错
        $display("cast failed for c=%0d",c);   // c的值此时为2
    $display("Color is %0d/%s",color,color.name());
    c++;                     // c的值为3,对于枚举类型已然越界
    c2 = COLOR_E'(c);        // 不做类型检查,下句c2.name()由于越界而打印不出
    $display("c2 is %0d/$s",c2,c2.name());     // 打印:c2 is 3/
end



版权声明:本文为lbt_dvshare原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。