Can I use .o files of compiled c files and use that to compile SV files for DPI

I have created one QT based C application. It takes few inputs from GUI and based on that input I need to call SystemVerilog function.
I checked examples of DPI but for that I need to compile .c files with .sv file. So Can I use compiled .o files with .sv files to communicate each other?

The mechanism for compiling C code for use with the SystemVerilog DPI is going to be tool specific. But any tool should be able to provide the necessary switches to include your object on the command line, or use gcc independently to build a shared object that can be loaded into your simulator.

Regardless of how the DPI is built, SystemVerilog requires that any C code calling a SystemVerilog function must be from the same thread where SystemVerilog code called the C code first.

Basically, a SystemVerilog simulation appears as a single thread to the C code, and any interaction between the languages has to be within the same thread.

In reply to dave_59:

Thanks Dave,

So I have created shared object file .so using gcc. Now I need to call C function of shared object from SV file.

I have created import DPI-C of that function in .sv file

How to compile and link that .so file in Questasim?

vlib work
vlog -sv -l libshared_code.so tb_dpi.sv
vsim -c tb_dpi -do “run -all”

on compiling using above command I got error like
Failed to find user specified function ‘getRandom’ in DPI C/C++ source files.

In reply to Ankit_Patel:

//Created shared library using below command
gcc -shared -fPIC -Bsymbolic -o test.so test.c main.c

//Simulating shared library with Questasim
vlib work
vlog -sv tb_dpi.sv
vsim -sv_lib test -c tb_dpi -do “run -all”

//Created executable using below command
gcc -o test test.so

Now problem here is my GUI sets some variable in test.c file but my systemverilog code dosen’t gets the updated value.
I thing Questa dosen’t get’s updated from .so at runtime. So is there a way to run executable in Questasim so that sv code gets updated value or any other suggestions?

In reply to Ankit_Patel:

It’s going to be very difficult to help you in this forum without explaining the complete flow of code execution. The only way to update a value on the SV side is when a thread cross the language boundary - when C calls an exported SV task or function and the SV arguments get assigned to SV variables, or when SV calls an inported C task or function, and that routine returns to update the output arguments.

I’m assuming your GUI has multiple threads running, and what I said earlier still holds - the DPI can only interact with a single thread. What you are trying to do may be too difficult to discuss in this forum and I suggest you seek out expert advice that may be more local to you.

In reply to dave_59:

Hi Dave,

Thanks for your reply. I understood the concept and now able to do what I want.

But I faced few difficulties in calling SystemVerilog tasks from C functions to insert delay.

I am receiving Fatal error as below
Fatal: (vsim-3743) DPI task ‘import_task’ returned non-zero value indicating disable when thread was not disabled.

If I comment printf(" C: After calling export function\n"); of C_file.c then it works.
I am unable to execute the remaining lines of C function after calling SV task.

Below is the code that I am trying to run.

//C_file.c

extern int export_task(int *t);
int import_task(int *t)
{ 
  printf("C : Before calling export\n");
  export_task(t);
  printf(" C: After calling export function\n"); 
}

//SV_file.sv

program main; 
export "DPI-C" task export_task; 
import "DPI-C" context task import_task(output int t); 

int a;
task export_task(output int t); 
  $display("SV: Entered the export function . wait for some time : %0d ",$time); 
  #100 t = $stime;
  $display("SV: After waiting %0d",$time);
endtask 

initial 
begin 
  $display("SV: Before calling import function %0d",$time); 
  import_task(a);
  $display("SV: After calling import function A=%0d Time=%0d",a,$time); 
end 
endprogram

//Run commands

vlib work
vlog SV_file.sv C_file.c
vsim -c main -do "run -all; exit"

In reply to Ankit_Patel:

Your export_task() is supposed to return an int, but you did not return anything (which means a garbage value got returned). See section 35.9 Disabling DPI tasks and functions of the 1800-2012 LRM.

Also please use the vlog -dpiheader switch and include that file in your C file. You will thank me later.

In reply to dave_59:

Thanks Dave, Now it works…

//C_file.c
int import_task(int *t)
{
printf(“C : Before calling export\n”);
if(export_task(t)) {
svAckDisabledState();
printf(“C : After calling export Return 1\n”);
return 1;
}
printf(" C: After calling export Return 0\n");
return 0;
}

Is that mandatory to call SV task from imported C function only?

In reply to Ankit_Patel:

The LRM requires that an SV exported task only be called from a C imported task, and an SV exported function can only be called from a C imported task or function

In reply to dave_59:

Thanks Dave,

I found that I can’t delay the execution of C code which requires outside simulator inputs.
i.e.
//SV_file.c

program main; 

import "DPI-C" context function void import_task();

initial 
begin 
  $display("SV: Print 1 %0t",$time); 
  #10000;
  $display("SV: Print 2 %0t",$time); 
  for(int i=0; i<3; i++) begin
    import_task();
  end
  $display("SV: Print 3 %0t",$time); 
end 

endprogram

//C_file

#include <stdio.h>
#include "cimports.h"

void import_task()
{ 
  int a;
  printf("Please input an integer value: ");
  scanf("%d", &a);
  printf("\nYou entered: %d\n", a);
}

//Simulation OUTPUT

run -all

1
2
3

SV: Print 1 0

SV: Print 2 10000

Please input an integer value:

You entered: 1

Please input an integer value:

You entered: 2

Please input an integer value:

You entered: 3

SV: Print 3 10000

Here I have two display statements before calling C function though I have to enter 3 inputs before SV simulation begins.
No doubt I got the outputs of C function on simulation time. But I have to delay execution of C function after displaying two statements. Is is possible?

In reply to Ankit_Patel:

This is getting too tool specific to be able to help you here.