RTL Design #73 · 2026-12-05 · Set-associative cache · Medium · about 30 min
A 2-way cache tag array with LRU
Write cache2w, the tag array of a small 2-way set-associative cache: 2 sets, 2 ways per set, LRU replacement. It only tracks which addresses are present; there is no data.
An address is 6 bits: addr[0] is the set, addr[5:1] is the tag.
- Reset and flush. Reset (
rst_nlow, synchronous) andflushboth invalidate every line and make way 0 the least recently used way in both sets. Aflushclock ignoresreq. No response. - Lookup. On a clock with
req= 1 (and no flush), look for a valid line in the set ofaddrwhose tag matches.- Hit: that way becomes the most recently used one in its set.
- Miss: the line is allocated. Pick an invalid way if there is one (way 0 first). If both ways are valid, replace the least recently used way. If the replaced line was valid, report its tag. The filled way becomes the most recently used one.
- Response. All outputs are registered. On the clock after an accepted
req:resp_v= 1,hitsays whether it hit,wayis the way that hit or was filled,evict_v= 1 only when a valid line was replaced, andevict_tagis that line's tag.resp_vis 0 on every other clock. The other outputs are only looked at whileresp_vis 1.
Example, starting from reset: req with tag 3, set 0 misses and fills way 0. Tag 9, set 0 misses and fills way 1. Tag 3, set 0 hits in way 0. Tag 7, set 1 misses and fills way 0 of set 1.
Fill in cache2w.sv. Run drives that example.
Write and run your answer in the browser. The hidden tests check it, and it’s free.