# Calculating the set field of associative cache

In this example:

Assume a system’s memory has 128M words. Blocks are 64 words in length and the cache consists of 32K blocks. Show the format for a main memory address assuming a 2-way set associative cache mapping scheme. Be sure to include the fields as well as their sizes.

The following is supposed to be the solution:

Each address has 27 bits, and there are 7 in the tag field, 14 in the set field and 6 in the word field.

I understand how the word field was calculated (2^6 = 64), but I don't understand how is the set field 14?.

Block size is 64 words, to find the number of sets we divide total number of blocks 32K by set size which is 64*2=128B (multiply by 2 since it's 2-way), my calculation should be correct, am I missing something?

32KB/(64*2)B = 32KB/128B = 256 = 2^8

(Which implies that we need 8 bits for the set field)

• 32K is the number of blocks, not their size. – AProgrammer Jan 12 '17 at 13:11
• @AProgrammer Block size is 64 words, to find the number of sets we divide total number of blocks 32K by set size which is 64*2=128B (multiply by 2 since it's 2-way), my calculation should be correct, am I missing something? – user3125670 Jan 12 '17 at 13:25
• There are 32K blocks of 64 B. So there are 16K sets of 2 blocks. – AProgrammer Jan 12 '17 at 14:01

To calculate the size of set we know that main memory address is a 2-way set associative cache mapping scheme,hence each set contains 2 blocks.

Hence Total no. of sets = Size of cache / Size of set
= (2^15/2^1)
= 2^14


(Which implies that we need 14 bits for the set field)

The memory has $128$M words = $2^{7+20}$ = $2^{27}$ words. Hence it needs needs $27$ bits for address space.

Each block has $64$ words, so the block offset = $2^6$ words. So Block offset field can be represented by $6$ bits.

Number of lines in the cache = $\frac{Cache Size}{Block Size}$ =Number of blocks = $32$K blocks = $2^{5+10}$ = $2^{15}$ blocks.

Number of sets in the cache = $\frac{Number of Lines}{Set Associativity}$ = $2^{15}$ blocks / $2$ blocks per set = $2^{14}$ sets. Hence number of bits to represent set field = $14$ bits.

Number of tag bits = $27 - (14+6) = 7$ bits.