SystemVerilog包括DPI(直接编程接口),可让您的SystemVerilog调用C函数,甚至可以让您的C调用SystemVerilog任务/函数。查看IEEE标准1800-2009第35节和附录H & I.数据类型存在限制,因此请查看附录H.7.4中的基本SV/C类型映射。
要调用C函数SystemVerilog中,简单地将其导入到所期望的范围(例如,模块或封装)
import "DPI-C" context function C_function_name(/* args */);
要选自C调用的SystemVerilog需要一个额外的步骤。
在SV:
export "DPI-C" function SV_function_name; /*no args */
在C:
extern return_type SV_function_name(/* args */);
根据您的模拟器,你可能需要先编译C代码和参考的对象文件,或者只包含源文件你的文件列表。你需要添加选项到你的模拟器,所以检查手册。
这里有一些资源,可以帮助您开始:
修订: 使用翻译包装,因为文件不确实没有翻译整个新闻部。 C的const char*
映射到SystemVerilog的string
。
C:
#include <stdlib.h>
#include <stdio.h>
// include for DPI
#include "svdpi.h"
// wrapper
int C2SV_readmem(int z, const char *filename1, const char *filename2) {
FILE *file1;
FILE *file2;
int rtn;
file1 = fopen(filename1, "r");
file2 = fopen(filename2, "w");
if (file1 == NULL) {
printf("failed to open '%s' for read\n", filename1);
return 1;
}
if (file2 == NULL) {
printf("failed to open '%s' for write\n", filename2);
return 1;
}
return readmem(z, file1, file2); // call original readmem function
}
/* ... */
的SystemVerilog:
module test;
import "DPI-C" context function int C2SV_readmem(input int z, input string filename1, input string filename2);
int value;
initial begin
value = C2SV_readmem(25, "FileIn.txt", "FileOut.txt");
end
endmodule
我想你告诉他,他已经知道的东西负载。问题是关于文件描述符,而不是如何使用DPI。 –
你是对的保罗。我错过了阅读说明,并且对我的回答不够明确。我用一个工作示例修改了我的答案。 – Greg
不错;这几乎是我该怎么做的。它确保每一边都看着自己的文件描述符。很高兴你没有否认我的评论。 –