How CPUs Work 6——Assembling CPUs

How CPUs Work 6——Assembling CPUs

Now that we have introduced all the basic parts, we can build the CPU. This section will cover a lot of content.

Since we are building a CPU, we need programs because the CPU is used to execute programs. We know that any program written in a programming language will eventually be converted into binary, so here we directly use the binary programming language (machine language). For example, if we let the CPU calculate an addition 3 14, this addition operation, if described in machine language, is similar to the following binary:

It looks a bit confusing, right? Don't worry, we will explain it later. All you need to know is that this binary program needs to be stored in memory so that the CPU can read and execute it.

Not only the program, but also 3 and 14 as the data for calculation are stored in the memory. What the CPU needs to do is to read the data 3 and 14 from a certain address in the memory, and then perform calculations on the two numbers according to the program requirements (addition, subtraction, etc.), and the result of the calculation is stored at a certain address in the memory.

To achieve such a function, there must be a convention so that the CPU can recognize the corresponding actions, namely reading, calculating, and saving, so we establish the following convention: (called an instruction table).

The instruction table can be understood as the interpreter of the program. When the CPU receives a program, such as 00101110, it must know what it means, and the instruction table can help answer questions and resolve doubts.

Specifically, the first four bits of each program (instruction) correspond to the opcode in the instruction table, and the last four bits correspond to the address in the instruction table. For example, 00101110, the first four bits 0010 are the opcode, which means LOAD_A (see the instruction table), which means reading data into register A. Looking at the last four bits 1110, the content described in the instruction table is "4-bit memory address", which is actually the memory address of the data to be read. Put together, it means "read data at address 1110 and save it to register A".

Next, let's move on to the circuit! First, we need a piece of memory. We can directly use the 256B memory mentioned in the previous section, but for the sake of convenience, we assume that it has only 16 addresses and can store 16 8-bit binary numbers. In addition, six registers are needed, each of which can store an 8-bit binary number. Register AD is used to temporarily store and operate data, the instruction address register is used to record where the program is running (the address of the program instruction), and the instruction register is used to store the instruction content.

Next, let's analyze the working process. When the computer starts, the initial values ​​of all registers are 00000000, and the CPU begins to enter the first stage: fetching instructions , that is, getting instructions from the memory. The instruction address register will be connected to the memory and read the data with address 00000000, that is, 00101110. This data will be saved in the instruction register, and the first stage ends.

The second stage: decoding, that is, figuring out what the instruction is going to do. In fact, we have already laid the groundwork before. The instruction content is 00101110, where the first four bits 0010 are the opcode, which corresponds to LOAD_A in the instruction table. The last four bits of the instruction are 1110, which corresponds to the 4-bit memory address. The overall meaning is to read data from the position 1110 and save it in register A. But there is no ready-made circuit that can do decoding work, so we need to add some circuits, as shown in the figure.

We will call this newly added circuit the decoding circuit for the time being. The first four bits of the instruction register, 0010, are used as the input of the decoding circuit. After being processed by these gate circuits, the output will eventually be 1. In other words, the function of the decoding circuit is to identify whether the instruction is 0010 (LOAD_A). Only when the instruction is 0010, the output is 1, otherwise it is 0.

The third stage: execution. The output of the decoding circuit will be connected to the memory's read permission port, and the last four bits of the instruction register 1110 will be connected to the memory's address port, which is equivalent to allowing the data at the memory address 1110 to be read, which is 00000011 (decimal 3).

How do we save this data to register A? We need to connect the output of the decoding circuit to the write-enabled port of register A at the same time, and the data input ports of the four registers need to be connected to the data ports of the memory. When the data 00000011 is read out, it will be sent to the four registers at the same time, but only register A is allowed to be written, so the data is saved in register A.

Next, the instruction address register 1 is changed to 00000001 to fetch the next instruction. The following steps are similar to the previous ones. It should be noted that the previous decoding circuit can only recognize the first instruction LOAD_A, and each subsequent instruction requires a separate decoding circuit to support it. We call the decoding circuits and instruction registers, instruction address registers, etc. corresponding to all instructions as the control unit .

