Q161FreeDesign Verification
Synchronize a one-outstanding producer and consumer
Interview prompt
Question
Implement two finite testbench threads in which the producer may send the next packet only after the consumer acknowledges the previous one. Consumer processing takes 0 through 5 clocks.

Candidate starting point
Implementation scaffold
module producer_consumer(input logic clk);
class packet;
int unsigned id;
function new(int unsigned id); this.id=id; endfunction
endclass
mailbox #(packet) data_mb = new(1);
mailbox #(bit) ack_mb = new(1);
task automatic producer();
// TODO: send IDs 0 through 9, waiting for each acknowledgment.
endtask
task automatic consumer();
// TODO: process exactly 10 packets and acknowledge each.
endtask
initial begin
// TODO: run both threads and wait for completion.
end
endmoduleReviewed example
Trace one case
Input
consumer delay = 5 clocks for every packetExpected output
packet IDs 0 through 9 are received once in order, with at most one packet outstandingThe producer blocks on ack_mb.get() after every put, so it cannot enqueue the next packet until the consumer completes the prior one.
What to cover
Requirements
- Block the producer after each data put until one acknowledgment is received.
- Have the consumer receive one packet, wait 0 through 5 clocks, and send exactly one acknowledgment.
- Transmit packet IDs 0 through 9 exactly once and wait for both threads to finish.
- Use persistent mailbox tokens rather than a bare event whose trigger could be missed.
