# Path: sydpy/component.py
# class Component:
#     """Component class is the base class for all objects that are part of the user 
#     object hierarchy.    
#     """
#     
#     comp = {}
#     __cur_parent_class__ = None
#     
#     def __init__(self, name=None, parent=None, **kwargs):
#         """Instantiate a new compinit
#         
#         name    -- The name of the compinit
#         """
#         self.name = name
#         self._parent = parent
#         if parent is not None:
#             self._parent.comp[name] = self
#         self.comp = {}
# 
# #     @property
# #     def qname(self):
# #         if self.name:
# #             if self._parent:
# #                 return sep.join([self._parent.qname, self.name])
# # 
# #         return self.name
# 
#     def __repr__(self):
#         return self.name
# 
#     def __setitem__(self, key, val):
#         try:
#             val = sydsys().get_config(self.name, key)
#         except KeyError: 
#             pass
#         
#         self.comp[key] = val
#         
#         return val
#     
#     def __getitem__(self, key):
# #         return ddic[sep.join([self.qname, key])]
#         return self.comp[key]
# 
#     def inst(self, name, cls, *args, **kwargs):
#         try:
#             val = sydsys().get_config(self.name, name)
#             if not inspect.isclass(val):
#                 setattr(self, name, val)
#                 return val
#             else:
#                 cls = val
#         except KeyError: 
#             pass
#         
#         
#         if self.name:
#             qname = self.name + '.' + name
#         else:
#             qname = name
#         
#         obj = cls(qname, *args , **kwargs)
#         
#         self.comp[name] = obj
#         
#         return obj
#     
#     def search(self, pattern='*', of_type=None, depth=0, pattern_relative=True):
#         
#         if pattern_relative:
#             if self.name:
#                 pattern = sep.join([self.name, pattern])
# 
#         for _, c in self.comp.items():
#             if fnmatch.fnmatch(c.name, pattern):
#                 if (of_type is None) or isinstance(c, of_type):
#                     yield c
#                     
#                 if depth:
#                     yield from c.search(pattern, pattern_relative=False, depth=depth-1)
# 
# def compinit(func):
# 
#     def wrapper(self, name=None, parent=None, *args, **kwargs):
#         
#         if func.__name__ != '__init__':
#             raise Exception('Decorated function must be __init__().')
# 
#         params = all2kwargs(func, self, *args, name=name, parent=parent, **kwargs)
#         if self.__cur_parent_class__ is None:
#             if (parent is not None) and (name is not None):    
#             
#                 parent[name] = self
#                 qname = sep.join([parent.qname, name])
#                 
#                 sydsys()[qname] = self
#                 
#                 sys_conf = sydsys().get_all_attrs(qname)
#                 
#                 for n, v in sys_conf.items():
#                     params[n] = v
#                     
#     #             arg_names, varargs, varkw, defaults = (
#     #                                                    inspect.getargspec(func))
#     #             
#     #             if defaults:
#     #                 for arg_name, _ in zip(reversed(arg_names), reversed(defaults)):
#     #                     setattr(self, arg_name, params[arg_name])
#         
#             self.__cur_parent_class__ = self.__class__
#             
#         self.__cur_parent_class__ = self.__cur_parent_class__.__bases__[0] 
#         self.__cur_parent_class__.__init__(**params)
# 
#         func(**params)
#         
#     return wrapper
# 
# def sydsys(sid = 0):
#     return _system[sid]


from sydpy.component import Component, compinit, sydsys
from subprocess import Popen, PIPE
import string
import os
import itertools

