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,451 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""Module with the HD44780 character LCD component, a 16x2 LCD module with a backlight"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from ._hd44780_rom import CHARACTER_ROM, TEXT
|
|
11
|
+
from .atoms import VALUE_TYPE, CallbackComponent, Parameter, PortIn, PortInOut, PortOutDelta
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from digsim.circuit import Circuit
|
|
16
|
+
|
|
17
|
+
COLUMNS = 16
|
|
18
|
+
ROWS = 2
|
|
19
|
+
# The DDRAM has 80 characters, a line of 40 in two-line mode
|
|
20
|
+
DDRAM_SIZE = 80
|
|
21
|
+
LINE_LENGTH = 40
|
|
22
|
+
# The address of the second line in two-line mode
|
|
23
|
+
LINE2_ADDRESS = 0x40
|
|
24
|
+
CGRAM_SIZE = 64
|
|
25
|
+
# The cursor blinks at 1.6 Hz (with a 250 kHz oscillator), this is half the period
|
|
26
|
+
BLINK_NS = 409_600_000
|
|
27
|
+
|
|
28
|
+
BACKLIGHTS = ["green", "blue", "amber", "white", "red"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class HD44780(CallbackComponent):
|
|
32
|
+
"""
|
|
33
|
+
A 16x2 character LCD module with an HD44780 controller and a backlight, programmed
|
|
34
|
+
like the real one, with the A00 (English and Japanese) character ROM.
|
|
35
|
+
|
|
36
|
+
The controller reads RS and RW, and the data on D0 to D7, on the falling edge of E. A
|
|
37
|
+
write with RS 0 is an instruction and with RS 1 data for the DDRAM or the CGRAM.
|
|
38
|
+
While E is 1 with RW 1 it drives the data pins: with RS 0 the busy flag (bit 7) and
|
|
39
|
+
the address counter, with RS 1 the data at the address. An instruction is done at
|
|
40
|
+
once, so the busy flag is always 0.
|
|
41
|
+
|
|
42
|
+
After a 4-bit Function Set (DL 0) the data is on D4 to D7, the high nibble first, and
|
|
43
|
+
D0 to D3 are not used. The data pins that nothing drives are 1, the controller has
|
|
44
|
+
pull-ups on them.
|
|
45
|
+
|
|
46
|
+
It starts like after power on: in 8-bit mode with one line (only the first line of
|
|
47
|
+
the display is shown), the display off and the DDRAM filled with spaces. Function Set
|
|
48
|
+
with N 1 selects two lines.
|
|
49
|
+
|
|
50
|
+
Ports:
|
|
51
|
+
RS: Register select input, 0 for an instruction and 1 for data.
|
|
52
|
+
RW: Read/write input, 0 to write and 1 to read.
|
|
53
|
+
E: Enable input, a write is done on its falling edge.
|
|
54
|
+
D0, D1, ..., D7: Data inout ports.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
circuit: The circuit to add the component to.
|
|
58
|
+
name: The component name, the class name if not given.
|
|
59
|
+
backlight: The color of the backlight in the GUI: "green" (yellow-green with dark
|
|
60
|
+
characters), "blue" (white characters), "amber", "white" or "red".
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self, circuit: Circuit, name: str | None = None, backlight: str = "green"
|
|
65
|
+
) -> None:
|
|
66
|
+
super().__init__(circuit, name)
|
|
67
|
+
# Before the ports, wiring them updates the component
|
|
68
|
+
self._blink_level = 0
|
|
69
|
+
self._reset_state()
|
|
70
|
+
self.add_port(PortIn(self, "RS"))
|
|
71
|
+
self.add_port(PortIn(self, "RW"))
|
|
72
|
+
self.add_port(PortIn(self, "E"))
|
|
73
|
+
# A driver for each data pin, and an input for each pin it reads, like CircuitInOut
|
|
74
|
+
self._drivers: list[PortOutDelta] = []
|
|
75
|
+
self._readers: list[PortIn] = []
|
|
76
|
+
for bit in range(8):
|
|
77
|
+
pin = PortInOut(self, f"D{bit}")
|
|
78
|
+
pin.set_tristate()
|
|
79
|
+
self.add_port(pin)
|
|
80
|
+
driver = PortOutDelta(self, f"D{bit}_driver", delay_ns=0)
|
|
81
|
+
driver.set_tristate()
|
|
82
|
+
driver.wire = pin.get_drive_bit(0)
|
|
83
|
+
self._drivers.append(driver)
|
|
84
|
+
reader = PortIn(self, f"D{bit}_reader")
|
|
85
|
+
pin.get_bit(0).wire = reader
|
|
86
|
+
self._readers.append(reader)
|
|
87
|
+
# Toggles the blink of the cursor
|
|
88
|
+
self._blink_timer = PortOutDelta(self, "blink_timer", delay_ns=BLINK_NS)
|
|
89
|
+
self._blink_timer.update_parent(True)
|
|
90
|
+
self.parameter_set("backlight", backlight)
|
|
91
|
+
|
|
92
|
+
def _reset_state(self) -> None:
|
|
93
|
+
"""The state after power on"""
|
|
94
|
+
self._ddram = [0x20] * DDRAM_SIZE
|
|
95
|
+
self._cgram = [0] * CGRAM_SIZE
|
|
96
|
+
# The address counter, and if it addresses the CGRAM
|
|
97
|
+
self._address = 0
|
|
98
|
+
self._cgram_selected = False
|
|
99
|
+
self._increment = True
|
|
100
|
+
self._shift_on_write = False
|
|
101
|
+
self._display_on = False
|
|
102
|
+
self._cursor_on = False
|
|
103
|
+
self._blink_on = False
|
|
104
|
+
self._eight_bit = True
|
|
105
|
+
self._two_lines = False
|
|
106
|
+
self._large_font = False
|
|
107
|
+
# How far the display is shifted left
|
|
108
|
+
self._shift = 0
|
|
109
|
+
# In 4-bit mode: the high nibble of a transfer, None until it is done
|
|
110
|
+
self._high_nibble: int | None = None
|
|
111
|
+
# The blink phase where the character is replaced by a block, and its next toggle
|
|
112
|
+
self._blink_dark = False
|
|
113
|
+
self._blink_at_ns: int | None = None
|
|
114
|
+
self._driven: VALUE_TYPE = "Z"
|
|
115
|
+
|
|
116
|
+
def init(self) -> None:
|
|
117
|
+
super().init()
|
|
118
|
+
for port in [*self._drivers, *self._readers, self._blink_timer]:
|
|
119
|
+
port.init()
|
|
120
|
+
|
|
121
|
+
def default_state(self) -> None:
|
|
122
|
+
self._reset_state()
|
|
123
|
+
for driver in self._drivers:
|
|
124
|
+
driver.value = "Z"
|
|
125
|
+
self._changed()
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def backlight(self) -> str:
|
|
129
|
+
"""The color of the backlight in the GUI"""
|
|
130
|
+
return self.parameter_get("backlight")
|
|
131
|
+
|
|
132
|
+
def _changed(self) -> None:
|
|
133
|
+
"""The display may have changed, the GUI repaints it"""
|
|
134
|
+
super().update()
|
|
135
|
+
|
|
136
|
+
def _bus(self) -> int | None:
|
|
137
|
+
"""The byte on the data pins, the nibble on D4 to D7 in 4-bit mode, None with an "X" """
|
|
138
|
+
value = 0
|
|
139
|
+
pins = self._readers[4:] if not self._eight_bit else self._readers
|
|
140
|
+
for bit, reader in enumerate(pins):
|
|
141
|
+
level = reader.value
|
|
142
|
+
if level == "Z":
|
|
143
|
+
# The pull-ups
|
|
144
|
+
level = 1
|
|
145
|
+
if not isinstance(level, int):
|
|
146
|
+
return None
|
|
147
|
+
value |= level << bit
|
|
148
|
+
return value
|
|
149
|
+
|
|
150
|
+
def _drive(self, value: VALUE_TYPE) -> None:
|
|
151
|
+
"""Drive the data pins with a byte (a nibble on D4 to D7 in 4-bit mode), or "Z" """
|
|
152
|
+
if value == self._driven:
|
|
153
|
+
return
|
|
154
|
+
self._driven = value
|
|
155
|
+
for bit, driver in enumerate(self._drivers):
|
|
156
|
+
if not isinstance(value, int) or (not self._eight_bit and bit < 4):
|
|
157
|
+
driver.value = "Z"
|
|
158
|
+
else:
|
|
159
|
+
shift = bit if self._eight_bit else bit - 4
|
|
160
|
+
driver.value = (value >> shift) & 1
|
|
161
|
+
|
|
162
|
+
def _read_byte(self, rs: int) -> int:
|
|
163
|
+
"""The byte that a read returns"""
|
|
164
|
+
if rs == 0:
|
|
165
|
+
# The busy flag is 0
|
|
166
|
+
return self._address
|
|
167
|
+
if self._cgram_selected:
|
|
168
|
+
return self._cgram[self._address]
|
|
169
|
+
return self._ddram[self._ddram_index(self._address)]
|
|
170
|
+
|
|
171
|
+
def _read_output(self, rs: int) -> int:
|
|
172
|
+
"""What the data pins show during a read, a nibble of the byte in 4-bit mode"""
|
|
173
|
+
byte = self._read_byte(rs)
|
|
174
|
+
if self._eight_bit:
|
|
175
|
+
return byte
|
|
176
|
+
return byte >> 4 if self._high_nibble is None else byte & 0xF
|
|
177
|
+
|
|
178
|
+
def update(self) -> None:
|
|
179
|
+
falling_edge = self.E.is_falling_edge()
|
|
180
|
+
now = self.circuit.time_ns
|
|
181
|
+
if self._blink_at_ns is not None and now >= self._blink_at_ns:
|
|
182
|
+
self._blink_dark = not self._blink_dark
|
|
183
|
+
self._blink_at_ns = None
|
|
184
|
+
self._update_blink()
|
|
185
|
+
self._changed()
|
|
186
|
+
rs, rw = self.RS.value, self.RW.value
|
|
187
|
+
if falling_edge and isinstance(rs, int) and isinstance(rw, int):
|
|
188
|
+
if rw == 0:
|
|
189
|
+
self._write(rs)
|
|
190
|
+
else:
|
|
191
|
+
self._read_done(rs)
|
|
192
|
+
# While E is 1 with RW 1, the controller drives the data pins
|
|
193
|
+
if self.E.value == 1 and rw == 1 and isinstance(rs, int):
|
|
194
|
+
self._drive(self._read_output(rs))
|
|
195
|
+
else:
|
|
196
|
+
self._drive("Z")
|
|
197
|
+
|
|
198
|
+
def _transfer(self) -> int | None:
|
|
199
|
+
"""The byte of a write, None while half of it is transferred in 4-bit mode"""
|
|
200
|
+
bus = self._bus()
|
|
201
|
+
if bus is None:
|
|
202
|
+
self._high_nibble = None
|
|
203
|
+
return None
|
|
204
|
+
if self._eight_bit:
|
|
205
|
+
return bus
|
|
206
|
+
if self._high_nibble is None:
|
|
207
|
+
self._high_nibble = bus
|
|
208
|
+
return None
|
|
209
|
+
byte = self._high_nibble << 4 | bus
|
|
210
|
+
self._high_nibble = None
|
|
211
|
+
return byte
|
|
212
|
+
|
|
213
|
+
def _write(self, rs: int) -> None:
|
|
214
|
+
byte = self._transfer()
|
|
215
|
+
if byte is None:
|
|
216
|
+
return
|
|
217
|
+
if rs == 0:
|
|
218
|
+
self._instruction(byte)
|
|
219
|
+
else:
|
|
220
|
+
self._write_data(byte)
|
|
221
|
+
self._update_blink()
|
|
222
|
+
self._changed()
|
|
223
|
+
|
|
224
|
+
def _read_done(self, rs: int) -> None:
|
|
225
|
+
"""The end of a read: after a data read the address counter moves"""
|
|
226
|
+
if not self._eight_bit:
|
|
227
|
+
if self._high_nibble is None:
|
|
228
|
+
# A placeholder, the next nibble is the low one
|
|
229
|
+
self._high_nibble = 0
|
|
230
|
+
return
|
|
231
|
+
self._high_nibble = None
|
|
232
|
+
if rs == 1:
|
|
233
|
+
self._move_address(self._increment)
|
|
234
|
+
self._update_blink()
|
|
235
|
+
self._changed()
|
|
236
|
+
|
|
237
|
+
def _instruction(self, byte: int) -> None:
|
|
238
|
+
if byte & 0x80:
|
|
239
|
+
# Set DDRAM address
|
|
240
|
+
self._address = self._valid_ddram_address(byte & 0x7F)
|
|
241
|
+
self._cgram_selected = False
|
|
242
|
+
elif byte & 0x40:
|
|
243
|
+
# Set CGRAM address
|
|
244
|
+
self._address = byte & 0x3F
|
|
245
|
+
self._cgram_selected = True
|
|
246
|
+
elif byte & 0x20:
|
|
247
|
+
# Function set
|
|
248
|
+
self._eight_bit = bool(byte & 0x10)
|
|
249
|
+
self._two_lines = bool(byte & 0x08)
|
|
250
|
+
self._large_font = bool(byte & 0x04)
|
|
251
|
+
self._address = self._valid_ddram_address(self._address)
|
|
252
|
+
elif byte & 0x10:
|
|
253
|
+
# Cursor or display shift, right or left
|
|
254
|
+
right = bool(byte & 0x04)
|
|
255
|
+
if byte & 0x08:
|
|
256
|
+
self._shift_display(not right)
|
|
257
|
+
else:
|
|
258
|
+
self._move_address(right)
|
|
259
|
+
elif byte & 0x08:
|
|
260
|
+
# Display on/off control
|
|
261
|
+
self._display_on = bool(byte & 0x04)
|
|
262
|
+
self._cursor_on = bool(byte & 0x02)
|
|
263
|
+
self._blink_on = bool(byte & 0x01)
|
|
264
|
+
elif byte & 0x04:
|
|
265
|
+
# Entry mode set
|
|
266
|
+
self._increment = bool(byte & 0x02)
|
|
267
|
+
self._shift_on_write = bool(byte & 0x01)
|
|
268
|
+
elif byte & 0x02:
|
|
269
|
+
# Return home
|
|
270
|
+
self._address = 0
|
|
271
|
+
self._cgram_selected = False
|
|
272
|
+
self._shift = 0
|
|
273
|
+
elif byte & 0x01:
|
|
274
|
+
# Clear display
|
|
275
|
+
self._ddram = [0x20] * DDRAM_SIZE
|
|
276
|
+
self._address = 0
|
|
277
|
+
self._cgram_selected = False
|
|
278
|
+
self._shift = 0
|
|
279
|
+
self._increment = True
|
|
280
|
+
|
|
281
|
+
def _write_data(self, byte: int) -> None:
|
|
282
|
+
if self._cgram_selected:
|
|
283
|
+
self._cgram[self._address] = byte
|
|
284
|
+
else:
|
|
285
|
+
self._ddram[self._ddram_index(self._address)] = byte
|
|
286
|
+
if self._shift_on_write:
|
|
287
|
+
self._shift_display(self._increment)
|
|
288
|
+
self._move_address(self._increment)
|
|
289
|
+
|
|
290
|
+
def _shift_display(self, left: bool) -> None:
|
|
291
|
+
"""Shift the display one character left or right"""
|
|
292
|
+
length = LINE_LENGTH if self._two_lines else DDRAM_SIZE
|
|
293
|
+
self._shift = (self._shift + (1 if left else -1)) % length
|
|
294
|
+
|
|
295
|
+
def _valid_ddram_address(self, address: int) -> int:
|
|
296
|
+
"""A DDRAM address, the ones between the lines are not used"""
|
|
297
|
+
if self._two_lines:
|
|
298
|
+
if address >= LINE2_ADDRESS:
|
|
299
|
+
return min(address, LINE2_ADDRESS + LINE_LENGTH - 1)
|
|
300
|
+
return min(address, LINE_LENGTH - 1)
|
|
301
|
+
return min(address, DDRAM_SIZE - 1)
|
|
302
|
+
|
|
303
|
+
def _ddram_index(self, address: int) -> int:
|
|
304
|
+
"""The index in the DDRAM of an address"""
|
|
305
|
+
if self._two_lines and address >= LINE2_ADDRESS:
|
|
306
|
+
return LINE_LENGTH + address - LINE2_ADDRESS
|
|
307
|
+
return address
|
|
308
|
+
|
|
309
|
+
def _move_address(self, increment: bool) -> None:
|
|
310
|
+
"""Move the address counter one step, it wraps around"""
|
|
311
|
+
step = 1 if increment else -1
|
|
312
|
+
if self._cgram_selected:
|
|
313
|
+
self._address = (self._address + step) % CGRAM_SIZE
|
|
314
|
+
return
|
|
315
|
+
index = (self._ddram_index(self._address) + step) % DDRAM_SIZE
|
|
316
|
+
if self._two_lines and index >= LINE_LENGTH:
|
|
317
|
+
self._address = LINE2_ADDRESS + index - LINE_LENGTH
|
|
318
|
+
else:
|
|
319
|
+
self._address = index
|
|
320
|
+
|
|
321
|
+
def _cursor_cell(self) -> tuple[int, int] | None:
|
|
322
|
+
"""The (row, column) of the address counter on the display, None if not shown"""
|
|
323
|
+
if self._cgram_selected:
|
|
324
|
+
return None
|
|
325
|
+
index = self._ddram_index(self._address)
|
|
326
|
+
length = LINE_LENGTH if self._two_lines else DDRAM_SIZE
|
|
327
|
+
row, position = divmod(index, length)
|
|
328
|
+
if row >= ROWS:
|
|
329
|
+
return None
|
|
330
|
+
column = (position - self._shift) % length
|
|
331
|
+
return (row, column) if column < COLUMNS else None
|
|
332
|
+
|
|
333
|
+
def _update_blink(self) -> None:
|
|
334
|
+
"""Run the blink timer while the cursor blinks"""
|
|
335
|
+
blinks = self._display_on and self._blink_on
|
|
336
|
+
if not blinks:
|
|
337
|
+
self._blink_dark = False
|
|
338
|
+
self._blink_at_ns = None
|
|
339
|
+
return
|
|
340
|
+
if self._blink_at_ns is None:
|
|
341
|
+
self._blink_at_ns = self.circuit.time_ns + BLINK_NS
|
|
342
|
+
# A change, so that the timer is an event
|
|
343
|
+
self._blink_level ^= 1
|
|
344
|
+
self._blink_timer.value = self._blink_level
|
|
345
|
+
|
|
346
|
+
def codes(self) -> list[list[int | None]]:
|
|
347
|
+
"""
|
|
348
|
+
The character code shown in each place of the display, a list for each line. In
|
|
349
|
+
one-line mode the second line is None.
|
|
350
|
+
"""
|
|
351
|
+
length = LINE_LENGTH if self._two_lines else DDRAM_SIZE
|
|
352
|
+
lines: list[list[int | None]] = []
|
|
353
|
+
for row in range(ROWS):
|
|
354
|
+
if row and not self._two_lines:
|
|
355
|
+
lines.append([None] * COLUMNS)
|
|
356
|
+
continue
|
|
357
|
+
lines.append(
|
|
358
|
+
[
|
|
359
|
+
self._ddram[row * length + (column + self._shift) % length]
|
|
360
|
+
for column in range(COLUMNS)
|
|
361
|
+
]
|
|
362
|
+
)
|
|
363
|
+
return lines
|
|
364
|
+
|
|
365
|
+
def lines(self) -> list[str]:
|
|
366
|
+
"""
|
|
367
|
+
The text of the two lines of the display, 16 characters each, also while the
|
|
368
|
+
display is off. A character of the CGRAM (codes 0 to 15) is chr(code), and a code
|
|
369
|
+
without a character is a space.
|
|
370
|
+
"""
|
|
371
|
+
return [
|
|
372
|
+
"".join(
|
|
373
|
+
" " if code is None else chr(code) if code < 0x10 else TEXT.get(code, " ")
|
|
374
|
+
for code in line
|
|
375
|
+
)
|
|
376
|
+
for line in self.codes()
|
|
377
|
+
]
|
|
378
|
+
|
|
379
|
+
@staticmethod
|
|
380
|
+
def rom_glyph(code: int) -> tuple[int, ...]:
|
|
381
|
+
"""
|
|
382
|
+
The glyph of a code in the character ROM, see glyph(). Codes 0 to 15 are blank.
|
|
383
|
+
|
|
384
|
+
Args:
|
|
385
|
+
code: The character code, 0 to 255.
|
|
386
|
+
"""
|
|
387
|
+
return CHARACTER_ROM[code]
|
|
388
|
+
|
|
389
|
+
def glyph(self, code: int) -> tuple[int, ...]:
|
|
390
|
+
"""
|
|
391
|
+
The 8 rows of 5 pixels of a character, the top row first, the left pixel in bit
|
|
392
|
+
4. Codes 0 to 15 are the 8 characters of the CGRAM (twice).
|
|
393
|
+
|
|
394
|
+
Args:
|
|
395
|
+
code: The character code, 0 to 255.
|
|
396
|
+
"""
|
|
397
|
+
if code < 0x10:
|
|
398
|
+
start = (code & 0x07) * 8
|
|
399
|
+
return tuple(row & 0x1F for row in self._cgram[start : start + 8])
|
|
400
|
+
return CHARACTER_ROM[code]
|
|
401
|
+
|
|
402
|
+
def pixels(self) -> list[list[tuple[int, ...]]]:
|
|
403
|
+
"""
|
|
404
|
+
What the display shows: the rows of pixels of each character (see glyph()), a list
|
|
405
|
+
for each line, with the cursor. Nothing is shown while the display is off.
|
|
406
|
+
"""
|
|
407
|
+
blank = (0,) * 8
|
|
408
|
+
cursor = self._cursor_cell()
|
|
409
|
+
shown: list[list[tuple[int, ...]]] = []
|
|
410
|
+
for row, line in enumerate(self.codes()):
|
|
411
|
+
cells: list[tuple[int, ...]] = []
|
|
412
|
+
for column, code in enumerate(line):
|
|
413
|
+
if not self._display_on or code is None:
|
|
414
|
+
cells.append(blank)
|
|
415
|
+
continue
|
|
416
|
+
glyph = self.glyph(code)
|
|
417
|
+
if cursor == (row, column):
|
|
418
|
+
if self._blink_on and self._blink_dark:
|
|
419
|
+
glyph = (0x1F,) * 8
|
|
420
|
+
elif self._cursor_on:
|
|
421
|
+
glyph = (*glyph[:7], 0x1F)
|
|
422
|
+
cells.append(glyph)
|
|
423
|
+
shown.append(cells)
|
|
424
|
+
return shown
|
|
425
|
+
|
|
426
|
+
def read_ddram(self, address: int) -> int:
|
|
427
|
+
"""
|
|
428
|
+
Return the character code at a DDRAM address.
|
|
429
|
+
|
|
430
|
+
Args:
|
|
431
|
+
address: The address, 0x00 to 0x27 and 0x40 to 0x67 in two-line mode, otherwise
|
|
432
|
+
0x00 to 0x4F.
|
|
433
|
+
"""
|
|
434
|
+
return self._ddram[self._ddram_index(address)]
|
|
435
|
+
|
|
436
|
+
@property
|
|
437
|
+
def address(self) -> int:
|
|
438
|
+
"""The address counter, in the DDRAM or the CGRAM"""
|
|
439
|
+
return self._address
|
|
440
|
+
|
|
441
|
+
@classmethod
|
|
442
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
443
|
+
return {
|
|
444
|
+
"backlight": {
|
|
445
|
+
"type": "list",
|
|
446
|
+
"items": BACKLIGHTS,
|
|
447
|
+
"default": "green",
|
|
448
|
+
"description": "Backlight color",
|
|
449
|
+
"reconfigurable": True,
|
|
450
|
+
},
|
|
451
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2026
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
The character generator ROM of the HD44780 LCD controller, the A00 (English and Japanese)
|
|
6
|
+
version that most LCD modules have
|
|
7
|
+
|
|
8
|
+
The glyphs are from the A00 table of luma.lcd (https://github.com/rm-hull/luma.lcd):
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2013-2026 Richard Hull & Contributors
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
13
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
14
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
15
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
16
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
17
|
+
conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
20
|
+
or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
23
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
24
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
25
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
26
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
27
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
# The glyphs of the codes 0x10 to 0xFF, four on each line (the code of the first one at
|
|
31
|
+
# the end): 8 rows of 5 pixels, the top row first, two hexadecimal digits for each row
|
|
32
|
+
# with the left pixel in bit 4. Codes 0x00 to 0x0F are the characters of the CGRAM.
|
|
33
|
+
_ROM = """
|
|
34
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 10
|
|
35
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 14
|
|
36
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 18
|
|
37
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 1C
|
|
38
|
+
0000000000000000 0404040400000400 0a0a0a0000000000 0a0a1f0a1f0a0a00 20
|
|
39
|
+
040f140e051e0400 1819020408130300 0c12140815120d00 0c04080000000000 24
|
|
40
|
+
0204080808040200 0804020202040800 0004150e15040000 0004041f04040000 28
|
|
41
|
+
000000000c040800 0000001f00000000 00000000000c0c00 0001020408100000 2C
|
|
42
|
+
0e11131519110e00 040c040404040e00 0e11010204081f00 1f02040201110e00 30
|
|
43
|
+
02060a121f020200 1f101e0101110e00 0608101e11110e00 1f11010204040400 34
|
|
44
|
+
0e11110e11110e00 0e11110f01020c00 000c0c000c0c0000 000c0c000c040800 38
|
|
45
|
+
0204081008040200 00001f001f000000 0804020102040800 0e11010204000400 3C
|
|
46
|
+
0e11010d15150e00 0e1111111f111100 1e11111e11111e00 0e11101010110e00 40
|
|
47
|
+
1c12111111121c00 1f10101e10101f00 1f10101e10101000 0e11101711110f00 44
|
|
48
|
+
1111111f11111100 0e04040404040e00 0702020202120c00 1112141814121100 48
|
|
49
|
+
1010101010101f00 111b151511111100 1111191513111100 0e11111111110e00 4C
|
|
50
|
+
1e11111e10101000 0e11111115120d00 1e11111e14121100 0f10100e01011e00 50
|
|
51
|
+
1f04040404040400 1111111111110e00 11111111110a0400 1111111515150a00 54
|
|
52
|
+
11110a040a111100 1111110a04040400 1f01020408101f00 1c10101010101c00 58
|
|
53
|
+
110a1f041f040400 0e02020202020e00 040a110000000000 0000000000001f00 5C
|
|
54
|
+
0804020000000000 00000e010f110f00 1010161911111e00 00000e1010110e00 60
|
|
55
|
+
01010d1311110f00 00000e111f100e00 0609081c08080800 000f11110f010e00 64
|
|
56
|
+
1010161911111100 04000c0404040e00 0200060202120c00 1010121418141200 68
|
|
57
|
+
0c04040404040e00 00001a1515111100 0000161911111100 00000e1111110e00 6C
|
|
58
|
+
00001e111e101000 00000d130f010100 0000161910101000 00000e100e011e00 70
|
|
59
|
+
08081c0808090600 0000111111130d00 00001111110a0400 0000111515150a00 74
|
|
60
|
+
0000110a040a1100 000011110f010e00 00001f0204081f00 0204040804040200 78
|
|
61
|
+
0404040404040400 0804040204040800 0004021f02040000 0004081f08040000 7C
|
|
62
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 80
|
|
63
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 84
|
|
64
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 88
|
|
65
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 8C
|
|
66
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 90
|
|
67
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 94
|
|
68
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 98
|
|
69
|
+
0000000000000000 0000000000000000 0000000000000000 0000000000000000 9C
|
|
70
|
+
0000000000000000 000000001c141c00 0704040400000000 0000000404041c00 A0
|
|
71
|
+
0000000010080400 0000000c0c000000 001f011f01020400 00001f0106040800 A4
|
|
72
|
+
000002040c140400 0000041f11010600 00001f0404041f00 0000021f060a1200 A8
|
|
73
|
+
0000081f090a0800 0000000e02021f00 00001e021e021e00 0000001515010600 AC
|
|
74
|
+
0000001f00000000 1f01050604040800 0102040c14040400 041f111101020400 B0
|
|
75
|
+
001f040404041f00 021f02060a120200 081f090909091200 041f041f04040400 B4
|
|
76
|
+
000f091101020c00 080f120202020400 001f010101011f00 0a1f0a0a02040800 B8
|
|
77
|
+
0018011901021c00 001f0102040a1100 081f090a08080700 0011110901020c00 BC
|
|
78
|
+
000f091503020c00 021c041f04040800 0015151501020400 0e001f0404040800 C0
|
|
79
|
+
0808080c0a080800 04041f0404081000 000e000000001f00 001f010a040a1000 C4
|
|
80
|
+
041f02040e150400 0202020202040800 0004021111111100 10101f1010100f00 C8
|
|
81
|
+
001f010101020c00 0008140201010000 041f040415150400 001f01010a040200 CC
|
|
82
|
+
000e000e000e0100 00040810111f0100 0001010a040a1000 001f081f08080700 D0
|
|
83
|
+
08081f090a080800 000e020202021f00 001f011f01011f00 0e001f0101020400 D4
|
|
84
|
+
1212121202040800 0004141415151600 0010101112141800 001f111111111f00 D8
|
|
85
|
+
001f111101020400 0018000101021c00 0412080000000000 1c141c0000000000 DC
|
|
86
|
+
0000091512120d00 0a000e010f110f00 00000e111e111e10 00000e100c110e00 E0
|
|
87
|
+
0000111111131d10 00000f1412110e00 0000060911111e10 00000f1111110f01 E4
|
|
88
|
+
0000070404140800 00021a0200000000 0200060202020202 0014081400000000 E8
|
|
89
|
+
00040e14150e0400 08081c081c080f00 0e00161911111100 0a000e1111110e00 EC
|
|
90
|
+
0000161911111e10 00000d1311110f01 000e111f11110e00 0000000b151a0000 F0
|
|
91
|
+
00000e11110a1b00 0a0011111111130d 1f10080408101f00 00001f0a0a0a1300 F4
|
|
92
|
+
1f00110a040a1100 0000111111110f01 00011e041f040400 00001f080f091100 F8
|
|
93
|
+
00001f151f111100 0004001f00040000 0000000000000000 1f1f1f1f1f1f1f1f FC
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _glyphs() -> list[tuple[int, ...]]:
|
|
98
|
+
glyphs: list[tuple[int, ...]] = [(0,) * 8] * 0x10
|
|
99
|
+
for line in _ROM.split("\n"):
|
|
100
|
+
for word in line.split()[:4]:
|
|
101
|
+
glyphs.append(tuple(bytes.fromhex(word)))
|
|
102
|
+
return glyphs
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# The glyph of each code, see _ROM
|
|
106
|
+
CHARACTER_ROM: list[tuple[int, ...]] = _glyphs()
|
|
107
|
+
|
|
108
|
+
# The text of the codes that are not ASCII, for HD44780.lines()
|
|
109
|
+
_KATAKANA = {code: chr(0xFF61 + code - 0xA1) for code in range(0xA1, 0xE0)}
|
|
110
|
+
_SYMBOLS = dict(zip(range(0xE0, 0x100), "αäβεμσρg√⁻jˣ¢£ñöpqθ∞ΩüΣπxy千万円÷ █", strict=True))
|
|
111
|
+
TEXT: dict[int, str] = {
|
|
112
|
+
**{code: chr(code) for code in range(0x20, 0x7E)},
|
|
113
|
+
0x5C: "¥",
|
|
114
|
+
0x7E: "→",
|
|
115
|
+
0x7F: "←",
|
|
116
|
+
**_KATAKANA,
|
|
117
|
+
**_SYMBOLS,
|
|
118
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Copyright (c) Fredrik Andersson, 2023-2025
|
|
2
|
+
# All rights reserved
|
|
3
|
+
|
|
4
|
+
"""HexDigit component module"""
|
|
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
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from digsim.circuit import Circuit
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class HexDigit(CallbackComponent):
|
|
19
|
+
"""
|
|
20
|
+
Hexadecimal digits that show the value of a bus.
|
|
21
|
+
|
|
22
|
+
Ports:
|
|
23
|
+
val: Input, 4 bits for each digit. The first digit shows the most significant bits.
|
|
24
|
+
dot: Input, one bit for each decimal point (only with `dot`).
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
circuit: The circuit to add the component to.
|
|
28
|
+
name: The component name, the class name if not given.
|
|
29
|
+
digits: The number of digits, 1 to 8.
|
|
30
|
+
dot: Add the decimal point input.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
VAL_TO_SEGMENTS: typing.ClassVar = {
|
|
34
|
+
0: "ABCDEF",
|
|
35
|
+
1: "BC",
|
|
36
|
+
2: "ABDEG",
|
|
37
|
+
3: "ABCDG",
|
|
38
|
+
4: "BCFG",
|
|
39
|
+
5: "ACDFG",
|
|
40
|
+
6: "ACDEFG",
|
|
41
|
+
7: "ABC",
|
|
42
|
+
8: "ABCDEFG",
|
|
43
|
+
9: "ABCDFG",
|
|
44
|
+
10: "ABCEFG",
|
|
45
|
+
11: "CDEFG",
|
|
46
|
+
12: "ADEF",
|
|
47
|
+
13: "BCDEG",
|
|
48
|
+
14: "ADEFG",
|
|
49
|
+
15: "AEFG",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self, circuit: Circuit, name: str | None = None, digits: int = 1, dot: bool = True
|
|
54
|
+
) -> None:
|
|
55
|
+
super().__init__(circuit, name)
|
|
56
|
+
self.add_port(PortIn(self, "val", width=4 * digits))
|
|
57
|
+
self.parameter_set("digits", digits)
|
|
58
|
+
self.parameter_set("dot", dot)
|
|
59
|
+
if dot:
|
|
60
|
+
self.add_port(PortIn(self, "dot", width=digits))
|
|
61
|
+
|
|
62
|
+
def get_digits(self) -> int:
|
|
63
|
+
"""Get the number of digits"""
|
|
64
|
+
return self.parameter_get("digits")
|
|
65
|
+
|
|
66
|
+
def value(self) -> VALUE_TYPE:
|
|
67
|
+
"""Get value"""
|
|
68
|
+
return self.val.value
|
|
69
|
+
|
|
70
|
+
def dot_active(self, digit_id: int = 0) -> bool:
|
|
71
|
+
"""Is the dot active for the selected digit_id?"""
|
|
72
|
+
if not self.parameter_get("dot"):
|
|
73
|
+
return False
|
|
74
|
+
dot = self.dot.value
|
|
75
|
+
if not isinstance(dot, int):
|
|
76
|
+
return False
|
|
77
|
+
dot_value = dot >> (self.dot.width - (digit_id + 1)) & 1
|
|
78
|
+
return dot_value == 1
|
|
79
|
+
|
|
80
|
+
def segments(self, digit_id: int = 0) -> str:
|
|
81
|
+
"""Which segments are active for the selected digit_id?"""
|
|
82
|
+
value = self.value()
|
|
83
|
+
if not isinstance(value, int):
|
|
84
|
+
segments = ""
|
|
85
|
+
else:
|
|
86
|
+
digit_value = (value >> (self.val.width - (digit_id + 1) * 4)) & 0xF
|
|
87
|
+
segments = self.VAL_TO_SEGMENTS.get(digit_value, "")
|
|
88
|
+
if self.dot_active(digit_id):
|
|
89
|
+
segments += "."
|
|
90
|
+
return segments
|
|
91
|
+
|
|
92
|
+
@classmethod
|
|
93
|
+
def get_parameters(cls) -> dict[str, Parameter]:
|
|
94
|
+
return {
|
|
95
|
+
"digits": {
|
|
96
|
+
"type": "int",
|
|
97
|
+
"min": 1,
|
|
98
|
+
"max": 8,
|
|
99
|
+
"default": 1,
|
|
100
|
+
"description": "Number of digits",
|
|
101
|
+
},
|
|
102
|
+
"dot": {
|
|
103
|
+
"type": "bool",
|
|
104
|
+
"default": True,
|
|
105
|
+
"description": "Use decimal dot",
|
|
106
|
+
},
|
|
107
|
+
}
|