Categories

# Learn to make a simple CPU in the first semester

A general talk about how to make a processing and memory unit in CPU. Learned @ Imperial.

It’s not a paramount task. Everything is just messy but elegant.

Imperial is teaching contents worth 2 years of studying in the first two semesters, so it doesn’t surprise me when I finally know how a CPU(Central Processing Unit) works. However, to understand the essence of the bigger parts, we have to look at simple gates and design patterns. Down to the earth, the logic gates are sea of math and physics. We are given the building blocks, only puzzled at what to do with them.

To start with, we have several basic units that we can deploy:

The details of those logic gates are in this site. In short, the three basic ones are not, or, and. Other gates are just the derivatives of those three. The buffer returns the input directly as output. It is used to increase propagation delay and amplify signals.

Some interesting things can be built using those basic gates. For instance, we can build a 2-input multiplexer using one not gate with two and gates, one or gate.

When X = 1, the output is B; when X = 0, the output is A. So the multiplier is just a selector for several input signals. Similarly, we can built multiplexers of any size with increasing size of selecting bits. For instance, a 4-input selector needs two selecting bits. In the order of S2S1, 00 for A, 01 for B, 10 for C, and 11 for D.

We can also build a decoder, which might be considered as the reverse of what multiplexer does. Instead of choose one output from several input, a decoder chooses a signal as the input towards some other structure connecting to it.

Those are really useful in terms of controlling signals. We will later see how those things play a critical role in a central processing unit.

Taking things to the next level, we now illustrate how a develop a circuit based on truth table.

First, let’s say we are developing a full adder. By the addition rule, 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 0. Because we need to take the carry-in from the last digit as well, so there is actually three inputs: A, B, and Cin. The truth table is shown below:

Next, we draw Karnaugh map for both Sum and Cout. (a) is the k-map for Sum and (b) is the k-map for Cout.

We then generate the minterm or maxterm formula. Minterm formula is in disjunctive normal form, while maxterm formula is in conjunctive normal form. From the K-map above, we can generate the minterm equations:

SUM = (A XOR B) XOR Cin = (A ⊕ B) ⊕ Cin
Cout = A AND B OR Cin(A XOR B) = A • B + Cin • (A ⊕ B)

In the similar fashion, we can design subtract or, multiplier and division hardwares. Those can be used in basic operations in a central processing unit. If you want to know more about how to use truth table and K-map to develop a circuit, go visit this site.

Here, the multiplexer can be of great use. For instance, you can choose from different operations using a 4-input multiplexer. There are two different sorts of operations: and, or, not, etc are called logic operations, whereas addition, subtraction and multiplication are called arithmetic operations. In a professor, those two sorts are implemented in a single unit called Arithmetic Logic Unit(ALU).

Other parts of a CPU can be built in a similar fashion, only differs in details and optimizations.

There is another kind of circuit that is dedicated to build memories: flipflops.