2026-09-11
Stack Overflow: View Question
Tags: caching, verilog, cpu-architecture, system-verilog, best-practices
Score: 0 | Views: 150
The asker is designing a direct-mapped, write-back, write-allocate L1 cache for a custom RISC-V core. The cache line is 128 bits (4 words), main memory uses a synchronous interface with a one-cycle mem_ready handshake, and the specific question is whether the write-hit path should be handled combinationally (tag compare and data write happen in the same cycle the CPU asserts the request) or sequentially (latched into a state machine that takes at least one extra cycle).
Why this is genuinely hard: it's not a Verilog-syntax question — it's a microarchitecture trade-off that shapes the entire pipeline. The combinational option looks attractive because a write-hit should logically be "free": tag matches, mux the word into the line, done. But collapsing tag compare, way select, byte-enable generation, and SRAM write-enable into one cycle piles logic onto the critical path. On any real FPGA (block RAMs are synchronous-write only) or ASIC (SRAM macros clock the write port), you physically cannot commit the write in the same cycle you finished comparing tags — the tag RAM output isn't valid until the clock edge, and the data RAM needs its address and write-enable stable before the next edge.
A cleaner framing: separate the decision (hit/miss) from the commit (SRAM update). A typical layout:
This gives single-cycle throughput for hits (one write commits per cycle in steady state) while respecting the fact that SRAMs are edge-triggered. The FSM only really needs states for miss handling: IDLE, ALLOCATE (issue memory read, wait for mem_ready), WRITEBACK (if evicting a dirty line), and back to IDLE. Write-hits should not visit the FSM at all — they're a fast path.
Gotchas:
sb/sh to a 128-bit line requires read-modify-write of the word within the line — trivial with byte write-enables on the SRAM, painful without them.mem_ready: the spec says data is valid "for one cycle" — you must capture it into a register on that cycle or lose it.