Teach you to make an ALU yourself

How does a computer perform calculations without gears?

ALU is the component responsible for calculations in the computer. This article teaches you how to make an ALU yourself.

First ALU

In 1970, the first complete ALU packaged in a single chip, the Intel 74181, was born . This was an amazing engineering feat at the time!

arithmetic unit

In binary, 1=true, 0=false

Add two numbers

Adding circuit half adder (cannot handle carry)

Add two bits (bit is 0 or 1).

Two inputs AB, one output is the sum of AB. These three values ​​are all single bits (0 or 1)

0+0=0

Converted to a logic gate, both inputs are false and the output is also false. Same as XOR logic gate

1+0=1,0+1 =1

Converting it to a logic gate means that one input is true, one input is false, and the output is true. This is also consistent with the XOR logic gate.

(Special) 1+1=0 requires carry

The result of 1+1 is 0, and 1 goes to the next bit. You can see that the XOR logic gate does not support this (the input is all true and the output is false, which is only partially correct). It also needs an output line to indicate carry , so we need to modify it slightly.

According to the above situation, a new output line is added to represent the carry number. The output of this carry will only output 1 when 1+1 ( it will be true when all inputs are true). Does this look like an AND logic gate?

Connect the two input lines to the AND logic gate specially used to process carry, so that a one-bit adder (which cannot process the carry input at this time, also called a half adder) is ready

Abstract encapsulation into independent components

Encapsulate it as a separate component.

Two inputs AB, two outputs SUM represents the sum, and CARRY represents the carry ( when calculating the next digit, the carry of the previous digit must be added )

Full adder (adder that can handle carry)

I just mentioned the carry: When calculating the next bit, you need to add the carry of the previous bit. The above only completes the addition of one bit (there is no need to process the addition of the carry input). If there are multiple bits that need to be operated, this is necessary . There is one more input carry. This kind of adder that requires three inputs is called a full adder and is used to calculate carry.

Look at the operation table of the full adder:

There are two inputs AB and one is the carry C of the previous one.

One of the two outputs represents the carry bit for the next adder as the C of the next adder , and SUM represents the calculation result of this bit.

Implementation ideas

Output SUM

Let's first look at how we people do calculations: first add the two inputs AB and then add the sum and carry . The calculation in the first step is the addition of a single bit, and the result and carry addition of the second step are also the addition of a single bit, so we assemble two half adders.

The first half adder is used to calculate the result (first two inputs: AB) ;

The second half adder accepts the result and carry of the previous half adder as input (the SUM and carry of the previous half adder [third input])

Output CARRY

First, let’s look at the two cases that require carry:

1. The two inputs themselves require a carry (when AB is both 1, 1+1 itself requires a carry). Isn't this situation the carry of the first adder~~

2. The calculation result of two AB is 1 and 1 also needs to be carried . Isn't this situation the carry output of the second adder (adding the output of AB and the carry)~~

It can be seen that if one of the two conditions is established, a carry is required. Therefore, an OR gate is used to connect the carries of the two half adders to the CARRY output of the full adder.

explain:

Note that this is still a one-bit adder, but it supports one more carry input than a half adder. This type of adder is called a full adder.

The verification process is like this: AB on the left is the input, and CS on the right is the carry and sum. The input AB of the second adder (actually not the input AB represents the SUM of the first adder and the input C), don’t confuse it.

Readers can input ABC in conjunction with the full adder table above to verify whether the final corresponding SUM and CARRY are correct.

Abstract encapsulation into independent components

One more input carry than a half adder

Make an eight-bit adder

What is mentioned above is a one-bit addition operation. Just combine multiple adders for multiple bits.


Represent numbers:

A, B represent two eight-digit numbers as input, A0, A1, A2... are used to represent the number.


Calculation:

Obviously, A0 and B0 can be operated using half adder since there is no carry .

Then use a full adder to connect the three carry bits of A1B1 and A0B0 as inputs for operation.

The third place, the fourth place and so on...

Look at the picture: The first calculation uses a half adder and then a full adder (note that the input C of the full adder is the carry output of the previous adder )

Calculate continuously, and finally arrange sum0, sum1, and the sum of each bit in order to get the result.

An eight-bit adder means that the computer processes data blocks expressed in 8-bit lengths, that is, an eight-bit computer

