This is the 15th day of my participation in the August Text Challenge.More challenges in August

Most of us use Intel or AMD cpus, But this article uses a simplified version of the CPU in the Book But How Do It Know (called the Scott CPU) as an example to describe each part of the CPU and How It works.

As shown below, this is the inside, shell and underside of a CPU:

There are pins underneath the CPU that connect the chip to other components on the motherboard. I can see that there are some lines on the motherboard corresponding to the three bus: address bus, data bus and control bus. Through these buses are our external devices, such as the RAM on the far right where our memory slots are. On the far left are slots for other external devices, such as monitors and keyboards.

Interact with RAM

Let’s start by looking at how the CPU interacts with RAM.

The picture is a 256 byte RAM, which can be divided into three leads: Address, data, (set&enable) exactly corresponding to the three buses on the motherboard.

We can roughly divide the data structure stored in RAM into two parts: one is the address, and the other is the data (corresponding to the white and gray parts of the picture respectively). We can see that the address or data is only 8 bits, because our address bus and data bus are also only 8 bits.

RAM stores data in zeros and ones that correspond to numbers, characters, instructions, and addresses.

  • Instructions: The CPU’s main job is to execute programs, which consist of a series of individual operations. These operations are calledinstruction. The most important part of a CPU’s calculations are the instructions, which tell the CPU what to do next.
  • Numbers: The operation data that we need to perform operations is usually followed by the operation instructions.
  • Characters: Our characters have corresponding encoding (such as ASCII code), characters are also stored in memory in the form of 0 and 1, when we need to output a string, the output instructions are followed by several character storage blocks.
  • Addresses: If we want to output results to external devices such as monitors, printers, etc., we need the addresses of these devices.

CPU common instruction set

Part of RAM’s data storage is the instruction set. Here are some common instruction sets.

  • LOAD: Loads data from RAM to CPU
  • ADD: Calculates the sum of the two numbers
  • STORE: Writes the calculated data back to RAM
  • COMPARE: COMPARE two numbers
  • JUMP IF Condition: judge the Condition and JUMP to another RAM address based on the result
  • JUMP: Forcibly jumps to another RAM address, similar to the JUMP instruction in C
  • OUT: Output to an external device address, such as a monitor
  • IN: Receives data from an external device, such as a keyboard

It should also be mentioned that each CPU has a circuit that goes on and off at a fixed frequency to synchronize all its components. This part is called the clock. The CPU’s computation needs the clock to constantly oscillate to synchronize all the components and make them coordinate to complete the task. A clock-cycle CPU can only do one thing. Modern cpus are measured in terms of computing speed in the GHZ, G for billion, and HZ for Hertz, the number of times per second. Modern CPU clocks can turn on and off billions of times per second. So modern cpus have clock rates and circuit on-off times in the billions of words. This allows the CPU to handle complex tasks extremely quickly.

Take the data

The CPU executes instructions sequentially by default, and addresses jump only when a jump is needed in the program. When the computer starts to execute a program, it sends an address to RAM to determine where the program starts.

You can see that the CPU sends the address 0110001 to the RAM through the address bus, and the RAM needs to return the data to the CPU after receiving the information. However, this return is not immediately automatic, and we can see that there are two lines on the control bus: set and enable. RAM does not do anything when the two lines are not connected, that is, it receives the address request from the CPU, but it does not return data! Only when the enable line is enabled will RAM send data to the CPU over the data bus, and the data returned here is 0010001.

After the CPU has processed the first block of data, the CPU will request the next address sequentially, and then repeat the above steps.

Write the data

During the execution of the program, we may need to write the calculated results into RAM. At this time, the address bus first tells RAM the address to be written, and then opens the SET line, and then transmits the data to RAM through the data bus.

Guess the number of games

We describe the processing process of CPU through a guessing game: the rule of the game is very simple, which is a random value in our program, and then judge whether the number is correct according to the user’s keyboard output. If it is correct, it will succeed, and if it is not correct, it will return guess again.

First, we can take a logical look inside the CPU:

There are two parts involved in the core work:

The control unit

  • Like the captain, you take instructions from RAM and parse them into commands that other components can understand.

ALU

  • The unit of operations that performs all mathematical operations, such as addition, subtraction, or comparison.
  • The ALU has two input interfaces, which we label as input A and input B.
  • The ALU OutPut may be a comparison result or a new number, so the ALU will have a state flag OutPut and an OutPut OutPut

The following is the circuit diagram for executing this instruction:

  1. First, our program evaluates a value that ALU first evaluates and writes to register (1), using the same set and enable lines as RAM. When the ALU calculation result is output to the register, the register can only store the value when it is in the set path.
  2. We set this value in the register, and this is just the number that we need to guess, and this value needs to be retrieved by the ALU and compared to the value that the user entered. The control unit will open the Enable line, and register (1) will be connected to the CPU bus, which will have a number of registers with separate enable and SET lines, which can be used to store the value of the previous operation. The controller opens one of the set lines and sets the value to the register, such as register (2). After setting, the control unit disconnects the ENABLE line to clear the entire CPU bus. (This step completes the transfer of a number from one register to another by enabling and setting the line.)
  3. However, the disadvantage of transmitting data through the bus is that only one number can be processed at a time point, so the two outputs of ALU cannot be obtained at the same time. Therefore, ALU adds a temporary register as input B, such as register (3). This register is only output to the ALU, so there are only set lines assigned to it by the control unit.
  4. The output of A is directly connected to the above registers and CPU bus, so when you need to input A, you only need to open the enable line on this register. (This video skips the process of getting user input.)
  5. The following two numbers have been obtained, and we need to read the next instruction from RAM to determine how to process the two numbers. This instruction is stored in the instruction register (4). The instruction register only has set, because it only has the operation of input to the control unit, but no synchronization or write back operation. We’ve parsed it out from the instructions, and passed it to ALU what we need ALU to do.
  6. After ALU compares the numbers, the result is A bool value because it is equal. ALU represents the base table result by four lines, and the connected form of the four lines (mainly two of them) represents A big, B big, and the same size. The Flag register also needs to be written to through the controller set.
  7. After the comparison is complete, it is usually followed by the JUMP IF command, which is used to JUMP to the execution of the logic that is not working. The CPU uses an instruction address register to find where the next instruction is in RAM. When the CPU is ready to receive the next instruction, the instruction address register is connected to the CPU bus (enable). This address is eventually streamed to RAM, but a memory address register acts as an intermediary to tell RAM the address of the required data. Then we can open the eanble line of the control bus and take the next instruction from RAM.
  8. When we get the JUMP IF EQUAL command, the control unit checksequalThis step is done by connecting the instruction register to the equivalent result of the preceding flagWith the doorOn, if these two conditions are true, then the line to the door is the path. The line triggers a JUMP JUMP to retrieve the next piece of data from memory, which is an address. This address is stored in the instruction address register.
  9. We then proceed to the address, where if we guess correctly, we receive an output to the monitor, which is then followedYou guessed itCharacter, and output to the display,

We will reduce the logical block, and then corresponding to the external wiring, and the above motherboard to do a corresponding. This makes it clear how the bus and ALU and controller interact with peripheral RAM and keyboard display devices.

The resources

How the CPU works