• 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. Manipulating C Global Variables Using DPI

Stats

  • Locked Locked
  • Replies 0
  • Subscribers 64
  • Views 13620
  • 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

Manipulating C Global Variables Using DPI

archive
archive over 18 years ago

I'm sharing this code as an example of how two different DPI calls (calls to same function in two different parts of the hierarchy) can both manipulate a C global variable.
----file: my_dpi.c---- #include "stdio.h" #include "svdpi.h" // any function or variable declared 'static' - which is outside the // scope of any DPI function, is a global func/var. static int my_global_int = 4; // FYI - VPI function io_printf() will print to ncsim.log file // This function can be called from any Verilog Hierarchy // and manipulate my_global_in int c_func () { my_global_int++; // increment the global variable via DPI call io_printf("C: Function Calling HDL scope is %s \n", svGetNameFromScope(svGetScope() ) ); io_printf("C: increment my_global_in to %d \n", my_global_int); return my_global_int; } --------------- ----file: test_dut.sv // this example developed to test if DPI can manipulate // a C global variable from different hierarchies module dut (); import "DPI-C" context c_func = function int sv_func (); int b; initial begin b = sv_func(); $display("Verilog: %m %d", b); end endmodule // ------------------------ module test(); import "DPI-C" context c_func = function int sv_func (); int b; initial begin b = sv_func(); $display("Verilog: %m %d", b); end endmodule // ------------------------ module top(); test U1(); dut U2(); endmodule ---------------- ncsim: 05.83-s006: (c) Copyright 1995-2007 Cadence Design Systems, Inc. ncsim> run C: Function Calling HDL scope is top.U1 C: increment my_global_in to 5 Verilog: top.U1 5 C: Function Calling HDL scope is top.U2 C: increment my_global_in to 6 Verilog: top.U2 6 ncsim: *W,RNQUIE: Simulation is complete. ncsim> exit -------script: ------------- ncvlog -sv -linedebug test_dut.sv ncelab top gcc -m32 -fPIC -g -shared -o my_dpi.so my_dpi.c -I/`ncroot`/tools/inca/include ncsim top -sv_lib ./my_dpi.so


Originally posted in cdnusers.org by tmackett
  • 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