saturnus-host 0.12.1

Host-side pieces shared by the Saturnus emulator's front ends: the emulator with its key queue, command-line typing, hidden Kermit transfers, key layouts and skins, ROM identification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
//! What a host needs around an [`Emulator`] to run it behind the front
//! end's command/event protocol (`web/protocol.md`): the key queue that
//! times presses and types letters in emulated time ([`KeyQueue`]), the
//! slice runner that feeds it, the change-detected `frame` and `keys`
//! events, and the choice of model for a ROM file.
//!
//! Every host uses this through [`crate::protocol`], which adds the
//! wall-clock pacing; everything here is in emulated time and has no I/O.

use std::collections::VecDeque;

use saturnus_core::io::Key;
use saturnus_core::{LCD_WIDTH, Machine, Model};

use crate::layout;
use crate::skins::{self, Typing};
use crate::{AnnunciatorFlags, Emulator, Error};

/// Shortest key press the ROM sees, in emulated ms (its debounce needs >10).
pub const MIN_HOLD_MS: f64 = 60.0;
/// Shortest pause between two queued key presses, in emulated ms.
pub const GAP_MS: f64 = 30.0;
/// How long a queued press waits for the ROM to go idle (SHUTDN) after
/// the previous key, in emulated ms; while the ROM stays busy longer, as
/// in a running program, the press goes in anyway. A press sent while
/// the ROM still handles the last key may be lost (the 48SX ROM drops
/// it), so the cap lies above the longest time each ROM stays awake
/// after an ordinary key: measured over 40 timer phases for a shift, a
/// digit, ENTER and a function key (kb: decision log, "Key waits per
/// model"), plus a margin; 300 ms where the ROM never stays awake that
/// long. The 48SX stays awake about 250 ms or, by its timer's phase,
/// about 525 ms after a shift, and up to 750 ms after √x.
pub fn busy_gap_ms(model: Model) -> f64 {
    match model {
        // Measured up to 753 ms (√x), 605 (ENTER), 583 (shift).
        Model::Hp48sx => 850.0,
        // Up to 521 ms (shift), 516 (ENTER).
        Model::Hp48gx => 600.0,
        // Up to 462 ms (ENTER).
        Model::Hp49g => 550.0,
        // Up to 968 ms (ENTER), 523 (a digit, √x, shift).
        Model::Hp38g => 1100.0,
        // Up to 244 ms (39G, 40G) and 78 ms (42S); the 27S, 17BII and
        // 17B are taken as the 42S (same board, not measured).
        Model::Hp39g
        | Model::Hp40g
        | Model::Hp42s
        | Model::Hp27s
        | Model::Hp17bii
        | Model::Hp17b => 300.0,
        // Not measured: the 28S shows its busy annunciator for about
        // 210 ms after ENTER and 2 ENTER 3 + (28S ROM 2BB); the 19BII is
        // taken as the 28S (same board).
        Model::Hp28s | Model::Hp19bii => 300.0,
    }
}
/// Shortest pause before a key is pressed again right after its own
/// release, in emulated ms. The 39G and 40G ROM takes a key pressed again
/// within about 95 ms of its release as the same press and drops it (in
/// Saturnus, 1133 typed as 13, twelve 1s in a row as six; seen before and
/// after the held-keys fix of 2026-10-13); 150 ms leaves a margin. The
/// other models take [`GAP_MS`] (tried on the 38G, 27S, 17B, 17BII and
/// 19BII: 1133.555 typed whole).
pub fn repeat_gap_ms(model: Model) -> f64 {
    match model {
        Model::Hp39g | Model::Hp40g => 150.0,
        _ => GAP_MS,
    }
}

/// Quiet time before a typed letter or a shifted press reads an
/// annunciator while the ROM stays busy, in emulated ms, at least: the
/// 48SX ROM blinks alpha while it redraws the command line, up to about
/// 250 ms after a key is released. Never shorter than the model's
/// [`busy_gap_ms`], which a queued press waits for first.
pub const LETTER_SETTLE_MS: f64 = 400.0;
/// Runs are cut in slices of this many emulated ms, so key timing is fine.
pub const SLICE_MS: f64 = 10.0;

/// The machine as the key queue sees it.
pub trait Keyboard {
    /// Emulated milliseconds since power-on.
    fn now_ms(&self) -> f64;
    /// True while the CPU sleeps in SHUTDN.
    fn idle(&self) -> bool;
    /// The alpha annunciator.
    fn alpha_on(&self) -> bool;
    /// The annunciator of shift key `shift` (`leftshift`, `rightshift`);
    /// a model with one `shift` key reports either.
    fn shift_on(&self, shift: &str) -> bool;
    /// Press the key named `name`.
    fn down(&mut self, name: &str) -> crate::Result<()>;
    /// Release the key named `name`.
    fn up(&mut self, name: &str);
}

impl Keyboard for Machine {
    fn now_ms(&self) -> f64 {
        self.cycles() as f64 * 1000.0 / f64::from(self.model().clock_hz())
    }

    fn idle(&self) -> bool {
        self.is_shutdown()
    }

    fn alpha_on(&self) -> bool {
        self.framebuffer().annunciators.alpha
    }

    fn shift_on(&self, shift: &str) -> bool {
        let a = self.framebuffer().annunciators;
        match shift {
            "leftshift" => a.left_shift,
            "rightshift" => a.right_shift,
            _ => a.left_shift || a.right_shift,
        }
    }

