Fix a DMA copy model that writes bytes too early when the destination does not start on a word boundary.
Hardware context: Memory-to-memory DMA with owned descriptors and bounded bus bursts. Check an unaligned copy without corrupting neighboring destination bytes.
A DMA copy between addresses with different word offsets damaged the bytes just before the destination. Copies to aligned destinations looked fine.
The function copy_bytes models the copy on a memory of 128 bytes with four-byte words. It checks the request, then moves the bytes in beats. A beat is the largest piece that stays inside one word on both sides. The function reports the result, the bytes it wrote and the whole memory.
The tests try every pair of word offsets for source and destination, with lengths 0, 1, 7, 16 and 31. They also try an overlap and a range past the end. No test injects a bus failure, so fail_beat is always -1.
Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
source_address | 8 | input of each check |
destination | 8 | input of each check |
length | 6 | input of each check |
failure_beat | 8 | input of each check |
Full specification
function automatic string copy_bytes(input logic [1023:0] initial_memory, input int src, dst, length, fail_beat);
Memory has 128 bytes in four-byte words. initial_memory holds byte a in bits 8a + 7 down to 8a, so address 0 is in the low bits.
| Error | Set when |
|---|---|
| 1, bounds | length is not 0 to 32, an address is below 0, or a range runs past byte 127. This check comes first. |
| 2, overlap | length is above 0 and the two ranges overlap. |
| 3, bus failure | Beat number fail_beat, counting from 0, fails. |
A request rejected with error 1 or 2 changes nothing.
| Beat rule | Meaning |
|---|---|
| Size | Take the bytes left, cut at the end of the current source word and at the end of the current destination word. A beat holds 1 to 4 bytes. |
| Placement | Byte k of the request goes from src + k to dst + k. |
| Failure | When the beat number equals fail_beat, stop with error 3 before writing any byte of that beat. Earlier beats stay written. |
| No failure | fail_beat = -1, or a number past the last beat, means no beat fails. |
| Report field | Holds |
|---|---|
| E= | The error, or 0. |
| BYTES= | The number of bytes written, in decimal. |
| BEATS= | The number of beats completed, in decimal. |
| COPIED= | 32 hex digits: a 128-bit mask. Bit a is set when the copy wrote byte a, even if its value did not change. |
| MEM= | All 128 bytes as two hex digits each, address 0 first. |
The fields are joined with |. A zero-length copy gives E=0, with all counts and the mask at 0.
The whole report string is compared with the expected one. The result shows the length of your report, the expected length and whether the two match.
Examples
Example 1
Offsets 1 and 2- Input
src 1, dst 66, length 7, fail_beat -1
- Output
report_length = 320, expected_length = 320, match = yes
The beats hold 2, 1, 3 and 1 bytes, each cut at a word end on one side. Bytes 66 to 72 change, and bytes 64 and 65 keep their values. The report starts E=0|BYTES=7|BEATS=4.
Example 2
Offsets 3 and 0- Input
src 3, dst 80, length 9, fail_beat -1
- Output
report_length = 320, expected_length = 320, match = yes
The word ends fall on alternate sides, so the beats hold 1, 3, 1, 3 and 1 bytes. The report starts E=0|BYTES=9|BEATS=5, with bits 80 to 88 set in COPIED.
Example 3
Overlapping Ranges- Input
src 16, dst 20, length 8, fail_beat -1
- Output
report_length = 320, expected_length = 320, match = yes
Bytes 16 to 23 and bytes 20 to 27 overlap. The request is rejected with E=2, and memory does not change.
Example 4
A Range Past the End- Input
src 120, dst 32, length 16, fail_beat -1
- Output
report_length = 320, expected_length = 320, match = yes
The source range runs to byte 135, past the end. The request is rejected with E=1, and nothing is written.
Constraints and Contract
- The tests never drive x or z.
- Keep the function name, its arguments and the return type.
Hints
Hint 1
Write down where byte k of the request must land. It depends on dst and k only, not on word boundaries.
Hint 2
Trace the first example: src 1, dst 66, 7 bytes. The first beat moves 2 bytes, to 66 and 67. Which addresses does the code write?
Hint 3
Look at the write in the inner loop. It rounds dst down to a word boundary before adding the offset. Write to dst + offset + j, as the COPIED mask already does.
What the grader checks: The tests cover all 16 pairs of word offsets at five lengths, plus an overlap and a range past the end. They compare every memory byte and the mask. Bus failures are not tested here.
84 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 8 steps and the final capstone