CPU چگونه کار می کند

CPU چگونه کار می کند

In One Lesson

۲۵ اسفند ۱۳۹۱، ۱۵:۲۷
watch in youtube
توضیحات نظرات
Learn how the most important component in your device works, right here!
Author's Website: http://www.buthowdoitknow.com/ See the Book: http://amzn.to/1mOYJvA
(As of 2024-01-15, all videos on this channel are under the CC0 license (very similar to Public Domain). Feel free to download and repost without compensation, attribution, or notice.)
https://creativecommons.org/public-domain/cc0/
See scripts for future videos here: https://github.com/In-One-Lesson/VideoScripts
See the 6502 CPU Simulation: http://visual6502.org/JSSim/index.html
Download the PowerPoint file used to make the video: https://docs.google.com/presentation/d/0BzwHNpicSnW0cGVmX0c3SVZzMFk/edit?usp=sharing&ouid=116531966426337918883&resourcekey=0-N0P5hrS6vx3En8ifQ-shGA&rtpof=true&sd=true
The CPU design used in the video is copyrighted by John Scott, author of the book But How Do It Know?.
There are a few small differences between the CPU in the video and the one used in the book. Those differences are listed below but they should not detract from your understanding of either.
CONTROL UNIT - This component is called the Control Section in the book. It is called Control Unit here simply because that is a more common name for it that you might see used elsewhere.
LOAD INSTRUCTION - In this video, what's called a LOAD instruction is actually called a DATA instruction in the book. The Scott CPU uses two different instructions to move data from RAM into the CPU. One loads the very next piece of data (called a DATA instruction in the book) and the other uses another register to tell it which address to pull that data from (called a LOAD instruction in the book). The instruction was renamed in the video for two reasons: 1) It might be confusing to hear that the first type of data we encounter in RAM is itself also called DATA. 2) Since the LOAD instruction from the book is a more complex concept, it was easier to use the DATA instruction in the video to introduce the concept of moving data from RAM to the CPU .
IN and OUT INSTRUCTIONS - In the Scott CPU, there is more involved in moving data between the CPU and external devices than just an IN or an OUT instruction. That process was simplified in the video to make the introduction of the concept easier.
ACCUMULATOR - The register that holds the output of the ALU is called the Accumulator in the book. That is the name typically used for this register, although it was simply called a register in the video.
MEMORY ADDRESS REGISTER - The Memory Address Register is a part of RAM in the book, but it is a part of the CPU in the video. It was placed in the CPU in the video as this is generally where this register resides in real CPUs.
JUMP INSTRUCTIONS - In the book there are two types of unconditional JUMP instructions. One jumps to the address stored at the next address in RAM (this is the one used in the video) and the other jumps to an address that has already been stored in a register. These are called JMP and JMPR instructions in the book respectively.
MISSING COMPONENT - There is an additional component missing from the CPU in the video that is used to add 1 to the number stored in a register. This component is called "bus 1" in the book and it simply overrides the temporary register and sends the number 1 to the ALU as input B instead.
REVERSED COMPONENTS - The Instruction Register and the Instruction Address Register are in opposite positions in the diagrams used in the book. They are reversed in the video because the internal wiring of the control unit will be introduced in a subsequent video and keeping these registers in their original positions made that design process more difficult.
OP CODE WIRING - The wires used by the control unit to tell the ALU what type of operation to perform appear near the bottom of the ALU in the video, but near the top of the ALU in the book. They were reversed for a similar reason as the one listed above. The wiring of the ALU will be introduced in a subsequent video and keeping these wires at the top of the ALU made the design process more difficult.
The CPU short for central processing unit is like the brain of the computer and once you understand how it workYou'll understand the computer as well. Let's remove the cover of the CPU and zoom in to see what happens insidthere are lots of different wires carrying information around the CPU this particular CPU is called th6502 and was used in computers like the Apple 2 and the Commodore 64 as well as in the original Nintendo Entertainment SysteThis simulation of the 6502 can be found online at visual 6502 org iEvery CPU there is a particular wire that turns on and off at a steady rate to help keep everything in synThat wire is called the clock and the clock in this simulation is turning on about twice a seconModern CPUs are measured in gigahertgiga meaning billion in hertz meaning times per second so the clock in modern cpus turns on several billion times per seconThat speed is what allows cpus to do very complicated things very quicklHowever, what the CPU does during each clock tick is actually very simple and something. We'll look at more in this videFor now we'll zoom back out and put the cover back oThe CPU and your computer might be manufactured by a company like Intel or AMBut the type of CPU we're going to look at today is called the Scott CPThe Scott CPU doesn't actually exist except as a design in a book calleBut how do it know by john scott the design of the scott cpu is copyrighteAnd it's being used in this video with john's permissioThe book is available at but how do it no comThis is a great book that goes through each of the components in the CPU very slowly without using a lot of overly technicaJargon, if you've been looking for a book that explains how a computer works. I would highly recommend this onSo let's flip the CPU over and look underneatYou'll see a lot of pins sticking out that allow the CPU to take in information and send it back ouThe CPU fits into what's known as the motherboard the motherboard allows all the components in the pewter to connect to each otheSo we'll flip the CPU back over and plug it into the motherboard oThe right of the motherboard is a place for something called RAM RAM is short for random access memorAnd it just contains all the data that is being processed by the CPLet's learn a little bit more about RAM by looking at how the CPU and RAM interacFor now well remove the wires on the left and move the motherboard over to make room for the RAM chiRam consists of a list of addresses and at each of those addresses is a piece of datthe CPU normally requests and processes each piece of data from RAM in order one after the otheHowever, if the CPU is instructed to pull data out of order it can do so that is why it's called random access memorThe data can be accessed randomly if it needs to be although normally. It's accessed in ordeWhen the computer first starts running a program it sends an address to RAM to begin retrieving that prograThe RAM address just consists of a series of ones and zeros representing on and off wireRam doesn't do anything with that address though until the CPU also turns on the set or the enable wirIf the enable wire is turned on Ram automatically sends whatever piece of data is at that address back to the CPThat data is then processed by the CPU accordinglOnce the CPU is finished processing that piece of data it then sends another address to RaTurns on the enable wire and gets the next piece of data from Ram this process happens over and over again inside the computeIf the CPU needs to save data to Ram it outputs an addresOutput some data, and then turns on the set wirThe RAM will then overwrite the data at that address with the new datBut what is that data inside Ram because it just looks like a bunch of ones and zeroWell, it's made up of different things some of the most important pieces of data in RAM are the instructionInstructions just tell the CPU to do different thingThere are also numbers inside that data. These are numbers that you might want to add together compare or simply process in some waAnother thing and this is kind of weird that is in the data is addresses aParticular memory addresses in RAM the data itself is also an addresThese addresses can be used for various thingFor instance if you want to output a number to an external device you have to know the address of that particular devicDo you want to send data to the printer, or do you want to send it to the monitor for instanceThere are also letters stored in RAM if you want to show some text on the screeYou would actually store it as a bunch of ones and zeros and RAEach letter is stored as a particular combination of ones and zeros according to a character code these character codes are arbitrarSomeone just decided that this is a lowercase a and this is an uppercase G for instancSo that's what's actually in the data inside Ram noLet's go back to seeing the data is just a bunch of ones and zeros and we'll now move the RAM chiinto the RAM socket on the motherboarWe'll then group the RAM addresses and data togethePull the CPU out of the motherboard and look at what's called the instruction set of the CPAs we saw earlier some of the pieces of data in RAM are instructions and each CPU has its own set of instructions that iUnderstands so there might be a load instruction which loads a number from RAM into the CPAfter a couple of these load instructions there might be an add instruction that adds these two numbers togetheAfter an add instruction might be a store instruction, which saves the result of that addition back out to RAM to be used later oThere might also be a compare instruction after some load instructions, which compares two numbers together to see which one is largeor if they are the samthe compare instruction can be very useful when used in conjunction with what's called a jump iInstruction as we saw earlier the CPU generally requests each piece of data from RAM in order one after the otheSometimes though the programmer wants to jump to an out of order ram address to process some other instructions and memorThe jump if instruction checks to see if a certain condition is true before it jumpIt uses the results of the compare instruction to make this decisioThere is also a regular job instruction that jumps no matter whatfinally there is an out instruction and an in instruction these will output data to an external device like a monitor oInput data from an external device like a keyboarThese two instructions are often used in conjunction with an address like we talked about earlieThere are some other instructions in the CPUs instruction set that these are some of the more commonly seen oneSo as we saw the data in RAM consists of things like instructionnumbers addresses and letterSo let's go through a program. That would use this instruction set to play a guessing gamSo it would load a number like nine into the CPLet's say that the programmer decided that that was the right answeSo he went ahead and put that number into RAM and then comes an end instruction to retrieve the user's guess afteThe in instruction is the address of the keyboard so we can know where we're getting the data froNext comes a comparInstruction that checks to see if those two numbers the one saved by the programmer and the one entered by the user are the samFollowing the compare instruction is a jump if equal instruction which will jump to another address in RAM if those two numberWe just talked about are the samthe jump if equal instruction is immediately followed by a new RAM address iThe two numbers are the samThe CPU jumps to that new address to begin processing its next set of instructions from there if the two numbers are not the samThen the computer ignores the jump if equal instruction and the corresponding address and just keeps goinFollowing the jump to address comes an out instruction with the address for the monitor, then the letter capital And then below that would be the letters U SS. Space again, so guess agaiSo if the user guesses the wrong number the program would tell him to guess agaiAnd then jump back up to the in instruction to retrieve that new guess and then process these instructions all over agaiBy the way the in and instructions used here have been simplified somewhat, but you'll find them covered in more detail in the booSo now let's briefly take a look inside the CPU itself to see how it would process an instructioas we saw earlieThis is the inside of the 6502 CPLet's take away the 6502 wiring and see what's inside the Scot CPThe first component is the control unit which is kind of like a captain in the armit receives its orders from RAM in the form of ainstruction and then breaks that instruction down into specific commands for the other componentOne of the most important components under the command of the control unit is the arithmetic logic unit or ALU for shortThe ALU is what performs all the mathematical operations inside the CPU such as addition subtractioOr even comparison like we saw earlieThe arithmetic logic unit has two inputs will label them input a and input B and assumThey are two numbers from some previous load instructionNow we might want to add those two numbers togetheThe control unit receives that instruction from RAM and then tells the ALU what type of operation to perforThe ALU performs the operation and then outputs the answeSometimes though depending upon the type of instruction the output from the ALU can actually be ignoreFor instance if you have a compare instruction the ALU doesn't need to output an answer insteaIt just needs to tell the control unit how the two numbers compare to each othefor this the ALU useWhat are called flags and they help the control unit decide what to do when it receives the nextInstruction like jump if which we'll see lateFor now though. Let's say that we are working with an instruction that does produce an output. Where does that output actually gowell the eight wires coming out of the ALU would actually run to what is called a register Register is a very simple component whose only job is to store a number temporarilregisters act just like RAM excepThey are inside the CPU making them faster and more useful for storing a number temporarily Wow instructions being processeWhen the ALU sends the output to the register it won't actually be saved untithe control unit turns on the registers set wirThe set wire is just like the one we saw earlier for RAWhen the set wire is turned on the register saves whatever number is on its input wireOnce we have the output saved in the register though. How do we get it back outWell when we are ready to move a number out of the register we need another control wire called the enable wirThat also runs from the control unit to the register as soon as the control unit turns the enable wire oThe register will output whatever number is saved insidthe output wires of the register then connect to what is called the CPU bus Bus as we saw earlier on the motherboard is simply a group of wires that connect multiple components inside a computer oThe bus are some other registers with their own set and enable wireThese may have numbers from previous instructions already saved insidso the control unit will then turn on the set wire of thParticular register that it wants to save that number to and that number will be saved in that registeAfterward the control unit will then turn off the enable wire from the first register and clear the buThe four registers at the top are just used for storing numbers between operationSo they have output wires that go directly back onto the buSo now we have moved a number from one register to another just by turning some wires on and ofThat's the advantage of the bus easily moving numbers between componentThe disadvantage of the bus is that you can only have one number on it at a timBecause of this limitation the arithmetic logic unit uses a temporary register for input When the control unit is processing an instruction involving the ALU it will move one of the inputs to the temporary registeThe temporary register has no need for an enable wire since it only outputs to the ALU and doesn't conflict with any other registerThe other input to the ALU comes directly from the buThe control unit will enable another register and that number will become input a to the ALU thanumber stays on the bus until the ALU is finished processing the instruction anSo now there are two inputs to the ALU and we're ready for the ALU to perform an operatioAs we saw earlier the control unit knows what operation to tell the ALU to perforBecause of the instruction it receives from riThe instruction itself is an another register called the instruction registeBy the way the input wires from the bus won't affect this register since the instruction was already saved in a previous steThis register like the temporary register, also has no need for an enable wire since it just outputs to the control uniBased upon this instruction the control unit then tells the ALU what type of operation to perforSo let's say the instruction. We're processing is a compare instruction with the compare instructioWe're not interested in the number that is output by the ALWe only want to know how the two inputs compare to each otheFor that we use the flags that we talked about earlieEach flag is just a wire that turns on or off depending upon whether or not a certain condition is truInside the Scott CPU there are four flags, and we'll look at two of them noThe a is larger flag will turn on if input a is larger than input if the inputs are the samThen the equal flag turns on and if both of these flags are off that means input B is largeBut in this case the equal flag is on so that means both inputs are the samOnce the compare instruction is over. We still need to use the flags for the next instructioSo we'll save them to a register that only has 4 inputs and four outputs one for each flaOnce the flags are set into the flags register the CPU is finished with the compare instruction and it can then request the nexinstruction from REgenerally the next instruction after a compare instruction is a jump iinstruction this combination of a compare and a jump if instruction by the way is very common iprogramminAnytime there is more than one possible path through a program the computer is using these instructions to tell it which way to gSo now that we're finished with the compare instruction and our flags are saved in the flags registeWe need to tell RAM that we're ready for the next piece of data in this case the next instructioSo inside the CPU another register that is very important is what's called the instruction address registeThe CPU uses this register to know where the next instruction should come from in RAWhen the CPU is ready for the next instruction it enables the instruction address register onto the bueventually the instruction address will flow to RABut it doesn't get there directly there is an intermediary register called the memory address registeWhose only job is to tell Ram what memory address the CPU wants next since it won't always be an instructioOnce the instruction address is set into the memory address register it has automatically sent to RAM since the memory address registeDoesn't have an enable wire the control unit then turns on the enable Ram wire and RAAutomatically sends back the data at that address which in this case is an instructioThat instruction is then saved in the instruction register, and the control unit begins processing it iThis case it's a jump if equal instruction, which checks to see if the equal flag is oIt does that by running one of its wires and the equal flag wire into a NAND gatIf both inputs to the and gate or on then the output wire turns on as welThis output wire will then trigger the jump thajump eventually retrieves the next piece of data from RAWhich happens to be an address and move it into the instruction address registewhen the jump if equal instruction is over the CPU then processes the instruction at that new address aThat new address, maybe some instructions that output the text you guessed correctly onto the screen because now we know that the user guessed correctlSo we'll add the final four wires to our CPU which are used to control the external devices like the monitor and keyboarWe now have a nearly complete picture of what the Scotts CPU looks likData moves around inside the CPU using the bus and is stored in each register according to how that data is going to be useEach instruction that we have seen can be processed by the Scott CPU in about 6 clock tickModern CPUs can process multiple instructions per clock tick meaning that the computer you're using to watch this video is likelprocessing tens or even hundreds of billions of instructions per seconthat phenomenal speed is accomplished by using more than one of each component anMaking sure that all the components are active as much as possiblThis makes modern CPUs much more complicated than the Scott CPBut they are still fundamentally doing the same things as the Scott CPSo now let's zoom back out and we can see all the wires that run back out to the pins on the chip oThe right are the set RAM and enable RAM wires oThe top are the RAM address wires on the bottom are the data wires that run to both RAM and the external deviceAnd on the left are the input/output control wireSo let's zoom back out to see the rest of the chiAnd we'll put the cover back on the CPU and put it back in the motherboarUsing the ports on the Left we can now plug in the cables that connect our monitor and our keyboarEach of these ports has an address and that port address is what the CPU uses with an in or an out instructioThat port address by the way is sent using the data bus since the address bus in this computer is only used for RASo we'll zoom out to see how the motherboard fits inside the computer case in the computer case is the last componenwe'll look at which is the hard drive aSoon as the power to the computer is turned off all the data and RAM is lost so you have to have a way tStore it more permanently for that we use a hard drivInside the hard drive is a spinning disk covered in tiny magnets with a small metal arm floating above iThe arm moves around to the different parts of the disk where a different data can be stored and retrieveThe disk and the arm generally move very very quickly, but nowhere near as fast as the CPU can process datFor this reason all the data from the hard drive must first be moved to RAM before it can be processeSo we'll put the hard drive back inside the computer and zoom out here. We can see the prograWe just ran and the message telling the user that he guessed correctlSo now you've seen the very basics of how a computer processes informatioYou'll find much more about the Scot CPU in the book at the website, but how do it know commAlso, there are a few small differences between the book and the videBut 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
تمامی حقوق محقوظ است. ساخته شده با Elham ExA2040