    fn down(&mut self, name: &str) -> crate::Result<()> {
        let k = Key::from_name(name).ok_or_else(|| format!("unknown key {name:?}"))?;
        Ok(self.key_down(k)?)
    }

    fn up(&mut self, name: &str) {
        if let Some(k) = Key::from_name(name) {
            // Only fails for a key the model lacks, which was never pressed.
            let _ = self.key_up(k);
        }
    }
}

/// One key press, queued or down.
#[derive(Clone, Debug)]
struct Press {
    name: &'static str,
    /// Released by the user (or a typed press, released once held long
    /// enough).
    up: bool,
    /// Emulated ms it went down.
    down_at: f64,
    /// Pressed by the queue for a typed letter or key sequence.
    typed: bool,
}

#[derive(Clone, Debug)]
enum Item {
    Press(Press),
    /// A letter, expanded into presses when its turn comes.
    Letter {
        letter: char,
        lower: bool,
    },
    /// A press of a key's shifted function (a modifier click): when its
    /// turn comes, the shift is tapped first unless its annunciator is on.
    Shifted {
        shift: &'static str,
        press: Press,
    },
}

/// The shift keys a [`KeyQueue::press_shifted`] may name.
const SHIFT_KEYS: [&str; 3] = ["leftshift", "rightshift", "shift"];

/// Key presses in emulated time: `active` keys are down in the machine;
/// `pending` presses (or letters, expanded when their turn comes) wait for
/// a fast-typed predecessor to be released first.
#[derive(Clone, Debug)]
pub struct KeyQueue {
    /// The model's keys (its drawn layout), the only names accepted.
    names: Vec<&'static str>,
    /// `None` on a model without typing data (the 42S types letters from
    /// its ALPHA menus), which then types no letters.
    typing: Option<Typing>,
    letters: Vec<(char, &'static str)>,
    active: Vec<Press>,
    pending: VecDeque<Item>,
    last_release_ms: f64,
    /// The key released last.
    last_released: Option<&'static str>,
    /// [`repeat_gap_ms`] of the model.
    repeat_gap_ms: f64,
    /// Presses the machine took since the queue was made.
    started: u64,
    /// [`busy_gap_ms`] of the model.
    busy_gap_ms: f64,
    /// [`LETTER_SETTLE_MS`], or the model's busy gap if longer.
    settle_ms: f64,
    /// Alpha is known to be off: a typed letter pressed alpha and its key
    /// used it up, and no alpha press came since. Letters then skip reading
    /// the annunciator and the settling wait.
    alpha_spent: bool,
    /// Errors from presses the machine refused, for the host to report.
    errors: Vec<Error>,
}

impl KeyQueue {
    /// An empty queue for `model`'s keys and typing rules.
    pub fn new(model: Model) -> Self {
        Self {
            names: layout::layout(model).iter().map(|k| k.name).collect(),
            typing: skins::typing(model),
            letters: skins::letters(model),
            active: Vec::new(),
            pending: VecDeque::new(),
            last_release_ms: f64::NEG_INFINITY,
            last_released: None,
            repeat_gap_ms: repeat_gap_ms(model),
            started: 0,
            busy_gap_ms: busy_gap_ms(model),
            settle_ms: busy_gap_ms(model).max(LETTER_SETTLE_MS),
            alpha_spent: false,
            errors: Vec::new(),
        }
    }

    fn name(&self, name: &str) -> Option<&'static str> {
        self.names.iter().copied().find(|&n| n == name)
    }

    /// Whether the model has a key called `name` (its drawn layout).
    pub fn has_key(&self, name: &str) -> bool {
        self.name(name).is_some()
    }

    /// Keys are down or presses are queued: the run must keep its slices
    /// short and pump.
    pub fn busy(&self) -> bool {
        !self.active.is_empty() || !self.pending.is_empty()
    }

    /// A key the user pressed is down and not yet released: its hold
    /// must run at 1x, or a tap at a higher speed reads as a long hold
    /// (the 42S shows NULL after about a second).
    pub fn held(&self) -> bool {
        self.active.iter().any(|p| !p.up)
    }

    /// Queue a press of `name` (held until [`KeyQueue::release`]); false
    /// if the model has no such key.
    pub fn press(&mut self, name: &str) -> bool {
        let Some(name) = self.name(name) else {
            return false;
        };
        self.pending.push_back(Item::Press(Press {
            name,
            up: false,
            down_at: 0.0,
            typed: false,
        }));
        true
    }

    /// Queue a press of `name` held until [`KeyQueue::release`], after a
    /// tap of shift key `shift` unless that shift is on when the press's
    /// turn comes: decided then, not now, so a shift still queued or one
    /// a queued key will use up counts. False if the model has no such key
    /// or `shift` is not one of its shift keys.
    pub fn press_shifted(&mut self, name: &str, shift: &str) -> bool {
        let (Some(name), Some(shift)) = (self.name(name), self.name(shift)) else {
            return false;
        };
        if !SHIFT_KEYS.contains(&shift) {
            return false;
        }
        self.pending.push_back(Item::Shifted {
            shift,
            press: Press {
                name,
                up: false,
                down_at: 0.0,
                typed: false,
            },
        });
        true
    }

