digsim-engine 1.13.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- digsim/circuit/__init__.py +7 -0
- digsim/circuit/_circuit.py +764 -0
- digsim/circuit/_native.py +1029 -0
- digsim/circuit/_waves_writer.py +131 -0
- digsim/circuit/components/__init__.py +52 -0
- digsim/circuit/components/_arithmetic.py +266 -0
- digsim/circuit/components/_bus_bits.py +103 -0
- digsim/circuit/components/_button.py +61 -0
- digsim/circuit/components/_buzzer.py +64 -0
- digsim/circuit/components/_circuit_port.py +293 -0
- digsim/circuit/components/_clock.py +105 -0
- digsim/circuit/components/_counter.py +147 -0
- digsim/circuit/components/_decoder.py +140 -0
- digsim/circuit/components/_dip_switch.py +91 -0
- digsim/circuit/components/_flip_flops.py +162 -0
- digsim/circuit/components/_gates.py +408 -0
- digsim/circuit/components/_hd44780.py +451 -0
- digsim/circuit/components/_hd44780_rom.py +118 -0
- digsim/circuit/components/_hexdigit.py +107 -0
- digsim/circuit/components/_ic.py +96 -0
- digsim/circuit/components/_label_wire.py +204 -0
- digsim/circuit/components/_led.py +66 -0
- digsim/circuit/components/_led_bar.py +67 -0
- digsim/circuit/components/_logic_analyzer.py +111 -0
- digsim/circuit/components/_memory.py +517 -0
- digsim/circuit/components/_memstdout.py +60 -0
- digsim/circuit/components/_note.py +40 -0
- digsim/circuit/components/_on_off_switch.py +64 -0
- digsim/circuit/components/_probe.py +90 -0
- digsim/circuit/components/_seven_segment.py +47 -0
- digsim/circuit/components/_splitter.py +214 -0
- digsim/circuit/components/_static_level.py +54 -0
- digsim/circuit/components/_static_value.py +97 -0
- digsim/circuit/components/_subcircuit.py +439 -0
- digsim/circuit/components/_tristate_buffer.py +81 -0
- digsim/circuit/components/_verilog_component.py +26 -0
- digsim/circuit/components/_yosys_atoms.py +1373 -0
- digsim/circuit/components/_yosys_component.py +1126 -0
- digsim/circuit/components/_yosys_functional.py +1714 -0
- digsim/circuit/components/atoms/__init__.py +28 -0
- digsim/circuit/components/atoms/_component.py +329 -0
- digsim/circuit/components/atoms/_digsim_exception.py +8 -0
- digsim/circuit/components/atoms/_port.py +708 -0
- digsim/circuit/components/ic/4017.json +1062 -0
- digsim/circuit/components/ic/4021.json +286 -0
- digsim/circuit/components/ic/4029.json +1447 -0
- digsim/circuit/components/ic/4094.json +584 -0
- digsim/circuit/components/ic/4511.json +1071 -0
- digsim/circuit/components/ic/74138.json +763 -0
- digsim/circuit/components/ic/74139.json +610 -0
- digsim/circuit/components/ic/74148.json +1368 -0
- digsim/circuit/components/ic/74151.json +303 -0
- digsim/circuit/components/ic/74157.json +248 -0
- digsim/circuit/components/ic/74161.json +674 -0
- digsim/circuit/components/ic/74162.json +631 -0
- digsim/circuit/components/ic/74163.json +673 -0
- digsim/circuit/components/ic/74164.json +267 -0
- digsim/circuit/components/ic/74165.json +319 -0
- digsim/circuit/components/ic/74173.json +403 -0
- digsim/circuit/components/ic/74181.json +2002 -0
- digsim/circuit/components/ic/74182.json +893 -0
- digsim/circuit/components/ic/74193.json +1386 -0
- digsim/circuit/components/ic/74194.json +919 -0
- digsim/circuit/components/ic/74245.json +466 -0
- digsim/circuit/components/ic/74283.json +732 -0
- digsim/circuit/components/ic/74374.json +395 -0
- digsim/circuit/components/ic/7448.json +834 -0
- digsim/circuit/components/ic/74574.json +395 -0
- digsim/circuit/components/ic/74595.json +603 -0
- digsim/circuit/components/ic/7485.json +1042 -0
- digsim/storage_model/__init__.py +19 -0
- digsim/storage_model/_app.py +159 -0
- digsim/storage_model/_circuit.py +266 -0
- digsim/synth/__init__.py +12 -0
- digsim/synth/__main__.py +111 -0
- digsim/synth/_synthesis.py +529 -0
- digsim/utils/__init__.py +25 -0
- digsim/utils/_netlist_graph.py +199 -0
- digsim/utils/_yosys_netlist.py +251 -0
- digsim/verilog/__init__.py +19 -0
- digsim/verilog/__main__.py +78 -0
- digsim/verilog/_formatter.py +225 -0
- digsim/verilog/_lexer.py +218 -0
- digsim_engine-1.13.0.dist-info/METADATA +59 -0
- digsim_engine-1.13.0.dist-info/RECORD +88 -0
- digsim_engine-1.13.0.dist-info/WHEEL +5 -0
- digsim_engine-1.13.0.dist-info/licenses/LICENSE.md +32 -0
- digsim_engine-1.13.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2023-2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Module for creating a yosys component in the ic folder
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from ._yosys_component import YosysComponent
|
|
14
|
+
from .atoms import ComponentException, Parameter, ParameterValue
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from digsim.circuit import Circuit
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# A short description of each bundled IC, for the IC selector of the GUI
|
|
22
|
+
IC_DESCRIPTIONS = {
|
|
23
|
+
"4017": "Decade counter with 10 decoded outputs",
|
|
24
|
+
"4021": "8-stage parallel-in, serial-out shift register",
|
|
25
|
+
"4029": "Presettable up/down counter, binary or decade",
|
|
26
|
+
"4094": "8-stage shift-and-store bus register, tristate outputs",
|
|
27
|
+
"4511": "BCD to 7-segment latch, decoder and driver",
|
|
28
|
+
"7448": "BCD to 7-segment decoder",
|
|
29
|
+
"7485": "4-bit magnitude comparator",
|
|
30
|
+
"74138": "3-to-8 line decoder, demultiplexer (demux)",
|
|
31
|
+
"74139": "Dual 2-to-4 line decoder, demultiplexer (demux)",
|
|
32
|
+
"74148": "8-to-3 line priority encoder",
|
|
33
|
+
"74151": "8-to-1 multiplexer (mux), data selector",
|
|
34
|
+
"74157": "Quad 2-to-1 multiplexer (mux), data selector",
|
|
35
|
+
"74161": "Synchronous 4-bit binary counter with parallel load",
|
|
36
|
+
"74162": "Synchronous decade counter with parallel load",
|
|
37
|
+
"74163": "Synchronous 4-bit binary counter, synchronous clear",
|
|
38
|
+
"74164": "8-bit serial-in, parallel-out shift register",
|
|
39
|
+
"74165": "8-bit parallel-in, serial-out shift register",
|
|
40
|
+
"74173": "4-bit D register with tristate outputs",
|
|
41
|
+
"74181": "4-bit arithmetic logic unit (ALU)",
|
|
42
|
+
"74182": "Look-ahead carry generator for the 74181",
|
|
43
|
+
"74193": "Up/down binary counter with parallel load",
|
|
44
|
+
"74194": "4-bit bidirectional universal shift register",
|
|
45
|
+
"74245": "Octal bus transceiver, tristate",
|
|
46
|
+
"74283": "4-bit binary full adder",
|
|
47
|
+
"74374": "Octal D flip-flops with tristate outputs",
|
|
48
|
+
"74574": "Octal D flip-flops with tristate outputs (flow-through pinout)",
|
|
49
|
+
"74595": "8-bit shift register with output register",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class IntegratedCircuit(YosysComponent):
|
|
54
|
+
"""
|
|
55
|
+
A classic logic chip, from the Yosys netlists in digsim/circuit/components/ic. It is
|
|
56
|
+
named after the netlist's module, like ttl_74162.
|
|
57
|
+
|
|
58
|
+
Ports:
|
|
59
|
+
The ports of the chip.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
circuit: The circuit to add the component to.
|
|
63
|
+
ic_name: The netlist's file name without .json, like "74162" or "7448".
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
def __init__(self, circuit: Circuit, ic_name: str | None = None) -> None:
|
|
67
|
+
if ic_name is None:
|
|
68
|
+
raise ComponentException("No IC selected")
|
|
69
|
+
path = f"{self.folder()}/{ic_name}.json"
|
|
70
|
+
super().__init__(circuit, path=path)
|
|
71
|
+
self.parameter_set("ic_name", ic_name)
|
|
72
|
+
|
|
73
|
+
@classmethod
|
|
74
|
+
def folder(cls) -> str:
|
|
75
|
+
"""Get predefined IC folder"""
|
|
76
|
+
return str(Path(__file__).parent / "ic")
|
|
77
|
+
|
|
78
|
+
@classmethod
|
|
79
|
+
def available(cls) -> dict[str, str]:
|
|
80
|
+
"""The ICs in the IC folder, with their descriptions (empty if there is none)"""
|
|
81
|
+
return {
|
|
82
|
+
path.stem: IC_DESCRIPTIONS.get(path.stem, "")
|
|
83
|
+
for path in sorted(Path(cls.folder()).glob("*.json"))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
def settings_to_dict(self) -> dict[str, ParameterValue]:
|
|
87
|
+
return {"ic_name": self.parameter_get("ic_name")}
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
91
|
+
return {
|
|
92
|
+
"ic_name": {
|
|
93
|
+
"type": "ic_name",
|
|
94
|
+
"description": "Select IC",
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2023-2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Label Wire components module. A label belongs to the circuit file of its label wires: the
|
|
6
|
+
top-level circuit, or a subcircuit instance (each instance has labels of its own).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import warnings
|
|
12
|
+
from collections.abc import Iterator
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
from typing import TYPE_CHECKING
|
|
15
|
+
|
|
16
|
+
from .atoms import Component, DigsimException, Parameter, PortWire
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from digsim.circuit import Circuit
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# The subcircuits whose parts are being created, the innermost last: a part is added to
|
|
24
|
+
# its subcircuit after it is created, a label wire that is created belongs to this one
|
|
25
|
+
_creating_parts_of: list[Component] = []
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@contextmanager
|
|
29
|
+
def label_scope(subcircuit: Component) -> Iterator[None]:
|
|
30
|
+
"""The label wires created in it are parts of a subcircuit, with its labels"""
|
|
31
|
+
_creating_parts_of.append(subcircuit)
|
|
32
|
+
try:
|
|
33
|
+
yield
|
|
34
|
+
finally:
|
|
35
|
+
_creating_parts_of.pop()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _new_scope() -> Component | None:
|
|
39
|
+
"""The scope of a label wire that is created now, None for the top-level circuit"""
|
|
40
|
+
return _creating_parts_of[-1] if _creating_parts_of else None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _drivers(circuit: Circuit, scope: Component | None) -> dict[str, LabelWireIn]:
|
|
44
|
+
"""The label wire drivers of a scope (a subcircuit, None for the top level), by label"""
|
|
45
|
+
return {
|
|
46
|
+
comp.label(): comp
|
|
47
|
+
for comp in circuit.components
|
|
48
|
+
if isinstance(comp, LabelWireIn) and comp.parent is scope
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _sinks(circuit: Circuit, label: str, scope: Component | None) -> list[LabelWireOut]:
|
|
53
|
+
"""The label wire sinks of a scope with the label"""
|
|
54
|
+
return [
|
|
55
|
+
comp
|
|
56
|
+
for comp in circuit.components
|
|
57
|
+
if isinstance(comp, LabelWireOut) and comp.label() == label and comp.parent is scope
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class LabelWireIn(Component):
|
|
62
|
+
"""
|
|
63
|
+
A Wire Sink: passes its input to every LabelWireOut with the same label, without a drawn wire.
|
|
64
|
+
|
|
65
|
+
Ports:
|
|
66
|
+
<label>: Input, named like the label, `width` bits.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
circuit: The circuit to add the component to.
|
|
70
|
+
name: The component name, the class name if not given.
|
|
71
|
+
label: The label.
|
|
72
|
+
width: The number of bits.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __init__(
|
|
76
|
+
self, circuit: Circuit, name: str | None = None, label: str = "", width: int = 1
|
|
77
|
+
) -> None:
|
|
78
|
+
# Check before the component is added to the circuit
|
|
79
|
+
if label in _drivers(circuit, _new_scope()):
|
|
80
|
+
raise DigsimException(f"Wire Driver '{label}' already added")
|
|
81
|
+
super().__init__(circuit, name)
|
|
82
|
+
self._label = label
|
|
83
|
+
self._port_in = PortWire(self, self._label, width=width)
|
|
84
|
+
self.add_port(self._port_in)
|
|
85
|
+
self.parameter_set("label", self._label)
|
|
86
|
+
self.parameter_set("width", width)
|
|
87
|
+
|
|
88
|
+
def label(self) -> str:
|
|
89
|
+
"""Return Label"""
|
|
90
|
+
return self._label
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def port_in(self) -> PortWire:
|
|
94
|
+
"""The port that drives the sinks"""
|
|
95
|
+
return self._port_in
|
|
96
|
+
|
|
97
|
+
def remove_connections(self) -> None:
|
|
98
|
+
for sink in _sinks(self.circuit, self._label, self.parent):
|
|
99
|
+
self._port_in.disconnect(sink.port_out)
|
|
100
|
+
super().remove_connections()
|
|
101
|
+
|
|
102
|
+
@classmethod
|
|
103
|
+
def _label_parameters(cls, label_list: list[str]) -> dict[str, Parameter]:
|
|
104
|
+
return {
|
|
105
|
+
"label": {
|
|
106
|
+
"type": "str",
|
|
107
|
+
"single_line": True,
|
|
108
|
+
"default": "WireLabel",
|
|
109
|
+
"description": "Label name",
|
|
110
|
+
"invalid_list": label_list,
|
|
111
|
+
},
|
|
112
|
+
"width": {
|
|
113
|
+
"type": "int",
|
|
114
|
+
"min": 1,
|
|
115
|
+
"max": 32,
|
|
116
|
+
"default": 1,
|
|
117
|
+
"description": "Bitwidth of wire",
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@classmethod
|
|
122
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
123
|
+
return cls._label_parameters([])
|
|
124
|
+
|
|
125
|
+
@classmethod
|
|
126
|
+
def get_circuit_parameters(cls, circuit: Circuit) -> dict[str, Parameter]:
|
|
127
|
+
# The labels of the circuit that is edited, not of its subcircuits
|
|
128
|
+
return cls._label_parameters(list(_drivers(circuit, None)))
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class LabelWireOut(Component):
|
|
132
|
+
"""
|
|
133
|
+
A Wire Source: outputs the signal of the LabelWireIn with the same label.
|
|
134
|
+
|
|
135
|
+
Ports:
|
|
136
|
+
<label>: Output, named like the label.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
circuit: The circuit to add the component to.
|
|
140
|
+
name: The component name, the class name if not given.
|
|
141
|
+
label: The label.
|
|
142
|
+
width: The number of bits, used when there is no LabelWireIn with the label yet.
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
def __init__(
|
|
146
|
+
self, circuit: Circuit, name: str | None = None, label: str = "", width: int = 1
|
|
147
|
+
) -> None:
|
|
148
|
+
driver = _drivers(circuit, _new_scope()).get(label)
|
|
149
|
+
width = driver.port_in.width if driver is not None else width
|
|
150
|
+
super().__init__(circuit, name)
|
|
151
|
+
self._label = label
|
|
152
|
+
self._port_out = PortWire(self, self._label, width=width, output=True)
|
|
153
|
+
self.add_port(self._port_out)
|
|
154
|
+
self.parameter_set("label", self._label)
|
|
155
|
+
self.parameter_set("width", width)
|
|
156
|
+
|
|
157
|
+
def label(self) -> str:
|
|
158
|
+
"""Return Label"""
|
|
159
|
+
return self._label
|
|
160
|
+
|
|
161
|
+
@property
|
|
162
|
+
def port_out(self) -> PortWire:
|
|
163
|
+
"""The port driven by the LabelWireIn"""
|
|
164
|
+
return self._port_out
|
|
165
|
+
|
|
166
|
+
def init(self) -> None:
|
|
167
|
+
if not self._port_out.has_driver():
|
|
168
|
+
driver = _drivers(self.circuit, self.parent).get(self._label)
|
|
169
|
+
if driver is not None:
|
|
170
|
+
driver.port_in.wire = self._port_out
|
|
171
|
+
else:
|
|
172
|
+
# Not an error: the LabelWireIn can have been deleted in the GUI. The output
|
|
173
|
+
# is then undefined, like an unconnected input.
|
|
174
|
+
warnings.warn(
|
|
175
|
+
f"LabelWireOut '{self.name()}': no LabelWireIn with the label "
|
|
176
|
+
f"'{self._label}', the output is undefined",
|
|
177
|
+
stacklevel=2,
|
|
178
|
+
)
|
|
179
|
+
super().init()
|
|
180
|
+
|
|
181
|
+
def remove_connections(self) -> None:
|
|
182
|
+
driver = self._port_out.get_driver()
|
|
183
|
+
if driver is not None:
|
|
184
|
+
driver.disconnect(self._port_out)
|
|
185
|
+
super().remove_connections()
|
|
186
|
+
|
|
187
|
+
@classmethod
|
|
188
|
+
def _label_parameters(cls, label_list: list[str]) -> dict[str, Parameter]:
|
|
189
|
+
return {
|
|
190
|
+
"label": {
|
|
191
|
+
"type": "list",
|
|
192
|
+
"items": label_list,
|
|
193
|
+
"description": "Select label",
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
@classmethod
|
|
198
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
199
|
+
return cls._label_parameters([])
|
|
200
|
+
|
|
201
|
+
@classmethod
|
|
202
|
+
def get_circuit_parameters(cls, circuit: Circuit) -> dict[str, Parameter]:
|
|
203
|
+
# The labels of the circuit that is edited, not of its subcircuits
|
|
204
|
+
return cls._label_parameters(list(_drivers(circuit, None)))
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2023-2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""Module with the LED component"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Callable
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from .atoms import CallbackComponent, Component, Parameter, PortIn
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from digsim.circuit import Circuit
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# The colors an LED can have, in the GUI
|
|
19
|
+
LED_COLORS = ["red", "green", "blue", "yellow"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Led(CallbackComponent):
|
|
23
|
+
"""
|
|
24
|
+
An LED, lit while its input is 1.
|
|
25
|
+
|
|
26
|
+
Ports:
|
|
27
|
+
I: Input.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
circuit: The circuit to add the component to.
|
|
31
|
+
name: The component name, the class name if not given.
|
|
32
|
+
callback: Called with the component when its input changes.
|
|
33
|
+
color: The color of the LED in the GUI: "red", "green", "blue" or "yellow".
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
circuit: Circuit,
|
|
39
|
+
name: str | None = None,
|
|
40
|
+
callback: Callable[[Component], None] | None = None,
|
|
41
|
+
color: str = "red",
|
|
42
|
+
) -> None:
|
|
43
|
+
super().__init__(circuit, name, callback)
|
|
44
|
+
self.add_port(PortIn(self, "I"))
|
|
45
|
+
self.parameter_set("color", color)
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def color(self) -> str:
|
|
49
|
+
"""The color of the LED in the GUI"""
|
|
50
|
+
return self.parameter_get("color")
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def active(self) -> bool:
|
|
54
|
+
return self.I.has_driver() and self.I.value == 1
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
58
|
+
return {
|
|
59
|
+
"color": {
|
|
60
|
+
"type": "list",
|
|
61
|
+
"items": LED_COLORS,
|
|
62
|
+
"default": "red",
|
|
63
|
+
"description": "Color",
|
|
64
|
+
"reconfigurable": True,
|
|
65
|
+
},
|
|
66
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2023-2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""Module with the LED bar component"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from ._led import LED_COLORS
|
|
11
|
+
from .atoms import CallbackComponent, Parameter, PortIn
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from digsim.circuit import Circuit
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class LedBar(CallbackComponent):
|
|
19
|
+
"""
|
|
20
|
+
A column of LEDs, with one input for each LED.
|
|
21
|
+
|
|
22
|
+
Ports:
|
|
23
|
+
0, 1, ...: Inputs, one for each LED, numbered from 0.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
circuit: The circuit to add the component to.
|
|
27
|
+
name: The component name, the class name if not given.
|
|
28
|
+
width: The number of LEDs, 1 to 16.
|
|
29
|
+
color: The color of the LEDs in the GUI: "red", "green", "blue" or "yellow".
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self, circuit: Circuit, name: str | None = None, width: int = 8, color: str = "red"
|
|
34
|
+
) -> None:
|
|
35
|
+
super().__init__(circuit, name, callback=None)
|
|
36
|
+
for bit in range(width):
|
|
37
|
+
self.add_port(PortIn(self, f"{bit}"))
|
|
38
|
+
self.parameter_set("width", width)
|
|
39
|
+
self.parameter_set("color", color)
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def color(self) -> str:
|
|
43
|
+
"""The color of the LEDs in the GUI"""
|
|
44
|
+
return self.parameter_get("color")
|
|
45
|
+
|
|
46
|
+
def lit(self) -> list[bool]:
|
|
47
|
+
"""The state of each LED, LED 0 first"""
|
|
48
|
+
return [port.value == 1 for port in self.inports()]
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
52
|
+
return {
|
|
53
|
+
"width": {
|
|
54
|
+
"type": "int",
|
|
55
|
+
"min": 1,
|
|
56
|
+
"max": 16,
|
|
57
|
+
"default": 8,
|
|
58
|
+
"description": "Number of LEDs",
|
|
59
|
+
},
|
|
60
|
+
"color": {
|
|
61
|
+
"type": "list",
|
|
62
|
+
"items": LED_COLORS,
|
|
63
|
+
"default": "red",
|
|
64
|
+
"description": "Color",
|
|
65
|
+
"reconfigurable": True,
|
|
66
|
+
},
|
|
67
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2023-2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""Module with Logic Analyzer component"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import typing
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from .atoms import VALUE_TYPE, CallbackComponent, Parameter, PortIn, PortOutDelta
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from digsim.circuit import Circuit
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class LogicAnalyzer(CallbackComponent):
|
|
19
|
+
"""
|
|
20
|
+
A logic analyzer, keeps the last 100 samples of each input to show them in the GUI.
|
|
21
|
+
The samples are cleared when the circuit is initialized (a reset in the GUI).
|
|
22
|
+
A sample is the value an input had just before the time of the sample: an input that
|
|
23
|
+
changes at the same time, like on the edge of a clock, is sampled before the change,
|
|
24
|
+
whatever the order of the events at that time.
|
|
25
|
+
|
|
26
|
+
Ports:
|
|
27
|
+
A, B, ..., H: Inputs.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
circuit: The circuit to add the component to.
|
|
31
|
+
name: The component name, the class name if not given.
|
|
32
|
+
sample_rate: The samples per second.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
PORTLIST: typing.ClassVar = ["A", "B", "C", "D", "E", "F", "G", "H"]
|
|
36
|
+
|
|
37
|
+
def __init__(self, circuit: Circuit, name: str | None = None, sample_rate: int = 100) -> None:
|
|
38
|
+
super().__init__(circuit, name)
|
|
39
|
+
self.data_dict: dict[str, list[int]] = {}
|
|
40
|
+
# The value of each input, the time it last changed, and its value before that
|
|
41
|
+
self._values: dict[str, VALUE_TYPE] = {}
|
|
42
|
+
self._changed_ns: dict[str, int] = {}
|
|
43
|
+
self._before: dict[str, VALUE_TYPE] = {}
|
|
44
|
+
for portname in self.PORTLIST:
|
|
45
|
+
self.add_port(PortIn(self, portname))
|
|
46
|
+
self.data_dict[portname] = [0] * 100
|
|
47
|
+
self._feedback = PortOutDelta(self, "feedback")
|
|
48
|
+
self._feedback.update_parent(True)
|
|
49
|
+
# The level of the feedback, it toggles at each sample
|
|
50
|
+
self._sample_level: VALUE_TYPE = "X"
|
|
51
|
+
self.parameter_set("sample_rate", sample_rate)
|
|
52
|
+
self.reconfigure()
|
|
53
|
+
|
|
54
|
+
def default_state(self) -> None:
|
|
55
|
+
# A new run from time 0, like the VCD file: the samples of the last one are gone
|
|
56
|
+
self.data_dict = {portname: [0] * 100 for portname in self.PORTLIST}
|
|
57
|
+
for portname in self.PORTLIST:
|
|
58
|
+
self._values[portname] = self.port(portname).value
|
|
59
|
+
self._changed_ns[portname] = -1
|
|
60
|
+
self._before[portname] = self._values[portname]
|
|
61
|
+
self._sample_level = "X"
|
|
62
|
+
self._feedback.value = 1
|
|
63
|
+
|
|
64
|
+
def update(self) -> None:
|
|
65
|
+
now = self.circuit.time_ns
|
|
66
|
+
level = self._feedback.value
|
|
67
|
+
if level == self._sample_level:
|
|
68
|
+
# An input changed
|
|
69
|
+
for portname in self.PORTLIST:
|
|
70
|
+
value = self.port(portname).value
|
|
71
|
+
old = self._values.get(portname, "X")
|
|
72
|
+
if value != old:
|
|
73
|
+
if self._changed_ns.get(portname) != now:
|
|
74
|
+
self._before[portname] = old
|
|
75
|
+
self._changed_ns[portname] = now
|
|
76
|
+
self._values[portname] = value
|
|
77
|
+
return
|
|
78
|
+
self._sample_level = level
|
|
79
|
+
self._feedback.value = 0 if level == 1 else 1
|
|
80
|
+
|
|
81
|
+
# Replace the lists instead of changing them, the GUI thread reads them while painting
|
|
82
|
+
for portname in self.PORTLIST:
|
|
83
|
+
value = (
|
|
84
|
+
self._before[portname]
|
|
85
|
+
if self._changed_ns.get(portname) == now
|
|
86
|
+
else self.port(portname).value
|
|
87
|
+
)
|
|
88
|
+
self.data_dict[portname] = [*self.data_dict[portname][1:], 1 if value == 1 else 0]
|
|
89
|
+
super().update()
|
|
90
|
+
|
|
91
|
+
def reconfigure(self) -> None:
|
|
92
|
+
sample_rate = self.parameter_get("sample_rate")
|
|
93
|
+
period_ns = int(1000000000 / sample_rate)
|
|
94
|
+
self._feedback.set_delay_ns(period_ns)
|
|
95
|
+
|
|
96
|
+
def signal_data(self) -> dict[str, list[int]]:
|
|
97
|
+
"""Get the logic analyzer signal data"""
|
|
98
|
+
return self.data_dict
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
102
|
+
return {
|
|
103
|
+
"sample_rate": {
|
|
104
|
+
"type": "int",
|
|
105
|
+
"min": 10,
|
|
106
|
+
"max": 100,
|
|
107
|
+
"default": 100,
|
|
108
|
+
"description": "Sample rate in Hz",
|
|
109
|
+
"reconfigurable": True,
|
|
110
|
+
},
|
|
111
|
+
}
|