141 lines
4.1 KiB
Python
141 lines
4.1 KiB
Python
'''
|
|
Created on 02.01.2019
|
|
|
|
@author: eyck
|
|
'''
|
|
from cppyy import gbl as cpp
|
|
from builtins import getattr
|
|
import re
|
|
from enum import Enum
|
|
import logging
|
|
|
|
class Mode(Enum):
|
|
SIM = 1
|
|
BUILD = 2
|
|
|
|
mode=Mode.SIM
|
|
|
|
class Simulation(object):
|
|
|
|
@staticmethod
|
|
def run():
|
|
cpp.sc_core.sc_start()
|
|
if not cpp.sc_core.sc_end_of_simulation_invoked():
|
|
cpp.sc_core.sc_stop()
|
|
|
|
@staticmethod
|
|
def setup(log_level = logging.WARNING):
|
|
try:
|
|
if log_level >= logging.FATAL:
|
|
cpp.scc.init_logging(cpp.logging.FATAL, False)
|
|
elif log_level >= logging.ERROR:
|
|
cpp.scc.init_logging(cpp.logging.ERROR, False)
|
|
elif log_level >= logging.WARNING:
|
|
cpp.scc.init_logging(cpp.logging.WARNING, False)
|
|
elif log_level >= logging.INFO:
|
|
cpp.scc.init_logging(cpp.logging.INFO, False)
|
|
elif log_level >= logging.DEBUG:
|
|
cpp.scc.init_logging(cpp.logging.DEBUG, False)
|
|
except Exception: # fall back: use basic SystemC logging setup
|
|
verb_lut={
|
|
logging.FATAL:100, #SC_LOW
|
|
logging.ERROR: 200, #SC_MEDIUM
|
|
logging.WARNING: 300, #SC_HIGH
|
|
logging.INFO: 400, #SC_FULL
|
|
logging.DEBUG: 500 #SC_DEBUG
|
|
}
|
|
cpp.sc_core.sc_report_handler.set_verbosity_level(verb_lut[log_level]);
|
|
cpp.sc_core.sc_report_handler.set_actions(cpp.sc_core.SC_ID_MORE_THAN_ONE_SIGNAL_DRIVER_, cpp.sc_core.SC_DO_NOTHING);
|
|
try:
|
|
cpp.scc.init_cci("GlobalBroker")
|
|
except Exception:
|
|
pass
|
|
|
|
@staticmethod
|
|
def configure(name="", enable_vcd=False):
|
|
if len(name) and os.path.isfile(name):
|
|
Simulation.cfg = cpp.scc.configurer(cpp.std.string(name));
|
|
if enable_vcd:
|
|
trace_name = os.path.basename(name)
|
|
Simulation.trace = cpp.scc.configurable_tracer(trace_name, 1, True, True)
|
|
Simulation.trace.add_control()
|
|
else:
|
|
if enable_vcd:
|
|
Simulation.trace = cpp.scc.tracer('vcd_trace', 1, True)
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
class Module(object):
|
|
'''
|
|
classdocs
|
|
'''
|
|
|
|
def __init__(self, clazz):
|
|
self.cppclazz=clazz
|
|
self.instance=None
|
|
|
|
def __getattr__(self, attr):
|
|
if self.instance is None:
|
|
raise AttributeError
|
|
return getattr(self.instance, attr)
|
|
|
|
def create(self, name):
|
|
self.instance = self.cppclazz(cpp.sc_core.sc_module_name(str(name)))
|
|
return self
|
|
|
|
class Connection(object):
|
|
'''
|
|
classdocs
|
|
'''
|
|
def __init__(self):
|
|
self.source=None
|
|
self.targets=[]
|
|
|
|
def src(self, module_port):
|
|
self.source=module_port
|
|
return self
|
|
|
|
def sink(self, module_port):
|
|
self.targets.append(module_port)
|
|
self.source.bind(module_port)
|
|
return self
|
|
|
|
class Signal(Connection):
|
|
'''
|
|
classdocs
|
|
'''
|
|
|
|
_sc_inout_re = re.compile(r'^sc_core::sc_(?:_in)?out<(.*)>$')
|
|
_sc_port_re = re.compile(r'^sc_core::sc_port<[^<]*<(.*)>>$')
|
|
|
|
|
|
def __init__(self, name=None):
|
|
Connection.__init__(self)
|
|
self.name=name
|
|
self.signal=None
|
|
self.data_type=None
|
|
|
|
def src(self, module_port):
|
|
self.source=module_port
|
|
port_class_name=type(module_port).__cppname__
|
|
match = self._sc_inout_re.match(port_class_name)
|
|
if match:
|
|
self.data_type=match.group(1)
|
|
else:
|
|
match = self._sc_port_re.match(port_class_name)
|
|
if match:
|
|
self.data_type=match.group(1)
|
|
if self.data_type is None:
|
|
raise AttributeError;
|
|
py_dt_name=self.data_type.replace("::", ".").replace("<", "[").replace(">", "]")
|
|
self.signal = eval("cpp.sc_core.sc_signal[cpp.%s](self.name)" % py_dt_name)
|
|
module_port.bind(self.signal)
|
|
return self
|
|
|
|
def sink(self, module_port):
|
|
self.targets.append(module_port)
|
|
module_port.bind(self.signal)
|
|
return self
|
|
|