quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif $cast : Dynamic Type Casting

Dynamic type casting allows one data type to be assigned another data type. $cast is the system method which can be used for dynamic type casting. $cast can be used as task or as function.

   

space.gif

Syntax

   

space.gif

task $cast( singular dest_handle, singular source_handle );
or
function int $cast( singular dest_handle, singular source_handle );
   

space.gif

Here

   

space.gif

  • sourc_exp Source expressio.
  • dest_var Destination variable.
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Example : Cast
   

space.gif


  1 module cast();
  2 
  3 typedef enum {IDLE,SFD,PREAMBLE,
  4               DATA,FCS,EFD} eth_state;
  5 
  6 eth_state fsm;
  7 
  8 initial begin
  9   #5  fsm = IDLE;
 10  $display("@%0dns Current value of FSM : %s\n",
 11     $time,get_name(fsm));
 12   #5  fsm = SFD;
 13  $display("@%0dns Current value of FSM : %s\n",
 14     $time,get_name(fsm));
 15   #1  $cast(fsm,1+2);
 16  $display("@%0dns Current value of FSM : %s\n",
 17     $time,get_name(fsm));
 18  // Below line should give compile error
 19  //fsm = 2 + 1;
 20 end
 21 
 22 eth_state temp;
 23 eth_state temp2;
 24 integer i ;
 25 
 26 initial begin
 27    #20 ;
 28   for(i = 0; i <= 5; i++) begin
 29     $cast(temp,i);
 30     $display("Value of temp is %s",
 31          get_name(temp.next()));
 32   end
 33 end
 34 
 35 function string get_name;
 36   input eth_state lstate;
 37   case(lstate)
 38     IDLE     : get_name = "IDLE";
 39     SFD      : get_name = "SFD";
 40     PREAMBLE : get_name = "PREAMBLE";
 41     DATA     : get_name = "DATA";
 42     FCS      : get_name = "FCS";
 43     EFD      : get_name = "EFD";
 44   endcase
 45 endfunction
 46 
 47 endmodule
You could download file cast.sv here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation Output - Cast
   

space.gif

 @5ns Current value of FSM : IDLE
 
 @10ns Current value of FSM : SFD
 
 @11ns Current value of FSM : DATA
 
 Value of temp is SFD
 Value of temp is PREAMBLE
 Value of temp is DATA
 Value of temp is FCS
 Value of temp is EFD
 Value of temp is IDLE
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com