Calculating Required Storage In a Direct-Mapped Cache
Suppose a 1-level direct-mapped cache need to have 64 KB's data, each block is 16-byte, and the memory address is 32-bit.
To calculate the total bits needed for the cache, we could go through the following steps:
- We need to have 64 KB's data (2^16 bytes) in the cache, and each block is sized as 16 bytes (2^4), so we totally need 2^12 blocks for the data, let's call the "12" as "a." This means a bits are used for the index.
- Each word has 4 bytes, then in this case each block has 2^2 words, let's call the exponent 2 as "b." This means b bits are used for word in each block.
- The size of the tag is determined by the equation "length of address - (a+b+2)," the 2 bits here are the byte offset of the address. So, in this case, it's "32 - (12+2+2) = 16."
- Each block has 16 bytes, each byte has 8 bits, so the amount of data for each block is 16 * 8 + tag + valid bit = 128 + 16 + 1 = 145 bits.
- Times the amount of data for each block (145) to the number of blocks (2^12). Then we get 593920 Bits. Convert to KB, it is 72.5 KB.
So 72.5 KB's of data is required to make a 64KB 1-level direct-mapped cache .
In general, the idea is very simple: total bits required = number of blocks * bits required for each block. Mistakes can usually be made during the process of calculating the size of the tag. Sometimes it's also easy to forget to add the valid bit. But if doing it carefully, we could realize that the way of calculating it is quite simple.