# 5-Stage Instruction Pipelining, Hazards & Cache Mapping Bit Math
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Computer Organization & Architecture
Canonical URL: https://gateandnet.com/exams/gate-cs/computer-organization/pipelining-hazards-and-cache-mapping

> Clock cycle formulas, stall penalties, operand forwarding, and Direct vs K-Way Set-Associative Tag/Index/Offset splits.

## Physical Address Bit Split for Cache Mapping

Suppose Main Memory has $2^m$ bytes ($m$-bit physical address), Cache size $= C$ bytes, and Block (Line) size $= B = 2^b$ bytes:
- **Number of Cache Lines:** $L = C / B$
- **Block Offset bits:** $b = \log_2 B$

| Mapping Scheme | Sets ($S$) | Index / Set Bits | Tag Bits | Comparator Count |
| :--- | :--- | :--- | :--- | :--- |
| **Direct Mapped** | $L$ | $\log_2 L$ | $m - \log_2 L - b$ | $1$ |
| **$K$-Way Set Associative** | $L / K$ | $\log_2(L / K)$ | $m - \log_2(L/K) - b$ | $K$ |
| **Fully Associative** | $1$ | $0$ bits | $m - b$ | $L$ |

> [!FORMULA]
> **Tag Directory Size:** Always multiply the number of **Cache Lines ($L$)** by **(Tag bits + Valid bit + Dirty bit + LRU bits)**! Increasing associativity $K$ by a factor of $2$ decreases Set Index by $1$ bit and increases Tag by $1$ bit.

## Data Hazard Classification

For instruction $I$ followed by instruction $J$:
- **RAW (Read After Write — True Dependency):** $J$ reads a register before $I$ writes it. Solved via **Operand Forwarding** (except Load-Use hazard which still costs $1$ stall cycle!).
- **WAR (Anti-Dependency) & WAW (Output Dependency):** Name dependencies — eliminated completely by **Register Renaming** in out-of-order processors.


## Solved Practice Questions & PYQs

### [GATE CS 2024 NAT | NAT]
A computer has a $32$-bit physical address space and a **4-way set-associative** cache of total capacity $64\text{ KB}$ with a cache block size of $64\text{ bytes}$. How many bits are in the **Tag** field of the physical address?
**Correct Answer:** 18
**Solution:** - Block size $B = 64 = 2^6 \implies$ **Block Offset $= 6$ bits**.
- Number of cache lines $L = \frac{64\text{ KB}}{64\text{ B}} = \frac{2^{16}}{2^6} = 2^{10}$ lines.
- Number of sets $S = \frac{L}{4} = \frac{2^{10}}{2^2} = 2^8 \implies$ **Set Index $= 8$ bits**.
- **Tag bits** $= 32 - 8 - 6 = \mathbf{18}$ bits!

### [GATE CS COA | MCQ]
A $64\text{ KB}$ **4-way set-associative** cache has block (line) size of $64\text{ bytes}$ in a system with a $32$-bit physical address. How many bits are in the **Tag** field?
**Correct Answer:** B
**Solution:** Block offset $= \log_2(64) = 6$ bits. Total blocks $= 64\text{ KB} / 64\text{ B} = 1024 = 2^{10}$. Number of sets $= 2^{10} / 4 = 2^8 = 256 \implies$ Set Index $= 8$ bits. Therefore, **Tag** $= 32 - 8 - 6 = \mathbf{18}$ bits.

### [GATE CS COA | MCQ]
In a standard in-order **5-stage RISC instruction pipeline** (`IF, ID, EX, MEM, WB`), which data hazard can actually occur and require stalling or operand forwarding?
**Correct Answer:** C
**Solution:** In an in-order 5-stage RISC pipeline, all reads occur early in `ID` and all register writes occur late in `WB`, so WAR and WAW cannot happen — only **RAW (Read-After-Write)** true data hazards occur!

### [GATE CS COA | MCQ]
An L1 cache has access time $2\text{ ns}$ and hit ratio $95\%$. Main memory access penalty on a cache miss (including block fill) is $60\text{ ns}$. Under simultaneous/hierarchical model $T_{\text{avg}} = T_c + (1 - h)T_m$, what is the Average Memory Access Time?
**Correct Answer:** A
**Solution:** $T_{\text{avg}} = T_{\text{hit}} + \text{Miss Rate} \times \text{Miss Penalty} = 2 + (1 - 0.95) \times 60 = 2 + 0.05 \times 60 = 2 + 3 = \mathbf{5.0\text{ ns}}$.
