Skip to content

Commit 5bf6119

Browse files
committed
Fix value type in _port.py (int, "X" or None is valid)
1 parent fdc6b1b commit 5bf6119

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## vx.x.x
44
- Fix validation "0", "1", "X" in yosys netlist
5+
- Fix value type in _port.py (int, "X" or None is valid)
56

67
## v0.12.0
78
- Use default audio output

src/digsim/circuit/_circuit.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from digsim.storage_model import CircuitDataClass, CircuitFileDataClass
1414

1515
from ._waves_writer import WavesWriter
16-
from .components.atoms import Component, DigsimException, PortOutDelta
16+
from .components.atoms import VALUE_TYPE, Component, DigsimException, PortOutDelta
1717

1818

1919
class CircuitError(DigsimException):
@@ -26,10 +26,10 @@ class CircuitEvent:
2626
delta events in the simulation.
2727
"""
2828

29-
def __init__(self, time_ns: int, port: PortOutDelta, value: int | str | None):
29+
def __init__(self, time_ns: int, port: PortOutDelta, value: VALUE_TYPE):
3030
self._time_ns: int = time_ns
3131
self._port: PortOutDelta = port
32-
self._value: int | str | None = value
32+
self._value: VALUE_TYPE = value
3333

3434
@property
3535
def time_ns(self) -> int:
@@ -42,15 +42,15 @@ def port(self) -> PortOutDelta:
4242
return self._port
4343

4444
@property
45-
def value(self) -> int | str | None:
45+
def value(self) -> VALUE_TYPE:
4646
"""Get the delta cycle value of this event"""
4747
return self._value
4848

4949
def is_same_event(self, port: PortOutDelta):
5050
"""Return True if the in the event is the same as"""
5151
return port == self._port
5252

53-
def update(self, time_ns: int, value: int | str | None):
53+
def update(self, time_ns: int, value: VALUE_TYPE):
5454
"""Update the event with a new time (ns) and a new value"""
5555
self._time_ns = time_ns
5656
self._value = value
@@ -231,7 +231,7 @@ def run_until(
231231
if stop_time_ns >= self._time_ns:
232232
self.run(ns=stop_time_ns - self._time_ns)
233233

234-
def add_event(self, port: PortOutDelta, value: int | str | None, propagation_delay_ns: int):
234+
def add_event(self, port: PortOutDelta, value: VALUE_TYPE, propagation_delay_ns: int):
235235
"""Add delta cycle event, this will also write values to .vcd file"""
236236
event_time_ns = self._time_ns + propagation_delay_ns
237237
# print(f"Add event {port.parent().name()}:{port.name()} => {value}")

src/digsim/circuit/components/atoms/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from ._digsim_exception import DigsimException # noqa: F401
1313
from ._port import ( # noqa: F401
14+
VALUE_TYPE,
1415
Port,
1516
PortConnectionError,
1617
PortIn,

src/digsim/circuit/components/atoms/_port.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
from __future__ import annotations
77

88
import abc
9+
from typing import Literal, Optional, Union
910

1011
from ._digsim_exception import DigsimException
1112

1213

14+
VALUE_TYPE = Optional[Union[int, Literal["X"]]]
15+
16+
1317
class PortConnectionError(DigsimException):
1418
"""Exception for illegal connections"""
1519

@@ -23,8 +27,8 @@ def __init__(self, parent, name: str, width: int = 1, output: bool = False):
2327
self._width: int = width # The bit-width of this port
2428
self._output: bool = output # Is this port an output port
2529
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
2832
self.init() # Initialize the port
2933

3034
def init(self):
@@ -39,12 +43,12 @@ def wired_ports(self) -> list[Port]:
3943
return self._wired_ports
4044

4145
@property
42-
def value(self) -> int | str | None:
46+
def value(self) -> VALUE_TYPE:
4347
"""Get the value of the port, can be "X" """
4448
return self._value
4549

4650
@value.setter
47-
def value(self, value: int | str | None):
51+
def value(self, value: VALUE_TYPE):
4852
"""Set the value of the port"""
4953
self.set_value(value)
5054

@@ -98,7 +102,7 @@ def parent(self):
98102
"""Get parent component"""
99103
return self._parent
100104

101-
def update_wires(self, value: int | str | None):
105+
def update_wires(self, value: VALUE_TYPE):
102106
"""Update connected wires (and self._value) with value"""
103107
if self._value == value:
104108
return
@@ -144,7 +148,7 @@ def is_falling_edge(self) -> bool:
144148
return falling_edge
145149

146150
@abc.abstractmethod
147-
def set_value(self, value: int | str | None):
151+
def set_value(self, value: VALUE_TYPE):
148152
"""Set value on port"""
149153

150154
@abc.abstractmethod
@@ -195,7 +199,7 @@ def __init__(self, parent, name: str, width: int = 1, output: bool = False):
195199
super().__init__(parent, name, width, output)
196200
self._port_driver: Port | None = None # The port that drives this port
197201

198-
def set_value(self, value: int | str | None):
202+
def set_value(self, value: VALUE_TYPE):
199203
if value != self.value:
200204
self.update_wires(value)
201205

@@ -221,7 +225,7 @@ class PortIn(PortWire):
221225
def __init__(self, parent, name: str, width: int = 1):
222226
super().__init__(parent, name, width, output=False)
223227

224-
def set_value(self, value: int | str | None):
228+
def set_value(self, value: VALUE_TYPE):
225229
super().set_value(value)
226230
self.parent().update()
227231

@@ -246,16 +250,16 @@ def set_delay_ns(self, delay_ns: int):
246250
"""Set port propagation delay"""
247251
self._delay_ns = delay_ns
248252

249-
def set_value(self, value: int | str | None):
253+
def set_value(self, value: VALUE_TYPE):
250254
self.parent().add_event(self, value, self._delay_ns)
251255

252-
def update_port(self, value: int | str | None):
256+
def update_port(self, value: VALUE_TYPE):
253257
"""Update the port output and the connected wires"""
254258
self.update_wires(value)
255259
if self._update_parent:
256260
self.parent().update()
257261

258-
def delta_cycle(self, value: int | str | None):
262+
def delta_cycle(self, value: VALUE_TYPE):
259263
"""Handle the delta cycle event from the circuit"""
260264
self.update_port(value)
261265

@@ -282,11 +286,11 @@ class PortOutImmediate(PortOutDelta):
282286
def __init__(self, parent, name: str, width: int = 1):
283287
super().__init__(parent, name, width)
284288

285-
def set_value(self, value: int | str | None):
289+
def set_value(self, value: VALUE_TYPE):
286290
self.parent().add_event(self, value, 0)
287291
super().update_port(value)
288292

289-
def delta_cycle(self, value: int | str | None):
293+
def delta_cycle(self, value: VALUE_TYPE):
290294
"""
291295
Do nothing here, the event is just used to updates waves in Circuit class
292296
"""
@@ -303,7 +307,7 @@ def __init__(self, parent, name: str, parent_port: PortMultiBitWire, output: boo
303307
super().__init__(parent, name, 1, output)
304308
self._parent_port = parent_port
305309

306-
def set_value(self, value: int | str | None):
310+
def set_value(self, value: VALUE_TYPE):
307311
super().set_value(value)
308312
self._parent_port.update_value_from_bits()
309313

@@ -333,7 +337,7 @@ def init(self):
333337
for bit in self._bits:
334338
bit.init()
335339

336-
def set_value(self, value: int | str | None):
340+
def set_value(self, value: VALUE_TYPE):
337341
if value is None or isinstance(value, str):
338342
return
339343
for bit_id, bit in enumerate(self._bits):
@@ -375,7 +379,7 @@ def update_value_from_bits(self):
375379
# Send event just to update waves
376380
self.parent().add_event(self, value, 0)
377381

378-
def delta_cycle(self, value: int | str | None):
382+
def delta_cycle(self, value: VALUE_TYPE):
379383
"""
380384
Do nothing here, the event passed in 'update_value_from_bits'
381385
is just used to updates waves in Circuit class

0 commit comments

Comments
 (0)