computer architecture risc design

70
Computer Architecture RISC Design Virendra Singh Associate Professor Computer A rchitecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/ E-mail: [email protected] CP-226:Computer Architecture Lecture 7 (10 Feb 2013)

Upload: others

Post on 11-May-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Architecture RISC Design

Computer ArchitectureRISC Design

Virendra SinghAssociate Professor

Computer Architecture and Dependable Systems LabDepartment of Electrical Engineering

Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/

E-mail: [email protected]

CP-226:Computer ArchitectureLecture 7 (10 Feb 2013)

Page 2: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 2

Example processor: MIPS subsetMIPS Instruction – Subset Arithmetic and Logical Instructions add, sub, or, and, slt

Memory reference Instructions lw, sw

Branch beq, j

Page 3: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 3

simple instructions, all 32 bits wide very structured, no unnecessary baggage only three instruction formats

rely on compiler to achieve performance

functop rs1 rs2 rd shmtop rs1 rd 16 bit addressop 26 bit address

R

I

J

Overview of MIPS

2004 © Morgan Kaufman Publishers

Page 4: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 4

Where Does It All Begin?• In a register called program counter (PC).• PC contains the memory address of the next

instruction to be executed.• In the beginning, PC contains the address of

the memory location where the program begins.

Page 5: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 5

Where is the Program?

Machine codeof program

Memory

Startaddress

Program counter(register)

Processor

Page 6: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 6

How Does It Run?

StartPC has memory address where program begins

Fetch instruction word from memory address in PCand increment PC ← PC + 4 for next instruction

Decode and execute instruction

Programcomplete?

YesNoSTOP

Page 7: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 7

Datapath and ControlDatapath: Memory, registers, adders, ALU, and

communication buses. Each step (fetch, decode, execute)

requires communication (data transfer) paths between

memory, registers and ALU.

Control: Datapath for each step is set up by control signals

that set up dataflow directions on communication buses

and select ALU and memory functions. Control signals are

generated by a control unit consisting of one or more

finite-state machines.

Page 8: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 8

Datapath for Instruction Fetch

PC InstructionMemory

4

Address

Instructionword to

control unitand registers

Add

Page 9: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 9

Register File: A Datapath Component

32 Registers(reg. file)

Writeregister

reg 1 data

reg 2 data

5

5

5

32

32

32

reg 1

reg 2

Readregisters

Write data

RegWritefrom control

Page 10: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 10

Multi-Operation ALU

ALU

3zero

result

overflow

Operationselect

from control

Operation select ALU function

000 AND001 OR010 Add110 Subtract111 Set on less than

zero = 1, when all bits of result are 0

Page 11: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 11

R-Type Instructions

• Also known as arithmetic-logical instructions• add, sub, slt• Example: add $t0, $s1, $s2

– Machine instruction word000000 10001 10010 01000 00000 100000opcode $s1 $s2 $t0 function

– Read two registers– Write one register– Opcode and function code go to control unit that

generates RegWrite and ALU operation code.

Page 12: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 12

Datapath for R-Type Instruction

32 Registers(reg. file)Write reg.

number

5

5

5

32

32$s1

$s2

Readregisternumbers

Write data

ALU

3zero

result

overflow

Operationselect

from control (add)

1000

110

010

0100

0

RegWrite from controlactivated

32$t0

000000 10001 10010 01000 00000 100000 opcode $s1 $s2 $t0 function (add)

Page 13: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 13

Load and Store Instructions

• I-type instructions• lw $t0, 1200 ($t1) # incr. in bytes

100011 01001 01000 0000 0100 1011 0000opcode $t1 $t0 1200

• sw$t0, 1200 ($t1) # incr. in bytes101011 01001 01000 0000 0100 1011 0000opcode $t1 $t0 1200

Page 14: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 14

Datapath for lw Instruction

Operationselect

from control(add)

32 Registers(reg. file)

Write reg.number

5

5

5

