
2e6300bcc5489c09cac133f30b12a900.ppt
- Количество слайдов: 28
Control units • • • In the last lecture, we introduced the basic structure of a control unit, and translated our assembly instructions into a binary representation. Today we fill in the last piece of the processor and build a control unit to convert these binary instructions into datapath signals. At the end of the lecture, we’ll have a complete example processor! December 3, 2003 Control Unit 1
Datapath review • • Set WR = 1 to write one of the registers. DA is the register to save to. AA and BA select the source registers. MB chooses a register or a constant operand. FS selects an ALU operation. MW = 1 to write to memory. MD selects between the ALU result and the RAM output. V, C, N and Z are status bits. WR D DA Register file AA A B constant 1 0 Mux B FS V C N Z A Control Unit MB B ALU ADRS MW DATA Data RAM OUT G 0 1 Mux D December 3, 2003 BA MD 2
Instruction format review • • We have three different instruction formats, each 16 bits long with a seven-bit opcode and nine bits for source registers or constants. The first three bits of the opcode determine the instruction category, while the other four bits indicate the exact instruction. – For ALU/shift instructions, the four bits choose an ALU operation. – For branches, the bits select one of eight branch conditions. – We only support one load, one store and one jump instruction. 15 December 3, 2003 9 8 6 5 Control Unit 3 2 0 3
Block diagram of a processor Program Control Unit • • Control signals Status signals Datapath The control unit connects programs with the datapath. – It converts program instructions into control words for the datapath, including signals WR, DA, AA, BA, MB, FS, MW, MD. – It executes program instructions in the correct sequence. – It generates the “constant” input for the datapath. The datapath also sends information back to the control unit. For instance, the ALU status bits V, C, N, Z can be inspected by branch instructions to alter a program’s control flow. December 3, 2003 Control Unit 4
Where does the program go? • • We’ll use a Harvard architecture, which includes two memory units. – An instruction memory holds the program. – A separate data memory is used for computations. – The advantage is that we can read an instruction and load or store data in the same clock cycle. For simplicity, our diagrams do not show any WR or DATA inputs to the instruction memory. ADRS Instruction RAM ADRS MW Data RAM OUT • • DATA Caches in modern CPUs often feature a Harvard architecture like this. However, there is usually a single main memory that holds both program instructions and data, in a Von Neumann architecture. December 3, 2003 Control Unit 5
Program counter • • A program counter or PC addresses the instruction memory, to keep track of the instruction currently being executed. On each clock cycle, the counter does one of two things. – If Load = 0, the PC increments, so the next instruction in memory will be executed. – If Load = 1, the PC is updated with Data, which represents some address specified in a jump or branch instruction. Data Load PC ADRS Instruction RAM OUT December 3, 2003 Control Unit 6
Instruction decoder • • The instruction decoder is a combinational circuit that takes a machine language instruction and produces the matching control signals for the datapath. These signals tell the datapath which registers or memory locations to access, and what ALU operations to perform. Data Load PC ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW (to the datapath) December 3, 2003 Control Unit 7
Jumps and branches • Finally, the branch control unit decides what the PC’s next value should be. – For jumps, the PC should be loaded with the target address specified in the instruction. – For branch instructions, the PC should be loaded with the target address only if the corresponding status bit is true. – For all other instructions, the PC should just increment. December 3, 2003 V C N Z Control Unit Branch Control PC ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW 8
That’s it! • This is the basic control unit. On each clock cycle: 1. An instruction is read from the instruction memory. 2. The instruction decoder generates the matching datapath control word. 3. Datapath registers are read and sent to the ALU or the data memory. 4. ALU or RAM outputs are written back to the register file. 5. The PC is incremented, or reloaded for branches and jumps. December 3, 2003 V C N Z Control Unit Branch Control PC ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW 9
The whole processor Control Unit V C N Z Datapath WR Branch Control DA Register file AA PC D A B BA constant ADRS Instruction RAM 1 0 Mux B MB OUT Instruction Decoder DA AA BA MB FS MD WR MW FS V C N Z A B ALU Control Unit MW DATA Data RAM OUT G 0 1 Mux D December 3, 2003 ADRS MD 10
Implementing the instruction decoder • • The first thing we’ll look at is how to build the instruction decoder. The instruction decoder’s input is a 16 -bit binary instruction I that comes from the instruction memory. The decoder’s output is a control word for the datapath. This includes: – WR, DA, AA, BA, and MD signals to control the register file. – FS for the ALU operation. – MW for the data memory write enable. – MB for selecting the second operand. We’ll see how these signals are generated for each of the three instruction formats. December 3, 2003 Control Unit ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW 11
MB, MD, WR and MW • The following table shows the correct signals MB, MD, WR and MW for each of the eight different instruction categories we defined. D WR Register file DA A B BA AA constant 1 0 Mux B FS V C N Z • A B ALU MB MW DATA Data RAM OUT G 0 1 Mux D ADRS MD As mentioned last time, this is the sense in which these categories contain “similar” instructions. December 3, 2003 Control Unit 12
MB, MD, WR and MW • The following table shows the correct signals MB, MD, WR and MW for each of the eight different instruction categories we defined. • As mentioned last time, this is the sense in which these categories contain “similar” instructions. December 3, 2003 Control Unit 13
Eight categories of instructions • There are several patterns visible in this table. – MW = 1 only for memory write operations. – MB = 1 only for immediate instructions, which require a constant. – MD is unused when WR = 0. – Jumps and branches modify neither registers nor main memory. December 3, 2003 Control Unit 14
Generating MB, MD, WR, and MW • Because of the way we defined our opcodes, the four control signals MB, MD, WR and MW can be expressed as functions of the first three opcode bits, or instruction bits I 15, I 14 and I 13. MB = I 15 MD = I 14 WR = I 14’ + I 15’ I 13 December 3, 2003 MW = I 15’ I 14 I 13’ Control Unit 15
Generating FS • • Yesterday, we used an ALU function selector as the last four bits in the opcode of ALU and shift instructions. For example, a register-based XOR has the opcode 0001100. – The first three bits 000 indicate a register-based ALU instruction. – 1100 is the ALU code for the XOR function. Thus, the control unit can “generate” the ALU’s FS control signal just by taking it directly out of the instruction opcode. For register and immediate-format instructions: 5 FS December 3, 2003 FS 4 FS 3 FS 2 FS 1 FS 0 = I 13 I 12 I 11 I 10 I 9 Control Unit 16
FS for branch instructions • • • FS would be don’t-cares for loads, stores and jumps, which do not involve the ALU. However, FS is required for branch instructions, which depend on the ALU’s status bit outputs. For example, in BZ R 3, -24 • the contents of R 3 must go through the ALU so that Z will be set appropriately. For our branches, we just need the ALU function “G = A” (FS = 00000 or 00111). December 3, 2003 WR D DA Register file AA A B BA constant 1 0 Mux B FS V C N Z Control Unit A MB B ALU ADRS MW Data RAM OUT G 0 1 Mux D DATA MD 17
Generating DA, AA, BA WR DA • Register file AA • D A B BA The register file addresses DA, AA and BA can be taken directly out of the 16 -bit binary instructions. – Instruction bits 8 -6 are the destination register, DA. – Bits 5 -3 are fed directly to AA, the first register file source. – Bits 2 -0 are connected directly to BA, the second source. This clearly works for a register-format instruction where bits 8 -6, 5 -3 and 2 -0 were defined to hold the destination and source registers. 3 DA December 3, 2003 Control Unit 3 AA 3 BA 18
Don’t-care conditions 3 DA • • 3 AA 3 BA In immediate-format instructions, bits 2 -0 store a constant operand, not a second source register! – However, immediate instructions only use one source register, so the control signal BA would be a don’t care condition anyway. – Similarly, jump and branch instructions require neither a destination register nor a second source register. So we can always take DA, AA and BA directly from the instruction. DA 2 DA 1 DA 0 = I 8 I 7 I 6 AA 2 AA 1 AA 0 = I 5 I 4 I 3 BA 2 BA 1 BA 0 = I 2 I 1 I 0 December 3, 2003 Control Unit 19
More about the branch control unit • • • Next, let’s see how to manage the control flow of a program. The branch control unit needs a lot of information about the current instruction. – Whether it’s a jump, a branch, or some other instruction. – For branches and jumps, the target address. – For branches, the specific branch condition. All of this can be generated by the instruction decoder, which has to process the instruction words anyway. December 3, 2003 V C N Z Control Unit Branch Control PC ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW 20
Branch control unit inputs and outputs • • Branch control inputs: – PL, JB, BC and AD are output by the instruction decoder, and carry information about the current instruction. – Status bits V, C, N and Z come from the datapath. – The current PC is needed for PC-relative mode jumps and branches. Branch control outputs: – A Load signal for the PC. – When Load = 1, the branch control unit also generates the target address to jump or branch to. December 3, 2003 V C N Z Control Unit Branch Control PC PL JB BC AD ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW 21
Branch control unit inputs • The decoder sends the following data to the branch control unit: – PL and JB indicate the type of instruction. – BC encodes the kind of branch. – AD determines the jump or branch target address. V C N Z Branch Control PC PL JB BC AD ADRS Instruction RAM OUT Instruction Decoder DA AA BA MB FS MD WR MW December 3, 2003 Control Unit 22
Generating PL and JB • The instruction decoder generates PL and JB from instruction opcodes. – Note that if PL = 0, then the value of JB doesn’t matter. – As expected, PL and JB only matter for jumps and branches. • From this table you could derive: PL = I 15 I 14 December 3, 2003 JB = I 13 Control Unit 23
Generating BC and AD • • We defined the branch opcodes so that they already contain the branch type, so BC can come straight from the instruction. AD can also be taken directly out of the instruction. 3 BC 3 AD BC 2 BC 1 BC 0 = I 11 I 10 I 9 AD 5 AD 4 AD 3 AD 2 AD 1 AD 0 = I 8 I 7 I 6 I 2 I 1 I 0 December 3, 2003 Control Unit 24
Branch control unit V C N Z Branch Control PC PL JB BC AD • • • Now we’ve seen how the instruction decoder generates PL, JB, BC and AD. How does the branch unit use these to control the PC? There are three cases, depending on the values of PL and JB. If PL = 0, the current instruction is not a jump or branch, so the branch control just needs to make the program counter increment, and execute the next instruction. December 3, 2003 Control Unit 25
Jumps V C N Z Branch Control PC PL JB BC AD • • If PL = 1 and JB = 1, the current instruction must be a jump. We assume PC-relative addressing, so the jump “offset” (AD) must be added to the current PC value, and then stored back into the PC. – The branch control unit would contain an adder just for computing the target address. – Again, AD is signed so we can jump forwards or backwards. December 3, 2003 Control Unit 26
Branches V C N Z Branch Control PC PL JB BC AD • • • If PL = 1 and JB = 0, the current instruction is a conditional branch. The branch control unit first determines if the branch should be taken. – It checks the type of branch (BC) and the status bits (VCNZ). – For example, if BC = 011 (branch if zero) and Z = 1, then the branch condition is true and the branch should be taken. Then the branch control unit sets the PC appropriately. – If the branch is taken, AD is added to the PC, just as for jumps. – Otherwise, the PC is incremented, just as for normal instructions. December 3, 2003 Control Unit 27
Summary • • • Today we saw an outline of the control unit hardware. – The program counter points into a special instruction memory, which contains a machine language program. – An instruction decoder looks at each instruction and generates the correct control signals for the datapath and a branching unit. – The branch control unit handles instruction sequencing. The control unit implementation depends on both the instruction set architecture and the datapath. – Careful selection of opcodes and instruction formats can make the control unit simpler. – In MP 4 you’ll design the control unit for a slightly different CPU. We now have a whole processor! This is the culmination of everything we did this semester, starting from those tiny little primitive gates. December 3, 2003 Control Unit 28