librarywork/hex1000/libs/Front/src/Front.xtl
Front: the physical HEX1000 keypad scanner, debounce, key handshake and MAX7219 stream Import with "fr:" u_se< "Front". Signals are integers and state is tuples. The board synchronizes external inputs before this model receives them.
ˡMap : Int
Map: physical scan positions, top keypad row first, then operators.
"fr:" u̲se< "Front"
4 t̲ake ᶠʳMap 12 13 14 15
ˡ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
ˡBootDigits : Int
BootDigits: shown DEADBEEF, digit zero (rightmost) first.
"fr:" u̲se< "Front"
ᶠʳBootDigits 31 30 30 27 29 26 30 29
ˡBootDigits ← 31 30 30 27 29 26 30 29
ˡs̲tartup : (Truthy a, Num b, Num c, Truthy d) => (a, b, c) -> d
s_tartup: latch the first accepted key; a short unaccepted bounce does not count.
"fr:" u̲se< "Front"
ᶠʳs̲tartup (0, 0, 1) 1
ˡs̲tartup ← { (had, phase, accepted) → had ∨ (phase = 0) ∧ accepted ≠ 0 }
ˡd̲isplay : Truthy a => (a, Int) -> Int
d_isplay: startup banner until the first key, then the captured calculator digits.
"fr:" u̲se< "Front"
ᶠʳd̲isplay (1, 16 0 0 0 0 0 0 0) 16 0 0 0 0 0 0 0
ˡd̲isplay ← { (had, digits) → had ? digits ˡBootDigits }
ˡScanReset : (Int, Int, Int, Int, Int, Int)
ˡScanReset ← (0, 0, 0, 0, 0, 0)
ˡKeyReset : (Int, Int, Int)
KeyReset: idle handshake, key code and previous-equals flag.
"fr:" u̲se< "Front"
ᶠʳKeyReset (0, 0, 0)
ˡKeyReset ← (0, 0, 0)
ˡSpiReset : (Int, Int, Int, Int, Int, Int)
SpiReset: phase, word, shift register, DIN, CLK and LOAD.
"fr:" u̲se< "Front"
ᶠʳSpiReset (0, 0, 0, 0, 0, 1)
ˡSpiReset ← (0, 0, 0, 0, 0, 1)
ˡs̲can : (Num a, Num b) => a -> ((a, Int, Int, Int, b, Int), Int, Int) -> (a, Int, Int, Int, b, Int)
s_can: one clock; four rows, active-low columns/operators, eight stable scans.
"fr:" u̲se< "Front"
4 ᶠʳs̲can (ᶠʳScanReset, 15, 15) (1, 0, 0, 0, 0, 0)
ˡs̲can ← { clocks ((tick, row, scan, candidate, count, accepted), cols, ops) → tick ≠ clocks − 1 ? (tick + 1, row, scan, candidate, count, accepted) scale ← 16 ^ row scan1 ← scan + scale × (15 − cols) − (scan d̲iv scale) m̲od 16 row ≠ 3 ? (0, row + 1, scan1, candidate, count, accepted) frame ← scan1 + 65536 × 15 − ops frame ≠ candidate ? (0, 0, scan1, frame, 0, accepted) count < 7 ? (0, 0, scan1, candidate, count + 1, accepted) (0, 0, scan1, candidate, count, frame) }
ˡk̲ey : Truthy a => ((Int, Int, a), Int, Int) -> (Int, Int, a)
k_ey: one handshake clock; the second consecutive equals becomes clear (20).
"fr:" u̲se< "Front"
ᶠʳk̲ey (ᶠʳKeyReset, 1, 32768) (1, 12, 0)
ˡk̲ey ← { ((phase, key, last), accepted, r) → ready ← (r d̲iv 32768) m̲od 2 phase = 1 ? (1 + ready, key, last) phase = 2 ? (3 − ready, key, last) phase = 3 ? (3 × accepted ≠ 0, key, last) accepted = 0 ? (phase, key, last) bits ← r̲ev (20 r̲eshape 2) e̲ncode accepted pos ← f̲irst bits i̲ndexOf 1 code ← pos s̲elect ˡMap translated ← code + ((code = 19) ∧ last) (1, translated, (code = 19) ∧ n̲ot last) }
ˡk̲inputs : (Num a, Any b) => ((a, Int, b), Int) -> Int
k_inputs: matrix K bits from the held key and the machine's R column select.
"fr:" u̲se< "Front"
ᶠʳk̲inputs ((2, 19, 1), 4096) 8
ˡk̲inputs ← { ((phase, key, last), r) → (phase = 2) × ((r d̲iv 2 ^ 8 + key d̲iv 4) m̲od 2) × 2 ^ key m̲od 4 }
ˡd̲igits : (Int, Int, Int, Int) -> Int
d_igits: capture O when each of the eight R digit strobes rises.
"fr:" u̲se< "Front"
ᶠʳd̲igits ((8 r̲eshape 0), 0, 22, 1) 22 0 0 0 0 0 0 0
ˡd̲igits ← { (digits, previous, o, r) → now ← 8 t̲ake r̲ev (16 r̲eshape 2) e̲ncode r old ← 8 t̲ake r̲ev (16 r̲eshape 2) e̲ncode previous digits + (now > old) × (o m̲od 32) − digits }
ˡs̲eg : Int -> Int
s_eg: a shown digit's seven segments in MAX7219 order, DP A B C D E F G.
"fr:" u̲se< "Front"
'ᶠʳs̲eg e̲ach 16 17 22 5 126 48 95 0
ˡs̲eg ← { d → font ← (1 + d m̲od 16) s̲elect ᵖⁿFont (d ≥ 16) × 2 d̲ecode r̲ev (7 r̲eshape 2) e̲ncode font }
ˡw̲ord : (Int, Int) -> Int
w_ord: initialization words 0-4 and display registers 1-8 at words 5-12.
"fr:" u̲se< "Front"
ᶠʳw̲ord (0, 8 r̲eshape 0) 3073
ˡw̲ord ← { (index, digits) → index < 5 ? (index + 1) s̲elect 3073 3840 2304 2823 2564 (256 × index − 4) + ˡs̲eg (index − 4) s̲elect digits }
ˡs̲pi : (Num a, Num b) => ((Int, Int, Int, Int, a, b), Int) -> (Int, Int, Int, Int, a, b)
s_pi: one serial clock step, 16 MSB-first bits and four clocks of LOAD high.
"fr:" u̲se< "Front"
ᶠʳs̲pi (ᶠʳSpiReset, 8 r̲eshape 0) (1, 0, 3073, 0, 0, 0)
ˡs̲pi ← { ((phase, index, shift, din, clk, load), digits) → phase = 0 ? ʰs̲tart (index, digits) phase = 32 ? (33, index, shift, din, 0, 1) phase = 35 ? (0, (index + 1) m̲od 13, shift, din, clk, load) phase > 31 ? (phase + 1, index, shift, din, clk, load) (phase m̲od 2) = 1 ? (phase + 1, index, shift, din, 1, load) (phase + 1, index, (2 × shift) m̲od 65536, (shift d̲iv 16384) m̲od 2, 0, load) }