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

Decoder with Gates

Verilog Gate Level Simulation

Example of a decoder using simple gates

Open in IDE
design.v
module dec_2x4 ( input x, y, en,
                 output a, b, c, d);

  wire x_n, y_n;

  not (x_n, x);
  not (y_n, y);

  and (a,  x,   y,   en);
  and (b,  x,   y_n, en);
  and (c,  x_n, y,   en);
  and (d,  x_n, y_n, en);

endmodule
testbench.v
module tb;
  reg x, y, en;
  wire a, b, c, d;
  integer i;

  dec_2x4 u0 ( .x(x), .y(y), .en(en),
           		.a(a), .b(b), .c(c), .d(d));

  initial begin
    {x, y, en} <= 0;
    $monitor ("T=%0t x=%0b y=%0b en=%0b a=%0b b=%0b c=%0b d=%0b",
              $time, x, y, en, a, b, c, d);

	en <= 1;
    for (i = 0; i < 10; i = i+1) begin
      #1 	x <= $random;
      		y <= $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.