sourcelibs/Hex1000/demos/terminal.xtl
1⍝!/usr/bin/env xetal
2⍝ HEX1000 at a terminal: type at the calculator and watch its display.
3⍝ Every key goes through the machine's key matrix into HEX1000 running
4⍝ its ROM program (rom/calc.asm), and the eight digits shown are the
5⍝ ones the program strobed out on R0 to R7: nothing else computes. Keys:
6⍝ 0-9 A-F (either case), + - * =, Enter (=), X (clear); q or Escape
7⍝ quits, as does the end of the input. Run it in a terminal with
8⍝ "scripts/xt run libs/Hex1000/demos/terminal.xtl" (or "just calc"), or
9⍝ script it: printf '12+34=' | scripts/xt run ...
10⍝ A session is at most 10000 keys (a bounded loop: ask F14).
11ʰˣ⁼u̲se< "Hex1000"
12ʳˣ⁼u̲se< "Radix"
13ˢ⁼u̲se< "Strings"
14⍝ The next key; the end of the input reads as Escape.
15ᵘk̲ey ← { u → '{ v → ⎕K̲EY v } ⎕T̲RAP '{ e → ⎕R̲ECOVER ⎕K̲NAMED 6 } }
16⍝ A key's code (Radix's): Enter is =, anything else not a key is 21.
17ᵘc̲ode ← { k →
18 k = ⎕K̲NAMED 5 ? 19
19 f̲irst (ʳˣk̲eys ˢu̲pper ⎕K̲CHAR k) c̲at 21
20}
21⍝ The screen: the display as the machine left it, and what the key cost.
22ᵘd̲raw ← { (digits, key, took) →
23 face ← " +----------+\n | " c̲at (ʰˣs̲creen r̲ev digits) c̲at " |\n +----------+"
24 cost ← " key " c̲at key c̲at ": " c̲at (f̲ormat took) c̲at " instructions, " c̲at (f̲ormat (2 × took) d̲iv 27) c̲at " microseconds on a Tang Nano"
25 help ← " keys: 0-9 A-F + - * = Enter (=) X (clear) q (quit)"
26 p̲rint! (⎕C̲LS @) c̲at " HEX1000, a 4-bit machine, computing in X_eTaL\n\n" c̲at face c̲at "\n\n" c̲at cost c̲at "\n" c̲at help
27}
28⍝ One key: run the machine until it is ready for the next, then draw.
29ᵘp̲ress ← { (t, digits, quit) →
30 quit ? (t, digits, quit)
31 k ← ᵘk̲ey @
32 (k = ⎕K̲NAMED 6) ∨ "q" m̲atch ⎕K̲CHAR k ? (t, digits, 1 = 1)
33 c ← ᵘc̲ode k
34 c = 21 ? (t, digits, quit)
35 (t1, d1, done, took) ← ʰˣCalcRom ʰˣt̲ypeOn (t, digits, 1 r̲eshape c, 1500 + 15000 × c ≥ 16)
36 shown ← ᵘd̲raw (d1, d̲isclose (1 + c) s̲elect ˢw̲ords "0 1 2 3 4 5 6 7 8 9 A B C D E F + - * = X", took)
37 (t1, d1, quit)
38}
39(t0, d0, done, took) ← ʰˣCalcRom ʰˣt̲ype (ʰˣReset, 0 r̲eshape 0, 2000)
40shown ← ᵘd̲raw (d0, "reset", took)
41(t, digits, quit) ← 10000 'ᵘp̲ress p̲ower (t0, d0, 0 = 1)