Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to compile and simulate DPI C file with Questasim?

Status
Not open for further replies.

kansagaratushar

Newbie level 5
Joined
Jan 7, 2013
Messages
8
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,338
I don't know how to compile and simulate DPI C file with Questasim. So Can you please give compilation process for the same.
Thanks in advance..
 

You need to import the function in your sv code:
Code:
import "DPI" function int get_pid();
import "DPI" function int get_vmem( output string output_str_ptr );

Questasim will compile c files with vlog so you need to just add the files in your "do" file
 
I didn't get it. Can you please write down compilation process for questasim for following example:

............................CODE: SV_file .................................
program main;
string str;

import "DPI-C" string_sv2c=task string_sv2c();

initial
begin
string_sv2c();
end

endprogram


................................CODE: C_file .......................................
#include "svdpi.h"

void string_sv2c(){
printf(" C: Hellow from C ");
}
 
Last edited:

change
import "DPI-C" string_sv2c=task string_sv2c();
to
import "DPI-C" string_sv2c=function void string_sv2c();

(you don't have to rename the function and I think it's better to move the import declaration out of the program)

for files string_sv2c.sv & string_sv2c.c
run :
vlib work
vlog -sv string_sv2c.sv string_sv2c.c
vsim -c work.main -do "run -all"
 
A couple of things to mention for future reference:
  • Do not use the -sv switch. *.sv files are already treated as SystemVerilog and using -sv will make legacy Verilog *.v files compile as SystemVerilog. This may cause errors if SV keywords are used in Verilog files.
  • Use void functions instead of tasks for procedures that do not consume time(block), for both pure SV and DPI usage. Calling a function is a guarentee that the procedure will not consume time, and there is less overhead associated with that kind of call.
  • Use vlog -dpiheader filename.h when compiling SV DPI code. That generates a header file that you should #include "filename.h" in your DPI C code. This gives you compile time checking that your C procedures have the correct argument types. Otherwise you may get run-time fatal errors from bad memory references.
 
vlog -dpiheader dpi_header_file.h ../Verification/verificationcodes/dpi/dpi_test.sv ../Verification/verificationcodes/dpi/dpi_c.c
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling program main
#
# Top level modules:
# main

vsim -c main -do "run -all"
# vsim -do {run -all} -c main
# ** Note: (vsim-3813) Design is being optimized due to module recompilation...
# Loading sv_std.std
# Loading work.main(fast)
# ** Warning: (vsim-3770) Failed to find user specified function 'string_sv2c'. The search list was empty.
# Using -sv_lib, -sv_root, and -sv_liblist arguments can provide a search list
# of shared libraries that will be used to resolve user specified functions.
# Time: 0 ns Iteration: 0 Instance: /main File: ../Verification/verificationcodes/dpi/dpi_test.sv
# run -all
# ** Fatal: (vsim-160) ../Verification/verificationcodes/dpi/dpi_test.sv(5): Null foreign function pointer encountered when calling 'string_sv2c'
# Time: 0 ns Iteration: 0 Process: /main/#INITIAL#8 File: ../Verification/verificationcodes/dpi/dpi_test.sv
# Fatal error at ../Verification/verificationcodes/dpi/dpi_test.sv line 5
#
# HDL call sequence:
# Stopped at ../Verification/verificationcodes/dpi/dpi_test.sv 5
# called from ../Verification/verificationcodes/dpi/dpi_test.sv 10

Getting this errors........

***********C Code********
#include"svdpi.h"
#include"F:\prepare\Verification\dpi_header_file.h"
void string_sv2c()
{
printf(" C: Hellow from C ");
}


**********SV Code***********
program main;
string str;
import "DPI-C" string_sv2c=function void string_sv2c();


initial
begin
string_sv2c();
end

endprogram



pls hlp...
 
your above code is write
in c file what you write in 2nd line?
remove 2nd line and run it will work
 

The output from vlog should have been
Code:
$ vlog -dpiheader dpi_header_file.h dpi_test.c dpi_test.sv
QuestaSim vlog 10.2a Compiler 2013.03 Mar 15 2013
-- Compiling program main

Top level modules:
        main
[COLOR="#FF0000"]-- Compiling DPI C/C++ file dpi_test.c[/COLOR]
Try putting everything in the same directory so there are no differences between cygwin and windows paths. If that doesn't work, you may need to compile your C files directly with gcc.

Also, you can simplify your import statement when the name in SV and C are the same.

import "DPI-C" function void string_sv2c();
 

