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

Type Checking

SystemVerilog Data Types

Enum strict typing: a direct integer assignment is illegal, an explicit e_light'(1) cast is legal, and the enum auto-casts to int inside comparisons.

Open in IDE
testbench.sv
typedef enum bit [1:0] {RED, YELLOW, GREEN} e_light;

module tb;
  e_light light;
 
  initial begin
  	light = GREEN;        
  	$display ("light = %s", light.name());
  
  	// Invalid because of strict typing rules
  	light = 0;               
  	$display ("light = %s", light.name());
  
  	// OK when explicitly cast
    light = e_light'(1);      
  	$display ("light = %s", light.name());
 
  	// OK. light is auto-cast to integer
    if (light == RED | light == 2)
    	$display ("light is now %s", light.name());
    
  end    
endmodule

/*
Simulation Log:
---------------
ncsim> run
light = GREEN
light = RED
light = YELLOW
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.