| |
Using MATLAB as Scoreboard checker
matlab_dpi.c
为了利用MATLAB函数作为记分板检查器,我们采用略微不同的方法,因为我们现在想直接从SV预测器代码调用我们的MATLAB函数,而不是通过HDL包装器进行通信。MATLAB提供了一个基于C的API,它允许您启动MATLAB以及将MATLAB命令发送到MATLAB引擎并从输出缓冲区读回结果。为了简化这些调用,我们创建了一个DPI包装器库来使用这些函数。该DPI模块提供4种基本功能:
int start_matlab(string cmd) - 使用给定的命令字符串启动MATLAB。如果传递空字符串,则使用默认的“matlab”命令。如果成功,返回值不为零。
int send_matlab_cmd(string cmd) - 将基于文本的命令发送到MATLAB以进行评估。您可以使用它来设置MATLAB变量,配置MATLAB或评估MATLAB函数。如果成功则返回0。
string get_matlab_buffer() - 从最后发送的MATLAB命令获取MATLAB输出缓冲区。您可以使用它来评估先前发送的MATLAB命令。
void stop_matlab() - 导致MATLAB引擎退出。
matlab_dpi.c与任何其他DPI函数一样编译,并作为共享库连接。它连接到两个MATLAB库(-leng和-lmx),它们提供MATLAB引擎功能和MX数据通信功能。另外,确保在$ LD_LIBRARY_PATH中有MATLAB库目录$ MATLAB / bin / <architecture>,以便找到所需的MATLAB库。
/*
* matlab_dpi.c
*
* DPI Functions to enable SV communication to MATLAB
*/
#include <stdlib.h>
#include <stdio.h>
#define BUFSIZE 256
mxArray *T = NULL, *result = NULL;
char buffer[BUFSIZE+1];
{
if (!ep && !(ep = engOpen(cmd))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return 0;
}
engOutputBuffer(ep, buffer, BUFSIZE);
return 1;
}
{
return engEvalString(ep, cmd);
}
{
return buffer;
}
{
engClose(ep);
}
import "DPI-C" function int send_matlab_cmd(string cmd);
import "DPI-C" function string get_matlab_buffer();
import "DPI-C" function void stop_matlab();