saturnus_core/machine/model.rs
1//! Supported calculator models and what each one wires behind its chip
2//! selects.
3//!
4//! Every model has the same six controllers in the Clarke (48SX) or Yorke
5//! (48GX, 49G) chip: NCE1, HDW, NCE2, CE1, CE2, NCE3 (wiki:
6//! hardware/memory-controller "Controller model (Giesselink)"). What sits
7//! behind them differs per model; [`HardwareProfile`] describes it and
8//! [`super::Hardware`] decodes accesses through it. Power-on is the same on
9//! every model: all controllers except NCE1 are unconfigured, the I/O
10//! registers cleared, the bank latch (where fitted) cleared; the ROM
11//! configures memory itself (wiki: hardware/memory-controller, tutorial
12//! p. 99, 151; Voyage p. 87, 130).
13
14use std::str::FromStr;
15
16use crate::bus::Chip;
17use crate::error::Error;
18use crate::io::Key;
19
20use super::hardware::Port;
21
22/// A supported calculator model.
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24pub enum Model {
25 /// HP48 SX: Clarke, 2 MHz, 256 KB ROM, 32 KB RAM, two 128 KB card
26 /// ports on CE1 and CE2.
27 Hp48sx,
28 /// HP48 GX: Yorke, 4 MHz, 512 KB ROM (upper half through DA19),
29 /// 128 KB RAM, CE1 bank latch, port 1 on CE2, banked port 2 on NCE3
30 /// (wiki: hardware/hp48gx).
31 Hp48gx,
32 /// HP 38G: Yorke, 4 MHz, 512 KB OTP ROM, 32 KB RAM that the ROM puts
33 /// at #F0000, no card ports (wiki: hardware/hp38g).
34 Hp38g,
35 /// HP49G: Yorke, 4 MHz, 2 MB flash on NCE1 banked by the CE1 latch,
36 /// 512 KB RAM on NCE2, CE2 and NCE3, its own keyboard matrix (wiki:
37 /// hardware/hp49g).
38 Hp49g,
39 /// HP 39G: the 49G board cut to a 1 MB mask ROM on NCE1, banked by the
40 /// CE1 latch, and 256 KB RAM on NCE2; nothing on CE2 or NCE3; the
41 /// 49G keyboard matrix with the 39G's labels (wiki: hardware/hp39g-40g).
42 Hp39g,
43 /// HP 40G: the 39G hardware without IR, running the same ROM, which
44 /// tells the two apart by a strap (wiki: hardware/hp39g-40g,
45 /// questions/hp39g-40g-model-detection).
46 Hp40g,
47 /// HP 42S: a Pioneer-series machine on the Lewis chip (1LR2), about
48 /// 1 MHz, 64 KB ROM at #00000, 8 KB RAM at #50000, the Lewis display
49 /// and register block at #40000, a 131x16 LCD, 37 keys, no card ports
50 /// and no serial port (wiki: hardware/hp42s, hardware/lewis).
51 Hp42s,
52 /// HP 27S: the 42S's board (one Lewis, ROM at #00000, display and
53 /// registers at #40000, 8 KB RAM at #50000, 131x16 LCD) with the 27S's
54 /// keyboard and 64 KB ROM (wiki: hardware/hp27s, hardware/saturn-models).
55 Hp27s,
56 /// HP 17BII: the 42S's board with the 17B keyboard, a 64 KB ROM
57 /// (English) or 96 KB (international: a 32 KB language ROM at
58 /// #20000), and a clock with alarm (wiki: hardware/hp17bii).
59 Hp17bii,
60 /// HP 17B: the 17BII's predecessor on the same board and keyboard
61 /// matrix, 64 or 96 KB ROM as the 17BII (wiki: hardware/hp17bii).
62 Hp17b,
63 /// HP 28S: a Clamshell machine on two Lewis chips, master and slave;
64 /// 128 KB ROM at #00000, 32 KB RAM at #C0000 (aliased at #D0000), the
65 /// slave's display and register block at #FF800 and the master's at
66 /// #FFC00, a 137x32 LCD split between them, no card ports and no
67 /// serial port (wiki: hardware/hp28s, hardware/lewis).
68 Hp28s,
69 /// HP 19BII: the 28S's two-Lewis board and map with the 19BII's
70 /// keyboard, a 128 KB ROM, 8 KB of RAM that the ROM uses at
71 /// #C4000-#C7FFF, the 137x32 LCD and a clock with appointment alarms;
72 /// no serial port (wiki: hardware/hp19bii).
73 Hp19bii,
74}
75
76/// Which Lewis board a model has, if any.
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum LewisBoard {
79 /// One Lewis chip, its block at #40000 and RAM at #50000 (42S, 27S,
80 /// 17B, 17BII; wiki: hardware/lewis "Memory map").
81 Pioneer,
82 /// Two Lewis chips, master and slave, their blocks at #FFC00 and
83 /// #FF800 and RAM at #C0000 (28S, 19BII; wiki: hardware/hp28s,
84 /// hardware/hp19bii).
85 Clamshell,
86}
87
88/// What a configurable chip select drives on a model.
89#[derive(Clone, Copy, Debug, PartialEq, Eq)]
90pub enum ChipRole {
91 /// Nothing: reads return the open-bus value, writes are ignored.
92 Empty,
93 /// Built-in RAM of this many nibbles.
94 Ram(usize),
95 /// A card slot; the card in this port answers, if any.
96 Card(Port),
97 /// A card slot seen through the bank latch: the window shows one
98 /// 128 KB bank of the card, selected by latch bits 0-4 (48GX port 2;
99 /// wiki: hardware/memory-controller "Bank switching on the 48GX").
100 BankedCard(Port),
101 /// The bank-select latch: any read in the window latches nibble
102 /// address bits A1-A6 (wiki: hardware/memory-controller; Mastracci
103 /// 4.4, Teuwen 4, tutorial p. 158).
104 BankLatch,
105}
106
107/// The per-model hardware description consumed by [`super::Hardware`].
108#[derive(Clone, Copy, Debug, PartialEq, Eq)]
109pub struct HardwareProfile {
110 /// Device behind CE1.
111 pub ce1: ChipRole,
112 /// Device behind CE2.
113 pub ce2: ChipRole,
114 /// Device behind NCE3.
115 pub nce3: ChipRole,
116 /// NCE3 shares its Yorke pin with ROM address line A19, switched by
117 /// DA19 (#129 bit 3): DA19 = 1 gives A19 to the ROM; DA19 = 0 mirrors
118 /// the lower 256 KB of ROM at #80000 and lets NCE3 select its device
119 /// only while the latch's BEN bit is set (48GX; wiki:
120 /// questions/da19-polarity, emulators/emu48 SP9/SP16). The 49G's pin
121 /// is always NCE3 (wiki: hardware/hp49g).
122 pub nce3_shares_a19: bool,
123 /// Writes in the bank-latch window latch their address as reads do.
124 /// Not on the 48GX (Giesselink: its slot 2 write-protect line gates
125 /// the latch); yes on the 49G (tutorial p. 163-165; wiki:
126 /// hardware/hp49g).
127 pub latch_writes: bool,
128 /// While #11C bit 3 is set, NCE3 accesses go to the NCE1 device (the
129 /// 49G's flash write path; wiki: hardware/hp49g "Controllers").
130 pub nce3_flash_path: bool,
131 /// SHUTDN clears the bank latch, as on the 48GX (wiki: emulators/emu48
132 /// SP23). Not on the 49G: its ROM executes SHUTDN with the OS running
133 /// from a switched bank and resumes there, so a cleared latch would
134 /// pull bank 0 under the running code (ROM 2.15, 2.10 and 1.19-6 boot
135 /// traces; wiki: hardware/hp49g).
136 pub shutdn_clears_latch: bool,
137 /// Largest card image per port (port 1, port 2) in bytes, 0 for a
138 /// model without card slots.
139 pub card_max_bytes: [usize; 2],
140 /// I/O register bits the board holds high whatever the CPU writes, as
141 /// (window offset, nibble mask): a model strap the ROM reads to tell
142 /// boards with one ROM apart. The 40G reads #11A bit 3 (the IR
143 /// receive sample) as 1: it has no IR receiver (see [`Model::Hp40g`]).
144 pub io_strap: Option<(u8, u8)>,
145 /// The Lewis chip's fixed memory map instead of the Clarke/Yorke
146 /// controllers: ROM, the display and register block(s) and RAM sit at
147 /// fixed addresses and CONFIG does not move them (42S, 28S; wiki:
148 /// hardware/lewis "Memory map", questions/lewis-memory-map). `None`
149 /// on the 48-family models.
150 pub lewis: Option<LewisBoard>,
151}
152
153impl HardwareProfile {
154 /// The role of `chip` (HDW is the I/O window and NCE2 the built-in RAM
155 /// on every model; both report [`ChipRole::Empty`] here and are
156 /// decoded separately).
157 pub fn role(&self, chip: Chip) -> ChipRole {
158 match chip {
159 Chip::Ce1 => self.ce1,
160 Chip::Ce2 => self.ce2,
161 Chip::Nce3 => self.nce3,
162 Chip::Hdw | Chip::Nce2 => ChipRole::Empty,
163 }
164 }
165
166 /// The chip select a card port hangs on, if the model has that port.
167 pub fn port_chip(&self, port: Port) -> Option<Chip> {
168 [Chip::Ce1, Chip::Ce2, Chip::Nce3].into_iter().find(
169 |&c| matches!(self.role(c), ChipRole::Card(p) | ChipRole::BankedCard(p) if p == port),
170 )
171 }
172}
173
174const KB: usize = 1024;
175
176/// The international 17B and 17BII ROM image: 64 KB plus a 32 KB
177/// language ROM.
178pub const INTL_17B_BYTES: usize = 96 * KB;
179
180/// 48SX: CE1 = port 1, CE2 = port 2, NCE3 unused (wiki:
181/// hardware/memory-controller "Modules on the HP48"). Cards up to 128 KB:
182/// an SX port shows no more (wiki: hardware/card-ports, emulators/emu48
183/// "an SX sees only the first 128 KB").
184const HP48SX: HardwareProfile = HardwareProfile {
185 ce1: ChipRole::Card(Port::One),
186 ce2: ChipRole::Card(Port::Two),
187 nce3: ChipRole::Empty,
188 nce3_shares_a19: false,
189 latch_writes: false,
190 nce3_flash_path: false,
191 shutdn_clears_latch: true,
192 card_max_bytes: [128 * KB, 128 * KB],
193 io_strap: None,
194 lewis: None,
195};
196
197/// 48GX: CE1 = bank latch, CE2 = port 1, NCE3 = port 2 (wiki:
198/// hardware/memory-controller, Mastracci 2.4). Port 1 takes 32 or 128 KB
199/// cards, port 2 up to 32 banks of 128 KB (wiki: hardware/card-ports
200/// "Ports as the OS sees them", Mastracci 4.3).
201const HP48GX: HardwareProfile = HardwareProfile {
202 ce1: ChipRole::BankLatch,
203 ce2: ChipRole::Card(Port::One),
204 nce3: ChipRole::BankedCard(Port::Two),
205 nce3_shares_a19: true,
206 latch_writes: false,
207 nce3_flash_path: false,
208 shutdn_clears_latch: true,
209 card_max_bytes: [128 * KB, 4096 * KB],
210 io_strap: None,
211 lewis: None,
212};
213
214/// 38G: a 48G without card connectors (wiki: hardware/hp38g). Inferred,
215/// not sourced: RAM on NCE2 as on the 48G, the CE1 bank latch and the
216/// DA19/A19 pin as on the 48G (its 512 KB ROM needs A19), nothing behind
217/// CE2 and NCE3 (wiki: questions/hp38g-memory-controllers).
218const HP38G: HardwareProfile = HardwareProfile {
219 ce1: ChipRole::BankLatch,
220 ce2: ChipRole::Empty,
221 nce3: ChipRole::Empty,
222 nce3_shares_a19: true,
223 latch_writes: false,
224 nce3_flash_path: false,
225 shutdn_clears_latch: true,
226 card_max_bytes: [0, 0],
227 io_strap: None,
228 lewis: None,
229};
230
231/// 49G: NCE1 = 2 MB flash banked by the CE1 latch, which reads and
232/// writes clock; CE2 and NCE3 = 128 KB RAM each (port 1); NCE3 is also the
233/// flash write path while #11C bit 3 is set; the A19/NCE3 pin is always
234/// NCE3; no card slots (wiki: hardware/hp49g "Controllers (Giesselink)",
235/// questions/hp49g-ram-controllers).
236const HP49G: HardwareProfile = HardwareProfile {
237 ce1: ChipRole::BankLatch,
238 ce2: ChipRole::Ram(2 * 128 * KB),
239 nce3: ChipRole::Ram(2 * 128 * KB),
240 nce3_shares_a19: false,
241 latch_writes: true,
242 nce3_flash_path: true,
243 shutdn_clears_latch: false,
244 card_max_bytes: [0, 0],
245 io_strap: None,
246 lewis: None,
247};
248
249/// 39G/40G: the 49G wiring minus what was cut (wiki: hardware/hp39g-40g,
250/// "1 MB ROM and 256 KB RAM instead of 2 MB flash and 512 KB RAM"). The
251/// CE1 bank latch is the 49G's, clocked by reads and writes, and SHUTDN
252/// leaves it alone as on the 49G (inferred from the shared design). CE2
253/// and NCE3, the 49G's ERAM, are empty and there is no flash write path
254/// (inferred; the ROM's CONFIG sequence parks both, see
255/// wiki: questions/hp39g-40g-memory-map).
256const HP39G: HardwareProfile = HardwareProfile {
257 ce1: ChipRole::BankLatch,
258 ce2: ChipRole::Empty,
259 nce3: ChipRole::Empty,
260 nce3_shares_a19: false,
261 latch_writes: true,
262 nce3_flash_path: false,
263 shutdn_clears_latch: false,
264 card_max_bytes: [0, 0],
265 io_strap: None,
266 lewis: None,
267};
268
269/// 40G: the 39G wiring with #11A bit 3 held high.
270const HP40G: HardwareProfile = HardwareProfile {
271 io_strap: Some((0x1A, 0x8)),
272 ..HP39G
273};
274
275/// 42S, 27S, 17B, 17BII: the Lewis chip's fixed map; no chip select is configurable, no
276/// card port, no bank latch (wiki: hardware/lewis). The CE/NCE roles are
277/// unused.
278const HP42S: HardwareProfile = HardwareProfile {
279 ce1: ChipRole::Empty,
280 ce2: ChipRole::Empty,
281 nce3: ChipRole::Empty,
282 nce3_shares_a19: false,
283 latch_writes: false,
284 nce3_flash_path: false,
285 shutdn_clears_latch: false,
286 card_max_bytes: [0, 0],
287 io_strap: None,
288 lewis: Some(LewisBoard::Pioneer),
289};
290
291/// 28S: two Lewis chips on their fixed map (wiki: hardware/hp28s,
292/// questions/lewis-memory-map); nothing configurable, as on the 42S.
293const HP28S: HardwareProfile = HardwareProfile {
294 lewis: Some(LewisBoard::Clamshell),
295 ..HP42S
296};
297
298impl Model {
299 /// Every model, in declaration order.
300 pub const ALL: [Model; 12] = [
301 Model::Hp48sx,
302 Model::Hp48gx,
303 Model::Hp38g,
304 Model::Hp49g,
305 Model::Hp39g,
306 Model::Hp40g,
307 Model::Hp42s,
308 Model::Hp27s,
309 Model::Hp17bii,
310 Model::Hp17b,
311 Model::Hp28s,
312 Model::Hp19bii,
313 ];
314
315 /// The Pioneer models on one Lewis chip (42S, 27S, 17BII, 17B): the
316 /// fixed Lewis map, the 131x16 LCD, no serial port, no card port, no
317 /// RPL (wiki: hardware/lewis, hardware/saturn-models).
318 pub fn is_pioneer(self) -> bool {
319 self.hardware().lewis == Some(LewisBoard::Pioneer)
320 }
321
322 /// The models on the Lewis chip: the Pioneers, the 28S and the 19BII. None has a
323 /// serial port or a card port, and none runs the 48's RPL system
324 /// (wiki: hardware/lewis, hardware/saturn-models).
325 pub fn is_lewis(self) -> bool {
326 self.hardware().lewis.is_some()
327 }
328
329 /// The display's size in pixels, width by height: 131x64 on the
330 /// 48-family models, 131x16 on the Pioneers, 137x32 on the 28S and
331 /// 19BII (wiki: hardware/display, hardware/lewis, hardware/hp28s,
332 /// hardware/hp19bii).
333 pub fn lcd_size(self) -> (usize, usize) {
334 match self.hardware().lewis {
335 None => (super::LCD_WIDTH, super::LCD_HEIGHT),
336 Some(LewisBoard::Pioneer) => (super::LCD_WIDTH, super::LCD_HEIGHT_42S),
337 Some(LewisBoard::Clamshell) => (super::LCD_WIDTH_28S, super::LCD_HEIGHT_28S),
338 }
339 }
340
341 /// Short lowercase name as the CLI and the oracle use it.
342 pub fn name(self) -> &'static str {
343 match self {
344 Model::Hp48sx => "48sx",
345 Model::Hp48gx => "48gx",
346 Model::Hp38g => "38g",
347 Model::Hp49g => "49g",
348 Model::Hp39g => "39g",
349 Model::Hp40g => "40g",
350 Model::Hp42s => "42s",
351 Model::Hp27s => "27s",
352 Model::Hp17bii => "17bii",
353 Model::Hp17b => "17b",
354 Model::Hp28s => "28s",
355 Model::Hp19bii => "19bii",
356 }
357 }
358
359 /// What the model wires behind its chip selects.
360 pub(crate) fn hardware(self) -> &'static HardwareProfile {
361 match self {
362 Model::Hp48sx => &HP48SX,
363 Model::Hp48gx => &HP48GX,
364 Model::Hp38g => &HP38G,
365 Model::Hp49g => &HP49G,
366 Model::Hp39g => &HP39G,
367 Model::Hp40g => &HP40G,
368 Model::Hp42s | Model::Hp27s | Model::Hp17bii | Model::Hp17b => &HP42S,
369 Model::Hp28s | Model::Hp19bii => &HP28S,
370 }
371 }
372
373 /// Range the ROM lets ON+ / ON- move the contrast in: 3-19 on the 48SX,
374 /// 9-24 on the 48GX, 38G, 49G, 39G and 40G (wiki: emulators/emu48
375 /// Display, from the KML 2.0 documentation; Voyage p. 193 agrees for
376 /// the SX; wiki: hardware/hp39g-40g "Display"), 15-31 on the 42S (wiki:
377 /// hardware/hp42s "Summary"), and 15-31 on the 27S, 17BII and 17B
378 /// (wiki: hardware/hp27s, hardware/hp17bii, from KML 2.0), 16-31 on
379 /// the 28S and 19BII (wiki: hardware/hp28s, hardware/hp19bii, from KML
380 /// 2.0).
381 /// Informational; the hardware register takes any value 0-31.
382 pub fn contrast_range(self) -> std::ops::RangeInclusive<u8> {
383 match self {
384 Model::Hp48sx => 3..=19,
385 Model::Hp28s | Model::Hp19bii => 16..=31,
386 m if m.is_pioneer() => 15..=31,
387 _ => 9..=24,
388 }
389 }
390
391 /// The contrast the ROM sets at power-on, observed after a cold start
392 /// (wiki: hardware/hp48sx, hardware/hp48gx, hardware/hp38g,
393 /// hardware/hp49g, hardware/hp39g-40g, hardware/hp42s): the front end
394 /// renders it properly dark. 11 on the 48SX (ROM J), 14 on the 48GX
395 /// (R), 38G (A) and 49G (1.19-6 and 2.10), 12 on the 39G/40G, 22 on
396 /// the 42S (C), 22 on the 27S, 17BII and 17B (the reset value KML 2.0
397 /// gives; wiki: hardware/hp27s, hardware/hp17bii), 22 on the 28S and
398 /// 19BII (KML 2.0; wiki: hardware/hp28s, hardware/hp19bii).
399 pub fn default_contrast(self) -> u8 {
400 match self {
401 Model::Hp48sx => 11,
402 Model::Hp39g | Model::Hp40g => 12,
403 Model::Hp42s
404 | Model::Hp27s
405 | Model::Hp17bii
406 | Model::Hp17b
407 | Model::Hp28s
408 | Model::Hp19bii => 22,
409 Model::Hp48gx | Model::Hp38g | Model::Hp49g => 14,
410 }
411 }
412
413 /// Size of the packed system ROM image in bytes (two nibbles per
414 /// byte): 256 KB SX, 512 KB GX (wiki: hardware/hp48gx), 2 MB 49G flash
415 /// (wiki: hardware/hp49g), 1 MB 39G/40G mask ROM (wiki:
416 /// hardware/hp39g-40g), 64 KB 42S, 27S, 17B and English 17BII (wiki:
417 /// hardware/hp42s, hardware/saturn-models); the international 17B's and
418 /// 17BII's 96 KB is [`Model::accepts_rom_len`]'s second size; 128 KB
419 /// on the 28S and 19BII (wiki: hardware/hp28s, hardware/hp19bii).
420 pub fn rom_bytes(self) -> usize {
421 match self {
422 Model::Hp42s | Model::Hp27s | Model::Hp17bii | Model::Hp17b => 64 * KB,
423 Model::Hp28s | Model::Hp19bii => 128 * KB,
424 Model::Hp48sx => 256 * KB,
425 Model::Hp48gx | Model::Hp38g => 512 * KB,
426 Model::Hp49g => 2048 * KB,
427 Model::Hp39g | Model::Hp40g => 1024 * KB,
428 }
429 }
430
431 /// Whether a ROM file of `len` bytes has a size [`super::Machine::new`]
432 /// accepts: the packed image, or for the 49G, 39G and 40G also the
433 /// same unpacked (one nibble per byte, twice the size; hpcalc's
434 /// `rom.39g` is in that form), and for the 17B and 17BII also the
435 /// 96 KB international image: the 64 KB main ROM at #00000 and the
436 /// 32 KB language ROM at #20000, in one file (wiki: hardware/hp17bii).
437 pub fn accepts_rom_len(self, len: usize) -> bool {
438 len == self.rom_bytes()
439 || (matches!(self, Model::Hp49g | Model::Hp39g | Model::Hp40g)
440 && len == 2 * self.rom_bytes())
441 || (matches!(self, Model::Hp17bii | Model::Hp17b) && len == INTL_17B_BYTES)
442 }
443
444 /// Built-in RAM on NCE2 in nibbles: 32 KB SX, 128 KB GX (wiki:
445 /// hardware/hp48gx), 256 KB 49G (wiki: hardware/hp49g, the IRAM half;
446 /// the other 256 KB sit on CE2 and NCE3), 8 KB on the 42S (wiki:
447 /// hardware/hp42s; the 32 KB upgrade is not modelled), 32 KB on the
448 /// 28S (wiki: hardware/hp28s), 8 KB on the 19BII, repeating through
449 /// the 28S's RAM window: its ROM reads and writes RAM only at
450 /// #C4000-#C7FFF (traced; wiki: hardware/hp19bii).
451 pub fn ram_nibbles(self) -> usize {
452 match self {
453 Model::Hp28s => 2 * 32 * KB,
454 Model::Hp42s | Model::Hp27s | Model::Hp17bii | Model::Hp17b | Model::Hp19bii => {
455 2 * 8 * KB
456 }
457 Model::Hp48sx => 2 * 32 * KB,
458 Model::Hp48gx => 2 * 128 * KB,
459 Model::Hp38g => 2 * 32 * KB,
460 Model::Hp49g | Model::Hp39g | Model::Hp40g => 2 * 256 * KB,
461 }
462 }
463
464 /// CPU clock in Hz: 2 MHz on the SX (wiki: hardware/hp48sx), 4 MHz on
465 /// the Yorke models (wiki: hardware/hp48gx "~4 MHz", hardware/hp49g),
466 /// 1 MHz on the 42S (wiki: hardware/hp42s "about 1 MHz"; the Lewis
467 /// RATE register's effect on the clock is not modelled), and the same
468 /// on the 27S, 17BII and 17B (inferred: the same board). The 28S
469 /// is taken as the same (inferred: the same chip; wiki:
470 /// questions/lewis-clock-and-rate).
471 pub fn clock_hz(self) -> u32 {
472 match self {
473 m if m.is_lewis() => 1_000_000,
474 Model::Hp48sx => 2_000_000,
475 _ => 4_000_000,
476 }
477 }
478
479 /// Cycle table: the SASM counts on the 48SX's Clarke, the Meta Kernel
480 /// counts the Saturn tutorial gives for the G series on every Yorke
481 /// model (wiki: hardware/saturn-cpu "Timing", emulators/emu48 SP1).
482 /// The 42S's Lewis takes the SASM counts as the Clarke does (inferred:
483 /// the same 1LT8 CPU core; no Lewis cycle table is documented).
484 pub(crate) fn cycle_table(self) -> crate::cpu::CycleTable {
485 match self {
486 Model::Hp48sx
487 | Model::Hp42s
488 | Model::Hp27s
489 | Model::Hp17bii
490 | Model::Hp17b
491 | Model::Hp28s
492 | Model::Hp19bii => crate::cpu::CycleTable::Sasm,
493 _ => crate::cpu::CycleTable::MetaKernel,
494 }
495 }
496
497 /// Calibration of instruction time, in per mille of the cycle table's
498 /// count: the factor that makes the HP Museum summation benchmark take
499 /// as long as on the real machines (wiki:
500 /// questions/instruction-speed-vs-hardware,
501 /// sources/hpmuseum-summation-benchmark). **Calibration, not a
502 /// documented hardware figure**: with the SASM counts (48SX) and the
503 /// Meta Kernel counts (Yorke models) plus the 13% display stall, the
504 /// ROMs run n = 1000 in 75.4 s (48SX, real 95.5 s), 41.2 / 40.5 s
505 /// (48GX sum / FOR, real 55 / 54 s) and 40.2 / 41.8 s (49G ROM 2.10,
506 /// real 47.8 / 51.0 s). The 38G is taken as a 48G and the 39G and 40G
507 /// as a 49G (inferred: same chip and memory types; no benchmark). The
508 /// 42S runs the table counts at its documented clock, unscaled: no
509 /// benchmark of a real 42S has been compared yet.
510 pub fn cycle_scale_permille(self) -> u32 {
511 match self {
512 Model::Hp42s
513 | Model::Hp27s
514 | Model::Hp17bii
515 | Model::Hp17b
516 | Model::Hp28s
517 | Model::Hp19bii => 1000,
518 Model::Hp48sx => 1267,
519 Model::Hp48gx | Model::Hp38g => 1335,
520 Model::Hp49g | Model::Hp39g | Model::Hp40g => 1205,
521 }
522 }
523
524 /// Keyboard: the 48 matrix on the 48SX and 48GX, the same matrix with
525 /// the 38G's labels on the 38G (wiki: hardware/hp38g "Keyboard"), the
526 /// 49G's own matrix, and that matrix with the 39G's labels on the 39G
527 /// and 40G (wiki: hardware/hp39g-40g "Keyboard"), and the 42S's own
528 /// 6 x 7 matrix (wiki: hardware/hp42s "Keyboard"); the 27S and the
529 /// 17B/17BII put their own keys on that matrix (wiki: hardware/hp27s,
530 /// hardware/hp17bii "Keyboard"); the 28S has its own 7 x 12 matrix
531 /// (wiki: hardware/hp28s "Keyboard"), and the 19BII its keys on that
532 /// matrix (wiki: hardware/hp19bii "Keyboard").
533 pub(crate) fn keyboard_layout(self) -> crate::io::Layout {
534 match self {
535 Model::Hp28s => crate::io::Layout::Hp28,
536 Model::Hp19bii => crate::io::Layout::Hp19,
537 Model::Hp42s => crate::io::Layout::Hp42,
538 Model::Hp27s => crate::io::Layout::Hp27,
539 Model::Hp17bii | Model::Hp17b => crate::io::Layout::Hp17,
540 Model::Hp48sx | Model::Hp48gx => crate::io::Layout::Hp48,
541 Model::Hp38g => crate::io::Layout::Hp38,
542 Model::Hp49g => crate::io::Layout::Hp49,
543 Model::Hp39g | Model::Hp40g => crate::io::Layout::Hp39,
544 }
545 }
546
547 /// Whether the model has a serial port (a UART): every model but the
548 /// Lewis ones (the Pioneers, the 28S and the 19BII), whose only I/O is the
549 /// infrared printer LED (wiki: hardware/hp42s, hardware/hp28s,
550 /// hardware/saturn-models).
551 pub fn has_serial(self) -> bool {
552 !self.is_lewis()
553 }
554
555 /// Largest card image `port` accepts, in bytes; 0 when the model has no
556 /// such slot.
557 pub fn card_max_bytes(self, port: Port) -> usize {
558 self.hardware().card_max_bytes[port.index()]
559 }
560
561 /// Whether the model's keyboard has `k` (wiki: hardware/keyboard; the
562 /// 48 and 49G matrices differ).
563 pub fn has_key(self, k: Key) -> bool {
564 k.position(self.keyboard_layout()).is_some()
565 }
566
567 /// Every key of the model's keyboard, ON included.
568 pub fn keys(self) -> impl Iterator<Item = Key> {
569 Key::on_layout(self.keyboard_layout())
570 }
571}
572
573/// The model by its short name ([`Model::name`]), ignoring case: "48sx",
574/// "48gx", "38g", "49g", "39g", "40g", "42s", "27s", "17bii", "17b", "28s", "19bii". Every host parses model
575/// names here.
576impl FromStr for Model {
577 type Err = Error;
578
579 fn from_str(name: &str) -> Result<Self, Error> {
580 Model::ALL
581 .into_iter()
582 .find(|m| m.name().eq_ignore_ascii_case(name))
583 .ok_or_else(|| Error::UnknownModel {
584 name: name.to_string(),
585 })
586 }
587}
588
589#[cfg(test)]
590mod tests {
591 use super::*;
592
593 #[test]
594 fn models_by_name() {
595 assert_eq!("48SX".parse(), Ok(Model::Hp48sx));
596 assert_eq!("49g".parse(), Ok(Model::Hp49g));
597 assert_eq!("42S".parse(), Ok(Model::Hp42s));
598 for m in Model::ALL {
599 assert_eq!(m.name().parse(), Ok(m));
600 }
601 let e = "41c".parse::<Model>().unwrap_err();
602 assert_eq!(
603 e.to_string(),
604 "unknown model \"41c\"; expected one of 48sx, 48gx, 38g, 49g, 39g, 40g, 42s, 27s, 17bii, 17b, 28s, 19bii"
605 );
606 }
607
608 #[test]
609 fn port_chips_follow_the_profile() {
610 let sx = Model::Hp48sx.hardware();
611 assert_eq!(sx.port_chip(Port::One), Some(Chip::Ce1));
612 assert_eq!(sx.port_chip(Port::Two), Some(Chip::Ce2));
613 let gx = Model::Hp48gx.hardware();
614 assert_eq!(gx.port_chip(Port::One), Some(Chip::Ce2));
615 assert_eq!(gx.port_chip(Port::Two), Some(Chip::Nce3));
616 assert_eq!(Model::Hp49g.hardware().port_chip(Port::One), None);
617 }
618
619 #[test]
620 fn model_facts() {
621 assert_eq!(Model::Hp48gx.rom_bytes(), 524_288);
622 assert_eq!(Model::Hp48gx.clock_hz(), 4_000_000);
623 assert_eq!(Model::Hp48gx.card_max_bytes(Port::Two), 4 * 1024 * 1024);
624 assert_eq!(Model::Hp48sx.card_max_bytes(Port::Two), 128 * 1024);
625 for m in Model::ALL {
626 assert!(m.contrast_range().end() < &32);
627 assert!(m.contrast_range().contains(&m.default_contrast()));
628 }
629 assert_eq!(Model::Hp39g.rom_bytes(), 1024 * 1024);
630 assert!(Model::Hp40g.accepts_rom_len(2 * 1024 * 1024));
631 assert!(Model::Hp49g.accepts_rom_len(4 * 1024 * 1024));
632 assert!(!Model::Hp48gx.accepts_rom_len(1024 * 1024));
633 assert_eq!(Model::Hp40g.hardware().io_strap, Some((0x1A, 0x8)));
634 assert_eq!(Model::Hp39g.hardware().io_strap, None);
635 assert_eq!(
636 Model::Hp39g.keyboard_layout(),
637 Model::Hp40g.keyboard_layout()
638 );
639 assert_eq!(Model::Hp42s.rom_bytes(), 65_536);
640 assert_eq!(Model::Hp42s.clock_hz(), 1_000_000);
641 assert_eq!(Model::Hp42s.card_max_bytes(Port::One), 0);
642 assert_eq!(Model::Hp42s.hardware().lewis, Some(LewisBoard::Pioneer));
643 assert!(Model::Hp28s.is_lewis() && !Model::Hp28s.is_pioneer());
644 assert!(!Model::Hp28s.has_serial());
645 assert_eq!(Model::Hp28s.rom_bytes(), 128 * 1024);
646 assert_eq!(Model::Hp28s.lcd_size(), (137, 32));
647 assert_eq!(Model::Hp19bii.lcd_size(), (137, 32));
648 assert!(Model::Hp19bii.is_lewis() && !Model::Hp19bii.has_serial());
649 assert_eq!(Model::Hp19bii.rom_bytes(), 128 * 1024);
650 assert_eq!(Model::Hp19bii.ram_nibbles(), 2 * 8 * 1024);
651 assert_ne!(
652 Model::Hp19bii.keyboard_layout(),
653 Model::Hp28s.keyboard_layout()
654 );
655 assert_eq!(Model::Hp42s.lcd_size(), (131, 16));
656 assert_eq!(Model::Hp48gx.lcd_size(), (131, 64));
657 assert!(!Model::Hp42s.has_serial() && Model::Hp40g.has_serial());
658 let pioneers: Vec<Model> = Model::ALL.into_iter().filter(|m| m.is_pioneer()).collect();
659 assert_eq!(
660 pioneers,
661 [Model::Hp42s, Model::Hp27s, Model::Hp17bii, Model::Hp17b]
662 );
663 for m in pioneers {
664 assert_eq!(m.rom_bytes(), 65_536);
665 assert!(!m.has_serial());
666 assert_eq!(m.default_contrast(), 22);
667 }
668 assert!(Model::Hp17bii.accepts_rom_len(96 * 1024));
669 assert!(Model::Hp17b.accepts_rom_len(96 * 1024));
670 assert!(!Model::Hp27s.accepts_rom_len(96 * 1024));
671 assert_ne!(
672 Model::Hp27s.keyboard_layout(),
673 Model::Hp17b.keyboard_layout()
674 );
675 assert_eq!(
676 Model::Hp17bii.keyboard_layout(),
677 Model::Hp17b.keyboard_layout()
678 );
679 let names: std::collections::HashSet<_> = Model::ALL.iter().map(|m| m.name()).collect();
680 assert_eq!(names.len(), Model::ALL.len());
681 }
682}