overflow

When there is a carry in the ninth position (that is, carry7 is 1), it means that the sum of the two numbers is too large, exceeding 8 digits.

For example, if the third output carry of 999+1 is 1 , that is, the carry of the fourth bit is 1. The result is that 1000 exceeds three digits. This is overflow

Generally speaking, "overflow" means that the sum of two numbers is too large to exceed the number of digits originally represented, which can lead to errors and unpredictable results.

"Pac-Man" overflow incident

The Pac-Man game uses 8 bits to store the current level number. If you play to level 256 (the maximum representation of 8 bits is 256 ), the ALU will overflow [because 9 bits are needed to represent this level number, and the number represented by 8 bits has already been Compared with the above example, it is not enough to add a digit of 1000 to represent], resulting in a series of errors and garbled codes, making the level impossible to proceed.

This bug has become a symbol of powerful Pac-Man players.

avoid overflow

The way to avoid overflow is also very simple. It is the same as the 999+1 example above, which is to add one more bit, which means that a full adder needs to be added to calculate the extra bit (used in 999+1 It is a three-digit addition. If it wants to become a four-digit 1000, it needs to support the fourth-digit addition).

The examples given are in decimal, so readers can understand the meaning.

So the solution is to add more full adders, which can operate on 16 or 32-bit numbers , making overflows more difficult to occur, but at the cost of more logic gates, and each carry operation takes a little time, which is fast but now In this era, there are billions of calculations per second, and no matter how small the delay is, it will be amplified.

carry lookahead adder

Modern computers use a somewhat different adder circuit, using carry-lookahead adders. It does the same thing faster, adding binary numbers.

Other mathematical operations

ALU also supports other mathematical operations, generally the following eight. Like adder, it is also composed of logic gates

multiplication and division

The simple ALU circuit does not have multiplication and division, but uses multiple additions to implement multiplication.

For example, 12X5 is the same as adding 12 five times. It takes five ALU operations to get the result.

It's slow but it works! Haha, this is how simple processors are made, such as TV remote controls, microwave ovens, and thermostats.

But computers and mobile phones have better processors and arithmetic units that specialize in handling such complex operations. Their essence is to assemble more logic gates.

logical unit

The logic unit performs logical operations, such as the previous simple logic of AND, OR, and XOR, but it can also make some complex logical judgments.

The following circuit is used to determine whether the output number is 0. When the number is 0, the output is true1

As long as there is a 1 through the OR gate, the final output is 0false because one of the bits in the number is 1.

Abstract encapsulation into independent components

First, INPUTA and INPUTB are two eight-bit inputs (because we are an eight-bit ALU)

OPERATION CODE: A four-digit operation code is used to indicate what kind of operation the ALU is required to perform . For example, addition ADD is 1000 and subtraction is 1100.

OUTPUT means that the output result is also 8Bit, so how do you know whether it overflows? Don't worry, they will be listed below.


The FLAGS on the right is 1 bit (1Bit), which represents a specific state (the following three states are the most commonly used, and advanced ALUs will have more FLAGS)

  • The OVERFLOW overflow flag represents whether it overflows . This line is connected to the carry bit of the adder
  • ZERO represents whether the result is 0 ( through the above logical unit operation to determine whether the number is 0)
  • NEGATIVE represents a negative sign . If AB is less than 0, it is 1.

Summarize

Okay, after the above explanation, you have made an ALU (arithmetic unit and logic unit) that can handle eight bits . However, the Intel 74181 mentioned at the beginning of the article can only handle a four-bit ALU. You have made an ALU that is better than Intel. Not bad ALU! Although it has not been completely created, you already understand the concepts and principles.

The 74181 had 70 circuits and couldn't handle multiplication or division, but it was a big step toward miniaturization, making computers smaller and cheaper.

The next article will use this ALU to make a CPU, but before that, the computer needs "memory" to record the results of these calculations. It doesn't make much sense if we just throw them away after calculation. We need to store the results. for later use. So our next article explains the memory module


Original video

[Computer Science Crash Course][40 episodes/fine revision] - Crash Course ComputerScience

Youtube original video

Original link: Teach you to make an ALU yourself - Nuggets (juejin.cn)

Guess you like

Origin blog.csdn.net/m0_65909361/article/details/132893490