



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The design of a memory for a general purpose computer must take into account that mem- ory is not uniform, but organized in a hierarchy according to cost and speed.
cost/bit speed
size
- register - cache - main memory - secondary memory (disk) - network connection
Typical costs and speeds for these components are shown on bottom p. 469.
In recent years, the widespread use of pipeline techniques in processor design has made processor performance increase much more rapidly than memory performance (fig. 7.37, p. 554). It is now necessary to have a cache hierarchy (fig. 7.3, p. 473) to keep up with the processor.
It is now common practice to have several levels of on chip and off chip cache memory as shown below. Note that there is a separate L1 cache for instructions and data.
L1 Instruction Cache L1 Data Cache
Increasing speed, cost per bit
Increasing size
processor chip
L2 Cache
L3 Cache
Main Memory
The pipelined processor designs require one instruction/clock which requires the data and instruction memories to respond within one clock. Memories this fast are most economi- cal to build inside the same silicon chip as the processor. Both on chip caches can be filled over the same external data bus which helps reduce cost. Note that this implies external caches and main memory has both instructions and data.
The on chip caches cannot be very large since they would take up too much of the chip area. A larger external cache is usually provided that is still very fast and expensive.
Cheaper (but slower) memory can be used for main memory.
At first glance, the cache memories may not seem to be very effective at increasing overall system speed. We can retrieve data and instructions at a very high speed out of the cache memories and keep up with the CPU until we run out of space in the cache memories. At this point, we must read in instructions and data from the slower main memory. The aver- age throughput seems to limited by the slower main memory. The cache memories actu- ally do increase system throughput because of locality of reference.
Locality of Reference. The French word “cache” (pronounced like “kawsh”) means a place to store things that will be used later. The idea is that any instructions or data that are currently being used by the CPU are likely to be used again in the near future. If they are used again, the instructions and data will already be in cache and no main memory access delays will be necessary. If the desired instructions and data are found in the cache most of the time (a high cache “hit” rate), then the average memory response time can be made to be close to the (fast) cache memory response time rather than the (slower) main memory response time.
The technical term for using the same data and instruction addresses over and over again is “locality of reference.”
- temporal locality - program loops produce similar sequences of addresses until the loop finishes. - spatial locality - each segment of a program (instruction, data, etc.) is contained in a set of addresses that are close to each other. - sequentiality - if address n is used, address n+1 is likely to be used next. This is true for instructions and arrays of data.
Locality of reference implies that a “block” of contiguous addresses should be stored in cache at one time. Typical block sizes are from 4 to 32 words (32 to 256 bytes if a word is 4 bytes).
Cache Addressing
To store a block in cache, it is necessary to choose a cache address for the block. The pro- cessor must then be able to find the block quickly whenever it is addressed again. It is essential that the address translation process (between real addresses and cache addresses)
The tag field contains the leftover bits that form the rest of the real address not used in the index or block offset.
A direct mapped cache has only a single comparator making it much cheaper. This com- parator compares the tag of the data in cache with the tag bits in the address. If the tag bits match and the cache has valid data at the index location, then there is a hit. Otherwise, there is a miss and the cache does not contain the data for that address.
Note that addresses with the same cache index all map into the same physical location in the cache (see fig. 7.5, p. 475). Addresses with different tags, but the same index, cannot be stored in memory simultaneously (we can’t put data anywhere we want) which can sig- nificantly decrease cache hit rate.
A direct mapped cache with several words per block (more realistic) is shown in fig. 7.9, p. 486. Part of the address is used as a block offset to control the MUX which selects the correct word to put on the data bus.
Note that hit signals are not used in the address decoding. Thus, the cache memory puts out data regardless of whether a hit occurred or not. When reading data from cache, the data and tag memories can operate in parallel. This does not work so well for writing as we shall see.
Set Associative Cache. This is a compromise between fully associative and direct mapped cache. The cache is divided into several sets. The bits in the real address are used to select a unique location in each set, just like direct mapped cache. Each set has a com- parator that determines a hit from that set, again just like direct mapped cache. However, since there are several sets, it is possible to store many data simultaneously with the same address tags. More words with the same address tag are allowed at the expense of adding additional comparators.
Fig. 7.17, p. 503, shows a four-way set associative cache which allows storing four data simultaneously with the same address tags.
If we define memory size as the number of data values stored (don’t count the tag store size), then
index bit size
tag bit size
index bit size
Direct mapped cache corresponds to a set size of 1, while fully associative cache corre- sponds to an index bit size of 0.
If the cache and main memory size is fixed, the set associative cache must store more bits in the tag as the set size increases. The above two equations can be used to eliminate
index bit size and solve for the tag bit size.
Sector Organized Cache. This is similar to a direct mapped cache except that all blocks in the same sector have the same tag. This allows considerable savings in memory size since it is no longer necessary to store a tag entry for each block as in the previous designs. The price paid is that an entire sector is invalidated on any single cache miss for one of the blocks in the sector (there is no place else to write the block, but one sector; so, the stored tag for the sector must be changed). The access time for the sector organized cache is the same as the direct mapped cache since only a single (sub) block is read in at a time. The hit rate for the sector cache is lower, unless the data has a high degree of sequentiality. This is why sector organized cache is often used for instruction cache since instructions do have a high degree of sequentiality.
Cache Miss on Read
When the desired address is not in the cache (no hit), a cache miss occurs. First, a quick decision must be made about which block must be freed to store the new information. Then the cache must be filled by reading data from the next lower level of memory. All the while this is occurring, the CPU must be stalled until the memory data is available.
Block Replacement. This is not a problem with the direct mapped cache since there is only one place to store each block. If that one place is already occupied, then that block is replaced. For the set associative caches, a decision must be made as to which member of the set to replace.
There are many block replacement algorithms which could be used to replace blocks in cache memory. The decision algorithm must be very fast to reduce the time the CPU is stalled. This short time interval requires that the decision algorithm be implemented in hardware. Then, the algorithm must be simple to keep the hardware expense down and the speed up.
- Random Replacement - a block is chosen at random. A hardware pseudo-random bit generator (see the VLSI course) can be used to decide which block in a set to replace. - Least Recently Used (LRU) - the block that has been unused for the longest time is cho- sen for replacement.
With a two-way associative cache, the LRU algorithm is easy to implement. Since we only have two choices, we simply choose to replace the entry that was not referenced last. This requires only a single bit to keep track of which entry was referenced last.
LRU is much more complex to implement for larger set sizes.
Studies have shown that LRU does not do significantly better than random replacement for large caches.
Cache Miss on Write. Now we can consider what to do when a CPU-write-cache-miss condition occurs.
Write Buffers. Note that a cache miss on write always requires some kind of lower level memory operation whether or not fetch-on-write is done. However, it is not necessary for the CPU to wait for the write operation to complete in lower level memory (quite different for read since the CPU must wait for the data it wants before it can proceed). It makes sense then to store the write data (in place called the write buffer) and let the CPU con- tinue with the next instruction.
As mentioned above, the CPU must wait for memory reads, but can proceed on memory writes by storing the write data temporarily in the write buffer. CPU delays are reduced if we allow a memory read operation to take precedence over any unfinished writes in the write buffer. The trouble with write buffers is that it is difficult to insure that the correct data gets out of the write buffer before it is used by a read. We would have to check all of the data addresses in the write buffer before allowing a read to proceed. This makes the write buffer into another cache! Since the write buffer usually has only a few words stored in it, it is small enough to be implemented as fully associative cache.