Skip to content

Commit b64c4a7

Browse files
committed
update systemverilog <-C-> python inout demo
1 parent d466ce4 commit b64c4a7

File tree

13 files changed

+123
-0
lines changed

13 files changed

+123
-0
lines changed

1.systemverilog_inout/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
SOFT_INC := $(abspath ./soft)
3+
SOFT_SRC := $(shell echo `pwd`)/soft
4+
5+
VCS_CFLAG := -CC -lpython3.6m -lpthread -ldl -lutil -lm -CC -I/usr/include/python3.6m -CC -I$(SOFT_INC) -CC -D'PYTHON_PATH=\"$(SOFT_SRC)\"'
6+
VCS_LDFLAG := -LDFLAGS -Wl,--no-as-needed -LDFLAGS -lpython3.6m
7+
8+
all: clean comp run
9+
10+
clean:
11+
rm -rf simv* csrc* *.log $(SOFT_SRC)/__pycache__ ucli.key vc_hdrs.h stack.info.*
12+
comp:
13+
vcs -full64 $(VCS_CFLAG) $(VCS_LDFLAG) $(SOFT_SRC)/c_call_py.c -sverilog gen_pkt_tb.sv -l comp.log
14+
run:
15+
./simv -l run.log

1.systemverilog_inout/gen_pkt_tb.sv

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
module hello_world;
3+
4+
import "DPI-C" function void c_py_init();
5+
import "DPI-C" function void c_py_final();
6+
import "DPI-C" function void c_py_gen_packet(input bit[64:0] mode, output bit[2047:0] pkt, output bit [7:0] len);
7+
8+
bit [2047:0] data;
9+
bit [7 :0] len ;
10+
bit [64 :0] mode;
11+
12+
initial begin
13+
mode = 'h11_13011112;
14+
c_py_init();
15+
c_py_gen_packet(mode,data,len);
16+
17+
$display("print data in systemverilog !");
18+
19+
$display("get len ='h%h",len );
20+
$display("get data ='h%h",data);
21+
$display("get data = %s",data);
22+
23+
c_py_final();
24+
$finish();
25+
end
26+
endmodule
27+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<Python.h>
2+
#include<svdpi.h>
3+
#include<stdlib.h>
4+
5+
#ifndef PYTHON_PATH
6+
#error You must define the PYTHONPATH to the python file in gcc compiler with -D
7+
#endif
8+
9+
static PyObject *pModule = NULL;
10+
static PyObject *pFunc = NULL;
11+
static PyObject *pDict = NULL;
12+
static PyObject *pReturn = NULL;
13+
static PyObject *PyList = NULL;
14+
static PyObject *ArgList = NULL;
15+
static PyGILState_STATE py_state;
16+
17+
extern void c_py_init(){
18+
char *append_path = malloc(sizeof(char) * 1000);
19+
append_path = PYTHON_PATH":.";
20+
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
21+
printf("PYTHONPATH is:%s !\n", append_path);
22+
23+
Py_Initialize();
24+
25+
py_state = PyGILState_Ensure();
26+
27+
pModule = PyImport_ImportModule("py_gen");
28+
printf("Import python script is py_gen.py !\n");
29+
pDict = PyModule_GetDict(pModule);
30+
pFunc = PyDict_GetItemString(pDict,"gen_pkt");
31+
printf("Python function is gen_pkt !\n");
32+
33+
PyList = PyList_New(2);
34+
ArgList = PyTuple_New(1);
35+
printf("initialize Python runtime !\n");
36+
}
37+
38+
extern void c_py_final(){
39+
PyGILState_Release(py_state);
40+
Py_Finalize();
41+
printf("finalize Python runtime !\n");
42+
}
43+
44+
static unsigned char tmp[256] = {0};
45+
extern void c_py_gen_packet( /*input*/const svBitVecVal *mode ,/*output*/ svBitVecVal *pkt, /*output*/ svBitVecVal *len){
46+
int num;
47+
int i;
48+
unsigned char *result;
49+
50+
PyList_SetItem(PyList, 0, PyLong_FromLong(mode[0]) );
51+
PyList_SetItem(PyList, 1, PyLong_FromLong(mode[1]) );
52+
53+
PyTuple_SetItem(ArgList, 0, PyList);
54+
55+
pReturn = PyObject_CallObject(pFunc, ArgList);
56+
57+
if(PyBytes_Check(pReturn)){
58+
num = PyBytes_Size(pReturn);
59+
result = PyBytes_AsString(pReturn);
60+
//memcpy(pkt,result,num);
61+
for( i = 0; i < num; i++){
62+
tmp[255-i] = result[i];
63+
printf("%02X ",result[i]);
64+
}
65+
memcpy(pkt, tmp, 256);
66+
67+
*len = num;
68+
printf("\n");
69+
} else {
70+
printf("Get No Bytes Form Python !!!\n");
71+
}
72+
}

1.systemverilog_inout/soft/py_gen.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
def gen_pkt(List):
4+
print("SV->C to Python :")
5+
print(List)
6+
p = "This is Python Data !"
7+
print("python print :"+p)
8+
byte = p.encode()
9+
return byte

0 commit comments

Comments
 (0)