Fix the request check of a dual-port RAM so that reject mode refuses only writes that share a byte lane.
Hardware context: Single-clock dual-port byte-enabled RAM. Resolve simultaneous port accesses and report the declared collision outcomes.
In reject mode, two writes to different bytes of one word were both refused. Software lost both updates. Only writes that share an active byte lane should be refused.
authorize_pair() checks both ports before the RAM acts on an edge. It returns the eight diagnostic bits. Bits 0 and 1 gate the real RAM: a refused write leaves memory unchanged, and a refused read returns nothing.
An enabled port with an address of depth or more is refused on its own. The other port is not affected. In modes 0 and 1, two writes may share lanes and are merged lane by lane. In mode 2, two valid writes to one word that share an active lane are both refused.
The tests drive the real RAM with every mix of idle, read and write on both ports, in all three modes. 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
typedef struct packed {bit reset, a_en, a_wr; bit[3:0] a_addr; bit[31:0] a_data; bit[3:0] a_be;
bit b_en, b_wr; bit[3:0] b_addr; bit[31:0] b_data; bit[3:0] b_be;} ram_op;
function automatic bit[7:0] authorize_pair(input ram_op request, input int depth, lanes, priority_mode);
| 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. |
| Term | Meaning |
|---|---|
| Valid | The port is enabled and its address is below depth. A disabled port is never invalid, whatever its address holds. |
| Active lanes | Lanes 0 to lanes - 1. Strobe bits above them never cause an overlap. |
| Accepted | Valid, and not refused by mode 2. Bit 4 and bit 6 describe the requests; bits 0 and 1 say what the RAM does. |
A write with no active strobe bit is accepted and changes nothing. It can never overlap.
| 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. |
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
A Shared Lane Is Refused in Mode 2- Input
32-bit RAM, depth 3, seed 0x11223344, mode 2. Edge 0: A writes 0xaabbccdd with strobe 0x3 and B writes 0x10203040 with strobe 0x2, both to 0. Edge 1: A reads 0.
- Output
trace_length = 272, expected_length = 272, trace_matches = yes
Both strobes include lane 1, so the pair is refused. Diagnostic 0x70 sets same word, overlap and reject, with neither accept bit. The read returns the unchanged 0x11223344.
Example 2
Disjoint Lanes Still Commit in Mode 2- Input
The same RAM in mode 2. Edge 0: A writes 0xaabbccdd with strobe 0x1 and B writes 0x10203040 with strobe 0x8, both to 0. Edge 1: A reads 0.
- Output
trace_length = 272, expected_length = 272, trace_matches = yes
No lane is shared, so both writes are accepted with diagnostic 0x43. The word becomes 0x102233dd. The buggy code refuses both, and the read returns 0x11223344.
Example 3
A Bad Address Does Not Block the Other Port- Input
32-bit RAM, depth 3, mode 1. Edge 0: A writes to 3 and B writes 0xdeadbeef to 2, both with strobe 0xf. Edge 1: A reads 2 and B reads 3.
- Output
trace_length = 272, expected_length = 272, trace_matches = yes
Address 3 is out of range at depth 3. A is refused alone and B's write lands, so the diagnostic is 0x06. On edge 1 only A is accepted, with diagnostic 0x09. Row C2 shows A1/deadbeef and no B response.
Constraints and Contract
- depth is 1, 3 or 5. The address fields are 4 bits, so they can reach 15.
- priority_mode is 0, 1 or 2. The tests never drive x or z.
- Every test sets both latencies to 1 and runs eight edges. Keep the type, the function name, the arguments and the return type.
Hints
Hint 1
Work out whether each port is valid first. Then compare the pair.
Hint 2
Try mode 2 with strobes 0x1 and 0x8 on one word. The lanes do not overlap, so both writes must be accepted. What does the code return?
Hint 3
Look at reject_pair. It uses same_word, which is true for any two requests to one word. It should use overlap.
What the grader checks: The function gates a real clocked dual-port RAM. The tests try every mix of idle, read and write on both ports, on the same word and on different words. They add out-of-range addresses up to 15 and all three modes. An independent model checks every diagnostic and response. This covers the RAM rules above, not a real SRAM macro.
203 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