2.1.1 Resize and preserve

EasyImplementVerilator · local serverReview the topic

Size burst storage from the request, keep the beats that arrived, and zero the rest.

Hardware context: Packet reassembly after fragmented or reordered receive traffic. Resize a captured payload buffer while retaining only its valid prefix.

A read scoreboard kept each burst in a fixed 16-beat array. When the test started issuing 32-beat bursts, beats 16 to 31 were silently dropped, and the checker passed bursts it never compared. Storage has to follow the request.

Write resize_payload(source_words, valid_count, new_size, result_words). source_words holds captured beats, but only the first valid_count of them are real; anything after that is leftover storage. Make result_words exactly new_size words long. Copy the real beats that fit, in order, and fill every other word with 0.

For source = [11, 22, 999, 888] with valid_count = 2 and new_size = 5, the result is [11, 22, 0, 0, 0]. The 999 is leftover storage, not a beat.

Return 0 on success. Return 1 for a new_size outside 0 to 16, and 2 for a valid_count below 0 or larger than the source; on either error result_words is empty.

Signals in the timeline

SignalWidthRole
allocated8input of each check
valid_count32input of each check
new_size32input of each check
selected8input of each check
Full specification
Signature (keep it)
function automatic int resize_payload(input int source_words[], input int valid_count, new_size, output int result_words[]);
Check, in orderReturnresult_words
new_size < 0 or new_size > 161empty
valid_count < 0 or valid_count > source_words.size()2empty
otherwise0new_size words: source_words[i] for i < min(valid_count, new_size), then zeros

new_size = 0 is legal and gives an empty result. Words are signed and must be copied bit for bit, negative values included.

Each case reports the status, the size of result_words and one selected word of it (0 when the selected position does not exist).

Examples

Example 1

Leftover storage is not a beat
Input
source = [11, 22, 999, 888], valid_count = 2, new_size = 5, selected word 2
Output
status = OK (0), size = 5, selected_word = 0

The result is [11, 22, 0, 0, 0]. Word 2 is 0, even though the source holds 999 there.

Example 2

Shrinking keeps signed values
Input
source = [-1, 7, 9], valid_count = 3, new_size = 1, selected word 0
Output
status = OK (0), size = 1, selected_word = -1

The result is [-1]: one word, copied bit for bit.

Example 3

A negative size is an error
Input
source = [1, 2], valid_count = 2, new_size = -1, selected word 0
Output
status = BAD_SIZE (1), size = 0, selected_word = 0

new_size is checked first: status BAD_SIZE and an empty result.

Constraints and contract

  • source_words has 0 to 16 elements; valid_count and new_size are signed ints.
  • Never copy words at or after valid_count, even when new_size is larger.
  • Keep the signature.

Hints

Hint 1

Which number decides how long the result is, and which decides how many words are real? They are different numbers.

Hint 2

Check the two arguments before you allocate anything, and leave the result empty when a check fails.

Hint 3

Allocate the result with new[new_size], which starts every word at 0. Then copy only the positions below both valid_count and new_size.

What this judge verifies: Your function is called with every size from -1 to 17, every valid count around the source size and seeded signed words, including leftover storage that must not be copied. Passing shows the status, size and every selected word are right on these inputs.

719 checks on Verilator · local server. Run the examples first, then check the full regression. The suite is local and inspectable. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 5 steps and the final capstone
Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Leftover storage is not a beat

Input
source = [11, 22, 999, 888], valid_count = 2, new_size = 5, selected word 2
Expected output
status = OK (0), size = 5, selected_word = 0

The result is [11, 22, 0, 0, 0]. Word 2 is 0, even though the source holds 999 there.

Run samples executes all 3 example checks. Check all tests runs 719 checks.

Verilator · local server