Building an Arithmetic Logic Unit (ALU) using HDL Part 1

Aditya Sakare
4 min readAug 10, 2020

An Arithmetic Logic Unit (ALU) is a one of the most important block of central processing unit(CPU) and we are going to build a 16 bit ALU from Logic gate using Hardware Description Language (HDL).

Source: DLASKOMUT 867 Youtube Channel

An Arithmetic Logic Unit (ALU) is a one of the most important block of central processing unit(CPU). It is a digital circuit to perform arithmetic and logical operations that processing units receive .

Mathematician John von Neumann proposed the ALU concept in 1945 in a report on the foundations for a new computer called the EDVAC.

The modern computers has more complex and powerful ALU. The goal of this article to understand and implement a basic building blocks of ALU from Universal gates to 16 bit ALU.

The Tutorial/article divided into 2 Parts. This is the first part in this we are going to make building blocks of ALU and in the next part we are connecting these blocks and make a 16 bit ALU.

Before starting to implement following code you need to have basic knowledge of digital electronics.

Download Hardware Simulator Software from here

This article divided into following sections:

  1. Functions of standard ALU
  2. Building blocks of ALU

Functions of Standard ALU.

  1. The primary function of the ALU is to perform computation arithmetic and logical operations on processing data
  2. Arithmetic operations are simple addition, subtraction, multiplication and division and logical operations are comparison between numbers, letters or special characters.
  3. The control unit tells ALU what operations need to be performed on the data.
  4. The result of these operations is sent to the register memory.

Building Blocks of ALU

  1. Components:

From Universal Gate Nand we are going to make boolean gates i.e NOT, AND, OR , NOR.

Above image is showing first part NOT gate using NAND gate and second part is OR gate using NAND gate.

Lets write code for these gates in HDL.

Open notepad and write down following program and save it as name of a chip with .hdl extenstion. i.e CHIP name is Not then save as a Not.hdl

Following is code of NOT gate. Basically CHIP is act as a function here. In the section of CHIP we need to define out inputs and outputs

For input IN variable_name and Output OUT out is constant.

In the section of PART we need to add structure of gate. (a=input_one, b=output_two, out=output).

As you can see in the Gate image NOT gate is made from NAND gate and its inputs are joined which means both gate input terminal have same input therefore, Nand(a=in, b=in, out=out).

To run this code open Hardware Simulator software. If you are a Windows user run .bat file and for MAC/LInux user run .sh file. Load hdl file from Load and Run the code.

This is the look of software. Check output on View window.

https://www.nand2tetris.org/software

Above code is for OR gate. we connected 2 ouput for NOT gate to the NAND gate. Likewise we can make hdl files for other gates. Complete code given in the Github.

This the image of Not16 which is 16-bit bus meaning that it is essentially 16 wires: a[0], a[1], … a[15].

This the code for Not16 gate. Note that n2t HDL doesn't support loops.

Likewise we can make for Half adder

Likewise for Full Adder we add 2 Half Adder and for Carry Or gate at the end.

This is diagram of 16 bit Full Adder.

Now Multiplexer

Likewise 16 bit Full Adder we implement a 16 bit Mux.

We completed building blocks for ALU. In the next article we will be joining these gates and build a 16 bit ALU.

Thanks for reading :)

--

--