3.5.1 Exact-count consumer

EasyImplementVerilator · local serverReview the topic

Take exactly the number of items you were asked for from a mailbox, in order, and leave the rest alone.

Hardware context: Generator-driver-monitor pipelines with bounded transaction channels. Consume exactly the scheduled hardware stimulus count and terminate.

A consumer reads transactions from a mailbox until it has the number the test expects. Read one too few and a transaction is never checked; read one too many and the consumer steals an item meant for someone else, or waits forever.

Write consume_exact(channel, count, received). Make received exactly count long, then take count items from the mailbox, in the order they arrive, and store them. Wait when the mailbox is empty. With count = 0, return at once without reading.

The producer puts the first item at 1 ns and each next one gap ns later. Two unrelated tokens arrive at 30 and 31 ns; they are not yours and must stay in the mailbox.

Signals in the timeline

SignalWidthRole
item_count4input of each check
producer_gap3input of each check
Full specification
Signature (keep it)
task automatic consume_exact(input mailbox #(int) channel, input int count, output int received[]);

count is 0 to 8 and gap is 1 to 3 ns. Values are any signed int; no value means "stop".

Each case reports how many items you returned, when you returned (ns), how many items were left in the mailbox at 40 ns, and whether your items match the ones sent, in order.

Examples

Example 1

Three items, then return at once
Input
values = [10, -7, 25], gap = 3
Output
items = 3, returned_ns = 7, left_in_mailbox = 2, in_order = yes

Items arrive at 1, 4 and 7 ns, so the consumer returns at 7 ns. The two late tokens stay in the mailbox.

Example 2

Nothing requested
Input
values = [], gap = 3
Output
items = 0, returned_ns = 0, left_in_mailbox = 2, in_order = yes

count = 0: return an empty array at 0 ns without waiting.

Example 3

Any value is data
Input
values = [-2147483648, 0, 2147483647], gap = 1
Output
items = 3, returned_ns = 3, left_in_mailbox = 2, in_order = yes

All three arrive by 3 ns, in order, including both extreme values.

Constraints and contract

  • The mailbox is unbounded; get() blocks while it is empty.
  • Take exactly count items, in arrival order.
  • Keep the signature.

Hints

Hint 1

Which mailbox call waits for an item, and which only looks at the front without taking it?

Hint 2

Size received before you fill it, and count your loop carefully: how many times must it run?

Hint 3

A loop of exactly count blocking gets, each storing into the next element, returns the moment the last wanted item arrives.

What this judge verifies: The consumer is run for every count from 0 to 8 and every gap from 1 to 3, with late tokens that must not be taken. Passing shows exactly the right items, in order, at the right time.

90 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

Three items, then return at once

Input
values = [10, -7, 25], gap = 3
Expected output
items = 3, returned_ns = 7, left_in_mailbox = 2, in_order = yes

Items arrive at 1, 4 and 7 ns, so the consumer returns at 7 ns. The two late tokens stay in the mailbox.

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

Verilator · local server