97
ACCESS IC LABORTORY 93-2Digital System Design Graduate Institute of Electronics Engineering, NTU Design a MIPS Processor (2/2) Design a MIPS Processor (2/2) Lecturer: Chihhao Chao Advisor: Prof. An-Yeu Wu 2005/5/13 Friday

Design a MIPS Processor (2)

  • Upload
    others

  • View
    27

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Design a MIPS Processor (2)

ACCESS IC LABORTORY

93-2 Digital System DesignGraduate Institute of Electronics Engineering, NTU

Design a MIPS Processor (2/2)Design a MIPS Processor (2/2)

Lecturer: Chihhao ChaoAdvisor: Prof. An-Yeu Wu2005/5/13 Friday

Page 2: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P2

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions

Page 3: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P3

Digital System Design

Pipelining is Natural!Pipelining is Natural!vPipelining provides a method for executing multiple

instructions at the same time.

vLaundry ExamplevAnn, Brian, Cathy, Dave

each have one load of clothes to wash, dry, and foldvWasher takes 30 minutesvDryer takes 40 minutesv“Folder” takes 20 minutes

A B C D

Page 4: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P4

Digital System Design

vSequential laundry takes 6 hours for 4 loadsvIf they learned pipelining, how long would laundry take?

A

B

C

D

30 40 20 30 40 20 30 40 20 30 40 20

6 PM 7 8 9 10 11 Midnight

Task

Order

Time

Sequential LaundrySequential Laundry

Page 5: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P5

Digital System Design

vPipelined laundry takes 3.5 hours for 4 loads

Task

Order

A

B

C

D

6 PM 7 8 9 10 11 MidnightTime

30 40 40 40 40 20

Pipelined Laundry: Start work ASAPPipelined Laundry: Start work ASAP

Page 6: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P6

Digital System Design

vPipelining doesn’t help latency of single task, it helps throughput of entire workloadvPipeline rate limited by

slowest pipeline stagevMultiple tasks operating

simultaneously using different resourcesvPotential speedup = Number

pipe stagesvUnbalanced lengths of pipe

stages reduces speedupvTime to “fill” pipeline and

time to “drain” it reduces speedupvStall for Dependences

A

B

C

D

6 PM 7 8 9

Task

Order

Time

30 40 40 40 40 20

Pipelining LessonsPipelining Lessons

Page 7: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P7

Digital System Design

The 5 Stages of the Load InstructionThe 5 Stages of the Load Instruction

vIFetch: Instruction FetchvFetch the instruction from the Instruction Memory

vReg/Dec: Registers Fetch and Instruction DecodevExec: Calculate the memory addressvMem: Read the data from the Data MemoryvWr: Write the data back to the register file

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5

IFetch Reg/Dec Exec Mem WrLoad

Page 8: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P8

Digital System Design

Pipeline ExecutionPipeline Execution

vOn a processor multple instructions are in various stages at the same time.vAssume each instruction takes five cycles

IFetch Dcd Exec Mem WB

IFetch Dcd Exec Mem WB

IFetch Dcd Exec Mem WB

IFetch Dcd Exec Mem WB

IFetch Dcd Exec Mem WB

IFetch Dcd Exec Mem WBProgram Flow

Time

Page 9: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P9

Digital System Design

Single Cycle, MultiSingle Cycle, Multi--cycle, Pipelinedcycle, Pipelined

Clk

Cycle 1

Multiple Cycle Implementation:

IFetch Reg Exec Mem Wr

Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10

Load IFetch Reg Exec Mem Wr

IFetch Reg Exec MemLoad Store

Pipeline Implementation:

IFetch Reg Exec Mem WrStore

Clk

Single Cycle Implementation:Load Store Waste

IFetchR-type

IFetch Reg Exec Mem WrR-type

Cycle 1 Cycle 2

Page 10: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P10

Digital System Design

Why Pipeline? Because the Resources Are There!Why Pipeline? Because the Resources Are There!

Instr.

Order

Time (clock cycles)

Inst 0

Inst 1

Inst 2

Inst 4

Inst 3

AL

UIm Reg Dm Reg

AL

UIm Reg Dm Reg

