S3.5.3 The Stale Read After a Buffered Write

HardDebug40 minIcarus Verilog · serverReview the topic

A program writes a word twice and reads it back before memory has caught up. It gets the first value. Find the first wrong sample in the write buffer, name the cause, and repair it.

Hardware context: A direct-mapped cache with its controller in front of a memory on the bus, acted out edge by edge as a script. Designing and timing the miss path of a cache

A cache hands its writes to a write buffer with two entries, and memory takes them one at a time. A read that misses in the cache asks the buffer first: if a write to that address is still waiting, the read must get the data of that write.

In the field, a read now and then returns a value that the program had already replaced. Run the failing bench. Select the first cycle and signal that differ from a correct buffer, choose the cause, and then repair the buffer.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
PartContract
pushAt a rising edge with push at 1 and full at 0, the write is kept. It can be found from the next cycle on.
full1 while two writes wait.
mem_write, mem_addr, mem_wdataThe oldest waiting write is offered to memory. mem_write is 1 while any write waits.
mem_doneAt a rising edge with mem_done at 1, the offered write leaves the buffer. A push at the same edge is kept as well.
look_addr, look_hit, look_dataNo clock edge in between. look_hit is 1 when a waiting write has this address. look_data is the data that memory will hold for this address once the buffer is empty, and 0 when look_hit is 0.
rst_nAt a rising edge with rst_n at 0 the buffer becomes empty.

Diagnose the Failing Run

The test below runs the buggy design. Read its log, then find where it first goes wrong in the waveform.

Log

Waveform

Click a signal at a cycle to select it. Or focus the waveform and use the arrow keys: Left and Right move one cycle, Up and Down one signal.

Step 1 · The first bad cycle and signal
Step 2 · The cause

Step 3 is optional: fix the code in the editor, then run the tests.

Examples

Example 1

One Write Waits
Input
push 0x00001111 for address 0x20, look for 0x20; then look for 0x20; then look for 0x21
Output
search_hits = 1, last_found = 0x1111, signature = 0x540851

In the cycle of the push the write is not in the buffer yet. One cycle later the search finds it. Address 0x21 is not there, so its search misses and the data is 0.

Example 2

Two Writes Wait
Input
push 0x00001111 for address 0x20, look for 0x20; then push 0x00003333 for address 0x30, look for 0x20; then look for 0x30; then memory takes the oldest write, look for 0x20; then look for 0x20; then look for 0x30
Output
search_hits = 4, last_found = 0x3333, signature = 0xc45882b1

Two writes wait. Both can be found. Memory takes the older one, for 0x20, and from the next cycle on only 0x30 is found.

Example 3

A Third Write Must Wait Outside
Input
push 0x0000aaaa for address 0x40, look for 0x40; then push 0x0000bbbb for address 0x41, look for 0x40; then look for 0x41; then look for 0x42
Output
search_hits = 2, last_found = 0xbbbb, signature = 0x2247ffa

The buffer holds two writes. The third push comes while it is full, so the cache does not make it: the bench leaves it out. Address 0x42 is not found.

Constraints and Contract

  • Keep module wbuf and its ports. The bench never pushes while full is 1. Every input carries a known value from the first reset on.

Hints

Hint 1

Find the first cycle in which two writes wait for the same address.

Hint 2

Ask which of the two memory will hold in the end.

Hint 3

Check every place where the two entries are told apart: older and newer.

What the grader checks: The failing run shows one fault. The fix is checked on hidden sequences: one and two waiting writes, two writes to one address, a push in the cycle in which memory takes a write, a reset with writes waiting, each address bit changed alone, and random sequences. Each check compares a signature over every output in every cycle.

79 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 4 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

One Write Waits

Input
push 0x00001111 for address 0x20, look for 0x20; then look for 0x20; then look for 0x21
Expected output
search_hits = 1, last_found = 0x1111, signature = 0x540851

In the cycle of the push the write is not in the buffer yet. One cycle later the search finds it. Address 0x21 is not there, so its search misses and the data is 0.

Run samples executes all 3 public checks. Check all tests runs 79 checks on the server.

Icarus Verilog · server