在systemverilog中

时间:2018-10-09 11:08:44

标签: system-verilog

我正在尝试通过参考https://verificationacademy.com/courses/systemverilog-oop-for-uvm-verification

来研究此案

要理解“ $ cast”,我已经实现了以下内容。

class A;
    int v=1;
endclass

class F extends A;
    int w=2;
endclass

class B extends A;
    int x=3;
endclass

class G extends B;
    int y=4;
endclass

class C extends B;
    int z=5;
endclass



module test;
initial begin
    A a_h = new();
    B b_h;
    G g_h;
    C c_h;
    F f_h;
    $display(a_h.v);
    if ( $cast(c_h, a_h) )
        $display(c_h.z);
    else if ( $cast(g_h, a_h) )
        $display(g_h.y);
end
endmodule

但是我只有

'1'

我认为下面的代码不起作用。

 if ( $cast(c_h, a_h) )
        $display(c_h.z);
    else if ( $cast(g_h, a_h) )
        $display(g_h.y);

请帮助我,它有什么层次名称组件查找失败错误?

更新2

如果我想将G转换为A类,那我该怎么办?

casting picture

像下面这样实现时,我收到错误消息

module test;

B b_h = new();
G g_h = new();
C c_h = new();
F f_h = new();
A a_h = new();

initial begin

$cast(c_h, a_h);
$display(c_h.z);
$display(b_h.x);
$display(a_h.v);

错误消息

    $cast(c_h, a_h);
            |
ncsim: *E,BCLCST (./test1.sv,33|5): Invalid cast: a value with the class datatype '$unit_0x118af7fb::A' cannot be assigned to a class variable with the datatype '$unit_0x118af7fb::C'.
          5
          3
          1

如您所见,我有5、3、1个值以及错误消息。 您能告诉我为什么会收到此错误消息吗?

1 个答案:

答案 0 :(得分:0)

要执行此转换,必须首先分配derived类,然后将其用作base类,然后才能进行转换,如以下示例所示。

class A;
  int a = 1;
endclass
class B extends A;
  int b = 2;
endclass

module C;
  B b_h = new(); // << allocate the derived class
  B bb_h;
  A ba_h;

  initial begin
    ba_h = b_h;  // << assign derived class to the base

    if ($cast(bb_h, ba_h)) // << now you can cast base to another variable of class B (or to one of its bases).
      $display(bb_h.b);
  end
endmodule
相关问题