5.4.2 · D1Memory Hierarchy & Caches

Foundations — Cache organization (direct-mapped)

1,755 words8 min readBack to topic

Before you can follow the parent note, every squiggle it uses has to mean something concrete in your head. This page builds each one from nothing, in an order where every idea rests on the one before it.


0. What is a "bit"? (the atom of everything)

A row of bits written together is a binary number. Reading it is exactly like reading a decimal number, except each place is worth 2 times the place to its right instead of 10 times.

Figure — Cache organization (direct-mapped)

Look at the figure: the rightmost bit is the "ones" place (), the next is "twos" (), then "fours" (), and so on. To find the value you add up the place-values wherever there is a .

Why the topic needs this: a memory address is a row of bits. All the "chopping" the parent does is grabbing certain bits out of that row — impossible to understand without knowing what a bit is worth.


1. The exponent notation (why powers of two rule everything)

bits patterns
(= 1 KB of things)

Why the topic needs this: cache size, block size, and line count are always powers of two precisely so a whole clean group of address bits can name each one.


2. The logarithm (the exact undo of )

answers "how many patterns do bits give?". But the parent constantly asks the reverse question: "I need to name lines — how many bits is that?"

Why this tool and not division? Division would tell you how many times something fits; tells you how many switches it takes — a completely different question, and it is the bit-counting question caches live on.

must be a power of two for this to land on a whole number — which is exactly why cache sizes always are.


3. Bytes and "byte-addressable memory"

Figure — Cache organization (direct-mapped)

Why the topic needs this: the parent's "block offset" only makes sense once you see memory as individually-numbered byte-mailboxes; the offset is which mailbox inside a group you want. See Memory Addressing for the full picture.


4. The address as a row of bits

The single most important move in the whole topic: one address is one row of bits, and we will slice that row into three labelled fields. Hold that image.


5. The block — grouping mailboxes

Why groups at all? Because programs almost always use bytes near each other soon after (this is spatial locality). Fetching a whole neighbourhood at once is cheaper than fetching one mailbox at a time.


6. Modulo mod — the wrap-around rule

The parent's core formula uses . This is the one piece of "real math" and it deserves a picture.

Figure — Cache organization (direct-mapped)

Why this tool? The cache has only lines but memory has millions of blocks. We need a rule that takes any block number and always lands inside , cycling fairly. Modulo is exactly the "wrap the counter" operation — no other simple operation gives every block a definite, repeatable home slot.

A beautiful shortcut: when , mod is just "keep the lowest bits" — because those low bits already are the remainder. That is why the parent can grab bits instead of doing real division.


7. The floor — throwing away the leftovers

The parent writes the block number as . Dividing an address by and flooring means "chop off the last offset bits" — it discards which byte you wanted and keeps only which block. In bit-terms it is a right shift by .


8. Putting the slices together — Tag / Index / Offset

Now every symbol earned, the parent's decomposition reads cleanly. Slice the -bit address:

Figure — Cache organization (direct-mapped)
  • Offset (low ): which byte inside the block — from §5.
  • Index (middle ): which cache line, via the wrap rule — from §6.
  • Tag (top ): everything left over, used to prove the line holds your block and not a colliding one.

Where do these lead next? Tags-per-line and valid bits drive Cache Performance Metrics; letting a block choose among several slots instead of one is Set-Associative Caches; deciding who to kick out when slots fill is Cache Replacement Policies; deciding how writes update memory is Cache Write Policies.


Prerequisite map

Bit and binary value

Powers of two 2^n

log base 2 counts bits

Byte and byte-addressable memory

Address is m bits

Block of 2^b bytes

Offset bits b

Modulo wrap rule

Index bits c

Tag = leftover high bits

Direct-Mapped Cache

Valid bit


Equipment checklist

Cover the right side and answer each before moving on.

Read the binary number as a decimal value
What does count, in one phrase
the number of distinct patterns bits can make
Compute
, because
How many bits to name cache lines
exactly bits
What does "byte-addressable" mean
every single byte has its own numbered address
If block size is bytes, how many offset bits
bits
Compute
(wraps: 0,1,2,3,0,1... 13 lands on 1)
When , the fast way to compute
keep the lowest bits of
What does give you
the block number (offset bits chopped off)
Which field picks the cache line
the Index (middle bits), never the Tag
What is the Tag for
to prove a line holds your block among all blocks sharing its index
What does the Valid bit being 0 force
a miss — the line is empty
For , how many Tag bits
bits