42
CENG3420 Lecture 05: Datapath Bei Yu (Latest update: February 7, 2019) Spring 2019 1 / 34

CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

CENG3420Lecture 05: Datapath

Bei Yu

(Latest update: February 7, 2019)

Spring 2019

1 / 34

Page 2: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

The Processor: Datapath & Control

I We’re ready to look at an implementation of the MIPSI Simplified to contain only:

I Memory-reference instructions: lw, swI Arithmetic-logical instructions: add, addu, sub, subu, and, or, xor,

nor, slt, sltuI Arithmetic-logical immediate instructions: addi, addiu, andi, ori, xori,

slti, sltiuI Control flow instructions: beq, j

I Generic implementation:I Use the program counter (PC)I To supply the instruction address and fetch the instruction from

memory (and update the PC)I Decode the instruction (and read registers)I Execute the instruction

FetchPC = PC+4

DecodeExec

2 / 34

Page 3: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Abstract Implementation View

I Two types of functional units:I elements that operate on data values (combinational)I elements that contain state (sequential)

Address Instruction

InstructionMemory

Write Data

Reg Addr

Reg Addr

Reg Addr

Register

File ALUDataMemory

Address

Write Data

Read DataPC

ReadData

ReadData

I Single cycle operationI Split memory (Harvard) model - one memory for instructions and one for data

3 / 34

Page 4: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Fetching Instructions

1. Reading the instruction from the Instruction Memory2. Updating the PC value to be the address of the next (sequential) instruction3. PC is updated every clock cycle, so it does not need an explicit write control signal4. Instruction Memory is read every clock cycle, so it doesn’t need an explicit read control

signal

FetchPC = PC+4

DecodeExec

clock

ReadAddress Instruction

InstructionMemory

Add

PC

4

4 / 34

Page 5: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Decoding Instructions

1. Sending the fetched instruction’s opcode and function field bits to the control unit2. Reading two values from the Register File3. (Register File addresses are contained in the instruction)

FetchPC = PC+4

DecodeExec Instruction

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ControlUnit

5 / 34

Page 6: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Reading Registers “Just in Case”

I Both RegFile read ports are active for all instructions during the Decode cycleI Using the rs and rt instruction field addressesI Since haven’t decoded the instruction yet, don’t know what the instruction isI Just in case the instruction uses values from the RegFile do “work ahead” by reading

the two source operands

QuestionWhich instructions do make use of the RegFile values?

6 / 34

Page 7: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Reading Registers “Just in Case”

I Both RegFile read ports are active for all instructions during the Decode cycleI Using the rs and rt instruction field addressesI Since haven’t decoded the instruction yet, don’t know what the instruction isI Just in case the instruction uses values from the RegFile do “work ahead” by reading

the two source operands

QuestionWhich instructions do make use of the RegFile values?

6 / 34

Page 8: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

EX-1All instructions (except j) use the ALU after reading the registers. Please analyzememory-reference, arithmetic, and control flow instructions.

7 / 34

Page 9: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Executing R Format OperationsR format operations: add, sub, slt, and, or

R-type:31 25 20 15 5 0

op rs rt rd functshamt

10

I Perform operation (op and funct) on values in rs and rtI Store the result back into the Register File (into location rd)I Note that Register File is not written every cycle (e.g. sw), so we need an explicit write

control signal for the Register File

FetchPC = PC+4

DecodeExec

Instruction

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

overflowzero

ALU controlRegWrite

8 / 34

Page 10: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Consider the slt InstructionI Remember the R format instruction slt

slt $t0, $s0, $s1 # if $s0 < $s1# then $t0 = 1# else $t0 = 0

I Where does the 1 (or 0) come from to store into $t0 in the Register File at the end ofthe execute cycle?

Instruction

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

overflowzero

ALU controlRegWrite

9 / 34

Page 11: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Executing Load and Store Operations

I-Type: op rs rt address offset31 25 20 15 0

Load and store operations have toI compute a memory address by adding the base register (in rs) to the 16-bit signed

offset field in the instructionI base register was read from the Register File during decodeI offset value in the low order 16 bits of the instruction must be sign extended to create a

32-bit signed valueI store value, read from the Register File during decode, must be written to the Data

MemoryI load value, read from the Data Memory, must be stored in the Register File

10 / 34

Page 12: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Executing Load and Store Operations (cont.)

Instruction

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

overflowzero

ALU controlRegWrite

DataMemory

Address

Write Data

Read Data

SignExtend

MemWrite

MemRead

16 32

11 / 34

Page 13: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Executing Branch Operations

I-Type: op rs rt address offset31 25 20 15 0

Branch operations have toI compare the operands read from the Register File during decode (rs and rt values)

for equality (zero ALU output)I compute the branch target address by adding the updated PC to the sign

extended16-bit signed offset field in the instructionI “base register” is the updated PCI offset value in the low order 16 bits of the instruction must be sign extended to create a

32-bit signed value and then shifted left 2 bits to turn it into a word address

12 / 34

Page 14: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Executing Branch Operations (cont.)

Instruction

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

zero

ALU control

SignExtend16 32

