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

Case Statement

Verilog Conditional Statements

Case Statement — a ready-to-run Verilog example.

Open in IDE
design.v
module my_mux (input  		[2:0] 	a, b, c,
               input		[1:0]	sel,
               output reg	[2:0] 	out);
  
  always @ (a, b, c, sel) begin
    case(sel)
      2'b00 	:	out = a;
      2'b01 	:	out = b;
      2'b10 	: 	out = c;
      default 	: 	out = 0;
    endcase
  end
endmodule
testbench.v
module tb;
  reg  [2:0] a, b, c;
  reg  [1:0] sel;
  wire [2:0] out;
  
  my_mux 	m0 ( .a(a),
                .b(b),
                .c(c),
                .sel(sel),
                .out(out));
  initial begin
    $monitor ("[%0t] a=0x%0h b=0x%0h c=0x%0h sel=0b%b out=0x%0h", $time, a, b, c, sel, out);
    for (int i = 0; i < 10; i = i+1) begin
      a <= $random;
      b <= $random;
      c <= $random;
      sel <= $random;
      #10;
    end
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.