Fix a write-back cache controller that skips the write-back of a dirty line before its refill.
Hardware context: Single-core cache with explicit geometry, write policy and replacement policy. Expose cache victim reuse while a dirty writeback remains stalled.
A cache controller passed every read-only test. With writes, some written data never reached memory. On a miss to a dirty line, the controller fetched the new line without saving the old one.
The module submitted_cache is a blocking write-back cache: it handles one CPU request at a time. It has two direct-mapped sets of one 4-byte word. Memory sits behind a valid/ready handshake. A flush writes back every dirty line and empties the cache.
The tests run three request scripts of 128 clock edges, with memory stalls and two flushes. A trusted checker compares every response and memory transfer with a flat memory model. Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
fixture | 4 | input of each check |
request_count | 4 | input of each check |
Full specification
module submitted_cache(
input logic clk, reset,
input logic cpu_valid, cpu_write,
input logic [5:0] cpu_address,
input logic [31:0] cpu_wdata,
input logic [3:0] cpu_strobe,
input logic flush,
input logic mem_ready,
input logic [31:0] mem_rdata,
output wire cpu_ready,
output logic response_valid,
output logic [31:0] response_data,
output logic mem_valid, mem_write,
output logic [5:0] mem_address,
output logic [31:0] mem_wdata,
output logic flush_done);
// your cache
endmodule
| Cache rule | Meaning |
|---|---|
| Shape | Two direct-mapped sets of one 4-byte word. The set is address bit 2 and the tag is address bits 5:3. Write-back, with allocation on a write miss. |
| Request | Accepted on an edge where cpu_valid = 1 and cpu_ready = 1. cpu_ready is 0 while the cache is busy, a flush is waiting or flush is 1. One request is handled at a time. |
| Hit | The response comes on the accepting edge: response_valid = 1 for one cycle after it. A write merges the enabled bytes, marks the line dirty and returns the merged word. |
| Dirty miss | First offer the old line: mem_valid = 1, mem_write = 1, the old address and its word. Hold all of them until an edge with mem_ready = 1. |
| Refill | Then offer a read of the requested address. On the edge with mem_ready = 1, take mem_rdata, merge any write, install the line and respond. |
| Flush | A one-cycle pulse, remembered until the current request ends. Then write back each dirty line, set 0 first, and invalidate both. flush_done pulses for one cycle at the end. |
| Reset | Synchronous and active high. It clears every line and state. |
| Test run | Details |
|---|---|
| Memory | Word i starts at 0x10203040 + i * 0x01010101. mem_rdata always shows the word at mem_address. |
| Plan 1 | 8 requests. Memory is ready on every fifth edge (edges 4, 9, 14 and so on). |
| Plan 2 | 7 requests. Memory is ready on edges not divisible by 3, but never on edges 8 to 15. |
| Plan 3 | 8 requests. Memory is ready on every edge. |
| Flush | A flush pulse arrives on edges 6 and 100. Each run lasts 128 edges after reset, counting from 0. |
The checker keeps a byte-level model of memory. Every response must equal the model's word. Every memory write must carry the model's word for its address. Memory must match the model at each flush_done and at the end. A stalled memory offer must not change.
violation in the result is yes when any of these checks failed.
Examples
Example 1
Plan 1: Evict, Then Refill- Input
Plan 1: read 0x00, write 0x00 = 0xaabbccdd strobes 0101, read 0x00, write 0x08 = 0x11223344, read 0x00, write 0x04 = 0x89abcdef strobes 1010, write 0x0c = 0x55667788 strobes 0011, read 0x04. Memory ready every fifth edge.
- Output
violation = no
The write to 0x0c on edge 35 evicts the dirty line of 0x04. The cache writes 0x8921cd41 to 0x04 on edge 39, then reads 0x0c. The last read of 0x04 must return 0x8921cd41.
Example 2
Plan 2: A Flush During a Long Stall- Input
Plan 2: write 0x04 = 0x80000001 strobes 1000, read 0x04, write 0x0c = 0x01020304 strobes 0001, read 0x04, write 0x00 = 0xffffffff strobes 0000, read 0x08, read 0x00. Memory never ready on edges 8 to 15.
- Output
violation = no
The flush on edge 6 must write 0x13233304 back to 0x0c. Memory is not ready from edge 8 to 15, so the offer waits unchanged until edge 16. flush_done follows on edge 17.
Example 3
Plan 3: A Flush During a Miss- Input
Plan 3: write 0x00 = 0x91827364, write 0x08 = 0x10203040, write 0x10 = 0xa5a55a5a strobes 0101, read 0x18, read 0x00, read 0x08, read 0x10, read 0x10. Memory always ready.
- Output
violation = no
Each write to set 0 evicts the previous dirty line: 0x00, then 0x08. The flush on edge 6 arrives during the miss for 0x10. The cache finishes that request first, then writes 0x14a5345a back.
Constraints and Contract
- There is no reset in the middle of a run. Inputs are always known 0 and 1 values.
- The cache may stall, but it must finish every request and both flushes within the 128 edges.
- Keep the module name and the ports.
Hints
Hint 1
Trace plan 1. The write to 0x0c needs set 1, which holds dirty data for 0x04. What must memory see before the read of 0x0c?
Hint 2
A dirty line needs its own step: offer the write, wait for mem_ready, clear dirty, then start the refill.
Hint 3
Look at the miss branch in state 0. It goes straight to state 2 and never tests dirty. A dirty line must go to state 1, the write-back state, first.
What the grader checks: The cache runs three scripts of 128 edges with three memory stall patterns and two flushes. A checker with a flat memory model checks every response, memory write and flush. Passing shows that these scripts run cleanly, not a proof for every schedule.
3 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 8 steps and the final capstone