The output from vlog should have been
Code:
$ vlog -dpiheader dpi_header_file.h dpi_test.c dpi_test.sv
QuestaSim vlog 10.2a Compiler 2013.03 Mar 15 2013
-- Compiling program main

Top level modules:
        main
[COLOR="#FF0000"]-- Compiling DPI C/C++ file dpi_test.c[/COLOR]
Try putting everything in the same directory so there are no differences between cygwin and windows paths. If that doesn't work, you may need to compile your C files directly with gcc.

Also, you can simplify your import statement when the name in SV and C are the same.

import "DPI-C" function void string_sv2c();



as u said i just put every file in the same path

---------C - File -------------
#include"svdpi.h"
#include"dpi_header_file.h"
void string_sv2c()
{
printf(" C: Hellow from C ");
}

--------sv file-----

program main;
string str;
import "DPI-C" function void string_sv2c();


initial
begin
string_sv2c();
end

endprogram


vlib work
vlog -dpiheader dpi_header_file.h dpi_test.c dpi_test.sv
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling program main
#
# Top level modules:
# main


vsim -c work.main
# vsim -c work.main
# ** Note: (vsim-3812) Design is being optimized...
# Loading sv_std.std
# Loading work.main(fast)
# ** Warning: (vsim-3770) Failed to find user specified function 'string_sv2c'. The search list was empty.
# Using -sv_lib, -sv_root, and -sv_liblist arguments can provide a search list
# of shared libraries that will be used to resolve user specified functions.
# Time: 0 ns Iteration: 0 Instance: /main File: dpi_test.sv


and getting this error....

- - - Updated - - -

The output from vlog should have been
Code:
$ vlog -dpiheader dpi_header_file.h dpi_test.c dpi_test.sv
QuestaSim vlog 10.2a Compiler 2013.03 Mar 15 2013
-- Compiling program main

Top level modules:
        main
[COLOR="#FF0000"]-- Compiling DPI C/C++ file dpi_test.c[/COLOR]
Try putting everything in the same directory so there are no differences between cygwin and windows paths. If that doesn't work, you may need to compile your C files directly with gcc.

Also, you can simplify your import statement when the name in SV and C are the same.

import "DPI-C" function void string_sv2c();



as u said i just put every file in the same path

---------C - File -------------
#include"svdpi.h"
#include"dpi_header_file.h"
void string_sv2c()
{
printf(" C: Hellow from C ");
}

--------sv file-----

program main;
string str;
import "DPI-C" function void string_sv2c();


initial
begin
string_sv2c();
end

endprogram


vlib work
vlog -dpiheader dpi_header_file.h dpi_test.c dpi_test.sv
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling program main
#
# Top level modules:
# main


vsim -c work.main
# vsim -c work.main
# ** Note: (vsim-3812) Design is being optimized...
# Loading sv_std.std
# Loading work.main(fast)
# ** Warning: (vsim-3770) Failed to find user specified function 'string_sv2c'. The search list was empty.
# Using -sv_lib, -sv_root, and -sv_liblist arguments can provide a search list
# of shared libraries that will be used to resolve user specified functions.
# Time: 0 ns Iteration: 0 Instance: /main File: dpi_test.sv


and getting this error....
 

You need a slightly newer version of Modelsim/Questa to auto-compile C code from the vlog command line on Windows. Either update your version, or see "Registering DPI Applications" in the User Manual.
 

Were these errors solved?

I am getting the same error when I try to run a simple "Hello World" program.
I am using the Questa 10.0b version.
Tried to compile the C program using gcc and getting some compile errors.

---System Verilog program ---
// Named as hello.sv
module top;

// Declare the DPI import function
import "DPI-C" function void hello(string str);

initial begin
hello("Hello World") ;
end

endmodule
---End of SV program---------
---Start of C program -----------
//Named as hello.c
#include <stdio.h>

void hello(char* str)
{
printf ("%s\n", str) ;
}

--End of C program ----
// Compile Steps //

vlog hello.sv
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling module top
#
# Top level modules:
# top

gcc -shared -Bsymbolic -o hello.so hello.c
vsim -sv_lib hello top

# vsim -sv_lib hello top
# ** Error: (vsim-3763) SystemVerilog DPI cannot access file '.\hello.dll'
# No such file or directory. (errno = ENOENT)
# Time: 0 ns Iteration: 0 Unknown: <UNKNOWN>
# Use the -help option for complete vsim usage.
# Error loading design

I am confused whether the vsim is expection a .so file or not.
Any answers?