32

32$t1

Readregisternumbers

Write data

ALU

3

zeroresult

overflow

0100

101

000

RegWritefrom control

activated

32

Signextend

16

0000

010

0 10

11 0

000

Datamemory

Addr.

Readdata

MemWrite

MemReadactivated

$t0Writedata

100011 01001 01000 0000 0100 1011 0000opcode $t1 $t0 1200

mem. data to $t0

Page 15: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 15

Datapath for sw Instruction

Operationselect

from control(add)

32 Registers(reg. file)

Write reg.number

5

5

5

32

32$t1

$t0

Readregisternumbers

Write data

ALU

3

zeroresult

overflow

0100

1

RegWritefrom control

32

Signextend

16

0000

010

0 10

11 0

000

Datamemory

Addr.

Readdata

$t0 data to mem.

0100

0

MemWrite activated

MemRead

Writedata

101011 01001 01000 0000 0100 1011 0000opcode $t1 $t0 1200

32

Page 16: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 16

Branch Instruction (I-Type)

• beq $s1, $s2, 25 # if $s1 = $s2, advance PC through 25 instructions

16-bits

000100 10001 10010 0000 0000 0001 1001opcode $s1 $s2 25

Note: Can branch within ± 215 words from the current instructionaddress in PC.

Page 17: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 17

Datapath for beq Instruction

Operation select from control (subtract)

32 Registers(reg. file)

Write reg.number

5

5

5

32

32

$s1Readregisternumbers

Write data

ALU

3

zeroresult

overflow

1000

110

010

RegWritefrom control

32Sign

extend

16

0000

000

0 00

01 1

001

Shiftleft 2

Add

000100 10001 10010 0000 0000 0001 1001opcode $s1 $s2 25

$s2

32

32

32

To branchcontrollogic

PC+4From instructionfetch datapath

Branchtarget

32

Page 18: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 18

J-Type Instruction

• j 2500 # jump to instruction 2,50026-bits

000010 0000 0000 0000 0010 0111 0001 00opcode 2,500

0000 0000 0000 0000 0010 0111 0001 0000bits 28-31 from PC+4

32-bit jump address

Page 19: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 19

Datapath for Jump Instruction

PC InstructionMemoryAddress Instruction word to

control and registers

4Add

1mux0

0mux1

opcode (bits 26-31)to control

Shiftleft 2

6

26

Branchaddr.

BranchJump

3232 32

28

4

32

32

PC+4

32

Page 20: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 20

Instr.mem.PC

Add

Reg

. File

Datamem.1

mux

0

1 m

ux 0

0 m

ux 1

4

1 m

ux 0

Signext.

Shiftleft 2

ALUCont.

CO

NTR

OL

opcode

MemWriteMemRead

ALU

Branch

zero

0-15

0-5

11-15

16-20

21-25

26-31

ALU

0 m

ux 1

Shiftleft 2

0-25 Jump

CombinedDatapaths

Reg

Dst MemtoReg

Page 21: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 21

Control Logic

ControlLogic

Instruction

bits 26-31opcode

RegDstJumpBranchMemReadMemtoReg

ALUOpMemWriteALUSrcRegWrite

ALUControl

Instructionbits 0-5funct.

2

to ALU

Page 22: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 22

Control Logic: Truth Table

Instrtype

Inputs: instr. opcode bits Outputs: control signals

31 30 29 28 27 26

RegD

st

Jump

ALU

Src

Mem

toReg

RegW

rite

Mem

Read

Mem

Write

Branch

ALO

Op1

ALU

Op2

R 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0

lw 1 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0

sw 1 0 1 0 1 1 X 0 1 X 0 0 1 0 0 0

beq 0 0 0 1 0 0 X 0 0 X 0 0 0 1 0 1

j 0 0 0 0 1 0 X 1 X X X X X X X X

Page 23: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 23

How Long Does It Take?• Assume control logic is fast and does not affect the

