building a cpu - swarthmore collegebryce/cs31/f16/slides/w03b_cpu.pdf · data memory we’re...

44
Building a CPU 9/15/16

Upload: others

Post on 25-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

BuildingaCPU9/15/16

Page 2: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

opcode

X

Y

XopY

flags

AbstractionofyourLab3ALU:

Page 3: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

InsideyourLab3ALU:

opcode

X

Y

XopY

flags

+

-

<<

…MUX

FlagLogic

Page 4: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

CircuitsinsidetheALU

• Arithmeticcircuits• Generallyonecircuitforeachpossibleoperation.

• Controlcircuits• Selecttherightoutput• Setappropriateflags

Page 5: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

CircuitsaroundtheALU

• Wheredotheinputscomefrom?• X,Y• opcode

• Wheredotheoutputsgo?• XopY• Flags

Page 6: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Recallfromlasttime…ThreemainclassificationsofHWcircuits:1. ALU:implementarithmetic&logicfunctionality

(ex)addertoaddtwovaluestogether

2. Storage:tostorebinaryvalues(ex)RegisterFile:setofCPUregisters

3. Control:support/coordinateinstructionexecution(ex)fetchthenextinstructiontoexecute

CircuitsarebuiltfromLogicGateswhicharebuiltfromtransistors

HW CircuitsLogic GatesTransistor

Page 7: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Recallfromlasttime…ThreemainclassificationsofHWcircuits:

2. Storage:tostorebinaryvalues(ex)RegisterFile:setofCPUregisters

HW CircuitsLogic GatesTransistor

GivetheCPUa“scratchspace”toperformcalculationsandkeeptrackofthestateitsin.

Page 8: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

MemoryCircuits:StartingSmall

• Storea0or1

• Retrievethe0or1valueondemand(read)

• Setthe0or1valueondemand(write)

Page 9: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

R-SLatch:StoresValueQWhenRanSareboth1:Storeavalue

RandSareneverbothsimultaneously0

• Towriteanewvalue:• SetSto0momentarily(Rstaysat1):towritea1• SetRto0momentarily(Sstaysat1):towritea0

Q (valuestored)

~Q

S

R

R-SLatch

a

b

Page 10: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

GatedDLatchControlsS-Rlatchwriting,ensuresS&Rneverboth0

D:datawewanttostoreWE:write-enable:allowdatatobestored

Latchesusedinregisters(upnext)andSRAM(caches,later)Fast,notverydense,expensive

DRAM:capacitor-based:

Q (valuestored)

~Q

S

R

R-SLatchD

WE

Page 11: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

WhatgetsstoredwhenWE=1?

A. Q=0B. Q=1C. Q=DD. Q=~DE. Somethingelse.

Q

~Q

S

R

D

WE

Page 12: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Registers

• Fixed-sizestorage(8-bit,32-bit,etc.)

• GatedDlatchletsusstoreonebit• ConnectNofthemtothesamewrite-enablewire!

Write-enable:

N-bitinputwires(bus):

N-bitRegisterBit0

Bit1

BitN-1

Page 13: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

“Registerfile”• AsetofregistersfortheCPUtostoretemporaryvalues.

• You(theprogrammer)candirectlyinteractwiththeregisterfile.

• Instructionsofform:• “addR1+R2,storeresultinR3”

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

Page 14: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

MemoryCircuitSummary

• Lotsofabstractiongoingonhere!• Gateshidethedetailsoftransistors.• BuildR-SLatchesoutofgatestostoreonebit.• CombiningmultiplelatchesgivesusN-bitregister.• GroupingN-bitregistersgivesusregisterfile.

• Registerfile’ssimpleinterface:• ReadRx’svalue,useforcalculation• WriteRy’svaluetostoreresult

Page 15: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Recallagain…ThreemainclassificationsofHWcircuits:1. ALU:implementarithmetic&logicfunctionality

(ex)addertoaddtwovaluestogether

2. Storage:tostorebinaryvalues(ex)RegisterFile:setofCPUregisters

3. Control:support/coordinateinstructionexecution(ex)fetchthenextinstructiontoexecute

CircuitsarebuiltfromLogicGateswhicharebuiltfromtransistors

HW CircuitsLogic GatesTransistor

Page 16: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Recallagain…ThreemainclassificationsofHWcircuits:

3. Control:support/coordinateinstructionexecution(ex)fetchthenextinstructiontoexecute

HW CircuitsLogic GatesTransistor

Keeptrackofwhereweareintheprogram.Executeinstruction,movetonext.

Page 17: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

CPUsofar…

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Weknowhowtostoredata(inregisterfile).Weknowhowtoperformarithmeticonit,byfeedingittoALU.Remainingquestions:

Whichregister(s)doweuseasinputtoALU?WhichoperationshouldtheALUperform?Towhichregistershouldwestoretheresult?

Allthisinfocomesfromourcompiledprogram:aseriesofinstructions.

Page 18: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Recall:VonNeumannModel

CPU(ControlandArithmetic)

Input/Output

ProgramandData

Memory

We’rebuildingthis.Ourprogram(instructions)livehere.We’llassumefornowthatwecanaccessitlikeanarray.

0:

1:

2:

3:

4:

N-1:

Mem Addresses(buckets)

Page 19: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

CPUGamePlan

• Fetchinstructionfrommemory

