Skip to content

Commit

Permalink
Add bsmedit support.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 27 changed files with 7,205 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
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*/
*~
15 changes: 15 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ set(SYSTEMC_CORE_SRC
sysc/utils/sc_stop_here.cpp
sysc/utils/sc_utils_ids.cpp
sysc/utils/sc_vector.cpp
sysc/bsm/bsm_sim_context_impl.cpp
sysc/bsm/sc_bsm_trace.cpp
sysc/bsm/sc_bsm_trace_buf.cpp
sysc/bsm/bsm.cpp
# TLM sources
tlm_core/tlm_2/tlm_generic_payload/tlm_gp.cpp
tlm_core/tlm_2/tlm_generic_payload/tlm_phase.cpp
Expand Down Expand Up @@ -355,6 +359,17 @@ set(SYSTEMC_CORE_SRC
sysc/utils/sc_temporary.h
sysc/utils/sc_utils_ids.h
sysc/utils/sc_vector.h
sysc/bsm/bsm_sysc.h
sysc/bsm/reg_type.h
sysc/bsm/bsm_object.h
sysc/bsm/bsm_buffer_intf.h
sysc/bsm/bsm_sim_context.h
sysc/bsm/bsm_sim_context_impl.h
sysc/bsm/sc_bsm_trace.h
sysc/bsm/sc_bsm_trace_buf.h
sysc/bsm/bsm.h
sysc/bsm/xsc_property.h
sysc/bsm/xsc_array.h
systemc
systemc.h
# TLM headers
Expand Down
1 change: 1 addition & 0 deletions src/sysc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ include communication/files.am
include datatypes/files.am
include tracing/files.am
include utils/files.am
include bsm/files.am

# include individual third-party packages
if WANT_QT_THREADS
Expand Down
260 changes: 260 additions & 0 deletions src/sysc/bsm/bsm.cpp
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);
}

77 changes: 77 additions & 0 deletions src/sysc/bsm/bsm.h
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_

Loading

0 comments on commit 801866c

Please sign in to comment.