在系统verilog代码中集成fftw C函数调用

我在linux系统上成功安装了fftw C库。 以下是有关fftw c => http://www.fftw.org/的更多信息。我有一个示例C代码,可以成功调用fftw C函数。 下面是一个C ccode和命令来运行C代码:代码:

#include  #include  #include  #include  #include  int main(void) { double FFT_in[] = {0.1, 0.6, 0.1, 0.4, 0.5, 0, 0.8, 0.7, 0.8, 0.6, 0.1,0}; double *IFFT_out; int i,size = 12; fftw_complex *middle; fftw_plan fft; fftw_plan ifft; middle = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*size); IFFT_out = (double *) malloc(size*sizeof(double)); fft = fftw_plan_dft_r2c_1d(size, FFT_in, middle, FFTW_ESTIMATE); //Setup fftw plan for fft (real 1D data) ifft = fftw_plan_dft_c2r_1d(size, middle, IFFT_out, FFTW_ESTIMATE); //Setup fftw plan for ifft fftw_execute(fft); fftw_execute(ifft); printf("Input: \tFFT_coefficient[i][0] \tFFT_coefficient[i][1] \tRecovered Output:\n"); for(i=0;i<size;i++) printf("%f\t%f\t\t\t%f\t\t\t%f\n",(FFT_in[i]),middle[i][0],middle[i][1],IFFT_out[i]/size); fftw_destroy_plan(fft); fftw_destroy_plan(ifft); fftw_free(middle); free(IFFT_out); return 0; } 

我可以使用以下gcc命令成功运行此代码:gcc -g -Wall -I / home / usr / fftw / local / include -L / home / usr / fftw / local / lib fftw_test.c -lfftw3 -lm -o fftw_test

现在我想使用DPI方法在系统verilog中调用此函数。 在我展示我的问题之前,下面是一个示例DPI-C / systemverilog测试用例,我可以成功运行:C:代码

 #include  #include  #include "svdpi.h" int add(x,y) { int z; z=x+y; printf("This is from C:%d+%d=%d\n",x,y,z); return z; } 

使用DPI从系统verilog调用以上C函数:

 module top; import "DPI-C" function int add(int a, int b); int a,b,j; initial begin $display("Entering in SystemVerilog Initial Block\n"); #20 a=20;b=10; j = add(a,b); $display("This is from System Verilog:Value of J=%d",j); $display("Exiting from SystemVerilog Initial Block"); #5 $finish; end endmodule 

最后我可以用irun命令irun -f run.f成功运行它,其中run.f包含以下命令:

 # Compile the SystemVerilog files basicadd.sv -access +rwc # Generate a header file called _sv_export.h -dpiheader _sv_export.h # Delay compilation of testexport.c until after elaboration -cpost add.c -end # Redirect output of ncsc_run to a log file called ncsc_run.log -log_ncsc_run ncsc_run.log 

现在我的真正问题是当我尝试将fftw C与系统verilog链接时,我无法运行它。 下面是我的C代码,它与我发布的第一个C代码非常相似:C代码:

 #include  #include  #include  #include  #include  #include "svdpi.h" void fftw_test_DPI(double FFT_in[],int size) { double *IFFT_out; int i; fftw_complex *middle; fftw_plan fft; fftw_plan ifft; middle = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*size); IFFT_out = (double *) malloc(size*sizeof(double)); fft = fftw_plan_dft_r2c_1d(size, FFT_in, middle, FFTW_ESTIMATE); //Setup fftw plan for fft (real 1D data) ifft = fftw_plan_dft_c2r_1d(size, middle, IFFT_out, FFTW_ESTIMATE); //Setup fftw plan for ifft fftw_execute(fft); fftw_execute(ifft); printf("Input: \tFFT_coefficient[i][0] \tFFT_coefficient[i][1] \tRecovered Output:\n"); for(i=0;i<size;i++) printf("%f\t%f\t\t\t%f\t\t\t%f\n",FFT_in[i],middle[i][0],middle[i][1],IFFT_out[i]/size); fftw_destroy_plan(fft); fftw_destroy_plan(ifft); fftw_free(middle); free(IFFT_out); //return IFFT_out; } 