    /// Queue `ch` typed through the calculator's alpha mode; false if the
    /// model has no key for it.
    pub fn type_letter(&mut self, ch: char) -> bool {
        let upper = ch.to_ascii_uppercase();
        if self.typing.is_none() || !self.letters.iter().any(|&(c, _)| c == upper) {
            return false;
        }
        self.pending.push_back(Item::Letter {
            letter: upper,
            lower: ch != upper,
        });
        true
    }

    /// Queue full presses of `names`, one after the other (unknown names
    /// are skipped).
    pub fn type_keys(&mut self, names: &[&str]) {
        for n in names {
            if let Some(name) = self.name(n) {
                self.pending.push_back(Item::Press(Press {
                    name,
                    up: true,
                    down_at: 0.0,
                    typed: true,
                }));
            }
        }
    }

    /// Release the newest press of `name` that the user still holds.
    pub fn release(&mut self, name: &str) {
        let queued = self.pending.iter_mut().rev().filter_map(|i| match i {
            Item::Press(p) | Item::Shifted { press: p, .. } => Some(p),
            Item::Letter { .. } => None,
        });
        if let Some(p) = queued
            .chain(self.active.iter_mut().rev())
            .find(|p| p.name == name && !p.up)
        {
            p.up = true;
        }
    }

    /// Release every held key once it has been down long enough (the
    /// window lost the focus).
    pub fn release_held(&mut self) {
        for p in &mut self.active {
            p.up = true;
        }
        for i in &mut self.pending {
            if let Item::Press(p) | Item::Shifted { press: p, .. } = i {
                p.up = true;
            }
        }
    }

    /// Drop the queue and forget every key; the machine's keys stay as
    /// they are ([`Emulator::release_keys`] releases both).
    pub fn clear(&mut self) {
        self.active.clear();
        self.pending.clear();
        self.last_release_ms = f64::NEG_INFINITY;
        self.last_released = None;
        self.alpha_spent = false;
    }

    /// The keys down now, for the view.
    pub fn down(&self) -> Vec<&'static str> {
        let mut v: Vec<&'static str> = self.active.iter().map(|p| p.name).collect();
        v.dedup();
        v
    }

    /// Presses the machine took since the queue was made (each key of a
    /// typed letter counts, alpha too).
    pub fn presses_started(&self) -> u64 {
        self.started
    }

    /// Errors collected since the last call.
    pub fn take_errors(&mut self) -> Vec<Error> {
        std::mem::take(&mut self.errors)
    }

    /// The presses that type a queued letter now: the letter's key after
    /// the alpha key, unless alpha is already on (one alpha press lasts for
    /// the next key; two lock it on the 48 and 49G, cancel it on the
    /// others); a lowercase letter adds the model's shift, before or after
    /// alpha as the model wants. Alpha is read from the annunciator, except
    /// right after a letter this queue pressed alpha for: that alpha was
    /// spent by its key.
    fn expand_letter(&mut self, kb: &impl Keyboard, letter: char, lower: bool) -> Vec<Item> {
        let Some(t) = self.typing else {
            return Vec::new();
        };
        let Some(&(_, key)) = self.letters.iter().find(|&&(c, _)| c == letter) else {
            return Vec::new();
        };
        let mut seq = Vec::new();
        if t.direct {
            // The 28S: the letter's own key; LC switches lowercase on
            // before it and off after it.
            if lower {
                seq.extend([t.lower_shift, key, t.lower_shift]);
            } else {
                seq.push(key);
            }
            return self.presses(seq);
        }
        let alpha_on = !self.alpha_spent && kb.alpha_on();
        if t.shift_first && lower {
            seq.push(t.lower_shift);
        }
        if !alpha_on {
            seq.push(t.alpha);
        }
        if !t.shift_first && lower {
            seq.push(t.lower_shift);
        }
        seq.push(key);
        self.alpha_spent = !alpha_on;
        self.presses(seq)
    }

    /// Full presses of the keys `seq` names (unknown names skipped).
    fn presses(&self, seq: Vec<&str>) -> Vec<Item> {
        seq.into_iter()
            .filter_map(|n| self.name(n))
            .map(|name| {
                Item::Press(Press {
                    name,
                    up: true,
                    down_at: 0.0,
                    typed: true,
                })
            })
            .collect()
    }

    /// Release keys held long enough, then start queued presses.
    pub fn pump(&mut self, kb: &mut impl Keyboard) {
        let now = kb.now_ms();
        let mut released = false;
        let mut last = self.last_released;
        self.active.retain(|p| {
            if p.up && now - p.down_at >= MIN_HOLD_MS {
                kb.up(p.name);
                last = Some(p.name);
                released = true;
                false
            } else {
                true
            }
        });
        if released {
            self.last_release_ms = now;
            self.last_released = last;
        }
        let idle = kb.idle();
        while let Some(head) = self.pending.front() {
            // Wait while a fast-typed key is still on its way up; a key the
            // user keeps holding (ON for a chord) does not block.
            if self.active.iter().any(|p| p.up) {
                break;
            }
            let since = now - self.last_release_ms;
            if since < GAP_MS || (!idle && since < self.busy_gap_ms) {
                break;
            }
            if let &Item::Letter { letter, lower } = head {
                // A letter waits for an empty keyboard, and, unless it
                // follows a letter whose alpha this queue pressed, for the
                // ROM to settle the alpha annunciator it reads.
                if !self.active.is_empty() {
                    break;
                }
                if !self.alpha_spent && !idle && since < self.settle_ms {
                    break;
                }
                self.pending.pop_front();
                for item in self.expand_letter(kb, letter, lower).into_iter().rev() {
                    self.pending.push_front(item);
                }
                continue;
            }
            if let &Item::Shifted { shift, .. } = head {
                // As a letter: the shift annunciator is read with every key
                // before it played and settled (a shift queued before it
                // lit it, a key before it used it up).
                if !self.active.is_empty() || (!idle && since < self.settle_ms) {
                    break;
                }
                let Some(Item::Shifted { press, .. }) = self.pending.pop_front() else {
                    continue;
                };
                self.pending.push_front(Item::Press(press));
                if !kb.shift_on(shift) {
                    self.pending.push_front(Item::Press(Press {
                        name: shift,
                        up: true,
                        down_at: 0.0,
                        typed: true,
                    }));
                }
                continue;
            }
            if let Item::Press(p) = head
                && Some(p.name) == self.last_released
                && since < self.repeat_gap_ms
            {
                break;
            }
            let Some(Item::Press(mut p)) = self.pending.pop_front() else {
                continue;
            };
            if self.typing.is_some_and(|t| p.name == t.alpha) && !p.typed {
                self.alpha_spent = false;
            }
            if let Some(o) = self.active.iter_mut().find(|o| o.name == p.name) {
                // Same key still down (held): count this press as the same one.
                if p.up {
                    o.up = true;
                }
                continue;
            }
            if let Err(e) = kb.down(p.name) {
                self.errors.push(e);
                continue;
            }
            p.down_at = now;
            self.started += 1;
            self.active.push(p);
        }
    }
}