critical timing. Major time delay components are ALU, memory read/write, and register read/write.

• Arithmetic-type (R-type)• Fetch (memory read) 2ns• Register read 1ns• ALU operation 2ns• Register write 1ns• Total 6ns

Page 24: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 24

Time for lw and sw (I-Types)

• ALU (R-type) 6ns• Load word (I-type)

– Fetch (memory read) 2ns– Register read 1ns– ALU operation 2ns– Get data (mem. Read) 2ns– Register write 1ns– Total 8ns

• Store word (no register write) 7ns

Page 25: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 25

Time for beq (I-Type)

• ALU (R-type) 6ns• Load word (I-type) 8ns• Store word (I-type) 7ns• Branch on equal (I-type)

– Fetch (memory read) 2ns– Register read 1ns– ALU operation 2ns– Total 5ns

Page 26: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 26

Time for Jump (J-Type)• ALU (R-type) 6ns• Load word (I-type) 8ns• Store word (I-type) 7ns• Branch on equal (I-type) 5ns• Jump (J-type)

– Fetch (memory read) 2ns– Total 2ns

Page 27: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 27

How Fast Can the Clock Be?

• If every instruction is executed in one clock cycle, then:– Clock period must be at least 8ns to perform the

longest instruction, i.e., lw.– This is a single cycle machine.– It is slower because many instructions take less

than 8ns but are still allowed that much time.

• Method of speeding up: Use multicycledatapath.

Page 28: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 28

A Single Cycle Examplea31...

a2a1a0

b31...

b2b1b0

1-b fulladder

1-b fulladder

1-b fulladder

1-b fulladder s31

.

.

.s2s1s0

0

c32

Delay of 1-bit full adder = 1nsClock period ≥ 32ns

Time of adding words ~ 32nsTime of adding bytes ~ 32ns

Page 29: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 29

A Multicycle Implementationa31...

a2a1a0

b31...

b2b1b0

1-b fulladder s31

.

.

.s2s1s0

c32 FF

Initializeto 0

Shift

Shift

Shift

Delay of 1-bit full adder = 1nsClock period ≥ 1ns

Time of adding words ~ 32nsTime of adding bytes ~ 8ns

Page 30: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 30

Multicycle DatapathPC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Mem

ory

Addr.

Data

One-cycle data transfer paths(need registers to hold data)

4

Page 31: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 31

Multicycle Datapath Requirements

• Only one ALU, since it can be reused.• Single memory for instructions and data.• Five registers added:

– Instruction register (IR)– Memory data register (MDR)– Three ALU registers, A and B for inputs and

ALUOut for output

Page 32: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 32

Multicycle DatapathPC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Mem

ory

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-2021-25

IorD

MemtoRegALUOp

ALU

SrcB

ALU

SrcA

RegDstIRWrite

RegWrite

MemWriteMemRead

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSourcePC

Writ

eet

c.

26-31 toControl

FSM

11-1

5

Page 33: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 33

3 to 5 Cycles for an InstructionStep R-type

(4 cycles)Mem. Ref.

(4 or 5 cycles)Branch type(3 cycles)

J-type(3 cycles)

Instruction fetch IR ← Memory[PC]; PC ← PC+4

Instr. decode/ Reg. fetch

A ← Reg(IR[21-25]); B ← Reg(IR[16-20])ALUOut ← PC + (sign extend IR[0-15]) << 2

Execution, addr. Comp., branch & jump completion

ALUOut ←A op B

ALUOut ←A+sign extend

(IR[0-15])

If (A= =B)then

PC←ALUOut

PC←PC[28-31]||

(IR[0-25]<<2)Mem. Access or R-type completion

Reg(IR[11-15]) ← ALUOut

MDR←M[ALUout] or M[ALUOut]←B

Memory read completion

Reg(IR[16-20]) ← MDR

Page 34: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 34

Cycle 1 of 5: Instruction Fetch (IF)

