SOGO論壇
  登入   註冊   找回密碼
查看: 682|回覆: 0
列印 上一主題 下一主題

[技術文章] 用 Iverilog-vpi 連結 C 語言與 Verilog [複製連結]

Rank: 11Rank: 11Rank: 11Rank: 11

熱心參予論壇活動及用心回覆主題勳章 數位硬體勳章

狀態︰ 離線
跳轉到指定樓層
1
發表於 2012-7-15 10:37:48 |只看該作者 |倒序瀏覽
# 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
喜歡嗎?分享這篇文章給親朋好友︰
               感謝作者     

請注意︰利用多帳號發表自問自答的業配文置入性行銷廣告者,將直接禁訪或刪除帳號及全部文章!
您需要登錄後才可以回覆 登入 | 註冊


本論壇為非營利自由討論平台,所有個人言論不代表本站立場。文章內容如有涉及侵權,請通知管理人員,將立即刪除相關文章資料。侵權申訴或移除要求:abuse@oursogo.com

GMT+8, 2025-2-6 17:05

© 2004-2025 SOGO論壇 OURSOGO.COM
回頂部