Wrap a RAM in a (13,8) SECDED code: encode on write, correct one flipped bit on read, and flag two.
Hardware context: A controller's state RAM next to a radio, with each 8-bit word stored as a 13-bit SECDED codeword. Build the SECDED encoder and decoder around a RAM, correct on read, and flag what cannot be corrected.
A controller keeps its state in a small RAM that sits next to a radio, where bits flip now and then. Each 8-bit word is stored as a 13-bit SECDED codeword.
This is an Implement problem. Write ecc_mem around the given RAM. Encode every write. Decode every read: correct a single flipped bit, and flag a double one.
The test reads your stored codewords straight from the RAM, so the layout must match the table exactly. Then it flips bits in the RAM and reads every word back. A corrected word must also be written back, so the error does not stay in the RAM.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
flips | 208 | input of each check |
Full specification
module ecc_mem (
input logic clk,
input logic we, // 1: write wdata to word addr at the next rising edge; 0: read it
input logic [3:0] addr,
input logic [7:0] wdata,
output logic [7:0] rdata, // after a read edge: the corrected data
output logic sbe, // after a read edge: one bit was wrong and is corrected
output logic dbe // after a read edge: two bits are wrong; rdata cannot be trusted
);
// ram16x13 ram (...); your encoder and decoder
endmodule
// ram16x13: the RAM of 16 words of 13 bits (a model). A write at a rising edge with we = 1;
// otherwise the edge reads word addr into rdata.
module ram16x13 (input logic clk, we, input logic [3:0] addr, input logic [12:0] wdata, output logic [12:0] rdata);
logic [12:0] mem [16];
always @(posedge clk) if (we) mem[addr] <= wdata; else rdata <= mem[addr];
endmodule
| Position | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bit | P | p1 | p2 | d0 | p4 | d1 | d2 | d3 | p8 | d4 | d5 | d6 | d7 |
| Rule | Definition |
|---|---|
| Check bit pk | The XOR of the data bits at the positions p with p AND k not 0 (k = 1, 2, 4, 8). |
| Overall parity P | The XOR of positions 1 to 12. The 13 stored bits then have even parity. |
| Read | The RAM returns the codeword after the read edge. rdata, sbe and dbe follow from it in the same cycle. |
| One flip | rdata is the written data, sbe = 1, dbe = 0. This includes a flip of P itself. |
| Two flips | dbe = 1, sbe = 0. rdata is not checked. |
| Scrub | Every read is followed by one cycle with we = 0 and the same addr. If sbe is 1, write the corrected codeword back into ram in that cycle. After a double error, write nothing. |
| No flip | rdata is the written data, sbe = 0, dbe = 0. |
Each test writes 16 words, flips none, one or two bits of each stored codeword, then reads the 16 words. The result has three fields.
| Field | Meaning |
|---|---|
| enc_wrong | How many stored codewords differ from the layout above, before any flip. |
| wrong_data | How many reads with no flip or one flip returned the wrong data. |
| wrong_flags | How many reads had the wrong sbe or dbe. |
| scrub_wrong | How many RAM words are wrong after the reads: a single flip must be repaired, a double flip left alone. |
Examples
Example 1
No Flips- Input
00, fe, a7, 59, 04, fa, a3, 5d, 08, f6, af, 51, 0c, f2, ab, 55
- Output
scrub_wrong = 0, enc_wrong = 0, wrong_data = 0, wrong_flags = 0, words = 16
Every stored codeword must match the layout, and every word must come back with both flags at 0.
Example 2
One Flipped Data Bit per Word- Input
4e flip 3, f6 flip 5, be flip 6, d3 flip 7, 32 flip 9, c2 flip 10, ea flip 11, 08 flip 12, 7b flip 3, 31 flip 5, ed flip 6, ac flip 7, de flip 9, 69 flip 10, 9c flip 11, 81 flip 12
- Output
scrub_wrong = 0, enc_wrong = 0, wrong_data = 0, wrong_flags = 0, words = 16
The syndrome names the flipped position. The decoder flips it back, returns the written data and raises sbe.
Example 3
Two Flips in Every Other Word- Input
4d, cd flip 3+10, 0a, 8d flip 3+10, fe, 02 flip 3+10, 8b, 13 flip 3+10, 64, ea flip 3+10, b3, 97 flip 3+10, 34, ab flip 3+10, 57, de flip 3+10
- Output
scrub_wrong = 0, enc_wrong = 0, wrong_data = 0, wrong_flags = 0, words = 16
Bits 3 and 10 flip, so the syndrome is 9 and the overall parity looks good. The decoder must raise dbe and must not raise sbe.
Constraints and Contract
- The inputs are known 0/1 values. They change while the clock is low.
- Keep the module name, the ports and the instance
ramofram16x13. The test reads and writesram.mem.
Hints
Hint 1
Write the encoder as a function of 8 bits that returns 13. Place the data first, then compute p1, p2, p4 and p8, and P last.
Hint 2
The syndrome has four bits. Syndrome bit j is the XOR of the stored bits at each position p with p AND 2^j not 0. The check bit counts too.
Hint 3
Decide with the overall parity first. Parity wrong: one flip at the syndrome position, even when the syndrome is 0. Parity good and syndrome not 0: two flips.
What the grader checks: Every single flip position and all 78 pairs of flip positions are tested, on seeded data, with directed cases for check-bit flips. Stored codewords are compared with a JavaScript encoder, and the RAM is read again after the scrub cycles. Three or more flips and a background scrub counter are outside this problem.
12 checks on Icarus · browser samples / server checks. 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 2 steps and the final capstone