/// Packed LCD bytes per row of `width` pixels.
pub fn packed_row_bytes(width: usize) -> usize {
    width.div_ceil(8)
}

/// The pixels packed one bit each, row-major from the top-left, each row
/// starting on a byte, the leftmost pixel in the most significant bit,
/// 1 = dark (`web/protocol.md`, `frame`).
pub fn pack_bits(rows: &[Vec<bool>]) -> Vec<u8> {
    let w = rows.first().map_or(LCD_WIDTH, Vec::len);
    let mut out = vec![0u8; packed_row_bytes(w) * rows.len()];
    for (y, row) in rows.iter().enumerate() {
        for (x, &on) in row.iter().enumerate() {
            if on {
                out[y * packed_row_bytes(w) + x / 8] |= 0x80 >> (x % 8);
            }
        }
    }
    out
}

/// Standard base64 with padding.
pub fn base64(bytes: &[u8]) -> String {
    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for chunk in bytes.chunks(3) {
        let b = [
            chunk[0],
            chunk.get(1).copied().unwrap_or(0),
            chunk.get(2).copied().unwrap_or(0),
        ];
        let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
        for i in 0..4 {
            if i <= chunk.len() {
                out.push(char::from(ALPHABET[(n >> (18 - 6 * i)) as usize & 63]));
            } else {
                out.push('=');
            }
        }
    }
    out
}

/// Decode standard base64 (padding optional, no whitespace): the JSON form
/// of the protocol's *bytes* fields.
pub fn base64_decode(s: &str) -> crate::Result<Vec<u8>> {
    fn value(c: u8) -> Option<u32> {
        Some(u32::from(match c {
            b'A'..=b'Z' => c - b'A',
            b'a'..=b'z' => c - b'a' + 26,
            b'0'..=b'9' => c - b'0' + 52,
            b'+' => 62,
            b'/' => 63,
            _ => return None,
        }))
    }
    let s = s.trim_end_matches('=').as_bytes();
    if s.len() % 4 == 1 {
        return Err("bad base64 length".into());
    }
    let mut out = Vec::with_capacity(s.len() * 3 / 4);
    for chunk in s.chunks(4) {
        let mut n = 0u32;
        for (i, &c) in chunk.iter().enumerate() {
            n |= value(c).ok_or("bad base64 character")? << (18 - 6 * i);
        }
        let bytes = n.to_be_bytes();
        out.extend_from_slice(&bytes[1..chunk.len()]);
    }
    Ok(out)
}

/// Serialize bytes as base64 (`web/protocol.md`: `frame`'s `pixels`,
/// `serial`'s `bytes`).
pub(crate) fn as_base64<S: serde::Serializer>(bytes: &[u8], s: S) -> Result<S::Ok, S::Error> {
    s.serialize_str(&base64(bytes))
}

/// The `frame` event (`web/protocol.md`): the display, sent when it
/// changed. Serializes as `{"type":"frame","width","height","pixels",
/// "annunciators","contrast","contrastRange","contrastDefault"}`.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
#[serde(tag = "type", rename = "frame", rename_all = "camelCase")]
pub struct Frame {
    /// Pixels per row: 131.
    pub width: usize,
    /// Rows: 64, or 16 on the 42S.
    pub height: usize,
    /// The pixels packed one bit each ([`pack_bits`]); base64 in JSON.
    #[serde(serialize_with = "as_base64")]
    pub pixels: Vec<u8>,
    /// The annunciators.
    pub annunciators: AnnunciatorFlags,
    /// Raw 5-bit contrast, 0-31, higher is darker.
    pub contrast: u8,
    /// The model's usable contrast range, `[low, high]`.
    pub contrast_range: [u8; 2],
    /// The contrast the ROM sets at power-on.
    pub contrast_default: u8,
}