AL

UIm Reg Dm RegA

LUIm Reg Dm Reg

AL

UIm Reg Dm Reg

Page 11: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P11

Digital System Design

Pipelining MIPS ExecutionPipelining MIPS Execution

Page 12: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P12

Digital System Design

Pipeline HazardsPipeline Hazardsv Structural hazardv An occurrence in which a planned instruction cannot execute in

the proper clock cycle because the hardware cannot support the combination of instructions that are set to execute in the given clock cycle.

v Data hazardv Also called pipeline data hazard. An occurrence in which a

planned instruction cannot execute in the proper clock cycle because data that is needed to execute the instruction is not yet available.

v Control hazardv Also called branch hazard. An occurrence in which the proper

instruction cannot execute in the proper clock cycle because theinstruction that was fetched is NOT the one that is needed; that is, the flow of instruction addresses is not what the pipeline expected.

Page 13: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P13

Digital System Design

Data HazardData Hazardv Forwardingv Also called bypassing. A method of resolving a data hazard by

retrieving the missing data element from internal buffers rather than waiting for it to arrive from programmer-visible register or memory.

v Load-use data hazardv A specific form of data hazard in which the data requested by a

load instruction has not yet become available when it is requested.v Pipeline stallv Also called bubble. A stall initiated in order to resolve a hazard

Page 14: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P14

Digital System Design

Control HazardControl HazardvUntaken branchvOne that falls through to the successive instruction. A

taken branch is one that causes transfer to the branch target

vBranch predictionvA method of resolving a branch hazard that assumes a

given outcome for the branch, and proceeds from that assumption rather than waiting to ascertain the actual outcome

Page 15: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P15

Digital System Design

Pipeline Overview SummaryPipeline Overview SummaryvLatency (pipeline)vThe number of stages in a pipeline or the number of

stages between two instructions during execution.vThroughput (pipeline)vThe number of instructions executed per unit time.

Page 16: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P16

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions

Page 17: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P17

Digital System Design

Designing a Pipelined ProcessorDesigning a Pipelined Processorv Examine the datapath and control diagramv Starting with single-or multi-cycle datapath?v Single-or multi-cycle control?

v Partition datapath into stages:v IF (instruction fetch), ID (instruction decode and register file read),

EX (execution or address calculation), MEM (data memory access), WB (write back)

v Associate resources with statesv Ensure that flows do not conflict, or figure out how to

resolvev Assert control in appropriate stage

Page 18: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P18

Digital System Design

Use MultiUse Multi--cycle Execution Stepscycle Execution Steps

But, use single-cycle datapath….. (separate memory, why??)

Page 19: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P19

Digital System Design

Split SingleSplit Single--cycle Datapathcycle Datapath

What to add to split the datapath into stages

Page 20: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P20

Digital System Design

Add Pipeline RegistersAdd Pipeline Registers

v Use registers between stages to carry data and control

(Flip/Flop)

Page 21: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P21

Digital System Design

Consider Consider loadload

v IF: Instruction Fetchv Fetch the instruction from the Instruction Memory

v ID: Instruction Decodev Registers fetch and instruction decode

v EX: Calculate the memory addressvMEM: Read the data from the Data MemoryvWB: Write the data back to the register file

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5

Ifetch Reg/Dec Exec Mem Wrlw

Page 22: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P22

Digital System Design

Pipelining Pipelining loadload

v 5 functional units in the pipeline datapath are:v Instruction Memory for the IFetch stagev Register File’s Read ports (busA and busB) for the Reg/Dec stagev ALU for the Exec stagev Data Memory for the MEM stagev Register File’s Write port (busW) for the WB stage

Clock

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7

Ifetch Reg/Dec Exec Mem Wr1st lw

Ifetch Reg/Dec Exec Mem Wr2nd lw

Ifetch Reg/Dec Exec Mem Wr3rd lw

Page 23: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P23

Digital System Design

IF Stage of IF Stage of load wordload wordv IF/ID= mem[PC] ; PC = PC + 4

Page 24: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P24

Digital System Design

