I'm currently self-learning on the cache memory and have come across a method on how to find out which cache block a memory address will be mapped to:
cache block index = memory address in decimal % no. of cache blocks
However, I have also come across another method that also finds which cache block a memory address maps to by finding its tag, index and block offset. Yet both seems to give different results.
Eg: Where would memory address 6146 of 32-bits be mapped to in a directly-mapped cache consisting of 1024 4-bytes blocks?
Using the 1st method, I get:
6146 % 1024 = 2
Using the 2nd method, I get:
Let B be the size of a cache block, b be the block offset, i be the index, S be the no. of blocks and t be the tag bits b = log₂B = log₂4 = 2 i = log₂S = log₂1024 = 10 t = 32-2-10 = 20 6146 in binary: 0000 0000 0000 0000 0001|1000 0000 00|10 tag |index |block offset 1000 0000 00 = 512 in decimal
In method 1, I see that memory address 6146 should be mapped to cache block index 2. But in method 2, I see that memory address 6146 should be mapped to cache block index 512 instead. So which method is right? Is memory address 6146 mapped to cache block index 2 or cache block index 512? Am I just mixing up the 2 methods (probably am)? If so, what exactly is each method for (since online says both are for mapping memory address to cache block)?
Much thanks and apologies if my question isn't quite clear in advance! (Learning cache has been rather confusing for me 😕)
EDIT: The only possible reason I can think of is that the 1st method is only applicable if the cache block size is 1 byte, which would make
b = log₂1 = 0 t = 32 - 0 - 10 = 22 6146 in binary: 0000 0000 0000 0000 000110|00 0000 0010 tag |index
in method 2, giving us cache block index 2. I'm not sure if this is the right reasoning however.