def connect(self, left_port, right_port): """Structurally connect a Signal to another Signal or a constant, or two PortBundles with the same interface. Ports structurally connected with s.connect are guaranteed to have the same value during simulation: >>> s.connect( s.port_a, s.port_b ) This works for slices as well: >>> s.connect( s.port_c[0:4], s.port_d[4:8] ) A signal connected to a constant will be tied to that value: >>> s.connect( s.port_e, 8 ) Several Signals can be structurally connected with a single statement by encapsulating them in PortBundles: >>> s.connect( s.my_bundle_a, s.my_bundle_b ) """ # Throw an error if connect() is used on two wires. if isinstance(left_port, Wire) and isinstance(right_port, Wire): e = ('Connecting two Wire signals is not supported!\n' 'If you are certain this is something you really need to do, ' 'use connect_wire instead:\n\n ' ' # translation will fail if directionality is wrong!!!\n' ' connect_wire( dest = <s.out>, src = <s.in_> )') frame, filename, lineno, func_name, lines, idx = inspect.stack()[1] msg = '{}\n\nLine: {} in File: {}\n>'.format(e, lineno, filename) msg += '>'.join(lines) raise PyMTLConnectError(msg) # Try to connect the two signals/portbundles try: if isinstance(left_port, PortBundle): self._connect_bundle(left_port, right_port) else: self._connect_signal(left_port, right_port) # Throw a helpful error on failure except PyMTLConnectError as e: frame, filename, lineno, func_name, lines, idx = inspect.stack()[1] msg = '{}\n\nLine: {} in File: {}\n>'.format(e, lineno, filename) msg += '>'.join(lines) raise PyMTLConnectError(msg)
def connect_dict(self, connections): """Structurally connect Signals given a dictionary mapping. This provides a more concise syntax for interconnecting large numbers of Signals by using a dictionary to specify Signal-to-Signal connection mapping. >>> s.connect_dict({ >>> s.mod.a : s.a, >>> s.mod.b : 0b11, >>> s.mod.c : s.x[4:5], >>> }) """ for left_port, right_port in connections.iteritems(): try: self.connect(left_port, right_port) # Throw a helpful error if any pairwise connection fails except PyMTLConnectError as e: frame, filename, lineno, func_name, lines, idx = inspect.stack( )[1] msg = '{}\n\nLine: {} in File: {}\n>'.format( e, lineno, filename) msg += '>'.join(lines) raise PyMTLConnectError(msg)
def connect_pairs(self, *connections): """Structurally connect pairs of signals. This provides a more concise syntax for interconnecting large numbers of Signals by allowing the user to supply a series of signals which are to be connected pairwise. >>> s.connect_pairs( >>> s.mod.a, s.a, >>> s.mod.b, 0b11, >>> s.mod.c, s.x[4:5], >>> ) Alternatively, a list of signals can be created and passed in: >>> s.connect_pairs( *signal_list ) """ # Throw an error if user tries to connect an odd number of signals if len(connections) & 1: e = 'An odd number of signals were provided!' frame, filename, lineno, func_name, lines, idx = inspect.stack()[1] msg = '{}\n\nLine: {} in File: {}\n>'.format(e, lineno, filename) msg += '>'.join(lines) raise PyMTLConnectError(msg) # Iterate through signals pairwise citer = iter(connections) for left_port, right_port in zip(citer, citer): try: self.connect(left_port, right_port) # Throw a helpful error if any pairwise connection fails except PyMTLConnectError as e: frame, filename, lineno, func_name, lines, idx = inspect.stack( )[1] msg = '{}\n\nLine: {} in File: {}\n>'.format( e, lineno, filename) msg += '>'.join(lines) raise PyMTLConnectError(msg)