/// The `keys` event (`web/protocol.md`): the keys down, sent when they
/// changed. Serializes as `{"type":"keys","down":[names]}`.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
#[serde(tag = "type", rename = "keys")]
pub struct KeysDown {
    /// Script names of the keys down.
    pub down: Vec<&'static str>,
}

/// The `menu` event (`web/protocol.md`): the six menu keys the screen
/// shows (48SX, 48GX, 49G; the 42S, the Pioneers and the 28S), sent when
/// they changed. Serializes as
/// `{"type":"menu","keys":[{label, command?, submenu?, type?} | null, ...]}`,
/// `keys` null where they are not known.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
#[serde(tag = "type", rename = "menu")]
pub struct MenuKeys {
    /// The keys A-F (F1-F6), `None` for a blank one.
    pub keys: Option<Vec<Option<saturnus_objects::softkeys::ShownKey>>>,
}

/// The model that runs `rom`, preferring `preferred`: a 2 MB file fits
/// both the 49G (packed) and the 39G/40G (unpacked, every byte a nibble);
/// the bytes tell which ([`crate::romid::fitting_models`]). `preferred`
/// when nothing fits (the boot then reports the size).
pub fn model_for_rom(rom: &[u8], preferred: Model) -> Model {
    let fits = crate::romid::fitting_models(rom);
    if fits.contains(&preferred) {
        return preferred;
    }
    fits.first().copied().unwrap_or(preferred)
}

impl Emulator {
    /// One slice of at most `left_ms` emulated ms: a sleeping CPU with no
    /// keys to time skips to its next timer event in one step; with `keys`
    /// the queue is fed afterwards. Returns the emulated ms run.
    pub fn run_slice(&mut self, left_ms: f64, keys: bool) -> crate::Result<f64> {
        if !left_ms.is_finite() || left_ms <= 0.0 {
            return Ok(0.0);
        }
        let timed = keys && self.queue.busy();
        let idle = if timed { None } else { self.idle_ms() };
        let step = left_ms.min(idle.filter(|&i| i > SLICE_MS).unwrap_or(SLICE_MS));
        self.run_ms(step)?;
        if keys {
            self.queue.pump(&mut self.machine);
        }
        Ok(step)
    }

    /// Feed the key queue now (after a command added to it).
    pub fn pump(&mut self) {
        self.queue.pump(&mut self.machine);
    }

    /// The key queue.
    pub fn queue(&mut self) -> &mut KeyQueue {
        &mut self.queue
    }

    /// Release every key at once: the machine's keyboard (matrix and ON)
    /// and the key queue (held presses, presses and letters waiting, with
    /// no minimum hold), as after a reset, a state load or before a
    /// script. The one way to let go of everything; [`KeyQueue::release_held`]
    /// is the gentle one (the window lost the focus).
    pub fn release_keys(&mut self) {
        self.machine.release_all_keys();
        self.queue.clear();
    }

    /// The `frame` event if the display changed since the last one
    /// (pixels, annunciators or contrast), else `None`.
    pub fn frame_if_changed(&mut self) -> Option<Frame> {
        let fb = self.machine.framebuffer();
        let model = self.machine.model();
        let range = model.contrast_range();
        let frame = Frame {
            width: fb.pixels.width(),
            height: fb.pixels.pixels.len(),
            pixels: pack_bits(&fb.pixels.pixels),
            annunciators: AnnunciatorFlags(fb.annunciators),
            contrast: fb.contrast,
            contrast_range: [*range.start(), *range.end()],
            contrast_default: model.default_contrast(),
        };
        if self.shown.as_ref() == Some(&frame) {
            return None;
        }
        self.shown = Some(frame.clone());
        Some(frame)
    }

    /// Forget what was shown, so the next [`Emulator::frame_if_changed`]
    /// and [`Emulator::keys_if_changed`] send their events (a new view
    /// subscribed, a state was loaded).
    pub fn reshow(&mut self) {
        self.shown = None;
        self.shown_keys = None;
        self.menu_seen = None;
        self.shown_menu = None;
        self.menu_dirty = true;
        self.menu_force = true;
    }

    /// The `keys` event if the keys down changed since the last one, else
    /// `None`.
    pub fn keys_if_changed(&mut self) -> Option<KeysDown> {
        let down = self.queue.down();
        if self.shown_keys.as_deref() == Some(down.as_slice()) {
            return None;
        }
        self.shown_keys = Some(down.clone());
        Some(KeysDown { down })
    }
}

impl Emulator {
    /// Queue a press of `name`, held until `release(name)`; false if the
    /// model has no such key. Takes effect at the next `pump`.
    pub fn press(&mut self, name: &str) -> bool {
        self.queue.press(name)
    }

    /// Queue a press of `name` after a tap of shift key `shift`, unless
    /// that shift is on when it plays ([`KeyQueue::press_shifted`]); false
    /// if the model has no such key or shift.
    pub fn press_shifted(&mut self, name: &str, shift: &str) -> bool {
        self.queue.press_shifted(name, shift)
    }

    /// Whether the model has a key called `name`.
    pub fn has_key(&self, name: &str) -> bool {
        self.queue.has_key(name)
    }

