- Rich Edelman - Mentor Graphics
- Rohit Jain - Mentor Graphics
- Hui Yin - Mentor Graphics
SystemVerilog DPI-C is commonly used for many different things - all interfacing SystemVerilog with C. Although well used in places, it is not uniformly used, nor is it used always in optimal ways. This paper will revisit the DPI-C use model, suggest common usage and recommend preferred optimizations.
SystemVerilog DPI-C  is a powerful, simple and fast interface between C code and the SystemVerilog language. It is a powerful way for SystemVerilog to access any C library that is available. It is simple because C calls share the same function and task call semantics as a regular SystemVerilog function or task call. C calls and SystemVerilog calls can be used interchangeably. It is fast because it uses the regular C calling conventions (push the function arguments on the stack and jump). Some implementations may choose to copy the function arguments first, but the fastest implementations provide call-by-reference arguments without copying. This paper will explore SystemVerilog DPI-C for the new user as well as the experienced user. After reading this paper, the reader should feel comfortable using SystemVerilog DPI-C to help solve their verification needs. A previous work  has many useful examples of using SystemVerilog DPI-C to achieve verification results. These examples are still valid, useful and should be used as a resource.
A. Imports and Exports
In SystemVerilog, a DPI-C call is either an import or an export. These definitions are from the point-of-view of SystemVerilog. An "import" function or task is C code that is IMPORTED, so that SystemVerilog may call it. An "export" function or task is SystemVerilog code that is EXPORTED, so that C code may call it.
B. Tasks and Functions
Tasks and functions are similar to each other. They may be used interchangeably in the remainder of the paper (due to laziness or error), but they are different. Both a task and function can take arguments - inputs, outputs and inouts. A function can return a value with the return statement, or by updating an output or inout argument. A task can only return a value by updating an output or inout argument. A function cannot consume simulation time. A task can consume simulation time.
C. Task and Function Arguments
Generally speaking, any SystemVerilog value can be passed across the DPI-C interface. Normally the best arguments to pass across the interface are values that have a direct representation in C. For example integers, shorts, bytes and real numbers. Passing a 4-state value to C is permitted, but our recommendation is that data on the C side is most optimal as 2-state values.
View & Download:
Read the entire DPI Redux. Functionality. Speed. Optimization. technical paper.