Reputation: 129
Suppose you have a 32 byte direct mapped cache with 16 bytes per block, and it reads the address 0xd. What are the contents of the cache? I thought the fetcher would add 0xd to the cache in set 1(so second one), byte 6. But in my simulator, 0xc is then a hit, so obviously this is wrong! Any help is appreciated.
Upvotes: 2
Views: 70
Reputation: 7374
To determine which set an address is found in, you use:
(addr >> log2(block size)) & (number of sets - 1)
(Assumes block size and number of sets are both a power of 2, which they pretty much always are, so &(n-1)
is the same as %n
, a modulo)
For your case, it would be: (0xd >> 4) & 1
, which is 0.
The block in set 0 contains bytes 0x0 through 0xf, so a reference to 0xc is a hit.
The reason why it contains bytes 0x0 through 0xf is that computer memory systems are usually designed to handle naturally aligned data more quickly than nonaligned data. A block of size N is naturally aligned if the address which starts the block has zeros in the low log2(N) bits of the address. So when address 0xd is referenced, the aligned block that contains it (with starting address 0x0) is loaded into the cache.
Upvotes: 2