Menu
Coddy logo textTech

Built In Gate Primitives

Part of the Fundamentals section of Coddy's Verilog journey — lesson 39 of 90.

Verilog has built-in gate primitives that allow you to describe logic circuits using actual gate symbols. This is called structural modeling — you build circuits by connecting gates, just like drawing a schematic.

Gate primitives are predefined keywords that model basic logic gates. Instead of writing an expression like assign out = a & b, you instantiate a gate:

and(out, a, b);   // AND gate with output out, inputs a and b

General Syntax

gate_type (output, input1, input2, ...);
  • First argument is always the output
  • Following arguments are inputs (1 or more, depending on the gate)

Available Gate Primitives

Gate TypeKeywordNumber of Inputs
ANDand2 or more
ORor2 or more
NOTnot1
NANDnand2 or more
NORnor2 or more
XORxor2 or more
XNORxnor2 or more

How Gate Primitives Work

When you write and(out, a, b), Verilog creates an AND gate that continuously drives out with the result of a & b. Whenever a or b changes, out updates immediately — just like a real gate.

Gate Primitives vs Continuous Assignment

Both methods produce the same hardware:

// Gate primitive
and(out, a, b);

// Continuous assignment (same result)
assign out = a & b;

Gate primitives are useful when you want to describe a circuit as a collection of gates (structural style). Continuous assignment is better for behavioral style (expressions).

challenge icon

Challenge

What to do:

  1. Add the correct gate primitive to make this circuit work. The module should output the AND of inputs a and b. The output port is already named c.

Cheat sheet

Gate primitives in Verilog allow structural modeling by instantiating logic gates directly.

Syntax: First argument is always the output, followed by inputs:

gate_type(output, input1, input2, ...);

Available primitives:

GateKeywordInputs
ANDand2+
ORor2+
NOTnot1
NANDnand2+
NORnor2+
XORxor2+
XNORxnor2+

Gate primitives and assign produce equivalent hardware:

and(out, a, b);       // structural (gate primitive)
assign out = a & b;   // behavioral (continuous assignment)

Try it yourself

module gate_challenge (
  input a,
  input b,
  output c
);
  
  // TODO: Add the correct gate primitive
  // The output c should be a AND b

endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals