• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Functional Verification
  3. How to interface Cpp code through DPI

Stats

  • Locked Locked
  • Replies 2
  • Subscribers 65
  • Views 17743
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to interface Cpp code through DPI

archive
archive over 14 years ago

I've been getting quite a few questions on this topic lately.  Here's one way to do it.  The main issue is that since all DPI is initiated by the simulator, if you use Cpp, then the complete SV <--> DPI <--> Cpp function call must be instantaneous.  This post describes a way to have your Cpp active throughout the simulation.

======file: top.sv =======================
package my_pkg;

import "DPI-C" function int dpi_build_cpp();
import "DPI-C" function void dpi_write_api(int n);
import "DPI-C" function int dpi_read_api();

endpackage // my_pkg
//---------------

module top;
import my_pkg::*;

initial begin
  if (!dpi_build_cpp() )
    $display("Vlog: Cpp did not construct correctly\n");
  else
    $display("Vlog: Cpp ready to go\n");

  dpi_write_api(1234567890);

  #100ns;  // let some time go by

  $display("Vlog: Retriev Cpp member %0d", dpi_read_api() );

  if (!dpi_build_cpp() )
    $display("Vlog: Cpp did not construct correctly\n");
  else
    $display("Vlog: Cpp ready to go\n");

end

endmodule

====file: dpi_cpp_api.cpp ======
#include "dpi_cpp_api.h"

// for vpi_printf
#include "vpi_user.h"

// for printf
#include <stdio.h>

// for cout etc
#include <iostream>

using namespace std;

// Class pointer  must be global - otherwise memory will be cleaned up after DPI call
mycpp_class* mycpp_class_inst = 0;

// #include <iostream>
//----------------C++ -------------------------

mycpp_class::mycpp_class() {
  cout << "Cpp: mycpp_class constructor" << endl;
}

mycpp_class::~mycpp_class() {
}

void mycpp_class::put_cpp(int x_cpp) {
  cout << "Cpp: put_cpp = " << x_cpp << endl;
  mycppint = x_cpp;
}

int mycpp_class::get_cpp() {
  cout << "Cpp: get_cpp\n" << endl;
  return mycppint;
}


//-----------------C/DPI wrapper----------------
extern "C" {

int dpi_build_cpp() {
  // check to see if class has been new-ed yet, if not new it
  if (mycpp_class_inst == 0) {
      mycpp_class_inst = new mycpp_class();
      vpi_printf("DPI-C: Cpp interface to DPI ready to go\n");
      return 1;
  }
  else {
      vpi_printf("DPI-C: Please don't call me twice\n");
      return 0;
  }
}

void dpi_write_api(int xx_c) {
  vpi_printf("DPI-C: dpi_write_api = %0d\n", xx_c);
  mycpp_class_inst -> put_cpp(xx_c);
  }

int dpi_read_api() {
  vpi_printf("DPI-C: dpi_read_api returning value\n");
  return mycpp_class_inst -> get_cpp();
  }

}  // extern C


===File: dpi_cpp_api.h ======
class mycpp_class {
public:
  mycpp_class();    // constructor

  ~mycpp_class();  // destructor

  void put_cpp(int xyz);

  int get_cpp();

 private:
  int mycppint;

};

=====3 Step Script============

  more RUN3
rm -rf  ./INCA_libs *.so

g++ -g -m32 -fPIC -shared -o dpi_cpp_api.so \
-I. -I./c_src -I`ncroot`/tools/inca/include  \
./dpi_cpp_api.cpp || exit

ncvlog -sv -linedebug top.sv  || exit

ncelab top -dpiheader mydpi.h || exit

# -uselic IES for Cpp debug in Simvision
ncsim top -sv_root ./ -sv_lib ./dpi_cpp_api.so -uselic IES

=====irun Script==============

rm -rf INCA_libs
rm *.so

# -uselic IES for Cpp debug in Simvision
irun top.sv -line -dpiheader mydpi.h -g -I.-cpost "dpi_cpp_api.cpp" -end -uselic IES

 =========Simulation Output==============================
ncsim> run
Cpp: mycpp_class constructor
DPI-C: Cpp interface to DPI ready to go
Vlog: Cpp ready to go

DPI-C: dpi_write_api = 1234567890
Cpp: put_cpp = 1234567890
Vlog: Retriev Cpp member DPI-C: dpi_read_api returning value
Cpp: get_cpp

1234567890
DPI-C: Please don't call me twice
Vlog: Cpp did not construct correctly

ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit

 

  • Cancel
Parents
  • vujaclyn
    vujaclyn over 4 years ago

    What is the flag -m32? If I use this flag, I'm getting incompatible /usr/lib64/libssl.so.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • vujaclyn
    vujaclyn over 4 years ago

    What is the flag -m32? If I use this flag, I'm getting incompatible /usr/lib64/libssl.so.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • StephenH
    StephenH over 4 years ago in reply to vujaclyn

    -m32 is an option to the GNU C compiler asking it to produce binaries in 32 bit mode. Without that, on a 64-bit system you get 64-bit binaries by default.

    Xcelium defaults to 32-bit compilation because it runs faster than 64-bit, however sometimes CAD environments force Xcelium into 64-bit mode either by adding the 64-bit bin directory to PATH or by setting an environment variable CDS_AUTO_64BIT.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information