A posting write buffer made writes fast and broke a read. Find the first wrong cycle, name the cause from five real bugs, and fix the buffer.
Hardware context: A burst of beats delimited by last on a valid/ready link. Diagnose and fix a posting write buffer that lets a read pass a write that has not landed.
Lesson P1.4 showed why posted writes are fast: the initiator moves on the moment a write is taken. The data lands later. So some block between the initiator and the registers holds writes that are complete but not yet written.
That block is a write buffer, and it owes every read one promise. A read must return the newest value written before it, whether that write has landed or not. This buffer breaks the promise.
A firmware driver programs a peripheral with posted writes and reads back some registers. The log says: ERROR: read 0 returned a5, expected 55. The driver is correct. The bug is in the buffer.
Step 1: find the first cycle where a signal shows a value a correct buffer would not, and name the signal. Step 2: choose the cause from five real bugs. Step 3, optional: fix the buffer and run the tests.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
steps | 5 | input of each check |
Full specification
module wbuf #(parameter int WLAT = 4, parameter int N = 4) (
input logic clk, rst_n,
input logic wr_valid, input logic [3:0] wr_addr, input logic [7:0] wr_data, output logic wr_ready,
input logic rd_valid, input logic [3:0] rd_addr, output logic rd_rvalid, output logic [7:0] rd_data,
output int pending);
| Rule | Correct buffer |
|---|---|
| Writes | A write is taken at an edge where wr_valid and wr_ready are 1. It lands in the registers WLAT edges later, oldest first. pending counts writes taken and not landed. |
| wr_ready | 1 while pending is below N. |
| Reads | A read is taken at every edge where rd_valid is 1. rd_rvalid and rd_data answer it one edge later. |
| Read data | The newest value written to rd_addr by a write taken before the read or at the same edge. Registers start as a0 + address. |
Cycle N is the stretch where the cycle counter shows N. A signal is wrong in cycle N if its value there differs from what a correct buffer shows.
The tests run nine more programs and report the data of every read, in order, and the number of reads answered.
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 3 is optional: fix the code in the editor, then run the tests.
Examples
Example 1
Read Straight After a Write- Input
Registers start as a0 + address. Steps: W 3=31, R 3, R 4
- Output
reads = 2, read_data = 0xa431
The write of 31 to address 3 is still in the buffer when the read arrives. The read must see 31, not a3. The read of address 4 sees a4.
Example 2
Two Pending Writes- Input
Registers start as a0 + address. Steps: W 6=61, W 6=62, R 6, R 6
- Output
reads = 2, read_data = 0x6262
Two writes to address 6 wait in the buffer. Both reads must return the newer value, 62.
Example 3
The Same Edge- Input
Registers start as a0 + address. Steps: W 9=91 + R 9, R 9
- Output
reads = 2, read_data = 0x9191
The write and the read of address 9 are taken at the same edge. The read counts as later, so it returns 91.
Constraints and Contract
- WLAT is 6 and N is 4: writes can arrive faster than they land. Addresses are 4 bits and data 8 bits.
- The driver holds a write while wr_ready is 0, and sends a read in the cycle its write goes in.
- Keep the module name, its parameters and its ports.
Hints
Hint 1
Find the first read whose answer is wrong. Which write did it follow, and had that write landed yet?
Hint 2
Compare pending with the read: a write that is still pending must win over the registers.
Hint 3
Each of the five bugs breaks a different case. Ask which case the program reaches first. It may be one pending write, a near address, two pending writes, a write at the same edge or a full buffer.
What the grader checks: The failing run is simulated live. The first bad cycle and each option's first wrong value come from simulating every buffer on the same bench. The optional fix is checked against a model of the read rule on ten programs.
10 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 2 steps and the final capstone