Fix the read-during-write choice of a dual-port RAM so that the NEW policy returns the word just written.
Hardware context: Single-clock dual-port byte-enabled RAM. Predict same-address read/write data according to the selected RAM policy.
A RAM is set to the NEW read-during-write policy. One port may read a word on the edge that the other port writes it. The read should then return the new word. This RAM always returns the old one.
read_snapshot() picks the value a read returns. The RAM passes it the word before the edge and the word after both writes of that edge. It also passes the policy bit new_data. The chosen value enters the response pipeline and comes out one edge later.
The tests drive the real RAM with both policies, 1, 2 or 4 byte lanes and depths 1, 3 and 5. They compare every response with an independent byte model. Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
active_lanes | 3 | input of each check |
memory_depth | 3 | input of each check |
read_policy | 1 | input of each check |
write_priority | 2 | input of each check |
image_seed | 32 | input of each check |
Full specification
function automatic bit[31:0] read_snapshot(input bit[31:0] old_word, new_word, input bit new_data);
| Policy | The read returns |
|---|---|
| OLD (new_data = 0) | old_word: the word before the edge. |
| NEW (new_data = 1) | new_word: the whole word after both writes of the edge. Lanes that were not written keep their old bytes. |
| No write to that word | Both candidates are equal, so either policy gives the same value. |
| Later edges | A value already captured never changes, even if a later write changes the word. |
The RAM calls read_snapshot() for every accepted read. Both candidates already have zeros above the width.
| RAM rule | Meaning |
|---|---|
| Ports | Ports A and B each have en, wr, address, data and a 4-bit strobe. One rising edge samples both ports. |
| Requests | en = 0 is idle, and the other fields are ignored. en = 1 with wr = 0 is a read. en = 1 with wr = 1 is a write. |
| Strobes | Strobe bit k enables byte lane k. Lane 0 is bits 7:0. A write with no active strobe bit is legal and changes nothing. |
| Width | A word has 1, 2 or 4 active byte lanes: 8, 16 or 32 bits. Strobe bits and data bytes above the width are ignored. Read bits above the width are 0. |
| Addresses | Word addresses 0 to depth - 1 are valid; depth is 1, 3 or 5. An enabled request with a larger address is refused. The other port is not affected. |
| Latency | A read accepted on edge c shows valid = 1 and its data right after edge c + L, never sooner. Time counts on every edge, idle or not. |
| Read value | The value is captured on the accepting edge. OLD policy: the word before the edge. NEW policy: the word after both writes of that edge. |
| Two writes, one word | A lane written by one port takes its byte. A lane written by both takes A in mode 0 and B in mode 1. In mode 2, two writes that share an active lane are both refused. |
| Reset | A reset edge refuses both requests and cancels every read in flight. Stored data is kept. |
| Start | Word a starts as seed XOR (a × 0x01020408), cut to the width. A trusted pulse loads this before each test. |
| Diagnostic bit | Set when |
|---|---|
| 0 | Port A is accepted. |
| 1 | Port B is accepted. |
| 2 | A is enabled with an address of depth or more. |
| 3 | B is enabled with an address of depth or more. |
| 4 | Both ports write the same valid word, and their strobes share an active lane. |
| 5 | Bit 4 is set in mode 2, so both writes are refused. |
| 6 | Both ports are enabled with the same valid address. Reads count too. |
| 7 | Always 0. |
The tests record one row per edge, counting edges from 0. Row k shows the RAM outputs right after edge k, in this form:
C<edge>:D<diagnostic, 2 hex digits>:O<overlap lanes, 1 hex digit>:A<valid>/<data, 8 hex digits>:B<valid>/<data>;
The overlap field shows the lanes that both writes of that edge committed. It is 0 when the pair is refused. Data is 0 when valid is 0.
The result gives the length of your trace, the length of the expected trace, and whether the two match exactly. The examples list the rows that matter.
Examples
Example 1
OLD Returns the Word Before the Write- Input
32-bit RAM, depth 3, seed 0x11223344, OLD policy. Edge 0: A reads 0 while B writes 0xaabbccdd to 0 with strobe 0x5. Edge 1: A reads 0.
- Output
trace_length = 272, expected_length = 272, trace_matches = yes
Diagnostic 0x43 means both requests were accepted on one word. The first read returns 0x11223344, the word before edge 0. The second read returns the merged word 0x11bb33dd.
Example 2
NEW Returns the Merged Word- Input
The same requests with the NEW policy.
- Output
trace_length = 272, expected_length = 272, trace_matches = yes
Both reads return 0x11bb33dd. Lanes 1 and 3 were not strobed, so they keep 0x33 and 0x11. The buggy code returns 0x11223344 for the first read.
Example 3
A Zero-Strobe Write Changes Nothing- Input
16-bit RAM, depth 1, seed 0x55667788, NEW policy. Edge 0: A writes 0x1234 to 0 with strobe 0 while B reads 0. Edge 1: A writes 0xabcdef01 to 0 with strobe 0x1. Edge 2: B reads 0.
- Output
trace_length = 272, expected_length = 272, trace_matches = yes
A strobe of 0 changes no byte, so both candidates are 0x7788 and row C1 shows B1/00007788. Edge 1 sets lane 0 to 0x01. Row C3 shows B1/00007701.
Constraints and Contract
- new_data stays the same for a whole test.
- Every test sets both latencies to 1 and runs eight edges, with idle edges after the listed requests.
- The tests never drive x or z.
- Keep the function name, the arguments and the 32-bit return type.
Hints
Hint 1
Read-during-write means one edge writes a word and also reads it. The policy decides which version the read sees.
Hint 2
Work through the NEW example by hand. After edge 0 the word is 0x11bb33dd. Which candidate does the code return?
Hint 3
The code never looks at new_data. Return new_word when it is 1, and old_word when it is 0.
What the grader checks: The function runs inside a real clocked dual-port RAM. The tests try both policies, both port orders and strobes 0, 0x1, 0x5 and 0xf, with resets between reads. An independent byte model checks every response. This covers the RAM rules above, not a real SRAM macro.
147 checks on Verilator · server. 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 7 steps and the final capstone