wrapper_tmpl = string.Template("""
module wrap();

    import "DPI-C" pure function int xsimintf_init ();
    import "DPI-C" pure function string xsimintf_export (input string s);
    import "DPI-C" pure function string xsimintf_import ();
    import "DPI-C" pure function int xsimintf_delay ();
    
    ${port_definition}
    
    initial begin
        int      vals_read;
        automatic string       strimp;
        
        if (xsimintf_init()) begin
          $$finish;
        end
        $$dumpfile("xsim.vdc");
        $$dumpvars(${in_port_list}, ${out_port_list});
        strimp = xsimintf_import();
        $$display("INIT: %s", strimp);
        vals_read = $$sscanf(strimp, ${import_str_format}, ${in_port_list});
    end
    
    always #1 begin
        automatic int  vals_read;
        automatic int  delay;

# Path: sydpy/component.py
# class Component:
#     """Component class is the base class for all objects that are part of the user 
#     object hierarchy.    
#     """
#     
#     comp = {}
#     __cur_parent_class__ = None
#     
#     def __init__(self, name=None, parent=None, **kwargs):
#         """Instantiate a new compinit
#         
#         name    -- The name of the compinit
#         """
#         self.name = name
#         self._parent = parent
#         if parent is not None:
#             self._parent.comp[name] = self
#         self.comp = {}
# 
# #     @property
# #     def qname(self):
# #         if self.name:
# #             if self._parent:
# #                 return sep.join([self._parent.qname, self.name])
# # 
# #         return self.name
# 
#     def __repr__(self):
#         return self.name
# 
#     def __setitem__(self, key, val):
#         try:
#             val = sydsys().get_config(self.name, key)
#         except KeyError: 
#             pass
#         
#         self.comp[key] = val
#         
#         return val
#     
#     def __getitem__(self, key):
# #         return ddic[sep.join([self.qname, key])]
#         return self.comp[key]
# 
#     def inst(self, name, cls, *args, **kwargs):
#         try:
#             val = sydsys().get_config(self.name, name)
#             if not inspect.isclass(val):
#                 setattr(self, name, val)
#                 return val
#             else:
#                 cls = val
#         except KeyError: 
#             pass
#         
#         
#         if self.name:
#             qname = self.name + '.' + name
#         else:
#             qname = name
#         
#         obj = cls(qname, *args , **kwargs)
#         
#         self.comp[name] = obj
#         
#         return obj
#     
#     def search(self, pattern='*', of_type=None, depth=0, pattern_relative=True):
#         
#         if pattern_relative:
#             if self.name:
#                 pattern = sep.join([self.name, pattern])
# 
#         for _, c in self.comp.items():
#             if fnmatch.fnmatch(c.name, pattern):
#                 if (of_type is None) or isinstance(c, of_type):
#                     yield c
#                     
#                 if depth:
#                     yield from c.search(pattern, pattern_relative=False, depth=depth-1)
# 
# def compinit(func):
# 
#     def wrapper(self, name=None, parent=None, *args, **kwargs):
#         
#         if func.__name__ != '__init__':
#             raise Exception('Decorated function must be __init__().')
# 
#         params = all2kwargs(func, self, *args, name=name, parent=parent, **kwargs)
#         if self.__cur_parent_class__ is None:
#             if (parent is not None) and (name is not None):    
#             
#                 parent[name] = self
#                 qname = sep.join([parent.qname, name])
#                 
#                 sydsys()[qname] = self
#                 
#                 sys_conf = sydsys().get_all_attrs(qname)
#                 
#                 for n, v in sys_conf.items():
#                     params[n] = v
#                     
#     #             arg_names, varargs, varkw, defaults = (
#     #                                                    inspect.getargspec(func))
#     #             
#     #             if defaults:
#     #                 for arg_name, _ in zip(reversed(arg_names), reversed(defaults)):
#     #                     setattr(self, arg_name, params[arg_name])
#         
#             self.__cur_parent_class__ = self.__class__
#             
#         self.__cur_parent_class__ = self.__cur_parent_class__.__bases__[0] 
#         self.__cur_parent_class__.__init__(**params)
# 
#         func(**params)
#         
#     return wrapper
# 
# def sydsys(sid = 0):
#     return _system[sid]


# Path: sydpy/xsim.py
from sydpy.component import Component, compinit, sydsys
from subprocess import Popen, PIPE
import string
import os
import itertools


wrapper_tmpl = string.Template("""
module wrap();

    import "DPI-C" pure function int xsimintf_init ();
    import "DPI-C" pure function string xsimintf_export (input string s);
    import "DPI-C" pure function string xsimintf_import ();
    import "DPI-C" pure function int xsimintf_delay ();
    
    ${port_definition}
    
    initial begin
        int      vals_read;
        automatic string       strimp;
        
        if (xsimintf_init()) begin
          $$finish;
        end
        $$dumpfile("xsim.vdc");
        $$dumpvars(${in_port_list}, ${out_port_list});
        strimp = xsimintf_import();
        $$display("INIT: %s", strimp);
        vals_read = $$sscanf(strimp, ${import_str_format}, ${in_port_list});
    end
    
    always #1 begin
        automatic int  vals_read;
        automatic int  delay;

        automatic string       strimp;