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

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

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

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

狀態︰ 離線
跳轉到指定樓層
1
發表於 2012-7-15 10:38:38 |只看該作者 |倒序瀏覽
# 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);
}

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

static int linkc_calltf(char*user_data)
{
      vpi_printf("Link C success!\n");
      return 0;
}

void linkc_register()
{
      s_vpi_systf_data tf_data;

      tf_data.type      = vpiSysTask;
      tf_data.tfname    = "$linkc";
      tf_data.calltf    = linkc_calltf;
      tf_data.compiletf = linkc_compiletf;
      tf_data.sizetf    = 0;
      tf_data.user_data = 0;
      vpi_register_systf(&tf_data);
}

void (*vlog_startup_routines[])() = {
    hello_register,
    linkc_register,
    0
};
檔案:hello2.v
module main;
initial $hello;
initial #100 $linkc;
initial #200 $linkc;
endmodule
編譯執行過程
D:\ccc101\Verilog\c>iverilog-vpi hello2.c
Compiling hello2.c...
Making hello2.vpi from  hello2.o...

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

D:\ccc101\Verilog\c>vvp -M. -mhello2 hello2.vvp
Hello, World!
Hello, World!
Hello, World!

D:\ccc101\Verilog\c>iverilog-vpi hello2.c
Compiling hello2.c...
Making hello2.vpi from  hello2.o...

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

D:\ccc101\Verilog\c>vvp -M. -mhello2 hello2.vvp
Hello, World!
Link C success!
Link C success!
原理解說
在 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.用 Iverilog-vpi 連結 C 語言與 Verilog
2.Using VPI — http://iverilog.wikia.com/wiki/Using_VPI
喜歡嗎?分享這篇文章給親朋好友︰
               感謝作者     

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


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

GMT+8, 2024-6-9 09:18

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