MIPS with Pipelining - Weber State Universityfaculty.weber.edu/snaik/ECE3610/14Lec14.pdf · MIPS...

Preview:

Citation preview

EE 3610 Digital Systems Suketu Naik

1

MIPS with

Pipelining

EE 3610: Digital Systems

EE 3610 Digital Systems Suketu Naik

2MIPS: Application

The MIPS is used in

Embedded systems,

Cisco routers

Nintendo 64

Sony PlayStation , Sony PlayStation 2

and Sony PlayStation Portable

Small computing devices

Small consumer electronics and

appliances

Google's Honeycomb (Android 3)

tablet

EE 3610 Digital Systems Suketu Naik

3MIPS Controller: Sequential

Fetch

Decode

Execute

Writeback

Memory

__/load_pc and

write reg_file

opcode=beq

or opcode

bne/load_pc

opcode=jr or j

/load_pc opcode≠jr /_

opcode=jw

or sw /_

opcode=sw/mem_

write and load_pc

Fetch:

PC outputs to memory

Instruction is loaded at the end of the state

Decode: Registers accessed

Execute: ALU computes the result

opcode≠sw/_

EE 3610 Digital Systems Suketu Naik

4MIPS: Five Stages

EE 3610 Digital Systems Suketu Naik

5MIPS: Five Stages

(1) IF: instruction fetch

(2) ID: instruction decode and register read

(3) EX: ALU execution

(4) MEM: data memory read or write

(5) WB: write result back into a register

EE 3610 Digital Systems Suketu Naik

6MIPS: Stage One

(1) IF: instruction fetchFetch the current instruction from memory using

the Program Counter (PC) as the address-add 4 to the PC (MIPS instruction=32 bits=4 bytes)

-and store new address as NPC

PC is a register that

contains the memory

address of the next

instruction

EE 3610 Digital Systems Suketu Naik

7MIPS: Stage One

Q: Why does PC have to be incremented by 4?

A: PC points (contains) to the first byte of the

instruction; each instruction is 4 byes (32 bits)

long

EE 3610 Digital Systems Suketu Naik

8

(2) ID: instruction decode and register read

-Determine which instruction is given

-Fetch the register values

(2 registers in MIPS

instruction set)

-Compare the two

registers and set

the EQUAL flag

if equal

MIPS: Stage Two

EE 3610 Digital Systems Suketu Naik

9

MIPS Instruction Formats

MIPS: Stage Two

EE 3610 Digital Systems Suketu Naik

10

(3) EX: ALU execution

- Memory reference: add the base register and the

offset to form the effective address

- Register-Register instruction: add, multiply,

logic operation

- Register-Immediate

instruction: add,

multiply, logic

operation on first

register and the

immediate value

MIPS: Stage Three

EE 3610 Digital Systems Suketu Naik

11

(4) MEM: data memory read or write

- LOAD or STORE: access memory

- BRANCH: update the PC using either PC or the

output of the ALU operation

- Otherwise do nothing

MIPS: Stage Four

Q: Why do we need Memory in MIPS?

A:

https://www.youtube.com/watch?v=rZev35tJaEY

EE 3610 Digital Systems Suketu Naik

12

(5) WB: - write results back into an appropriate register

MIPS: Stage Five

EE 3610 Digital Systems Suketu Naik

13Example: add $s0, $s1, $s2

EE 3610 Digital Systems Suketu Naik

14Example: lw $t2, 16($s0)

Load will take the data from memory and put it into the register file

Store will take the data from register file and put it into the memory

EE 3610 Digital Systems Suketu Naik

15

PIPELINING

EE 3610 Digital Systems Suketu Naik

16Pipelining: examples

Laundry

Wash Dry Fold Clothes Put Away

Serial

Pipelined

EE 3610 Digital Systems Suketu Naik

17Pipelining: examples

Factory Assembly Line

Good Pipelining: Kia Sportage Assembly Line

Bad Pipelining: Chaplin: Modern Times

EE 3610 Digital Systems Suketu Naik

18PIPELINING: Why do we care?

Serial Microprocessor:

Executes n instructions using s number of stages

in ns clock periods

Total Execution Time = ns

Pipelined Microprocessor:

Executes n instructions using s number of stages

Total Execution Time = s + (n ─ 1)

Examples:

n=30, s=5

Serial Microprocessor 150 clock cycles

Pipelined Microprocessor 5+29=34 clock cycles

EE 3610 Digital Systems Suketu Naik

19MIPS: Five Stages with Pipeline Registers

EE 3610 Digital Systems Suketu Naik

20MIPS: Five Stages with Pipeline Registers

Inter-stage registers are master-slave D flip-flops; the master receives new data from the

previous stage of the instruction while the slave flip-flop provides data to the next stage

EE 3610 Digital Systems Suketu Naik

21Master Slave D-Flipflop

EE 3610 Digital Systems Suketu Naik

22MIPS: Five Stages with Pipeline Registers(1) IF: instruction fetch

IF/ID Register

(2) ID/RF: instruction decode and register fetch

ID/EX Register

