SomethingSomething SomethingSomething - 10 months ago 85
C Question

SystemVerilog: How to connect C function using DPI call in VCS simulator?

I have the following files:

C file with functions:

// funcs.c

#include <stdio.h>

void something() {

System verilog file:

// hello_world.v

module kuku;
export "DPI-C" function sayHello;
import "DPI-C" function void something();
initial something();
function int sayHello ();
$display("hello world");
sayHello = 1;

How can I compile it and make this work so when I call
from SV, it will call the C function, and when I call
from C, it will call the SV function?

Answer Source

Answering myself:

When SV code is compiled using VCS, it is first translated into C code.

When exporting a function out of SV, it generates a C header file vc_hdrs.h that should be included by the C file.

So a change I made in the C file is to add the line:

#include "vc_hdrs.h"

Then, I just added the C functions file to the VCS compilation command:

> vcs -sverilog hello_world.v funcs.c

It works!

The output I get is:

hello world