Explore RISC-Vision
Learn CPU architecture concepts and understand how instructions move through a processor.
โก Go to CPU Simulator1. What is a CPU Pipeline?
A CPU pipeline is a technique used by processors to execute multiple instructions efficiently by dividing instruction execution into smaller stages.
Instead of completing one instruction before starting another, the processor overlaps different stages of multiple instructions. This improves overall instruction throughput.
Five Stages of a CPU Pipeline
2. The Five Pipeline Stages
IF โ Instruction Fetch
The processor fetches the next instruction from instruction memory.
ID โ Instruction Decode
The processor decodes the instruction and reads the required registers.
EX โ Execute
The ALU performs arithmetic or logical operations, calculates memory addresses, or evaluates branches.
MEM โ Memory Access
The processor reads data from or writes data to data memory when required.
WB โ Write Back
The final result is written into the destination register.
๐ Pipeline Timing Diagram
Observe how different instructions occupy different pipeline stages during each clock cycle.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| ADD | IF | ID | EX | MEM | WB | ||
| SUB | IF | ID | EX | MEM | WB | ||
| AND | IF | ID | EX | MEM | WB |
Remember: Each instruction advances one stage per clock cycle in this ideal pipeline example.
Pipeline order: IF โ ID โ EX โ MEM โ WB
3. Pipeline Hazards
A pipeline hazard is a condition that prevents an instruction from executing in its expected clock cycle.
1. Data Hazard
Occurs when an instruction depends on the result of an earlier instruction that has not yet completed.
Instruction 2: SUB R4, R1, R5
Problem: SUB needs the result of ADD.
Solution: Forwarding or a pipeline stall.
2. Control Hazard
Occurs when a branch or jump instruction changes the normal sequence of instruction execution.
Normal next instruction: ADD R3, R4, R5
Branch target: Instruction 4
Problem: The processor may fetch an instruction that must be skipped.
Solution: Branch handling and pipeline flushing when required.
3. Structural Hazard
Occurs when multiple instructions need the same hardware resource at the same time.
Instruction 2: Another instruction needs the same memory resource.
Problem: Both instructions need one shared resource simultaneously.
Solution: Separate resources, scheduling or stalls.
Key idea: Hazards can interrupt the ideal flow of a CPU pipeline.
4. Data Forwarding
Data forwarding is a technique that sends a result directly from a later pipeline stage to an instruction that needs it, without waiting for the result to be written back to the register file.
Example
ADD R1, R2, R3
SUB R4, R1, R5
The SUB instruction needs R1, which is calculated by ADD. Forwarding can provide the result directly to SUB.
โก How Data Forwarding Works
Instruction 2: SUB R4, R1, R5
ADD calculates the result for R1. SUB needs that result. Forwarding sends the result directly to SUB without waiting for the register write-back stage.
5. Pipeline Stalls and Bubbles
A pipeline stall temporarily delays an instruction when the required data or resource is not ready.
A bubble is an empty pipeline slot introduced to prevent an incorrect instruction execution.
Stalls can increase the total number of clock cycles required to complete a program.
โธ๏ธ Understanding a Pipeline Stall
Example: A load instruction is followed by an instruction that immediately needs its loaded data.
| Instruction | Cycle 1 | Cycle 2 | Cycle 3 | Cycle 4 | Cycle 5 | Cycle 6 |
|---|---|---|---|---|---|---|
| LW | IF | ID | EX | MEM | WB | |
| ADD | IF | ID | STALL | EX | MEM | |
| Bubble | EMPTY |
ADD needs the data loaded by LW. The processor delays ADD for one cycle to allow the data to become available.
Important: This is an illustrative load-use stall. Actual pipeline timing depends on the processor's forwarding design.6. Clock Cycles and CPI
A clock cycle is one basic timing unit of processor operation. Pipeline stages advance according to the processor clock.
CPI means Cycles Per Instruction. It measures the average number of clock cycles required per completed instruction.
Formula
CPI = Total Clock Cycles / Completed Instructions
Example
Total Clock Cycles = 10
Completed Instructions = 2
CPI = 10 / 2 = 5.00
๐งฎ Interactive CPI Calculator
7. Register File and Data Memory
๐ฆ Register File
Registers are small, fast storage locations inside the CPU. They temporarily hold operands and results during instruction execution.
๐พ Data Memory
Data memory stores information that instructions can read or write. For example, LW loads data and SW stores data.
8. Complete Instruction Walkthrough
Let's understand what happens when a CPU executes this instruction:
ADD R1, R2, R3
This means: Add the values stored in R2 and R3, then store the result in R1.
Worked Example
Assume:
R2 = 10
R3 = 5
ALU calculates: 10 + 5 = 15
Final result: R1 = 15The instruction has now completed all five pipeline stages. Its result is stored in the destination register.
9. What You Have Learned
- CPU pipeline architecture
- Five pipeline execution stages
- Data, control, and structural hazards
- Data forwarding
- Pipeline stalls and bubbles
- Clock cycles and CPI calculation
- Register file and data memory
- Complete instruction execution
You have completed the RISC-Vision theory lessons. Now apply these concepts practically using the CPU simulator.