Quantcast
Channel: UVM Forums RSS Feed
Viewing all articles
Browse latest Browse all 756

What's the matter with $cast()?

$
0
0

Hi, all

 

 

     I has an example as follow:

 

 

class BasePacket;
  int A = 1;
  int C = 2;
 
  function void printA;
    $display("BasePacket::A is %d", A);
  endfunction : printA
 
  virtual function void printC;
    $display("BasePacket::C is %d", C);
  endfunction : printC
endclass : BasePacket


class My_Packet extends BasePacket;
  int A = 3;
  int C = 4;
 
  function void printA;
    $display("My_Packet::A is %d", A);
  endfunction: printA
 
  virtual function void printC;
    $display("My_Packet::C is %d", C);
  endfunction : printC
endclass : My_Packet

BasePacket   P1 = new;
My_Packet     P2 = new;

 

 

Case one:

initial begin
//P1 = P2;
P1.printA;
P1.printC;
$cast(P2, P1);
P2.printA;
P2.printC;
end

 

...
# BasePacket::A is           1
# BasePacket::C is           2
# ** Error: (vsim-3971) $cast to type 'class work.obc_pkg_sv_unit::My_Packet' from 'class work.obc_pkg_sv_unit::BasePacket' failed in file ../../sv/top/bip4_vtop.sv at line 80.
#    Time: 0 ps  Iteration: 0  Instance: /bip4_vtop
# My_Packet::A is           3
# My_Packet::C is           4
...

 

 

Case Two:
initial begin
P1 = P2;
P1.printA;
P1.printC;
$cast(P2, P1);
P2.printA;
P2.printC;
end

...
# BasePacket::A is           1
# My_Packet::C is           4
# My_Packet::A is           3
# My_Packet::C is           4

...

 

 

Case one, I didn't assign P1 with P2, the simulator reported error information as above;

the type of P1 is not a superclass of the P2 type?

 

Case twon, I assign P1 with P2 at the begbining, the simulator reported normally as above;

 

Why the assignment of P2 with P1 is cast-compatible after assigned P1 with P2? 

How to do the simulator judge the cast-compatible?

 

So I want to know what the assignment of P1 with P2 does?

 

Thank you in advanced.

 

BR

 

QIN


Viewing all articles
Browse latest Browse all 756

Trending Articles