How to use the cache address breakdown calculator
- Enter the cache size and choose its unit: bytes, KiB or MiB.
- Enter the number of ways (1 for direct mapped) and the block size in bytes. Both must be powers of 2.
- Enter the address width in bits, such as 32 or 48.
- Optionally enter an address in hexadecimal to split it. Read the offset, index and tag bits, the number of sets and blocks and the tag storage.
Formula
Offset bits = log2(block size)
Number of sets = cache size / (ways x block size), and index bits = log2(sets)
Tag bits = address width - index bits - offset bits
Tag storage = number of blocks x tag bits
The offset picks the byte in a block, the index picks the set, and the tag is compared with the tags stored in the set to see whether the block is present. The tag storage excludes valid and dirty bits.
A 32 KiB, 4-way cache with 64 byte blocks and 32 bit addresses
The cache holds 32768 / 64 = 512 blocks and has 512 / 4 = 128 sets. The offset takes 6 bits and the index takes 7 bits, which leaves 32 - 7 - 6 = 19 bits for the tag. The tag storage is 512 x 19 = 9728 bits.
For a 1 MiB, 16-way cache with the same block size there are 1024 sets, so the index takes 10 bits and the tag 16 bits. The address 0x1A2B3C then splits into tag 0x1a, set 172 and offset 60. A direct mapped cache has one block per set, so it has more sets and a longer index.
What the fields do
When the processor reads an address, the hardware uses the index bits to find a set and compares the tag with the tag of each block in that set, in parallel. A match is a hit and the offset picks the byte or word. A miss brings in the whole block, and if the set is full a block is chosen for replacement.
To see how the block is chosen, use the cache replacement policy simulator. To convert an address between bases, see the binary calculator.
Trade-offs
More ways reduce conflict misses but cost more comparators and a slower lookup. Larger blocks use spatial locality but waste capacity and time when only a small part is needed. A fully associative cache has one set, no index and the longest tag. A direct mapped cache has the shortest tag and the most conflicts.
Faster memory helps only the part of the run time spent waiting for it, which is why the Amdahl's law calculator is a good companion for estimating the overall gain.
Assumptions and limits
- The cache is physically indexed and tagged, and all addresses are byte addresses.
- Cache size, ways and block size are powers of 2, with at least one set.
- Tag storage counts only the tag bits, not valid, dirty or replacement bits.
- The optional address is hexadecimal and must fit in the address width.
Frequently asked questions
How do I find the number of index bits?
Divide the cache size by the ways times the block size to get the sets, then take log base 2 of the number of sets.
What is the tag for?
Many addresses map to the same set. The tag identifies which block is stored, so the hardware can tell a hit from a miss.
What is a direct mapped cache?
It is a cache with one way, so each address maps to exactly one block. It has the most sets, the fewest comparators and the most conflict misses.
Can I split an address?
Yes. Enter it in hexadecimal and the page shows the tag in hexadecimal, the set number and the byte offset.