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.
Files changed (88) hide show
  1. digsim/circuit/__init__.py +7 -0
  2. digsim/circuit/_circuit.py +764 -0
  3. digsim/circuit/_native.py +1029 -0
  4. digsim/circuit/_waves_writer.py +131 -0
  5. digsim/circuit/components/__init__.py +52 -0
  6. digsim/circuit/components/_arithmetic.py +266 -0
  7. digsim/circuit/components/_bus_bits.py +103 -0
  8. digsim/circuit/components/_button.py +61 -0
  9. digsim/circuit/components/_buzzer.py +64 -0
  10. digsim/circuit/components/_circuit_port.py +293 -0
  11. digsim/circuit/components/_clock.py +105 -0
  12. digsim/circuit/components/_counter.py +147 -0
  13. digsim/circuit/components/_decoder.py +140 -0
  14. digsim/circuit/components/_dip_switch.py +91 -0
  15. digsim/circuit/components/_flip_flops.py +162 -0
  16. digsim/circuit/components/_gates.py +408 -0
  17. digsim/circuit/components/_hd44780.py +451 -0
  18. digsim/circuit/components/_hd44780_rom.py +118 -0
  19. digsim/circuit/components/_hexdigit.py +107 -0
  20. digsim/circuit/components/_ic.py +96 -0
  21. digsim/circuit/components/_label_wire.py +204 -0
  22. digsim/circuit/components/_led.py +66 -0
  23. digsim/circuit/components/_led_bar.py +67 -0
  24. digsim/circuit/components/_logic_analyzer.py +111 -0
  25. digsim/circuit/components/_memory.py +517 -0
  26. digsim/circuit/components/_memstdout.py +60 -0
  27. digsim/circuit/components/_note.py +40 -0
  28. digsim/circuit/components/_on_off_switch.py +64 -0
  29. digsim/circuit/components/_probe.py +90 -0
  30. digsim/circuit/components/_seven_segment.py +47 -0
  31. digsim/circuit/components/_splitter.py +214 -0
  32. digsim/circuit/components/_static_level.py +54 -0
  33. digsim/circuit/components/_static_value.py +97 -0
  34. digsim/circuit/components/_subcircuit.py +439 -0
  35. digsim/circuit/components/_tristate_buffer.py +81 -0
  36. digsim/circuit/components/_verilog_component.py +26 -0
  37. digsim/circuit/components/_yosys_atoms.py +1373 -0
  38. digsim/circuit/components/_yosys_component.py +1126 -0
  39. digsim/circuit/components/_yosys_functional.py +1714 -0
  40. digsim/circuit/components/atoms/__init__.py +28 -0
  41. digsim/circuit/components/atoms/_component.py +329 -0
  42. digsim/circuit/components/atoms/_digsim_exception.py +8 -0
  43. digsim/circuit/components/atoms/_port.py +708 -0
  44. digsim/circuit/components/ic/4017.json +1062 -0
  45. digsim/circuit/components/ic/4021.json +286 -0
  46. digsim/circuit/components/ic/4029.json +1447 -0
  47. digsim/circuit/components/ic/4094.json +584 -0
  48. digsim/circuit/components/ic/4511.json +1071 -0
  49. digsim/circuit/components/ic/74138.json +763 -0
  50. digsim/circuit/components/ic/74139.json +610 -0
  51. digsim/circuit/components/ic/74148.json +1368 -0
  52. digsim/circuit/components/ic/74151.json +303 -0
  53. digsim/circuit/components/ic/74157.json +248 -0
  54. digsim/circuit/components/ic/74161.json +674 -0
  55. digsim/circuit/components/ic/74162.json +631 -0
  56. digsim/circuit/components/ic/74163.json +673 -0
  57. digsim/circuit/components/ic/74164.json +267 -0
  58. digsim/circuit/components/ic/74165.json +319 -0
  59. digsim/circuit/components/ic/74173.json +403 -0
  60. digsim/circuit/components/ic/74181.json +2002 -0
  61. digsim/circuit/components/ic/74182.json +893 -0
  62. digsim/circuit/components/ic/74193.json +1386 -0
  63. digsim/circuit/components/ic/74194.json +919 -0
  64. digsim/circuit/components/ic/74245.json +466 -0
  65. digsim/circuit/components/ic/74283.json +732 -0
  66. digsim/circuit/components/ic/74374.json +395 -0
  67. digsim/circuit/components/ic/7448.json +834 -0
  68. digsim/circuit/components/ic/74574.json +395 -0
  69. digsim/circuit/components/ic/74595.json +603 -0
  70. digsim/circuit/components/ic/7485.json +1042 -0
  71. digsim/storage_model/__init__.py +19 -0
  72. digsim/storage_model/_app.py +159 -0
  73. digsim/storage_model/_circuit.py +266 -0
  74. digsim/synth/__init__.py +12 -0
  75. digsim/synth/__main__.py +111 -0
  76. digsim/synth/_synthesis.py +529 -0
  77. digsim/utils/__init__.py +25 -0
  78. digsim/utils/_netlist_graph.py +199 -0
  79. digsim/utils/_yosys_netlist.py +251 -0
  80. digsim/verilog/__init__.py +19 -0
  81. digsim/verilog/__main__.py +78 -0
  82. digsim/verilog/_formatter.py +225 -0
  83. digsim/verilog/_lexer.py +218 -0
  84. digsim_engine-1.13.0.dist-info/METADATA +59 -0
  85. digsim_engine-1.13.0.dist-info/RECORD +88 -0
  86. digsim_engine-1.13.0.dist-info/WHEEL +5 -0
  87. digsim_engine-1.13.0.dist-info/licenses/LICENSE.md +32 -0
  88. digsim_engine-1.13.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,131 @@
