sourcework/hex1000/libs/Front/src/Front.xtl
1⍝│ Front: the physical HEX1000 keypad scanner, debounce, key handshake and MAX7219 stream
2⍝│ Import with "fr:" u_se< "Front". Signals are integers and state is tuples.
3⍝│ The board synchronizes external inputs before this model receives them.
4"pn:" u̲se< "Panel"
5
6⍝│ Map: physical scan positions, top keypad row first, then operators.
7⍝│ >> "fr:" u_se< "Front"
8⍝│ >> 4 t_ake fr:Map
9⍝│ 12 13 14 15
10ˡMap ← (12 + o̲ffsets 4) c̲at (8 + o̲ffsets 4) c̲at (4 + o̲ffsets 4) c̲at (o̲ffsets 4) c̲at 16 17 18 19
11
12⍝│ BootDigits: shown DEADBEEF, digit zero (rightmost) first.
13⍝│ >> "fr:" u_se< "Front"
14⍝│ >> fr:BootDigits
15⍝│ 31 30 30 27 29 26 30 29
16ˡBootDigits ← 31 30 30 27 29 26 30 29
17
18⍝│ s_tartup: latch the first accepted key; a short unaccepted bounce does not count.
19⍝│ >> "fr:" u_se< "Front"
20⍝│ >> fr:s_tartup (0, 0, 1)
21⍝│ 1
22ˡs̲tartup ← { (had, phase, accepted) → had ∨ (phase = 0) ∧ accepted ≠ 0 }
23
24⍝│ d_isplay: startup banner until the first key, then the captured calculator digits.
25⍝│ >> "fr:" u_se< "Front"
26⍝│ >> fr:d_isplay (1, 16 0 0 0 0 0 0 0)
27⍝│ 16 0 0 0 0 0 0 0
28ˡd̲isplay ← { (had, digits) →
29 had ? digits
30 ˡBootDigits
31}
32
33⍝│ ScanReset: scanner and debouncer at startup.
34⍝│ >> "fr:" u_se< "Front"
35⍝│ >> fr:ScanReset
36⍝│ (0, 0, 0, 0, 0, 0)
37ˡScanReset ← (0, 0, 0, 0, 0, 0)
38
39⍝│ KeyReset: idle handshake, key code and previous-equals flag.
40⍝│ >> "fr:" u_se< "Front"
41⍝│ >> fr:KeyReset
42⍝│ (0, 0, 0)
43ˡKeyReset ← (0, 0, 0)
44
45⍝│ SpiReset: phase, word, shift register, DIN, CLK and LOAD.
46⍝│ >> "fr:" u_se< "Front"
47⍝│ >> fr:SpiReset
48⍝│ (0, 0, 0, 0, 0, 1)
49ˡSpiReset ← (0, 0, 0, 0, 0, 1)
50
51⍝│ s_can: one clock; four rows, active-low columns/operators, eight stable scans.
52⍝│ >> "fr:" u_se< "Front"
53⍝│ >> 4 fr:s_can (fr:ScanReset, 15, 15)
54⍝│ (1, 0, 0, 0, 0, 0)
55ˡs̲can ← { clocks ((tick, row, scan, candidate, count, accepted), cols, ops) →
56 tick ≠ clocks − 1 ? (tick + 1, row, scan, candidate, count, accepted)
57 scale ← 16 ^ row
58 scan1 ← scan + scale × (15 − cols) − (scan d̲iv scale) m̲od 16
59 row ≠ 3 ? (0, row + 1, scan1, candidate, count, accepted)
60 frame ← scan1 + 65536 × 15 − ops
61 frame ≠ candidate ? (0, 0, scan1, frame, 0, accepted)
62 count < 7 ? (0, 0, scan1, candidate, count + 1, accepted)
63 (0, 0, scan1, candidate, count, frame)
64}
65
66⍝│ k_ey: one handshake clock; the second consecutive equals becomes clear (20).
67⍝│ >> "fr:" u_se< "Front"
68⍝│ >> fr:k_ey (fr:KeyReset, 1, 32768)
69⍝│ (1, 12, 0)
70ˡk̲ey ← { ((phase, key, last), accepted, r) →
71 ready ← (r d̲iv 32768) m̲od 2
72 phase = 1 ? (1 + ready, key, last)
73 phase = 2 ? (3 − ready, key, last)
74 phase = 3 ? (3 × accepted ≠ 0, key, last)
75 accepted = 0 ? (phase, key, last)
76 bits ← r̲ev (20 r̲eshape 2) e̲ncode accepted
77 pos ← f̲irst bits i̲ndexOf 1
78 code ← pos s̲elect ˡMap
79 translated ← code + ((code = 19) ∧ last)
80 (1, translated, (code = 19) ∧ n̲ot last)
81}
82
83⍝│ k_inputs: matrix K bits from the held key and the machine's R column select.
84⍝│ >> "fr:" u_se< "Front"
85⍝│ >> fr:k_inputs ((2, 19, 1), 4096)
86⍝│ 8
87ˡk̲inputs ← { ((phase, key, last), r) →
88 (phase = 2) × ((r d̲iv 2 ^ 8 + key d̲iv 4) m̲od 2) × 2 ^ key m̲od 4
89}
90
91⍝│ d_igits: capture O when each of the eight R digit strobes rises.
92⍝│ >> "fr:" u_se< "Front"
93⍝│ >> fr:d_igits ((8 r_eshape 0), 0, 22, 1)
94⍝│ 22 0 0 0 0 0 0 0
95ˡd̲igits ← { (digits, previous, o, r) →
96 now ← 8 t̲ake r̲ev (16 r̲eshape 2) e̲ncode r
97 old ← 8 t̲ake r̲ev (16 r̲eshape 2) e̲ncode previous
98 digits + (now > old) × (o m̲od 32) − digits
99}
100
101⍝│ s_eg: a shown digit's seven segments in MAX7219 order, DP A B C D E F G.
102⍝│ >> "fr:" u_se< "Front"
103⍝│ >> 'fr:s_eg e_ach 16 17 22 5
104⍝│ 126 48 95 0
105ˡs̲eg ← { d →
106 font ← (1 + d m̲od 16) s̲elect ᵖⁿFont
107 (d ≥ 16) × 2 d̲ecode r̲ev (7 r̲eshape 2) e̲ncode font
108}
109
110⍝│ w_ord: initialization words 0-4 and display registers 1-8 at words 5-12.
111⍝│ >> "fr:" u_se< "Front"
112⍝│ >> fr:w_ord (0, 8 r_eshape 0)
113⍝│ 3073
114ˡw̲ord ← { (index, digits) →
115 index < 5 ? (index + 1) s̲elect 3073 3840 2304 2823 2564
116 (256 × index − 4) + ˡs̲eg (index − 4) s̲elect digits
117}
118
119⍝│ s_pi: one serial clock step, 16 MSB-first bits and four clocks of LOAD high.
120⍝│ >> "fr:" u_se< "Front"
121⍝│ >> fr:s_pi (fr:SpiReset, 8 r_eshape 0)
122⍝│ (1, 0, 3073, 0, 0, 0)
123ˡs̲pi ← { ((phase, index, shift, din, clk, load), digits) →
124 phase = 0 ? ʰs̲tart (index, digits)
125 phase = 32 ? (33, index, shift, din, 0, 1)
126 phase = 35 ? (0, (index + 1) m̲od 13, shift, din, clk, load)
127 phase > 31 ? (phase + 1, index, shift, din, clk, load)
128 (phase m̲od 2) = 1 ? (phase + 1, index, shift, din, 1, load)
129 (phase + 1, index, (2 × shift) m̲od 65536, (shift d̲iv 16384) m̲od 2, 0, load)
130}
131ʰs̲tart ← { (index, digits) →
132 word ← ˡw̲ord (index, digits)
133 (1, index, word, word d̲iv 32768, 0, 0)
134}