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

Priorty Encoder with Gates

Verilog Gate Level Simulation

Example of a priority encoder implemented using simple gates

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

  wire c_n;        // ~c
  wire b_and_cn;   // b & ~c
  wire d_or_c;     // d | c

  // x = d | c
  or  (x, d, c);

  // y = d | (b & ~c)
  not (c_n, c);
  and (b_and_cn, b, c_n);
  or  (y, d, b_and_cn);

endmodule
testbench.v
module tb;
  reg a, b, c, d;
  wire x, y;
  integer i;
  
  enc_4x2 u0 ( .a(a), .b(b), .c(c), .d(d), .x(x), .y(y));
  
  initial begin
    {a, b, c, d} <= 0;
    
    $monitor("T=%0t a=%0b b=%0b c=%0b d=%0b x=%0b y=%0b", 
             $time, a, b, c, d, x, y);
    
    for (i = 0; i <= 16; i = i+1) begin
      #1 {a, b, c, d} <= i;
    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.