• Decodewhattheinstructionistellingustodo• TelltheALUwhatitshouldbedoing• Findthecorrectoperands

• Executetheinstruction(arithmetic,etc.)

• Storetheresult

Page 20: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

ProgramState

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Let’saddtwomorespecialregisters(notinregisterfile)tokeeptrackofprogram.

ProgramCounter(PC): Memoryaddressofnextinstr 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): Instruction contents(bits)

Page 21: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Fetchinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

LoadIRwiththecontentsofmemoryattheaddressstoredinthePC.

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): InstructionatAddress0

Page 22: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Decodinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

Page 23: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Decodinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

OPCodetellsALUwhichoperationtoperform.

Page 24: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Decodinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

RegisterID#’sspecifyinputarguments.

Page 25: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Executinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

LettheALUdoitsthing.(e.g.,Add)

Page 26: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Storingresults.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

We’vejustcomputedsomething.Wheredoweputit?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

ResultlocationspecifieswheretostoreALUoutput.

Page 27: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Whydoweneedaprogramcounter?Can’twejuststartat0andcountuponeatatimefromthere?

A. Wedon’t,it’sthereforconvenience.

B. SomeinstructionsmightskipthePCforwardbymore

thanone.

C. SomeinstructionsmightadjustthePCbackwards.

D. WeneedthePCforsomeotherreason(s).

Page 28: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Storingresults.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

Resultmightbe:MemoryRegisterPC

Page 29: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

RecapCPUModel

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Fourstages:fetchinstruction,decodeinstruction,execute,storeresult

ProgramCounter(PC): Memoryaddressofnextinstr 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): Instruction contents(bits)

Page 30: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Fetchinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

LoadIRwiththecontentsofmemoryattheaddressstoredinthePC.

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): InstructionatAddress0

Page 31: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Decodinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

Page 32: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Decodinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

OPCodetellsALUwhichoperationtoperform.

Page 33: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Decodinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

RegisterID#’sspecifyinputarguments.

Page 34: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Executinginstructions.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Whatoperation?Whicharguments?

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

LettheALUdoitsthing.(e.g.,Add)

Page 35: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Storingresults.

32-bitRegister#0WEDatain

32-bitRegister#1WEDatain

32-bitRegister#2WEDatain

32-bitRegister#3WEDatain

MUX

MUX

RegisterFile

ALU

Interprettheinstructionbits:Storeresultinregister,memory,PC.

ProgramCounter(PC): Address0 0:

1:

2:

3:

4:

N-1:

(Memory)

InstructionRegister(IR): OPCode|Reg A|Reg B|Result

Resultmightbe:MemoryRegisterPC

Page 36: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Clocking

• Needtoperiodicallytransitionfromoneinstructiontothenext.

• Ittakestimetofetchfrommemory,forsignaltopropagatethroughwires,etc.• Toofast:don’tfullycomputeresult• Tooslow:wastetime

Page 37: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

ClockDrivenSystem• Everythinginisdrivenbyadiscreteclock• clock:anoscillatorcircuit,generateshilowpulse• clockcycle:onehi-lowpair

• Clockdetermineshowfastsystemruns• Processorcanonlydoonethingperclockcycle

• Usuallyjustonepartofexecutinganinstruction• 1GHzprocessor:

1billioncycles/secondà 1cycleeverynanosecond

Clock

1cycle

1 0 1 0 1 0 1 0 1 0

Page 38: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

ClockandCircuitsClockEdgesTriggersevents• Circuitshavecontinuousvalues• RisingEdge:triggernewinputvalues• FallingEdge:consistentoutputreadytoread• Betweenrisingandfallingedgecanhaveinconsistentstateasnewinputvaluesflowthroughcircuit

^ newinput

^ outputready

^ newinput

Clock:

Page 39: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Timeperinstruction:LaundryAnalogy

• Discretestages:fetch,decode,execute,store

• Analogy(laundry):washer,dryer,folding,dresser

W Dy F Dr

4 Hours

Page 40: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

LaundryW Dy F Dr

4 Hours

W Dy F Dr

4 Hours

W Dy F Dr

4 Hours

4-hourcycletime.

Finishesalaundryloadeverycycle.

(6laundryloadsperday)

Page 41: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Pipelining(Laundry)

DyW

FDyW

DrFDyW

DrFDyW

W

1Hour

1st hour:

2nd hour:

3rd hour:

4th hour:

5th hour:

Steadystate:Oneloadfinisheseveryhour!(Noteveryfourhourslikebefore.)

Page 42: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

DF

EDF

SEDF

SEDF

F

1Nanosecond

1st nanosecond:

2nd nanosecond:

3rd nanosecond:

4th nanosecond:

5th nanosecond:

Steadystate:Oneinstructionfinisheseverynanosecond!(Clockratecanbefaster.)

CPUStages:fetch,decode,execute,storeresults

Pipelining(CPU)

Page 43: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Pipelining

(Formoredetailsaboutthisandtheotherthingswetalkedabouthere,takearchitecture.)

Page 44: Building a CPU - Swarthmore Collegebryce/cs31/f16/slides/w03b_CPU.pdf · Data Memory We’re building this. Our program (instructions) live here. We’ll assume for now that ... Load

Comingupnextweek…

• TalkingtotheCPU:Assemblylanguage