Projects/Verilog: Basic Adder
8.Verilog: Basic Adder
4/10/2024
Verilog, as I'd like to eventually work on making CPUs, digital systems, circuits, and the such, it is a necessary tool to have to reach my goal. And so we'll be working with it here!
But what is Verilog?
Verilog is one of the most popular Hardware Description Languages (HDLs) used for designing and modeling digital systems. It allows engineers to describe complex digital logic circuits
and simulate their behavior before physical implementation. And so it allows for greater efficient use of materials, greater accessiblity to those learning, and lesser waste on prototypes and tries that might turn sour!
Verilog can simulate anything from high-level algorithms to low-level gate and switch-level modeling. It was developed in 1984 by Gateway Design Automation and later became an IEEE standard (IEEE 1364).
Features of Verilog
Modules
In Verilog, the fundamental building block is the module. A module can represent anything from a simple logic gate to an entire system. Here's an example of a simple module:
module and_gate (
input wire a, // Input a
input wire b, // Input b
output wire y // Output y
);
assign y = a & b; // AND operation
endmodule
In this example, and_gate is a module with two inputs (a and b) and one output (y). The assign statement describes the logic operation.
operators
Verilog supports a wide range of operators for arithmetic, logical, relational, and bitwise operations. For example:
- Arithmetic: +, -, *, /
- Logical: &&, ||, !
- Relational: ==, !=, <, >
- Bitwise: &, |, ^, ~
Writing a Basic Adder in Verilog
Now, let's dive into writing a basic adder in Verilog. We'll create a simple 1-bit full adder, which is a fundamental digital circuit that adds two binary numbers along with a carry input and
produces a sum and a carry output.
The logic for a full adder can be described as follows:
- The sum output is obtained by XORing the three input bits.
- The carry output is obtained by ORing the ANDs of each pair of input bits.
module full_adder(
input wire a, // First input bit
input wire b, // Second input bit
input wire cin, // Carry input
output wire sum, // Sum output
output wire cout // Carry output
);
// Sum is the XOR of the three input bits
assign sum = a ^ b ^ cin;
// Carry out is obtained by ORing the ANDs of the input pairs
assign cout = (a & b) | (b & cin) | (cin & a);
endmodule
Explanation of the Full Adder Code
Module Declaration:
- module full_adder(...) declares the module named full_adder.
- The inputs (a, b, and cin) and outputs (sum and cout) are defined as wires using the input wire and output wire keywords.
Sum Calculation:
- assign sum = a ^ b ^ cin; uses the XOR (^) operator to calculate the sum bit. The XOR of the three input bits gives the correct sum bit for binary addition.
Carry Calculation:
- assign cout = (a & b) | (b & cin) | (cin & a); calculates the carry out bit. The carry out is 1 if at least two of the input bits are 1, which is achieved using the AND (&) and OR (|) operators.
This website was made by me.
This page was made with only HTML & CSS. No JS