| |
| |
| |
|
|
| #include <cocotb_utils.h> |
| #include <embed.h> |
| #include <gpi.h> |
|
|
| #include <cstdlib> |
| #ifdef _WIN32 |
| #include <windows.h> |
|
|
| #include <string> |
| #endif |
|
|
| #ifndef PYTHON_LIB |
| #error "Name of Python library required" |
| #else |
| #define PYTHON_LIB_STR xstr(PYTHON_LIB) |
| #endif |
|
|
| #ifndef EMBED_IMPL_LIB |
| #error "Name of embed implementation library required" |
| #else |
| #define EMBED_IMPL_LIB_STR xstr(EMBED_IMPL_LIB) |
| #endif |
|
|
| static void (*_embed_init_python)(); |
| static void (*_embed_sim_cleanup)(); |
| static int (*_embed_sim_init)(int argc, char const *const *argv); |
| static void (*_embed_sim_event)(const char *msg); |
|
|
| static bool init_failed = false; |
|
|
| #ifdef _WIN32 |
| static ACTCTX act_ctx = { |
| sizeof(ACTCTX), |
| ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID, |
| NULL, |
| 0, |
| 0, |
| NULL, |
| MAKEINTRESOURCE(1000), |
| NULL, |
| 0}; |
|
|
| BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID) { |
| if (fdwReason == DLL_PROCESS_ATTACH) { |
| act_ctx.hModule = hinstDLL; |
| } |
|
|
| return TRUE; |
| } |
| #endif |
|
|
| extern "C" void embed_init_python(void) { |
| |
| char const *libpython_path = getenv("LIBPYTHON_LOC"); |
| if (!libpython_path) { |
| |
| libpython_path = PYTHON_LIB_STR; |
| } |
| auto loaded = utils_dyn_open(libpython_path); |
| if (!loaded) { |
| |
| init_failed = true; |
| return; |
| |
| } |
|
|
| #ifdef _WIN32 |
| if (!act_ctx.hModule) { |
| |
| init_failed = true; |
| return; |
| |
| } |
|
|
| HANDLE hact_ctx = CreateActCtx(&act_ctx); |
| if (hact_ctx == INVALID_HANDLE_VALUE) { |
| |
| init_failed = true; |
| return; |
| |
| } |
|
|
| ULONG_PTR Cookie; |
| if (!ActivateActCtx(hact_ctx, &Cookie)) { |
| |
| init_failed = true; |
| return; |
| |
| } |
| #endif |
|
|
| |
| void *embed_impl_lib_handle; |
| if (!(embed_impl_lib_handle = utils_dyn_open(EMBED_IMPL_LIB_STR))) { |
| |
| init_failed = true; |
| return; |
| |
| } |
| if (!(_embed_init_python = reinterpret_cast<decltype(_embed_init_python)>( |
| utils_dyn_sym(embed_impl_lib_handle, "_embed_init_python")))) { |
| |
| init_failed = true; |
| return; |
| |
| } |
| if (!(_embed_sim_cleanup = reinterpret_cast<decltype(_embed_sim_cleanup)>( |
| utils_dyn_sym(embed_impl_lib_handle, "_embed_sim_cleanup")))) { |
| |
| init_failed = true; |
| return; |
| |
| } |
| if (!(_embed_sim_init = reinterpret_cast<decltype(_embed_sim_init)>( |
| utils_dyn_sym(embed_impl_lib_handle, "_embed_sim_init")))) { |
| |
| init_failed = true; |
| return; |
| |
| } |
| if (!(_embed_sim_event = reinterpret_cast<decltype(_embed_sim_event)>( |
| utils_dyn_sym(embed_impl_lib_handle, "_embed_sim_event")))) { |
| |
| init_failed = true; |
| return; |
| |
| } |
|
|
| #ifdef _WIN32 |
| if (!DeactivateActCtx(0, Cookie)) { |
| |
| init_failed = true; |
| return; |
| |
| } |
|
|
| ReleaseActCtx(hact_ctx); |
| #endif |
|
|
| |
| _embed_init_python(); |
| } |
|
|
| extern "C" void embed_sim_cleanup(void) { |
| if (!init_failed) { |
| _embed_sim_cleanup(); |
| } |
| } |
|
|
| extern "C" int embed_sim_init(int argc, char const *const *argv) { |
| if (init_failed) { |
| |
| return -1; |
| |
| } else { |
| return _embed_sim_init(argc, argv); |
| } |
| } |
|
|
| extern "C" void embed_sim_event(const char *msg) { |
| if (!init_failed) { |
| _embed_sim_event(msg); |
| } |
| } |
|
|