5/14/2025
VIETNAM NATIONAL UNIVERSITY HANOI (VNU)
VNU INFORMATION TECHNOLOGY INSTITUTE
Computer Architecture
Lecture 5: The processor - The Basic Structure
Duy-Hieu Bui, PhD
AIoT Laboratory
Email:
[email protected] https://duyhieubui.github.io
Copyright
Content adapted from “Computer Organization and Design RISC-V
Edition: The Hardware Software Interface, Second Edition” by David A.
Patterson, John L. Hennessy, published by Morgan Kaufmann. © 2020
Elsevier Inc. All rights reserved.
5/14/2025 VNU-ITI/CICA 2
1
5/14/2025
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 3
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 4
2
5/14/2025
Introduction
• CPU performance factors
– Instruction count
• Determined by ISA and compiler
– CPI and Cycle time
• Determined by CPU hardware
• We will examine two RISC-V implementations
– A simplified version
– A more realistic pipelined version
• Simple subset, shows most aspects
– Memory reference: ld, sd
– Arithmetic/logical: add, sub, and, or
– Control transfer: beq
5/14/2025 VNU-ITI/CICA 5
Instruction Execution
• PC → instruction memory, fetch instruction
• Register numbers → register file, read registers
• Depending on instruction class
– Use ALU to calculate
• Arithmetic result
• Memory address for load/store
• Branch comparison
– Access data memory for load/store
– PC target address or PC + 4
5/14/2025 VNU-ITI/CICA 6
3
5/14/2025
CPU Overview
5/14/2025 VNU-ITI/CICA 7
Multiplexers
◼ Can’t just join
wires together
◼ Use multiplexers
5/14/2025 VNU-ITI/CICA 8
4
5/14/2025
Control
5/14/2025 VNU-ITI/CICA 9
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 10
5
5/14/2025
Logic Design Basics
• Information encoded in binary
– Low voltage = 0, High voltage = 1
– One wire per bit
– Multi-bit data encoded on multi-wire buses
• Combinational element
– Operate on data
– Output is a function of input
• State (sequential) elements
– Store information
5/14/2025 VNU-ITI/CICA 11
Combinational Elements
• AND-gate • Adder A
– Y=A&B – Y=A+B + Y
A B
Y
B
• Multiplexer • Arithmetic/Logic
– Y = S ? I1 : I0 Unit
– Y =A F(A, B)
I0 M
u Y ALU Y
I1 x
B
S F
5/14/2025 VNU-ITI/CICA 12
6
5/14/2025
Sequential Elements
• Register: stores data in a circuit
– Uses a clock signal to determine when to update the
stored value
– Edge-triggered: update when Clk changes from 0 to 1
Clk
D Q
D
Clk
Q
5/14/2025 VNU-ITI/CICA 13
Sequential Elements
• Register with write control
– Only updates on clock edge when write control input is 1
– Used when stored value is required later
Clk
D Q Write
Write D
Clk
Q
5/14/2025 VNU-ITI/CICA 14
7
5/14/2025
Clocking Methodology
• Combinational logic transforms data during clock
cycles
– Between clock edges
– Input from state elements, output to state element
– Longest delay determines clock period
5/14/2025 VNU-ITI/CICA 15
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 16
8
5/14/2025
Building a Datapath
• Datapath
– Elements that process data and addresses
in the CPU
• Registers, ALUs, mux’s, memories, …
• We will build a RISC-V datapath incrementally
– Refining the overview design
5/14/2025 VNU-ITI/CICA 17
Instruction Fetch
Increment by
4 for next
64-bit instruction
register
5/14/2025 VNU-ITI/CICA 18
9
5/14/2025
R-Format Instructions
• Read two register operands
• Perform arithmetic/logical operation
• Write register result
5/14/2025 VNU-ITI/CICA 19
Load/Store Instructions
• Read register operands
• Calculate address using 12-bit offset
– Use ALU, but sign-extend offset
• Load: Read memory and update register
• Store: Write register value to memory
5/14/2025 VNU-ITI/CICA 20
10
5/14/2025
Branch Instructions
• Read register operands
• Compare operands
– Use ALU, subtract and check Zero output
• Calculate target address
– Sign-extend displacement
– Shift left 1 place (halfword displacement)
– Add to PC value
5/14/2025 VNU-ITI/CICA 21
Branch Instructions
Just
re-routes
wires
Sign-bit wire
replicated
5/14/2025 VNU-ITI/CICA 22
11
5/14/2025
Composing the Elements
• First-cut data path does an instruction in one
clock cycle
– Each datapath element can only do one function at a
time
– Hence, we need separate instruction and data
memories
• Use multiplexers where alternate data sources
are used for different instructions
5/14/2025 VNU-ITI/CICA 23
R-Type/Load/Store Datapath
5/14/2025 VNU-ITI/CICA 24
12
5/14/2025
Full Datapath
5/14/2025 VNU-ITI/CICA 25
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 26
13
5/14/2025
ALU Control
• ALU used for
– Load/Store: F = add
– Branch: F = subtract
– R-type: F depends on opcode
ALU control Function
0000 AND
0001 OR
0010 add
0110 subtract
5/14/2025 VNU-ITI/CICA 27
ALU Control
• Assume 2-bit ALUOp derived from opcode
– Combinational logic derives ALU control
ALU
opcode ALUOp Operation Opcode field ALU function control
ld 00 load register XXXXXXXXXXX add 0010
sd 00 store register XXXXXXXXXXX add 0010
beq 01 branch on equal XXXXXXXXXXX subtract 0110
R-type 10 add 100000 add 0010
subtract 100010 subtract 0110
AND 100100 AND 0000
OR 100101 OR 0001
5/14/2025 VNU-ITI/CICA 28
14
5/14/2025
The Main Control Unit
• Control signals derived from instruction
5/14/2025 VNU-ITI/CICA 29
Datapath With Control
5/14/2025 VNU-ITI/CICA 30
15
5/14/2025
R-Type Instruction
5/14/2025 VNU-ITI/CICA 31
Load Instruction
5/14/2025 VNU-ITI/CICA 32
16
5/14/2025
Branch-on-Equal Instruction
5/14/2025 VNU-ITI/CICA 33
Performance Issues
• Longest delay determines clock period
– Critical path: load instruction
– Instruction memory → register file → ALU → data
memory → register file
• Not feasible to vary period for different
instructions
• Violates design principle
– Making the common case fast
• We will improve performance by pipelining
5/14/2025 VNU-ITI/CICA 34
17
5/14/2025
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 35
Pipelining Analogy
• Pipelined laundry: overlapping execution
– Parallelism improves performance
• Four loads:
– Speedup
= 8/3.5 = 2.3
• Non-stop:
– Speedup
= 2n/0.5n + 1.5 ≈ 4
= number of stages
5/14/2025 VNU-ITI/CICA 36
18
5/14/2025
RISC-V Pipeline
• Five stages, one step per stage
1. IF: Instruction fetch from memory
2. ID: Instruction decode & register read
3. EX: Execute operation or calculate address
4. MEM: Access memory operand
5. WB: Write result back to register
5/14/2025 VNU-ITI/CICA 37
Pipeline Performance
• Assume time for stages is
– 100ps for register read or write
– 200ps for other stages
• Compare pipelined datapath with single-cycle
datapath
Instr Instr fetch Register ALU op Memory Register Total time
read access write
ld 200ps 100 ps 200ps 200ps 100 ps 800ps
sd 200ps 100 ps 200ps 200ps 700ps
R-format 200ps 100 ps 200ps 100 ps 600ps
beq 200ps 100 ps 200ps 500ps
5/14/2025 VNU-ITI/CICA 38
19
5/14/2025
Pipeline Performance
Single-cycle (Tc= 800ps)
Pipelined (Tc= 200ps)
5/14/2025 VNU-ITI/CICA 39
Pipeline Speedup
• If all stages are balanced
– i.e., all take the same time
– Time between instructionspipelined
= Time between instructionsnonpipelined
Number of stages
• If not balanced, speedup is less
• Speedup due to increased throughput
– Latency (time for each instruction) does not decrease
5/14/2025 VNU-ITI/CICA 40
20
5/14/2025
Pipelining and ISA Design
• RISC-V ISA designed for pipelining
– All instructions are 32-bits
• Easier to fetch and decode in one cycle
• c.f. x86: 1- to 17-byte instructions
– Few and regular instruction formats
• Can decode and read registers in one step
– Load/store addressing
• Can calculate address in 3rd stage, access memory in 4th
stage
5/14/2025 VNU-ITI/CICA 41
Hazards
• Situations that prevent starting the next
instruction in the next cycle
• Structure hazards
– A required resource is busy
• Data hazard
– Need to wait for previous instruction to complete its
data read/write
• Control hazard
– Deciding on control action depends on previous
instruction
5/14/2025 VNU-ITI/CICA 42
21
5/14/2025
Structural Hazards
• Conflict for use of a resource
• In RISC-V pipeline with a single memory
– Load/store requires data access
– Instruction fetch would have to stall for that cycle
• Would cause a pipeline “bubble”
• Hence, pipelined datapaths require separate
instruction/data memories
– Or separate instruction/data caches
5/14/2025 VNU-ITI/CICA 43
Data Hazards
• An instruction depends on completion of data
access by a previous instruction
– add x19, x0, x1
sub x2, x19, x3
5/14/2025 VNU-ITI/CICA 44
22
5/14/2025
Forwarding (aka Bypassing)
• Use result when it is computed
– Don’t wait for it to be stored in a register
– Requires extra connections in the datapath
5/14/2025 VNU-ITI/CICA 45
Load-Use Data Hazard
• Can’t always avoid stalls by forwarding
– If value not computed when needed
– Can’t forward backward in time!
5/14/2025 VNU-ITI/CICA 46
23
5/14/2025
Code Scheduling to Avoid Stalls
• Reorder code to avoid use of load result in the
next instruction
• C code for a = b + e; c = b + f;
ld x1, 0(x0) ld x1, 0(x0)
ld x2, 8(x0) ld x2, 8(x0)
stall add x3, x1, x2 ld x4, 16(x0)
sd x3, 24(x0) add x3, x1, x2
ld x4, 16(x0) sd x3, 24(x0)
stall add x5, x1, x4 add x5, x1, x4
sd x5, 32(x0) sd x5, 32(x0)
13 cycles 11 cycles
5/14/2025 VNU-ITI/CICA 47
Control Hazards
• Branch determines flow of control
– Fetching next instruction depends on branch
outcome
– Pipeline can’t always fetch correct instruction
• Still working on ID stage of branch
• In RISC-V pipeline
– Need to compare registers and compute
target early in the pipeline
– Add hardware to do it in ID stage
5/14/2025 VNU-ITI/CICA 48
24
5/14/2025
Stall on Branch
• Wait until branch outcome determined
before fetching next instruction
5/14/2025 VNU-ITI/CICA 49
Branch Prediction
• Longer pipelines can’t readily determine
branch outcome early
– Stall penalty becomes unacceptable
• Predict outcome of branch
– Only stall if prediction is wrong
• In RISC-V pipeline
– Can predict branches not taken
– Fetch instruction after branch, with no delay
5/14/2025 VNU-ITI/CICA 50
25
5/14/2025
More-Realistic Branch Prediction
• Static branch prediction
– Based on typical branch behavior
– Example: loop and if-statement branches
• Predict backward branches taken
• Predict forward branches not taken
• Dynamic branch prediction
– Hardware measures actual branch behavior
• e.g., record recent history of each branch
– Assume future behavior will continue the trend
• When wrong, stall while re-fetching, and update
history
5/14/2025 VNU-ITI/CICA 51
Pipeline Summary The BIG Picture
• Pipelining improves performance by
increasing instruction throughput
– Executes multiple instructions in parallel
– Each instruction has the same latency
• Subject to hazards
– Structure, data, control
• Instruction set design affects complexity of
pipeline implementation
5/14/2025 VNU-ITI/CICA 52
26
5/14/2025
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 53
RISC-V Pipelined Datapath
MEM
Right-to-left WB
flow leads to
hazards
5/14/2025 VNU-ITI/CICA 54
27
5/14/2025
Pipeline registers
• Need registers between stages
– To hold information produced in previous cycle
5/14/2025 VNU-ITI/CICA 55
Pipeline Operation
• Cycle-by-cycle flow of instructions through
the pipelined datapath
– “Single-clock-cycle” pipeline diagram
• Shows pipeline usage in a single cycle
• Highlight resources used
– c.f. “multi-clock-cycle” diagram
• Graph of operation over time
• We’ll look at “single-clock-cycle” diagrams
for load & store
5/14/2025 VNU-ITI/CICA 56
28
5/14/2025
IF for Load, Store, …
5/14/2025 VNU-ITI/CICA 57
ID for Load, Store, …
5/14/2025 VNU-ITI/CICA 58
29
5/14/2025
EX for Load
5/14/2025 VNU-ITI/CICA 59
MEM for Load
5/14/2025 VNU-ITI/CICA 60
30
5/14/2025
WB for Load
Wrong
register
number
5/14/2025 VNU-ITI/CICA 61
Corrected Datapath for Load
5/14/2025 VNU-ITI/CICA 62
31
5/14/2025
EX for Store
5/14/2025 VNU-ITI/CICA 63
MEM for Store
5/14/2025 VNU-ITI/CICA 64
32
5/14/2025
WB for Store
5/14/2025 VNU-ITI/CICA 65
Multi-Cycle Pipeline Diagram
• Form showing resource usage
5/14/2025 VNU-ITI/CICA 66
33
5/14/2025
Multi-Cycle Pipeline Diagram
• Traditional form
5/14/2025 VNU-ITI/CICA 67
Single-Cycle Pipeline Diagram
• State of pipeline in a given cycle
5/14/2025 VNU-ITI/CICA 68
34
5/14/2025
Pipelined Control (Simplified)
5/14/2025 VNU-ITI/CICA 69
Pipelined Control
• Control signals derived from instruction
– As in single-cycle implementation
5/14/2025 VNU-ITI/CICA 70
35
5/14/2025
Pipelined Control
5/14/2025 VNU-ITI/CICA 71
Outline
• Introduction
• Logic Design Conventions
• Building a Datapath
• A Simple Implementation Scheme
• An Overview of Pipelining
• Pipelined Datapath and Control
• Concluding Remarks
5/14/2025 VNU-ITI/CICA 72
36
5/14/2025
Concluding Remarks
• ISA influences design of datapath and control
• Datapath and control influence design of ISA
• Pipelining improves instruction throughput
using parallelism
– More instructions completed per second
– Latency for each instruction not reduced
• Hazards: structural, data, control
• Multiple issue and dynamic scheduling (ILP)
– Dependencies limit achievable parallelism
– Complexity leads to the power wall
5/14/2025 VNU-ITI/CICA 73
37