PySysC/pysysc/structural.py

81 lines
2.0 KiB
Python

'''
Created on 02.01.2019
@author: eyck
'''
from cppyy import gbl as cpp
from builtins import getattr
import re
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 Signal(object):
'''
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):
self.name=name
self.source=None
self.targets=[]
self.signal=None
self.sig_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.sig_data_type=match.group(1)
else:
match = self._sc_port_re.match(port_class_name)
if match:
self.sig_data_type=match.group(1)
if self.sig_data_type is None:
raise AttributeError;
py_dt_name=self.sig_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
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