Fix the sparse memory model of a RAM scoreboard, whose byte keys collide across words.
Hardware context: Single-clock dual-port byte-enabled RAM. Check dual-port RAM data through a sparse byte model independent of DUT storage.
A RAM scoreboard flagged data errors on a correct RAM. Each one came after a write to a different word. A write to one word was changing the model of another.
This ram_scoreboard keeps no full copy of memory. It stores only the bytes that were written, in an associative array keyed by byte position. A byte never written is worked out from the start contents and is not stored. allocated() returns how many bytes the array holds.
Everything else follows the pipelined RAM scoreboard: per-port deadlines, captured read values, reset, error kinds and the report. The tests run it on a correct RAM and on faulty ones. They also record allocated() after every edge.
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 |
latency_a | 2 | input of each check |
latency_b | 2 | input of each check |
fault_mode | 4 | input of each check |
image_seed | 32 | input of each check |
Full specification
class ram_scoreboard;
function new(input int depth, lanes, lat_a, lat_b, policy, priority_mode, input bit [31:0] seed);
function void sample(input int cycle, input ram_op item,
input bit av, input bit [31:0] ad, input bit bv, input bit [31:0] bd);
function string report();
function int allocated();
endclass
| Sparse model | Rule |
|---|---|
| Key | word_address × lanes + lane. Every byte of the memory has its own key. |
| Write | Store the byte under its key. |
| Read | Use the stored byte if its key exists. Otherwise use the byte of the start word. A read never adds a key. |
| Reset | Keep every key. |
| allocated() | The number of keys, so the number of different bytes ever written. |
| Step in sample() | What it does |
|---|---|
| Reset edge | Cancel every read in flight, even one due on this edge. A valid response on either port is a RESET error. Accept no request, and stop. |
| 1. Retire | Take every queued read whose due edge is this edge, for both ports. |
| 2. Compare | Check A, then B. Expected but no response is MISSING. A response with nothing expected is EXTRA. Both present with different data is DATA. |
| 3. Write | Apply the accepted writes of this edge to your own memory model, with the lane and priority rules. |
| 4. Queue | Queue each accepted read with due = cycle + its port latency and a value captured now: the OLD or NEW word. |
| Error kind | Value |
|---|---|
| MISSING | 1 |
| EXTRA | 2 |
| DATA | 4 |
| RESET | 8 |
K=<bad>:<first_cycle>:<first_port>:<first_kind>|E=<errors>|N=<accepted>,<retired>,<matched>,<canceled>,<pending>
| Report field | Meaning |
|---|---|
| bad | The OR of every error kind seen so far. |
| first_cycle, first_port, first_kind | The first error. On one edge, A comes before B. Before any error they are ff, ff and 00. |
| errors | The number of errors. Each port has at most one error per edge. |
| accepted | Reads queued so far. |
| retired | Reads that reached their due edge, right or wrong. |
| matched | Retired reads whose response came on time with equal data. |
| canceled | Reads removed by a reset. |
| pending | Reads still queued. accepted = retired + canceled + pending after every edge. |
Reset keeps memory and the error history. Only the reads in flight are cancelled.
| Faulty RAM | What is wrong |
|---|---|
| Swapped bytes | Write bytes 0 and 1 trade places, when both exist. |
| Wrong priority | B wins shared lanes in mode 0, and A in mode 1. |
| Wrong policy | OLD and NEW are swapped. |
| Stale data | The response data register never updates. |
| Late | Every response comes one edge late. |
| Dropped B | Reads on port B never respond. |
| Reset leak | Reset does not cancel the reads in flight. |
| Alias | Out-of-range addresses wrap into memory. |
Report only what you see. A fault that a trace cannot show must give a clean report.
| 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 run 16 edges. After each edge they record the RAM trace row, as in the basic RAM problems. Your report() and a semicolon follow it. The result gives both trace lengths and whether the traces match exactly. Each report also ends with |S=<allocated()>.
Examples
Example 1
Swapped Write Bytes Are Caught- Input
16-bit RAM, depth 3, seed 0x11223344, latencies A = 2 and B = 1. The RAM swaps write bytes 0 and 1. Edge 0: A writes 0xaabbccdd to 1 with strobe 0x1. Edge 1: A reads 1 and B reads 0.
- Output
trace_length = 1094, expected_length = 1094, trace_matches = yes
Word 1 starts as 0x374c, so the write should give 0x37dd. The faulty RAM stores 0x37cc, and A reports DATA after edge 3. B's read of word 0 returns 0x3344 and matches. One byte is stored, so S=1.
Example 2
Wrong Lane Priority Is Caught- Input
32-bit RAM, depth 3, seed 0x11223344, mode 0, latencies A = 1 and B = 2. The RAM lets B win shared lanes. Edge 0: A writes 0xaabbccdd with strobe 0x3 and B writes 0x10203040 with strobe 0x6, both to 0. Edge 1: both read 0.
- Output
trace_length = 1094, expected_length = 1094, trace_matches = yes
A should win shared lane 1, giving 0x1120ccdd. The faulty RAM returns 0x112030dd. A reports DATA after edge 2 and B after edge 3. Three different bytes were written, so S=3.
Example 3
A Fault That Cannot Show Stays Clean- Input
8-bit RAM, depth 1, latencies A = 3 and B = 1. The RAM swaps write bytes 0 and 1. Edge 0: A writes 0xab with strobe 0x1. Edge 1: both read 0. Edge 2: reset. Edge 3: A reads 0.
- Output
trace_length = 1094, expected_length = 1094, trace_matches = yes
An 8-bit word has no lane 1, so the swap cannot happen. Reset cancels both reads, including B's read due on edge 2. The last read returns 0xab after edge 6. There is no error, and S stays 1.
Constraints and Contract
- Never read the memory or the pipeline of the RAM. Use only the arguments of sample().
- A late response is an error. Do not match it to a later deadline.
- Keep the class name, the method names and the argument types.
Hints
Hint 1
Each byte of the memory needs its own key. Two bytes of different words must never share one.
Hint 2
In the first example the RAM is 16 bits wide. Which key does the code use for lane 0 of word 1? Which key does a read of lane 0 of word 0 use?
Hint 3
Look at the key in read_word() and write_lane(). It uses only the lane. It must be address × lanes + lane in both places.
What the grader checks: Real clocked RAMs, correct and with planted faults, feed the scoreboard. An independent model checks every response, every report and the byte count after every edge. The tests cover latencies 1 to 3 per port with resets that keep data. They do not cover x or z values or RAMs on two clocks.
87 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