CIRCT  19.0.0git
svdpi.h
Go to the documentation of this file.
1 /*===-- dpi/svdpi.h - SystemVerilog Direct Programming Interface --*- C -*-===*\
2 |* *|
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4 |* Exceptions. *|
5 |* See https://llvm.org/LICENSE.txt for license information. *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7 |* *|
8 |*===----------------------------------------------------------------------===*|
9 |* *|
10 |* This file contains the constant definitions, structure definitions, *|
11 |* and routine declarations used by SystemVerilog DPI. *|
12 |* *|
13 |* This file is from the SystemVerilog IEEE 1800-2017 Annex I. *|
14 |* *|
15 \*===----------------------------------------------------------------------===*/
16 
17 // clang-format off
18 
19 #ifndef DPI_SVDPI_H
20 #define DPI_SVDPI_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* Define size-critical types on all OS platforms. */
27 #if defined(_MSC_VER)
28 typedef unsigned __int64 uint64_t;
29 typedef unsigned __int32 uint32_t;
30 typedef unsigned __int8 uint8_t;
31 typedef signed __int64 int64_t;
32 typedef signed __int32 int32_t;
33 typedef signed __int8 int8_t;
34 #elif defined(__MINGW32__)
35 #include <stdint.h>
36 #elif defined(__APPLE__)
37 #include <stdint.h>
38 #elif defined(__linux) || (defined(__APPLE__) && defined(__MACH__))
39 #include <inttypes.h>
40 #else
41 #include <sys/types.h>
42 #endif
43 
44 /* Use to import a symbol into dll */
45 #ifndef DPI_DLLISPEC
46 #if (defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__))
47 #define DPI_DLLISPEC __declspec(dllimport)
48 #else
49 #define DPI_DLLISPEC
50 #endif
51 #endif
52 
53 /* Use to export a symbol from dll */
54 #ifndef DPI_DLLESPEC
55 #if (defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__))
56 #define DPI_DLLESPEC __declspec(dllexport)
57 #else
58 #define DPI_DLLESPEC
59 #endif
60 #endif
61 
62 /* Use to mark a function as external */
63 #ifndef DPI_EXTERN
64 #define DPI_EXTERN
65 #endif
66 
67 #ifndef DPI_PROTOTYPES
68 #define DPI_PROTOTYPES
69 /* object is defined imported by the application */
70 #define XXTERN DPI_EXTERN DPI_DLLISPEC
71 /* object is exported by the application */
72 #define EETERN DPI_EXTERN DPI_DLLESPEC
73 #endif
74 
75 /* canonical representation */
76 #define sv_0 0
77 #define sv_1 1
78 #define sv_z 2
79 #define sv_x 3
80 
81 /* common type for 'bit' and 'logic' scalars. */
82 typedef uint8_t svScalar;
83 typedef svScalar svBit; /* scalar */
84 typedef svScalar svLogic; /* scalar */
85 
86 /*
87  * DPI representation of packed arrays.
88  * 2-state and 4-state vectors, exactly the same as PLI's avalue/bvalue.
89  */
90 #ifndef VPI_VECVAL
91 #define VPI_VECVAL
92 typedef struct TVpiVecval {
93  uint32_t aval;
94  uint32_t bval;
96 #endif
97 
98 /* (a chunk of) packed logic array */
100 
101 /* (a chunk of) packed bit array */
102 typedef uint32_t svBitVecVal;
103 
104 /* Number of chunks required to represent the given width packed array */
105 #define SV_PACKED_DATA_NELEMS(WIDTH) (((WIDTH) + 31) >> 5)
106 
107 /*
108  * Because the contents of the unused bits is undetermined,
109  * the following macros can be handy.
110  */
111 #define SV_MASK(N) (~(-1 << (N)))
112 
113 #define SV_GET_UNSIGNED_BITS(VALUE, N) \
114  ((N) == 32 ? (VALUE) : ((VALUE)&SV_MASK(N)))
115 
116 #define SV_GET_SIGNED_BITS(VALUE, N) \
117  ((N) == 32 ? (VALUE) \
118  : (((VALUE) & (1 << (N))) ? ((VALUE) | ~SV_MASK(N)) \
119  : ((VALUE)&SV_MASK(N))))
120 
121 /*
122  * Implementation-dependent representation.
123  */
124 /*
125  * Return implementation version information string ("1800-2005" or "SV3.1a").
126  */
127 XXTERN const char *svDpiVersion(void);
128 
129 /* a handle to a scope (an instance of a module or interface) */
130 XXTERN typedef void *svScope;
131 
132 /* a handle to a generic object (actually, unsized array) */
133 XXTERN typedef void *svOpenArrayHandle;
134 
135 /*
136  * Bit-select utility functions.
137  *
138  * Packed arrays are assumed to be indexed n-1:0,
139  * where 0 is the index of LSB
140  */
141 
142 /* s=source, i=bit-index */
143 XXTERN svBit svGetBitselBit(const svBitVecVal *s, int i);
145 
146 /* d=destination, i=bit-index, s=scalar */
147 XXTERN void svPutBitselBit(svBitVecVal *d, int i, svBit s);
148 XXTERN void svPutBitselLogic(svLogicVecVal *d, int i, svLogic s);
149 
150 /*
151  * Part-select utility functions.
152  *
153  * A narrow (<=32 bits) part-select is extracted from the
154  * source representation and written into the destination word.
155  *
156  * Normalized ranges and indexing [n-1:0] are used for both arrays.
157  *
158  * s=source, d=destination, i=starting bit index, w=width
159  * like for variable part-selects; limitations: w <= 32
160  */
161 XXTERN void svGetPartselBit(svBitVecVal *d, const svBitVecVal *s, int i, int w);
162 XXTERN void svGetPartselLogic(svLogicVecVal *d, const svLogicVecVal *s, int i,
163  int w);
164 
165 XXTERN void svPutPartselBit(svBitVecVal *d, const svBitVecVal s, int i, int w);
166 XXTERN void svPutPartselLogic(svLogicVecVal *d, const svLogicVecVal s, int i,
167  int w);
168 
169 /*
170  * Open array querying functions
171  * These functions are modeled upon the SystemVerilog array
172  * querying functions and use the same semantics.
173  *
174  * If the dimension is 0, then the query refers to the
175  * packed part of an array (which is one-dimensional).
176  * Dimensions > 0 refer to the unpacked part of an array.
177  */
178 /* h= handle to open array, d=dimension */
179 XXTERN int svLeft(const svOpenArrayHandle h, int d);
180 XXTERN int svRight(const svOpenArrayHandle h, int d);
181 XXTERN int svLow(const svOpenArrayHandle h, int d);
182 XXTERN int svHigh(const svOpenArrayHandle h, int d);
183 XXTERN int svIncrement(const svOpenArrayHandle h, int d);
184 XXTERN int svSize(const svOpenArrayHandle h, int d);
186 
187 /*
188  * Pointer to the actual representation of the whole array of any type
189  * NULL if not in C layout
190  */
192 
193 /* total size in bytes or 0 if not in C layout */
195 
196 /*
197  * Return a pointer to an element of the array
198  * or NULL if index outside the range or null pointer
199  */
200 XXTERN void *svGetArrElemPtr(const svOpenArrayHandle, int indx1, ...);
201 
202 /* specialized versions for 1-, 2- and 3-dimensional arrays: */
203 XXTERN void *svGetArrElemPtr1(const svOpenArrayHandle, int indx1);
204 XXTERN void *svGetArrElemPtr2(const svOpenArrayHandle, int indx1, int indx2);
205 XXTERN void *svGetArrElemPtr3(const svOpenArrayHandle, int indx1, int indx2,
206  int indx3);
207 
208 /*
209  * Functions for copying between simulator storage and user space.
210  * These functions copy the whole packed array in either direction.
211  * The user is responsible for allocating an array to hold the
212  * canonical representation.
213  */
214 
215 /* s=source, d=destination */
216 /* From user space into simulator storage */
218  const svBitVecVal *s, int indx1, ...);
220  const svBitVecVal *s, int indx1);
222  const svBitVecVal *s, int indx1, int indx2);
224  const svBitVecVal *s, int indx1, int indx2,
225  int indx3);
226 
228  const svLogicVecVal *s, int indx1, ...);
230  const svLogicVecVal *s, int indx1);
232  const svLogicVecVal *s, int indx1,
233  int indx2);
235  const svLogicVecVal *s, int indx1,
236  int indx2, int indx3);
237 
238 /* From simulator storage into user space */
240  int indx1, ...);
242  int indx1);
244  int indx1, int indx2);
246  int indx1, int indx2, int indx3);
248  int indx1, ...);
250  const svOpenArrayHandle s, int indx1);
252  const svOpenArrayHandle s, int indx1,
253  int indx2);
255  const svOpenArrayHandle s, int indx1,
256  int indx2, int indx3);
257 
258 XXTERN svBit svGetBitArrElem(const svOpenArrayHandle s, int indx1, ...);
259 XXTERN svBit svGetBitArrElem1(const svOpenArrayHandle s, int indx1);
260 XXTERN svBit svGetBitArrElem2(const svOpenArrayHandle s, int indx1, int indx2);
261 XXTERN svBit svGetBitArrElem3(const svOpenArrayHandle s, int indx1, int indx2,
262  int indx3);
263 XXTERN svLogic svGetLogicArrElem(const svOpenArrayHandle s, int indx1, ...);
266  int indx2);
268  int indx2, int indx3);
270  int indx1, ...);
272  int indx1);
274  int indx1, int indx2);
276  int indx1, int indx2, int indx3);
277 XXTERN void svPutBitArrElem(const svOpenArrayHandle d, svBit value, int indx1,
278  ...);
279 XXTERN void svPutBitArrElem1(const svOpenArrayHandle d, svBit value, int indx1);
280 XXTERN void svPutBitArrElem2(const svOpenArrayHandle d, svBit value, int indx1,
281  int indx2);
282 XXTERN void svPutBitArrElem3(const svOpenArrayHandle d, svBit value, int indx1,
283  int indx2, int indx3);
284 
285 /* Functions for working with DPI context */
286 
287 /*
288  * Retrieve the active instance scope currently associated with the executing
289  * imported function. Unless a prior call to svSetScope has occurred, this
290  * is the scope of the function's declaration site, not call site.
291  * Returns NULL if called from C code that is *not* an imported function.
292  */
294 
295 /*
296  * Set context for subsequent export function execution.
297  * This function must be called before calling an export function, unless
298  * the export function is called while executing an import function. In that
299  * case the export function shall inherit the scope of the surrounding import
300  * function. This is known as the "default scope".
301  * The return is the previous active scope (per svGetScope)
302  */
303 XXTERN svScope svSetScope(const svScope scope);
304 
305 /* Gets the fully qualified name of a scope handle */
306 XXTERN const char *svGetNameFromScope(const svScope);
307 
308 /*
309  * Retrieve svScope to instance scope of an arbitrary function declaration.
310  * (can be either module, program, interface, or generate scope)
311  * The return value shall be NULL for unrecognized scope names.
312  */
313 XXTERN svScope svGetScopeFromName(const char *scopeName);
314 
315 /*
316  * Store an arbitrary user data pointer for later retrieval by svGetUserData()
317  * The userKey is generated by the user. It must be guaranteed by the user to
318  * be unique from all other userKey's for all unique data storage requirements
319  * It is recommended that the address of static functions or variables in the
320  * user's C code be used as the userKey.
321  * It is illegal to pass in NULL values for either the scope or userData
322  * arguments. It is also an error to call svPutUserData() with an invalid
323  * svScope. This function returns -1 for all error cases, 0 upon success. It is
324  * suggested that userData values of 0 (NULL) not be used as otherwise it can
325  * be impossible to discern error status returns when calling svGetUserData()
326  */
327 XXTERN int svPutUserData(const svScope scope, void *userKey, void *userData);
328 
329 /*
330  * Retrieve an arbitrary user data pointer that was previously
331  * stored by a call to svPutUserData(). See the comment above
332  * svPutUserData() for an explanation of userKey, as well as
333  * restrictions on NULL and illegal svScope and userKey values.
334  * This function returns NULL for all error cases, 0 upon success.
335  * This function also returns NULL in the event that a prior call
336  * to svPutUserData() was never made.
337  */
338 XXTERN void *svGetUserData(const svScope scope, void *userKey);
339 
340 /*
341  * Returns the file and line number in the SV code from which the import call
342  * was made. If this information available, returns TRUE and updates fileName
343  * and lineNumber to the appropriate values. Behavior is unpredictable if
344  * fileName or lineNumber are not appropriate pointers. If this information is
345  * not available return FALSE and contents of fileName and lineNumber not
346  * modified. Whether this information is available or not is implementation-
347  * specific. Note that the string provided (if any) is owned by the SV
348  * implementation and is valid only until the next call to any SV function.
349  * Applications must not modify this string or free it
350  */
351 XXTERN int svGetCallerInfo(const char **fileName, int *lineNumber);
352 
353 /*
354  * Returns 1 if the current execution thread is in the disabled state.
355  * Disable protocol must be adhered to if in the disabled state.
356  */
357 XXTERN int svIsDisabledState(void);
358 
359 /*
360  * Imported functions call this API function during disable processing to
361  * acknowledge that they are correctly participating in the DPI disable
362  * protocol. This function must be called before returning from an imported
363  * function that is in the disabled state.
364  */
365 XXTERN void svAckDisabledState(void);
366 
367 /*
368  **********************************************************
369  * DEPRECATED PORTION OF FILE ENDS REMOVED.
370  * So that we don't accidently use them
371  **********************************************************
372  */
373 
374 #undef DPI_EXTERN
375 
376 #ifdef DPI_PROTOTYPES
377 #undef DPI_PROTOTYPES
378 #undef XXTERN
379 #undef EETERN
380 #endif
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif
uint32_t aval
Definition: svdpi.h:93
uint32_t bval
Definition: svdpi.h:94
XXTERN void svGetLogicArrElem2VecVal(svLogicVecVal *d, const svOpenArrayHandle s, int indx1, int indx2)
Definition: DummySvDpi.cpp:166
XXTERN svScope svGetScopeFromName(const char *scopeName)
Definition: DummySvDpi.cpp:249
XXTERN void svPutLogicArrElem3VecVal(const svOpenArrayHandle d, const svLogicVecVal *s, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:138
XXTERN const char * svGetNameFromScope(const svScope)
Definition: DummySvDpi.cpp:245
XXTERN int svSizeOfArray(const svOpenArrayHandle)
Definition: DummySvDpi.cpp:89
uint8_t svScalar
Definition: svdpi.h:82
XXTERN int svIncrement(const svOpenArrayHandle h, int d)
Definition: DummySvDpi.cpp:73
XXTERN int svLeft(const svOpenArrayHandle h, int d)
Definition: DummySvDpi.cpp:57
XXTERN void svPutLogicArrElem2VecVal(const svOpenArrayHandle d, const svLogicVecVal *s, int indx1, int indx2)
Definition: DummySvDpi.cpp:134
XXTERN void svPutPartselBit(svBitVecVal *d, const svBitVecVal s, int i, int w)
Definition: DummySvDpi.cpp:51
XXTERN void * svGetArrElemPtr1(const svOpenArrayHandle, int indx1)
Definition: DummySvDpi.cpp:97
XXTERN int svDimensions(const svOpenArrayHandle h)
Definition: DummySvDpi.cpp:81
XXTERN void svGetBitArrElemVecVal(svBitVecVal *d, const svOpenArrayHandle s, int indx1,...)
Definition: DummySvDpi.cpp:142
svScalar svLogic
Definition: svdpi.h:84
XXTERN const char * svDpiVersion(void)
Definition: DummySvDpi.cpp:27
XXTERN void * svGetUserData(const svScope scope, void *userKey)
Definition: DummySvDpi.cpp:257
XXTERN void svGetPartselLogic(svLogicVecVal *d, const svLogicVecVal *s, int i, int w)
Definition: DummySvDpi.cpp:48
XXTERN svBit svGetBitselBit(const svBitVecVal *s, int i)
Definition: DummySvDpi.cpp:31
XXTERN svBit svGetBitArrElem(const svOpenArrayHandle s, int indx1,...)
Definition: DummySvDpi.cpp:174
XXTERN svLogic svGetLogicArrElem3(const svOpenArrayHandle s, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:203
XXTERN void svGetLogicArrElemVecVal(svLogicVecVal *d, const svOpenArrayHandle s, int indx1,...)
Definition: DummySvDpi.cpp:158
XXTERN void svGetBitArrElem2VecVal(svBitVecVal *d, const svOpenArrayHandle s, int indx1, int indx2)
Definition: DummySvDpi.cpp:150
struct TVpiVecval s_vpi_vecval
XXTERN void svPutBitArrElem2(const svOpenArrayHandle d, svBit value, int indx1, int indx2)
Definition: DummySvDpi.cpp:229
XXTERN void svGetBitArrElem1VecVal(svBitVecVal *d, const svOpenArrayHandle s, int indx1)
Definition: DummySvDpi.cpp:146
XXTERN int svIsDisabledState(void)
Definition: DummySvDpi.cpp:265
XXTERN void svPutBitselLogic(svLogicVecVal *d, int i, svLogic s)
Definition: DummySvDpi.cpp:42
XXTERN void svGetBitArrElem3VecVal(svBitVecVal *d, const svOpenArrayHandle s, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:154
XXTERN int svSize(const svOpenArrayHandle h, int d)
Definition: DummySvDpi.cpp:77
XXTERN int svPutUserData(const svScope scope, void *userKey, void *userData)
Definition: DummySvDpi.cpp:253
XXTERN svBit svGetBitArrElem3(const svOpenArrayHandle s, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:186
XXTERN void svPutLogicArrElem3(const svOpenArrayHandle d, svLogic value, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:219
XXTERN void svPutBitArrElem3(const svOpenArrayHandle d, svBit value, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:233
svScalar svBit
Definition: svdpi.h:83
XXTERN void svGetLogicArrElem1VecVal(svLogicVecVal *d, const svOpenArrayHandle s, int indx1)
Definition: DummySvDpi.cpp:162
XXTERN void svPutBitArrElem(const svOpenArrayHandle d, svBit value, int indx1,...)
Definition: DummySvDpi.cpp:223
XXTERN svLogic svGetLogicArrElem1(const svOpenArrayHandle s, int indx1)
Definition: DummySvDpi.cpp:195
XXTERN void svGetLogicArrElem3VecVal(svLogicVecVal *d, const svOpenArrayHandle s, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:170
XXTERN typedef void * svScope
Definition: svdpi.h:130
s_vpi_vecval svLogicVecVal
Definition: svdpi.h:99
XXTERN void svPutLogicArrElem2(const svOpenArrayHandle d, svLogic value, int indx1, int indx2)
Definition: DummySvDpi.cpp:215
XXTERN void * svGetArrayPtr(const svOpenArrayHandle)
Definition: DummySvDpi.cpp:85
XXTERN void svPutLogicArrElem(const svOpenArrayHandle d, svLogic value, int indx1,...)
Definition: DummySvDpi.cpp:208
XXTERN void * svGetArrElemPtr(const svOpenArrayHandle, int indx1,...)
Definition: DummySvDpi.cpp:93
XXTERN svLogic svGetLogicArrElem(const svOpenArrayHandle s, int indx1,...)
Definition: DummySvDpi.cpp:191
XXTERN svLogic svGetBitselLogic(const svLogicVecVal *s, int i)
Definition: DummySvDpi.cpp:35
XXTERN void * svGetArrElemPtr3(const svOpenArrayHandle, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:105
XXTERN int svHigh(const svOpenArrayHandle h, int d)
Definition: DummySvDpi.cpp:69
XXTERN void svPutBitArrElemVecVal(const svOpenArrayHandle d, const svBitVecVal *s, int indx1,...)
Definition: DummySvDpi.cpp:110
XXTERN void svGetPartselBit(svBitVecVal *d, const svBitVecVal *s, int i, int w)
Definition: DummySvDpi.cpp:45
XXTERN void svPutBitArrElem1(const svOpenArrayHandle d, svBit value, int indx1)
Definition: DummySvDpi.cpp:226
XXTERN svScope svGetScope(void)
Definition: DummySvDpi.cpp:237
XXTERN void * svGetArrElemPtr2(const svOpenArrayHandle, int indx1, int indx2)
Definition: DummySvDpi.cpp:101
XXTERN int svRight(const svOpenArrayHandle h, int d)
Definition: DummySvDpi.cpp:61
XXTERN void svPutBitArrElem2VecVal(const svOpenArrayHandle d, const svBitVecVal *s, int indx1, int indx2)
Definition: DummySvDpi.cpp:118
XXTERN void svPutLogicArrElemVecVal(const svOpenArrayHandle d, const svLogicVecVal *s, int indx1,...)
Definition: DummySvDpi.cpp:126
#define XXTERN
Definition: svdpi.h:70
XXTERN svScope svSetScope(const svScope scope)
Definition: DummySvDpi.cpp:241
XXTERN void svAckDisabledState(void)
Definition: DummySvDpi.cpp:269
XXTERN int svLow(const svOpenArrayHandle h, int d)
Definition: DummySvDpi.cpp:65
XXTERN int svGetCallerInfo(const char **fileName, int *lineNumber)
Definition: DummySvDpi.cpp:261
XXTERN void svPutBitArrElem3VecVal(const svOpenArrayHandle d, const svBitVecVal *s, int indx1, int indx2, int indx3)
Definition: DummySvDpi.cpp:122
XXTERN typedef void * svOpenArrayHandle
Definition: svdpi.h:133
uint32_t svBitVecVal
Definition: svdpi.h:102
XXTERN svLogic svGetLogicArrElem2(const svOpenArrayHandle s, int indx1, int indx2)
Definition: DummySvDpi.cpp:199
XXTERN void svPutLogicArrElem1(const svOpenArrayHandle d, svLogic value, int indx1)
Definition: DummySvDpi.cpp:212
XXTERN svBit svGetBitArrElem1(const svOpenArrayHandle s, int indx1)
Definition: DummySvDpi.cpp:178
XXTERN void svPutLogicArrElem1VecVal(const svOpenArrayHandle d, const svLogicVecVal *s, int indx1)
Definition: DummySvDpi.cpp:130
XXTERN void svPutBitArrElem1VecVal(const svOpenArrayHandle d, const svBitVecVal *s, int indx1)
Definition: DummySvDpi.cpp:114
struct TVpiVecval * p_vpi_vecval
XXTERN void svPutPartselLogic(svLogicVecVal *d, const svLogicVecVal s, int i, int w)
Definition: DummySvDpi.cpp:54
XXTERN svBit svGetBitArrElem2(const svOpenArrayHandle s, int indx1, int indx2)
Definition: DummySvDpi.cpp:182
XXTERN void svPutBitselBit(svBitVecVal *d, int i, svBit s)
Definition: DummySvDpi.cpp:39