ID Stage of ID Stage of load wordload wordv ID/EX(A)= Reg[IR[25-21]]; ID/EX(B)= Reg[IR[20-16]];v ID/EX = Sign-extension of ID[15:0]

Page 25: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P25

Digital System Design

EX Stage of EX Stage of load wordload wordv EX/MEM = A + sign-ext(IR[15-0]) % address computation

Page 26: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P26

Digital System Design

MEM Stage of MEM Stage of load wordload wordv MEM/WB = mem[ALUout]

Page 27: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P27

Digital System Design

WB Stage of WB Stage of loadloadv Reg[ IR[20-16] ] = MEM/WB

Page 28: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P28

Digital System Design

Pipelined DatapathPipelined Datapath

Page 29: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P29

Digital System Design

The Four Stages of RThe Four Stages of R--typetype

v IF: fetch the instruction from the Instruction Memoryv ID: registers fetch and instruction decodev EX: v ALU operates on the two register operandsv Update PC

vWB: write ALU output back to the register file

Clock

Cycle 1 Cycle 2 Cycle 3 Cycle 4

Ifetch Reg/Dec Exec WrR-type

Page 30: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P30

Digital System Design

Pipelining RPipelining R--type and type and loadload

vWe have a structural hazard:vTwo instructions try to write to the register file at the

same time!vOnly one write port

Clock

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9

Ifetch Reg/Dec Exec WrR-type

Ifetch Reg/Dec Exec WrR-type

Ifetch Reg/Dec Exec Mem WrLoad

Ifetch Reg/Dec Exec WrR-type

Ifetch Reg/Dec Exec WrR-type

We have a problem!

Page 31: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P31

Digital System Design

Important ObservationImportant Observationv Each functional unit can only be used once per

instruction v Each functional unit must be used at the same stage for

all instructions:v Load uses Register File’s write port during its 5th stage

v R-type uses Register File’s write port during its 4th stage

Several ways to solve: 1) forwarding, 2) adding pipeline bubble, 3) making

instructions same length

Ifetch Reg/Dec Exec WrR-type

Ifetch Reg/Dec Exec Mem WrLoad

Page 32: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P32

Digital System Design

Solution 1: Insert BubbleSolution 1: Insert Bubble

v Insert a bubble into the pipeline to prevent two writes at the same cyclev The control logic can be complexv Lose instruction fetch and issue opportunity

v No instruction is started in Cycle 6

Clock

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9

Ifetch Reg/Dec Exec WrR-type

Ifetch Reg/Dec Exec

Ifetch Reg/Dec Exec Mem WrLoad

Ifetch Reg/Dec Exec WrR-type

Ifetch Reg/Dec Exec WrR-type Pipeline

Bubble

Ifetch Reg/Dec Exec WrR-type

Page 33: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P33

Digital System Design

Solution 2: Delay RSolution 2: Delay R--typetype’’s Writes Writev Delay R-type’s register write by one cycle:v R-type also use Reg File’s write port at Stage 5v MEM is a NOP stage: nothing is being done.

Clock

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9

Ifetch Reg/Dec Mem WrR-type

Ifetch Reg/Dec Mem WrR-type

Ifetch Reg/Dec Exec Mem WrLoad

Ifetch Reg/Dec Mem WrR-type

Ifetch Reg/Dec Mem WrR-type

Exec

Exec

Exec

Exec

Ifetch Reg/Dec Mem WrR-type Exec

Page 34: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P34

Digital System Design

The Four Stages of The Four Stages of storestore

v IF: fetch the instruction from the Instruction Memoryv ID: registers fetch and instruction decodev EX: calculate the memory addressv MEM: write the data into the Data Memory

Add an extra stage:v WB: NOP

Cycle 1 Cycle 2 Cycle 3 Cycle 4

Ifetch Reg/Dec Exec MemStore Wr

Page 35: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P35

Digital System Design

The Four Stages of The Four Stages of beqbeq

v IF: fetch the instruction from the Instruction Memoryv ID: registers fetch and instruction decodev EX:

v compares the two register operandv select correct branch target addressv latch into PC

v Add two extra stages:v MEM: NOPv WB: NOP

Cycle 1 Cycle 2 Cycle 3 Cycle 4

