Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() {
    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?

like image 530
SomethingSomething Avatar asked Dec 20 '22 09:12

SomethingSomething


2 Answers

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

.

like image 128
SomethingSomething Avatar answered Dec 21 '22 23:12

SomethingSomething


A solution that works with all simulator that follow IEEE Std 1800-2012 is to have #include "svdpi.h" and prefix the extern keyword in front of all methods being exported to C. funcs.c should look like:

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

extern int sayHello();

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

Examples from IEEE Std 1800-2012

  • § H.10.2 Example 2—Simple packed array application
  • § H.10.3 Example 3—Application with complex mix of types
like image 22
Greg Avatar answered Dec 21 '22 22:12

Greg