I am currently investigating some previous exams for my CA course.
There is one question which i found really confusing, here is the the data to work with:
Considering a 32-bit address (tag 20bits, index 7 bits, byte offset 5bits) with a 4 way set associative cache. The two least significant address bits are always zero.
Cache is initially empty and when fetching it always hits. Its a write back cache which always transfers complete blocks with an allocate on write miss policy.
There is the trace to work with in hex:
Read 0770 3718 – write 0770 3704 – read 0770 3768 - [...]
Now to my question:
When looking at the first 3 statements only:
| Trace | Tag | Set | Offset |
|---|---|---|---|
| 07703718 | 0000 0111 0111 0000 0011 | 0111 000 | 1 1000 |
| 07703704 | 0000 0111 0111 0000 0011 | 0111 000 | 0 0100 |
| 07703768 | 0000 0111 0111 0000 0011 | 0111 011 | 0 1000 |
I am likely doing something wrong., but i thought that there cant be the same tag within different / multiple sets.
I looked at Computer Architecture: A quantitative approach but all there all the examples match.
I think you're doing it right.
Remember that the index is part of the address, so cache lines can represent different addresses even with the same tag. Tag + index portions together identify a 32-byte range of main memory, while the offset says which starting byte in the 32-byte range.
In your example, the first two addresses are in the same 32-byte range of main memory, so the 2nd access is a hit. That 32-byte block is caching main memory address range 0x07703700 through 0x0770371F.
The third address is in a wholly different main memory address range (it doesn't fall in the range for the prior two accesses) so that maps to a different index, the tag being the same is irrelevant. That 32-byte block is caching main memory address range 0x07703760 to 0x0770377F.
The 4-way cache mechanism will always look for addresses 0x07703718 & 0x07703704 at index position 0x38, and, for address 0x07703768 at index position 0x3b. So, there's no conflict with having the same tags at those two separate index positions.