Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Combinational Logic

Gate-Level Primitives in Verilog

Use Verilog's built-in gate primitives and, or, nand, nor, xor, xnor, not, buf and tri-state buffers: syntax, multiple inputs, instance arrays, delays and when to prefer assign.

Verilog has a set of built-in gate primitives: instantiable gates that need no module definition. They let you write a circuit structurally, almost as you would draw it. Modern RTL rarely uses them for ordinary logic (an assign with operators is shorter), but you will meet them in netlists produced by synthesis tools, in educational designs, in standard-cell libraries and when you must force a specific structure.

The primitives

Primitive Function Inputs
and, nand AND, NOT-AND 2 or more
or, nor OR, NOT-OR 2 or more
xor, xnor XOR, XNOR 2 or more
buf Buffer (output copies input) 1 input, 1 or more outputs
not Inverter 1 input, 1 or more outputs
bufif1, bufif0 Tri-state buffer, enable high / low data, enable
notif1, notif0 Tri-state inverter data, enable

Syntax

For multi-input gates, the output comes first, then the inputs:

VERILOG
and  g1 (y, a, b);          // y = a & b
or   g2 (y, a, b, c);       // 3-input OR
nand g3 (y, a, b);
xor  g4 (y, a, b);
not  g5 (y, a);             // y = ~a
buf  g6 (y, a);

The instance name (g1) is optional for primitives. The gate output must be a net (wire), and it is always a single bit. Positional connections only: primitives have no named ports.

Example: a 2:1 multiplexer from gates

VERILOG
module mux2_gates (
    input  wire a, b, sel,
    output wire y
);
    wire nsel, t0, t1;
    not  (nsel, sel);
    and  (t0, a, nsel);
    and  (t1, b, sel);
    or   (y, t0, t1);
endmodule

This is the textbook structure: y = (a & ~sel) | (b & sel). Equivalent behavior in one line: assign y = sel ? b : a;.

flowchart LR
  sel --> N["not"] --> A0["and"]
  a --> A0
  sel --> A1["and"]
  b --> A1
  A0 --> O["or"]
  A1 --> O
  O --> y

The multiplexer built from one inverter, two ANDs and an OR.

Tri-state buffers

bufif1 (out, in, enable); drives out with in when enable is 1, and releases it (z) otherwise. bufif0 enables when the control is 0.

VERILOG
wire bus;
bufif1 (bus, data_a, en_a);
bufif1 (bus, data_b, en_b);   // both can share bus if never enabled together

Gate arrays and delays

You can instantiate several copies at once with a range:

VERILOG
wire [7:0] a, b, y;
and g [7:0] (y, a, b);     // eight AND gates, bitwise

Gates also accept delays in simulation, for rise and fall times:

VERILOG
and #(2, 3) g (y, a, b);   // rise 2, fall 3 time units
nand #4 n1 (y, a, b);      // same delay both ways

Synthesis ignores these delays.

Truth of x and z through gates

Gate primitives follow four-state rules: and with a 0 input outputs 0 even if another input is x; or with a 1 input outputs 1. An undriven input (z) is treated like x at the gate input.

When to use primitives

  • Prefer assign and operators for readable RTL: assign y = ~(a & b); is clearer than nand (y, a, b);.
  • Use primitives when you want an exact gate-level structure (teaching, reproducing a schematic), or when reading gate-level netlists.
  • Vendor and ASIC libraries provide their own cells as modules (for example a NAND2 cell); synthesis maps your logic onto them. You normally never instantiate those by hand in RTL.

Building universal gates

NAND is functionally complete: every logic function can be built from NAND gates alone.

VERILOG
module not_from_nand (input wire a, output wire y);
    nand (y, a, a);               // NOT: ~(a & a) = ~a
endmodule

module and_from_nand (input wire a, b, output wire y);
    wire t;
    nand (t, a, b);
    nand (y, t, t);               // inverts the NAND: gives AND
endmodule

module or_from_nand (input wire a, b, output wire y);
    wire na, nb;
    nand (na, a, a);
    nand (nb, b, b);
    nand (y, na, nb);             // De Morgan: a | b = ~(~a & ~b)
endmodule

Stand-in: NAND is universal (C)

This stand-in (not Verilog) implements everything from a single nand function and verifies each result against C's own operators for all inputs.

C Stand-in: building every gate from NAND (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The XOR needs four NAND gates; the sharing of t keeps it that small. Cell libraries often count area in NAND2 equivalents for this reason.

Stand-in: a 2:1 mux from gates (C)

C Stand-in: gate-level multiplexer (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In and g1 (y, a, b); which port is the output?

    For multi-input primitives the output is listed first, then the inputs.
  2. 2. What does bufif1 (bus, d, en); do when en is 0?

    When the enable is inactive, the tri-state buffer outputs z.
  3. 3. Which is usually preferred in modern RTL for a simple AND?

    Operators with assign are shorter and more readable; primitives are for structure and netlists.
  4. 4. Which single gate type can build every other gate?

    NAND (like NOR) is functionally complete.

Summary

  • Primitives (and, or, nand, nor, xor, xnor, not, buf, bufif) are built-in gates; the output is listed first.
  • They use positional connections, single-bit outputs and optional instance names; ranges make gate arrays.
  • Delays on primitives are for simulation only.
  • Prefer assign in RTL; expect primitives in netlists and teaching examples.
  • NAND (or NOR) alone can build any logic function.

Next: always_comb, if and case