Ifetch Reg/Dec Exec MemStore Wr

Page 36: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P36

Digital System Design

Use Graphical Representation for Pipelined Use Graphical Representation for Pipelined MIPSMIPS

v The graph can help to answer questions like:v How many cycles to execute this code?vWhat is the ALU doing during cycle 4?

Page 37: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P37

Digital System Design

Example 1: Cycle 1Example 1: Cycle 1

Page 38: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P38

Digital System Design

Example 1: Cycle 2Example 1: Cycle 2

Page 39: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P39

Digital System Design

Example 1: Cycle 3Example 1: Cycle 3

Page 40: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P40

Digital System Design

Example 1: Cycle 4Example 1: Cycle 4

Page 41: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P41

Digital System Design

Example 1: Cycle 5Example 1: Cycle 5

Page 42: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P42

Digital System Design

Example 1: Cycle 6Example 1: Cycle 6

Page 43: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P43

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions

Page 44: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P44

Digital System Design

Pipeline Control: Control SignalsPipeline Control: Control Signals

Page 45: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P45

Digital System Design

Group Signals According to StagesGroup Signals According to Stagesv Can use control signals of single-cycle CPU

Page 46: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P46

Digital System Design

Data Stationary ControlData Stationary Controlv Pass control signals along just like the datavMain control generates control signals during ID

Page 47: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P47

Digital System Design

Data Stationary Control (cont.)Data Stationary Control (cont.)v Signals for EX (ExtOp, ALUSrc, ...) are used 1 cycle laterv Signals for MEM (MemWr, Branch) are used 2 cycles laterv Signals for WB (MemtoReg, MemWr) are used 3 cycles later

IF/ID R

egister

ID/E

x Register

Ex/M

emR

egister

Mem

/Wr

Register

Reg/Dec Exec Mem

ExtOp

ALUOpRegDst

ALUSrc

BranchMemWr

MemtoRegRegWr

MainControl

ExtOp

ALUOpRegDst

ALUSrc

MemtoRegRegWr

MemtoRegRegWr

MemtoRegRegWr

BranchMemWr Branch

MemWr

Wr

Page 48: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P48

Digital System Design

Datapath with ControlDatapath with Control

Page 49: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P49

Digital System Design

LetLet’’s Try it Outs Try it Out

Sample Assembly Program