    /// Release the newest held press of `name` (at the next `pump`).
    pub fn release(&mut self, name: &str) {
        self.queue.release(name);
    }

    /// Release every held key once it was down long enough.
    pub fn release_held(&mut self) {
        self.queue.release_held();
    }

    /// Queue the first character of `ch` typed through alpha mode; false
    /// if the model has no key for it.
    pub fn type_letter(&mut self, ch: &str) -> bool {
        ch.chars().next().is_some_and(|c| self.queue.type_letter(c))
    }

    /// Queue full presses of the space-separated key `names`.
    pub fn type_keys(&mut self, names: &str) {
        let names: Vec<&str> = names.split_whitespace().collect();
        self.queue.type_keys(&names);
    }

    /// Keys are down or queued: run in short slices.
    pub fn keys_busy(&self) -> bool {
        self.queue.busy()
    }

    /// A key the user pressed is still down ([`KeyQueue::held`]).
    pub fn keys_held(&self) -> bool {
        self.queue.held()
    }

    /// Errors from refused key presses since the last call.
    pub fn take_errors(&mut self) -> Vec<Error> {
        self.queue.take_errors()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A keyboard that records presses; time and idleness are set by hand.
    /// Its shift annunciators follow the keys as a ROM's do: a shift key
    /// turns its shift on (the other off), again off; any other key uses it
    /// up.
    #[derive(Default)]
    struct Fake {
        now: f64,
        idle: bool,
        /// The ROM stays awake this long after each key goes up.
        busy_after_up: f64,
        last_up: f64,
        /// Keys pressed while the ROM was awake (it may drop them).
        pressed_busy: Vec<String>,
        /// When each key went down.
        down_at: Vec<f64>,
        alpha: bool,
        left: bool,
        right: bool,
        log: Vec<String>,
    }

    impl Keyboard for Fake {
        fn now_ms(&self) -> f64 {
            self.now
        }
        fn idle(&self) -> bool {
            self.idle && self.now >= self.last_up + self.busy_after_up
        }
        fn alpha_on(&self) -> bool {
            self.alpha
        }
        fn shift_on(&self, shift: &str) -> bool {
            match shift {
                "rightshift" => self.right,
                "leftshift" => self.left,
                _ => self.left || self.right,
            }
        }
        fn down(&mut self, name: &str) -> crate::Result<()> {
            self.log.push(format!("+{name}"));
            self.down_at.push(self.now);
            if !self.idle() {
                self.pressed_busy.push(name.to_string());
            }
            match name {
                "leftshift" | "shift" => (self.left, self.right) = (!self.left, false),
                "rightshift" => (self.left, self.right) = (false, !self.right),
                _ => (self.left, self.right) = (false, false),
            }
            Ok(())
        }
        fn up(&mut self, name: &str) {
            self.log.push(format!("-{name}"));
            self.last_up = self.now;
        }
    }

    /// Run the fake in 10 ms steps for `ms`, pumping after each.
    fn run(q: &mut KeyQueue, kb: &mut Fake, ms: f64) {
        let end = kb.now + ms;
        while kb.now < end {
            kb.now += 10.0;
            q.pump(kb);
        }
    }

    #[test]
    fn a_press_is_held_at_least_min_hold() {
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        assert!(q.press("enter"));
        assert!(!q.press("apps"), "a 49G key on a 48SX");
        q.pump(&mut kb);
        assert_eq!(kb.log, ["+enter"]);
        assert_eq!(q.down(), ["enter"]);
        q.release("enter");
        kb.now = 50.0;
        q.pump(&mut kb);
        assert_eq!(kb.log, ["+enter"], "released before 60 ms");
        kb.now = 60.0;
        q.pump(&mut kb);
        assert_eq!(kb.log, ["+enter", "-enter"]);
        assert!(!q.busy());
    }

    #[test]
    fn queued_presses_wait_for_the_gap_and_a_busy_rom() {
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake::default();
        q.type_keys(&["1", "2"]);
        q.pump(&mut kb);
        assert_eq!(kb.log, ["+1"]);
        // Busy ROM: the next press waits the 48SX's busy gap after the release at 60.
        run(&mut q, &mut kb, 50.0 + busy_gap_ms(Model::Hp48sx));
        assert_eq!(kb.log, ["+1", "-1"]);
        run(&mut q, &mut kb, 20.0);
        assert_eq!(kb.log, ["+1", "-1", "+2"]);
        // Idle ROM: only GAP_MS.
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        q.type_keys(&["1", "2"]);
        // Down at 10, up at 70, the next one 30 ms later.
        run(&mut q, &mut kb, 100.0);
        assert_eq!(kb.log, ["+1", "-1", "+2"]);
    }

    #[test]
    fn a_busy_rom_takes_keys_at_its_models_rate() {
        // A program keeps the ROM awake: each queued key is held 60 ms and
        // the next waits the model's busy gap, so the 39G (which sleeps
        // within 250 ms of a key) takes keys every 360 ms, about 2.8 a
        // second, and the 48SX, which may stay awake 750 ms, every 910 ms.
        for (model, every) in [
            (Model::Hp39g, 360.0),
            (Model::Hp42s, 360.0),
            (Model::Hp48sx, 910.0),
        ] {
            let mut q = KeyQueue::new(model);
            let mut kb = Fake::default();
            q.type_keys(&["1", "2", "3", "4"]);
            run(&mut q, &mut kb, 5000.0);
            assert_eq!(downs(&kb), ["1", "2", "3", "4"], "{model:?}");
            let gaps: Vec<f64> = kb.down_at.windows(2).map(|w| w[1] - w[0]).collect();
            assert_eq!(gaps, [every; 3], "{model:?}");
        }
    }

    /// The keys pressed, in order.
    fn downs(kb: &Fake) -> Vec<&str> {
        kb.log.iter().filter_map(|s| s.strip_prefix('+')).collect()
    }

    #[test]
    fn two_quick_shifted_presses_each_get_their_shift() {
        // Two Ctrl+clicks on √x in quick succession: both are queued before
        // the first plays, and the first one's shift is spent by its key,
        // so the second taps the shift again.
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        assert!(q.press_shifted("sqrt", "leftshift"));
        q.pump(&mut kb);
        q.release("sqrt");
        assert!(q.press_shifted("sqrt", "leftshift"));
        q.release("sqrt");
        run(&mut q, &mut kb, 1000.0);
        assert_eq!(downs(&kb), ["leftshift", "sqrt", "leftshift", "sqrt"]);
        assert!(!q.busy());
    }

    #[test]
    fn quick_shifted_presses_wait_out_the_roms_long_key_wait() {
        // The 48SX ROM, depending on its timer's phase, stays awake about
        // 520 ms after a key goes up and may drop a key pressed then: two
        // quick Ctrl+clicks lost the second shift and gave √x for x²
        // (the page test under load). Every press waits for the ROM.
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            busy_after_up: 520.0,
            last_up: f64::NEG_INFINITY,
            ..Fake::default()
        };
        assert!(q.press_shifted("sqrt", "leftshift"));
        q.release("sqrt");
        assert!(q.press_shifted("sqrt", "leftshift"));
        q.release("sqrt");
        run(&mut q, &mut kb, 5000.0);
        assert_eq!(downs(&kb), ["leftshift", "sqrt", "leftshift", "sqrt"]);
        assert_eq!(
            kb.pressed_busy,
            Vec::<String>::new(),
            "no key pressed while the ROM was awake"
        );
        assert!(!q.busy());
    }

