Cache Memory: CS 322M Digital Logic & Computer Architecture
Cache Memory: CS 322M Digital Logic & Computer Architecture
Cache Memory
J. K. Deka
Professor
Department of Computer Science & Engineering
Indian Institute of Technology Guwahati, Assam.
Cache Mapping
• Direct Mapping
• Associative Mapping
• Set Associative Mapping
– K way Set Associative Mapping
Mapping: Example
• A block set associative cache consists of a
total of 64 lines divided into 4-line sets. The
main memory contains 4096 blocks, each
consisting of 128 words.
– What is the size of main memory and cache
memory
Mapping: Example
• A block set associative cache consists of a
total of 64 lines divided into 4-line sets. The
main memory contains 4096 blocks, each
consisting of 128 words.
– How many bits are there in a main memory
address
– How many bits are there in each of the TAG,
SET and WORD fields.
Replacement Algorithms
• Direct mapping
– No choice
– Each block only maps to one line
– Replace that line
Replacement Algorithms
• Associative & Set Associative
– Hardware implemented algorithm (speed)
– Least Recently used (LRU)
• e.g. in 2 way set associative
• Which of the 2 block is lru?
– First in first out (FIFO)
• replace block that has been in cache longest
– Least frequently used
• replace block which has had fewest hits
– Random
Replacement Algorithms
• Least Recently Used (LRU)
– Program usually stays in localized area for a
reasonable period of time.
– There is a high probability that the blocks that
have been referenced recently will be
referenced again soon.
– When a block is to be overwritten, it is sensible
to overwrite the one that has gone the longest
time without being referenced.
– This block is called the Least Recently Used
(LRU) block and the technique is called the
LRU replacement policy.
Least Recently Used (LRU)
• Consider four-line set in a set-associative
cache
• Control bits:
– TAG bits
– 2-bit counter for each line(to track the LRU
block)
– d_bit: dirty bit
– f_bit: occupied bit
• Initially reset all the counters, d_bit and
f_bit
Least Recently Used (LRU)
• A cache hit occurs:
– set the counter value to 0 of this cache line
– for other counters, if the value is less than the
referenced line, increment the counter value
provided f_bit is 1.
– otherwise, do not change the counter value
Least Recently Used (LRU)
• A cache miss occurs:
– Set is not full
– Set is full
• Cache organization:
– 16 lines/blocks in the cache
– block size is 512
• Data in Main memory:
– block0 contains i, j, etc
– block1 onward: Array A
– block25 onward: Array B
Example
Array stored in memory:
Computer Organization
Hamacher, Vranesic and Zaky, Fifth Edition