Cache Memory
Cache Memory
The CPU logic is usually faster than main memory access time, with the
result that processing speed is limited primarily by the speed of main
memory.
The cache is used for storing segments of program currently being executed
in the CPU and temporary data frequently needed in the present
calculations.
If the active portions of the program and data are placed in a fast small memory,
the average memory access time can be reduced, thus reducing the total
execution time of the program. Such a fast small memory is referred to as a cache
memory.
The basic operation of the cache is as follows. When the CPU needs to access
memory, the cache is examined.
If the word is found in the cache, it is read from the fast memory.
If the word addressed by the CPU is not found in the cache, the main memory
is accessed to read the word. A block of words containing the one just accessed
is then transferred from main memory to cache memory.
Hit/Miss: When the CPU refers to memory and finds the word in cache, it is said to
produce a hit. If the word is not found in cache, it is in main memory and it counts
as a miss .
1. Associative mapping
2. Direct mapping
3. Set-associative mapping
Let the main memory can store 32K words of 12 bits each. The cache is
capable of storing 512 of these words at any given time.
For every word stored in cache, there is a duplicate copy in main memory.
The CPU communicates with both memories. It first sends a 15-bit address to
cache. If there is a hit, the CPU accept the 12-bit data from cache. If there is a miss,
the CPU reads the word from main memory and the word is then transferred to
cache.
Associative Mapping:
The fastest and most flexible cache organization uses an associative memory.
The associative memory stores both the address and content (data) of the
memory word. This permits any location in cache to store any word from main
memory.
The diagram shows thee words presently stored in the cache. The address value of
15 bits is show as a five-digit octal number and its corresponding 12-bit word is
shown as a four-digit octal number.
A CPU address of 15 bits is placed in the argument register and the associative
memory is searched for a matching address.
If the address is found, the corresponding 12-bit data is read and sent to the CPU.
If no match occurs, the main memory is accessed for the word. The address-data
pair is then transferred to the associative cache memory.
If the cache is full, an address-data pair must be displaced to make room for a pair
that is needed and not presently in the cache.
The nine least significant bits constitute the index field and the remaining six bits for
the tag field.
The number of bits in the index field is equal to the number of address bits
required to access the cache memory.
In the general case, there are 2^k words in cache memory and 2^n words in main
memory. The n-bit memory address is divided into two fields:
The direct mapping cache organization uses the n-bit address to access the main
memory and the k-bit index to access the cache.
Each word in cache consists of the data word and its associated tag.
When a new word is first brought into the cache, the tag bits are stored alongside
the data bits.
When the CPU generates a memory request, the index field is used for the
address to access the cache.
The tag field of the CPU address is compared with the tag in the word read from
the cache. If the two tags match, there is a hit and the desired data word is in
cache.
If there is no match, there is a miss and the required word is read from main
memory. It is then stored in the cache together with the new tag, replacing the
previous value.
The disadvantage of direct mapping is that the hit ratio can drop considerably if
two or more words whose addresses have the same index but different tags are
accessed repeatedly.
As shown in previous figure, the word at address zero is presently stored in the
cache (index = 000, tag = 00, data = 1220). Suppose that the CPU now wants to
access the word at address 02000. The index address is 000, so it is used to access
the cache. The two tags are then compared. The cache tag is 00 but the address
tag is 02, which does not produce a match.
Therefore, the main memory is accessed and the data word 5670 is transferred to
the CPU. The cache word at index address 000 is then replaced with a tag of 02 and
data of 5670.
The index field is now divided into two parts: the block field and the word field
In a 512-word cache there are 64 blocks of 8 words each, since 64 x 8 = 512.
The block number is specified with a 6-bit field and the word within the block is
specified with a 3-bit field.
The tag field stored within the cache is common to all eight words of the same
block.
Every time a miss occurs, an entire block of eight words must be transferred from
main memory to cache memory.
Set-Associative Mapping:
It was mentioned previously that the disadvantage of direct mapping is that two words
with the same index in their address but the different tag values cannot reside in
cache memory at the same time.
In Figure, each index address refers to two data words and their associated tags.
Each tag requires six bits and each data word has 12 bits, so the word length is 2(6
+ 12) = 36 bits.
A index address of nine bits can accommodate 512 words. Thus the size of cache
memory is 512 x 36. It can accommodate 1024 words of main memory since each
word of cache contains two data words.
In figure, The words stored at addresses 01000 and 02000 of main memory are
stored in cache memory at index address 000. Similarly, the words at addresses
02777 and 00777 are stored in cache at index address 777.
When the CPU generates a memory request, the index value of the address is used
to access the cache. The tag field of the CPU address is then compared with both
tags in the cache to determine if a match occurs.
The hit ratio will improve as the set size increases because more words with the
same index but different tags can reside in cache.
Problem 1:
a. How many bits are there in the tag, index, block and word fields of the
address format?
b. How many bits are there in each word of the cache, and how are they
divided into functions? Include the valid bit.
The primary advantage of cache is its ability to reduce the average access
time in uniprocessors. When the processor finds a word in cache during a
read operation, the main memory is not involved in the transfer.
Write-through policy: Both cache and main memory are updated with every
write operation.
Write-back policy: Only the cache is updated and the location is marked so
that it can be copied later into main memory.
In a shared memory multiprocessor system, all the processors share a
common memory. In addition, each processor may have a local memory, part
or all of which may be a cache.
The compelling reason for having separate caches for each processor is to
reduce the average access time in each processor.
The same information may reside in a number of copies in some caches and
main memory. To ensure the ability of the system to execute memory
operations correctly, the multiple copies must be kept identical. This
requirement imposes a cache coherence problem.
For simplicity, we assume that X contains the value of 52. The load on X to
the three processors results in consistent copies in the caches and main
memory.
A simple scheme is to disallow private caches for each processor and have
a shard cache memory associated with main memory. Every data access is
made to the shard cache. This method violates the principle of closeness
of CPU to cache and increases the average memory access time. In effect,
this scheme solves the problem by avoiding it.
For performance considerations it is desirable to attach a private cache to each
processor. One scheme that has been used allows only nonshared and read-only
data to be stored in caches. Such items are called cachable. Shared writable data are
noncachable. The compier must tag data as either cachable or noncachable, and the
system hardware makes sure that only cachable data are stored in caches. The
noncachable data remain in main memory. This method restrict the type of data
stored in caches and introduces an extra software overhead that may degradate
performance.