Starting with DPI

I haven’t worked on using DPI calls and currently am figuring out one step at a time on how it gets done. I would like to know the direction (tutorials/papers/etc) to be taken for me to achieve what I want. I have looked into the cookbook and other material online but have been unable to figure out if these help me in what I need to achieve. Here’s the project I am trying to achieve

  1. I have a DUT model(written in C++) which has been developed for architectural purposes. I am trying to integrate this model into my verification environment (UVM)
  2. The C++ model has a main() function which gets called when it is run independently. Under this main() function there are other class initialization and numerous other function calls made. However these functions’ definitions are inside other C++ files. All the necessary files that the model requires are included via multiple “.h” files in the top file.

I am trying to figure out a few things first, so any help is appreciated.

  1. I believe the main() function cannot be directly called from SV. Is this right?
  2. Does this mean I would need to change the main() function into another function name and then have this imported via “DPI-C” inside my SV code?
  3. Let’s say I changed the main() function name to svCall(). Now, do all the other class and function definitions that are included as part of the “.h” files get called just by invoking the svCall() via DPI call? (I am including the filelist of all the .cpp and .h files to g++)

In reply to tpan:

  • Technically, it’s possible, but not practical. It involves some symbol re-naming, but there is no way to pass the argv/argc arguments its expecting
  • Yes, the DPI is defined SystemVerilog centric. You have to start from SystemVerilog
  • How you compile your C/C++ code and link it together is tool specific. Many tools allow you to throw everything on the command line and figure out which compiler to use based on the file extension. And that’s separate from the calling stack.

Here are some other links you might want to read:

https://verificationacademy.com/cookbook/cbasedstimulus

In reply to dave_59:

I went through some of the links you provided. Thanks, Dave. Played around with simple functions and got a hang of the basic stuff. I have a follow-up question. In my case there are some header files which are being passed to the tool for compilation along with the SV files (I know they are getting passed since initially I was getting errors complaining that these files were missing).

Within some of the header files there are namespace declarations made. These get imported before in the main C++ file via “using namespace”. However I find that whenever a declaration is being made using this namespace, g++ throws an error since it cannot find it. Is this a restriction that comes along with SV?

Example of the error seen -


#include <iostream>
#include "example.h"  // There's a namespace declaration inside this called 'ns'.It has a class declaration called classA

using namespace ns;
using namespace std;

extern "C" void svCall() {
  ...
  ns::classA A; // Compilation error is seen saying 'undefined reference to ns::classA'
  ...
}