日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深入浅出FPGA-18-VPI

發布時間:2024/1/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深入浅出FPGA-18-VPI 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引言

我們在進行RTL仿真時,有時候會遇到HDL工程和C語言工程需要進行數據通信時,使用$readmem()等系統任務會方便很多,但是有時候,實現較復雜功能時,$readmem()就會稍顯不足。這時,就需要我們編寫特殊的系統任務,來實現。

HDL語言提供的PLI,VPI正是為了解決這個問題而設計的,本小節,我們就熟悉一下VPI。


1,VPI簡介

Verilog過程接口(Verilog Procedural Interface, VPI),最初被稱為編程語言接口(Program Language Interface, PLI) 2.0,是一個針對C語言的Verilog過程接口。它可以使數字電路的行為級描述代碼直接調用C語言的函數,而用到的C語言函數也可以調用標準的Verilog系統任務。Verilog程序結構是IEEE 1364編程語言接口標準的一部分。它最新的版本是2005年更新的。

更多信息請參考:

http://en.wikipedia.org/wiki/Verilog_Procedural_Interface

http://www.asic-world.com/verilog/pli6.html#Verilog_Procedural_Interface_(VPI)


2,簡單示例

在了解了VPI的含義以及工作原理之后,我們通過下面一個簡單的例子來說明VPI的具體使用方法。

針對不同的仿真工具(VCS,modelsim,NC sim),使用VPI有不同的方式。

本實驗以modelsim為例。


a,編寫C語言實現

hello.c


/* * vpi simple test * rill,2014-03-21 */#include "vpi_user.h"static PLI_INT32 hello(PLI_BYTE8 * param) {vpi_printf("Hello Rill!\n");return 0; }// Associate C Function with a New System Task void registerHelloSystfs(void) {s_vpi_systf_data task_data_s;vpiHandle systf_handle;task_data_s.type = vpiSysTask;task_data_s.sysfunctype = vpiSysTask;task_data_s.tfname = "$hello";task_data_s.calltf = hello;task_data_s.compiletf = 0;task_data_s.sizetf = 0;task_data_s.user_data = 0;systf_handle = vpi_register_systf(&task_data_s);vpi_free_object(systf_handle); }// Register the new system task here void (*vlog_startup_routines[]) () = {registerHelloSystfs,0 // last entry must be 0 };

b,編寫verilog HDL文件

hello.v:


module hello; initial $hello; endmodule

c,測試

為了測試方便,我編寫了一個簡單的shell腳本,如下所示:

hello.sh:

#!/bin/bash # Rill creat 140321gcc -c -I/home/openrisc/modelsim/modeltech/include hello.c ld -G -o hello.sl hello.o vlib work vlog hello.v vsim -c -pli hello.sl hello #run -all #quit

d,測試結果

下面是在linux下的測試結果:



3,實際應用實例

通過上面的例子,我們看到了verilog和C語言的交互過程。

其實,VPI的本質就是為了方便C語言和verilog之間交換數據。下面是一個實際工程中部分關鍵代碼,請參考:

vpi_demo.c:


