indexVtagdata: one 16-byte lineDCPU0001····010101····011001····011101····01main memory · 256 bytes0x000x100x200x300x400x500x600x700x800x900xA00xB00xC00xD00xE00xF0tag = ?··address····tag··index····offset···tag·idx····offset
Lit trace with moving dashes: it carries the value the step is aboutWhite tags: addresses, index and tag bits, lines and bytes on the moveColours: tag (magenta), index (blue-violet), offset (lime) on the address and the tableRound lamps on each row: V = valid, D = dirty, glowing = 1The CPU asks for 0x24, 0x28, 0x64 and 0x24 again. Each address splits into tag, index and offset; the index picks a row and the tag says whether the row holds the right line. The direct-mapped cache hits once in four; regrouped as 2-way set-associative it hits twice, and LRU picks which line to evict. Then a store, written through to memory or held back with a dirty bit, and the average access time. A cache keeps copies of recently used memory in a small, fast memory (SRAM) near the processor and moves data in fixed-size blocks called lines; each address is split into a tag, an index and a block offset, and each row has a valid bit (Patterson and Hennessy, Computer Organization and Design RISC-V Edition, section 5.3). Intel 64 and IA-32 processors and AMD Zen processors use 64-byte cache lines (Intel Optimization Reference Manual; AMD Software Optimization Guide). The figure uses 16-byte lines, 8-bit addresses and 4 rows so every bit fits on screen; the rules are the same.

Why caches

  1. Main memory (DRAM) answers in 50 to 100 ns. A 3 GHz CPU ticks 300 times in 100 ns. A cache is a small, fast memory (SRAM, about 1 ns) beside the CPU that keeps copies of memory it used recently.

Hits and misses

Where a line can go

Writes and speed

The working

;; step 1/11 · why
;; about: CPU, Cache, Main memory
typical times
  L1 cache (SRAM)       ~1 ns
  main memory (DRAM)    50–100 ns

3 GHz clock = 3 ticks per ns
  100 ns × 3 = 300 ticks waiting

The cache sits between the CPU and
memory. When it already has a copy,
the CPU skips the long trip.

This figure: CPU, a 4-row cache and
256 bytes of main memory.

Lit traces with moving dashes carry the value the step is about; the white tags riding them are those values (addresses, index and tag bits, lines and bytes). Round lamps on each row: V = valid, D = dirty, glowing = 1. The Python mirrors the hardware: shifting right by 4 drops the offset bits, & 0b11 keeps the 2 index bits, and the rows list remembers one tag per row of the direct-mapped cache.