Home > Community > Forums > Functional Verification > C++ code through DPI?

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 C++ code through DPI? 

Last post Mon, Apr 23 2007 12:30 AM by archive. 2 replies.
Started by archive 23 Apr 2007 12:30 AM. Topic has 2 replies and 1268 views
Page 1 of 1 (3 items)
Sort Posts:
  • Mon, Apr 23 2007 12:30 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    C++ code through DPI? Reply

    Hi all,

    I have to encapsulte some transaction layer C++ code using DPI, is there any example on how to do it?

    Shall I simply replace gcc with g++ to compile my code?

    Thanks!

    Best regards,
    Davy


    Originally posted in cdnusers.org by davyzhu
    • Post Points: 0
  • Mon, Apr 23 2007 1:08 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: C++ code through DPI? Reply

    Hi Davy,

    It depends on exactly what you are trying to do. DPI is a C interface, so you can't blindly compile the DPI code using g++ and have that work. At a minimum, you need to make sure that the import and export c functions are defined as extern "C" so that they don't use mangled names. The export functions will always be C functions (that is how the verilog simulator exposes them), so all you need to do is make sure that your header file has extern "C" in front of the import function name. Your import functions are presumably the functions which are wrapping your c++ code. Again, you should be able to just wrap the functions with extern "C".

    For instance, you can have the following:
    //my.cpp
    #include
    using namespace std;
    extern "C" void doit() ;
    void doit() {
    cout << "hi from doit" << endl;
    }
    //test.sv
    module test;
    initial doit();
    import"DPI" function void doit();
    endmodule

    Now, if what you want to do is call a class method from a dpi import function, you will need to be a little more clever. The easiest thing to do is to keep a map on the c++ side which has string (or integral) lookup keys to access specific objects. Then, you need the sv function call to include the key for the object that you want to execute the function on. This means basically wrapping each class method with a c function (there are tricks you can do like using the boost bind library to create a more generic way of doing this, but the basic requirments are still the same).

    john


    Originally posted in cdnusers.org by jlrose
    • Post Points: 0
  • Tue, Apr 24 2007 2:02 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: C++ code through DPI? Reply

    Hi John,

    Sorry for the ambiguous problem and I will try to clarify it. The C++ files mainly for reference model in testbench (written by other people).

    Now image I have a c++ file have just one class, and I can use your turn function in class to pure function method.

    But If I have a c++ file have several class (class A contains class B object, class B contains class C object , and so on), how can I modify so many things by hand. And I don't want to break the encapsulation for it will be hard for me to update the C++ code.

    Any suggestions are welcome!

    Best regards,
    Davy


    Originally posted in cdnusers.org by davyzhu
    • Post Points: 0
Page 1 of 1 (3 items)
Sort Posts:
Started by archive at 23 Apr 2007 12:30 AM. Topic has 2 replies.