← Design Verification Interviews

Interview Question · Concept

Import and Export in DPI

L0 FresherL1 Juniordpisystemverilog

The Question

The interviewer asks

What is the difference between import "DPI-C" and export "DPI-C"? When do you use a task instead of a function on each side?

Reveal the Model Answer

An import lets SystemVerilog call a C routine. An export lets C call a SystemVerilog function or task. The import line gives the C name, the directions and the types. The export line names only the SystemVerilog routine, because its own declaration already has the types. Use functions when C computes and returns at once. Use tasks only when C must call an exported task that waits for simulation time.

DeclarationWho calls whomWhat the line carriesCan time pass?
import "DPI-C" functionSystemVerilog calls CC name, directions, typesNo
import "DPI-C" context taskSystemVerilog calls CSame, plus contextOnly inside an exported task it calls
export "DPI-C" functionC calls SystemVerilogThe routine name onlyNo
export "DPI-C" taskC calls SystemVerilogThe routine name onlyYes, inside the task body

The four DPI declarations. Time only passes in SystemVerilog code.

Self-Check

Tick each key point you covered in your answer.

What Interviewers Listen For (Fresher)

Says which side calls which, and writes one import line with directions and types.

Cross-Questions

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

WhyWhat IfBreak ItCompare

  1. Import and Export in DPI
  2. WhyWhy an Export Line Has No TypesL1 Junior

    Why does export "DPI-C" function sv_accept; carry no argument list, when an import must spell out every type?

    Reveal the Model Answer

    For an export, the SystemVerilog function declaration already holds the types. Repeating them would give two copies that can drift apart. For an import, the C body is invisible to SystemVerilog, so the import line is the only description it has. The simulator writes a C prototype for both into its generated header.

    Self-Check

    Tick each key point you covered in your answer.

    Spec Misquote

    Writes export "DPI-C" function int sv_accept(input int v);.

    The standard does not allow a return type or arguments on an export line. The tool rejects it before any C is compiled.

    Source Spear & Tumbush, SystemVerilog for Verification (3rd ed.), §12.8.1 (the export declaration takes no return type or arguments)

  3. CompareDPI Compared with VPIL1 JuniorL2 Mid-Level

    Compare DPI with VPI. Which one do you use for a C reference model, and which one for a tool that browses the design?

    Reveal the Model Answer

    DPI is a typed function call: you pass values in and get values out, like any C call. VPI is an object model of the design. C code finds signals by name, reads and writes them, and registers callbacks on value changes. Use DPI for reference models and helper code. Use VPI for tools such as debuggers, waveform dumpers or coverage readers that must walk the hierarchy.

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "DPI lets C read any signal in the design by its name."

    DPI sees only the arguments it is given, plus exported routines. Reading by name is VPI work, and the signals must also be visible to VPI.

    Source IEEE 1800-2017 §36 (VPI overview), IEEE 1800-2017 §35.2 (DPI overview)

  4. What IfA C Model That Needs a DUT SignalL2 Mid-Level

    What if the C model needs the value of a DUT status register at each prediction?

    Reveal the Model Answer

    Pass it in. The monitor samples the register and gives it to the import as an argument. If C must ask for it in the middle of a call, export a small getter function and make the import context. Avoid hierarchical lookups from C: they hide what the model depends on, and they break when the hierarchy changes.

    Self-Check

    Tick each key point you covered in your answer.

    Wrong Altitude

    "Have C look the register up through VPI when it needs it."

    It works, but now the model depends on a hierarchical path and on VPI visibility switches. An argument keeps the dependency visible in the import line.

  5. Break ItCalling a Class Object from CL2 Mid-Level

    Asked if they want C to call a scoreboard object

    What if C needs to call a method of a scoreboard object? Can you export the class method?

    Reveal the Model Answer

    No. Class methods cannot be exported, because objects do not exist when the design is elaborated. Export a function in a module or package instead. Keep the objects in a table on the SystemVerilog side, and pass C an index or an ID. The exported function looks the object up and calls its method.

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "Pass the class handle to C, and C calls back with it."

    A class handle cannot cross the DPI boundary at all. Only a chandle crosses, and that holds a C pointer, never a SystemVerilog object.

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

  6. What IfHow C Code Joins the SimulationL1 JuniorL2 Mid-Level

    What if someone asks how the C code actually gets into the simulation? Describe the build.

    Reveal the Model Answer

    With Verilator, you list the .c or .cpp files on the command line. They are compiled and linked into the one simulation binary. Many event-driven simulators instead load a shared library at run time, named with a switch such as -sv_lib. Either way, the C must use C linkage, so C++ files need extern "C".

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "The simulator reads the C source and interprets it."

    C is always compiled by a C or C++ compiler. The simulator only links the compiled code, either into its binary or as a loaded library.

    Source IEEE 1800-2017 Annex J (inclusion of foreign language code)

What People Get Wrong

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

Misconception

"Make the import a task, and the C step takes one clock."

Being a task allows time to pass only inside SystemVerilog code. The C body still returns at once. The twin reads $time before and after the call.

Run It

An imported task called right after a clock edge.

RUNVerilator + C/C++ over DPI · simulator service
Edit the code, then run it
Expected: before=5 after=5 r=8 FAIL: the imported task returned at the same time, so no clock passed inside C
See the Fix
`timescale 1ns/1ps
module tb;
  bit clk = 0;
  always #5 clk = ~clk;
  // The fix: C computes at once; the clock wait is written in SystemVerilog.
  import "DPI-C" task c_step(input int v, output int r);
  int r;
  longint t0;
  initial begin
    @(posedge clk);
    t0 = $time;
    c_step(4, r);
    @(posedge clk);
    $display("before=%0d after=%0d r=%0d", t0, $time, r);
    if ($time == t0) $display("FAIL: the imported task returned at the same time, so no clock passed inside C");
    else $display("PASS: the wait in SystemVerilog moved time by one clock period");
    $finish;
  end
endmodule

Prints: before=5 after=15 r=8 PASS: the wait in SystemVerilog moved time by one clock period

Misconception

"Any import may call an export."

Only a context import carries the calling scope that an export needs. Verilator 5.052 stops the run with "scope wasn't set" when a plain import calls an export.

Source IEEE 1800-2017 §35.5.3; ASIC Point engine notes (Verilator 5.052 DPI probe, 2026-11-08: plain import calling an export aborts the run)

Half-Truth

"export brings a C function into SystemVerilog."

The direction is the other way round. The keyword names what SystemVerilog offers to C. A C function always arrives through an import.

Learn It, Then Prove It