I've given an assignment where I need to simulate an associative cache with Java. We're given a set of byte addresses, we need to find whether each address is a hit or a miss. The cache consists of 32 blocks and each block is 8 bytes
So the scheme is like the following:
- Calculate the group address from the address (Group address is the address divided by the size of each block in the cache. For example group address for address 950 will be 950/8)
- Use the entire group address as the tag
- Scan the entire cache and see if the tag is already in the cache
- If the tag is found, record a hit
- If the tag is not found, record a miss, and store the tag in the first available block
- If there are no spaces available, remove the LEAST recently used (LRU) block from the cache
- Update the cache so the new tag is stored (if there was a miss) and remember this block is the MOST recently used.
- Set valid bit to true, store tag in block, remember which address was used originally.
The problem I'm having is doing the Least Recently Used bit. The way we're asked to implement the LRU is by keeping the blocks in order from most recently used to least recently used, and when we need to replace a block we remove the old one from the end and add the new onto the beginning. Then when we get a hit we move the hit to the beginning of the cache.