1

As I know, in systemverilog, there are no char type and string. SystemVerilog-specific types, including packed types (arrays, structures, unions), 2-state or 4-state, which have no natural correspondence in C.

if we use the char or string in c, How to handle SystemVerilog-specific types in c and vice-versa?

sogood
  • 21
  • 3

2 Answers2

2

My recommendation for "DPI-C" users is that unless you need access to 4-state values, keep your arguments C-compatible. That means using only ints, bytes(char), unpacked array/structs of ints/bytes, or strings(char *) across the language boundary.

Modelsim/Questa has a switch -dpiheader filename you should use when compiling your code that generates the proper C argument types for DPI-C routines. Your C code should `include this file to make sure your C code matches the required prototypes. That way you get a compiler error if they don't, instead of accessing garbage or corrupting data.

dave_59
  • 39,096
  • 3
  • 24
  • 63
1

An SV string input (to C) is a const char *. An SV string output or inout (from C) is a const char **.

As you might imagine, 4-state types are more complicated:

              SV TYPE - C TYPE
                logic - svLogic
packed array of logic - svLogicVecVal *
       unpacked array - svOpenArrayHandle

svLogic, svLogicVecVal and svOpenArrayHandle are types in svdpi.h. svLogic is typedef of an unsigned char, which can take the values sv_0, sv_1, sv_x, and sv_z, which are macros defined in svdpi.h. svLogicVecVal is a struct with two fields: aval and bval. aval and bval are 32-bit ints (canonical form). (In IUS, aval and bval are called a and b.)

Passing unpacked arrays using svOpenArrayHandle is much more complicated.

I don't know how to pass structs or unions or even if it is possible. (Though to pass such a thing to C I guess you could do a bit-stream cast, but that is going to be messy).

Matthew Taylor
  • 13,365
  • 3
  • 17
  • 44