The Tag Field
This page uses a toy memory model to help you understand how the tag field works in a direct-mapped cache.
- the problem is that each cache location can hold a copy of any one of many memory locations
- this means that we need a way to tell which memory location is in the cache at any given time
- the tag field is what does this for us, and you need to understand how that works
Here's a diagram of a Main Memory with 16 words and a Direct-Mapped Cache with 4 words.
- 16 words is 64 bytes, so we have 6-bit addresses (26 = 64)
- caches don't do bytes, so we always ignore the rightmost 2 bits
- that leaves us with word addresses that are 4 bits long
- these addresses count from 0000 to 1111, which is 0 to 15 in decimal
- each address is divided into 2 fields, each of which is 2 bits long in this case
- in this particular case, the index field is 2 bits long because the cache has 4 locations
- however many bits the index field has,
they are at the right (least significant) end of the word address
- the tag field is whatever bits are left after we choose the index bits
- the tag field is at the left (most significant) end of the word address
- in the diagram, the tag bits are written in red so that you can pick them out easily
- when we copy a memory location into the cache, there is only one place that it can go:
the spot that has the same value in its index field (also the spot with the same color)
- at the same time,
we copy the tag-field bits from the word's address into the tag field of the cache entry
- let's do an example: we'll install the word at address 6 (0110 in binary) into the cache
- the data area at address 6 is yellow, so we'll use the yellow entry in the cache
- notice that the index fields match -- both are 10
- there are 4 words in memory that are yellow
- the index field is the same (10) for all 4 of them, so it can't help us tell which one is in the cache
- the tag fields of the yellow boxes are all different (00, 01, 10, 11)
- the tag field at location 6 is 01
- we copy that into the tag field of the cache
at the same time that we copy the data value into the data field
- it will tell us which of the 4 yellow words is in the cache
- the index is not stored in the cache; it acts as an address,
similar to a memory address or a register number
- of course, the data fields have values in them, too, but they never enter the discussion here;
it's all about the addresses
here is an example of the cache with all the tag fields filled in;
at the moment addresses 12, 1, 6, and 15 are in the cache