    #[test]
    fn a_shifted_press_after_a_queued_shift_does_not_tap_it_again() {
        // A click on the left shift, then at once a Ctrl+click: the shift
        // is still queued, then on when the shifted press plays.
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        assert!(q.press("leftshift"));
        q.release("leftshift");
        assert!(q.press_shifted("sqrt", "leftshift"));
        q.release("sqrt");
        run(&mut q, &mut kb, 1000.0);
        assert_eq!(downs(&kb), ["leftshift", "sqrt"]);
        // The right shift on, a left-shifted press: the left one is tapped.
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            right: true,
            ..Fake::default()
        };
        assert!(q.press_shifted("sqrt", "leftshift"));
        q.release("sqrt");
        run(&mut q, &mut kb, 1000.0);
        assert_eq!(downs(&kb), ["leftshift", "sqrt"]);
    }

    #[test]
    fn a_shifted_press_is_held_until_released_and_checks_its_names() {
        let mut q = KeyQueue::new(Model::Hp39g);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        assert!(
            !q.press_shifted("enter", "leftshift"),
            "no left shift on a 39G"
        );
        assert!(!q.press_shifted("enter", "alpha"), "not a shift key");
        assert!(!q.press_shifted("bogus", "shift"));
        assert!(q.press_shifted("enter", "shift"));
        run(&mut q, &mut kb, 500.0);
        assert_eq!(downs(&kb), ["shift", "enter"]);
        assert_eq!(q.down(), ["enter"], "held while the button is");
        q.release("enter");
        run(&mut q, &mut kb, 100.0);
        assert!(!q.busy());
        // One shift: either annunciator counts as on.
        let mut q = KeyQueue::new(Model::Hp39g);
        let mut kb = Fake {
            idle: true,
            right: true,
            ..Fake::default()
        };
        assert!(q.press_shifted("enter", "shift"));
        q.release("enter");
        run(&mut q, &mut kb, 500.0);
        assert_eq!(downs(&kb), ["enter"]);
    }

    #[test]
    fn letters_press_alpha_then_skip_it_while_spent() {
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        assert!(q.type_letter('A'));
        assert!(q.type_letter('b'));
        assert!(!q.type_letter('!'));
        run(&mut q, &mut kb, 2000.0);
        let downs: Vec<&str> = kb.log.iter().filter_map(|s| s.strip_prefix('+')).collect();
        // A = alpha + key; b = alpha + left shift + key (shift after alpha).
        let a = skins::letters(Model::Hp48sx)
            .into_iter()
            .find(|&(c, _)| c == 'A')
            .map(|(_, k)| k)
            .unwrap();
        let b = skins::letters(Model::Hp48sx)
            .into_iter()
            .find(|&(c, _)| c == 'B')
            .map(|(_, k)| k)
            .unwrap();
        assert_eq!(downs, ["alpha", a, "alpha", "leftshift", b]);
        assert!(!q.busy());
    }

    /// The 28S has a key per letter: no alpha press, even with alpha on;
    /// a lowercase letter is LC, the key, LC.
    #[test]
    fn hp28s_letters_are_keys_of_their_own() {
        let mut q = KeyQueue::new(Model::Hp28s);
        let mut kb = Fake {
            idle: true,
            alpha: true,
            ..Fake::default()
        };
        assert!(q.type_letter('A'));
        assert!(q.type_letter('z'));
        assert!(!q.type_letter('1'));
        run(&mut q, &mut kb, 2000.0);
        let downs: Vec<&str> = kb.log.iter().filter_map(|s| s.strip_prefix('+')).collect();
        assert_eq!(downs, ["lettera", "lc", "letterz", "lc"]);
        assert!(!q.busy());
    }

    #[test]
    fn aplet_models_shift_before_alpha_and_respect_alpha_on() {
        let mut q = KeyQueue::new(Model::Hp38g);
        let mut kb = Fake {
            idle: true,
            alpha: true,
            ..Fake::default()
        };
        q.type_letter('c');
        run(&mut q, &mut kb, 1000.0);
        let downs: Vec<&str> = kb.log.iter().filter_map(|s| s.strip_prefix('+')).collect();
        // Alpha already on: shift, then the key, no alpha press.
        assert_eq!(downs.len(), 2);
        assert_eq!(downs[0], "shift");
    }

    #[test]
    fn release_held_lets_go_of_user_presses() {
        let mut q = KeyQueue::new(Model::Hp48sx);
        let mut kb = Fake {
            idle: true,
            ..Fake::default()
        };
        q.press("on");
        q.pump(&mut kb);
        q.release_held();
        run(&mut q, &mut kb, 100.0);
        assert_eq!(kb.log, ["+on", "-on"]);
    }

    #[test]
    fn packs_bits_msb_first_per_row() {
        let mut rows = vec![vec![false; LCD_WIDTH]; 2];
        rows[0][0] = true;
        rows[0][9] = true;
        rows[1][130] = true;
        let p = pack_bits(&rows);
        assert_eq!(p.len(), 2 * 17);
        assert_eq!(p[0], 0x80);
        assert_eq!(p[1], 0x40);
        assert_eq!(p[17 + 16], 0x20);
        assert_eq!(p.iter().map(|b| b.count_ones()).sum::<u32>(), 3);
    }

    #[test]
    fn base64_matches_rfc4648() {
        assert_eq!(base64(b""), "");
        assert_eq!(base64(b"f"), "Zg==");
        assert_eq!(base64(b"fo"), "Zm8=");
        assert_eq!(base64(b"foo"), "Zm9v");
        assert_eq!(base64(b"foobar"), "Zm9vYmFy");
        assert_eq!(base64(&[0xff, 0xfe]), "//4=");
        for n in 0..40 {
            let bytes: Vec<u8> = (0..n).map(|i| (i * 37 + 11) as u8).collect();
            assert_eq!(base64_decode(&base64(&bytes)).unwrap(), bytes);
        }
        assert_eq!(base64_decode("aGk").unwrap(), b"hi");
        assert!(base64_decode("a").is_err());
        assert!(base64_decode("a*bc").is_err());
    }

    #[test]
    fn rom_picks_the_model() {
        let two = 2 * 1024 * 1024;
        assert_eq!(
            model_for_rom(&vec![0u8; 256 * 1024], Model::Hp49g),
            Model::Hp48sx
        );
        assert_eq!(
            model_for_rom(&vec![0u8; 256 * 1024], Model::Hp48sx),
            Model::Hp48sx
        );
        // 2 MB of nibbles: an unpacked 39G/40G, not a 49G.
        assert_eq!(model_for_rom(&vec![0u8; two], Model::Hp49g), Model::Hp39g);
        assert_eq!(model_for_rom(&vec![0u8; two], Model::Hp40g), Model::Hp40g);
        // 2 MB with full bytes: the 49G.
        assert_eq!(
            model_for_rom(&vec![0xffu8; two], Model::Hp39g),
            Model::Hp49g
        );
        assert_eq!(model_for_rom(&[0u8; 1000], Model::Hp38g), Model::Hp38g);
    }

    #[test]
    fn frames_and_keys_only_when_changed() {
        let mut emu = Emulator::new(Model::Hp48sx, &vec![0u8; 256 * 1024]).unwrap();
        let f = serde_json::to_string(&emu.frame_if_changed().unwrap()).unwrap();
        assert!(
            f.starts_with("{\"type\":\"frame\",\"width\":131,\"height\":64,\"pixels\":\"AAAA"),
            "{f}"
        );
        assert!(f.contains(",\"annunciators\":{\"leftshift\":false,"), "{f}");
        assert!(
            f.ends_with(",\"contrastRange\":[3,19],\"contrastDefault\":11}"),
            "{f}"
        );
        assert!(emu.frame_if_changed().is_none());
        emu.reshow();
        assert!(emu.frame_if_changed().is_some());
        let keys = |emu: &mut Emulator| {
            emu.keys_if_changed()
                .map(|k| serde_json::to_string(&k).unwrap())
        };
        assert_eq!(
            keys(&mut emu).as_deref(),
            Some("{\"type\":\"keys\",\"down\":[]}")
        );
        assert!(emu.keys_if_changed().is_none());
        emu.queue().press("enter");
        emu.pump();
        assert_eq!(
            keys(&mut emu).as_deref(),
            Some("{\"type\":\"keys\",\"down\":[\"enter\"]}")
        );
    }
}