Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie tamadd c function call and run with questa
• Use the SystemVerilog DPI-C interface to “import” a C routine into your test-bench.
• Compile the C code as position-independent object code and link it into a shared library (.so
on Linux, .dll
on Windows).
• Compile the SystemVerilog files with vlog –dpiheader
(or vlog -sv
) so that a matching DPI header is generated.
• Invoke the simulator with vsim -sv_lib <library-basename> <top_module>
, then run -all
.
Key steps
gcc -c -fPIC your_func.c -o your_func.o
gcc -shared -o libyourfunc.so your_func.o
vlog -dpiheader dpi_hdr.h testbench.sv
vsim -sv_lib yourfunc testbench
Theoretical foundations
• DPI-C (Direct Programming Interface for C) is part of the IEEE 1800-2023 SystemVerilog standard.
• It is a foreign-function interface: SystemVerilog “import” ⇒ C “export”.
• Data are passed by value for simple types (bit
, int
, longint
, real
, etc.); by reference for arrays, structs or ref
, inout
arguments; all marshalled by the simulator at run-time.
• Questa identifies a shared object by stripping the lib
prefix and file-type extension supplied with -sv_lib
.
Minimal working example
C code (adder.c
)
#include <stdio.h>
int adder(int a, int b) /* must match SV prototype */
{
printf("C-side: %d + %d = %d\n", a, b, a + b);
return a + b;
}
SystemVerilog test-bench (tb.sv
)
module tb;
import "DPI-C" function int adder (input int a, input int b);
int r;
initial begin
r = adder (10, 20);
$display("SV-side: Result = %0d", r);
$finish;
end
endmodule
Build & run script (Linux)
# 1. compile C → PIC object and shared lib
gcc -Wall -Werror -c -fPIC adder.c -o adder.o
gcc -shared -o libadder.so adder.o
# 2. compile SystemVerilog, generate DPI header (optional but good practice)
vlog -dpiheader adder_dpi.h tb.sv # creates adder_dpi.h
# 3. simulate, telling Questa where the shared lib lives
vsim -c -sv_lib adder tb -do "run -all; quit -f"
Output excerpt
C-side: 10 + 20 = 30
SV-side: Result = 30
Cross-platform notes
• Windows: build adder.dll
with gcc -shared -o adder.dll adder.o
(or MSVC). Launch vsim -sv_lib adder tb
.
• macOS (Questa 2023+): build libadder.dylib
similarly.
Frequently-used compile flags
• -fPIC
→ Position Independent Code, mandatory for shared libraries on ELF/ELF-like systems.
• -g
for C and +acc
for SV when you need source-level debugging through Questa’s built-in GDB bridge.
• -dpiheader file.h
forces vlog
to produce a header containing typedefs for SV types (svBitVecVal
, etc.)—reduces signature mismatches.
Common pitfalls
• Name mismatch: import "DPI-C" function int my_func(...);
must find an int my_func(...)
symbol after linking.
• 32- vs 64-bit: compile C with the same word size as the simulator (vsim -64
, or default 32-bit on older installations).
• Library search path: use absolute path or set LD_LIBRARY_PATH
(Linux) / PATH
(Windows) / DYLD_LIBRARY_PATH
(macOS).
• Siemens EDA (Mentor) Questa 2024.1 introduced:
– Automatic re-compilation of DPI shared libraries on vopt
when timestamps differ.
– Built-in Clang 16 tool-chain (questa_clang++
) for uniform Windows/Linux builds, removing external GCC dependency.
– Support for C11 _Atomic
, aligned_alloc
, and __int128
when using -std=c11
.
• UVM-based verification plans increasingly rely on DPI for “scoreboard off-load” (e.g., golden reference written in C/C++/Python via Python-C-API).
• Growing adoption of “Foreign Simulation Interface” (FSI) in Questa for co-simulation with MATLAB or Python—DPI is still the default for pure C interaction.
• How Questa maps types
SystemVerilog | C type in dpi_hdr.h | Notes |
---|---|---|
bit / logic | svLogic | 4-state; passed by value |
int (32-bit signed) | int | Two’s-complement |
longint (64-bit) | long long | 64-bit machine word |
real | double | IEEE-754 double |
string | const char* | NUL-terminated UTF-8 |
• Returning arrays: SV must allocate; C receives a pointer (shortint foo[10]
in SV ⇒ const shortint*
in C).
• Task vs function: import as task
if you need to suspend simulation (svSetDelay()
, svGetSimTime()
from VPI).
• Ensure that any third-party C code linked through DPI complies with its original license (GPL-incompatible code may contaminate closed test-bench IP).
• Export-controlled algorithms (e.g., cryptography) might require ITAR/EAR compliance when shared with offshore simulation farms.
• Runtime printf
from C can be redirected to Questa’s transcript; avoid excessive output because it can drastically slow down simulation.
• Automate the flow with make
or QuestaSim/Makefile
:
LIB := adder
CFILES := adder.c
SVFILES := tb.sv
all: sim
$(LIB).so: $(CFILES)
$(CC) -fPIC -c $< -o $@.o
$(CC) -shared -o lib$@ $@.o
compile: $(LIB).so
vlib work
vlog -dpiheader dpi.h $(SVFILES)
sim: compile
vsim -c -sv_lib $(LIB) tb -do "run -all; quit -f"
clean:
rm -rf work lib$(LIB).so *.o *.wlf transcript vsim.* dpi.h
• Debugging
– vsim -foreign debuglib=libadder
to pre-load GDB symbols.
– Use callstack -dpi
in the Questa console to see mixed SV/C stack frames.
• CI integration: Siemens’ Questa PrimeSim can be scripted entirely in batch; add the make target to your GitLab CI/CD.
• DPI does not allow calling back into SV from C unless you pass a callback handle. For complex cross-domain synchronization, use VPI or the new SLI (SystemVerilog Language Interface) instead.
• On Windows, anti-virus real-time scanners occasionally lock .dll
files, producing “failed to load shared library” errors—exclude your simulation directory.
• 32-bit Questa installations are deprecated after 2022; verify with vsim -version
.
• Examine examples/dpi
inside the Questa installation tree—every release ships regression-tested demos.
• Study IEEE 1800-2023 Annex I (DPI) for full type-mapping rules, clock-callback API, transaction streaming.
• Consider the Accellera DPI Working Group proposals for direct C++ class import (CXX-DPI) that will reduce the need for extern "C"
wrappers.
• Explore co-simulation with Python via pybind11 + DPI for algorithmic reference models.
Using Questa to call C code from SystemVerilog revolves around the DPI-C interface: write the C routine, compile it into a shared library, import the function in SV, and instruct vsim
to load the library. Maintain strict type and name consistency, compile with -fPIC
, and point Questa to the library via -sv_lib
. With the flow scripted (Makefile or .do
file), the integration becomes deterministic, repeatable, and scalable for modern UVM and mixed-language verification environments.