Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
B-ASIC Port Module.
TODO: More info.
"""
from b_asic.signal import Signal
from abc import ABC, abstractmethod
from typing import NewType, Optional, List, Dict, final
PortId = NewType("PortId", int)
class Port(ABC):
"""
Abstract port class.
TODO: More info.
"""
_identifier: PortId
def __init__(self, identifier: PortId):
"""
Construct a Port.
"""
self._identifier = identifier
@final
def identifier(self) -> PortId:
"""
Get the unique identifier.
"""
return self._identifier
@abstractmethod
def signals(self) -> List[Signal]:
"""
Get a list of all connected signals.
"""
pass
@abstractmethod
def signal_count(self) -> int:
"""
Get the number of connected signals.
"""
pass
@abstractmethod
def signal(self, i: int = 0) -> Signal:
"""
Get the connected signal at index i.
"""
pass
@abstractmethod
def connect(self, signal: Signal) -> None:
"""
Connect a signal.
"""
pass
@abstractmethod
def disconnect(self, i: int = 0) -> None:
"""
Disconnect a signal.
"""
pass
# TODO: More stuff.
class InputPort(Port):
"""
Input port.
TODO: More info.
"""
_source_signal: Optional[Signal]
def __init__(self, identifier: PortId):
"""
Construct an InputPort.
"""
super().__init__(identifier)
self._source_signal = None
@final
def signals(self) -> List[Signal]:
return [] if self._source_signal == None else [self._source_signal]
@final
def signal_count(self) -> int:
return 0 if self._source_signal == None else 1
@final
def signal(self, i: int = 0) -> Signal:
assert i >= 0 and i < self.signal_count() # TODO: Error message.
assert self._source_signal != None # TODO: Error message.
return self._source_signal
@final
def connect(self, signal: Signal) -> None:
self._source_signal = signal
@final
def disconnect(self, i: int = 0) -> None:
assert i >= 0 and i < self.signal_count() # TODO: Error message.
self._source_signal = None
# TODO: More stuff.
class OutputPort(Port):
"""
Output port.
TODO: More info.
"""
_destination_signals: List[Signal]
def __init__(self, identifier: PortId):
"""
Construct an OutputPort.
"""
super().__init__(identifier)
self._destination_signals = []
@final
def signals(self) -> List[Signal]:
return self._destination_signals.copy()
@final
def signal_count(self) -> int:
return len(self._destination_signals)
@final
def signal(self, i: int = 0) -> Signal:
assert i >= 0 and i < self.signal_count() # TODO: Error message.
return self._destination_signals[i]
@final
def connect(self, signal: Signal) -> None:
assert signal not in self._destination_signals # TODO: Error message.
self._destination_signals.append(signal)
@final
def disconnect(self, i: int = 0) -> None:
assert i >= 0 and i < self.signal_count() # TODO: Error message.
del self._destination_signals[i]
# TODO: More stuff.