RTL Design #22 · 2026-10-15 · CAMs · Easy · about 10 min

A CAM that finds keys nobody stored

cam4 is a four-entry content-addressable memory: you give it a key and it tells you which entry holds that key.

  • Write. On a rising edge with wr_en = 1, entry wr_idx is replaced. With wr_valid = 1 it now holds wr_key; with wr_valid = 0 it's empty, whatever wr_key is.
  • Lookup. Combinationally, hit is 1 when some entry holds key, and hit_idx is the lowest-numbered entry that does. With no match, hit and hit_idx are both 0.
  • Reset empties every entry.

A key can be any 8-bit value. The current design sometimes reports a hit for a key that isn't stored in any entry. Fix cam.sv.

Write and run your answer in the browser. The hidden tests check it, and it’s free.