(3) EX: ALU execution

EX/MEM Register

(4) MEM: data memory read or write

MEM/WB Register

(5) WB: write result back into a register

EE 3610 Digital Systems Suketu Naik

23MIPS Pipline Execution

EE 3610 Digital Systems Suketu Naik

24Pipelining: Without Controller

EE 3610 Digital Systems Suketu Naik

25Pipelining: With Controller

EE 3610 Digital Systems Suketu Naik

26Pipelining: With Controller

Controller is necessary to switch operations within and

between stages

Control information must be carried as a part of the

instruction, since this information is required at different

stages of the pipeline

This can be done by adding more inter-stage storage

register bits to forward control data yet to be used

EE 3610 Digital Systems Suketu Naik

27MIPS Pipelining: Example Program*

Assembly Code Operation

idle

lw $10, 20($1) $10=$1 + 20

sub $11, $2, $3 $11=$2 - $3

and $12, $4, $5 $12=$4 and $5

or $13, $6, $7 $13=$6 or $7

add $14, $8, $9 $14=$8 + $9

idle

*Ref: Dodge, "Lecture 20: The PIPELINED MIPS PROCESSOR", UT Dallas, 2012

EE 3610 Digital Systems Suketu Naik

28MIPS Pipelining: Idle

EE 3610 Digital Systems Suketu Naik

29MIPS Pipelining: lw $10, 20($1)

EE 3610 Digital Systems Suketu Naik

30MIPS Pipelining: sub $11, $2, $3

EE 3610 Digital Systems Suketu Naik

31MIPS Pipelining: and $12, $4, $5

EE 3610 Digital Systems Suketu Naik

32MIPS Pipelining: or $13, $6, $7

EE 3610 Digital Systems Suketu Naik

33MIPS Pipelining: add $14, $8, $9

EE 3610 Digital Systems Suketu Naik

34MIPS Pipelining: Processing

EE 3610 Digital Systems Suketu Naik

35MIPS Pipelining: Processing

EE 3610 Digital Systems Suketu Naik

36MIPS Pipelining: Processing

EE 3610 Digital Systems Suketu Naik

37MIPS Pipelining: Processing

EE 3610 Digital Systems Suketu Naik

38MIPS Pipelining: Idle

EE 3610 Digital Systems Suketu Naik

39MIPS Pipelining: Summary Pipelining replaces the serial

processor with a row of five

'mini-processors', each capable

of completing one part of each

instruction

A new instruction is started every

clock cycle

Inter-process registers store instruction information (data, write

register, branch conditions) between cycles so that the instructions

are passed between the pipeline stages

When the pipeline is filled with instructions, an instruction

completes every clock cycle

EE 3610 Digital Systems Suketu Naik

40

Hazards

in

Pipelining

EE 3610 Digital Systems Suketu Naik

41MIPS Pipelining: Hazards Hazards occur because data required for executing the

current instruction may not be available

Example:

An instruction in the FETCH cycle

may need data from a register whose value will be changed by an

instruction elsewhere but still in process in the pipeline: e.g. EX,

MEM, or WB cycle)

So FETCH instruction could

access a register and

get incorrect data because

the register data has not

yet been updated by other

instruction(s)

EE 3610 Digital Systems Suketu Naik

42MIPS Pipelining: Types of HazardsTwo Types of Hazards

Data Hazard

Data hazards occur when an instruction needs register contents

for an arithmetic/ logical/memory instruction

Control Hazard

Control hazards occur when a branch instruction is pending and

the data necessary to initiate/bypass the branch is not yet available

in the same sort of scenario

Both occur because an instruction in the ID/RF stage of the

MIPS pipeline needs register data that will be shortly updated

by instructions in the EX or MEM/Bypass, or WB stage

EE 3610 Digital Systems Suketu Naik

43Data Hazards: Example1

Here, last four instructions require data from $2, which is changed

in the first instruction

data in $2 will not be rewritten until cycle 4, so the AND (2nd

instruction) and OR (3rd instruction) will fetch incorrect data from

$2

ADD may not get correct information

SW will be correct

EE 3610 Digital Systems Suketu Naik

44Data Hazards: Example2

lw $1, 4($2) IF ID EX MEM WB

add $3, $1,$4 IF ID EX MEM WB

Load will take the data from memory and put it into the register file

Store will take the data from register file and put it into the memory

“Take the contents of $2 from the register file, add 4 to it, access the memory at

that location, then move the data at that location into $1 in the register file”

EE 3610 Digital Systems Suketu Naik

45Solution: Stalling

One solution is in insert bubbles

This means delaying certain operation in the pipeline

Another solution may require modification in the datapath,

which will raise the hardware cost

Hazards slow down the instruction execution speed

An alternative approach to deal with this is for the compiler

(or the assembler) to insert NOP instructions, or reorder the

instructions

lw $1, 4($2) IF ID EX MEM WB

add $3, $1,$4 IF nop nop nop ID

EE 3610 Digital Systems Suketu Naik

46Hazard and Solution: Example

Problem 14 Practice Test

Recommended