Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Casting Invalid Values

SystemVerilog Classes & OOP

$cast(myCent, 75) where 75 is not a valid enum value, so the cast fails and the target keeps its old value.

Open in IDE
testbench.sv
typedef enum { PENNY=1, FIVECENTS=5, DIME=10, QUARTER=25, DOLLAR=100 } Cents;

module tb;
	Cents 	myCent;
	
	initial begin
		$cast (myCent, 75);
		$display ("Money=%s", myCent.name());
	end
endmodule




/*
Simulation Log:
---------------

ncsim> run
		$cast (myCent, 75);
		    |
ncsim: *E,INVCST (./testbench.sv,7|6): Invalid cast: the source expression can not be cast to the type of the destination variable.
Money=
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
*/
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.