• Read instruction into IR, M[PC] → IR• Control signals used:

» IorD = 0 select PC» MemRead = 1 read memory» IRWrite = 1 write IR

• Increment PC, PC + 4 → PC• Control signals used:

» ALUSrcA = 0 select PC into ALU» ALUSrcB = 01 select constant 4» ALUOp = 00 ALU adds» PCSource = 00 select ALU output» PCWrite = 1 write PC

Page 35: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 35

Cycle 2 of 5: Instruction Decode (ID)

• Control unit decodes instruction• Datapath prepares for execution

• R and I types, reg 1→ A reg, reg 2 → B reg» No control signals needed

• Branch type, compute branch address in ALUOut» ALUSrcA = 0 select PC into ALU» ALUSrcB = 11 Instr. Bits 0-15 shift 2 into ALU» ALUOp = 00 ALU adds

opcode | reg 1 | reg 2 | reg 3 | shamt | fncode

opcode | reg 1 | reg 2 | word address increment

opcode | word address jump

31-26 25-21 20-16 15-11 10-6 5-0R

I

J

Page 36: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 36

Cycle 3 of 5: Execute (EX)• R type: execute function on reg A and reg B, result in

ALUOut• Control signals used:

» ALUSrcA = 1 A reg into ALU» ALUsrcB = 00 B reg into ALU» ALUOp = 10 instr. Bits 0-5 control ALU

• I type, lw or sw: compute memory address in ALUOut ← A reg + sign extend IR[0-15]

• Control signals used:» ALUSrcA = 1 A reg into ALU» ALUSrcB = 10 Instr. Bits 0-15 into ALU» ALUOp = 00 ALU adds

Page 37: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 37

Cycle 3 of 5: Execute (EX)• I type, beq: subtract reg A and reg B, write ALUOut to PC

• Control signals used:» ALUSrcA = 1 A reg into ALU» ALUsrcB = 00 B reg into ALU» ALUOp = 01 ALU subtracts» If zero = 1, PCSource = 01 ALUOut to PC» If zero = 1, PCwriteCond = 1 write PC» Instruction complete, go to IF

• J type: write jump address to PC ← IR[0-25] shift 2 and four leading bits of PC

• Control signals used:» PCSource = 10» PCWrite = 1 write PC» Instruction complete, go to IF

Page 38: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 38

Cycle 4 of 5: Reg Write/Memory• R type, write destination register from ALUOut

• Control signals used:» RegDst = 1 Instr. Bits 11-15 specify reg.» MemtoReg = 0 ALUOut into reg.» RegWrite = 1 write register» Instruction complete, go to IF

• I type, lw: read M[ALUOut] into MDR• Control signals used:

» IorD = 1 select ALUOut into mem adr.» MemRead = 1 read memory to MDR

• I type, sw: write M[ALUOut] from B reg• Control signals used:

» IorD = 1 select ALUOut into mem adr.» MemWrite = 1 write memory» Instruction complete, go to IF

Page 39: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 39

Cycle 5 of 5: Reg Write

• I type, lw: write MDR to reg[IR(16-20)]• Control signals used:

» RegDst = 0 instr. Bits 16-20 are write reg» MemtoReg = 1 MDR to reg file write input» RegWrite = 1 read memory to MDR» Instruction complete, go to IF

For an alternative method of designing datapath, seeN. Tredennick, Microprocessor Logic Design, the Flowchart Method,Digital Press, 1987.

Page 40: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 40

1-bit Control SignalsSignal name Value = 0 Value =1

RegDst Write reg. # = bit 16-20 Write reg. # = bit 11-15RegWrite No action Write reg. ← Write dataALUSrcA First ALU Operand ← PC First ALU Operand←Reg. AMemRead No action Mem.Data Output←M[Addr.]MemWrite No action M[Addr.]←Mem. Data InputMemtoReg Reg.File Write In ←ALUOut Reg.File Write In ←MDRIorD Mem. Addr. ← PC Mem. Addr. ← ALUOutIRWrite No action IR ← Mem.Data OutputPCWrite No action PC is writtenPCWriteCond No action PC is written if zero(ALU)=1

