Contrasting cache mappings and computing bits
Direct-mapped caches assign each memory block to exactly one cache line, offering fast access and simple hardware but suffering from frequent conflict misses. Fully associative caches allow any block to be placed anywhere, minimizing conflict misses but requiring complex, slower hardware due to exhaustive tag searches. Set-associative caches strike a balance by mapping blocks to a specific set of lines (ways), reducing conflict misses compared to direct-mapped while keeping hardware complexity manageable.
For a 64KB, 4-way set associative cache with 64-byte lines and 32-bit addresses:
- Offset bits = log₂(64) = 6
- Total lines = 65536 / 64 = 1024
- Number of sets = 1024 / 4 = 256 → index bits = log₂(256) = 8
- Tag bits = 32 - (8 + 6) = 18
Thus, the cache uses 6 offset bits, 8 index bits, and 18 tag bits.
On a read, the cache controller has to decide “where could this block be?” and “if it is here, how do I recognise it?”. The three common organisations differ in how tightly they constrain the placement:
-
In a direct‑mapped cache each block of main memory has exactly one location in the cache. Given the block address, a fixed subset of address bits is used as an index into a single line. This makes look‑up very simple and fast, but it also means that two hot blocks that map to the same line will continually evict each other, causing conflict misses.
-
In a fully associative cache there is no fixed index at all: any block can be placed in any cache line. To find a hit the controller compares the tag against the tags of all lines in parallel. This minimises conflict misses, but the hardware cost and access time grow because of the wide associative lookup.
-
A set‑associative cache is a compromise. The cache is divided into sets, each set containing (n) “ways”. A given block maps to exactly one set, using index bits as in the direct‑mapped case, but within that set it may occupy any of the (n) ways. Comparing tags only within the indexed set reduces conflicts compared to direct‑mapped, with less hardware than full associativity.
For the sizing calculation you ask about:
- Total capacity: 64 KiB = 65 536 bytes.
- Line (block) size: 64 bytes → offset within a line requires (\log_2 64 = 6) bits.
- Total number of lines: (65 536 / 64 = 1 024).
- Four‑way set associative → number of sets = (1 024 / 4 = 256) → index requires (\log_2 256 = 8) bits.
- With a 32‑bit physical address, the remaining bits are used as the tag: (32 − 8 − 6 = 18) bits.
So the address is partitioned into 18 tag bits, 8 index bits, and 6 offset bits.