ac6502 1.11.0 → 1.13.0

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.
@@ -1,153 +0,0 @@
1
- import { AttachmentBase } from './Attachment'
2
-
3
- /**
4
- * USB HID keycode to keypad value mapping
5
- * Maps USB HID usage IDs to the 5-bit keypad codes ($00-$17) per the 6502 Keypad Mapping table
6
- *
7
- * Keypad layout (4 columns × 6 rows = 24 keys):
8
- * $00 = ◄ $01=1 $02=2 $03=3
9
- * $04 = 4 $05=5 $06=6 $07=7
10
- * $08 = 8 $09=9 $0A=0 $0B=►
11
- * $0C = F $0D=E $0E=D $0F=C
12
- * $10 = ESC $11=INS $12=PGUP $13=A
13
- * $14 = ▲/Enter $15=DEL $16=PGDN $17=B
14
- */
15
- const USB_HID_TO_KEYPAD: { [key: number]: number } = {
16
- 0x50: 0x00, // Left Arrow → ◄
17
- 0x1E: 0x01, // 1
18
- 0x1F: 0x02, // 2
19
- 0x20: 0x03, // 3
20
- 0x21: 0x04, // 4
21
- 0x22: 0x05, // 5
22
- 0x23: 0x06, // 6
23
- 0x24: 0x07, // 7
24
- 0x25: 0x08, // 8
25
- 0x26: 0x09, // 9
26
- 0x27: 0x0A, // 0
27
- 0x4F: 0x0B, // Right Arrow → ►
28
- 0x09: 0x0C, // f → F
29
- 0x08: 0x0D, // e → E
30
- 0x07: 0x0E, // d → D
31
- 0x06: 0x0F, // c → C
32
- 0x29: 0x10, // Escape → ESC
33
- 0x49: 0x11, // Insert → INS
34
- 0x4B: 0x12, // Page Up → PGUP
35
- 0x04: 0x13, // a → A
36
- 0x52: 0x14, // Up Arrow → ▲
37
- 0x28: 0x14, // Enter → ▲
38
- 0x4C: 0x15, // Delete → DEL
39
- 0x4E: 0x16, // Page Down → PGDN
40
- 0x05: 0x17, // b → B
41
- }
42
-
43
- /**
44
- * KeypadAttachment - Emulates a 4×6 matrix keypad with a built-in hardware encoder
45
- *
46
- * The encoder converts a key press into a 5-bit code (PA0–PA4) that appears on the GPIO
47
- * port. Bits 5–7 are never driven by the keypad and always read as 0 when data is present.
48
- *
49
- * Behaviour mirrors a typical 74C922-style encoder:
50
- * - On key press → the 5-bit keypad code is latched and a CA1/CB1 interrupt is asserted
51
- * - On port read → the latched code is returned on bits 0–4 (bits 5–7 = 0)
52
- * - clearInterrupts → clears the interrupt and the data-ready latch
53
- * - Key releases → ignored (encoder only reports on the falling edge of a keypress)
54
- *
55
- * The attachment may be wired to either Port A or Port B via the constructor parameter.
56
- * CA1/CB1 is the DA (Data Available) interrupt line from the 74C922.
57
- * CA2/CB2 is connected to the 74C922 OE (Output Enable) pin; data is only driven onto the
58
- * bus when OE is asserted LOW by the 6522.
59
- */
60
- export class KeypadAttachment extends AttachmentBase {
61
- private keypadValue: number = 0x00
62
- private dataReady: boolean = false
63
- private interruptPending: boolean = false
64
- private readonly attachedToPortA: boolean
65
-
66
- // OE state: CA2 for Port A, CB2 for Port B. HIGH = output disabled (default).
67
- private oeState: boolean = true
68
-
69
- constructor(attachToPortA: boolean = true, priority: number = 0) {
70
- super(priority, false, false, false, false)
71
- this.attachedToPortA = attachToPortA
72
- this.reset()
73
- }
74
-
75
- reset(): void {
76
- super.reset()
77
- this.keypadValue = 0x00
78
- this.dataReady = false
79
- this.interruptPending = false
80
- this.oeState = true // OE disabled until explicitly asserted by the 6522
81
- }
82
-
83
- updateControlLines(ca1: boolean, ca2: boolean, cb1: boolean, cb2: boolean): void {
84
- // CA2 controls OE for Port A; CB2 controls OE for Port B.
85
- // 74C922 OE is active-LOW, so a LOW signal enables the output.
86
- this.oeState = this.attachedToPortA ? ca2 : cb2
87
- }
88
-
89
- readPortA(ddr: number, or: number): number {
90
- // Only drive the bus when attached to Port A, OE is asserted (LOW), and data is latched
91
- if (this.attachedToPortA && !this.oeState && this.dataReady) {
92
- return this.keypadValue & 0x1F // bits 0–4 only; bits 5–7 = 0
93
- }
94
- return 0xFF // not driving the bus
95
- }
96
-
97
- readPortB(ddr: number, or: number): number {
98
- // Only drive the bus when attached to Port B, OE is asserted (LOW), and data is latched
99
- if (!this.attachedToPortA && !this.oeState && this.dataReady) {
100
- return this.keypadValue & 0x1F // bits 0–4 only; bits 5–7 = 0
101
- }
102
- return 0xFF // not driving the bus
103
- }
104
-
105
- hasCA1Interrupt(): boolean {
106
- return this.attachedToPortA && this.interruptPending
107
- }
108
-
109
- hasCB1Interrupt(): boolean {
110
- return !this.attachedToPortA && this.interruptPending
111
- }
112
-
113
- clearInterrupts(ca1: boolean, ca2: boolean, cb1: boolean, cb2: boolean): void {
114
- if ((this.attachedToPortA && ca1) || (!this.attachedToPortA && cb1)) {
115
- this.interruptPending = false
116
- this.dataReady = false
117
- }
118
- }
119
-
120
- /**
121
- * Notify the attachment of a USB HID key event.
122
- * Key releases are ignored; only presses generate output on the GPIO port.
123
- *
124
- * @param usbHidKeycode - USB HID usage ID for the key
125
- * @param pressed - true for key-down, false for key-up
126
- */
127
- updateKey(usbHidKeycode: number, pressed: boolean): void {
128
- if (!pressed) {
129
- return
130
- }
131
-
132
- const keypadCode = USB_HID_TO_KEYPAD[usbHidKeycode]
133
- if (keypadCode === undefined) {
134
- return // key is not present on this keypad
135
- }
136
-
137
- this.keypadValue = keypadCode
138
- this.dataReady = true
139
- this.interruptPending = true
140
- }
141
-
142
- /**
143
- * Returns the current latched keypad code (bits 0–4) or 0xFF if no data is ready.
144
- */
145
- getCurrentKey(): number {
146
- return this.dataReady ? (this.keypadValue & 0x1F) : 0xFF
147
- }
148
-
149
- /** Returns true when a key has been pressed and the latch has not yet been cleared. */
150
- hasDataReady(): boolean {
151
- return this.dataReady
152
- }
153
- }