Verilog HDL Code for All Logic Gates
Advertisement
This page provides Verilog HDL code for implementing all common logic gates. Below, you’ll find the Verilog code along with a reference to a truth table image for clarity.

Image alt: all gates truth table with symbol
Verilog Code
Here’s the Verilog code for a module that implements AND, OR, NOT, NAND, NOR, and XOR gates:
module allgate ( a, b, y );
input a, b;
output [1:6] y;
assign y[1] = a & b; // AND
assign y[2] = a | b; // OR
assign y[3] = ~a; // NOT
assign y[4] = ~(a & b); // NAND
assign y[5] = ~(a | b); // NOR
assign y[6] = a ^ b; // XOR
endmodule
Simulation and Verification
To verify the functionality of the code, follow these steps:
- After the program is synthesized, create a Test Bench (TBW) file to apply inputs to the module.
- Highlight the TBW file.
- Use Modelsim to simulate the behavioral model. This step allows you to observe the outputs for given inputs.
- Examine the waveform to view the simulation results. Zoom in on the waveform for a detailed view of the logic gate outputs.

Explore Verilog Logic Gates, Mux & Encoders
- Verilog HDL Code for All Logic Gates
- 2-to-4 Decoder (Verilog HDL Code)
- 4-to-1 Multiplexer and 1-to-4 Demultiplexer (Verilog Code)
- Verilog Code: 1-to-4 Demultiplexer
- 8-to-1 Multiplexer (Verilog HDL Code)
- 8-to-3 Encoder Without Priority (Verilog)
- 8-to-3 Priority Encoder (Verilog)
- 8-to-3 Priority Encoder (Verilog Code)
- 1-Bit and 4-Bit Comparator Design (Verilog)
Explore Verilog Flip-Flops, Counters & Registers
- T, D, SR, JK Flip Flop (Verilog HDL Code)
- D Flip Flop Synchronous Reset (Verilog)
- D Flip Flop Without Reset (Verilog)
- Verilog Code for Binary Up-Down Counter
- Verilog HDL: BCD & Gray Counters
- 4-bit Down Counter (Verilog Code Test Bench)
- 4-bit Binary Asynchronous Reset Counter (Verilog)
- 4-bit Binary Synchronous Reset Counter (Verilog)
- 4-bit BCD Asynchronous Reset Counter (Verilog)
- 4-bit BCD Synchronous Reset Counter (Verilog Code)
- 4-bit Mod-13 Counter (Verilog Code Test Bench)
- Shift Left / Shift Right Register (Verilog)
- Parallel Load Shift Left Register (Verilog Code)
- PRBS Generator (Verilog Code Test Bench)
Explore Verilog Math, Processing & Memory
- Full Adder (Verilog HDL Code)
- Verilog Code: Half Adder, Half Subtractor, Full Subtractor
- 32-Bit ALU (Verilog Code)
- RAM / ROM (Verilog Code)
- Asynchronous FIFO (Verilog Code Test Bench)
- Asynchronous FIFO Design (Verilog Code and Explanation)
- Mealy / Moore Machine (Verilog)
- Low Pass FIR Filter (Verilog Code)
- 4-bit Binary to Gray Counter Converter in Verilog