lw $10, 20($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

Page 50: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P50

Digital System Design

Example 2: Cycle 1Example 2: Cycle 1

Page 51: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P51

Digital System Design

Example 2: Cycle 2Example 2: Cycle 2

Page 52: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P52

Digital System Design

Example 2: Cycle 3Example 2: Cycle 3

Page 53: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P53

Digital System Design

Example 2: Cycle 4Example 2: Cycle 4

Page 54: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P54

Digital System Design

Example 2: Cycle 5Example 2: Cycle 5

Page 55: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P55

Digital System Design

Example 2: Cycle 6Example 2: Cycle 6

Page 56: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P56

Digital System Design

Example 2: Cycle 7Example 2: Cycle 7

Page 57: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P57

Digital System Design

Example 2: Cycle 8Example 2: Cycle 8

Page 58: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P58

Digital System Design

Example 2: Cycle 9Example 2: Cycle 9

Page 59: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P59

Digital System Design

Summary of Pipeline BasicsSummary of Pipeline BasicsvPipelining is a fundamental conceptvMultiple steps using distinct resourcesvUtilize capabilities of datapath by pipelined instruction

processingØStart next instruction while working on the current oneØLimited by length of longest stage (plus fill/flush)ØNeed to detect and resolve hazards

vWhat makes it easy in MIPS?vAll instructions are of the same lengthvJust a few instruction formatsvMemory operands only in loads and stores

vWhat makes pipelining hard? hazards

Page 60: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P60

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions

Page 61: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P61

Digital System Design

Data HazardsData Hazardsv Order of operand accesses changed by pipelinev Starting next instruction before first is finishedv Dependencies “go backward in time”

Page 62: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P62

Digital System Design

Handling Data HazardsHandling Data HazardsvDetect vResolve remaining onesvCompiler inserts NOPvStallvForward

Page 63: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P63

Digital System Design

Software SolutionSoftware SolutionvHave compiler guarantee no hazardsvWhere do we insert the NOPs?

sub $2, $1, $3and $12, $2, $5or $13, $6, $2add $14, $2, $2sw $15, 100($2)

vProblem: not efficient enough!

Page 64: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P64

Digital System Design

Detecting Data HazardsDetecting Data Hazardsv Hazard conditions:v EX/MEM.RegisterRd = ID/EX.RegisterRs (1a)v EX/MEM.RegisterRd = ID/EX.RegisterRt (1b)vMEM/WB.RegisterRd = ID/EX.RegisterRs (2a)vMEM/WB.RegisterRd = ID/EX.RegisterRt (2b)

v Two optimizations:v Don’t forward if instruction does not write registerà check if RegWrite is assertedv Don’t forward if destination register is $0à check if RegisterRd = 0

Page 65: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P65

Digital System Design

Detecting Data Hazards (cont.)Detecting Data Hazards (cont.)vHazard conditions using control signals:

At EX stage (EX hazard):

l If ( EX/MEM.RegWriteand (EX/MEM.RegRd≠0)and (EX/MEM.RegRd=ID/EX.RegRs )

ForwardA = 10

Page 66: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P66

Digital System Design

Detecting Data Hazards (cont.)Detecting Data Hazards (cont.)vHazard conditions using control signals:vAt MEM stage:

lMEM/WB.RegWriteand (MEM/WB.RegRd≠0)and (MEM/WB.RegRd=ID/EX.RegRs)

v(replace ID/EX.RegRt for ID/EX.RegRs for the other two conditions)

Page 67: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P67

Digital System Design

Resolving Hazards: ForwardingResolving Hazards: Forwardingv Use temporary results, e.g., those in pipeline registers, don’t wait for

them to be written

Page 68: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P68

Digital System Design

Forwarding LogicForwarding Logicv Forwarding: input to ALU from any pipe registers

v Add multiplexors to ALU input v Control forwarding in EX à carry Rs in ID/EX

v Control signals for forwarding:v If both WB and MEM forward, e.g., add $1,$1,$2; add $1,$1,$3; add

$1,$1,$4; => let MEM forward

v EX hazard:Ø if (EX/MEM.RegWrite and (EX/MEM.RegRd≠0) and

(EX/MEM.RegRd=ID/EX.RegRs))ForwardA=10

v MEM hazard:Ø if (MEM/WB.RegWriteand (MEM/WB.RegRd≠0)

and (EX/MEM.RegRd≠ID/EX.Reg.Rs)and (MEM/WB.RegRd=ID/EX.RegRs))

ForwardA=01

(ID/EX.RegRt <-> ID/EX.RegRs, ForwardB <-> ForwardA)

Page 69: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P69

Digital System Design

No ForwardingNo Forwarding

Page 70: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P70

Digital System Design

With ForwardingWith Forwarding

Page 71: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P71

Digital System Design

Pipeline with ForwardingPipeline with Forwarding

Page 72: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P72

Digital System Design

Example 3: Cycle 3Example 3: Cycle 3

Page 73: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P73

Digital System Design

Example 3: Cycle 4Example 3: Cycle 4

Page 74: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P74

Digital System Design

Example 3: Cycle 5Example 3: Cycle 5

Page 75: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P75

Digital System Design

Example 3: Cycle 6Example 3: Cycle 6

Page 76: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P76

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions

Page 77: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P77

Digital System Design

Can't Always ForwardCan't Always Forwardv lw can still cause a hazard:

v if is followed by an instruction to read the loaded reg.

Page 78: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P78

Digital System Design

StallingStallingv Stall pipeline by keeping instructions in same stage and inserting an

NOP instead

Page 79: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P79

Digital System Design

Handling StallsHandling Stallsv Hazard detection unit in ID to insert stall between a

load instruction and its use:if (ID/EX.MemRead and

((ID/EX.RegisterRt= IF/ID.RegisterRs) or(ID/EX.RegisterRt= IF/ID.registerRt))

stall the pipeline for one cycle(ID/EX.MemRead=1 indicates a load instruction)

v How to stall?v Stall instruction in IF and ID: not change PC and IF/ID

=> the stages re-execute the instructionsvWhat to move into EX: insert an NOP by changing EX, MEM,

WB control fields of ID/EX pipeline register to 0l as control signals propagate, all control signals to EX, MEM, WB

are deasserted and no registers or memories are written

Page 80: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P80

Digital System Design

Pipeline with Stalling UnitPipeline with Stalling Unitv Forwarding controls ALU inputs, hazard detection controls PC, IF/ID,

control signals

Page 81: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P81

Digital System Design

Example 4: Cycle 2Example 4: Cycle 2

Page 82: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P82

Digital System Design

Example 4: Cycle 3Example 4: Cycle 3

Page 83: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P83

Digital System Design

Example 4: Cycle 4Example 4: Cycle 4

Page 84: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P84

Digital System Design

Example 4: Cycle 5Example 4: Cycle 5

Page 85: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P85

Digital System Design

Example 4: Cycle 6Example 4: Cycle 6

Page 86: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P86

Digital System Design

Example 4: Cycle 7Example 4: Cycle 7

Page 87: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P87

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions (optional)v (optional)

Page 88: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P88

Digital System Design

Branch HazardsBranch HazardsvWhen decide to branch, other instructions are still in the pipe

Page 89: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P89

Digital System Design

Handling Branch HazardHandling Branch Hazardv Predict branch always not taken

v Need to add hardware for flushing inst. if wrongv Branch decision made at MEM => need to flush inst. in IF, ID, EX by changing

control values to 0

v Reduce delay of taken branch by moving branch execution earlier in the pipelinev Move up branch address calculation to IDv Check branch equality at ID (using XOR) by comparing the two registers read

during IDv Branch decision made at EX => one inst. to flushv Add a control signal, IF.Flush, to zero instruction field of IF/ID => making the

instruction an NOP

v Dynamic branch predictionv Compiler rescheduling, delay branch

Page 90: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P90

Digital System Design

Delayed BranchDelayed Branchv Predict-not-taken + branch decision at ID

=> the following inst. is always executed=> branches take effect 1 cycle later

v 0 clock cycle per branch instruction if can find instruction to put in slot (≅50% of time)

Page 91: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P91

Digital System Design

Pipeline with FlushingPipeline with Flushing

Page 92: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P92

Digital System Design

Example 5: Cycle 3Example 5: Cycle 3

Page 93: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P93

Digital System Design

Example 5: Cycle 4Example 5: Cycle 4

Page 94: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P94

Digital System Design

OutlineOutlinev 6.1 An Overview of Pipeliningv 6.2 A Pipelined Datapathv 6.3 Pipelined Controlv 6.4 Data Hazards and Forwardingv 6.5 Data Hazards and Stallsv 6.6 Branch Hazardsv 6.8 Exceptions

Page 95: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P95

Digital System Design

What about Exceptions?What about Exceptions?v 5 instructions executing in 5 stage pipelinev How to stop the pipeline? restart?vWho caused the interrupt?vWho to serve first, if multiple interrupts at the same time?

v Need to know in which stage an exception can occurStage Problem interrupts occurringIF Page fault; misaligned memory access;

memory-protection violationID Undefined or illegal opcodeEX Arithmetic exceptionMEM Page fault; misaligned memory access; memory

error; mem-protection violation;

Page 96: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P96

Digital System Design

Handling ExceptionsHandling Exceptionsv Suppose overflow occur at add $1,$2,$1v Disable writes of instructions till trap hits WB, e.g., flush

following instructions using IF.Flush, ID.Flush, EX.Flush to cause multiplexorsto zero control signals(overflow exception detected at EX => flush offending instr.)v Force trap instruction into IF, e.g., fetch from 4000 0040hex by

adding 4000 0040hex to PC input MUXv Save address of offending instruction in EPC

vMultiple interrupts: use priority hardware to choose the earliest instruction to interrupt

v External interrupts: flexible in when to interrupt

Page 97: Design a MIPS Processor (2)

Graduate Institute of Electronics Engineering, NTU

P97

Digital System Design

Pipeline with ExceptionPipeline with Exception