Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build cosim shared library without VCS access #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ if(DEFINED ENV{VCS_HOME})
message(STATUS "Detected VCS...")
message(STATUS "Building DPI/VPI cosimulation library")
add_subdirectory(cosim)
add_definitions(-DUSING_VCS)
else()
message(WARNING "VCS not detected, skipping build of cosimulation library...")
message(WARNING "VCS not detected, building cosimulation library without VCS support")
add_subdirectory(cosim)
endif()
4 changes: 4 additions & 0 deletions src/cosim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if(DEFINED ENV{VCS_HOME})
add_definitions(-DUSING_VCS)
endif()

add_library(spect_iss_dpi SHARED
spect_iss_dpi.cpp
)
Expand Down
53 changes: 52 additions & 1 deletion src/cosim/spect_iss_dpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#include "CpuProgram.h"
#include "HexHandler.h"

#include "vpi_user.h"
#ifdef USING_VCS
#include "vpi_user.h"
#endif

#include "spect_iss_dpi.h"

spect::Compiler *compiler = nullptr;
spect::CpuModel *model = nullptr;

#ifdef USING_VCS
#define DPI_CALL_LOG_ENTER \
assert (model != nullptr && \
MODEL_LABEL " Model not initalized. Did you forget to call 'spect_dpi_init'?"); \
Expand All @@ -34,11 +37,28 @@ spect::CpuModel *model = nullptr;
if (model->verbosity_ >= VERBOSITY_HIGH) \
vpi_printf("%s DPI function '%s' exiting.\n", MODEL_LABEL, __func__); \

#else
#define DPI_CALL_LOG_ENTER \
assert (model != nullptr && \
MODEL_LABEL " Model not initalized. Did you forget to call 'spect_dpi_init'?"); \
if (model->verbosity_ >= VERBOSITY_HIGH) \
printf("%s DPI function '%s' entered.\n", MODEL_LABEL, __func__); \

#define DPI_CALL_LOG_EXIT \
if (model->verbosity_ >= VERBOSITY_HIGH) \
printf("%s DPI function '%s' exiting.\n", MODEL_LABEL, __func__); \

#endif

extern "C" {

uint32_t spect_dpi_init()
{
#ifdef USING_VCS
vpi_printf("%s DPI function '%s' entered.\n", MODEL_LABEL, __func__);
#else
printf("%s DPI function '%s' entered.\n", MODEL_LABEL, __func__);
#endif

uint32_t rv = 0;
try {
Expand All @@ -47,25 +67,46 @@ extern "C" {

// According to C standard, typecasting function pointer is undefined behavior,
// but we only get rid of "const", so we hope its fine :)
#ifdef USING_VCS
model->print_fnc = (int (*)(const char *format, ...))(&(vpi_printf));
compiler->print_fnc = (int (*)(const char *format, ...))(&(vpi_printf));
#else
model->print_fnc = (int (*)(const char *format, ...))(&(printf));
compiler->print_fnc = (int (*)(const char *format, ...))(&(printf));
#endif
} catch (const std::bad_alloc& e) {
#ifdef USING_VCS
vpi_printf("%s Failed to initialize SPECT DPI model: %s\n", MODEL_LABEL, e.what());
#else
printf("%s Failed to initialize SPECT DPI model: %s\n", MODEL_LABEL, e.what());
#endif
rv = 1;
}

#ifdef USING_VCS
vpi_printf("%s DPI function '%s' exiting.\n", MODEL_LABEL, __func__);
#else
printf("%s DPI function '%s' exiting.\n", MODEL_LABEL, __func__);
#endif
return rv;
}

void spect_dpi_exit()
{
#ifdef USING_VCS
vpi_printf("%s DPI function '%s' entered.\n", MODEL_LABEL, __func__);
#else
printf("%s DPI function '%s' entered.\n", MODEL_LABEL, __func__);
#endif

delete model;
delete compiler;

#ifdef USING_VCS
vpi_printf("%s DPI function '%s' exiting.\n", MODEL_LABEL, __func__);
#else
printf("%s DPI function '%s' exiting.\n", MODEL_LABEL, __func__);
#endif
}

void spect_dpi_reset()
Expand Down Expand Up @@ -337,8 +378,13 @@ extern "C" {
internal_parity_type);

} catch(std::system_error &exception) {
#ifdef USING_VCS
vpi_printf("%s Failed to compile program with exit code: %d\n",
MODEL_LABEL, exception.code().value());
#else
printf("%s Failed to compile program with exit code: %d\n",
MODEL_LABEL, exception.code().value());
#endif
err = exception.code().value();
}

Expand All @@ -354,7 +400,12 @@ extern "C" {
if (s_start_addr)
rv = s_start_addr->val_;
else
#ifdef USING_VCS
vpi_printf("%s: '%s' symbol not defined! Returning 0.\n", MODEL_LABEL, START_SYMBOL);
#else
printf("%s: '%s' symbol not defined! Returning 0.\n", MODEL_LABEL, START_SYMBOL);
#endif

DPI_CALL_LOG_EXIT
return rv;
}
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if(DEFINED ENV{VCS_HOME})
message(STATUS "Detected VCS...")
message(STATUS "Building DPI library tests")
add_subdirectory(dpi)
add_definitions(-DUSING_VCS)
else()
message(WARNING "VCS not detected, skipping build of DPI library tests...")
endif()
Expand Down