💻 - See How a CPU Works

💻 – See How a CPU Works

the CPU short for central processing unit is like the brain of the computer and once you understand how it works you'll understand the computer as well let's remove the cover of the CPU and zoom in to see what happens inside there are lots of different wires carrying information around the CPU this particular CPU is called the 6502 and was used in computers like the Apple 2 and the Commodore 64 as well as in the original Nintendo Entertainment System this simulation of the 6502 can be found online at visual 6502 org in every CPU there is a particular wire that turns on and off at a steady rate to help keep everything in sync that wire is called the clock and the clock in this simulation is turning on about twice a second modern CPUs are measured in gigahertz giga meaning billion in hertz meaning times per second so the clock in modern cpus turns on several billion times per second that speed is what allows cpus to do very complicated things very quickly however what the CPU does during each clock tick is actually very simple and something we'll look at more in this video for now we'll zoom back out and put the cover back on the CPU and your computer might be manufactured by a company like Intel or AMD but the type of CPU we're going to look at today is called the Scott CPU the Scott CPU doesn't actually exist except as a design in a book called but how do it know by john scott the design of the scott cpu is copyrighted and it's being used in this video with john's permission the 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 technical jargon if you've been looking for a book that explains how a computer works I would highly recommend this one so let's flip the CPU over and look underneath you'll see a lot of pins sticking out that allow the CPU to take in information and send it back out the CPU fits into what's known as the motherboard the motherboard allows all the components in the computer to connect to each other so we'll flip the CPU back over and plug it into the motherboard on the right of the motherboard is a place for something called RAM RAM is short for random access memory and it just contains all the data that is being processed by the CPU let's learn a little bit more about by looking at how the CPU in RAM interact for now we'll remove the wires on the left and move the motherboard over to make room for the RAM chip Ram consists of a list of addresses and at each of those addresses is a piece of data the CPU normally requests and processes each piece of data from RAM in order one after the other however if the CPU is instructed to pull data out of order it can do so that is why it's called random access memory the data can be accessed randomly if it needs to be although normally it's accessed in order when the computer first starts running a program it sends an address to RAM to begin retrieving that program the RAM address just consists of a series of ones and zeros representing on and off wires Ram doesn't do anything with that address though until the CPU also turns on the set or the enable wire if the enable wire is turned on Ram automatically sends whatever piece of data is at that address back to the CPU that data is then processed by the CPU accordingly once the CPU is finished processing that piece of data it then sends another address to ram turns on the enable wire and gets the next piece of data from Ram this process happens over and over again inside the computer if the CPU needs to save data to Ram it outputs an address output some data and then turns on the set wire the RAM will then overwrite the data at that address with the new data but what is that data inside Ram because it just looks like a bunch of ones and zeros well it's made up of different things some of the most important pieces of data in Ram are the instructions instructions just tell the CPU to do different things there are also numbers inside that data these are numbers that you might want to add together compare or simply process in some way another thing and this is kind of weird and that is in the data is addresses the particular memory addresses in RAM the data itself is also an address these addresses can be used for various things for instance if you want to output a number to an external device you have to know the address of that particular device do 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 screen you would actually store it as a bunch of ones and zeros and RAM each letter is stored as a particular combination of ones and zeros according to a character code these character codes are arbitrary someone just decided that this is a lowercase a and this is an uppercase G for instance so that's what's actually in the data inside Ram now let's go back to seeing the data is just a bunch of ones and zeros will now move the RAM chip into the RAM socket on the motherboard will then group the RAM addresses and data together pull the CPU out of the motherboard and look at what's called the instruction set of the CPU as we saw earlier some of the pieces of data in RAM or instructions and each CPU has its own set of instructions that it understands so there might be a load instruction which loads a number from RAM into the CPU after a couple of these load instructions there might be an add instruction that adds these two numbers together after an add instruction might be a store instruction which saves the result of that addition back out to RAM to be used later on there might also be a compare instruction after some load instructions which compares two numbers together to see which one is larger or if they are the same the compare instruction can be very useful when used in conjunction with what's called a jump if instruction as we saw earlier the CPU generally requests each piece of data from RAM in order one after the other sometimes though the programmer wants to jump to an out of order ram address to process some other instructions and memory the jump if instruction checks to see if a certain condition is true before it jumps it uses the results of the compare instruction to make this decision there is also a regular jump 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 or input data from an external device like a keyboard these two instructions are often used in conjunction with an address like we talked about earlier there are some other instructions in a CPUs instruction set that these are some of the more commonly seen ones so as we saw the data in RAM consists of things like instructions numbers addresses and letters so let's go through a program that would use this instruct set to play a guessing game so it would load a number like nine into the CPU let's say that the programmer decided that that was the right answer so he went ahead and put that number into RAM and then comes an end instruction to retrieve the user's guess after the in instruction is the address of the keyboard so he can know where we're getting the data from next comes a compare instruction that checks to see if those two numbers the one saved by the programmer and the one entered by the user are the same following the compare instruction is a jump if equal instruction which will jump to another address in RAM if those two numbers we just talked about are the same the jump if equal instruction is immediately followed by a new RAM address if the two numbers are the same the CPU jumps to that new address to begin processing its next set of instructions from there if the two numbers are not the same then the computer ignores the jump if equal instruction and the corresponding address and just keeps going following the jump to address comes an out instruction with the address for the monitor then the letter capital G and then below that would be the letters U e SS space again so guess again so if the user guesses the wrong number the program would tell him to guess again and then jump back up to the in instruction to retrieve that new guess and then process these instructions all over again by the way the in and out instructions used here have been simplified somewhat but you'll find them covered in more detail in the book so now let's briefly take a look inside the CPU itself to see how it would process an instruction as we saw earlier this is the inside of the 6502 CPU let's take away the 6502 wiring and see what's inside the Scotts CPU the first component is the control unit which is kind of like a captain in the army it receives its orders from RAM in the form of an instruction and then breaks that instruction down into specific commands for the other components one 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 subtraction or even comparison like we saw earlier the arithmetic logic unit has two inputs will label them input a and input B and assume they are two numbers from some previous load instructions now we might want to add those two numbers together the control unit receives that instruction from RAM and then tells the ALU what type of operation to perform the ALU performs the operation and then outputs the answer sometimes though depending upon the type of instruction the output from the ALU can actually be ignored for instance if you have a compare instruction the ALU doesn't need to output an answer instead it just needs to tell the control unit and how the two numbers compare to each other for this the ALU uses what 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 later for 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 a register is a very simple component whose only job is to store a number temporarily registers act just like RAM except they are inside the CPU making them faster and more useful for storing a number temporarily while instructions being processed when the ALU sends the output to the register it won't actually be saved until the control unit turns on the registers set wire the set wire is just like the one we saw earlier for rim when the set wire is turned on the register saves whatever number is on its input wires once 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 wire that also runs from the control unit to the register as soon as the control unit turns the enable wire on the register will output whatever number is saved inside the output wires of the register then connect to what is called the CPU bus a bus as we saw earlier on the motherboard is simply a group of wires that connect multiple components inside a computer on the bus are some other registers with their own set and enable wires these may have numbers from previous instructions already saved inside so the control unit will then turn on the set wire of the particular register that it wants to save that number to and that number will be saved in that register afterward the control unit will then turn off the enable wire from the first register and clear the bus the four registers at the top are just used for storing numbers between operations so they have output wires that go directly back onto the bus so now we have moved a number from one register to another just by turning some wires on and off that's the advantage of the bus easily moving numbers between components the disadvantage of the bus is that you can only have one number on it at a time because of this limitation the arithmetic logic unit uses a temporary register for input B when the control unit is processing an instruction involving the ALU it will move one of the inputs to the temporary register the temporary register has no need for an enable wire since it only outputs to the ALU and doesn't conflict with any other registers the other input to the ALU comes directly from the bus the control unit will enable another register and that number will become input a to the ALU that number stays on the bus until the ALU is finished processing the instruction and so now there are two inputs to the ALU and we're ready for the ALU to perform an operation as we saw earlier the control unit knows what operation to tell the ALU to perform because of the instruction it receives from rim the instruction itself is in another register called the instruction register by the way the input wires from the bus won't affect this register since the instruction was already saved in a previous step this register like the temporary register also has no need for an enable fire since it just outputs to the control unit based upon this instruction the control unit then tells the ALU what type of operation to perform so let's say the instruction we're processing is a compare instruction with the compare instruction we're not interested in the number that is output by the ALU we only want to know how the two inputs compare to each other for that we use the flags that we talked about earlier each flag is just a wire that turns on or off depending upon whether or not a certain condition is true inside the Scott's CPU there are four flags and we'll look at two of them now the a is larger flag will turn on if input a is larger than input B if the inputs are the same then the equal flag turns on and if both of these flags are off that means input B is larger but in this case the equal flag is on so that means both inputs are the same once the compare instruction is over we still need to use the flags for the next instruction so we'll save them to a register that only has four inputs and four outputs one for each flag once the flags are set into the flags register the CPU is finished with the compare instruction and it can then request the next instruction from REM generally the next instruction after a compare instruction is a jump if instruction this combination of a compare and a jump if instruction by the way is very common in computer programming anytime there is more than one possible path through a program the computer is using these instructions to tell it which way to go so now that we're finished with the compare instruction and our flags are saved in the flags register we need to tell Ram that we're ready for the next piece of data in this case the next instruction so inside the CPU another register that is very important is what's called the instruction address register the CPU uses this register to know where the next instruction should come from in RAM when the CPU is ready for the next instruction it enables the instruction address register on to the bus eventually the instruction address will flow to RAM but it doesn't get there directly there is an intermediary register called the memory address register whose only job is to tell RAM what memory address the CPU wants next since it won't always be an instruction once the instruction address is set into the memory address register it has automatically sent to RAM since the memory address register doesn't have an enable wire the control unit then turns on the enable RAM wire and RAM automatically sends back the data at that address which in this case is an instruction that instruction is then saved in the instruction register and the control unit begins processing it in this case it's a jump if equal instruction which checks to see if the equal flag is on it does that by running one of its wires and the equal flag wire into a NAND gate if both inputs to the and gate or on then the output wire turns on as well this output wire will then trigger the jump that jump eventually retrieves the next piece of data from RAM which happens to be an address and move it into the instruction address register when the jump if equal instruction is over the CPU then processes the instruction at that new address at that new address maybe some instructions that output the text you guessed correctly onto the screen because now we know that the user guessed correctly so we'll add the final four wires to our CPU which are used to control the external devices like the monitor and the keyboard we now have a nearly complete picture of what the Scott CPU looks like data moves around inside the CPU using the bus and is stored in each register according to how that data is going to be used each instruction that we have seen can be processed by the Scott CPU in about 6 clock ticks modern CPUs can process multiple instructions per clock tick meaning that the computer you're using to watch this video is likely processing tens or even hundreds of billions of instructions per second that phenomenal speed is accomplished by using more than one of each component and making sure that all the components are active as much as possible this makes modern CPUs much more calm than the Scot CPU but they are still fundamentally doing the same things as the Scot CPU so now let's zoom back out and we can see all the wires that run back out to the pins on the chip on the right are the set RAM and enable Ram wires on the top are the RAM address wires on the bottom are the data wires that run to both RAM and the external devices and on the left are the input/output control wires so let's zoom back out to see the rest of the chip and we'll put the cover back on the CPU and put it back in the motherboard using the ports on the Left we can now plug in the cables that connect our monitor and our keyboard each of these ports has an address and that port address is what the CPU uses with an in or an out instruction that port address by the way is sent using the data bus since the address bus in this computer is only used for RAM so we'll zoom out to see how the motherboard fits inside the computer case in the computer case is the last component we'll look at which is the hard drive as soon as the power to the computer is turned off all the data and RAM is lost so you have to have a way to store it more permanently for that we use a hard drive inside the hard drive is a spinning disk covered in tiny magnets with a small metal arm floating above it the arm moves around to the different parts of the disk where a different data can be stored and retrieved the disk and the arm generally move very very quickly but nowhere near as fast as the CPU can process data for this reason all the data from the hard drive must first be moved to RAM before it can be processed so we'll put the hard drive back inside the computer and zoom out here we can see the program we just ran and the message telling the user that he guessed correctly so now you've seen the very basics of how a computer processes information you'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 video but those shouldn't detract from your understanding of either you can find a list of these differences in the video description thanks watching you

38 thoughts on “💻 – See How a CPU Works”

  1. There is now an interactive model of the cpu at the buthowdoitknow website. A fantastic tool to use while reading the book.

  2. Did anyone else sympathize with his/her computer???????? I will never frown again when computer takes a rest (freezes), this is much to expect the slim obedient thing. Anyway, they are quite impressive machines.

  3. cpu controls 16 peripherals throgh 64 wires(bits).. maybe these days its not the same but 1 hz means listening to all 15 . it can take(or send) 4*16*64 bits of information in one hert and what cpu actually does is programmed in that exe that we run .

  4. Amazing video, as always.
    I was going to buy the book until I heard that he copyrighted the schemes… like… what? If your focus is to spread knowledge then why in the world would you copyright a way of explaining something?

  5. Shame you had to throw religion in there, that kept me from becoming interested in possible other videos you might have made.

  6. The Scott CPU apparently runs on no power, since there are no VCC or ground pins. The CPU doesn't start with a program in RAM. No mention of ROM. This blows.

  7. No computer now or in the future will ever match the sheer wit of humans that is displayed here (as an example) in the comments section when the subject matter becomes over complicated. I love these technical videos to read the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Tags: , , , , ,