Master the fundamental concepts of memory hierarchy through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksWhen a cache set is full and a new tag arrives, a replacement policy picks the victim line. LRU (Least Recently Used) evicts the line untouched longest. It approximates temporal locality well though real hardware often uses pseudo-LRU trees for speed.
cLoading…
For example, accessing ways A, B, A in a 2-way set leaves B as LRU; the next conflicting miss evicts B.
For this exercise, you will add LRU tracking to your set-associative simulator. This task asks you to log victim way indices and verify LRU order on a scripted access sequence before benchmarking realistic traces.
Keep the relevant datasheet, ISA manual, or architecture textbook chapter open while you implement. When your output disagrees with the reference trace on the same program, the bug is usually a mis-decoded opcode, a stale register read, or a flag bit left unchanged after arithmetic.
For this exercise, you will use those habits while implementing the requirement in the starter code. Microarchitectural product names change across CPU generations, but the control ideas (fetch, bypass, cache lines, vector lanes) stay stable enough to debug from first principles.
Compare three cache replacement policies on the same trace of block numbers. LRU evicts the block unused for the longest time, tracked with per-way counters. FIFO evicts the block that was brought in first, whether or not it has been used since. Random evicts a pseudo-random way. Print the LRU recency order after every access, then the hit/miss pattern of each policy.
cLoading…
Block B maps to set B mod S. The cache starts empty, and each policy runs the trace on its own fresh cache.
On a hit, nothing is evicted. On a miss, the block goes into the lowest-numbered empty way if the set has one. Otherwise a victim is chosen:
x = (x · 1103515245 + 12345) mod 2^31, starting from x = seed. The victim is way (x >> 16) mod W. The generator advances only when a victim is needed.cLoading…
order lists the set's blocks from most to least recently used, after the access.H or M per access.best names the policy with the most hits. On a tie, the earlier in the order LRU, FIFO, Random wins.Input:
cLoading…
Output:
cLoading…
Hidden tests cover a looping trace one block larger than a set (where LRU gets zero hits and Random does better), and a trace with a hot block that FIFO keeps evicting.