PCWrite etc.PCWrite

PCWriteCondzero(ALU)

Page 41: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 41

2-bit Control SignalsSignal name Value Action

ALUOp00 ALU performs add01 ALU performs subtract10 Funct. field (0-5 bits of IR ) determines ALU operation

ALUSrcB00 Second input of ALU ← B reg.01 Second input of ALU ← 4 (constant)10 Second input of ALU ← 0-15 bits of IR sign ext. to 32b11 Second input of ALU ← 0-15 bits of IR sign ext. and left shift

2 bits

PCSource00 ALU output (PC +4) sent to PC01 ALUOut (branch target addr.) sent to PC10 Jump address IR[0-25] shifted left 2 bits, concatenated with

PC+4[28-31], sent to PC

Page 42: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 42

Control: Finite State Machine

Instruction decode and register fetch

Memoryaccessinstr.

R-typeinstr.

Branchinstr.

Jumpinstr.

Start

Instruction fetchState 0

State 1

FSM-MFSM-R FSM-B FSM-J

Clockcycle 1

Clockcycle 2

Clockcycles 3-5

Page 43: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 43

State 0: Instruction Fetch (CC1)PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Mem

ory

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-2021-25

IorD=0

MemtoRegALUOp

=00

ALU

SrcB

=01

ALU

SrcA

=0

RegDstIRWrite

=1

RegWrite

MemWriteMemRead = 1

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSource=00PC

Writ

eet

c.=1

Add

26-31 toControl

FSM

Page 44: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 44

State 0 Control FSM Outputs

MemRead =1ALUSrcA = 0IorD = 0IRWrite = 1ALUSrcB = 01ALUOp = 00PCWrite = 1PCSource = 00

Start

State0Instructionfetch

State 1Instruction decode/Register fetch/Branch addr.

Outputs?

Page 45: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 45

State 1: Instr. Decode/Reg. Fetch/ Branch Address (CC2)

PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Mem

ory

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-2021-25

IorD

MemtoRegALUOp

= 00

ALU

SrcB

=11

ALU

SrcA

=0

RegDstIRWrite

RegWrite

MemWriteMemRead

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSourcePC

Writ

eet

c.

Add

26-31 toControl

FSM

Page 46: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 46

State 1 Control FSM Outputs

MemRead =1ALUSrcA = 0IorD = 0IRWrite = 1ALUSrcB = 01ALUOp = 00PCWrite = 1PCSource = 00

Start State0Instructionfetch(IF)

State 1Instruction decode (ID) /Register fetch /Branch addr.

ALUSrcA = 0ALUSrcB = 11ALUOp = 00

FSM-M FSM-R FSM-B FSM-J

Opcode = J-typeOpcode= BEQ

Page 47: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 47

State 1 (Opcode = lw) → FSM-M (CC3-5)PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-2021-25

IorD=1

MemtoReg=1ALUOp

= 00

ALU

SrcB

=10

ALU

SrcA

=1

RegDst=0IRWrite

RegWrite=1

MemWriteMemRead=1

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSourcePC

Writ

eet

c.

Add

26-31 toControl

FSMM

emor

y CC3

CC4

CC5

Page 48: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 48

State 1 (Opcode= sw)→FSM-M (CC3-4)

PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-2021-25

IorD=1

MemtoRegALUOp

= 00

ALU

SrcB

=10

ALU

SrcA

=1

RegDst=0IRWrite

RegWrite

MemWrite=1MemRead

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSourcePC

Writ

eet

c.

Add

26-31 toControl

FSMM

emor

y CC3

CC4

CC4

Page 49: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 49

FSM-M (Memory Access)

ALUSrcA =1ALUSrcB = 10ALUOp = 00

