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

SomethingSomething picture SomethingSomething · Nov 11, 2014 · Viewed 10k times · Source

I have the following files:

C file with functions:

// funcs.c

#include <stdio.h>

void something() {
    printf("something\n");
    sayHello();
}

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;
    endfunction
endmodule

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

Answer

SomethingSomething picture SomethingSomething · Nov 11, 2014

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:

something
hello world

.