Dynamic cast Failure

Hi,
I am seeing Dynamic cast failure in UVM TB. I have a enum of type bit and when i cast it with logic signal in interface dynamic cast fails. If it change it to bit in interface again it works. Any idea why ?

Error-[STASKE_DCF] Dynamic cast failed
ahb_mmonitor.svh, 87
Dynamic cast using ‘$cast’ failed. The source expression is not yielding a
valid value for the destination variable.
// Assignment in Interface
logic [1:0] HTRANS;

// datatype in transaction item
typedef enum bit[1:0] {IDLE, BUSY, NONSEQ, SEQ} transfer_t;
rand transfer_t trans_type;

// In monitor
$cast(xtn.trans_type[0], vif.mmon_cb.HTRANS);

In reply to rag123:

Could you please post some more code.
See my example which is running:

import uvm_pkg::*;
`include "uvm_macros.svh"

 typedef enum bit[1:0] {IDLE, BUSY, NONSEQ, SEQ} transfer_t;
module top;
  transfer_t trans_type[];
  logic [1:0] HTRANS;

class transaction extends uvm_object;
   transfer_t tr_type[];
   `uvm_object_utils(transaction)
   function new (string name = "");
      super.new(name);
      tr_type = new[3];
   endfunction   
endclass

  transaction my_trans;

  initial begin
    my_trans = new("my_trans");
    trans_type = new[3];
    HTRANS = 3;
    $display("HTRANS = %0h", HTRANS);
    $cast (trans_type[0], HTRANS);
    $display("trans_type[0] = %0h", trans_type[0]);
    $cast (my_trans.tr_type[0], HTRANS);
    $display("my_trans.tr_type[0] = %0h", my_trans.tr_type[0]);
  end
endmodule

In reply to rag123:

A bit is a two-state signal, while a logic is a four-state signal. If you attempt to cast a logic vector which contains an ‘x’ or ‘z’ to a two-state signal, you will get a casting error.

The below code will generate a $cast error:


import uvm_pkg::*;
`include "uvm_macros.svh"
 
 typedef enum bit[1:0] {IDLE, BUSY, NONSEQ, SEQ} transfer_t;
module top;
  transfer_t trans_type[];
  logic [1:0] HTRANS;
 
class transaction extends uvm_object;
   transfer_t tr_type[];
   `uvm_object_utils(transaction)
   function new (string name = "");
      super.new(name);
      tr_type = new[3];
   endfunction   
endclass
 
  transaction my_trans;
 
  initial begin
    my_trans = new("my_trans");
    trans_type = new[3];
    HTRANS = 'bx;
    $display("HTRANS = %0h", HTRANS);
    $cast (trans_type[0], HTRANS);
    $display("trans_type[0] = %0h", trans_type[0]);
    $cast (my_trans.tr_type[0], HTRANS);
    $display("my_trans.tr_type[0] = %0h", my_trans.tr_type[0]);
  end
endmodule

In reply to cgales:

Thanks Cgales and Chris !