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

Gate Level Examples: Example #1: 2x1 Multiplexer

Verilog Examples

Runnable example from the Verilog Gate Level Examples tutorial (Example #1: 2x1 Multiplexer)

Open in IDE
design.v
module mux_2x1 ( input a, b, sel,
                 output out);
  wire sel_n;     // Inverted select
  wire out_0;     // a path, active when sel = 1
  wire out_1;     // b path, active when sel = 0

  not (sel_n, sel);

  and (out_0, a, sel);
  and (out_1, b, sel_n);

  or (out, out_0, out_1);
endmodule
testbench.v
module tb;
  reg a, b, sel;
  wire out;
  integer i;

  mux_2x1 u0 ( .a(a), .b(b), .sel(sel), .out(out));

  initial begin
    {a, b, sel} <= 0;

    $monitor ("T=%0t a=%0b b=%0b sel=%0b out=%0b", $time, a, b, sel, out);

    for (i = 0; i < 10; i = i+1) begin
      #1  a <= $random;
          b <= $random;
          sel <= $random;
    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.