systemVerilog dpic调用python总结

本文详细介绍了如何在SystemVerilog中调用Python,包括Python库的安装要求,如添加--enable-shared选项,以及编译过程。在C++文件中使用extern "C"来配合SystemVerilog,并利用g++编译生成动态链接库。同时,提供了两种不同的加载方式,一种是vcs动态加载.so文件,另一种是vcs直接加载.c/.cpp文件。最后,提到了C++调用Python接口的在线手册作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、python3库准备

安装python的时候要加 --enable-shared,否则不会生成和python相关的xxx.so动态链接库文件,

而且最好安装在/usr下面,而不是/usr/local/,这样的话需要的Python.h头文件就会在/usr/include/Python3.7m,除此之外,还需要svdpi.h头文件,这个文件目录在${VCS_HOME}/include下面。

./configure --prefix=/usr/local/python3 --enable-shared 
//./configure --prefix=/usr/python3 --enable-shared

二、编译

<A. vcs动态加载 .so>

文件./soft/c_call_py.cpp

//./soft/c_call_py.cpp
#include<Python.h>
#include<svdpi.h>
#include<stdlib.h>
#include<iostream>
//#define PYTHON_PATH "/home/ICer/workspace/systemverilog-python-main/1.systemverilog_demo/soft"
//#ifndef PYTHON_PATH
//#error You must define the PYTHONPATH to the python file in gcc compiler with -D
//#endif

static PyObject *pModule  = NULL;
static PyObject *pFunc    = NULL;
static PyObject *pDict    = NULL;
static PyObject *pReturn  = NULL;
static PyObject *PyList   = NULL;
static PyObject *ArgList  = NULL;
static PyGILState_STATE py_state;

//extern "C" must be needed if code is C++
extern "C" void c_py_init(){
    char *append_path = (char *)malloc(sizeof(char) * 1000);
    //append_path = PYTHON_PATH":.";
    append_path = "/home/ICer/workspace/systemverilog-python-main/1.systemverilog_demo/soft:.";
    setenv("PYTHONPATH",append_path,1);//Set PYTHONPATH TO working directory https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/setenv.htm
    printf("PYTHONPATH is:%s !\n", append_path);

    Py_Initialize();

    py_state = PyGILState_Ensure();

    pModule = PyImport_ImportModule("py_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值