Next, we quickly analyze the remaining instructions. Now the instruction address is 00000001, so 00011111 is taken out of the memory and stored in the instruction register. The instruction corresponding to the first four bits 0001 is LOAD_B, and the last four bits 1111 are the memory address to be read, and the corresponding data is 00001110 (decimal 14), which will be stored in register B. Then the instruction address register 1 (00000010) continues to fetch the next instruction. The instruction content is 10000100. The first four bits 1000 correspond to ADD in the instruction table, and the last four bits 0100 are the addresses of the two registers 01 and 00 respectively (because there are only four registers AD, which can be described by a two-bit binary number), where 00 is register A and 01 is register B, so the function of this instruction is to add the values ​​of registers A and B. When it comes to addition, the arithmetic logic unit mentioned before, also known as the operation unit (ALU), must be used. Let's simplify the circuit as follows:

Registers A and B are connected to the two input terminals of the arithmetic unit through the control unit, and the control unit also passes the operator to the arithmetic unit, so that calculation can be performed. The result of the calculation must be saved, but the instruction itself does not specify where to save it, so there is a convention that the result of the calculation will be saved in the last register in the instruction address. The two register addresses are 01 and 00, followed by 00, which is register A, so the final result will be saved to register A through the control unit. The result is 00010001 (decimal 17).

Instruction address register 1 (00000011), continue to fetch the next instruction. The instruction content is 01000111, the first four bits 0100 indicate that the instruction is STORE_A, that is, write the data of register A to the memory, the memory address is the last four bits of the instruction 0111, the control unit will send a read permission signal to register A, send a write permission signal to the memory, and save the value of register A in the corresponding position in the memory.

Finally, we have completed a simple program task, adding two numbers, and successfully saved the result. We will find that the reading, decoding, and execution of each instruction is equivalent to a cycle. The CPU repeats this cycle continuously to complete various tasks. In each cycle, the arithmetic logic unit, control unit, and storage unit (memory) need to work closely together, and the rhythm must not be chaotic to ensure the correct final result. But how to ensure that this rhythm is appropriate? It cannot be too fast, because even the processing of electrical signals takes time, and it cannot be too slow, resulting in low computing efficiency. So there is a separate circuit controlling this rhythm, just like a clock, accurately directing each part to run in an orderly manner. The CPU has an important indicator: the main frequency, such as 2.6GHz, which is equivalent to 2.6 billion cycles/second, which means that the CPU will execute 2.6 billion cycles in one second. The higher the main frequency of the CPU, the faster the speed. We encapsulate the arithmetic logic unit with a clock circuit, the control unit, and 6 registers into a relatively independent part, which is the CPU!

So far, we have started from a simple transistor switch, and finally built a complete CPU, which is of course the most basic CPU. I believe that this series of articles can give us a clear understanding of the combination of hardware and software. All kinds of software we use in daily life are written by program instructions. Each instruction of each program will be processed by many transistor switches inside the CPU to finally complete the task we hope for.

<<:  Do you think plants don’t feel “pain” when you pick flowers and step on grass?

>>:  The red signals on women's faces should not be ignored! Why do "menstrual acne" recur?

Recommend

React mobile enterprise data project practice

Course Description This project starts with getti...

How to speed up NFC development?

In 2014, thanks to Apple's participation, NFC...

Why can't we breathe through both nostrils at the same time?

Review expert: Yuan Xiandao (Deputy Chief Physici...

How can social e-commerce make good use of “Internet celebrity” thinking?

Introduction: It has to be said that the topic of...

Basic knowledge of POS machines, understand POS machine knowledge

The POS machine used always jumps code, which caus...

I was bitten by a snake! What should I do? What should I do?

Life is not just about the immediate, but also ab...

Why are konjac shreds white and konjac cubes black?

In summer, all the fat people know one thing: &qu...