// VPI includes #include <vpi_user.h>uint32_t vpi_pipe[2]; // [0] - read, [1] - writevoid check_for_command(); void get_command_data(); void return_command_data();//========================================= void init_pipe(void) {if(pipe(vpi_pipe) == -1){perror("pipe error\n");exit(1);} }//========================================= void check_for_command(char *userdata){vpiHandle systfref, args_iter, argh;struct t_vpi_value argval;int value,i;int n;unsigned char data;//if(DBG_JP_VPI) printf("check_for_command\n");//n = read(rsp_to_vpi_pipe[0], &data, 1);n = read(vpi_pipe[0], &data, 1);if ( ((n < 0) && (errno == EAGAIN)) || (n==0) ){// Nothing in the fifo this time, let's returnreturn;}else if (n < 0){// some sort of errorperror("check_for_command");exit(1);}if (DBG_JP_VPI){printf("jp_vpi: c = %x:",data);print_command_string(data);fflush(stdout);}// Return the command to the sim// Obtain a handle to the argument listsystfref = vpi_handle(vpiSysTfCall, NULL);// Now call iterate with the vpiArgument parameterargs_iter = vpi_iterate(vpiArgument, systfref); // get a handle on the variable passed to the functionargh = vpi_scan(args_iter);// now store the command value back in the simargval.format = vpiIntVal;// Now set the command valuevpi_get_value(argh, &argval);argval.value.integer = (uint32_t) data;// And vpi_put_value() it back into the simvpi_put_value(argh, &argval, NULL, vpiNoDelay);// Cleanup and returnvpi_free_object(args_iter);n = write(vpi_to_rsp_pipe[1],&data,1);if (DBG_JP_VPI) printf("jp_vpi: r");if (DBG_JP_VPI) printf("\n");return; }void get_command_data(char *userdata){vpiHandle systfref, args_iter, argh;struct t_vpi_value argval;int value,i;int n = 0;uint32_t data;char* recv_buf;recv_buf = (char *) &data; // cast data as our receive char bufferread_command_data_again: n = read(vpi_pipe[0],recv_buf,4);if ((n < 4) && errno==EAGAIN)goto read_command_data_again;else if (n < 4){printf("jp_vpi: get_command_data errno: %d\n",errno);perror("jp_vpi: get_command_data read failed");}if (DBG_JP_VPI) printf("jp_vpi: get_command_data = 0x%.8x\n",data);// Obtain a handle to the argument listsystfref = vpi_handle(vpiSysTfCall, NULL);// Now call iterate with the vpiArgument parameterargs_iter = vpi_iterate(vpiArgument, systfref); // get a handle on the variable passed to the functionargh = vpi_scan(args_iter);// now store the command value back in the simargval.format = vpiIntVal;// Now set the data valuevpi_get_value(argh, &argval);argval.value.integer = (uint32_t) data;// And vpi_put_value() it back into the simvpi_put_value(argh, &argval, NULL, vpiNoDelay);// Cleanup and returnvpi_free_object(args_iter);return;}void return_command_data(char *userdata){vpiHandle systfref, args_iter, argh;struct t_vpi_value argval;int value,i;int n, length;uint32_t data;char* send_buf;// Obtain a handle to the argument listsystfref = vpi_handle(vpiSysTfCall, NULL);// Now call iterate with the vpiArgument parameterargs_iter = vpi_iterate(vpiArgument, systfref); // get a handle on the length variableargh = vpi_scan(args_iter);argval.format = vpiIntVal;// get the value for the length objectvpi_get_value(argh, &argval);// now set lengthlength = argval.value.integer;// get a handle on the object passed to the functionargh = vpi_scan(args_iter);// now store the command value back in the simargval.format = vpiIntVal;// Now set the data valuevpi_get_value(argh, &argval);data = (uint32_t) argval.value.integer;// Cleanup and returnvpi_free_object(args_iter);if (DBG_JP_VPI) printf("jp_vpi: return_command_data %d bytes, 0x%.8x\n",length,data);send_buf = (char *) &data; //cast our long as a char buf// write the data backn = write(vpi_pipe[1],send_buf,length);return;}//=========================================void register_check_for_command() {s_vpi_systf_data data = {vpiSysTask, 0, "$check_for_command", (void *)check_for_command, 0, 0, 0};vpi_register_systf(&data);return; }void register_get_command_data() {s_vpi_systf_data data = {vpiSysTask, 0, "$get_command_data", (void *)get_command_data, 0, 0, 0};vpi_register_systf(&data);return; }void register_return_command_data() {s_vpi_systf_data data = {vpiSysTask, 0, "$return_command_data", (void *)return_command_data, 0, 0, 0};vpi_register_systf(&data);return; }//========================================= void (*vlog_startup_routines[]) () = {register_check_for_command,register_get_command_data,register_return_command_data,0 // last entry must be 0 };

vpi_demo.v:


integer cmd;reg [31:0] cmd_data; reg [31:0] exec_data;task exec_cmd; input [31:0] cmd_data; beginexec_data <= cmd_data + 1;//verilog process code endtask main;beginwhile (1)begincmd = -1;while (cmd == -1)begin#1000 $check_for_command(cmd);case (cmd)`TEST_CMD0:begin$get_command_data(cmd_data);exec_cmd(cmd_data);$return_command_data(4,exec_data);endendendend





總結

以上是生活随笔為你收集整理的深入浅出FPGA-18-VPI的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。