Master the fundamental concepts of memory hierarchy through this focused micro-challenge.
A direct-mapped cache splits physical addresses into tag, index, and offset. Each index maps to exactly one cache line; conflicts evict even if other lines sit idle.
cLoading…
On access:
index with computed tagFor example, with 64-byte lines and 256 lines, index is 8 bits and two addresses that agree on those bits contend for the same slot.
For this exercise, you will simulate loads and stores, counting hits and misses. This task asks you to implement tag compare and line fill, the same mechanics perf indirectly observes when LLC miss rate spikes.
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.
Simulate a direct-mapped cache. Each address is split into tag / index / offset. The index picks exactly one line, and a stored tag decides hit or miss. The cache is write-back, write-allocate, so stores mark lines dirty and dirty lines are written back when evicted. Trace every access and classify each miss as a cold miss (empty line) or a conflict miss (another tag was there).
cLoading…
offset = the low log2(LINE) bits. index = the next log2(SIZE / LINE) bits. tag = the remaining high bits.
W hit sets the dirty bit.W.cLoading…
(, writeback only if the evicted line was dirty.) dump prints line I: tag=0xT plus dirty for each valid line in index order, or cache empty. At the end:
cLoading…
with P to one decimal (0.0 when there were no accesses). Addresses and tags print in lower-case hex without leading zeros.
Input:
cLoading…
Output:
cLoading…
{valid, dirty, tag}; compute the field widths from SIZE and LINE with shifts and masks.Hidden tests cover two arrays that map to the same lines (ping-pong conflicts), writes that dirty lines and later write back, a cache with a single line, and dump.
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 works