2013-02-06 145 views
0

我正在尝试将C函数导入System verilog测试工作台。 C函数的代码如下所示。我想传递文件作为参数。该功能基本上从一个文件读取并写入另一个文件。使用文件类型参数导入System Verilog中的C函数

int readmem(int z, FILE *file1, FILE *file2) { 
     char data; 
     int x; 
     int i; 
     for(i = 0; i<z;i ++) { 
     data = fgetc(file1); 
     x = data; 
     fputc(x,file2); 
     } 
     return 0; 
    } 

请告诉我如何在System verilog测试平台中调用此函数。

回答

1

您无法通过DPI在SystemVerilog和C之间传递文件描述符,所以我不认为可以直接按原样导入函数。

如果您真的需要做的是获得SystemVerilog中的功能,将它移植到SystemVerilog会更容易,而不是试图通过DPI导入它。

像这样的东西应该工作(没有测试!):

function int readmem(int z, int file1, int file2); 
    reg[8:0] data; 
    for (int i = 0; i < z; i++) begin 
    data = $fgetc(file1); // Really should break out of the loop if data == EOF ('h1FF) 
    $fwrite(file2, "%c", data[7:0]); 
    end 
    return 0; 
endfunction 

然后从别的地方:

int file1 = $fopen("input_file", "r"); 
int file2 = $fopen("output_file", "w"); 

readmem(10, file1, file2) 

原因data声明为9位是捕捉如果最终的EOF的文件已达到。由于您未检查EOF,因此原始功能可能会在file1的末尾运行。

0

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 
+0

我想你告诉他,他已经知道的东西负载。问题是关于文件描述符,而不是如何使用DPI。 –

+0

你是对的保罗。我错过了阅读说明,并且对我的回答不够明确。我用一个工作示例修改了我的答案。 – Greg

+0

不错;这几乎是我该怎么做的。它确保每一边都看着自己的文件描述符。很高兴你没有否认我的评论。 –