SOGO論壇

標題: 用 Iverilog-vpi 連結 C 語言與 Verilog [列印本頁]

作者: mm117777    時間: 2012-7-15 10:37:48     標題: 用 Iverilog-vpi 連結 C 語言與 Verilog

# include  <vpi_user.h>

static int hello_compiletf(char*user_data)
{
      return 0;
}

static int hello_calltf(char*user_data)
{
      vpi_printf("Hello, World!\n");
      return 0;
}

void hello_register()
{
      s_vpi_systf_data tf_data;

      tf_data.type      = vpiSysTask;
      tf_data.tfname    = "$hello";
      tf_data.calltf    = hello_calltf;
      tf_data.compiletf = hello_compiletf;
      tf_data.sizetf    = 0;
      tf_data.user_data = 0;
      vpi_register_systf(&tf_data);
}

void (*vlog_startup_routines[])() = {
    hello_register,
    0
};
檔案:hello.v
module main;
initial $hello;
endmodule
編譯執行過程
D:\ccc101\Verilog\c>iverilog-vpi -mingw=c:\\mingw -ivl=c:\\iverilog hello.c
info:  storing c:\\mingw in Windows' registry entry
       HKEY_LOCAL_MACHINE\Software\Icarus Verilog\MingwDir
info:  storing c:\\iverilog in Windows' registry entry
       HKEY_LOCAL_MACHINE\Software\Icarus Verilog\InstallDir
Compiling hello.c...
Making hello.vpi from  hello.o...

D:\ccc101\Verilog\c>iverilog hello.v -o hello.vvp

D:\ccc101\Verilog\c>vvp -M. -mhello hello.vvp
Hello, World!
原理解說
在 Using VPI — http://iverilog.wikia.com/wiki/Using_VPI 這篇文章中,記載了這個機製的運作原理,以下是我認為最重要的一段話:

The simulator run time (The "vvp" program) gets a handle on a freshly loaded module by looking for the symbol "vlog_startup_routines" in the loaded module. This table, provided by the module author and compiled into the module, is a null terminated table of function pointers. The simulator calls each of the functions in the table in order. The following simple C definition defines a sample table:

void (*vlog_startup_routines[])() = {
hello_register,
0
};

Note that the "vlog_startup_routines" table is an array of function pointers, with the last pointer a 0 to mark the end. The programmer can organize the module to include many startup functions in this table, if desired.

透過這種方式,我們就能讓 Icarus Verilog 的 vvp 虛擬機呼叫 C 語言的函數了。

參考文獻
1.Using VPI — http://iverilog.wikia.com/wiki/Using_VPI
2.(原創) 如何使用C開發Verilog System Task/Function? (SOC) (Verilog) (Verilog PLI)
http://www.cnblogs.com/oomusou/a ... og_hello_world.html





歡迎光臨 SOGO論壇 (https://oursogo.com/) Powered by OURSOGO.COM