Chapter 4 Notes
Chapter 4 Notes
Computer Architecture
Chapter 4
The Processor
Lecture 4.1
The Processor
Processor (CPU): The active part of the computer that does all
the work (data manipulation and decision making)
Datapath: Portion of the processor that contains hardware
necessary to perform operations required by the processor.
Control: Portion of the processor (also in hardware) that tells
the datapath what needs to be done.
Single-Cycle Implementation
Overview of Implementation
1. Send the program counter (PC) to the memory that contains
the code and fetch the instruction from that memory
2. Read one or two registers based on the instruction fields
determining which registers to read. For the load word
instruction we need to read only one register, but most other
instructions require reading two.
Instruction Fetching
The CPU is always in an infinite loop, fetching instructions
from memory and executing them. The program counter (PC) holds
the address of the current instruction, and is incremented to
indicate the next instruction.
CPU Overview
The value written into the PC can come from one of two adders.
Then, the data written into the register file can come from
either the ALU or the data memory. Second input to the ALU can
come from a register or the immediate field of the instruction.
Then, a multiplexer to select from several inputs based on the
setting of its control lines to send to the output. Control
lines are set based on information taken from the instructions
being executed.
Figure 5: CPU circuitry diagram
Building a Datapath
Datapath: elements that process data and addresses in the CPU,
including registers, ALUs, Multiplexers, memory, etc...
ALU Control
The ALU is used for load/store (add), branch (subtract), r-type
instructions (opcode dependent). Assume that the 2-bit ALUOp is
derived from the opcode, while combinational logic will derive
the ALU control.
Figure 6: ALU Control fields
The ALUOp line exiting the control unit is 'thicker' because it
is a 2bit transfer, while the others are 1 bit.
Memories
To fetch instructions and read and write words, we need these
memories to be 32-bits wide. Buses are represented by dark
lines here. Blue lines represent control signals. MemRead and
MemWrite should be set to 1 if the data memory is to be read or
written, and 0 otherwise.
Figure 7: Memory operations
Figure 8: Interpretation of funct7 + funct 3, producing ALU
Control
R-Format Instructions
• Read two register operands
• Perform arithmetic / logical operation
• Write register result
Load/Store Instructions
1. Read register operands
2. Calculate address using a 12-bit offset
(Use ALU, but sign-extend offset)
3. Load: read memory and update register / Store: write
register value to memory
Branch Instructions
1. Read register operands
2. Compare operands
(Use ALU, subtract, and check Zero output)
3. Calculate target address
• sign-extend displacement
• shift left one place (halfword displacement)
• Add to PC value
Pipelining Analogy
Figure 17: Sequential vs. Pipelined Laundry
Laundromat example
If any laundry stage takes 30 min (wash, dry, fold, store) and
the laundromat has 4 of each station (washers, driers, folding
stations, storing stations) then what is the
a) latency: 2 hours (30 x 4 stages) for any given load
b) throughput: 8 loads/hour (in steady state, 2 x 4 processes
completed each hour)
Hazards
Situations which prevent starting the next instruction in the
next cycle
Structure Hazard - a required resource is busy
• Two or more instructions in the pipeline compete for
access to a single resource.
• Solution 1: Some instructions have to stall
• Solution 2: Add more hardware to the machine
Data Hazard (most common) - there are data dependencies between
instructions
• Solve by forwarding (bypassing): retrieve the missing data
element from internal buffers rather than waiting for it
to arrive to program-visible registers or memory
Control Hazard - Deciding on the control action depends on the
previous instruction
Note:
shading on right → reading;
shading on left → writing;
no shading → no memory access
Control Hazards
Branch instructions determine the flow of control. Fetching the
next instruction will depend on the outcome of the branch
instruction. The pipeline can't always fetch the correct
instruction because it would still be on decoding the
instruction.
Branch Prediction
Longer pipelines cannot readily determine the branch outcome
early. The stall penalty becomes unacceptable. As an
alternative, predicting the outcome to the branch, only
stalling if the prediction turned out to be wrong.
The selection of the next value of the PC, choosing between the
incremented PC and the branch address from the MEM stage
Pipelined Registers
Registers are needed between stages to hold info produced from
the previous cycle.
Figure 28: Pipelined Registers
b) Store stages
3. EX
The effective address (reg + offset) is placed in EX/MEM reg
4. MEM
The register containing data to be stored was already read in
an earlier stage (ID/EX) so it needs to be passed over to
EX/MEM. Then it can be stored to memory as appropriate.
5. WB
Nothing happens, because the store instruction does not have an
RD. All steps are complete and the stage passes without action.
Lecture 4.3
A signal is asserted when its logical state is set to true. A
signal is deasserted when its set to false or unknown.
• Some signals are 'true' on low voltage, some on high
When a 1-bit control to a 2-way MUX is asserted, the MUX
selects input corresponding w/ 1. Otherwise, the 0 input is
select.
Figure 35: Red lines indicate the need for forwarding in order
to maintain this operation; otherwise, a stall is necessary to
resolve the dependency