I found a good book in the library that offered me the clear explanation I needed, and now I’ll talk about it here if any other student stumbles on this topic when searching in caches.
In Computer Architecture - A Quantitative Approach, 3rd edition of Hennessey and Patterson, p. 390.
First, keep in mind that the main memory is divided into blocks for the cache. If we have a cache of 64 bytes and 1 GB of RAM, the RAM will be divided into 128 KB blocks (1 GB RAM / 64V cache = 128 KB Block size).
From book:
Where can I place a block in the cache?
- If in each block there is only one place that can appear in the cache, the cache is considered a direct mapping. The target block is calculated using the following formula:
<RAM Block Address> MOD <Number of Blocks in the Cache>
So, suppose we have 32 RAM blocks and 8 cache blocks.
If we want to save block 12 from RAM to cache, block 12 of RAM will be stored in block 4 of the cache. Why? Since 12/8 = 1 remainder 4. The remainder is the target block.
If a block can be placed anywhere in the cache, the cache is considered completely associative.
If the block can be placed anywhere in a limited set of places in the cache, the cache is installed associatively.
Basically, a set is a group of blocks in a cache. The block is first displayed on the set, and then the block can be placed anywhere inside.
Formula: <RAM Block Address> MOD <Number of Sets in the Cache>
So, suppose we have 32 RAM blocks and a cache divided into 4 sets (each set has two blocks, which means only 8 blocks). Thus, the value 0 will have blocks 0 and 1, for set 1 there will be blocks 2 and 3, etc.
If we want to store the 12 RAM block in the cache, the RAM block will be stored in cache blocks 0 or 1. Why? Since 12/4 = 3 of the remainder 0. Therefore, 0 is chosen, and the block can be placed anywhere inside the set 0 (which means block 0 and 1).
Now I will return to the original address problem.
How is a block detected if it is in the cache?
Each block block in the cache has an address. Just to make it clear, a block has both an address and data.
The block address is divided into several parts: tag, index, and offset.
The tag is used to find the block inside the cache, the index shows only the set in which the block is located (which makes it quite redundant), and the offset is used to select data.
By "select the data" I mean that in the cache block there will obviously be more than one memory location, the offset is used to select between them.
So, if you want to present a table, these will be the columns:
TAG | INDEX | OFFSET | DATA 1 | DATA 2 | ... | DATA N
The tag will be used to find the block, the index will show in which the block is installed, the offset will select one of the fields on the right.
I hope that my understanding of this is correct, if it is not, please let me know.