Shiftleft 2

Add

4 Add

PC

Branchtargetaddress

(to branch control logic)

13 / 34

Page 15: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Executing Jump Operations

J-Type: op31 25 0

jump target address

I Jump operations have to replace the lower 28 bits of the PC with the lower 26 bits ofthe fetched instruction shifted left by 2 bits

ReadAddress Instruction

InstructionMemory

Add

PC

4

Shiftleft 2

Jumpaddress

26

4

28

14 / 34

Page 16: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Creating a Single Datapath from the Parts

I Assemble the datapath elements, add control lines as needed, and design the controlpath

I Fetch, decode and execute each instruction in one clock cycle – single cycle designI no datapath resource can be used more than once per instruction, so some must be

duplicated (e.g., why we have a separate Instruction Memory and Data Memory)I to share datapath elements between two different instruction classes will need

multiplexors at the input of the shared elements with control lines to do the selectionI Cycle time is determined by length of the longest path

15 / 34

Page 17: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Multipilier Insertion

ReadAddress Instruction

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovfzero

ALU controlRegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemReadSignExtend16 32

16 / 34

Page 18: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Multipilier Insertion

MemtoReg

ReadAddress Instruction

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovfzero

ALU controlRegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemReadSignExtend16 32

ALUSrc

16 / 34

Page 19: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Clock Distribution

MemtoReg

ReadAddress Instruction

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovfzero

ALU control

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemReadSign

Extend16 32

ALUSrc

System Clock

clock cycle

17 / 34

Page 20: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Adding the Branch Portion

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovfzero

ALU controlRegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemReadSign

Extend16 32

MemtoRegALUSrc

ReadAddress Instruction

InstructionMemory

Add

PC

4 Shiftleft 2

Add

PCSrc

18 / 34

Page 21: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Our Simple Control Structure

I We wait for everything to settle downI ALU might not produce "right answer" right awayI Memory and RegFile reads are combinational (as are ALU, adders, muxes, shifter,

signextender)I Use write signals along with the clock edge to determine when to write to the sequential

elements (to the PC, to the Register File and to the Data Memory)I The clock cycle time is determined by the logic delay through the longest pathI (We are ignoring some details like register setup and hold times)

19 / 34

Page 22: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Summary: Adding the ControlI Selecting the operations to perform (ALU, Register File and Memory read/write)I Controlling the flow of data (multiplexor inputs)I Information comes from the 32 bits of the instruction

I-Type: op rs rt address offset31 25 20 15 0

R-type:31 25 20 15 5 0

op rs rt rd functshamt

10

Observations:I op field always in bits 31-26I address of two registers to be read are always specified by the rs and rt fields (bits

25–21 and 20–16)I base register for lw and sw always in rs (bits 25–21)I address of register to be written is in one of two places:

I in rt (bits 20–16) for lw;I in rd (bits 15–11) for R-type instructions

I offset for beq, lw, and sw always in bits 15–0

20 / 34

Page 23: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Single Cycle Datapath

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr ALU

ovfzero

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

Register

File

ReadData 1

ReadData 2

RegWrite

SignExtend16 32

MemtoRegALUSrc

Shiftleft 2

Add

PCSrc

10

10

1

0

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

21 / 34

Page 24: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Single Cycle Datapath

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr ALU

ovfzero

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

Register

File

ReadData 1

ReadData 2

RegWrite

SignExtend16 32

MemtoRegALUSrc

Shiftleft 2

Add

PCSrc

10

RegDst

0

1

10

1

0

ALUcontrol

ALUOpInstr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

21 / 34

Page 25: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

ALU Control

ALU’s operation based on instruction type and function code∗

ALU control input

Function

0000 and0001 or0010 xor0011 nor0110 add1110 subtract1111 set on less than

∗Notice that we are using different encodings than in the book22 / 34

Page 26: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

EX: ALU ControlControlling the ALU uses of multiple decoding levelsI main control unit generates the ALUOp bitsI ALU control unit generates ALUcontrol bits

Instr op funct ALUOp action ALUcontrollw xxxxxx 00 add 0110sw xxxxxx 00 add 0110beq xxxxxx 01 subtract 1110add 100000 10 add 0110subt 100010 10 subtract 1110and 100100 10 and 0000or 100101 10 or 0001xor 100110 10 xor 0010nor 100111 10 nor 0011slt 101010 10 slt 1111

23 / 34

Page 27: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

ALU Control Truth Table

F5 F4 F3 F2 F1 F0 ALU Op1

ALU Op0

ALU control3

ALU control2

ALU control1

ALU control0

X X X X X X 0 0 0 1 1 0X X X X X X 0 1 1 1 1 0X X 0 0 0 0 1 0 0 1 1 0X X 0 0 1 0 1 0 1 1 1 0X X 0 1 0 0 1 0 0 0 0 0X X 0 1 0 1 1 0 0 0 0 1X X 0 1 1 0 1 0 0 0 1 0X X 0 1 1 1 1 0 0 0 1 1X X 1 0 1 0 1 0 1 1 1 1

24 / 34

Page 28: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

