Lets Build a Processor

Similar documents
The Processor: Datapath & Control

Multicycle Approach. Designing MIPS Processor

ECE369. Chapter 5 ECE369

Chapter 4 The Processor (Part 2)

ENE 334 Microprocessors

ﻪﺘﻓﺮﺸﻴﭘ ﺮﺗﻮﻴﭙﻣﺎﻛ يرﺎﻤﻌﻣ MIPS يرﺎﻤﻌﻣ data path and ontrol control

Systems Architecture I

Implementing the Control. Simple Questions

Lecture 5 and 6. ICS 152 Computer Systems Architecture. Prof. Juan Luis Aragón

CC 311- Computer Architecture. The Processor - Control

Topic #6. Processor Design

EECE 417 Computer Systems Architecture

Chapter 5: The Processor: Datapath and Control

Processor: Multi- Cycle Datapath & Control

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Data Paths and Microprogramming

CPE 335. Basic MIPS Architecture Part II

Mapping Control to Hardware

Multiple Cycle Data Path

Lecture 5: The Processor

CSE 2021 COMPUTER ORGANIZATION

RISC Processor Design

CSE 2021 COMPUTER ORGANIZATION

LECTURE 6. Multi-Cycle Datapath and Control

Outline. Combinational Element. State (Sequential) Element. Clocking Methodology. Input/Output of Elements

Lecture 8: Control COS / ELE 375. Computer Architecture and Organization. Princeton University Fall Prof. David August

Multicycle conclusion

Chapter 5 Solutions: For More Practice

CENG 3420 Computer Organization and Design. Lecture 06: MIPS Processor - I. Bei Yu

Chapter 3 Arithmetic for Computers

Inf2C - Computer Systems Lecture 12 Processor Design Multi-Cycle

RISC Architecture: Multi-Cycle Implementation

CS/COE0447: Computer Organization

CS/COE0447: Computer Organization

Alternative to single cycle. Drawbacks of single cycle implementation. Multiple cycle implementation. Instruction fetch

Computer Science 141 Computing Hardware

Major CPU Design Steps

Processor (I) - datapath & control. Hwansoo Han

Systems Architecture

RISC Design: Multi-Cycle Implementation

Initial Representation Finite State Diagram. Logic Representation Logic Equations

CENG 3420 Lecture 06: Datapath

Microprogramming. Microprogramming

RISC Architecture: Multi-Cycle Implementation

Points available Your marks Total 100

CO Computer Architecture and Programming Languages CAPL. Lecture 18 & 19

ALUOut. Registers A. I + D Memory IR. combinatorial block. combinatorial block. combinatorial block MDR

ELEC 5200/6200 Computer Architecture and Design Spring 2017 Lecture 4: Datapath and Control

Design of the MIPS Processor

Chapter 4. The Processor. Computer Architecture and IC Design Lab

ECE 313 Computer Organization FINAL EXAM December 14, This exam is open book and open notes. You have 2 hours.

5.7. Microprogramming: Simplifying Control Design 5.7

EECE 417 Computer Systems Architecture

Review: Abstract Implementation View

Ch 5: Designing a Single Cycle Datapath

Initial Representation Finite State Diagram Microprogram. Sequencing Control Explicit Next State Microprogram counter

Computer Architecture Set Four. Arithmetic

Design of the MIPS Processor (contd)

LECTURE 5. Single-Cycle Datapath and Control

