← Design Verification Interviews

Interview Question · Concept

What Context and Pure Promise

L1 JuniorL2 Mid-LevelL3 Seniordpiperformance

The Question

The interviewer asks

What does context give the C code, and what does pure allow the simulator to do? Which do you choose for a stateful reference model?

Reveal the Model Answer

A context import gets the calling scope. So C may call exports, use svGetScope and use VPI. The simulator must record that scope and assume C may change SystemVerilog state. A pure function promises its result depends only on its arguments. So the simulator may skip or reuse calls, though it does not have to. A stateful model is never pure: use a plain import, and context only if it calls back.

Self-Check

Tick each key point you covered in your answer.

What Interviewers Listen For (Junior)

Names one thing each keyword allows, and rejects pure for a model that keeps state.

Cross-Questions

Where the interviewer goes next. Open a node to practise that follow-up.

WhyBreak ItCompareScale It

  1. What Context and Pure Promise
  2. WhyWhy Context Imports Cost MoreL2 Mid-Level

    Why is a context import slower than a plain one?

    Reveal the Model Answer

    On each call, the simulator records the calling scope so C can ask for it. C may also call exports that change SystemVerilog variables. So the simulator cannot keep values cached across the call. A plain import promises neither, so the call can be cheaper.

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "Context makes the call run in a separate thread."

    Every DPI call runs on the calling simulator thread and returns before the caller continues. Context adds the scope, not a thread.

    Source Lesson D1.7 (the call chain is synchronous; measured on Verilator 5.052)

  3. CompareCallbacks Compared with ArgumentsL2 Mid-LevelL3 Senior

    The C model needs five configuration values. Compare passing them as arguments with letting C fetch them through exported getters.

    Reveal the Model Answer

    Arguments keep the import plain and the data flow visible. C cannot read anything the testbench did not hand it. Getters need a context import and hide the dependency inside C. They also run on every call unless C caches them. Pass configuration once, at model creation or reset, and keep callbacks for real events such as reporting an error.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Senior)

    Puts configuration in a create or reset call, so the hot path stays a plain import.

    Half-Truth

    "Getters are better, because C always sees the latest value."

    Configuration rarely changes inside a test. When it does, a reset or reconfigure call makes the change explicit and testable.

  4. Break ItA Stateful Function Marked PureL3 Senior

    Break it: a sequence-number model is declared pure, and every test passes on your simulator. What could happen on another simulator or a new release?

    Reveal the Model Answer

    The other tool may skip a call whose result looks unused, or reuse an earlier result for the same arguments. Then the model misses an update, and its sequence numbers drift from the DUT. Nothing in your passing runs proves the declaration is safe. Review imports for pure on any function with side effects.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Senior)

    Treats a passing run as no evidence here, and fixes it with a declaration rule, not a test.

    Tool as Truth

    "It passed on our simulator, so pure is fine."

    Skipping is optional. Verilator 5.052 never skipped in our probe, so a run there cannot show the bug that another tool may expose.

    Source ASIC Point engine notes (Verilator 5.052 DPI probe, 2026-11-08: a pure import with an unused result was still called)

  5. Scale ItKeeping Pure Honest in a TeamL3 SeniorL4 Staff

    How do you stop this mistake across a team of twenty engineers?

    Reveal the Model Answer

    Keep every import in one package, so a reviewer sees all declarations together. Allow pure only for functions that are clearly mathematical, such as a CRC of the arguments. Run the regression on a second simulator when the project has one, because skipped calls show up there first.

    Self-Check

    Tick each key point you covered in your answer.

    Half-Truth

    "Write a test that checks the pure function is called every time."

    A test cannot force the optimiser to act. The tool that skips calls may not be the one the test runs on.

What People Get Wrong

Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.

Misconception

"pure makes the simulator cache the result, so calling it in a loop is free."

It is a permission, not a promise. Verilator 5.052 made every call in the twin. Another simulator may reuse results, which is why state must never hide behind pure.

Run It

A pure import called 100 times with the same argument. A C counter records each real call.

RUNVerilator + C/C++ over DPI · simulator service
Edit the code, then run it
Expected: x=9 calls into C=100 FAIL: the simulator made all 100 calls; pure allows reuse but does not promise it
See the Fix
module tb;
  // The fix: the testbench calls once and keeps the value itself.
  import "DPI-C" pure function int c_sq(input int v);
  import "DPI-C" function int c_calls();
  int x;
  initial begin
    x = c_sq(3);
    for (int i = 0; i < 100; i++) assert (x == 9);
    $display("x=%0d calls into C=%0d", x, c_calls());
    if (c_calls() == 100) $display("FAIL: the simulator made all 100 calls; pure allows reuse but does not promise it");
    else if (c_calls() == 1) $display("PASS: one call, because the testbench reused the value");
    $finish;
  end
endmodule

Prints: x=9 calls into C=1 PASS: one call, because the testbench reused the value

Misconception

"A function that only reads a C global is still pure."

Its result depends on that global, not only on its arguments. If the simulator reuses an old result, the model reads stale state.

Source IEEE 1800-2017 §35.5.2 (pure functions)

Half-Truth

"context lets C read the module's variables directly."

It gives C a scope, not a pointer into the module. C still reads through exported functions or VPI.

Buzzword Without Mechanism

"Mark every import context, just to be safe."

Each context call makes the simulator save the scope. It must also treat the call as able to change any SystemVerilog state, which blocks optimisation.

Source Spear & Tumbush, SystemVerilog for Verification (3rd ed.), §12.7

Learn It, Then Prove It