ALU Control Truth Table

F5 F4 F3 F2 F1 F0 ALU Op1

ALU Op0

ALU control3

ALU control2

ALU control1

ALU control0

X X X X X X 0 0 0 1 1 0X X X X X X 0 1 1 1 1 0X X 0 0 0 0 1 0 0 1 1 0X X 0 0 1 0 1 0 1 1 1 0X X 0 1 0 0 1 0 0 0 0 0X X 0 1 0 1 1 0 0 0 0 1X X 0 1 1 0 1 0 0 0 1 0X X 0 1 1 1 1 0 0 0 1 1X X 1 0 1 0 1 0 1 1 1 1

Add/subt Mux control

24 / 34

Page 29: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

ALU Control LogicFrom the truth table can design the ALU Control logic

Instr[3]Instr[2]Instr[1]Instr[0]ALUOp1ALUOp0

ALUcontrol3

ALUcontrol2

ALUcontrol1

ALUcontrol0

25 / 34

Page 30: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Datapath with Control Unit

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

26 / 34

Page 31: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Datapath with Control Unit

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

0

0

1

26 / 34

Page 32: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Datapath with Control Unit

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

0 1

0

26 / 34

Page 33: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Datapath with Control Unit

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

0

1

1

26 / 34

Page 34: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

(Almost) Complete Datapath with Control Unit

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

0

0

0

26 / 34

Page 35: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Main Control Unit

Instr RegDst ALUSrc MemReg RegWr MemRd MemWr Branch ALUOp

R-type000000

1 0 0 1 0 0 0 10

lw100011

0 1 1 1 1 0 0 00

sw101011

X 1 X 0 0 1 0 00

beq000100

X 0 X 0 0 0 1 01

27 / 34

Page 36: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Control Unit LogicInstr[31]Instr[30]Instr[29]Instr[28]Instr[27]Instr[26]

R-type lw sw beqRegDst

ALUSrc

MemtoReg

RegWrite

MemRead

MemWrite

Branch

ALUOp1ALUOp0

28 / 34

Page 37: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Review: Executing Jump Operations

J-Type: op31 25 0

jump target address

I Jump operations have to replace the lower 28 bits of the PC with the lower 26 bits ofthe fetched instruction shifted left by 2 bits

ReadAddress Instruction

InstructionMemory

Add

PC

4

Shiftleft 2

Jumpaddress

26

4

28

29 / 34

Page 38: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

ReadAddress Instr[31-0]

InstructionMemory

Add

PC

4

Write Data

Read Addr 1

Read Addr 2

Write Addr

Register

File

ReadData 1

ReadData 2

ALU

ovf

zero

RegWrite

DataMemory

Address

Write Data

Read Data

MemWrite

MemRead

SignExtend16 32

MemtoReg

ALUSrc

Shiftleft 2

Add

PCSrc

RegDst

ALUcontrol

1

1

1

00

0

0

1

ALUOp

Instr[5-0]

Instr[15-0]

Instr[25-21]

Instr[20-16]

Instr[15 -11]

ControlUnit

Instr[31-26]

Branch

Shiftleft 2

0

1

Jump

32Instr[25-0]

26PC+4[31-28]

28

0 0

0

30 / 34

Page 39: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

EX: Main Control Unit of j

Instr RegDst ALUSrc MemReg RegWr MemRd MemWr Branch ALUOp Jump

R-type000000

1 0 0 1 0 0 0 10 0

lw100011

0 1 1 1 1 0 0 00 0

sw101011

X 1 X 0 0 1 0 00 0

beq000100

X 0 X 0 0 0 1 01 0

j000010

X X X 0 0 0 X XX 1

31 / 34

Page 40: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Single Cycle Implementation Cycle Time

I Unfortunately, though simple, the single cycle approach is not used because it is veryslow

I Clock cycle must have the same length for every instructionI What is the longest path (slowest instruction)?

32 / 34

Page 41: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

EX: Instruction Critical PathsCalculate cycle time assuming negligible delays (for muxes, control unit, sign extend, PCaccess, shift left 2, wires) except:I Instruction and Data Memory (4 ns)I ALU and adders (2 ns)I Register File access (reads or writes) (1 ns)

Instr. I Mem Reg Rd ALU Op D Mem Reg Wr TotalR-typeloadstorebeqjump

4 1 2 1 8

4 1 2 4 1 124 1 2 4 114 1 2 74 4

33 / 34

Page 42: CUHKredCENG3420 Lecture 05: Datapathbyu/CENG3420/2019Spring/... · FetchingInstructions 1.ReadingtheinstructionfromtheInstructionMemory 2.UpdatingthePCvaluetobetheaddressofthenext(sequential)instruction

Single Cycle Disadvantages & Advantages

I Uses the clock cycle inefficiently – the clock cycle must be timed to accommodate theslowest instr

I Especially problematic for more complex instructions like floating point multiplyI May be wasteful of area since some functional units (e.g., adders) must be duplicated

since they can not be shared during a clock cycleI but It is simple and easy to understand

Clk

lw sw Waste

Cycle 1 Cycle 2

34 / 34