Note- E~ S. \3 \S U\e. ~ ~s ~. 4. \\ o~ (fw' \i<.t. (~e., 3\0)

TDT4255 Computer Design. Lecture 4. Magnus Jahre. TDT4255 Computer Design

ECE 3056: Architecture, Concurrency and Energy of Computation. Single and Multi-Cycle Datapaths: Practice Problems

ECE468 Computer Organization and Architecture. Designing a Multiple Cycle Controller

COMPUTER ORGANIZATION AND DESIGN. The Hardware/Software Interface. Chapter 4. The Processor: A Based on P&H

THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY Computer Organization (COMP 2611) Spring Semester, 2014 Final Examination

Processor (multi-cycle)

Introduction. ENG3380 Computer Organization and Architecture MIPS: Data Path Design Part 3. Topics. References. School of Engineering 1

Control & Execution. Finite State Machines for Control. MIPS Execution. Comp 411. L14 Control & Execution 1

Single Cycle Datapath

CS Computer Architecture Spring Week 10: Chapter

Computer Organization & Design The Hardware/Software Interface Chapter 5 The processor : Datapath and control

CSE Computer Architecture I Fall 2009 Lecture 13 In Class Notes and Problems October 6, 2009

Single vs. Multi-cycle Implementation

Control Unit for Multiple Cycle Implementation

CPU Organization (Design)

COMP303 - Computer Architecture Lecture 8. Designing a Single Cycle Datapath

Digital Design & Computer Architecture (E85) D. Money Harris Fall 2007

Microprogrammed Control Approach

Computer Architecture Chapter 5. Fall 2005 Department of Computer Science Kent State University

Single Cycle Datapath

CPE 335 Computer Organization. Basic MIPS Architecture Part I

CSE 2021: Computer Organization Fall 2010 Solution to Assignment # 3: Multicycle Implementation

The Processor. Z. Jerry Shi Department of Computer Science and Engineering University of Connecticut. CSE3666: Introduction to Computer Architecture

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

CSE Lecture In Class Example Handout

4. What is the average CPI of a 1.4 GHz machine that executes 12.5 million instructions in 12 seconds?

Chapter 4. The Processor

CS152 Computer Architecture and Engineering. Lecture 8 Multicycle Design and Microcode John Lazzaro (

Mark Redekopp and Gandhi Puvvada, All rights reserved. EE 357 Unit 15. Single-Cycle CPU Datapath and Control

Using a Hardware Description Language to Design and Simulate a Processor 5.8

CSE 141 Computer Architecture Summer Session Lecture 3 ALU Part 2 Single Cycle CPU Part 1. Pramod V. Argade

Processor Implementation in VHDL. University of Ulster at Jordanstown University of Applied Sciences, Augsburg

Inf2C - Computer Systems Lecture Processor Design Single Cycle

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

The Processor (1) Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture 9: Microcontrolled Multi-Cycle Implementations. Who Am I?

The Big Picture: Where are We Now? EEM 486: Computer Architecture. Lecture 3. Designing a Single Cycle Datapath

ECE 313 Computer Organization EXAM 2 November 9, 2001

CS152 Computer Architecture and Engineering Lecture 13: Microprogramming and Exceptions. Review of a Multiple Cycle Implementation

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

Chapter 4. Instruction Execution. Introduction. CPU Overview. Multiplexers. Chapter 4 The Processor 1. The Processor.

Transcription:

Lets Build a Processor Almost ready to move into chapter 5 and start building a processor First, let s review Boolean Logic and build the ALU we ll need (Material from Appendix B) operation a 32 ALU result 32 b 32 86

Review: Boolean Algebra & Gates Problem: Consider a logic function with three inputs: A, B, and C. Output D is true if at least one input is true Output E is true if exactly two inputs are true Output F is true only if all three inputs are true Show the truth table for these three functions. Show the Boolean equations for these three functions. Show an implementation consisting of inverters, AND, and OR gates. 87

An ALU (arithmetic logic unit) Let's build an ALU to support the andi and ori instructions we'll just build a 1 bit ALU, and use 32 of them operation op a b res a b result Possible Implementation (sum-of-products): 88

Review: The Multiplexor Selects one of the inputs to be the output, based on a control input S A B 0 1 C note: we call this a 2-input mux even though it has 3 inputs! Lets build our ALU using a MUX: 89

Different Implementations Not easy to decide the best way to build something Don't want too many inputs to a single gate Don t want to have to go through too many gates for our purposes, ease of comprehension is important Let's look at a 1-bit ALU for addition: CarryIn a b Sum c out = a b + a c in + b c in sum = a xor b xor c in CarryOut How could we build a 1-bit ALU for add, and, and or? How could we build a 32-bit ALU? 90

Building a 32 bit ALU CarryIn Operation CarryIn Operation a0 b0 CarryIn ALU0 CarryOut Result0 a 0 a1 b1 CarryIn ALU1 CarryOut Result1 1 Result b 2 a2 b2 CarryIn ALU2 CarryOut Result2 CarryOut a31 b31 CarryIn ALU31 Result31 91

What about subtraction (a b)? Two's complement approach: just negate b and add. How do we negate? A very clever solution: Binvert CarryIn Operation a 0 1 Result b 0 1 2 CarryOut 92

Adding a NOR function Can also choose to invert a. How do we get a NOR b? Ainvert Binvert CarryIn Operation a 0 1 0 1 Result b 0 + 2 1 CarryOut 93

Tailoring the ALU to the MIPS Need to support the set-on-less-than instruction (slt) remember: slt is an arithmetic instruction produces a 1 if rs < rt and 0 otherwise use subtraction: (a-b) < 0 implies a < b Need to support test for equality (beq $t5, $t6, $t7) use subtraction: (a-b) = 0 implies a = b 94

Supporting slt Can we figure out the idea? Ainvert Binvert Operation CarryIn Ainvert Binvert Operation CarryIn a 0 1 0 a 0 1 0 1 1 b 0 1 + 2 Result b 0 1 + 2 Result Less 3 Less 3 Set Overflow detection Overflow CarryOut Use this ALU for most significant bit all other bits

Supporting slt Binvert Ainvert Operation CarryIn a0 b0 CarryIn ALU0 Less CarryOut Result0 a1 b1 0 CarryIn ALU1 Less CarryOut Result1 a2 b2 0 CarryIn ALU2 Less CarryOut Result2.. CarryIn. a31 CarryIn Result31 b31 ALU31 Set 0 Less Overflow 96

Test for equality Notice control lines: 0000 = and 0001 = or 0010 = add 0110 = subtract 0111 = slt 1100 = NOR Bnegate Ainvert a0 b0 a1 b1 0 CarryIn ALU0 Less CarryOut CarryIn ALU1 Less CarryOut Result0 Result1 Operation. Zero Note: zero is a 1 when the result is zero! a2 b2 0 CarryIn ALU2 Less CarryOut Result2.. CarryIn.. Result31 a31 CarryIn b31 ALU31 Set 0 Less Overflow 97

Conclusion We can build an ALU to support the MIPS instruction set key idea: use multiplexor to select the output we want we can efficiently perform subtraction using two s complement we can replicate a 1-bit ALU to produce a 32-bit ALU Important points about hardware all of the gates are always working the speed of a gate is affected by the number of inputs to the gate the speed of a circuit is affected by the number of gates in series (on the critical path or the deepest level of logic ) Our primary focus: comprehension, however, Clever changes to organization can improve performance (similar to using better algorithms in software) We saw this in multiplication, let s look at addition now 98

Problem: ripple carry adder is slow Is a 32-bit ALU as fast as a 1-bit ALU? Is there more than one way to do addition? two extremes: ripple carry and sum-of-products Can you see the ripple? How could you get rid of it? c 1 = b 0 c 0 + a 0 c 0 + a 0 b 0 c 2 = b 1 c 1 + a 1 c 1 + a 1 b 1 c 2 = c 3 = b 2 c 2 + a 2 c 2 + a 2 b 2 c 3 = c 4 = b 3 c 3 + a 3 c 3 + a 3 b 3 c 4 = Not feasible! Why? 99

Carry-lookahead adder An approach in-between our two extremes Motivation: If we didn't know the value of carry-in, what could we do? When would we always generate a carry? g i = a i b i When would we propagate the carry? p i = a i + b i Did we get rid of the ripple? c 1 = g 0 + p 0 c 0 c 2 = g 1 + p 1 c 1 c 2 = c 3 = g 2 + p 2 c 2 c 3 = c 4 = g 3 + p 3 c 3 c 4 = Feasible! Why? 100

Use principle to build bigger adders CarryIn a0 b0 a1 b1 a2 b2 a3 b3 CarryIn ALU0 P0 G0 C1 pi gi ci + 1 Result0 3 Carry-lookahead unit a4 b4 a5 b5 a6 b6 a7 b7 CarryIn ALU1 P1 G1 C2 pi + 1 gi + 1 ci + 2 Result4 7 Can t build a 16 bit adder this way... (too big) Could use ripple carry of 4-bit CLA adders Better: use the CLA principle again! a8 b8 a9 b9 a10 b10 a11 b11 CarryIn ALU2 P2 G2 C3 pi + 2 gi + 2 ci + 3 Result8 11 a12 b12 a13 b13 a14 b14 a15 b15 CarryIn ALU3 P3 G3 C4 pi + 3 gi + 3 ci + 4 Result12 15 CarryOut 101

ALU Summary We can build an ALU to support MIPS addition Our focus is on comprehension, not performance Real processors use more sophisticated techniques for arithmetic Where performance is not critical, hardware description languages allow designers to completely automate the creation of hardware! 102

Chapter Five 103

The Processor: Datapath & Control We're ready to look at an implementation of the MIPS Simplified to contain only: memory-reference instructions: lw, sw arithmetic-logical instructions: add, sub, and, or, slt control flow instructions: beq, j Generic Implementation: use the program counter (PC) to supply instruction address get the instruction from memory read registers use the instruction to decide exactly what to do All instructions use the ALU after reading the registers Why? memory-reference? arithmetic? control flow? 104

More Implementation Details Abstract / Simplified View: 4 Add Add Data PC Address Instruction Instruction memory Register # Registers ALU Address Register # Data memory Register # Data Two types of functional units: elements that operate on data values (combinational) elements that contain state (sequential) 105

State Elements Unclocked vs. Clocked Clocks used in synchronous logic when should an element that contains state be updated? Falling edge Clock period cycle time Rising edge 106

An unclocked state element The set-reset latch output depends on present inputs and also on past inputs R Q S Q 107

Latches and Flip-flops Output is equal to the stored value inside the element (don't need to ask for permission to look at the value) Change of state (value) is based on the clock Latches: whenever the inputs change, and the clock is asserted Flip-flop: state changes only on a clock edge (edge-triggered methodology) "logically true", could mean electrically low A clocking methodology defines when signals can be read and written wouldn't want to read a signal at the same time it was being written 108

D-latch Two inputs: the data value to be stored (D) the clock signal (C) indicating when to read & store D Two outputs: the value of the internal state (Q) and it's complement C Q D C D _ Q Q 109

D flip-flop Output changes only on the clock edge D D C D latch Q D C D latch Q Q Q Q C D C Q 110

Our Implementation An edge triggered methodology Typical execution: read contents of some state elements, send values through some combinational logic write results to one or more state elements State element 1 Combinational logic State element 2 Clock cycle 111

Register File Built using D flip-flops Read register number 1 Register 0 Read register number 1 Read register number 2 Read data 1 Register 1... Register n 2 Register n 1 M u x Read data 1 Write register Write data Register file Write Read data 2 Read register number 2 M u x Read data 2 Do you understand? What is the Mux above? 112

Abstraction Make sure you understand the abstractions! Sometimes it is easy to think you do, when you don t Select Select A31 B31 M u x C31 32 A 32 B M u x 32 C A30 B30 M u x. C30. A0 B0 M u x C0 113

Register File Note: we still use the real clock to determine when to write Write Register number n-to-2 n decoder 0 1. C D C Register 0 n 1 n D Register 1. C D Register n 2 Register data C D Register n 1 114

Simple Implementation Include the functional units we need for each instruction Instruction address MemWrite Instruction memory Instruction PC Add a. Instruction memory b. Program counter c. Adder Sum Address Write data Read data Data memory 16 Sign 32 extend MemRead a. Data memory unit b. Sign-extension unit Register numbers Data 5 5 5 Read register 1 Read register 2 Write register Write Data Registers Read data 1 Read data 2 Data 4 ALU operation ALU Zero ALU result Why do we need this stuff? RegWrite a. Registers b. ALU 115

Building the Datapath Use multiplexors to stitch them together PCSrc Add M ux 4 Add ALU result Shift left 2 PC Read address Instruction Instruction memory Read register 1 Read register 2 Registers Write register Write data RegWrite Read data 1 Read data 2 ALUSrc M ux 4 ALU ALU operation Zero ALU result Address Write data MemWrite Read data Data memory MemtoReg M ux 16 Sign 32 extend MemRead 116

Control Selecting the operations to perform (ALU, read/write, etc.) Controlling the flow of data (multiplexor inputs) Information comes from the 32 bits of the instruction Example: add $8, $17, $18 Instruction Format: 000000 10001 10010 01000 00000 100000 op rs rt rd shamt funct ALU's operation based on instruction type and function code 117

Control e.g., what should the ALU do with this instruction Example: lw $1, 100($2) 35 2 1 100 op rs rt 16 bit offset ALU control input 0000 AND 0001 OR 0010 add 0110 subtract 0111 set-on-less-than 1100 NOR Why is the code for subtract 0110 and not 0011? 118

Control Must describe hardware to compute 4-bit ALU control input given instruction type 00 = lw, sw 01 = beq, 10 = arithmetic function code for arithmetic ALUOp computed from instruction type Describe it using a truth table (can turn into gates): 119

0 Add M ux 4 Add ALU result 1 Instruction [31 26] Control RegDst Branch MemRead MemtoReg ALUOp MemWrite ALUSrc RegWrite Shift left 2 PC Read address Instruction [25 21] Read register 1 Read Instruction [20 16] data 1 Read register 2 Zero Instruction 0 [31 0] ALU M Read ALU Write 0 data 2 result Instruction ux Instruction [15 11] register M memory ux 1 Write 1 data Registers Address Write data Read data Data memory 1 M ux 0 Instruction [15 0] 16 Sign 32 extend ALU control Instruction [5 0] Instruction RegDst ALUSrc Memto- Reg Reg Write Mem Read Mem Write Branch ALUOp1 ALUp0 R-format 1 0 0 1 0 0 0 1 0 lw 0 1 1 1 1 0 0 0 0 sw X 1 X 0 0 1 0 0 0 beq X 0 X 0 0 0 1 0 1

Control Simple combinational logic (truth tables) Inputs ALUOp ALUOp0 ALUOp1 ALU control block Op5 Op4 Op3 Op2 Op1 Op0 F (5 0) F3 F2 F1 F0 Operation2 Operation1 Operation0 Operation R-format Iw sw beq Outputs RegDst ALUSrc MemtoReg RegWrite MemRead MemWrite Branch ALUOp1 ALUOpO 121

Our Simple Control Structure All of the logic is combinational We wait for everything to settle down, and the right thing to be done ALU might not produce right answer right away we use write signals along with clock to determine when to write Cycle time determined by length of the longest path State element 1 Combinational logic State element 2 Clock cycle We are ignoring some details like setup and hold times 122

Single Cycle Implementation Calculate cycle time assuming negligible delays except: memory (200ps), ALU and adders (100ps), register file access (50ps) PCSrc Add M ux 4 Add ALU result Shift left 2 PC Read address Instruction Instruction memory Read register 1 Read register 2 Registers Write register Write data RegWrite Read data 1 Read data 2 ALUSrc M ux 4 ALU ALU operation Zero ALU result Address Write data MemWrite Read data Data memory MemtoReg M ux 16 Sign 32 extend MemRead 123

Where we are headed Single Cycle Problems: what if we had a more complicated instruction like floating point? wasteful of area One Solution: use a smaller cycle time have different instructions take different numbers of cycles a multicycle datapath: PC Address Instruction or data Memory Data Instruction register Memory data register Data Register # Registers Register # Register # A B ALU ALUOut 124

Multicycle Approach We will be reusing functional units ALU used to compute address and to increment PC Memory used for instruction and data Our control signals will not be determined directly by instruction e.g., what should the ALU do for a subtract instruction? We ll use a finite state machine for control 125

Multicycle Approach Break up the instructions into steps, each step takes a cycle balance the amount of work to be done restrict each cycle to use only one major functional unit At the end of a cycle store values for use in later cycles (easiest thing to do) introduce additional internal registers PC Instruction Read 0 Address [25 21] register 1 M ux 0 1 Memory MemData Write data Instruction [20 16] Instruction [15 0] Instruction register Instruction [15 0] Memory data register Instruction [15 11] 0 M ux 1 M ux 0 1 Read register 2 Registers Write register Write data 16 Sign 32 extend Read data 1 Read data 2 Shift left 2 A M ux 1 B 0 4 1 M ux 2 3 Zero ALU ALU result ALUOut 126

Instructions from ISA perspective Consider each instruction from perspective of ISA. Example: The add instruction changes a register. Register specified by bits 15:11 of instruction. Instruction specified by the PC. New value is the sum ( op ) of two registers. Registers specified by bits 25:21 and 20:16 of the instruction Reg[Memory[PC][15:11]] <= Reg[Memory[PC][25:21]] op Reg[Memory[PC][20:16]] In order to accomplish this we must break up the instruction. (kind of like introducing variables when programming) 127

Breaking down an instruction ISA definition of arithmetic: Reg[Memory[PC][15:11]] <= Reg[Memory[PC][25:21]] op Reg[Memory[PC][20:16]] Could break down to: IR <= Memory[PC] A <= Reg[IR[25:21]] B <= Reg[IR[20:16]] ALUOut <= A op B Reg[IR[20:16]] <= ALUOut We forgot an important part of the definition of arithmetic! PC <= PC + 4 128

Idea behind multicycle approach We define each instruction from the ISA perspective (do this!) Break it down into steps following our rule that data flows through at most one major functional unit (e.g., balance work across steps) Introduce new registers as needed (e.g, A, B, ALUOut, MDR, etc.) Finally try and pack as much work into each step (avoid unnecessary cycles) while also trying to share steps where possible (minimizes control, helps to simplify solution) Result: Our book s multicycle Implementation! 129

Five Execution Steps Instruction Fetch Instruction Decode and Register Fetch Execution, Memory Address Computation, or Branch Completion Memory Access or R-type instruction completion Write-back step INSTRUCTIONS TAKE FROM 3-5 CYCLES! 130

Step 1: Instruction Fetch Use PC to get instruction and put it in the Instruction Register. Increment the PC by 4 and put the result back in the PC. Can be described succinctly using RTL "Register-Transfer Language" IR <= Memory[PC]; PC <= PC + 4; Can we figure out the values of the control signals? What is the advantage of updating the PC now? 131

Step 2: Instruction Decode and Register Fetch Read registers rs and rt in case we need them Compute the branch address in case the instruction is a branch RTL: A <= Reg[IR[25:21]]; B <= Reg[IR[20:16]]; ALUOut <= PC + (sign-extend(ir[15:0]) << 2); We aren't setting any control lines based on the instruction type (we are busy "decoding" it in our control logic) 132

Step 3 (instruction dependent) ALU is performing one of three functions, based on instruction type Memory Reference: ALUOut <= A + sign-extend(ir[15:0]); R-type: ALUOut <= A op B; Branch: if (A==B) PC <= ALUOut; 133

Step 4 (R-type or memory-access) Loads and stores access memory MDR <= Memory[ALUOut]; or Memory[ALUOut] <= B; R-type instructions finish Reg[IR[15:11]] <= ALUOut; The write actually takes place at the end of the cycle on the edge 134

Write-back step Reg[IR[20:16]] <= MDR; Which instruction needs this? 135

Summary: 136

Simple Questions How many cycles will it take to execute this code? lw $t2, 0($t3) lw $t3, 4($t3) beq $t2, $t3, Label add $t5, $t2, $t3 sw $t5, 8($t3) Label:... #assume not What is going on during the 8th cycle of execution? In what cycle does the actual addition of $t2 and $t3 takes place? 137

0 M ux Instruction [31 26] PC Instruction Read 0 Address [25 21] register 1 1 Memory MemData Write data PCWriteCond Instruction [20 16] Instruction [15 0] Instruction register Instruction [15 0] Memory data register PCWrite IorD MemRead MemWrite MemtoReg IRWrite Instruction [25-0] Outputs Control Op [5 0] Instruction [15 11] 0 M ux 1 0 M ux 1 PCSource ALUOp ALUSrcB ALUSrcA RegWrite RegDst Read register 2 Registers Write register Write data 16 Sign 32 extend Read data 1 Read data 2 A Shift left 2 M ux 1 B 0 4 1 M ux 2 3 26 Shift 28 left 2 PC [31 28] Zero ALU ALU result ALU control Jump address [31 0] ALUOut 0 M 1 ux 2 Instruction [5 0]

Review: finite state machines Finite state machines: a set of states and next state function (determined by current state and the input) output function (determined by current state and possibly input) Current state Next-state function Next state Inputs Clock Output function Outputs We ll use a Moore machine (output based only on current state) 139

Review: finite state machines Example: B. 37 A friend would like you to build an electronic eye for use as a fake security device. The device consists of three lights lined up in a row, controlled by the outputs Left, Middle, and Right, which, if asserted, indicate that a light should be on. Only one light is on at a time, and the light moves from left to right and then from right to left, thus scaring away thieves who believe that the device is monitoring their activity. Draw the graphical representation for the finite state machine used to specify the electronic eye. Note that the rate of the eye s movement will be controlled by the clock speed (which should not be too great) and that there are essentially no inputs. 140

Implementing the Control Value of control signals is dependent upon: what instruction is being executed which step is being performed Use the information we ve accumulated to specify a finite state machine specify the finite state machine graphically, or use microprogramming Implementation can be derived from specification 141

Graphical Specification of FSM Note: don t care if not mentioned asserted if name only otherwise exact value Start Instruction fetch 0 MemRead ALUSrcA = 0 IorD = 0 IRWrite ALUSrcB = 01 ALUOp = 00 PCWrite PCSource = 00 1 Instruction decode/ register fetch ALUSrcA = 0 ALUSrcB = 11 ALUOp = 00 How many state bits will we need? 2 Memory address computation ALUSrcA = 1 ALUSrcB = 10 ALUOp = 00 6 Execution ALUSrcA = 1 ALUSrcB = 00 ALUOp = 10 8 Branch completion ALUSrcA = 1 ALUSrcB = 00 ALUOp = 01 PCWriteCond PCSource = 01 9 Jump completion PCWrite PCSource = 10 3 Memory access Memory access 5 7 R-type completion MemRead IorD = 1 MemWrite IorD = 1 RegDst = 1 RegWrite MemtoReg = 0 4 Memory read completon step RegDst = 1 RegWrite MemtoReg = 0 142

Finite State Machine for Control Implementation: Control logic Inputs Outputs PCWrite PCWriteCond IorD MemRead MemWrite IRWrite MemtoReg PCSource ALUOp ALUSrcB ALUSrcA RegWrite RegDst NS3 NS2 NS1 NS0 5 p O 4 p O 3 p O 2 p O 1 p O 0 p O 3 S 2 S 1 S 0 S Instruction register opcode field State register 143

PLA Implementation If I picked a horizontal or vertical line could you explain it? Op5 Op4 Op3 Op2 Op1 Op0 S3 S2 S1 S0 PCWrite PCWriteCond IorD MemRead MemWrite IRWrite MemtoReg PCSource1 PCSource0 ALUOp1 ALUOp0 ALUSrcB1 ALUSrcB0 ALUSrcA RegWrite RegDst NS3 NS2 NS1 NS0 144

ROM Implementation ROM = "Read Only Memory" values of memory locations are fixed ahead of time A ROM can be used to implement a truth table if the address is m-bits, we can address 2 m entries in the ROM. our outputs are the bits of data that the address points to. m n 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 1 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1 1 1 m is the "height", and n is the "width" 145

ROM Implementation How many inputs are there? 6 bits for opcode, 4 bits for state = 10 address lines (i.e., 2 10 = 1024 different addresses) How many outputs are there? 16 datapath-control outputs, 4 state bits = 20 outputs ROM is 2 10 x 20 = 20K bits (and a rather unusual size) Rather wasteful, since for lots of the entries, the outputs are the same i.e., opcode is often ignored 146

ROM vs PLA Break up the table into two parts 4 state bits tell you the 16 outputs, 2 4 x 16 bits of ROM 10 bits tell you the 4 next state bits, 2 10 x 4 bits of ROM Total: 4.3K bits of ROM PLA is much smaller can share product terms only need entries that produce an active output can take into account don't cares Size is (#inputs #product-terms) + (#outputs #product-terms) For this example = (10x17)+(20x17) = 510 PLA cells PLA cells usually about the size of a ROM cell (slightly bigger) 147

Another Implementation Style Complex instructions: the "next state" is often current state + 1 Control unit PLA or ROM Input Outputs PCWrite PCWriteCond IorD MemRead MemWrite IRWrite BWrite MemtoReg PCSource ALUOp ALUSrcB ALUSrcA RegWrite RegDst AddrCtl 1 State Adder Address select logic ] 0 [5 p O Instruction register opcode field 148

Details Dispatch ROM 1 Dispatch ROM 2 Op Opcode name Value Op Opcode name Value 000000 R-format 0110 100011 lw 0011 000010 jmp 1001 101011 sw 0101 000100 beq 1000 100011 lw 0010 PLA or ROM 101011 sw 0010 1 Adder State Mux 3 2 1 0 AddrCtl 0 Dispatch ROM 2 Dispatch ROM 1 Address select logic State number Address-control action Value of AddrCtl 0 Use incremented state 3 1 Use dispatch ROM 1 1 2 Use dispatch ROM 2 2 3 Use incremented state 3 4 Replace state number by 0 0 5 Replace state number by 0 0 6 Use incremented state 3 7 Replace state number by 0 0 8 Replace state number by 0 0 9 Replace state number by 0 0 Instruction register opcode field 149

Microprogramming Control unit Microcode memory Input Outputs PCWrite PCWriteCond IorD MemRead MemWrite IRWrite BWrite MemtoReg PCSource ALUOp ALUSrcB ALUSrcA RegWrite RegDst AddrCtl Datapath 1 Microprogram counter Adder Address select logic Instruction register opcode field What are the microinstructions? 150

Microprogramming A specification methodology appropriate if hundreds of opcodes, modes, cycles, etc. signals specified symbolically using microinstructions Label ALU control SRC1 SRC2 Register control Memory PCWrite control Sequencing Fetch Add PC 4 Read PC ALU Seq Add PC Extshft Read Dispatch 1 Mem1 Add A Extend Dispatch 2 LW2 Read ALU Seq Write MDR Fetch SW2 Write ALU Fetch Rformat1 Func code A B Seq Write ALU Fetch BEQ1 Subt A B ALUOut-cond Fetch JUMP1 Jump address Fetch Will two implementations of the same architecture have the same microcode? What would a microassembler do? 151

Microinstruction format Field name Value Signals active Comment Add ALUOp = 00 Cause the ALU to add. ALU control Subt ALUOp = 01 Cause the ALU to subtract; this implements the compare for branches. Func code ALUOp = 10 Use the instruction's function code to determine ALU control. SRC1 PC ALUSrcA = 0 Use the PC as the first ALU input. A ALUSrcA = 1 Register A is the first ALU input. B ALUSrcB = 00 Register B is the second ALU input. SRC2 4 ALUSrcB = 01 Use 4 as the second ALU input. Extend ALUSrcB = 10 Use output of the sign extension unit as the second ALU input. Extshft ALUSrcB = 11 Use the output of the shift-by-two unit as the second ALU input. Read Read two registers using the rs and rt fields of the IR as the register numbers and putting the data into registers A and B. Write ALU RegWrite, Write a register using the rd field of the IR as the register number and Register RegDst = 1, the contents of the ALUOut as the data. control MemtoReg = 0 Write MDR RegWrite, Write a register using the rt field of the IR as the register number and RegDst = 0, the contents of the MDR as the data. MemtoReg = 1 Read PC MemRead, Read memory using the PC as address; write result into IR (and lord = 0 the MDR). Memory Read ALU MemRead, Read memory using the ALUOut as address; write result into MDR. lord = 1 Write ALU MemWrite, Write memory using the ALUOut as address, contents of B as the lord = 1 data. ALU PCSource = 00 Write the output of the ALU into the PC. PCWrite PC write control ALUOut-cond PCSource = 01, If the Zero output of the ALU is active, write the PC with the contents PCWriteCond of the register ALUOut. jump address PCSource = 10, Write the PC with the jump address from the instruction. PCWrite Seq AddrCtl = 11 Choose the next microinstruction sequentially. Sequencing Fetch AddrCtl = 00 Go to the first microinstruction to begin a new instruction. Dispatch 1 AddrCtl = 01 Dispatch using the ROM 1. Dispatch 2 AddrCtl = 10 Dispatch using the ROM 2. 152

Maximally vs. Minimally Encoded No encoding: 1 bit for each datapath operation faster, requires more memory (logic) used for Vax 780 an astonishing 400K of memory! Lots of encoding: send the microinstructions through logic to get control signals uses less memory, slower Historical context of CISC: Too much logic to put on a single chip with everything else Use a ROM (or even RAM) to hold the microcode It s easy to add new instructions 153

Microcode: Trade-offs Distinction between specification and implementation is sometimes blurred Specification Advantages: Easy to design and write Design architecture and microcode in parallel Implementation (off-chip ROM) Advantages Easy to change since values are in memory Can emulate other architectures Can make use of internal registers Implementation Disadvantages, SLOWER now that: Control is implemented on same chip as processor ROM is no longer faster than RAM No need to go back and make changes 154

Historical Perspective In the 60s and 70s microprogramming was very important for implementing machines This led to more sophisticated ISAs and the VAX In the 80s RISC processors based on pipelining became popular Pipelining the microinstructions is also possible! Implementations of IA-32 architecture processors since 486 use: hardwired control for simpler instructions (few cycles, FSM control implemented using PLA or random logic) microcoded control for more complex instructions (large numbers of cycles, central control store) The IA-64 architecture uses a RISC-style ISA and can be implemented without a large central control store 155

Pentium 4 Pipelining is important (last IA-32 without it was 80386 in 1985) Control Control I/O interface Chapter 7 Instruction cache Data cache Enhanced floating point and multimedia Integer datapath Control Advanced pipelining hyperthreading support Secondary cache and memory interface Chapter 6 Control Pipelining is used for the simple instructions favored by compilers Simply put, a high performance implementation needs to ensure that the simple instructions execute quickly, and that the burden of the complexities of the instruction set penalize the complex, less frequently used, instructions 2004 Morgan Kaufmann Publishers 156

Pentium 4 Somewhere in all that control we must handle complex instructions Control Control I/O interface Instruction cache Data cache Enhanced floating point and multimedia Integer datapath Control Advanced pipelining hyperthreading support Secondary cache and memory interface Control Processor executes simple microinstructions, 70 bits wide (hardwired) 120 control lines for integer datapath (400 for floating point) If an instruction requires more than 4 microinstructions to implement, control from microcode ROM (8000 microinstructions) Its complicated! 2004 Morgan Kaufmann Publishers 157

Chapter 5 Summary If we understand the instructions We can build a simple processor! If instructions take different amounts of time, multi-cycle is better Datapath implemented using: Combinational logic for arithmetic State holding elements to remember bits Control implemented using: Combinational logic for single-cycle implementation Finite state machine for multi-cycle implementation 158