๐Ÿ“˜ Theory & Learning

Understand the concepts behind the RISC-Vision CPU Simulator

Explore RISC-Vision

Learn CPU architecture concepts and understand how instructions move through a processor.

โšก Go to CPU Simulator

1. 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

Five Stages of CPU Pipeline IF ID EX MEM WB Fetch Decode Execute Memory Write Back Instruction execution flows from IF to WB
Simple example: While one instruction is being executed, another can be decoded and a third can be fetched.

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 123 4567
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 1: ADD R1, R2, R3

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.

Instruction 1: BEQ R1, R2, 2

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 1: LW R1, 0(R2)

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 1: ADD R1, R2, R3

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.

ADD Instruction Produces R1 SUB Instruction Needs R1 FORWARD R1 Result travels directly to the dependent instruction
Result: Data forwarding reduces unnecessary waiting when the required result is available from a later pipeline 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 1Cycle 2 Cycle 3Cycle 4 Cycle 5Cycle 6
LW IF ID EX MEM WB
ADD IF ID STALL EX MEM
Bubble EMPTY
What happened?

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

Your CPI: 5.00

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.

CPU Register File R0 R1 R2 R3 ALU Uses register values for calculations Registers provide data to processor operations

๐Ÿ’พ Data Memory

Data memory stores information that instructions can read or write. For example, LW loads data and SW stores data.

CPU Executes LW / SW Data Memory Address 10: 20 Address 11: 25 Address 12: 30 SW: Store LW: Load CPU and memory exchange data
Remember: Registers are inside the CPU, while data memory stores program data outside the register file.

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.

1. IF โ€” Fetch ADD instruction 2. ID โ€” Decode and read R2, R3 3. EX โ€” Add R2 + R3 4. MEM โ€” No data access needed 5. WB โ€” Write result into R1

Worked Example

Assume:

R2 = 10

R3 = 5


ALU calculates: 10 + 5 = 15

Final result: R1 = 15

The instruction has now completed all five pipeline stages. Its result is stored in the destination register.

9. What You Have Learned

Congratulations!

You have completed the RISC-Vision theory lessons. Now apply these concepts practically using the CPU simulator.

โšก Start CPU Simulation