H2.6 Byte-Lane Collision Resolver

MediumFixVerilator · serverReview the topic

Fix the lane resolver of a dual-port RAM so that two writes to one word merge byte by byte.

Hardware context: Single-clock dual-port byte-enabled RAM. Resolve overlapping and disjoint byte-lane writes from two RAM ports.

Two ports wrote different bytes of the same word on one edge. Port A's bytes vanished, although B never touched those lanes. The resolver treats priority as a whole-word decision.

resolve_lanes() runs when both ports write the same word on one edge. It gets the old word, both write words, both strobes, the number of active lanes and the priority mode. It returns the merged word and a mask of the lanes both ports wrote.

Each lane is decided on its own. A lane written by one port takes that port's byte. A lane written by both takes A's byte in mode 0 and B's in mode 1. A lane nobody writes keeps its old byte.

The tests try every pair of strobes for 1, 2 and 4 lanes, on the real RAM. Narrow RAMs try each pair in both modes. Find the bug and fix it.

Signals in the Timeline

SignalWidthRole
active_lanes3input of each check
memory_depth3input of each check
read_policy1input of each check
write_priority2input of each check
image_seed32input of each check
Full specification
The function (keep its signature)
function automatic bit[35:0] resolve_lanes(input bit[31:0] old_word, a_word, b_word, input bit[3:0] a_mask, b_mask,
                                           input int lanes, priority_mode);  // returns {overlap[3:0], word[31:0]}
Active lane kByte k of the result
Only A strobes itByte k of a_word.
Only B strobes itByte k of b_word.
Both strobe ita_word in mode 0, b_word in mode 1. Overlap bit k is 1.
Neither strobes itByte k of old_word.

Lanes above the width are 0 in the result and never overlap. An overlap counts even when the two bytes are equal.

The RAM shows the overlap mask on its overlap_lanes output, the O field of the trace.

RAM ruleMeaning
PortsPorts A and B each have en, wr, address, data and a 4-bit strobe. One rising edge samples both ports.
Requestsen = 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.
StrobesStrobe bit k enables byte lane k. Lane 0 is bits 7:0. A write with no active strobe bit is legal and changes nothing.
WidthA 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.
AddressesWord 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.
LatencyA 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 valueThe 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 wordA 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.
ResetA reset edge refuses both requests and cancels every read in flight. Stored data is kept.
StartWord a starts as seed XOR (a × 0x01020408), cut to the width. A trusted pulse loads this before each test.
Diagnostic bitSet when
0Port A is accepted.
1Port B is accepted.
2A is enabled with an address of depth or more.
3B is enabled with an address of depth or more.
4Both ports write the same valid word, and their strobes share an active lane.
5Bit 4 is set in mode 2, so both writes are refused.
6Both ports are enabled with the same valid address. Reads count too.
7Always 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:

One trace row
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

Disjoint Lanes Both Land
Input
32-bit RAM, depth 3, seed 0x11223344, mode 1. Edge 0: A writes 0xaabbccdd with strobe 0x5 and B writes 0x10203040 with strobe 0xa, both to 0. Edge 1: both read 0.
Output
trace_length = 272, expected_length = 272, trace_matches = yes

A owns lanes 0 and 2, and B owns lanes 1 and 3. The word becomes 0x10bb30dd with no overlap. Both reads return it in row C2. The buggy code drops A's bytes and gives 0x10223044.

Example 2

A Wins the Shared Lanes in Mode 0
Input
The same RAM in mode 0. Edge 0: A writes 0xaabbccdd with strobe 0x7 and B writes 0x10203040 with strobe 0x6, both to 0. Edge 1: A reads 0.
Output
trace_length = 272, expected_length = 272, trace_matches = yes

Lanes 1 and 2 are shared, so the overlap mask is 0x6 and A wins both. Lane 3 keeps 0x11, giving 0x11bbccdd. Row C0 shows diagnostic 0x53 and overlap 0x6.

Example 3

Strobes Above the Width Never Overlap
Input
8-bit RAM, depth 1, seed 0x12, mode 1. Edge 0: A writes 0xaa with strobe 0x9 and B writes 0xbb with strobe 0x8. Edge 1: B reads 0.
Output
trace_length = 272, expected_length = 272, trace_matches = yes

Only lane 0 exists. A strobes it, and B's only strobe bit is lane 3. So nothing overlaps and the word becomes 0xaa, even though B has priority.

Constraints and Contract

  • The tests use modes 0 and 1. In mode 2, writes that share a lane are refused before this function runs.
  • 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 36-bit return type.

Hints

Hint 1

Loop over the active lanes and decide each lane on its own.

Hint 2

Try masks A = 0x5 and B = 0xa. No lane is shared, so both writes must land. Does the code keep A's bytes?

Hint 3

Look at the condition for taking A's byte. It asks whether B writes anything at all. It should ask about B's bit for this lane, and then the mode.

What the grader checks: The function runs inside a real clocked dual-port RAM. The tests try every pair of strobes at widths of 8, 16 and 32 bits. The 8- and 16-bit RAMs try each pair in both modes. The 32-bit RAM tries each pair in one mode, alternating between pairs. Four more 16-bit tests set strobe bits above the width on both ports. An independent byte model checks the overlap field and every response. This covers the RAM rules above, not a real SRAM macro.

303 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

Report a problem with this page

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

Disjoint Lanes Both Land

Input
32-bit RAM, depth 3, seed 0x11223344, mode 1. Edge 0: A writes 0xaabbccdd with strobe 0x5 and B writes 0x10203040 with strobe 0xa, both to 0. Edge 1: both read 0.
Expected output
trace_length = 272, expected_length = 272, trace_matches = yes

A owns lanes 0 and 2, and B owns lanes 1 and 3. The word becomes 0x10bb30dd with no overlap. Both reads return it in row C2. The buggy code drops A's bytes and gives 0x10223044.

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

Verilator · server