How a CPU Works

# How Computers Process Information: A Deep Dive into CPUs and Memory

## Introduction to the CPU

The Central Processing Unit (CPU), often referred to as the brain of a computer, is responsible for executing instructions that drive nearly every action your computer performs. Understanding how the CPU works provides insight into the inner workings of computers themselves. By removing the cover of the CPU and zooming in, we can see a complex network of wires carrying information around. This particular CPU is the 6502, famously used in iconic computers like the Apple II, Commodore 64, and the original Nintendo Entertainment System. A detailed simulation of this chip can be found online at [visual6502.org](https://visual6502.org).

## The Clock: TheheartbeatoftheCPU

Every CPU has a specific wire that turns on and off at a steady rate, keeping all components in sync. This wire is called the clock. In modern CPUs, this clock operates at several billion cycles per second, measured in gigahertz (giga meaning billion, hertz meaning cycles per second). While modern CPUs operate at lightning-fast speeds, the basic operations performed during each clock tick are surprisingly simple.

## Introducing the Scott CPU

The type of CPU we will examine in detail is called the Scott CPU. However, this chip does not physically exist; it is a conceptual design found in the book *But How Do It Know?* by John Scott. This book provides an excellent foundation for understanding how computers work without overwhelming technical jargon. If you're looking to deepen your knowledge of computer architecture, this resource is highly recommended.

## Exploring the Physical Components

When we flip the CPU over, we notice a series of pins that allow the CPU to interact with other components. These pins connect the CPU to the motherboard, which serves as the central hub for all components within the computer. The motherboard provides slots for RAM (Random Access Memory), which stores data being processed by the CPU.

## Understanding RAM

RAM consists of a list of addresses, each containing a piece of data. Normally, the CPU processes this data sequentially; however, it can access any address randomly when instructed to do so. When a program starts, the CPU requests an address from RAM to begin retrieving the program. This address is represented as a series of ones and zeros, corresponding to on and off wires.

The process involves three key steps:

1. The CPU sends an address to RAM.

2. It turns on the "enable" wire, prompting RAM to send back the data at that address.

3. The CPU processes this data before requesting the next piece of information.

If the CPU needs to save data, it sends an address along with new data and activates the "set" wire, instructing RAM to overwrite the existing data at that address.

## Decoding Data in RAM

The data stored in RAM is a mix of instructions, numbers, and addresses. Instructions guide the CPU on what operations to perform (e.g., load, add, store). Numbers are processed mathematically, while addresses direct the CPU to specific locations or external devices (e.g., printers or monitors). Letters are also stored as binary representations according to character codes, which define how each letter is displayed.

## The Instruction Set of the Scott CPU

The Scott CPU's instruction set includes fundamental commands like "load," "add," "store," and "compare." These instructions allow the CPU to manipulate data. For example:

- A load instruction retrieves a number from RAM.

- An add instruction combines two numbers.

- A store instruction saves the result back into RAM.

Conditional jumps are another critical feature of this instruction set. The "jump if" instruction evaluates conditions (e.g., whether two numbers are equal) before deciding to execute specific code paths. This capability is essential for creating programs with branching logic, such as a guessing game that provides feedback based on user input.

## Inside the CPU: Control Unit and Arithmetic Logic Unit

The CPU's control unit acts like a manager, interpreting instructions from RAM and directing other components to perform tasks. The Arithmetic Logic Unit (ALU) handles mathematical operations, receiving inputs from registers—temporary storage units within the CPU.

For instance:

- When processing an add instruction, the ALU receives two numbers from registers.

- It performs the addition and outputs the result.

- Depending on the operation, this output might be stored in another register or sent to RAM.

## Flags and Registers

The ALU also sets flags based on its operations. These flags provide critical information about the results of operations (e.g., whether two numbers are equal). The control unit uses these flags to make decisions, such as whether to jump to a new address in RAM.

Registers play multiple roles within the CPU:

- Temporary registers store intermediate values during calculations.

- Instruction and flags registers hold the current instruction and status information.

- Memory address and data registers interact with RAM and external devices.

## Bus System: Connecting Components

The bus is a group of wires connecting different components within the computer. While it allows for efficient communication, it can only carry one piece of data at a time. This limitation necessitates careful management when moving data between components like the ALU, registers, and RAM.

## External Devices and Port Addresses

Ports on the CPU allow interaction with external devices like monitors and keyboards. These ports have addresses used in "in" and "out" instructions to send or receive data. For example:

- An "out" instruction sends a message (e.g., "G") to the monitor.

- An "in" instruction retrieves user input from the keyboard.

## The Hard Drive: Permanent Storage

While RAM is essential for processing, it loses all data when power is turned off. To preserve information, computers use hard drives. These devices store data magnetically on spinning disks, which must be transferred to RAM before processing.

## Conclusion: From Basics to Modern CPUs

The Scott CPU provides a foundational understanding of computer architecture. While modern CPUs are far more complex, they still rely on the same principles—executing instructions, manipulating data, and communicating via buses. Resources like John Scott's book and online simulations offer valuable insights into these mechanisms.

By exploring the inner workings of the CPU and its interactions with RAM and external devices, we gain a deeper appreciation for the intricate dance of ones and zeros that powers our digital world.

"WEBVTTKind: captionsLanguage: enThe CPU short for central processing unit is like the brain of the computer and once you understand how it worksYou'll understand the computer as well. Let's remove the cover of the CPU and zoom in to see what happens insidethere are lots of different wires carrying information around the CPU this particular CPU is called the6502 and was used in computers like the Apple 2 and the Commodore 64 as well as in the original Nintendo Entertainment SystemThis simulation of the 6502 can be found online at visual 6502 org inEvery CPU there is a particular wire that turns on and off at a steady rate to help keep everything in syncThat wire is called the clock and the clock in this simulation is turning on about twice a secondModern CPUs are measured in gigahertzgiga meaning billion in hertz meaning times per second so the clock in modern cpus turns on several billion times per secondThat speed is what allows cpus to do very complicated things very quicklyHowever, what the CPU does during each clock tick is actually very simple and something. We'll look at more in this videoFor now we'll zoom back out and put the cover back onThe CPU and your computer might be manufactured by a company like Intel or AMDBut the type of CPU we're going to look at today is called the Scott CPUThe Scott CPU doesn't actually exist except as a design in a book calledBut how do it know by john scott the design of the scott cpu is copyrightedAnd it's being used in this video with john's permissionThe book is available at but how do it no com?This is a great book that goes through each of the components in the CPU very slowly without using a lot of overly technicalJargon, if you've been looking for a book that explains how a computer works. I would highly recommend this oneSo let's flip the CPU over and look underneathYou'll see a lot of pins sticking out that allow the CPU to take in information and send it back outThe CPU fits into what's known as the motherboard the motherboard allows all the components in the pewter to connect to each otherSo we'll flip the CPU back over and plug it into the motherboard onThe right of the motherboard is a place for something called RAM RAM is short for random access memoryAnd it just contains all the data that is being processed by the CPULet's learn a little bit more about RAM by looking at how the CPU and RAM interactFor now well remove the wires on the left and move the motherboard over to make room for the RAM chipRam consists of a list of addresses and at each of those addresses is a piece of datathe CPU normally requests and processes each piece of data from RAM in order one after the otherHowever, if the CPU is instructed to pull data out of order it can do so that is why it's called random access memoryThe data can be accessed randomly if it needs to be although normally. It's accessed in orderWhen the computer first starts running a program it sends an address to RAM to begin retrieving that programThe RAM address just consists of a series of ones and zeros representing on and off wiresRam doesn't do anything with that address though until the CPU also turns on the set or the enable wireIf the enable wire is turned on Ram automatically sends whatever piece of data is at that address back to the CPUThat data is then processed by the CPU accordinglyOnce the CPU is finished processing that piece of data it then sends another address to RamTurns on the enable wire and gets the next piece of data from Ram this process happens over and over again inside the computerIf the CPU needs to save data to Ram it outputs an addressOutput some data, and then turns on the set wireThe RAM will then overwrite the data at that address with the new dataBut what is that data inside Ram because it just looks like a bunch of ones and zerosWell, it's made up of different things some of the most important pieces of data in RAM are the instructionsInstructions just tell the CPU to do different thingsThere are also numbers inside that data. These are numbers that you might want to add together compare or simply process in some wayAnother thing and this is kind of weird that is in the data is addresses atParticular memory addresses in RAM the data itself is also an addressThese addresses can be used for various thingsFor instance if you want to output a number to an external device you have to know the address of that particular deviceDo you want to send data to the printer, or do you want to send it to the monitor for instance?There are also letters stored in RAM if you want to show some text on the screenYou would actually store it as a bunch of ones and zeros and RAMEach letter is stored as a particular combination of ones and zeros according to a character code these character codes are arbitrarySomeone just decided that this is a lowercase a and this is an uppercase G for instanceSo that's what's actually in the data inside Ram nowLet's go back to seeing the data is just a bunch of ones and zeros and we'll now move the RAM chipinto the RAM socket on the motherboardWe'll then group the RAM addresses and data togetherPull the CPU out of the motherboard and look at what's called the instruction set of the CPUAs we saw earlier some of the pieces of data in RAM are instructions and each CPU has its own set of instructions that itUnderstands so there might be a load instruction which loads a number from RAM into the CPUAfter a couple of these load instructions there might be an add instruction that adds these two numbers togetherAfter an add instruction might be a store instruction, which saves the result of that addition back out to RAM to be used later onThere might also be a compare instruction after some load instructions, which compares two numbers together to see which one is largeror if they are the samethe compare instruction can be very useful when used in conjunction with what's called a jump ifInstruction as we saw earlier the CPU generally requests each piece of data from RAM in order one after the otherSometimes though the programmer wants to jump to an out of order ram address to process some other instructions and memoryThe jump if instruction checks to see if a certain condition is true before it jumpsIt uses the results of the compare instruction to make this decisionThere is also a regular job instruction that jumps no matter what?finally there is an out instruction and an in instruction these will output data to an external device like a monitor orInput data from an external device like a keyboardThese two instructions are often used in conjunction with an address like we talked about earlierThere are some other instructions in the CPUs instruction set that these are some of the more commonly seen onesSo as we saw the data in RAM consists of things like instructionsnumbers addresses and lettersSo let's go through a program. That would use this instruction set to play a guessing gameSo it would load a number like nine into the CPULet's say that the programmer decided that that was the right answerSo he went ahead and put that number into RAM and then comes an end instruction to retrieve the user's guess afterThe in instruction is the address of the keyboard so we can know where we're getting the data fromNext comes a compareInstruction that checks to see if those two numbers the one saved by the programmer and the one entered by the user are the sameFollowing the compare instruction is a jump if equal instruction which will jump to another address in RAM if those two numbersWe just talked about are the samethe jump if equal instruction is immediately followed by a new RAM address ifThe two numbers are the sameThe CPU jumps to that new address to begin processing its next set of instructions from there if the two numbers are not the sameThen the computer ignores the jump if equal instruction and the corresponding address and just keeps goingFollowing the jump to address comes an out instruction with the address for the monitor, then the letter capital GAnd then below that would be the letters U eSS. Space again, so guess againSo if the user guesses the wrong number the program would tell him to guess againAnd then jump back up to the in instruction to retrieve that new guess and then process these instructions all over againBy the way the in and instructions used here have been simplified somewhat, but you'll find them covered in more detail in the bookSo now let's briefly take a look inside the CPU itself to see how it would process an instructionas we saw earlierThis is the inside of the 6502 CPULet's take away the 6502 wiring and see what's inside the Scot CPUThe first component is the control unit which is kind of like a captain in the armyit receives its orders from RAM in the form of aninstruction and then breaks that instruction down into specific commands for the other componentsOne of the most important components under the command of the control unit is the arithmetic logic unit or ALU for short?The ALU is what performs all the mathematical operations inside the CPU such as addition subtractionOr even comparison like we saw earlierThe arithmetic logic unit has two inputs will label them input a and input B and assumeThey are two numbers from some previous load instructionsNow we might want to add those two numbers togetherThe control unit receives that instruction from RAM and then tells the ALU what type of operation to performThe ALU performs the operation and then outputs the answerSometimes though depending upon the type of instruction the output from the ALU can actually be ignoredFor instance if you have a compare instruction the ALU doesn't need to output an answer insteadIt just needs to tell the control unit how the two numbers compare to each otherfor this the ALU usesWhat are called flags and they help the control unit decide what to do when it receives the next?Instruction like jump if which we'll see laterFor now though. Let's say that we are working with an instruction that does produce an output. Where does that output actually go?well the eight wires coming out of the ALU would actually run to what is called a register aRegister is a very simple component whose only job is to store a number temporarilyregisters act just like RAM exceptThey are inside the CPU making them faster and more useful for storing a number temporarily Wow instructions being processedWhen the ALU sends the output to the register it won't actually be saved untilthe control unit turns on the registers set wireThe set wire is just like the one we saw earlier for RAMWhen the set wire is turned on the register saves whatever number is on its input wiresOnce we have the output saved in the register though. How do we get it back out?Well when we are ready to move a number out of the register we need another control wire called the enable wireThat also runs from the control unit to the register as soon as the control unit turns the enable wire onThe register will output whatever number is saved insidethe output wires of the register then connect to what is called the CPU bus aBus as we saw earlier on the motherboard is simply a group of wires that connect multiple components inside a computer onThe bus are some other registers with their own set and enable wiresThese may have numbers from previous instructions already saved insideso the control unit will then turn on the set wire of theParticular register that it wants to save that number to and that number will be saved in that registerAfterward the control unit will then turn off the enable wire from the first register and clear the busThe four registers at the top are just used for storing numbers between operationsSo they have output wires that go directly back onto the busSo now we have moved a number from one register to another just by turning some wires on and offThat's the advantage of the bus easily moving numbers between componentsThe disadvantage of the bus is that you can only have one number on it at a timeBecause of this limitation the arithmetic logic unit uses a temporary register for input BWhen the control unit is processing an instruction involving the ALU it will move one of the inputs to the temporary registerThe temporary register has no need for an enable wire since it only outputs to the ALU and doesn't conflict with any other registersThe other input to the ALU comes directly from the busThe control unit will enable another register and that number will become input a to the ALU thatnumber stays on the bus until the ALU is finished processing the instruction andSo now there are two inputs to the ALU and we're ready for the ALU to perform an operationAs we saw earlier the control unit knows what operation to tell the ALU to performBecause of the instruction it receives from rimThe instruction itself is an another register called the instruction registerBy the way the input wires from the bus won't affect this register since the instruction was already saved in a previous stepThis register like the temporary register, also has no need for an enable wire since it just outputs to the control unitBased upon this instruction the control unit then tells the ALU what type of operation to performSo let's say the instruction. We're processing is a compare instruction with the compare instructionWe're not interested in the number that is output by the ALUWe only want to know how the two inputs compare to each otherFor that we use the flags that we talked about earlierEach flag is just a wire that turns on or off depending upon whether or not a certain condition is trueInside the Scott CPU there are four flags, and we'll look at two of them nowThe a is larger flag will turn on if input a is larger than input Bif the inputs are the sameThen the equal flag turns on and if both of these flags are off that means input B is largerBut in this case the equal flag is on so that means both inputs are the sameOnce the compare instruction is over. We still need to use the flags for the next instructionSo we'll save them to a register that only has 4 inputs and four outputs one for each flagOnce the flags are set into the flags register the CPU is finished with the compare instruction and it can then request the nextinstruction from REMgenerally the next instruction after a compare instruction is a jump ifinstruction this combination of a compare and a jump if instruction by the way is very common inprogrammingAnytime there is more than one possible path through a program the computer is using these instructions to tell it which way to goSo now that we're finished with the compare instruction and our flags are saved in the flags registerWe need to tell RAM that we're ready for the next piece of data in this case the next instructionSo inside the CPU another register that is very important is what's called the instruction address registerThe CPU uses this register to know where the next instruction should come from in RAMWhen the CPU is ready for the next instruction it enables the instruction address register onto the buseventually the instruction address will flow to RAMBut it doesn't get there directly there is an intermediary register called the memory address registerWhose only job is to tell Ram what memory address the CPU wants next since it won't always be an instructionOnce the instruction address is set into the memory address register it has automatically sent to RAM since the memory address registerDoesn't have an enable wire the control unit then turns on the enable Ram wire and RAMAutomatically sends back the data at that address which in this case is an instructionThat instruction is then saved in the instruction register, and the control unit begins processing it inThis case it's a jump if equal instruction, which checks to see if the equal flag is onIt does that by running one of its wires and the equal flag wire into a NAND gateIf both inputs to the and gate or on then the output wire turns on as wellThis output wire will then trigger the jump thatjump eventually retrieves the next piece of data from RAMWhich happens to be an address and move it into the instruction address registerwhen the jump if equal instruction is over the CPU then processes the instruction at that new address atThat new address, maybe some instructions that output the text you guessed correctly onto the screen because now we know that the user guessed correctlySo we'll add the final four wires to our CPU which are used to control the external devices like the monitor and keyboardWe now have a nearly complete picture of what the Scotts CPU looks likeData moves around inside the CPU using the bus and is stored in each register according to how that data is going to be usedEach instruction that we have seen can be processed by the Scott CPU in about 6 clock ticksModern CPUs can process multiple instructions per clock tick meaning that the computer you're using to watch this video is likelyprocessing tens or even hundreds of billions of instructions per secondthat phenomenal speed is accomplished by using more than one of each component andMaking sure that all the components are active as much as possibleThis makes modern CPUs much more complicated than the Scott CPUBut they are still fundamentally doing the same things as the Scott CPUSo now let's zoom back out and we can see all the wires that run back out to the pins on the chip onThe right are the set RAM and enable RAM wires onThe top are the RAM address wires on the bottom are the data wires that run to both RAM and the external devicesAnd on the left are the input/output control wiresSo let's zoom back out to see the rest of the chipAnd we'll put the cover back on the CPU and put it back in the motherboardUsing the ports on the Left we can now plug in the cables that connect our monitor and our keyboardEach of these ports has an address and that port address is what the CPU uses with an in or an out instructionThat port address by the way is sent using the data bus since the address bus in this computer is only used for RAMSo we'll zoom out to see how the motherboard fits inside the computer case in the computer case is the last componentwe'll look at which is the hard drive asSoon as the power to the computer is turned off all the data and RAM is lost so you have to have a way toStore it more permanently for that we use a hard driveInside the hard drive is a spinning disk covered in tiny magnets with a small metal arm floating above itThe arm moves around to the different parts of the disk where a different data can be stored and retrievedThe disk and the arm generally move very very quickly, but nowhere near as fast as the CPU can process dataFor this reason all the data from the hard drive must first be moved to RAM before it can be processedSo we'll put the hard drive back inside the computer and zoom out here. We can see the programWe just ran and the message telling the user that he guessed correctlySo now you've seen the very basics of how a computer processes informationYou'll find much more about the Scot CPU in the book at the website, but how do it know comm?Also, there are a few small differences between the book and the videoBut those shouldn't detract from your understanding of either you can find a list of these differences in the video description. Thanks for watching.Whatever you do, work at it with all your heart, as working for the Lord, not for human masters -Colossians 3:23\n"