Cache Memory
Cache Memory
Location-
● Cache memory lies on the path between the CPU and the main memory.
● It facilitates the transfer of data between the processor and the main memory at
the speed which matches to the speed of the processor.
● Data is transferred in the form of words between the cache memory and the CPU.
● Data is transferred in the form of blocks or pages between the cache memory and
the main memory.
Purpose-
● The fast speed of the cache memory makes it extremely useful.
● It is used for bridging the speed mismatch between the fastest CPU and the main
memory.
● It does not let the CPU performance suffer due to the slower speed of the main
memory.
Execution Of Program-
● Whenever any program has to be executed, it is first loaded in the main memory.
● The portion of the program that is mostly probably going to be executed in the
near future is kept in the cache memory.
● This allows CPU to access the most probable portion at a faster speed.
Step-01:
Whenever CPU requires any word of memory, it is first searched in the CPU registers.
Case-01:
● If the required word is found in the CPU registers, it is read from there.
Case-02:
● If the required word is not found in the CPU registers, Step-02 is followed.
Step-02:
● When the required word is not found in the CPU registers, it is searched in the
cache memory.
● Tag directory of the cache memory is used to search whether the required word is
present in the cache memory or not.
Case-01:
● If the required word is found in the cache memory, the word is delivered to the
CPU.
● This is known as Cache hit.
Case-02:
● If the required word is not found in the cache memory, Step-03 is followed.
● This is known as Cache miss.
Step-03:
● When the required word is not found in the cache memory, it is searched in the
main memory.
● Page Table is used to determine whether the required page is present in the main
memory or not.
Case-01:
If the page containing the required word is found in the main memory,
● The page is mapped from the main memory to the cache memory.
● This mapping is performed using cache mapping techniques.
● Then, the required word is delivered to the CPU.
Case-02:
If the page containing the required word is not found in the main memory,
Example-
Three level cache organization consists of three cache memories of different size organized
at three different levels as shown below-
Size (L1 Cache) < Size (L2 Cache) < Size (L3 Cache) < Size (Main Memory)
Cache Mapping-
● Cache mapping defines how a block from the main memory is mapped to the
cache memory in case of a cache miss.
OR
● Cache mapping is a technique by which the contents of main memory are brought
into the cache memory.
NOTES
● Main memory is divided into equal size partitions called as blocks or frames.
● Cache memory is divided into partitions having same size as that of blocks
called as lines.
● During cache mapping, block of main memory is simply copied to the cache
and the block is not actually brought from the main memory.
1. Direct Mapping-
In direct mapping,
● A particular block of main memory can map only to a particular line of the cache.
● The line number of cache to which a particular block can map is given by-
Example-
In direct mapping,
● A block of main memory can map to any line of the cache that is freely available at
that moment.
● This makes fully associative mapping more flexible than direct mapping.
Example-
● Cache lines are grouped into sets where each set contains k number of lines.
● A particular block of main memory can map to only one particular set of the cache.
● However, within that set, the memory block can map any cache line that is freely
available.
● The set of the cache to which a particular block of the main memory can map is
given by-
Example-
Special Cases-
● If k = Total number of lines in the cache, then k-way set associative mapping
becomes fully associative mapping.
Direct Mapping-
In direct mapping,
● A particular block of main memory can map to only one particular line of the
cache.
● The line number of cache to which a particular block can map is given by-
● The line number field of the address is used to access the particular line of the
cache.
● The tag field of the CPU address is then compared with the tag of the line.
● If the two tags match, a cache hit occurs and the desired word is found in the
cache.
● If the two tags do not match, a cache miss occurs.
● In case of a cache miss, the required word has to be brought from the main
memory.
● It is then stored in the cache together with the new tag replacing the previous one.
Implementation-
(For simplicity, this diagram shows does not show all the lines of multiplexers)
● Each multiplexer reads the line number from the generated physical address using
its select lines in parallel.
● To read the line number of L bits, number of select lines each multiplexer must
have = L.
Step-02:
● After reading the line number, each multiplexer goes to the corresponding line in
the cache memory using its input lines in parallel.
● Number of input lines each multiplexer must have = Number of lines in the cache
memory
Step-03:
● Each multiplexer outputs the tag bit it has selected from that line to the comparator
using its output line.
● Number of output line in each multiplexer = 1.
UNDERSTAND
It is important to understand-
Example-
So,
● Each multiplexer selects the tag bit of the selected line for which it has been
configured and outputs on the output line.
● The complete tag as a whole is sent to the comparator for comparison in
parallel.
Step-04:
● Comparator compares the tag coming from the multiplexers with the tag of the
generated address.
● Only one comparator is required for the comparison where-
● If the two tags match, a cache hit occurs otherwise a cache miss occurs.
Hit latency-
The time taken to find out whether the required word is present in the Cache Memory or
not is called as hit latency.
Important Results-
Following are the few important results for direct mapped cache-
● Block j of main memory can map to line number (j mod number of lines in cache)
only of the cache.
● Number of multiplexers required = Number of bits in the tag
● Size of each multiplexer = Number of lines in cache x 1
● Number of comparators required = 1
● Size of comparator = Number of bits in the tag
● Hit latency = Multiplexer latency + Comparator latency