how to use verilog PLI communicate with c by ncver

2019-09-06 19:38发布

问题:

I would like to communicate c with verilog. I find the Verilog PLI can solve my problem. I read this website to learn http://www.asic-world.com/verilog/pli1.html#How_it_Works . But I still can't work even a printf function. I use ncverilog for verilog compiler. What I done is blow. I can't have a successful compile for this. It says that it can't find the function. Can anyone tell me how to solve my problem. thanks =)

C code: hello.c

#include<stdio.h>
void hello(){
   printf("HELLO");
}

make library:

gcc hello.c -fPIC -shared -o hello.so

verilog code: test.v

module test();
   initial begin
      $hello;
      #10 $finish;
   end
endmodule

verilog command:

ncverilog test.v +access+r -v hello.so

回答1:

VPI (PLI 2.0)

hello_vpi.c :

#include<stdio.h>
#include <vpi_user.h>
void hello(){
   printf("HELLO");
}

void register_hello()
{
    s_vpi_systf_data data;
    data.type      = vpiSysTask; //vpiSysFunc;
//  data.sysfunctype = vpiSysFuncInt; // return type if type is Func
    data.tfname    = "$hello";
    data.calltf    = hello;
    data.compiletf = 0;
    data.sizetf    = 0;
    data.user_data = 0;
    vpi_register_systf(&data);
}

Commands:

gcc hello_vpi.c -fPIC -shared -o hello_vpi.so
ncverilog test.v +access+r -loadvpi ./hello_vpi.so:register_hello

PLI 1.0 requires all the C methods use in verilog to be defined in s_tfcell veriusertfs[]. Ncverilog requires an additional bootstrap method that returns a type p_tf_cell, this method defines a static s_tfcell veriusertfs[]. Here is an example:

veriuser_nc.c :

#include "veriuser.h"
#include "acc_user.h"

extern void hello();

#define user_task          1
#define user_function      2

p_tfcell my_boot() {
  s_tfcell veriusertfs[] = {
    /* {user_function/usertask, (int)data, pli_checkp, pli_sizep, pli, ?, verilog_name, ? } */
    {usertask, 0, 0, 0, hello, 0, "$hello", 1},
    {0}  // last entry must be 0 
  };
  return(veriusertfs);
}

Commands:

gcc hello.c veriuser_nc.c -fPIC -shared -o hello_pli.so
ncverilog test.v +access+r -loadpli1 ./hello_pli.so:my_boot

VCS requires a tab file instead of an bootstrap method. This is lightly covered here: http://www.asic-world.com/verilog/pli2.html#Linking_With_Simulator


Another approach is to use SystemVerilog DPI which does not have a wrapper/translation layer in the same sense as PLI/VPI.

Add #include "svdpi.h" to the head of hello.c. I'm just changing the shared objects name to identify the library type (not required). gcc hello.c -fPIC -shared -o hello_dpi.so

verilog code: test.sv

module test();
   import "DPI-C" pure function void hello();
   // DPI methods are treated as verilog task/function after import, including scope access
   initial begin
      hello(); // dpi doesn't have a $
      #10 $finish;
   end
endmodule

verilog command:

ncverilog -sv test.sv +access+r -sv_lib hello_dpi.so

Documentation on DPI is in the IEEE std 1800-2012 § 35. Direct programming interface. PLI/VPI is covered in § 36 but does not cover simulator specif requirements. For more description and tutorials about DPI, checkout http://en.wikipedia.org/wiki/SystemVerilog_DPI and http://www.doulos.com/knowhow/sysverilog/tutorial/dpi/