MemRead = 1IorD = 1

RegWrite = 1MemtoReg = 1RegDst = 0

MemWrite = 1IorD = 1

Compute memaddrress

ReadMemory data

Writememory

Writeregister

Opcode

= “lw”

Opcode

= “sw”

To state 0(Instr. Fetch)

From state 1 Opcode = “lw” or “sw”

Page 50: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 50

State 1(Opcode=R-type)→FSM-R (CC3-4)PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-2021-25

IorD

MemtoReg=0ALUOp

= 10

ALU

SrcB

=00

ALU

SrcA

=1

RegDst=0IRWrite

RegWrite

MemWriteMemRead

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSource

PCW

rite

etc.

26-31 toControl

FSMM

emor

y

“funct. code”

11-15 CC3

CC4

Page 51: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 51

FSM-R (R-type Instruction)

ALUSrcA =1ALUSrcB = 00ALUOp = 10

RegWrite = 1MemtoReg = 0RegDst = 1

ALUoperation

Writeregister

To state 0(Instr. Fetch)

From state 1 Opcode = R-type

Page 52: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 52

State 1 (Opcode = beq ) → FSM -B (CC3)PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

Reg

.

A R

eg.

B R

eg. ALU

Reg

iste

r file

Add

r.

Data

4

Signextend

Shiftleft 2

ALUcontrol

0-50-15

16-20

21-25

IorD

MemtoRegALUOp

= 01

ALU

SrcB

=00

ALU

SrcA

=1

RegDstIRWrite

RegWrite

MemWriteMemRead

in1 in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSource01

PCW

rite

etc.

=1

26-31 toControl

FSMM

emor

y

subtract

11-15

zero

CC3

If(zero)

Page 53: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 53

Write PC on “zero”

PCWrite etc.=1PCWrite

PCWriteCond=1

zero=1

Page 54: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 54

FSM-B (Branch)

ALUSrcA =1ALUSrcB = 00ALUOp = 01PCWriteCond=1PCSource=01

Branch condition:If A – B=0

zero = 1

To state 0(Instr. Fetch)

From state 1 Opcode = “beq”

Write PC onbranch condition

Page 55: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 55

State 1 (Opcode = j) → FSM-J (CC3)PC

Inst

r. re

g. (I

R)

Mem

. Dat

a (M

DR

)

ALU

Out

R

eg.

A R

eg.

B R

eg.

Reg

iste

r file

Add

r.

Data

4

Signextend

Shiftleft 2 ALU

control0-50-15

16-2021-25

IorD

MemtoRegALUOp

ALU

SrcB

ALU

SrcA

RegDstIRWrite

RegWrite

MemWriteMemRead

in1in2

outcontrolMUX

Shiftleft 20-25

28-31

PCSource10

PCW

rite

etc.

26-31 toControl

FSM

Mem

ory

11-15

zeroA

LU

CC3

Page 56: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 56

Write PC

PCWrite etc.=1PCWrite=1

PCWriteCond

zero

Page 57: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 57

FSM-J (Jump)

PCWrite=1PCSource=10

To state 0(Instr. Fetch)

From state 1 Opcode = “jump”

Writejump addr. In PC

Page 58: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 58

Control FSM

Instr.decode/reg.fetch/branch

addr.

ALUoperation

Write PCon branchcondition

Writememory

data

Writejump addr.

to PC

Writeregister

Readmemory

data

Instr.fetch/

adv. PC

Computememory

addr.

Writeregister

lw

sw

RB

J

StartState 0 1

23

4 5

6

7

8 9

Page 59: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 59

Control FSM (Controller)

Combinationallogic

FF

FF

FF

FF

16 controloutputs

6 inputs(opcode)

Nextstate

Presentstate

ClockReset

Page 60: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 60

Designing the Control FSM

• Encode states; need 4 bits for 10 states, e.g.,– State 0 is 0000, state 1 is 0001, and so on.

• Write a truth table for combinational logic:

