How to calculate the number of tag, index and offset bits of 2 different caches? I have two exercises:
1) A 128KB direct-mapped cache, with lines from 32bytes/lines and 36-bit address.
2) A 512KB 2-way associative cache, with lines from 64bytes/lines and 36-bit address.
For both exercises i have to find the number of bits needed for the tag,index and offset fields.
I tried to do both exercises and i found these results:
1)5 bit offset, 17 bit index, 14 bit tag
2)6 bit offset, 12 bit index, 18 bit tag
First exercise:
128KB = 217
32 bytes = 25 so 5 bit offset
17 bit index
36-5-17=14 bit tag
 Second exercise:
64 bytes = 26 so i have 6 bit offset
512KB = 219
512/2 = 219/2 = 218 cache lines number
218/26= 212 so i have 12 bit index
36-(12+6)=18 bit tag
Assuming that your machine is byte addressable(1 word = 1 byte), Let us solve the problem step by step.

1.

Physical address = 36 bits. Since 32 bytes/line and size of cache line = size of main memory block, this means block offset = 5 bits. Hence remaining 31 bits is block number( = tag + index).

number of cache lines = 128KB/32B, therefore, 12 bits for index and hence remaining 19 bits for tag.

2.

Physical address = 36 bits. Since 64 bytes/line and size of cache line = size of main memory block, this means block offset = 6 bits. 2-way associative cache means that two lines in one set.

number of sets = total cache lines/2 = (512KB/64)/2 = 2^12 , therefore 12 bits for set, and hence remaining 18 bits for the tag.