H2.1 Byte-Strobe Memory Update

EasyFixVerilator · serverReview the topic

Fix the byte-strobe merge of a dual-port RAM so that 8-bit and 16-bit builds ignore strobe bits above their width.

Hardware context: Single-clock dual-port byte-enabled RAM. Apply the byte strobes of a memory write while preserving disabled lanes.

A RAM passed every test when built 32 bits wide. Built 8 or 16 bits wide, its reads came back with bits set above the word. Strobe bits for lanes that do not exist were still writing.

merge_bytes() is the write path of a clocked dual-port RAM. It takes the stored word, the write data, a 4-bit strobe and the number of active byte lanes: 1, 2 or 4. It returns the new word.

Each strobed active lane takes the byte from the write data. Every other active lane keeps its old byte. Bits above the width must be 0.

The tests drive the real RAM through ports A and B. They use 1, 2 or 4 lanes and depth 1, 3 or 5. They compare every response with an independent byte model. 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[31:0] merge_bytes(input bit[31:0] prior_word, write_word, input bit[3:0] strobe, input int lanes);
LaneResult byte
Active and strobedThe byte of write_word in the same lane.
Active, not strobedThe byte of prior_word in the same lane.
Above the widthAlways 0. Its strobe bit and data byte are ignored.

The RAM calls merge_bytes() for every accepted write, except when both ports write the same word on one edge. A separate trusted resolver handles that case.

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

Strobe 0x5 Writes Lanes 0 and 2
Input
32-bit RAM, depth 3, seed 0x11223344. Edge 0: A reads 1. Edge 1: A writes 0xaabbccdd to 1 with strobe 0x5. Edge 2: B reads 1.
Output
trace_length = 272, expected_length = 272, trace_matches = yes

Word 1 starts as 0x1020374c. The write replaces lanes 0 and 2, giving 0x10bb37dd. Row C1 shows A1/1020374c, and row C3 shows B1/10bb37dd.

Example 2

An 8-bit Word Ignores High Strobes
Input
8-bit RAM, depth 1, seed 0x89abcdef. Edge 0: B writes 0xffffffff to 0 with strobe 0xe. Edge 1: A reads 0.
Output
trace_length = 272, expected_length = 272, trace_matches = yes

Only lane 0 exists, and strobe 0xe leaves it off. The word stays 0xef, so row C2 shows A1/000000ef. The buggy code writes three lanes that do not exist and returns 0xffffffef.

Example 3

A 16-bit Write to the Top Lane
Input
16-bit RAM, depth 5, seed 0x10203040. Edge 0: B writes 0xff008877 to 4 with strobe 0x2. Edge 1: A reads 4. Edge 2: A reads 0.
Output
trace_length = 272, expected_length = 272, trace_matches = yes

Word 4 starts as 0x2060. Strobe 0x2 writes lane 1 only, giving 0x8860. Word 0 keeps 0x3040. Rows C2 and C3 show those two values on port A.

Constraints and Contract

  • lanes is 1, 2 or 4. The tests often set strobe bits above the width; they must have no effect.
  • 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

A lane exists only when its number is below lanes. Strobe bits for higher lanes are noise.

Hint 2

Work through the 8-bit example by hand. With one lane and strobe 0xe, which bytes may change? What does the code return?

Hint 3

Look at the loop bound and the first assignment. Neither uses lanes. Loop to lanes - 1 and start from prior_word with the bits above the width cleared.

What the grader checks: The function runs inside a real clocked dual-port RAM. Widths are 8, 16 and 32 bits, and depths are 1, 3 and 5. Every strobe value is tried from both ports. An independent byte model checks every response. This covers the RAM rules above, not the behaviour of a real SRAM macro.

99 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

Strobe 0x5 Writes Lanes 0 and 2

Input
32-bit RAM, depth 3, seed 0x11223344. Edge 0: A reads 1. Edge 1: A writes 0xaabbccdd to 1 with strobe 0x5. Edge 2: B reads 1.
Expected output
trace_length = 272, expected_length = 272, trace_matches = yes

Word 1 starts as 0x1020374c. The write replaces lanes 0 and 2, giving 0x10bb37dd. Row C1 shows A1/1020374c, and row C3 shows B1/10bb37dd.

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

Verilator · server