DPI简介
SystemVerilog引入了直接编程接口(DPI,Direct Programming Interface),它能更加简单地连接C、C++或其他非Verilog编程语言。
DPI经常被用来调用C代码读取激励、包含一个参考模型或扩展SV的功能。
关于DPI的更多内容请参考绿皮书第12章。
DPI例程
exapmle.c
#include "stdio.h"
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
uint64_t add(uint32_t a, uint32_t b, uint32_t c)
{
uint64_t p = a + b + c;
return p;
}
test.sv
//dpi example
//import C library function
import "DPI-C" function real sin(input real r);
//import user-defined function
import "DPI-C" function longint unsigned add(int unsigned a, int unsigned b, int unsigned c);
module test;
const real PI = 3.1415926;
initial
begin
for (int i=0; i<8; i++)
$display ("sin(%.3f*PI) = %f", real'(i)/4, sin(i*PI/4));
for (int i=0; i<8; i++)
$display ("add(%0d, %0d, %0d) = %0d", i ,2 ,3, add(i, 2, 3));
end
endmodule
运行结果
先使用gcc命令将.c文件编译为.o文件,再使用vcs调用.o文件,可以避免使用UVM时调用报错。
gcc -c -m32 example.c
vcs -R -sverilog test.sv example.o
运行结果:
sin(0.000*PI) = 0.000000
sin(0.250*PI) = 0.707107
sin(0.500*PI) = 1.000000
sin(0.750*PI) = 0.707107
sin(1.000*PI) = 0.000000
sin(1.250*PI) = -0.707107
sin(1.500*PI) = -1.000000
sin(1.750*PI) = -0.707107
add(0, 2, 3) = 5
add(1, 2, 3) = 6
add(2, 2, 3) = 7
add(3, 2, 3) = 8
add(4, 2, 3) = 9
add(5, 2, 3) = 10
add(6, 2, 3) = 11
add(7, 2, 3) = 12