4 Bit ALU
Tags: computerscience, computerarchitecture, alu, circuits
Published: 2022-04-28 19:36:38

## Processor and its components
A processor consists of several components including a control unit, an arithmetic logic unit, registers, and some sort of CPU interconnection. All these components help organize and allow data processing by the computer


### What is an ALU?
The arithmetic logic unit is the main functional component of a processor that performs all the data processing. It can perform arithmetic operations as well as logical operations. 
We'll try to build a very basic ALU that can perform data processing on 4 bits of data, using [Logisim](http://www.cburch.com/logisim/).

### Design
We'll design an ALU that can perform four types of operations on two inputs each consisting of 4 bits. We can use a 2-bit op-code to represent the operation we want to perform.
</br>
#### Operations
* 00 - Logical AND
* 01 - Logical OR
* 10 - Addition
* 11 - Subtraction

#### Components
We'll divide our ALU into following smaller blocks.
* AND block
* OR block
* Arithmetic block
* 3-1 Multiplexer

#### Flags
We'll also have two output flags
* Bit overflow - active when addition yields a result that can't be represented by a 4-bit output
* Equal flag - active when two numbers (inputs) are equal

<center> <b> Let's get started! </b> </center>

### Arithmetic Unit
We'll start from very basic circuits and then create an arithmetic unit that can perform addition and subtraction.

**Full Adder**
A full adder can add 2 single-bit numbers along with a carry and generate the sum and a carry out which can be passed to the next adder
![Full Adder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/so9flou7i7n2tjkyx3k9.png)
Chip View

![Full Adder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e63xdm5xk0o3uvr5nhhp.png)
**4 bit adder**
Using full adder we can build a circuit which can add up to four bits

![Four Bit Adder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okk2xt2y30j0j4tkz2ox.png)
Chip View

![Four Bit Adder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2c2g7tyw0ns0bshzt3m.png)
**Arithmetic Unit**
An adder can be used to perform subtraction using the technique called [2's complement](https://en.wikipedia.org/wiki/Two%27s_complement#:~:text=Two's%20complement%20is%20a%20mathematical,number%20is%20signed%20as%20negative.). 

![Arithmetic Unit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bws68efuer7grsv81i7j.png)
Chip View

![Arithmetic Unit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9ji0akn35uu41uq8kwg.png)
### Logical Unit
Logical units are fairly simple and they perform a logical operation on a single bit from each input.
**OR Block**

![OR Block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o10rmnch1ek3skumiacs.png)
Chip View

![OR Block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjisj8z5truhuk4olimv.png)
**AND Block**

![AND Block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/942b8k52km3vq3eo8syq.png)
Chip View

![AND Block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh9txa4d0s1i5wbkk8yx.png)
### Flags
**Bit Overflow Flag**
The flag is only active when the op-code is 10 (ADD) and the output can't be represented by a 4-bit number.

![Bit Overflow Flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pguxffvi254dwm7yhzz6.png)
Chip View

![Bit Overflow Flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a45mb0rkexulex8xzqbg.png)
**Equal Flag**
The flag is only active when the op-code is 11 (SUB) and both inputs are equal

![Equal Flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zni9u8idt8dv49lbdge.png)
Chip View

![Equal Flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjg4gf4tcs4adfmiu0bo.png)
### Multiplexer
We can generate a single output from multiple outputs from our different blocks based on our op-code. We can use a 3-1 [multiplexer](https://en.wikipedia.org/wiki/Multiplexer)
**2-1 MUX**

![2-1 MUX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mf3ffwf9t0tlbwturnyb.png)
Chip View

![2-1 MUX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbcrw61fl2f85mk3hos4.png)
**3-1 MUX**

![3-1 MUX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrpiw8sxnr1anr1myqi0.png)
Chip View

![3-1 MUX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qe8ueavsigfluyidrno1.png)
### ALU
Now, let's plug everything together

![4 Bit ALU](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cley8shak84f3hui0jtn.png)

### Conclusion
Most modern ALUs can perform way many operations on high number of bits. We can upgrade our ALU to perform other operations as well. This is just a very basic representation of how we can create our circuit that may perform very basic operations.