6
6
from __future__ import annotations
7
7
8
8
import abc
9
+ from typing import Literal , Optional , Union
9
10
10
11
from ._digsim_exception import DigsimException
11
12
12
13
14
+ VALUE_TYPE = Optional [Union [int , Literal ["X" ]]]
15
+
16
+
13
17
class PortConnectionError (DigsimException ):
14
18
"""Exception for illegal connections"""
15
19
@@ -23,8 +27,8 @@ def __init__(self, parent, name: str, width: int = 1, output: bool = False):
23
27
self ._width : int = width # The bit-width of this port
24
28
self ._output : bool = output # Is this port an output port
25
29
self ._wired_ports : list [Port ] = [] # The ports that this port drives
26
- self ._value : int | str | None = None # The value of this port
27
- self ._edge_detect_value : int | str | None = "X" # Last edge detect value
30
+ self ._value : VALUE_TYPE = None # The value of this port
31
+ self ._edge_detect_value : VALUE_TYPE = "X" # Last edge detect value
28
32
self .init () # Initialize the port
29
33
30
34
def init (self ):
@@ -39,12 +43,12 @@ def wired_ports(self) -> list[Port]:
39
43
return self ._wired_ports
40
44
41
45
@property
42
- def value (self ) -> int | str | None :
46
+ def value (self ) -> VALUE_TYPE :
43
47
"""Get the value of the port, can be "X" """
44
48
return self ._value
45
49
46
50
@value .setter
47
- def value (self , value : int | str | None ):
51
+ def value (self , value : VALUE_TYPE ):
48
52
"""Set the value of the port"""
49
53
self .set_value (value )
50
54
@@ -98,7 +102,7 @@ def parent(self):
98
102
"""Get parent component"""
99
103
return self ._parent
100
104
101
- def update_wires (self , value : int | str | None ):
105
+ def update_wires (self , value : VALUE_TYPE ):
102
106
"""Update connected wires (and self._value) with value"""
103
107
if self ._value == value :
104
108
return
@@ -144,7 +148,7 @@ def is_falling_edge(self) -> bool:
144
148
return falling_edge
145
149
146
150
@abc .abstractmethod
147
- def set_value (self , value : int | str | None ):
151
+ def set_value (self , value : VALUE_TYPE ):
148
152
"""Set value on port"""
149
153
150
154
@abc .abstractmethod
@@ -195,7 +199,7 @@ def __init__(self, parent, name: str, width: int = 1, output: bool = False):
195
199
super ().__init__ (parent , name , width , output )
196
200
self ._port_driver : Port | None = None # The port that drives this port
197
201
198
- def set_value (self , value : int | str | None ):
202
+ def set_value (self , value : VALUE_TYPE ):
199
203
if value != self .value :
200
204
self .update_wires (value )
201
205
@@ -221,7 +225,7 @@ class PortIn(PortWire):
221
225
def __init__ (self , parent , name : str , width : int = 1 ):
222
226
super ().__init__ (parent , name , width , output = False )
223
227
224
- def set_value (self , value : int | str | None ):
228
+ def set_value (self , value : VALUE_TYPE ):
225
229
super ().set_value (value )
226
230
self .parent ().update ()
227
231
@@ -246,16 +250,16 @@ def set_delay_ns(self, delay_ns: int):
246
250
"""Set port propagation delay"""
247
251
self ._delay_ns = delay_ns
248
252
249
- def set_value (self , value : int | str | None ):
253
+ def set_value (self , value : VALUE_TYPE ):
250
254
self .parent ().add_event (self , value , self ._delay_ns )
251
255
252
- def update_port (self , value : int | str | None ):
256
+ def update_port (self , value : VALUE_TYPE ):
253
257
"""Update the port output and the connected wires"""
254
258
self .update_wires (value )
255
259
if self ._update_parent :
256
260
self .parent ().update ()
257
261
258
- def delta_cycle (self , value : int | str | None ):
262
+ def delta_cycle (self , value : VALUE_TYPE ):
259
263
"""Handle the delta cycle event from the circuit"""
260
264
self .update_port (value )
261
265
@@ -282,11 +286,11 @@ class PortOutImmediate(PortOutDelta):
282
286
def __init__ (self , parent , name : str , width : int = 1 ):
283
287
super ().__init__ (parent , name , width )
284
288
285
- def set_value (self , value : int | str | None ):
289
+ def set_value (self , value : VALUE_TYPE ):
286
290
self .parent ().add_event (self , value , 0 )
287
291
super ().update_port (value )
288
292
289
- def delta_cycle (self , value : int | str | None ):
293
+ def delta_cycle (self , value : VALUE_TYPE ):
290
294
"""
291
295
Do nothing here, the event is just used to updates waves in Circuit class
292
296
"""
@@ -303,7 +307,7 @@ def __init__(self, parent, name: str, parent_port: PortMultiBitWire, output: boo
303
307
super ().__init__ (parent , name , 1 , output )
304
308
self ._parent_port = parent_port
305
309
306
- def set_value (self , value : int | str | None ):
310
+ def set_value (self , value : VALUE_TYPE ):
307
311
super ().set_value (value )
308
312
self ._parent_port .update_value_from_bits ()
309
313
@@ -333,7 +337,7 @@ def init(self):
333
337
for bit in self ._bits :
334
338
bit .init ()
335
339
336
- def set_value (self , value : int | str | None ):
340
+ def set_value (self , value : VALUE_TYPE ):
337
341
if value is None or isinstance (value , str ):
338
342
return
339
343
for bit_id , bit in enumerate (self ._bits ):
@@ -375,7 +379,7 @@ def update_value_from_bits(self):
375
379
# Send event just to update waves
376
380
self .parent ().add_event (self , value , 0 )
377
381
378
- def delta_cycle (self , value : int | str | None ):
382
+ def delta_cycle (self , value : VALUE_TYPE ):
379
383
"""
380
384
Do nothing here, the event passed in 'update_value_from_bits'
381
385
is just used to updates waves in Circuit class
0 commit comments