Skip to content

Commit bffd44f

Browse files
committed
Update to support AMD Xilinx Xsim
Signed-off-by: Matthew Ballance <[email protected]>
1 parent 41c2541 commit bffd44f

File tree

17 files changed

+218
-14
lines changed

17 files changed

+218
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Test build
1414
run: |
1515
python3 -m venv py
16-
./py/bin/pip install ivpm pytest pytest-dfm dv-flow-libhdlsim
16+
./py/bin/pip install -U ivpm pytest pytest-dfm dv-flow-libhdlsim
1717
./py/bin/python3 -m ivpm update -a
1818
./packages/python/bin/python3 setup.py bdist_wheel
1919
- name: Run tests

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,4 @@ results.xml
175175
*.bk
176176

177177
examples/call/dpi/call_sv_bfm/call_sv_bfm_pkg.sv
178+
rundir/
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11

22
pyhdl-if
3-
cocotb
3+
dv-flow-mgr
4+
dv-flow-libhdlsim
5+

ivpm.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ package:
2525
src: pypi
2626
- name: cython
2727
src: pypi
28+
- name: build
29+
src: pypi
2830
- name: setuptools_scm
2931
src: pypi
3032
- name: pytest-dfm

src/entry.c

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,29 @@ extern "C" {
6161
#endif
6262

6363
static lib_h_t _python_lib = 0;
64-
6564
static lib_h_t init_python();
6665

66+
/**
67+
* The following ensures that this DPI library has a dependency
68+
* on DPI-exported symbols. This ensures that Python is able
69+
* to find the exports
70+
*/
71+
int pyhdl_pi_if_RegisterTimeCB();
72+
int pyhdl_call_if_invoke_hdl_f(void);
73+
int pyhdl_call_if_invoke_hdl_t(void);
74+
int pyhdl_call_if_response_py_t(void);
75+
76+
void *funcs[] = {
77+
&pyhdl_pi_if_RegisterTimeCB,
78+
&pyhdl_call_if_invoke_hdl_f,
79+
&pyhdl_call_if_invoke_hdl_t,
80+
&pyhdl_call_if_response_py_t,
81+
};
82+
83+
void *get_dpiexport_funcs() {
84+
return funcs;
85+
}
86+
6787
//typedef void *PyObject;
6888

6989
/*******************************************************************
@@ -234,7 +254,7 @@ lib_h_t find_loaded_lib(const char *sym) {
234254
char mapfile_path[256];
235255
FILE *map_fp;
236256

237-
DEBUG("find_loaded_lib(linux) %s", sym);
257+
DEBUG("--> find_loaded_lib(linux) %s", sym);
238258

239259
// First, try loading the executable
240260
{
@@ -244,6 +264,7 @@ lib_h_t find_loaded_lib(const char *sym) {
244264
DEBUG("returning %p", ret);
245265
return ret;
246266
} else {
267+
DEBUG("didn't find symbol \"%s\" in the executable\n", sym);
247268
ret = 0;
248269
}
249270
}
@@ -299,11 +320,14 @@ lib_h_t find_loaded_lib(const char *sym) {
299320
free(path_s);
300321
}
301322

323+
DEBUG("<-- find_loaded_lib(linux) %s %p", sym, ret);
324+
302325
return ret;
303326
}
304327

305328
lib_h_t check_lib(const char *path, const char *sym) {
306329
lib_h_t ret = 0;
330+
DEBUG("--> check_lib(%s, %s)", path, sym);
307331
lib_h_t lib = dlopen(path, RTLD_LAZY);
308332
if (lib) {
309333
void *sym_h = dlsym(lib, sym);
@@ -314,11 +338,53 @@ lib_h_t check_lib(const char *path, const char *sym) {
314338
ret = lib;
315339
}
316340
}
341+
DEBUG("<-- check_lib(%s, %s) %p", path, sym, ret);
317342
return ret;
318343
}
319344

320345
#endif
321346

347+
char *clean_env(const char *name, const char *omit) {
348+
const char *value = getenv(name);
349+
char *new_value;
350+
const char *cp, *cpn;
351+
int first_entry = 1;
352+
353+
if (!value || !value[0]) {
354+
return 0;
355+
}
356+
357+
cp = value;
358+
359+
new_value = (char *)malloc(strlen(name)+strlen(value)+2);
360+
strcpy(new_value, name);
361+
strcat(new_value, "=");
362+
363+
while (cp && *cp) {
364+
cpn = strchr(cp, ':');
365+
if (!cpn) {
366+
cpn = cp + strlen(cp);
367+
}
368+
369+
// Check if this path starts with pythonhome
370+
if (strncmp(cp, omit, strlen(omit)) != 0) {
371+
if (!first_entry) {
372+
strcat(new_value, ":");
373+
}
374+
strncat(new_value, cp, cpn - cp);
375+
first_entry = 0;
376+
}
377+
378+
if (*cpn == ':') {
379+
cp = cpn + 1;
380+
} else {
381+
cp = NULL;
382+
}
383+
}
384+
385+
return new_value;
386+
}
387+
322388
#ifdef _WIN32
323389
#else
324390
lib_h_t find_config_python_lib() {
@@ -342,6 +408,62 @@ lib_h_t find_config_python_lib() {
342408
fprintf(stdout, "PyHDL-IF Note: Using Python interpreter \"%s\", specified by $PYHDL_IF_PYTHON\n",
343409
python);
344410
fflush(stdout);
411+
const char *pythonhome = getenv("PYTHONHOME");
412+
if (pythonhome && pythonhome[0]) {
413+
const char *pythonpath = getenv("PYTHONPATH");
414+
const char *ldlibrarypath = getenv("LD_LIBRARY_PATH");
415+
const char *path = getenv("PATH");
416+
417+
fprintf(stdout, "PyHDL-IF Note: Clearing PYTHONHOME and cleaning PYTHONPATH\n");
418+
fflush(stdout);
419+
420+
{
421+
char *new_pythonpath = clean_env("PYTHONPATH", pythonhome);
422+
if (new_pythonpath) {
423+
fprintf(stdout, "PyHDL-IF Note: Setting PYTHONPATH to \"%s\"\n", new_pythonpath);
424+
putenv(new_pythonpath);
425+
}
426+
}
427+
{
428+
char *new_ldlibrarypath = clean_env("LD_LIBRARY_PATH", pythonhome);
429+
if (new_ldlibrarypath) {
430+
fprintf(stdout, "PyHDL-IF Note: Setting LD_LIBRARY_PATH to \"%s\"\n", new_ldlibrarypath);
431+
putenv(new_ldlibrarypath);
432+
}
433+
}
434+
435+
{
436+
char *new_path = clean_env("PATH", pythonhome);
437+
if (new_path) {
438+
fprintf(stdout, "PyHDL-IF Note: Setting PATH to \"%s\"\n", new_path);
439+
putenv(new_path);
440+
}
441+
}
442+
443+
putenv(strdup("PYTHON="));
444+
putenv(strdup("PYTHONHOME="));
445+
}
446+
447+
// Now, add the new Python interpreter to the PATH
448+
{
449+
char *python_dir = strdup(python);
450+
char *slash = strrchr(python_dir, '/');
451+
char *new_path;
452+
if (slash) {
453+
*slash = 0; // Remove the filename
454+
}
455+
456+
DEBUG("Python directory: %s", python_dir);
457+
new_path = (char *)malloc(strlen(python_dir)+strlen(getenv("PATH"))+16);
458+
strcpy(new_path, "PATH=");
459+
strcat(new_path, python_dir);
460+
strcat(new_path, ":");
461+
strcat(new_path, getenv("PATH"));
462+
DEBUG("New PATH: %s", new_path);
463+
putenv(new_path);
464+
free(python_dir);
465+
}
466+
345467
}
346468

347469
args[0] = python;
@@ -350,8 +472,15 @@ lib_h_t find_config_python_lib() {
350472
args[3] = 0;
351473

352474
{
475+
int i;
476+
extern char **environ;
353477
const char *ld_library_path = getenv("LD_LIBRARY_PATH");
354478
DEBUG("LD_LIBRARY_PATH: %s", ld_library_path?ld_library_path:"null");
479+
480+
for (i=0; environ[i]; i++) {
481+
DEBUG("environ[%d]: %s", i, environ[i]);
482+
}
483+
355484
}
356485

357486
(void)pipe(cout_pipe);

src/hdl_if/cmd/cmd_api_gen_sv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __call__(self, args):
5454
gen = GenSVClass(fp, uvm=args.uvm)
5555

5656
if args.package is not None:
57+
gen.println('`include "pyhdl_if_macros.svh"')
5758
gen.println("package %s;" % args.package)
5859
gen.inc_ind()
5960

src/hdl_if/impl/call/call_proxy_dpi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ async def invoke_hdl_t(
4646
from hdl_if.backend import Backend
4747
be = Backend.inst();
4848
evt = be.mkEvent()
49+
# param = (method_name,)
50+
param = method_name
4951

50-
self.ep.invoke_hdl_t(self.obj_id, evt, method_name, args)
52+
self.ep.invoke_hdl_t(self.obj_id, evt, param, args)
5153
res = await evt.wait()
5254

5355
return res

src/hdl_if/impl/call/gen_sv_class.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def gen_task_dispatch(self, api):
392392
self.dec_ind()
393393
self.println()
394394
self.inc_ind()
395+
self.println('`PYHDL_IF_ENTER(("invokeTask"));');
395396
self.println("retval = pyhdl_if::None;");
396397
self.println()
397398
self.println("case (method)")
@@ -440,6 +441,7 @@ def gen_task_dispatch(self, api):
440441

441442
self.dec_ind()
442443
self.println("endcase")
444+
self.println('`PYHDL_IF_LEAVE(("invokeTask"));');
443445
self.dec_ind()
444446
self.println("endtask")
445447

src/hdl_if/impl/call/hdl_call_endpoint_dpi.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,24 @@ def __init__(self, scope):
3030
self.scope = scope
3131

3232
exe_l = ctypes.cdll.LoadLibrary(None)
33-
if not hasattr(exe_l, "pyhdl_call_if_invoke_hdl_f"):
34-
print("TODO: Try DPIExportLib")
35-
exe_l = self._findDPIExportLib()
33+
pyhdl_call_if_invoke_hdl_f = None
34+
try:
35+
pyhdl_call_if_invoke_hdl_f = getattr(exe_l, "pyhdl_call_if_invoke_hdl_f")
36+
pyhdl_call_if_invoke_hdl_f.restype = ctypes.c_int
37+
except Exception as e:
38+
print("Exception(__init__): %s" % str(e), flush=True)
39+
# pyhdl_call_if_invoke_f = None
40+
# if not hasattr(exe_l, "pyhdl_call_if_invoke_hdl_f"):
41+
# print("TODO: Try DPIExportLib")
42+
# exe_l = self._findDPIExportLib()
43+
3644
try:
3745
# self.svSetScope = exe_l.svSetScope
3846
# self.svSetScope.restype = None
3947
# self.svSetScope.argtypes = (
4048
# ctypes.c_void_p,
4149
# )
42-
self.pyhdl_call_if_invoke_hdl_f = exe_l.pyhdl_call_if_invoke_hdl_f
50+
self.pyhdl_call_if_invoke_hdl_f = pyhdl_call_if_invoke_hdl_f
4351
self.pyhdl_call_if_invoke_hdl_f.restype = ctypes.py_object
4452
self.pyhdl_call_if_invoke_hdl_f.argtypes = (
4553
ctypes.c_int,
@@ -50,7 +58,8 @@ def __init__(self, scope):
5058
self.pyhdl_call_if_invoke_hdl_t.argtypes = (
5159
ctypes.c_int,
5260
ctypes.py_object,
53-
ctypes.c_char_p,
61+
# ctypes.c_char_p,
62+
ctypes.py_object,
5463
ctypes.py_object)
5564
self.pyhdl_call_if_response_py_t = exe_l.pyhdl_call_if_response_py_t
5665
self.pyhdl_call_if_response_py_t.restype = None
@@ -80,7 +89,7 @@ def invoke_hdl_t(self,
8089
self.pyhdl_call_if_invoke_hdl_t(
8190
obj_id,
8291
evt_obj,
83-
method_name.encode(),
92+
method_name,
8493
args)
8594

8695
def response_py_t(self, sem_id, res):

src/hdl_if/impl/dpi/hdl_services_dpi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#*
2121
#****************************************************************************
2222
import ctypes
23+
import os
2324
from hdl_if.hdl_services import HdlServices
2425

2526
class HdlServicesDpi(HdlServices):

0 commit comments

Comments
 (0)