Skip to content
Snippets Groups Projects
signal.py 3.91 KiB
Newer Older
  • Learn to ignore specific revisions
  • """@package docstring
    
    B-ASIC Signal Module.
    """
    
    from typing import Optional, Iterable, TYPE_CHECKING
    
    from b_asic.graph_component import GraphComponent, AbstractGraphComponent, TypeName, Name
    
    Kevin Scott's avatar
    Kevin Scott committed
    if TYPE_CHECKING:
    
        from b_asic.port import InputPort, OutputPort
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
        """A connection between two ports."""
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
        _source: Optional["OutputPort"]
        _destination: Optional["InputPort"]
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
    
    
        def __init__(self, source: Optional["OutputPort"] = None, destination: Optional["InputPort"] = None, bits: Optional[int] = None, name: Name = ""):
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
            self._source = None
            self._destination = None
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
    
    
        @classmethod
        def type_name(cls) -> TypeName:
    
            return "s"
    
        @property
        def neighbors(self) -> Iterable[GraphComponent]:
            return [p.operation for p in [self.source, self.destination] if p is not None]
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
        @property
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
        def source(self) -> Optional["OutputPort"]:
    
            """Return the source OutputPort of the signal."""
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
            return self._source
    
        @property
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
        def destination(self) -> Optional["InputPort"]:
    
            """Return the destination "InputPort" of the signal."""
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
            return self._destination
    
    
        def set_source(self, src: "OutputPort") -> None:
    
            """Disconnect the previous source OutputPort of the signal and
            connect to the entered source OutputPort. Also connect the entered
            source port to the signal if it hasn't already been connected.
    
            Keyword arguments:
            - src: OutputPort to connect as source to the signal.
            """
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
            if src is not self._source:
                self.remove_source()
                self._source = src
                if self not in src.signals:
                    src.add_signal(self)
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
    
    
        def set_destination(self, dest: "InputPort") -> None:
    
            """Disconnect the previous destination InputPort of the signal and
            connect to the entered destination InputPort. Also connect the entered
            destination port to the signal if it hasn't already been connected.
    
            Keywords argments:
            - dest: InputPort to connect as destination to the signal.
            """
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
            if dest is not self._destination:
                self.remove_destination()
                self._destination = dest
                if self not in dest.signals:
                    dest.add_signal(self)
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
    
    
            """Disconnect the source OutputPort of the signal. If the source port
            still is connected to this signal then also disconnect the source port."""
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
            src = self._source
            if src is not None:
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
                if self in src.signals:
                    src.remove_signal(self)
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
    
    
            """Disconnect the destination InputPort of the signal."""
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
            dest = self._destination
            if dest is not None:
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
                if self in dest.signals:
                    dest.remove_signal(self)
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
        def dangling(self) -> bool:
            """Returns true if the signal is missing either a source or a destination,
    
    Ivar Härnqvist's avatar
    Ivar Härnqvist committed
            return self._source is None or self._destination is None
    
    
        @property
        def bits(self) -> Optional[int]:
            """Get the number of bits that this operations using this signal as an input should truncate received values to.
            None = unlimited."""
            return self.param("bits")
    
        @bits.setter
        def bits(self, bits: Optional[int]) -> None:
            """Set the number of bits that operations using this signal as an input should truncate received values to.
            None = unlimited."""
    
            assert bits is None or (isinstance(bits, int)
                                    and bits >= 0), "Bits must be non-negative."
            self.set_param("bits", bits)