1
+ # Copyright (c) Fredrik Andersson, 2023-2026
2
+ # All rights reserved
3
+
4
+ """
5
+ Module that handles the creation of vcd files
6
+ """
7
+
8
+ import io
9
+ from typing import Any
10
+
11
+ from vcd import VCDWriter
12
+
13
+ from . import _native
14
+ from .components.atoms import Port
15
+ from .components.atoms._port import connection_generation
16
+
17
+
18
+ class WavesWriter:
19
+ """
20
+ Class that handles the creation of vcd files. With digsim-native it writes them with
21
+ its VcdWriter (the same text as pyvcd's VCDWriter, faster): the native engine then
22
+ writes the changes of its ports itself, see native_vars().
23
+ """
24
+
25
+ def __init__(self, filename: str) -> None:
26
+ self._vcd_name: str = filename
27
+ self._vcd_file: io.TextIOWrapper | None = None
28
+ self._vcd_writer: Any = None
29
+ # The native VcdWriter, if the writer is one (the native engine writes to it)
30
+ self.native: Any = None
31
+ self._vcd_dict: dict[Port, Any] = {}
32
+ # The ports that are written as text, like the name of a state (see init())
33
+ self._text_ports: set[Port] = set()
34
+ # Per port: the variables of all ports it drives, valid for one connection generation
35
+ self._vars_cache: dict[Port, list[tuple[Port, Any, bool]]] = {}
36
+ self._vars_cache_generation = -1
37
+
38
+ def init(self, ports: list[Port]) -> None:
39
+ """
40
+ Initialize vcd writer, with a variable for each port. A port with a vcd_text()
41
+ method, like a state of a Yosys component, is a string variable with that text.
42
+ """
43
+ if self._vcd_file is not None or self._vcd_writer is not None:
44
+ self.close()
45
+ self._vcd_file = open(self._vcd_name, mode="w", encoding="utf-8") # noqa
46
+ if self._vcd_file is None:
47
+ raise RuntimeError("VCD file is None")
48
+ native_writer = _native.vcd_writer()
49
+ if native_writer is not None:
50
+ self.native = native_writer(self._vcd_file, timescale="1 ns", date="today")
51
+ self._vcd_writer = self.native
52
+ else:
53
+ self.native = None
54
+ self._vcd_writer = VCDWriter(self._vcd_file, timescale="1 ns", date="today")
55
+ self._vcd_dict = {}
56
+ self._text_ports = set()
57
+ self._vars_cache = {}
58
+ for port in ports:
59
+ if hasattr(port, "vcd_text"):
60
+ var = self._vcd_writer.register_var(port.path(), port.name(), "string", init="")
61
+ self._text_ports.add(port)
62
+ else:
63
+ var = self._vcd_writer.register_var(
64
+ port.path(), port.name(), "wire", size=port.width
65
+ )
66
+ self._vcd_dict[port] = var
67
+ self._vcd_file.flush()
68
+
69
+ def _port_vars(self, port: Port) -> list[tuple[Port, Any, bool]]:
70
+ """The (port, variable, is text) to update when 'port' changes"""
71
+ if self._vars_cache_generation != connection_generation.value:
72
+ self._vars_cache = {}
73
+ self._vars_cache_generation = connection_generation.value
74
+ port_vars = self._vars_cache.get(port)
75
+ if port_vars is None:
76
+ port_vars = []
77
+ for wired_port in port.get_wired_ports_recursive():
78
+ var = self._vcd_dict.get(wired_port)
79
+ if var is not None:
80
+ port_vars.append((wired_port, var, wired_port in self._text_ports))
81
+ self._vars_cache[port] = port_vars
82
+ return port_vars
83
+
84
+ def native_vars(self, port: Port) -> list[tuple[int, int]] | None:
85
+ """
86
+ For the native engine: the variables to update when 'port' changes, as (variable,
87
+ index of its port in the engine). None if the engine can't write them itself: a
88
+ variable is text, or its port isn't in the engine (then it calls write()).
89
+ """
90
+ found = []
91
+ for wired_port, var, is_text in self._port_vars(port):
92
+ nid = getattr(wired_port, "_nid", None)
93
+ if is_text or nid is None:
94
+ return None
95
+ found.append((var, nid))
96
+ return found
97
+
98
+ def write(self, port: Port, time_ns: int) -> None:
99
+ """Write port value to vcd file"""
100
+ if self._vcd_file is None:
101
+ raise RuntimeError("VCD file is None")
102
+ if self._vcd_writer is None:
103
+ raise RuntimeError("VCD Writer is None")
104
+ change = self._vcd_writer.change
105
+ for wired_port, var, is_text in self._port_vars(port):
106
+ if is_text:
107
+ change(var, timestamp=time_ns, value=wired_port.vcd_text()) # type: ignore[attr-defined]
108
+ else:
109
+ change(var, timestamp=time_ns, value=wired_port.value)
110
+
111
+ def flush(self) -> None:
112
+ """Write buffered changes to the vcd file"""
113
+ if self.native is not None:
114
+ self.native.drain()
115
+ if self._vcd_file is not None:
116
+ self._vcd_file.flush()
117
+
118
+ def close(self) -> None:
119
+ """Close vcd file"""
120
+ if self._vcd_writer is not None:
121
+ self._vcd_writer.close()
122
+ self._vcd_writer = None
123
+ self.native = None
124
+
125
+ if self._vcd_file is not None:
126
+ self._vcd_file.close()
127
+ self._vcd_file = None
128
+
129
+ self._vcd_dict = {}
130
+ self._text_ports = set()
131
+ self._vars_cache = {}
@@ -0,0 +1,52 @@
1
+ # Copyright (c) Fredrik Andersson, 2023-2026
2
+ # All rights reserved
3
+
4
+ """All classes within digsim.circuit.components namespace"""
5
+
6
+ from ._arithmetic import Adder, Comparator, Multiplier, Negator, Subtractor
7
+ from ._bus_bits import Bus2Wires, Wires2Bus
8
+ from ._button import PushButton
9
+ from ._buzzer import Buzzer
10
+ from ._circuit_port import (
11
+ CIRCUIT_PORT_TYPES,
12
+ CircuitInOut,
13
+ CircuitInput,
14
+ CircuitOutput,
15
+ CircuitPort,
16
+ free_label,
17
+ )
18
+ from ._clock import Clock
19
+ from ._counter import Counter
20
+ from ._decoder import Decoder, Demultiplexer
21
+ from ._dip_switch import DipSwitch
22
+ from ._flip_flops import SRFF, ClockedJKFF, ClockedSRFF, ClockedTFF, FlipFlop
23
+ from ._gates import AND, DFF, MUX, NAND, NOR, NOT, OR, SR, XNOR, XOR
24
+ from ._hd44780 import HD44780
25
+ from ._hexdigit import HexDigit
26
+ from ._ic import IntegratedCircuit
27
+ from ._label_wire import LabelWireIn, LabelWireOut
28
+ from ._led import Led
29
+ from ._led_bar import LedBar
30
+ from ._logic_analyzer import LogicAnalyzer
31
+ from ._memory import RAM, ROM, TEXT_FILE_TYPES, read_memory_file
32
+ from ._memstdout import MemStdOut
33
+ from ._note import Note
34
+ from ._on_off_switch import OnOffSwitch
35
+ from ._probe import Probe
36
+ from ._seven_segment import SevenSegment
37
+ from ._splitter import Splitter, group_ranges, group_text, parse_groups
38
+ from ._static_level import GND, VDD
39
+ from ._static_value import StaticValue
40
+ from ._subcircuit import SubCircuit, SubCircuitException, files_used
41
+ from ._tristate_buffer import TristateBuffer
42
+ from ._verilog_component import VerilogComponent
43
+ from ._yosys_component import (
44
+ NetlistParts,
45
+ YosysComponent,
46
+ YosysComponentException,
47
+ YosysVariant,
48
+ file_modules,
49
+ parameter_dict,
50
+ parameter_text,
51
+ )
52
+ from .atoms import PortConnectionError, PortInOut
@@ -0,0 +1,266 @@
1
+ # Copyright (c) Fredrik Andersson, 2026
2
+ # All rights reserved
3
+
4
+ """Arithmetic components: adder, subtractor, multiplier, comparator and negator"""
5
+
6
+ from __future__ import annotations
7
+
8
+ from typing import TYPE_CHECKING
9
+
10
+ from .atoms import VALUE_TYPE, Component, ComponentException, Parameter, PortIn, PortOutDelta
11
+
12
+
13
+ if TYPE_CHECKING:
14
+ from digsim.circuit import Circuit
15
+
16
+ MAX_WIDTH = 32
17
+ # The product of a multiplier is twice as wide as its inputs
18
+ MAX_MULTIPLIER_WIDTH = 16
19
+
20
+
21
+ def _numbers(*values: VALUE_TYPE) -> list[int] | None:
22
+ """The values as numbers, None if one of them is "X" or "Z" """
23
+ numbers = [value for value in values if isinstance(value, int)]
24
+ return numbers if len(numbers) == len(values) else None
25
+
26
+
27
+ def _width_parameter(maximum: int = MAX_WIDTH) -> Parameter:
28
+ return {
29
+ "type": "int",
30
+ "min": 1,
31
+ "max": maximum,
32
+ "default": 8,
33
+ "description": "Bitwidth of the inputs",
34
+ }
35
+
36
+
37
+ class _Arithmetic(Component):
38
+ """The base class of the arithmetic components, with inputs of 'width' bits"""
39
+
40
+ MAX_WIDTH = MAX_WIDTH
41
+
42
+ def __init__(self, circuit: Circuit, name: str | None, width: int) -> None:
43
+ super().__init__(circuit, name)
44
+ if not 1 <= width <= self.MAX_WIDTH:
45
+ raise ComponentException(f"The width must be 1 to {self.MAX_WIDTH}")
46
+ self._width = width
47
+ self._mask = (1 << width) - 1
48
+ self.parameter_set("width", width)
49
+
50
+
51
+ class Adder(_Arithmetic):
52
+ """
53
+ Adds two numbers: S is A + B (+ Cin), and Cout is the carry out.
54
+
55
+ Ports:
56
+ A: Input, `width` bits.
57
+ B: Input, `width` bits.
58
+ Cin: Carry input (only with `carry_in`).
59
+ S: Sum output, `width` bits.
60
+ Cout: Carry output.
61
+
62
+ Args:
63
+ circuit: The circuit to add the component to.
64
+ name: The component name, the class name if not given.
65
+ width: The number of bits, 1 to 32.
66
+ carry_in: Add the carry input Cin, to chain adders.
67
+ """
68
+
69
+ def __init__(
70
+ self, circuit: Circuit, name: str | None = None, width: int = 8, carry_in: bool = False
71
+ ) -> None:
72
+ super().__init__(circuit, name, width)
73
+ self._carry_in = carry_in
74
+ self.add_port(PortIn(self, "A", width=width))
75
+ self.add_port(PortIn(self, "B", width=width))
76
+ if carry_in:
77
+ self.add_port(PortIn(self, "Cin"))
78
+ self.add_port(PortOutDelta(self, "S", width=width))
79
+ self.add_port(PortOutDelta(self, "Cout"))
80
+ self.parameter_set("carry_in", carry_in)
81
+
82
+ def update(self) -> None:
83
+ numbers = _numbers(self.A.value, self.B.value, self.Cin.value if self._carry_in else 0)
84
+ if numbers is None:
85
+ self.S.value = "X"
86
+ self.Cout.value = "X"
87
+ return
88
+ total = sum(numbers)
89
+ self.S.value = total & self._mask
90
+ self.Cout.value = total >> self._width
91
+
92
+ @classmethod
93
+ def get_parameters(cls) -> dict[str, Parameter]:
94
+ return {
95
+ "width": _width_parameter(),
96
+ "carry_in": {"type": "bool", "default": False, "description": "Carry input Cin"},
97
+ }
98
+
99
+
100
+ class Subtractor(_Arithmetic):
101
+ """
102
+ Subtracts two numbers: D is A - B (- Bin), and Bout is 1 when it borrows (A is
103
+ smaller).
104
+
105
+ Ports:
106
+ A: Input, `width` bits.
107
+ B: Input, `width` bits.
108
+ Bin: Borrow input (only with `borrow_in`).
109
+ D: Difference output, `width` bits.
110
+ Bout: Borrow output.
111
+
112
+ Args:
113
+ circuit: The circuit to add the component to.
114
+ name: The component name, the class name if not given.
115
+ width: The number of bits, 1 to 32.
116
+ borrow_in: Add the borrow input Bin, to chain subtractors.
117
+ """
118
+
119
+ def __init__(
120
+ self, circuit: Circuit, name: str | None = None, width: int = 8, borrow_in: bool = False
121
+ ) -> None:
122
+ super().__init__(circuit, name, width)
123
+ self._borrow_in = borrow_in
124
+ self.add_port(PortIn(self, "A", width=width))
125
+ self.add_port(PortIn(self, "B", width=width))
126
+ if borrow_in:
127
+ self.add_port(PortIn(self, "Bin"))
128
+ self.add_port(PortOutDelta(self, "D", width=width))
129
+ self.add_port(PortOutDelta(self, "Bout"))
130
+ self.parameter_set("borrow_in", borrow_in)
131
+
132
+ def update(self) -> None:
133
+ numbers = _numbers(self.A.value, self.B.value, self.Bin.value if self._borrow_in else 0)
134
+ if numbers is None:
135
+ self.D.value = "X"
136
+ self.Bout.value = "X"
137
+ return
138
+ a, b, borrow = numbers
139
+ difference = a - b - borrow
140
+ self.D.value = difference & self._mask
141
+ self.Bout.value = 1 if difference < 0 else 0
142
+
143
+ @classmethod
144
+ def get_parameters(cls) -> dict[str, Parameter]:
145
+ return {
146
+ "width": _width_parameter(),
147
+ "borrow_in": {"type": "bool", "default": False, "description": "Borrow input Bin"},
148
+ }
149
+
150
+
151
+ class Multiplier(_Arithmetic):
152
+ """
153
+ Multiplies two numbers: P is A * B, twice as wide as them.
154
+
155
+ Ports:
156
+ A: Input, `width` bits.
157
+ B: Input, `width` bits.
158
+ P: Product output, 2 * `width` bits.
159
+
160
+ Args:
161
+ circuit: The circuit to add the component to.
162
+ name: The component name, the class name if not given.
163
+ width: The number of bits of A and B, 1 to 16.
164
+ """
165
+
166
+ MAX_WIDTH = MAX_MULTIPLIER_WIDTH
167
+
168
+ def __init__(self, circuit: Circuit, name: str | None = None, width: int = 8) -> None:
169
+ super().__init__(circuit, name, width)
170
+ self.add_port(PortIn(self, "A", width=width))
171
+ self.add_port(PortIn(self, "B", width=width))
172
+ self.add_port(PortOutDelta(self, "P", width=2 * width))
173
+
174
+ def update(self) -> None:
175
+ numbers = _numbers(self.A.value, self.B.value)
176
+ self.P.value = "X" if numbers is None else numbers[0] * numbers[1]
177
+
178
+ @classmethod
179
+ def get_parameters(cls) -> dict[str, Parameter]:
180
+ return {"width": _width_parameter(MAX_MULTIPLIER_WIDTH)}
181
+
182
+
183
+ class Comparator(_Arithmetic):
184
+ """
185
+ Compares two numbers: LT is 1 when A < B, EQ when A = B and GT when A > B.
186
+
187
+ Ports:
188
+ A: Input, `width` bits.
189
+ B: Input, `width` bits.
190
+ LT: Output, A is less than B.
191
+ EQ: Output, A equals B.
192
+ GT: Output, A is greater than B.
193
+
194
+ Args:
195
+ circuit: The circuit to add the component to.
196
+ name: The component name, the class name if not given.
197
+ width: The number of bits, 1 to 32.
198
+ signed: Compare A and B as two's complement numbers.
199
+ """
200
+
201
+ def __init__(
202
+ self, circuit: Circuit, name: str | None = None, width: int = 8, signed: bool = False
203
+ ) -> None:
204
+ super().__init__(circuit, name, width)
205
+ self._signed = signed
206
+ self.add_port(PortIn(self, "A", width=width))
207
+ self.add_port(PortIn(self, "B", width=width))
208
+ self.add_port(PortOutDelta(self, "LT"))
209
+ self.add_port(PortOutDelta(self, "EQ"))
210
+ self.add_port(PortOutDelta(self, "GT"))
211
+ self.parameter_set("signed", signed)
212
+
213
+ def _number(self, value: int) -> int:
214
+ if self._signed and value >> (self._width - 1):
215
+ return value - (1 << self._width)
216
+ return value
217
+
218
+ def update(self) -> None:
219
+ numbers = _numbers(self.A.value, self.B.value)
220
+ if numbers is None:
221
+ for port in (self.LT, self.EQ, self.GT):
222
+ port.value = "X"
223
+ return
224
+ a, b = (self._number(number) for number in numbers)
225
+ self.LT.value = int(a < b)
226
+ self.EQ.value = int(a == b)
227
+ self.GT.value = int(a > b)
228
+
229
+ @classmethod
230
+ def get_parameters(cls) -> dict[str, Parameter]:
231
+ return {
232
+ "width": _width_parameter(),
233
+ "signed": {
234
+ "type": "bool",
235
+ "default": False,
236
+ "description": "Signed numbers (two's complement)",
237
+ },
238
+ }
239
+
240
+
241
+ class Negator(_Arithmetic):
242
+ """
243
+ Negates a number: Y is -A, in two's complement.
244
+
245
+ Ports:
246
+ A: Input, `width` bits.
247
+ Y: Output, `width` bits.
248
+
249
+ Args:
250
+ circuit: The circuit to add the component to.
251
+ name: The component name, the class name if not given.
252
+ width: The number of bits, 1 to 32.
253
+ """
254
+
255
+ def __init__(self, circuit: Circuit, name: str | None = None, width: int = 8) -> None:
256
+ super().__init__(circuit, name, width)
257
+ self.add_port(PortIn(self, "A", width=width))
258
+ self.add_port(PortOutDelta(self, "Y", width=width))
259
+
260
+ def update(self) -> None:
261
+ numbers = _numbers(self.A.value)
262
+ self.Y.value = "X" if numbers is None else -numbers[0] & self._mask
263
+
264
+ @classmethod
265
+ def get_parameters(cls) -> dict[str, Parameter]:
266
+ return {"width": _width_parameter()}
@@ -0,0 +1,103 @@
1
+ # Copyright (c) Fredrik Andersson, 2023-2025
2
+ # All rights reserved
3
+
4
+ """Module with the bus/bit <=> bus/bit components"""
5
+
6
+ from __future__ import annotations
7
+
8
+ from typing import TYPE_CHECKING
9
+
10
+ from .atoms import Component, Parameter, PortMultiBitWire
11
+
12
+
13
+ if TYPE_CHECKING:
14
+ from digsim.circuit import Circuit
15
+
16
+
17
+ class Bus2Wires(Component):
18
+ """
19
+ Splits a bus into single-bit wires.
20
+
21
+ Ports:
22
+ bus: Input, `width` bits.
23
+ bus_0, bus_1, ...: Outputs, one for each enabled bit. bus_0 is the least significant bit.
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 bits.
29
+ enable: The bits to have outputs for, all if None.
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ circuit: Circuit,
35
+ name: str | None = None,
36
+ width: int = 1,
37
+ enable: list[int] | None = None,
38
+ ) -> None:
39
+ super().__init__(circuit, name)
40
+ self._bus = PortMultiBitWire(self, "bus", width=width)
41
+ self.add_port(self._bus)
42
+ self._enable = enable if enable is not None else list(range(width))
43
+
44
+ for bit_id in range(width):
45
+ if bit_id in self._enable:
46
+ bit_port = self._bus.get_bit(bit_id)
47
+ self.add_port(bit_port)
48
+
49
+ self.parameter_set("width", width)
50
+ self.parameter_set("enable", enable)
51
+
52
+ @classmethod
53
+ def get_parameters(cls) -> dict[str, Parameter]:
54
+ return {
55
+ "width": {
56
+ "type": "int",
57
+ "min": 2,
58
+ "max": 32,
59
+ "default": 2,
60
+ "description": "Bus width",
61
+ },
62
+ "enable": {
63
+ "type": "width_bool",
64
+ "default": None,
65
+ "description": "Enable bit",
66
+ },
67
+ }
68
+
69
+
70
+ class Wires2Bus(Component):
71
+ """
72
+ Merges single-bit wires into a bus.
73
+
74
+ Ports:
75
+ bus_0, bus_1, ...: Inputs, one for each bit. bus_0 is the least significant bit.
76
+ bus: Output, `width` bits.
77
+
78
+ Args:
79
+ circuit: The circuit to add the component to.
80
+ name: The component name, the class name if not given.
81
+ width: The number of bits.
82
+ """
83
+
84
+ def __init__(self, circuit: Circuit, name: str | None = None, width: int = 1) -> None:
85
+ super().__init__(circuit, name)
86
+ self._bus = PortMultiBitWire(self, "bus", width=width, output=True)
87
+ self.add_port(self._bus)
88
+ for bit_id in range(width):
89
+ bit_port = self._bus.get_bit(bit_id)
90
+ self.add_port(bit_port)
91
+ self.parameter_set("width", width)
92
+
93
+ @classmethod
94
+ def get_parameters(cls) -> dict[str, Parameter]:
95
+ return {
96
+ "width": {
97
+ "type": "int",
98
+ "min": 2,
99
+ "max": 32,
100
+ "default": 2,
101
+ "description": "Bus width",
102
+ },
103
+ }
@@ -0,0 +1,61 @@
1
+ # Copyright (c) Fredrik Andersson, 2023-2025
2
+ # All rights reserved
3
+
4
+ """A PushButton component"""
5
+
6
+ from __future__ import annotations
7
+
8
+ from typing import TYPE_CHECKING
9
+
10
+ from .atoms import CallbackComponent, PortOutImmediate
11
+
12
+
13
+ if TYPE_CHECKING:
14
+ from digsim.circuit import Circuit
15
+
16
+
17
+ class PushButton(CallbackComponent):
18
+ """
19
+ A push button. Its output is 1 while it is pushed, and 0 otherwise.
20
+
21
+ Ports:
22
+ O: Output.
23
+
24
+ Args:
25
+ circuit: The circuit to add the component to.
26
+ name: The component name, the class name if not given.
27
+ """
28
+
29
+ def __init__(self, circuit: Circuit, name: str | None = None) -> None:
30
+ super().__init__(circuit, name)
31
+ portout = PortOutImmediate(self, "O")
32
+ self.add_port(portout)
33
+ portout.update_parent(True)
34
+
35
+ def default_state(self) -> None:
36
+ self.release()
37
+
38
+ def push(self) -> None:
39
+ """Push pushbutton"""
40
+ self.O.value = 1
41
+
42
+ def release(self) -> None:
43
+ """Release pushbutton"""
44
+ self.O.value = 0
45
+
46
+ def reconfigure(self) -> None:
47
+ self.release()
48
+
49
+ @property
50
+ def has_action(self) -> bool:
51
+ return True
52
+
53
+ @property
54
+ def active(self) -> bool:
55
+ return self.O.value == 1
56
+
57
+ def onpress(self) -> None:
58
+ self.push()
59
+
60
+ def onrelease(self) -> None:
61
+ self.release()
@@ -0,0 +1,64 @@
1
+ # Copyright (c) Fredrik Andersson, 2023-2025
2
+ # All rights reserved
3
+
4
+ """Module with the Buzzer component"""
5
+
6
+ from __future__ import annotations
7
+
8
+ import typing
9
+ from typing import TYPE_CHECKING
10
+
11
+ from .atoms import CallbackComponent, Parameter, PortIn
12
+
13
+
14
+ if TYPE_CHECKING:
15
+ from digsim.circuit import Circuit
16
+
17
+
18
+ class Buzzer(CallbackComponent):
19
+ """
20
+ A buzzer, plays a tone in the GUI while its input is 1.
21
+
22
+ Ports:
23
+ I: Input.
24
+
25
+ Args:
26
+ circuit: The circuit to add the component to.
27
+ name: The component name, the class name if not given.
28
+ tone: The note, "A" to "G".
29
+ """
30
+
31
+ TONE_TO_FREQUENCY: typing.ClassVar = {
32
+ "A": 880,
33
+ "B": 987.77,
34
+ "C": 1046.5,
35
+ "D": 1174.66,
36
+ "E": 1318.51,
37
+ "F": 1396.91,
38
+ "G": 1567.98,
39
+ }
40
+
41
+ def __init__(self, circuit: Circuit, name: str | None = None, tone: str = "A") -> None:
42
+ super().__init__(circuit, name)
43
+ self.add_port(PortIn(self, "I"))
44
+ self.parameter_set("tone", tone)
45
+
46
+ @property
47
+ def active(self) -> bool:
48
+ return self.I.has_driver() and self.I.value == 1
49
+
50
+ def tone_frequency(self) -> float:
51
+ """Get frequency for tone"""
52
+ return self.TONE_TO_FREQUENCY[self.parameter_get("tone")]
53
+
54
+ @classmethod
55
+ def get_parameters(cls) -> dict[str, Parameter]:
56
+ return {
57
+ "tone": {
58
+ "type": "list",
59
+ "items": ["A", "B", "C", "D", "E", "F", "G"],
60
+ "default": "A",
61
+ "description": "Buzzer tone",
62
+ "reconfigurable": True,
63
+ },
64
+ }