The CPU Central Processing Unit Reminder - how it fits together processor (CPU) memory bus I/O devices 2 What does the CPU do? The CPU is the heart of the computer. it: • Executes programs written in machine code • Performs arithmetic and logic calculations • Controls the bus • Controls the operation of memory and I/O devices, via the bus 3 How does the CPU do this? • When you want the CPU to perform a task, how does it know? • It knows, because it gets operations to perform from memory • It is continually reading machine code instructions, executing them, and going on to the next instruction • This is called the “fetch-execute cycle”, and is what the CPU does nearly all of the time 4 The Fetch-Execute Cycle 1. Read the next machine-code instruction from memory 2. Perform the actions specified by that instruction 5 What actions must be included in a set of machine-code instructions? • Load Data – (from a specified memory or I/O address) • Store Data – (to a specified memory or I/O address) • Operate on Data – arithmetic and logical operations • What else? 6 CPU Registers • The CPU needs to store some data internally • Other data can be stored internally, to speed up operations (memory accesses take a relatively long time) • For this purpose, the CPU has registers • These are simple (typically 8 or 16 bit) storage units inside the CPU 7 Examples of Register Use • The address of the next machine-code instruction • the address-bus value for the current bus cycle • the value of the current instruction • the result of the most recent arithmetic operation 8 What is a register? • A register is a collection of flip-flops! • A flip-flop can store only one bit (a 1 or a 0), so we need a string of them • When a value is written to the flip-flop, it stores that value as long as the power remains on, or until it is overwritten with another value 9 A very simple CPU 10 Some abbreviations MAR Memory Address Register - controls the address bus MBR Memory Buffer Register - interface to data bus (also called MDR: Memory Data Register) IR Instruction Register - contains current instruction (often divided into an op-code and an address) op-code Operation code - the part of a machine-code operation that specifies the operation 11 Some abbreviations PC Acc ALU Program Counter - address of next instruction (don’t get confused by the term “PC”!) Accumulator - holds operands and results of ALU operations Arithmetic Logic Unit - performs addition, subtraction, logical AND, logical OR etc. 12 The Control Unit (1) • Translates each op-code (which is simply a binary number) into a sequence of primitive actions • These actions carry out the operation specified by the op-code 13 The Control Unit (2) Primitive actions are: – switching data pathways on and off – clocking data into and out of registers – selecting ALU functions These actions are carried out by internal control lines (nothing to do with the control bus!) 14 Example: how the CPU executes a command For all commands: • copy PC to MAR; increment PC – program counter contains memory address of next op-code • perform bus read cycle – causes op-code to be read from memory location now held in MAR • copy MBR to IR (op-code) – MBR (input from data bus) will contain the op-code, read from memory; this is copied into the Instruction Register 15 Example: store accumulator to memory This op-code is going to require an address, so we will need to get that next • copy PC to MAR; increment PC – get the memory address for store operation from next memory location • perform bus read cycle • copy MBR to IR (addr) – we now have the op-code and address in the IR • copy IR(addr) to MAR – specify in MAR where we’re going to write to • copy accumulator to MBR – ready to go out on data bus • perform bus write cycle – causes contents of MBR to be written to address held in MAR 16 What’s happening? • Follow the example on the previous two slides, with reference to the CPU diagram 17 Test Yourself! 1. 2. 3. 4. What is the fetch-execute cycle? What are CPU registers used for? What is a flip-flop? Can you identify all the components of the CPU in the diagram given? Can you remember what they do? See next slide… 5. Write down the sequence of operations the CPU has to perform to implement read accumulator from memory 18 Fill in the blanks Possible question: What is the component labelled x? Or: label the blanks 19