forked from accellera-official/systemc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To compile 1. in systemc folder $ autoreconf -vfi 2. in objdir $ ../configure 'CXXFLAGS=-std=c++17' $ make $ make install 3. to compile the bsmedit example CC = g++ -std=c++17 LDFLAGS = -Wl,-U,_sc_main,-U,___sanitizer_start_switch_fiber,-U,___sanitizer_finish_switch_fiber g++ -shared $(LDFLAGS) $^ -o [email protected]
- Loading branch information
Tianzhu Qiao
committed
Dec 17, 2024
1 parent
11ad094
commit 801866c
Showing
27 changed files
with
7,205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
autom4te.cache/ | ||
build/ | ||
include/ | ||
lib-macosx64/ | ||
lib-macosx64_2/ | ||
objdir*/ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <vector> | ||
#include <map> | ||
#include <string> | ||
#include <algorithm> | ||
#include "../systemc.h" | ||
|
||
extern "C" { | ||
#include "bsm.h" | ||
} | ||
|
||
class bsm_buffer_impl : public bsm_buf_read_inf, public bsm_buf_write_inf { | ||
public: | ||
bsm_buffer_impl(int size = 256); | ||
virtual ~bsm_buffer_impl(); | ||
|
||
public: | ||
// bsm_buf_read_inf | ||
virtual int size(); | ||
virtual double read(int n) const; | ||
|
||
// bsm_buf_write_inf | ||
virtual bool write(double value, int n); | ||
virtual bool append(double value); | ||
virtual bool resize(int nSize); | ||
|
||
bool retrive(double* buf, int size); | ||
|
||
protected: | ||
std::vector<double> m_buffer; | ||
int m_nRead; | ||
int m_nWrite; | ||
}; | ||
|
||
static std::map<std::string, bsm_sim_object*> sim_objs; | ||
static std::map<std::string, bsm_sim_trace_file*> sim_tfiles; | ||
static std::map<std::string, bsm_sim_trace_buf*> sim_tbufs; | ||
static std::map<std::string, bsm_buffer_impl*> sim_bufs; | ||
|
||
bsm_buffer_impl::bsm_buffer_impl(int size) | ||
: m_nRead(0) | ||
, m_nWrite(0) { | ||
resize(size); | ||
} | ||
|
||
bsm_buffer_impl::~bsm_buffer_impl() { | ||
} | ||
|
||
// bsm_buf_read_inf | ||
int bsm_buffer_impl::size() { | ||
return m_buffer.size(); | ||
} | ||
|
||
double bsm_buffer_impl::read(int n) const { | ||
if (m_buffer.size() > 0) { | ||
n = (n + m_nWrite) % m_buffer.size(); | ||
return m_buffer[n]; | ||
} | ||
return 0.0; | ||
} | ||
|
||
// bsm_buf_write_inf | ||
bool bsm_buffer_impl::append(double value) { | ||
if (m_buffer.size() > 0) { | ||
assert(m_nWrite < (int)m_buffer.size() && m_nWrite >= 0); | ||
m_buffer[m_nWrite] = value; | ||
m_nWrite++; | ||
m_nWrite = m_nWrite%m_buffer.size(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool bsm_buffer_impl::write(double value, int n) { | ||
if (n < (int)m_buffer.size() && n >= 0) { | ||
n = (n + m_nWrite) % m_buffer.size(); | ||
m_buffer[n] = value; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool bsm_buffer_impl::resize(int nSize) { | ||
if (nSize < 0) return false; | ||
m_buffer.resize(nSize); | ||
m_nRead = m_nWrite = 0; | ||
return true; | ||
} | ||
|
||
bool bsm_buffer_impl::retrive(double* buf, int size) { | ||
int count = (int)(size < this->size() ? size : this->size()); | ||
if (m_nWrite >= count) { | ||
std::copy(m_buffer.begin()+m_nWrite-count, m_buffer.begin()+m_nWrite, buf); | ||
} else { | ||
int sz = count - m_nWrite; | ||
std::copy(m_buffer.begin()+(this->size()-sz), m_buffer.begin()+this->size(), buf); | ||
std::copy(m_buffer.begin(), m_buffer.begin()+m_nWrite, buf+sz); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bsm_sim_context* g_sim = NULL; | ||
BSMEDIT_EXPORT bool ctx_read(sim_object* obj) { | ||
if (obj && obj->readable) { | ||
return sim_objs[obj->name]->read((bsm_sim_object::bsm_object_value*)&obj->value); | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_write(sim_object* obj) { | ||
if (obj && obj->writable) { | ||
return sim_objs[obj->name]->write((bsm_sim_object::bsm_object_value*)&obj->value); | ||
} | ||
return false; | ||
} | ||
|
||
void copy_simobject(sim_object* obj, bsm_sim_object* simobj) { | ||
snprintf(obj->name, MAX_NAME_LEN, "%s", simobj->name()); | ||
snprintf(obj->basename, MAX_NAME_LEN, "%s", simobj->basename()); | ||
snprintf(obj->kind, MAX_NAME_LEN, "%s", simobj->kind()); | ||
obj->writable = simobj->is_writable(); | ||
obj->readable = simobj->is_readable(); | ||
obj->numeric = simobj->is_number(); | ||
sim_objs[obj->name] = simobj; | ||
ctx_read(obj); | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_first_object(sim_object* obj) { | ||
bsm_sim_object* simobj = g_sim->first_object(); | ||
if (obj && simobj) { | ||
copy_simobject(obj, simobj); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_next_object(sim_object* obj) { | ||
bsm_sim_object* simobj = g_sim->next_object(); | ||
if (obj && simobj) { | ||
copy_simobject(obj, simobj); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_free_object(sim_object* obj) { | ||
if (obj) { | ||
delete sim_objs[obj->name]; | ||
sim_objs.erase(obj->name); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT void ctx_start(double duration, int unit) { | ||
g_sim->start(duration, unit); | ||
} | ||
|
||
BSMEDIT_EXPORT void ctx_stop() { | ||
if (g_sim) { | ||
g_sim->stop(); | ||
delete g_sim; | ||
g_sim = NULL; | ||
} | ||
} | ||
|
||
BSMEDIT_EXPORT double ctx_time() { | ||
return g_sim->time(); | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_time_str(char* time) { | ||
if (time) { | ||
snprintf(time, MAX_NAME_LEN, "%s", g_sim->time_string()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT void ctx_set_callback(bsm_sim_context::bsm_callback fun) { | ||
g_sim->set_callback(fun); | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_create_trace_file(sim_trace_file* t) { | ||
bsm_sim_trace_file* obj = g_sim->add_trace(t->name, t->type); | ||
if (obj) { | ||
sim_tfiles[t->name] = obj; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_close_trace_file(sim_trace_file* t) { | ||
if (g_sim->remove_trace(sim_tfiles[t->name])) { | ||
sim_tfiles.erase(t->name); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_trace_file(sim_trace_file* t, sim_object* obj, | ||
sim_object* val, int trigger) { | ||
if (!t || !obj) { | ||
return false; | ||
} | ||
if (val) { | ||
// ugly code, to be updated | ||
g_sim->trace(sim_tfiles[t->name], sim_objs[val->name]); | ||
sim_tfiles[t->name]->set_trace_type(-1, trigger, 1); | ||
g_sim->trace(sim_tfiles[t->name], sim_objs[obj->name]); | ||
sim_tfiles[t->name]->set_trace_type(-1, 4, 0); | ||
} else { | ||
g_sim->trace(sim_tfiles[t->name], sim_objs[obj->name]); | ||
sim_tfiles[t->name]->set_trace_type(-1, trigger, 0); | ||
} | ||
return true; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_create_trace_buf(sim_trace_buf* t) { | ||
bsm_sim_trace_buf* obj = g_sim->add_trace_buf(t->name); | ||
if (obj) { | ||
sim_tbufs[t->name] = obj; | ||
bsm_buffer_impl* buf = new bsm_buffer_impl(t->size); | ||
obj->set_buffer(buf); | ||
sim_bufs[t->name] = buf; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_close_trace_buf(sim_trace_buf* t) { | ||
if (sim_tbufs.find(t->name) != sim_tbufs.end()) | ||
return g_sim->remove_trace_buf(sim_tbufs[t->name]); | ||
return false; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_trace_buf(sim_trace_buf* t, sim_object* obj, | ||
sim_object* val, int trigger) { | ||
if (val) { | ||
// ugly code, to be updated | ||
g_sim->trace_buf(sim_tbufs[t->name], sim_objs[val->name]); | ||
sim_tbufs[t->name]->set_trace_type(-1, trigger, 1); | ||
g_sim->trace_buf(sim_tbufs[t->name], sim_objs[obj->name]); | ||
sim_tbufs[t->name]->set_trace_type(-1, 4, 0); | ||
} else { | ||
g_sim->trace_buf(sim_tbufs[t->name], sim_objs[obj->name]); | ||
sim_tbufs[t->name]->set_trace_type(-1, trigger, 0); | ||
} | ||
return true; | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_read_trace_buf(sim_trace_buf* t) { | ||
return sim_bufs[t->name]->retrive(t->buffer, t->size); | ||
} | ||
|
||
BSMEDIT_EXPORT bool ctx_resize_trace_buf(sim_trace_buf* t) { | ||
return sim_bufs[t->name]->resize(t->size); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#ifndef SRC_SYSC_BSM_BSM_H_ | ||
#define SRC_SYSC_BSM_BSM_H_ | ||
|
||
#ifdef WIN32 | ||
#define BSMEDIT_EXPORT __declspec(dllexport) | ||
#else | ||
#define BSMEDIT_EXPORT | ||
#endif | ||
#define MAX_NAME_LEN 256 | ||
|
||
typedef struct bsm_object_value { | ||
char sValue[MAX_NAME_LEN]; | ||
double fValue; | ||
unsigned long long uValue; | ||
long long iValue; | ||
int type; | ||
}bsm_object_value; | ||
typedef struct sim_object { | ||
char name[MAX_NAME_LEN]; | ||
char basename[MAX_NAME_LEN]; | ||
char kind[MAX_NAME_LEN]; | ||
bsm_object_value value; | ||
bool writable; | ||
bool readable; | ||
bool numeric; | ||
}sim_object; | ||
|
||
typedef struct sim_trace_file { | ||
char name[MAX_NAME_LEN]; | ||
int type; | ||
}sim_trace_file; | ||
|
||
typedef struct sim_trace_buf { | ||
char name[MAX_NAME_LEN]; | ||
double* buffer; | ||
int size; | ||
}sim_trace_buf; | ||
|
||
typedef struct sim_context { | ||
char version[MAX_NAME_LEN]; | ||
char copyright[MAX_NAME_LEN]; | ||
}sim_context; | ||
|
||
#define BSMEDIT_IMPLEMENT_MODULE(T, name) extern bsm_sim_context* g_sim;\ | ||
extern "C"\ | ||
{\ | ||
BSMEDIT_EXPORT void bsm_sim_top(sim_context *context)\ | ||
{\ | ||
g_sim = bsm_create_sim_context(new T(name));\ | ||
snprintf(context->copyright, MAX_NAME_LEN, "%s", g_sim->sc_copyright());\ | ||
snprintf(context->version, MAX_NAME_LEN, "%s", g_sim->sc_version());\ | ||
}\ | ||
} | ||
BSMEDIT_EXPORT void bsm_sim_top(sim_context *context); | ||
BSMEDIT_EXPORT bool ctx_read(sim_object* obj); | ||
BSMEDIT_EXPORT bool ctx_write(sim_object* obj); | ||
BSMEDIT_EXPORT bool ctx_first_object(sim_object* obj); | ||
BSMEDIT_EXPORT bool ctx_next_object(sim_object* obj); | ||
BSMEDIT_EXPORT bool ctx_free_object(sim_object* obj); | ||
BSMEDIT_EXPORT void ctx_start(double duration, int unit); | ||
BSMEDIT_EXPORT void ctx_stop(); | ||
BSMEDIT_EXPORT double ctx_time(); | ||
BSMEDIT_EXPORT bool ctx_time_str(char* time); | ||
BSMEDIT_EXPORT typedef int(*bsm_callback)(int); | ||
BSMEDIT_EXPORT void ctx_set_callback(bsm_callback fun); | ||
BSMEDIT_EXPORT bool ctx_create_trace_file(sim_trace_file* t); | ||
BSMEDIT_EXPORT bool ctx_close_trace_file(sim_trace_file* t); | ||
BSMEDIT_EXPORT bool ctx_trace_file(sim_trace_file* t, sim_object* obj, | ||
sim_object* val, int trigger); | ||
BSMEDIT_EXPORT bool ctx_create_trace_buf(sim_trace_buf* t); | ||
BSMEDIT_EXPORT bool ctx_close_trace_buf(sim_trace_buf* t); | ||
BSMEDIT_EXPORT bool ctx_trace_buf(sim_trace_buf* t, sim_object* obj, | ||
sim_object* val, int trigger); | ||
BSMEDIT_EXPORT bool ctx_read_trace_buf(sim_trace_buf* t); | ||
BSMEDIT_EXPORT bool ctx_resize_trace_buf(sim_trace_buf* t); | ||
#endif // SRC_SYSC_BSM_BSM_H_ | ||
|
Oops, something went wrong.