Opcode Present state Control signals Next state000000 0000 0001000110000100 0001. . . . . . . . . . . . . . . .

• Synthesize a logic circuit from the truth table.• Connect four flip-flops between the next state outputs and present

state inputs.

Page 61: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 61

Block Diagram of a Processor

Datapath(PC, register file, registers, ALU)

Controller(Control FSM)

ALUcontrol

Reset

Clock

Mem. Addr.

Mem. write data

Mem. data out

MemWrite

MemRead ALUOp2-bits

func

t.[0

,5]

ALU

Op

3-bi

ts

Opc

ode

6-bi

tsze

ro

Ove

rflo

w

ALU

SrcA

ALU

SrcB

2-bi

tsPC

Sour

ce2-

bits

Reg

Dst

Reg

Writ

e

Mem

toR

eg

IorD

IRW

rite

PCW

rite

PCW

riteC

ond

Page 62: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 62

Exceptions or Interrupts

• Conditions under which the processor may produce incorrect result or may “hang”.– Illegal or undefined opcode.– Arithmetic overflow, divide by zero, etc.– Out of bounds memory address.

• EPC: 32-bit register holds the affected instruction address.

• Cause: 32-bit register holds an encoded exception type. For example,– 0 for undefined instruction– 1 for arithmetic overflow

Page 63: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 63

Implementing ExceptionsPC

Inst

r. re

g. (I

R)

4

ALUcontrol

ALUOp=01

ALU

SrcB

=01

ALU

SrcA

=0

in1

in2

outcontrol

MUX

PCSource11

PCW

rite

etc.

=1

26-31 toControl

FSM

ALU

Subtract

8000 0180(hex)

EPC

Cause

EPCWrite=1

0

1

CauseWrite=1Overflow toControl FSM

32-bitregister

Page 64: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 64

How Long Does It Take? Again

• Assume control logic is fast and does not affect the critical timing. Major time components are ALU, memory read/write, and register read/write.

• Time for hardware operations, suppose• Memory read or write 2ns• Register read 1ns• ALU operation 2ns• Register write 1ns

Page 65: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 65

Single-Cycle Datapath• R-type 6ns• Load word (I-type) 8ns• Store word (I-type) 7ns• Branch on equal (I-type) 5ns• Jump (J-type) 2ns• Clock cycle time = 8ns• Each instruction takes one cycle

Page 66: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 66

Multicycle Datapath• Clock cycle time is determined by the longest

operation, ALU or memory:• Clock cycle time = 2ns

• Cycles per instruction (CPI):• lw 5 (10ns)• sw 4 (8ns)• R-type 4 (8ns)• beq 3 (6ns)• j 3 (6ns)

Page 67: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 67

CPI of a Computer

∑k (Instructions of type k) × CPIkCPI = ——————————————

∑k (instructions of type k)

where

CPIk = Cycles for instruction of type k

Note: CPI is dependent on the instruction mix of theprogram being run. Standard benchmark programsare used for specifying the performance of CPUs.

Page 68: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 68

Example

• Consider a program containing:• loads 25%• stores 10%• branches 11%• jumps 2%• Arithmetic 52%

• CPI= 0.25×5 + 0.10×4 + 0.11×3 + 0.02×3 + 0.52×4

= 4.12 for multicycle datapath• CPI= 1.00 for single-cycle datapath

Page 69: Computer Architecture RISC Design

10 Feb 2013 Computer Architecture@MNIT 69

Multicycle vs. Single-Cycle

Performance ratio = Single cycle time / Multicycle time

(CPI × cycle time) for single-cycle= ———————————————

(CPI × cycle time) for multicycle

1.00 × 8ns= ————— = 0.97

4.12 × 2ns

Single cycle is faster in this case, but remember, performance ratiodepends on the instruction mix.

Page 70: Computer Architecture RISC Design

Thank You

10 Feb 2013 Computer Architecture@MNIT 70