Monday, May 21, 2012

CACHE



There are two main classifications of RAM: static RAM (SRAM) and dynamic RAM (DRAM). SRAM is faster, but that speed comes at a price: it has a lower density and it is more expensive. Since main memory needs to be quite large and inexpensive, it is implemented with DRAM.

A small, fast SRAM is placed between the processor and main memory to hold the most recently used code and data under the assumption that they will most likely be used again soon. This small, fast SRAM is called a RAM cache.

The reason the SRAM of the cache needs to be small is that larger address decoder circuits are slower than small address decoder circuits.

It is possible to take this concept a step further by placing an even smaller SRAM between the cache and the processor thereby creating two levels of cache. This new cache is typically contained inside of the processor.
When two caches are present, the one inside the processor is referred to as a level 1 or L1 cache while the one between the L1 cache and memory is referred to as a level 2 or L2 cache. The split cache is another cache system that requires two caches. In this case, a processor will use one cache to store code and a second cache to store data.

Cache Organization
The success of caches is due primarily to the principle of locality. This suggests that when one data item is loaded into a cache, the items close to it in memory should be loaded too. For example, if a program enters a loop, most of the instructions that make up that loop will be executed multiple times.

Because of this, caches are typically organized so that when one piece of data or code is loaded, the block of neighboring items is loaded too. Each block loaded into the cache is identified with a number called a tag that can be used to determine the original addresses of the data in main memory. This way, when the processor is looking for a piece of data or code, it only needs to look at the tags to see if the word is contained in the cache. The each block of words and its corresponding tag are combined in the cache to form a line.

Cache Operation
When the processor needs a word from memory, it first checks the cache. The circuits used to access the same word from main memory may be activated simultaneously so no time is lost in case the data is not available in the cache, a condition known as a miss. If the search of the cache is successful, then the processor will use the cache's word and disregard the results from main memory. This is referred to as a hit. In the case of a miss, the entire block containing the requested word is loaded into a line of the cache, and the word is sent to the processor.

Cache Characteristics
  • The cache system used by a processor is defined by six traits:
  • •      The size of the cache.
  • •      The size of a block, which when combined with the size of the cache defines the number of lines.
  • •      The number of caches (i.e., multiple levels or a split cache).
  • •      The mapping function (the link between a block's address in memory and its location in the cache).
o   Direct Mapping.
o   Associative Mapping.
o   Set-Associative Mapping.
  • •      The replacement algorithm (the method used to figure out which block to remove from the cache in order to free up a line).
o   Least Recently Used (LRU) – This method replaces the block that
hasn't been read by the processor in the longest period of time.
o   First In First Out (FIFO) – This method replaces the block that
has been in cache the longest.
o   Least Frequently Used (LFU) – This method replaces the block
which has had fewest hits since being loaded into the cache.
o   Random – This method randomly selects a block to be replaced. It
has only slightly lower performance than LRU, FIFO, or LFU.
  • •      The write policy (how the processor writes data to the cache so that main memory eventually gets updated)
o   Write Through Policy : One method for resolving this is to have the cache update memory every time the processor writes to the cache.
o   Write Back Policy : A second method is to update main memory only when a line of the cache that has been modified to is about to be replaced with a new block from memory.