Cache Memory Working Mechanism

Cache memory is built using Static RAM (SRAM) rather than Dynamic RAM (DRAM), which is used for main memory (RAM). The reason behind this choice is that SRAM is much faster than DRAM, though it is also more expensive and consumes more power.

Key Features of Cache Memory Construction:

  1. Made of SRAM – Unlike DRAM (which requires constant refreshing), SRAM is faster and does not need frequent refreshing, making it ideal for cache memory.
  2. Located Close to the CPU – Cache memory is either embedded inside the processor (L1 and L2 cache) or placed on a separate chip near the CPU (L3 cache).
  3. Small in Size – Since SRAM is costly, cache memory is limited in size, typically ranging from a few kilobytes (KB) to several megabytes (MB).
  4. Uses Associative Mapping Techniques – Data storage in cache follows specific mapping methods to ensure quick access (explained later in this article).

Components of Cache Memory

Cache memory consists of several essential components that enable efficient data retrieval and management:

1. Cache Controller

  • The cache controller is responsible for managing cache operations, including data storage, retrieval, and replacement policies.
  • It ensures that the most frequently accessed data is available in the cache.

2. Cache Memory Blocks (Cache Lines)

  • The cache memory is divided into small storage units called cache blocks or cache lines.
  • Each block stores a chunk of data fetched from the main memory.

3. Tags

  • Each cache block has a tag, which is an identifier that helps locate data in the cache.
  • The tag is used to match the requested data with the stored data during a cache lookup.

4. Data Store

  • This is the actual memory space where data is stored in cache lines.
  • It holds frequently accessed instructions and data for faster CPU retrieval.

5. Address Mapping Mechanism

  • The CPU generates memory addresses to fetch data, and the cache memory uses different mapping techniques to locate the requested data.
  • There are three main mapping techniques: Direct Mapping, Fully Associative Mapping, and Set-Associative Mapping (explained later).

6. Replacement Policy Unit

  • When the cache is full, old data needs to be replaced with new data.
  • The replacement policy unit decides which data should be removed using algorithms like Least Recently Used (LRU) or First-In-First-Out (FIFO).

7. Write Policy Unit

  • If the CPU modifies data in the cache, the write policy unit determines how and when those changes are updated in the main memory.
  • Two common write policies are Write-Through (updates RAM immediately) and Write-Back (updates RAM only when necessary).

How Cache Memory Works?

Cache memory operates using a well-defined process that involves fetching, storing, and managing frequently used data.

Step-by-Step Operation of Cache Memory:

1. CPU Requests Data

  • The CPU generates a memory request for data or an instruction.

2. Cache Lookup (Checking for Cache Hit or Miss)

  • The cache controller checks if the requested data is already stored in the cache (using tag comparison).
  • If the data is foundCache Hit occurs (fast access).
  • If the data is not foundCache Miss occurs (data must be fetched from RAM).

3. Handling Cache Miss (Fetching from RAM)

  • If there is a cache miss, the data is retrieved from RAM and stored in the cache for future requests.
  • If the cache is full, the replacement policy determines which old data should be replaced.

4. Sending Data to CPU

  • Once the data is found (either from cache or RAM), it is sent to the CPU for processing.

5. Updating Cache (Write Policy Management)

  • If the CPU modifies data, the write policy unit decides whether to update RAM immediately (Write-Through) or later (Write-Back).

Cache Mapping Techniques

Cache memory uses mapping techniques to determine how main memory data is placed into cache blocks. There are three primary mapping techniques:

1. Direct Mapping

  • Each block in the main memory is assigned to a specific cache block.
  • It is simple and fast but may lead to frequent replacements if multiple memory blocks map to the same cache block.

2. Fully Associative Mapping

  • Any block in the main memory can be stored in any cache block.
  • This provides flexibility and reduces conflicts but requires more complex hardware.

3. Set-Associative Mapping

  • A combination of direct and fully associative mapping.
  • The cache is divided into sets, and a block from main memory can be stored in any block within a specific set.
  • It balances speed and efficiency.

Conclusion

Cache memory is a critical component of modern computer architecture, significantly improving system performance by providing high-speed data access. Built using SRAM, cache memory consists of several components such as cache controllers, cache lines, tags, and mapping units. The operation of cache memory revolves around minimizing cache misses and maximizing cache hits using efficient mapping and replacement strategies.