← Design Verification Interviews
Interview Question · Code Writing
A Task That Waits on the Caller's Flag
The Question
The interviewer asks
Write a task wait_for(flag) that blocks until a flag, set later by another process, goes high. Why does the obvious version with an input argument hang?
You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
An input argument is copied when the task is called. So the task waits on its own frozen copy and never sees the caller's variable change. Declare it ref bit f, which passes a reference, and make the task automatic, because ref is only allowed in automatic subroutines. Add const (const ref) when the task must read but never write the variable.
ready, both called at time 0. One takes an input argument, the other a ref. Arrows show where values flow: into a variable when it is written, and out of it when a task reads it.- Both calls startt=0 (illustrative)
The
inputargument copies the currentready, 0, into a localrdy. Therefargument copies nothing: insidewait_ref,rdyis another name forready.Follow the calls, values and waits
- data 0: ready → rdy of wait_copy
- Waiting: wait_ref — waits for edge 1
- Waiting: wait_copy — waits for edge 1
- Edges 1 to 3t=5, 15, 25 (illustrative)
On each edge, both tasks test
rdy. Both read 0, so both wait for the next edge.Follow the calls, values and waits
- data 0: ready → wait_ref
- data 0: rdy of wait_copy → wait_copy
- ready risest=32 (illustrative)
The initial block writes 1 into
ready. Nothing copies it again, so therdyofwait_copykeeps the 0 from the call.Follow the calls, values and waits
- data 1: initial block → ready
- Edge 4t=35 (illustrative)
wait_refreadsreadyitself, finds 1 and returns: ready seen on edge 4.wait_copyreads its copy and still finds 0.Follow the calls, values and waits
- data 1: ready → wait_ref
- data 0: rdy of wait_copy → wait_copy
- Waiting: wait_copy — waits for edge 5
- Released: wait_ref
- Released: wait_copy
- Edge 8t=75 (illustrative)
wait_copyhas read 0 on all eight edges. It gives up and returnsedgesas -1.Follow the calls, values and waits
- data 0: rdy of wait_copy → wait_copy
- Released: wait_copy
Read the diagram as text
- initial block: #32 ready = 1
- ready: module variable
- wait_ref: ref bit rdy
- rdy of wait_copy: a local copy
- wait_copy: input bit rdy
- initial block → ready: writes
- ready → wait_ref: read through ref
- ready → rdy of wait_copy: copied at the call
- rdy of wait_copy → wait_copy: reads its copy
An input is a snapshot taken at the call. A ref follows the caller's variable.
Self-Check
Tick each key point you covered in your answer.
What Interviewers Listen For (Junior)
A good junior answer explains that an input is copied at the moment of the call. It writes task automatic wait_for(ref bit f).
What Interviewers Listen For (Mid-Level)
A good mid-level answer adds that ref also avoids copying large arrays. It uses const ref so the compiler can check that the task only reads.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
WhyWhat IfProve ItCompare
- A Task That Waits on the Caller's Flag
-
WhyWhy Ref Arguments Need an Automatic SubroutineL2 Mid-Level
Why does the language forbid
refarguments in static tasks?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
A static task has one shared argument slot for all calls. Two overlapping calls would each want that slot to refer to a different variable, and the second would silently re-point the first. Automatic tasks allocate a fresh slot per call, so every reference stays tied to its own caller.
Self-Check
Tick each key point you covered in your answer.
Tool as Truth
"It is a simulator limitation that newer tools lift."
It is a language rule, not a tool restriction.
Source IEEE 1800-2017 §13.5.2
-
What IfWhat a Const Ref Argument Still SeesL1 JuniorL2 Mid-Level
What if you pass a large array as
const ref? What does that buy, and what can still change it?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
const refpasses the array without a copy, and the compiler refuses writes to it inside the routine. The routine still reads the caller's variable. So changes the caller makes while the routine runs are visible to it. It cannot change the array by mistake.Self-Check
Tick each key point you covered in your answer.
Misconception
"
const refis a snapshot of the array at the call."It is the caller's variable, made read-only. Later changes by the caller are visible inside the routine.
Source IEEE 1800-2017 §13.5.2
-
CompareTask or FunctionL0 FresherL1 Junior
Compare a task with a function. Could
wait_forhave been a function?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
A function runs in zero simulation time and may not contain
#,@orwait, sowait_formust be a task. A function can return a value and appear inside an expression. A task returns results throughoutputarguments and is called as a statement. Use avoidfunction for zero-time work with no result, so the compiler guarantees it never blocks.Self-Check
Tick each key point you covered in your answer.
Half-Truth
"Functions cannot call tasks at all."
A function may not call a task that could block. It may start one inside
fork ... join_none, because the function itself does not wait.Source IEEE 1800-2017 §13.4
-
Prove ItSolve 3.1.1: Popcount FunctionSimulator-graded
Show it, don't just say it: this Silicon Lab problem is graded by the simulator.
-
What IfA Ref Argument That Changed Under the TaskL1 JuniorL2 Mid-Level
What if the task reads a
refarray, waits a few cycles, then reads it again for a checksum?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
The second read sees whatever the caller wrote in between, because a reference is live. If the task needs a stable snapshot, copy the array into a local variable at the start. Or pass it by value.
Self-Check
Tick each key point you covered in your answer.
Misconception
"
const reffreezes the array for the duration of the call."constonly stops the task from writing. The caller can still change the variable, and the task sees the change.Source IEEE 1800-2017 §13.5.2
What People Get Wrong
Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.
Misconception
"An input argument tracks the caller's variable while the task waits."
Input arguments are copied once at the call. The twin waits on an input copy and times out; the ref version wakes at t=5.
Run It
A task waiting on its argument while another process raises the flag at t=5.
t=20 woke=0 FAIL: the input argument was a copy taken at the call, so it never saw flag riseSee the Fix
module tb;
bit flag = 0;
bit woke = 0;
// The fix: ref, so the task watches the caller's variable.
task automatic wait_for(ref bit f);
wait (f == 1);
woke = 1;
endtask
initial begin
fork
wait_for(flag);
#20; // give up after 20
join_any
$display("t=%0t woke=%0d", $time, woke);
if (!woke) $display("FAIL: the input argument was a copy taken at the call, so it never saw flag rise");
else $display("PASS: the ref argument saw flag rise at t=5");
$finish;
end
initial #5 flag = 1;
endmodule
Prints: t=5 woke=1 PASS: the ref argument saw flag rise at t=5
Misconception
"Use inout; it passes by reference."
inout copies in at the call and copies back out at the return. While the task is waiting, it still sees its private copy.
Source IEEE 1800-2017 §13.3
Spec Misquote
"ref works in any task."
A ref argument is legal only in a subroutine with automatic lifetime. That is why a task in a module needs the keyword.
Source IEEE 1800-2017 §13.5.2
Delivery
Writes the task with ref and a busy loop: while (!f) #1;.
It works, but polls every time unit. wait (f) sleeps until the variable changes, which is what the interviewer is looking for.