Thanks.
 

If you are running on windows, then change -o hello.so to -o hello.dll.

Also, we strongly recommend that you create a dpi header file.

Code:
 vlog hello.sv -dpiheader hello.h

Then #include "hello.h" inside your C file.
 

Thanks Dave. The simulation error disappeared, but the output is still missing.

vlog hello.sv
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling module top
#
# Top level modules:
# top
gcc -shared -Bsymbolic -o hello.dll hello.c
vsim -sv_lib hello top
# vsim -sv_lib hello top
# ** Note: (vsim-3813) Design is being optimized due to module recompilation...
# Loading sv_std.std
# Loading work.top(fast)
# Loading .\hello.dll
run -all

I tried with the dpiheader option also.
vlog hello.sv -dpiheader dpiheader.h
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling module top
#
# Top level modules:
# top

But the compilation of C file in the next step ended in error.

gcc -shared -Bsymbolic -o hello.dll hello.c
# hello.c:5:6: error: conflicting types for 'hello'
# In file included from hello.c:2:0:
# dpiheader.h:27:1: note: previous declaration of 'hello' was here

//DPI header contents are pasted below
--- dpiheader starts here -----------
#ifndef INCLUDED_DPIHEADER
#define INCLUDED_DPIHEADER

#ifdef __cplusplus
#define DPI_LINK_DECL extern "C"
#else
#define DPI_LINK_DECL
#endif

#include "svdpi.h"


DPI_LINK_DECL DPI_DLLESPEC
void
hello(
const char* str);

#endif
--- dpiheader ends here -----------

The C program seems to be working fine when I ran independently.
 

The output should have gone to stdout. If you want the output to go to the transcript or log files as well, you will need to use vpi_printf() instead of printf(). You will need to #include "vpi_user.h" to pick up the VPI routines.

The conflicting types for 'hello' message you got is exactly the kind of message using -dpiheader should be giving you. It means your C code did not match the expected prototype fro the function hello. The str argument should have been declare const char* str to prevent the C code from modifying the string. This is not a problem in your simple code example, but I hope you can see how a conflicting type could become a hard to debug run-time error hand you not caught it at compile time.
 

Thanks Dave.

Changed it to const char* in C program and the conflicting type error disappeared. Pretty good utility this header inclusion :)

Still I dint get the output on my transcript because of a gcc error. (I have not used verilog PLI/VPI. Started directly with DPI). I included the "vpi_user.h" and the "vpi_compatibility.h". Is there anything else I need to add to get the vpi_printf function? (Using Windows + Questa 10.0b).
---------Modified C program ---------------
#include "vpi_user.h"
#include "dpiheader.h"


void hello(const char* a)
{
vpi_printf("%s\n",a);
}
-----------End of C program ---------------



vlog -dpiheader dpiheader.h hello.sv
# QuestaSim vlog 10.0b Compiler 2011.05 May 5 2011
# -- Compiling module top
#
# Top level modules:
# top
gcc -shared -Bsymbolic -o hello.dll hello.c
# C:\Users\..\AppData\Local\Temp\ccyjsdv0.o:hello.c:(.text+0x15): undefined reference to `vpi_printf'
# collect2.exe: error: ld returned 1 exit status


I tried the qverilog option (after changing to const char*) and then got the output comfortably.
But still at a loss after trying to compare what qverilog did with what I should have done for it to work without using qverilog.

QuestaSim qverilog 10.0b Compiler 2011.05 May 5 2011
C:\questasim_10.0b\win32\qverilog.exe -reportprogress 300 -guimode hello.sv hello.c
-- Compiling module top

Top level modules:
top
+ C:\questasim_10.0b\win32/vsim -lib work top -c -do "run -all; quit -sim" -appendlog -l qverilog.log -vopt -sv_lib work/_dpi/qv_dpi
modules:
# top
# + C:\questasim_10.0b\win32/vsim -lib work top -c -do "run -all; quit -sim" -appendlog -l qverilog.log -vopt -sv_lib work/_dpi/qv_dpi
vsim -lib work top -c -do {run -all; quit -sim} -appendlog -l qverilog.log -vopt -sv_lib work/_dpi/qv_dpi
# vsim -appendlog -do {run -all; quit -sim} -l qverilog.log -lib work -c -vopt -sv_lib work/_dpi/qv_dpi top
# Loading sv_std.std
# Loading work.top(fast)
# Loading .\work/_dpi/qv_dpi.dll
# run -all
# Hello System Verilog
# quit -sim
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top