Skip to content

Commit 02f4443

Browse files
committed
update systemverilog <-C-> python demo
1 parent b64c4a7 commit 02f4443

File tree

13 files changed

+225
-169
lines changed

13 files changed

+225
-169
lines changed

0.c_call_py_only/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
all: clean comp run
3+
4+
clean:
5+
rm -rf main __pycache__
6+
comp:
7+
gcc c_call_py.c -o main -I/usr/include/python3.6m -lpython3.6m -lpthread -ldl -lutil -lm
8+
run:
9+
export PYTHONPATH=. && ./main

0.systemverilog_only/c_call_py.c renamed to 0.c_call_py_only/c_call_py.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include<Python.h>
2-
#include<svdpi.h>
32
#include<stdlib.h>
43

54
static PyObject *pModule = NULL;
@@ -32,16 +31,21 @@ extern void c_py_init(){
3231
}
3332

3433
extern void c_py_final(){
34+
Py_DECREF(pModule);
35+
Py_DECREF(pFunc );
36+
Py_DECREF(pDict );
37+
Py_DECREF(pReturn);
38+
Py_DECREF(ArgList);
3539
PyGILState_Release(py_state);
3640
Py_Finalize();
3741
printf("finalize Python runtime !\n");
3842
}
3943

40-
static unsigned char tmp[256] = {0};
41-
extern void c_py_gen_packet(/*output*/ svBitVecVal *pkt, /*output*/ svBitVecVal *len){
44+
//static unsigned char tmp[256] = {0};
45+
extern void c_py_gen_packet(){
4246
int num;
4347
int i;
44-
unsigned char *result;
48+
char *result;
4549

4650
pReturn = PyObject_CallObject(pFunc, ArgList);
4751

@@ -50,14 +54,22 @@ extern void c_py_gen_packet(/*output*/ svBitVecVal *pkt, /*output*/ svBitVecVal
5054
result = PyBytes_AsString(pReturn);
5155
//memcpy(pkt,result,num);
5256
for( i = 0; i < num; i++){
53-
tmp[255-i] = result[i];
54-
printf("%02X ",result[i]);
57+
printf("%02X ", result[i]);
5558
}
56-
memcpy(pkt, tmp, 256);
57-
58-
*len = num;
59+
5960
printf("\n");
6061
} else {
6162
printf("Get No Bytes Form Python !!!\n");
6263
}
6364
}
65+
66+
int main( int argc, char* args[] ){
67+
68+
c_py_init();
69+
70+
c_py_gen_packet();
71+
72+
c_py_final();
73+
74+
return 0;
75+
}
File renamed without changes.

0.systemverilog_only/Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

0.systemverilog_only/gen_pkt_tb.sv

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
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-
1+
module hello_world;
2+
3+
import "DPI-C" function void c_py_init();
4+
import "DPI-C" function void c_py_final();
5+
import "DPI-C" function void c_py_gen_packet(input bit[63:0] mode, output bit[2047:0] pkt, output bit [7:0] len);
6+
7+
bit [2047:0] data;
8+
bit [7 :0] len ;
9+
bit [63 :0] mode;
10+
11+
initial begin
12+
mode = 64'h11_13011112;
13+
c_py_init();
14+
c_py_gen_packet(mode,data,len);
15+
16+
$display("print data in systemverilog !");
17+
18+
$display("get len ='h%h",len );
19+
$display("get data ='h%h",data);
20+
$display("get data = %s",data);
21+
22+
c_py_final();
23+
$finish();
24+
end
25+
endmodule

1.systemverilog_demo/makefile.irun

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
PYTHON_PATH := $(abspath ./soft)
2+
SOFT_INC := $(abspath ./soft)
3+
SOFT_SRC := $(shell echo `pwd`)/soft
4+
5+
IRUN_CFLAG := -ccargs -lpython3.6m -ccargs -I/usr/include/python3.6m -ccargs -I$(SOFT_INC) -D'PYTHON_PATH=\"$(PYTHON_PATH)\"'
6+
IRUN_LDFLAG := -Wld,-lpython3.6m
7+
8+
all: clean comp run
9+
10+
clean:
11+
rm -rf INCA_libs irun.* *.log $(SOFT_SRC)/__pycache__
12+
comp:
13+
irun -sv -64bit -c $(IRUN_CFLAG) $(IRUN_LDFLAG) $(SOFT_SRC)/c_call_py.c gen_pkt_tb.sv -top hello_world
14+
run:
15+
irun -R -64bit

1.systemverilog_demo/makefile.questa

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PYTHON_PATH := $(abspath ./soft)
2+
SOFT_INC := $(abspath ./soft)
3+
SOFT_SRC := $(shell echo `pwd`)/soft
4+
5+
VLOG_CFLAG := -ccflags -lpython3.6m -ccflags -I/usr/include/python3.6m -ccflags -I$(SOFT_INC) -ccflags -D'PYTHON_PATH=\"$(PYTHON_PATH)\"'
6+
VLOG_LDFLAG := -ldflags -lpython3.6m
7+
8+
all: clean comp run
9+
10+
clean:
11+
rm -rf transcript *.log $(SOFT_SRC)/__pycache__ work*
12+
comp:
13+
vlib work
14+
vlog -64 $(VLOG_CFLAG) $(SOFT_SRC)/c_call_py.c gen_pkt_tb.sv
15+
run:
16+
vsim -64 $(VLOG_LDFLAG) -do vsim.do -novopt -c hello_world
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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+
PYTHON_PATH := $(abspath ./soft)
2+
SOFT_INC := $(abspath ./soft)
3+
SOFT_SRC := $(shell echo `pwd`)/soft
4+
5+
VCS_CFLAG := -CC -lpython3.6m -CC -I/usr/include/python3.6m -CC -I$(SOFT_INC) -CC -D'PYTHON_PATH=\"$(PYTHON_PATH)\"'
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
Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,78 @@
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+
#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+
Py_DECREF(pModule);
40+
Py_DECREF(pFunc );
41+
Py_DECREF(pDict );
42+
Py_DECREF(pReturn);
43+
Py_DECREF(ArgList);
44+
Py_DECREF(PyList );
45+
PyGILState_Release(py_state);
46+
Py_Finalize();
47+
printf("finalize Python runtime !\n");
48+
}
49+
50+
static unsigned char tmp[256] = {0};
51+
extern void c_py_gen_packet( /*input*/const svBitVecVal *mode ,/*output*/ svBitVecVal *pkt, /*output*/ svBitVecVal *len){
52+
int num;
53+
int i;
54+
char *result;
55+
56+
PyList_SetItem(PyList, 0, PyLong_FromLong(mode[0]) );
57+
PyList_SetItem(PyList, 1, PyLong_FromLong(mode[1]) );
58+
59+
PyTuple_SetItem(ArgList, 0, PyList);
60+
61+
pReturn = PyObject_CallObject(pFunc, ArgList);
62+
63+
if(PyBytes_Check(pReturn)){
64+
num = PyBytes_Size(pReturn);
65+
result = PyBytes_AsString(pReturn);
66+
//memcpy(pkt,result,num);
67+
for( i = 0; i < num; i++){
68+
tmp[255-i] = result[i];
69+
printf("%02X ",result[i]);
70+
}
71+
memcpy(pkt, tmp, 256);
72+
73+
*len = num;
74+
printf("\n");
75+
} else {
76+
printf("Get No Bytes Form Python !!!\n");
77+
}
78+
}

0 commit comments

Comments
 (0)