This is a demonstration of how to call a routine in Python from Verilog using a C++ VPI hook intermediary layer.
Before compiling, make sure to update the following variables in Makefile
:
PYTHON_LIB_DIR
- should point at the directory containing thelibpython
shared library for your installation.PYTHON_LIB_NAME
- should be the name of your Python library (excluding thelib
prefix and.so
/.dylib
suffix).ICARUS_LIB_DIR
- should point at the directory containinglibvpi.a
, provided with Icarus Verilog.ICARUS_INC_DIR
- should point at the directory containing header files for Icarus Verilog (specificallyvpi_user.h
).ICARUS_VPI_LIB
- should be the name of the VPI library provided with Icarus Verilog (excluding thelib
prefix and.a
/.so
suffix).
Once you've adjusted the configuration as above, execute make run
to run the demo:
$> make run
# Creating directory output/obj
# Compiling demo.cpp -> demo.o
# Linking objects to form demo.vpi
# Compiling Verilog
About to call C++ from Verilog through VPI
About to call Python from C++ via pybind11
This is from Python - calling C++: 3
The following steps are happening:
- In
demo.v
a call is made to a$demo
system task. - When
$demo
is called, it actually executes a VPI function fromdemo.cpp
calledtrampoline_calltf
. - The trampoline function then calls
do_something
- which is a function imported from thedemo.py
. - The
do_something
function indemo.py
then calls back to a function exposed from C++ calledadd
(seedemo.cpp
lines 19-22) again using pybind11.