这是我的系统verilog,我调用C函数:

 module top; import "DPI-C" function void fftw_test_DPI(real FFT_in[0:11], int size); real j [0:11]; integer i,size; real FFT_in [0:11]; initial begin size = 12; FFT_in[0] = 0.1; FFT_in[1] = 0.6; FFT_in[2] = 0.1; FFT_in[3] = 0.4; FFT_in[4] = 0.5; FFT_in[5] = 0.0; FFT_in[6] = 0.8; FFT_in[7] = 0.7; FFT_in[8] = 0.8; FFT_in[9] = 0.6; FFT_in[10] = 0.1; FFT_in[11] = 0.0; $display("Entering in SystemVerilog Initial Block\n"); #20 fftw_test_DPI(FFT_in,size); $display("Printing recovered output from system verilog\n"); //for(i=0;i<size;i++) //$display("%f\t\n",(j[i])/size); $display("Exiting from SystemVerilog Initial Block"); #5 $finish; end endmodule 

最后我尝试了几种方法,我会提到我试过的几种方法:第一种方法:

 irun -f run_fftw.f where run_fftw.f contains: # Compile the SystemVerilog files fftw_test.sv -access +rwc # Generate a header file called _sv_export.h -dpiheader _sv_export.h # Delay compilation of fftw_test.c until after elaboration -cpost fftw_test_DPI.c -end -I/home/usr/fftw/local/include -L/home/usr/fftw/local/lib fftw_test_DPI.c -lfftw3 -lm # Redirect output of ncsc_run to a log file called ncsc_run.log -log_ncsc_run ncsc_run.log 

但这导致以下错误:构建库run.so ld:/home/usr/fftw/local/lib/libfftw3.a(mapflags.o):在创建共享对象时,不能使用针对`.rodata’的重定位R_X86_64_32 ; 用-fPIC /home/usr/fftw/local/lib/libfftw3.a重新编译:无法读取符号:错误值collect2:ld返回1退出状态make: * [/home/usr/DPI/./INCA_libs/irun。 lnx8664.12.20.nc/librun.so]错误1 ncsc_run:* E,TBBLDF:无法构建测试库/home/usr/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so

第二种方法:1.gcc -fPIC -O2 -c -g -I / home / ss69 / fftw / local / include -I。 -L / home / usr / fftw / local / lib -L。 -o fftw_test_DPI.o fftw_test_DPI.c -lfftw3 -lm

2.gcc -shared -o libdpi.so fftw_test_DPI.o

3.irun -64bit -sv_lib libdpi.so fftw_test.sv

这导致以下错误:加载快照worklib.top:sv ………………..完成ncsim>运行进入SystemVerilog初始块

ncsim:符号查找错误:./ libdpi.so:未定义符号:fftw_malloc

我知道我的post有点难以理解,但我非常感谢任何帮助。

这是我们之前讨论过的问题的一个很好的描述和总结。 我通常不会在* nix系统上工作,因此我无法提出任何具体细节。 我要指出的是,逐步解决这样的问题通常是一种很好的方法。

main()直接调用fft代码的第一步很好。 但是,我没有看到一个步骤(不使用SystemVerilog或ncsim)您将fft代码编译到库中,然后从main()调用该代码。

似乎将代码存储在单独的lib中并从main()调用是必要的步骤。 在你工作之后,你应该能够在SystemVerilog中包含fft lib,而无需同时编译fft例程吗?

您可能需要exportsetenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/home/usr/fftw/local/lib:. 。 除非您的libdpi.so与静态FFTW库链接,否则,您的代码将需要加载FFTW库的动态版本(libfftw.so?),因为您使用的是fftw_* API。