2012-04-13 01:08:20 +00:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2009-12-05 15:38:49 +00:00
|
|
|
//*************************************************************************
|
|
|
|
//
|
|
|
|
// Copyright 2009-2009 by Wilson Snyder. This program is free software; you can
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2020-03-21 15:24:24 +00:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
2009-12-05 15:38:49 +00:00
|
|
|
// Version 2.0.
|
2020-03-21 15:24:24 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2009-12-05 15:38:49 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
|
2010-01-25 00:00:34 +00:00
|
|
|
#include <cstdio>
|
2010-04-10 10:46:24 +00:00
|
|
|
#include "svdpi.h"
|
2009-12-05 15:38:49 +00:00
|
|
|
|
|
|
|
//======================================================================
|
|
|
|
|
|
|
|
#if defined(VERILATOR)
|
|
|
|
# ifdef T_DPI_CONTEXT_NOOPT
|
|
|
|
# include "Vt_dpi_context_noopt__Dpi.h"
|
|
|
|
# else
|
|
|
|
# include "Vt_dpi_context__Dpi.h"
|
|
|
|
# endif
|
|
|
|
#elif defined(VCS)
|
|
|
|
# include "../vc_hdrs.h"
|
|
|
|
#elif defined(CADENCE)
|
|
|
|
# define NEED_EXTERNS
|
|
|
|
#else
|
|
|
|
# error "Unknown simulator for DPI test"
|
|
|
|
#endif
|
|
|
|
|
2009-12-08 11:41:16 +00:00
|
|
|
#ifdef VERILATOR
|
|
|
|
# include "verilated.h"
|
|
|
|
#endif
|
|
|
|
|
2009-12-05 15:38:49 +00:00
|
|
|
#ifdef NEED_EXTERNS
|
|
|
|
extern "C" {
|
|
|
|
|
2020-10-28 00:03:17 +00:00
|
|
|
extern int dpic_line();
|
|
|
|
extern int dpic_save(int value);
|
|
|
|
extern int dpic_restore();
|
|
|
|
extern unsigned dpic_getcontext();
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//======================================================================
|
|
|
|
|
|
|
|
int dpic_line() {
|
|
|
|
svScope scope = svGetScope();
|
|
|
|
if (!scope) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetScope failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 11:41:16 +00:00
|
|
|
#ifdef VERILATOR
|
|
|
|
static int didDump = 0;
|
2019-11-10 01:35:12 +00:00
|
|
|
if (didDump++ == 0) { Verilated::scopesDump(); }
|
2009-12-08 11:41:16 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-05 15:38:49 +00:00
|
|
|
const char* scopenamep = svGetNameFromScope(scope);
|
|
|
|
if (!scopenamep) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetNameFromScope failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
if (scope != svGetScopeFromName(scopenamep)) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetScopeFromName repeat failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* filenamep = "";
|
|
|
|
int lineno = 0;
|
|
|
|
if (svGetCallerInfo(&filenamep, &lineno)) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("Call from %s:%d:%s\n", filenamep, lineno, scopenamep);
|
2009-12-05 15:38:49 +00:00
|
|
|
} else {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetCallerInfo failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
2020-10-14 01:13:52 +00:00
|
|
|
if (svGetCallerInfo(nullptr, nullptr)) {} // Check doesn't segflt
|
2009-12-05 15:38:49 +00:00
|
|
|
return lineno;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern int Dpic_Unique;
|
2019-05-08 02:34:09 +00:00
|
|
|
int Dpic_Unique = 0; // Address used for uniqueness
|
2009-12-05 15:38:49 +00:00
|
|
|
|
|
|
|
int dpic_save(int value) {
|
|
|
|
svScope scope = svGetScope();
|
|
|
|
if (!scope) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetScope failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 02:04:53 +00:00
|
|
|
// Use union to avoid cast to different size pointer warnings
|
|
|
|
union valpack {
|
2019-05-08 02:34:09 +00:00
|
|
|
void* ptr;
|
|
|
|
int i;
|
2012-05-11 02:04:53 +00:00
|
|
|
} vp;
|
|
|
|
|
2020-10-28 00:03:17 +00:00
|
|
|
vp.i = value;
|
|
|
|
if (vp.i) {}
|
2012-05-11 02:04:53 +00:00
|
|
|
if (svPutUserData(scope, &Dpic_Unique, vp.ptr)) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svPutUserData failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dpic_restore() {
|
|
|
|
svScope scope = svGetScope();
|
|
|
|
if (!scope) {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetScope failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 02:04:53 +00:00
|
|
|
if (void* userp = svGetUserData(scope, (void*)&Dpic_Unique)) {
|
2019-05-08 02:34:09 +00:00
|
|
|
// Use union to avoid cast to different size pointer warnings
|
|
|
|
union valpack {
|
|
|
|
void* ptr;
|
|
|
|
int i;
|
|
|
|
} vp;
|
|
|
|
vp.ptr = userp;
|
|
|
|
return vp.i;
|
2009-12-05 15:38:49 +00:00
|
|
|
} else {
|
2019-05-08 02:34:09 +00:00
|
|
|
printf("%%Warning: svGetUserData failed\n");
|
|
|
|
return 0;
|
2009-12-05 15:38:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 00:55:52 +00:00
|
|
|
|
|
|
|
unsigned dpic_getcontext() {
|
|
|
|
svScope scope = svGetScope();
|
2019-11-10 01:35:12 +00:00
|
|
|
printf("%%Info: svGetScope returned scope (%p) with name %s\n",
|
|
|
|
scope, svGetNameFromScope(scope));
|
|
|
|
return (unsigned)(uintptr_t)scope;
|
2014-07-22 00:55:52 +00:00
|
|
|
}
|