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 bGeneral 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 Type | Keyword | Number of Inputs |
|---|---|---|
| AND | and | 2 or more |
| OR | or | 2 or more |
| NOT | not | 1 |
| NAND | nand | 2 or more |
| NOR | nor | 2 or more |
| XOR | xor | 2 or more |
| XNOR | xnor | 2 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
What to do:
- Add the correct gate primitive to make this circuit work. The module should output the AND of inputs
aandb. The output port is already namedc.
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:
| Gate | Keyword | Inputs |
|---|---|---|
| AND | and | 2+ |
| OR | or | 2+ |
| NOT | not | 1 |
| NAND | nand | 2+ |
| NOR | nor | 2+ |
| XOR | xor | 2+ |
| XNOR | xnor | 2+ |
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
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic