Cache Mapping 1
Cache Mapping 1
In direct mapping,
A particular block of main memory can be mapped to one particular cache line only.
Block ‘j’ of main memory will map to line number (j mod number of cache lines) of the cache.
There is no need of any replacement algorithm.
Problem-01:
Solution-
Given-
We have,
Size of main memory
Problem-01:
Consider a direct mapped cache of size 16 KB with block size 256 bytes. The size of main memory is 128 KB. Find-
1. Number of bits in tag
2. Tag directory size
Solution-
Given-
We have,
Size of main memory
= 128 KB
= 217 bytes
Thus, Number of bits in physical address = 17 bits
We have,
Block size
= 256 bytes
= 28 bytes
Thus, Number of bits in block offset = 8 bits
Problem-02:
Consider a direct mapped cache of size 512 KB with block size 1 KB. There are 7
bits in the tag. Find-
1. Size of main memory
2. Tag directory size
Solution-
Given-
Cache memory size = 512 KB
Block size = Frame size = Line size = 1 KB
Number of bits in tag = 7 bits
We have,
Block size
= 1 KB
= 210 bytes
Thus, Number of bits in block offset = 10 bits
We have,
Number of bits in physical address = 26 bits
Thus, Size of main memory
= 226 bytes
= 64 MB
Problem-03:
Consider a direct mapped cache with block size 4 KB. The size of main memory is
16 GB and there are 10 bits in the tag. Find-
1. Size of cache memory
2. Tag directory size
Solution-
Given-
Block size = Frame size = Line size = 4 KB
Size of main memory = 16 GB
Number of bits in tag = 10 bits
We have,
Size of main memory
= 16 GB
= 234 bytes
Thus, Number of bits in physical address = 34 bits
We have,
Block size
= 4 KB
= 212 bytes
Thus, Number of bits in block offset = 12 bits
Number of Bits in Line Number-
We have-
Number of bits in line number = 12 bits
Thus, Total number of lines in cache = 212 lines
Problem-04:
Consider a direct mapped cache of size 32 KB with block size 32 bytes. The CPU
generates 32 bit addresses. The number of bits needed for cache indexing and the
number of tag bits are respectively-
1. 10, 17
2. 10, 22
3. 15, 17
4. 5, 17
Solution-
Given-
Cache memory size = 32 KB
Block size = Frame size = Line size = 32 bytes
Number of bits in physical address = 32 bits
We have,
Block size
= 32 bytes
= 25 bytes
Thus, Number of bits in block offset = 5 bits
Problem-05:
Consider a machine with a byte addressable main memory of 232 bytes divided into
blocks of size 32 bytes. Assume that a direct mapped cache having 512 cache lines
is used with this machine. The size of the tag field in bits is ______.
Solution-
Given-
Main memory size = 232 bytes
Block size = Frame size = Line size = 32 bytes
Number of lines in cache = 512 lines
We have,
Size of main memory
= 232 bytes
Thus, Number of bits in physical address = 32 bits
Number of Bits in Block Offset-
We have,
Block size
= 32 bytes
= 25 bytes
Thus, Number of bits in block offset = 5 bits
Problem-06:
Solution-
Given-
Cache memory size = 8 KB
Block size = Frame size = Line size = 32 bytes
Number of bits in physical address = 32 bits
We have,
Block size
= 32 bytes
= 25 bytes
Thus, Number of bits in block offset = 5 bits
A block of main memory can be mapped to any freely available cache line.
This makes fully associative mapping more flexible than direct mapping.
A replacement algorithm is needed to replace a block if the cache is full.
Problem-01:
Consider a fully associative mapped cache of size 16 KB with block size 256 bytes. The size of main memory is 128 KB. Find-
1. Number of bits in tag
2. Tag directory size
Solution-
Given-
We have,
Size of main memory
= 128 KB
= 217 bytes
Thus, Number of bits in physical address = 17 bits
We have,
Block size
= 256 bytes
= 28 bytes
Thus, Number of bits in block offset = 8 bits
Problem-02:
Consider a fully associative mapped cache of size 512 KB with block size 1 KB. There are 17 bits in the tag. Find-
1. Size of main memory
2. Tag directory size
Solution-
Given-
We have,
Number of bits in physical address = 27 bits
Thus, Size of main memory
= 227 bytes
= 128 MB
Problem-03:
Consider a fully associative mapped cache with block size 4 KB. The size of main memory is 16 GB. Find the number of bits in
tag.
Solution-
Given-
We have,
Size of main memory
= 16 GB
= 234 bytes
Thus, Number of bits in physical address = 34 bits
We have,
Block size
= 4 KB
= 212 bytes
Thus, Number of bits in block offset = 12 bits
A particular block of main memory can be mapped to one particular cache set only.
Block ‘j’ of main memory will map to set number (j mod number of sets in cache) of the cache.
A replacement algorithm is needed if the cache is full.
Problem-01:
Consider a 2-way set associative mapped cache of size 16 KB with block size 256 bytes. The size of main memory is 128 KB.
Find-
1. Number of bits in tag
2. Tag directory size
Solution-
Given-
Set size = 2
Cache memory size = 16 KB
Block size = Frame size = Line size = 256 bytes
Main memory size = 128 KB
We have,
Size of main memory
= 128 KB
= 217 bytes
Thus, Number of bits in physical address = 17 bits
We have,
Block size
= 256 bytes
= 28 bytes
Thus, Number of bits in block offset = 8 bits
Problem-02:
Consider a 8-way set associative mapped cache of size 512 KB with block size 1 KB. There are 7 bits in the tag. Find-
1. Size of main memory
2. Tag directory size
Solution-
Given-
Set size = 8
Cache memory size = 512 KB
Block size = Frame size = Line size = 1 KB
Number of bits in tag = 7 bits
We have,
Block size
= 1 KB
= 210 bytes
Thus, Number of bits in block offset = 10 bits
We have,
Number of bits in physical address = 23 bits
Thus, Size of main memory
= 223 bytes
= 8 MB
Problem-03:
Consider a 4-way set associative mapped cache with block size 4 KB. The size of main memory is 16 GB and there are 10 bits
in the tag. Find-
1. Size of cache memory
2. Tag directory size
Solution-
Given-
Set size = 4
Block size = Frame size = Line size = 4 KB
Main memory size = 16 GB
Number of bits in tag = 10 bits
We have,
Size of main memory
= 16 GB
= 234 bytes
Thus, Number of bits in physical address = 34 bits
We have,
Block size
= 4 KB
= 212 bytes
Thus, Number of bits in block offset = 12 bits
We have-
Number of bits in set number = 12 bits
Thus, Total number of sets in cache = 212 sets
We have-
Total number of sets in cache = 212 sets
Each set contains 4 lines
Thus,
Total number of lines in cache
= Total number of sets in cache x Number of lines in each set
= 212 x 4 lines
= 214 lines
Problem-04:
Consider a 8-way set associative mapped cache. The size of cache memory is 512 KB and there are 10 bits in the tag. Find the
size of main memory.
Solution-
Given-
Set size = 8
Cache memory size = 512 KB
Number of bits in tag = 10 bits
Sum of Number Of Bits Of Set Number Field And Block Offset Field-
We have,
Cache memory size = Number of sets in cache x Number of lines in one set x Line size
Now, substituting the values, we get-
512 KB = 2x x 8 x 2y bytes
219 bytes = 23+x+y bytes
19 = 3 +x + y
x + y = 19 – 3
x + y = 16
We have,
Number of bits in physical address = 26 bits
Thus, Size of main memory
= 226 bytes
= 64 MB
Thus, size of main memory = 64 MB
Problem-05:
Consider a 4-way set associative mapped cache. The size of main memory is 64 MB and there are 10 bits in the tag. Find the
size of cache memory.
Solution-
Given-
Set size = 4
Main memory size = 64 MB
Number of bits in tag = 10 bits
We have,
Size of main memory
= 64 MB
= 226 bytes
Thus, Number of bits in physical address = 26 bits
Sum Of Number Of Bits Of Set Number Field And Block Offset Field-
Let-
So, we have-
26 bits = 10 bits + x bits + y bits
26 = 10 + (x + y)
x + y = 26 – 10
x + y = 16
Thus, Sum of number of bits of set number field and block offset field = 16 bits