systemless 0.114.0

High-Level Emulation for classic Macintosh applications
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
use super::*;
use crate::managers::resource::ResourceFork;
use crate::memory::globals::{addr, DEFAULT_UNIT_TABLE_ENTRY_COUNT};
use crate::trap::dispatch::TrapTableProfile;
use std::collections::HashMap;

#[test]
fn init_app_zeroes_fresh_top_of_stack() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    runner.init_app(&app);

    assert_eq!(
        runner.bus.read_word(addr::SYS_VERSION),
        crate::machine_profile::REFERENCE_MACHINE_PROFILE.system_version_bcd
    );

    let stack_seed_start = app.initial_sp.saturating_sub(0x8000);
    assert_eq!(
        runner.bus.read_long(stack_seed_start),
        0,
        "fresh process stack should match a newly initialized application partition"
    );
}

#[test]
fn init_app_seeds_appparmhandle_with_empty_finder_information() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    runner
        .dispatcher_mut()
        .set_launched_app_path("Games/Armor Alley");
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    runner.init_app(&app);

    let handle = runner.bus.read_long(addr::APP_PARM_HANDLE);
    assert_ne!(
        handle, 0,
        "AppParmHandle should point at Finder launch information"
    );
    let data_ptr = runner.bus.read_long(handle);
    assert_ne!(
        data_ptr, 0,
        "Finder launch information handle should be loaded"
    );
    assert_eq!(
        runner.bus.get_alloc_size(data_ptr),
        Some(4),
        "empty Finder launch information is message/count only"
    );
    assert_eq!(
        runner.bus.read_word(data_ptr),
        0,
        "message should be appOpen for a normal application launch"
    );
    assert_eq!(
        runner.bus.read_word(data_ptr + 2),
        0,
        "normal application launch has no selected documents"
    );
}

#[test]
fn init_app_seeds_current_application_fcb_low_memory_state() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    runner
        .dispatcher_mut()
        .vfs_rsrc
        .insert("Games/Armor Alley".to_string(), vec![0xA5; 1234]);
    runner
        .dispatcher_mut()
        .set_vfs_entry_metadata("Games/Armor Alley", *b"APPL", *b"TEST", 0);
    runner
        .dispatcher_mut()
        .set_launched_app_path("Games/Armor Alley");
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    let app_map = 0x0018_0000;
    let app_map_handle = 0x0018_0100;
    runner.bus.write_long(app_map_handle, app_map);
    runner.bus.write_long(0x0A50, app_map_handle);
    runner.bus.write_word(app_map + 20, 0);
    runner.init_app(&app);

    assert_eq!(
        runner.bus.read_word(addr::CUR_APREF_NUM),
        APPLICATION_RESOURCE_REFNUM,
        "CurApRefNum should be the app resource fork access path"
    );
    assert_eq!(runner.bus.read_word(0x0A5A), APPLICATION_RESOURCE_REFNUM);
    assert_eq!(
        runner.bus.read_word(app_map + 20),
        APPLICATION_RESOURCE_REFNUM
    );
    assert_eq!(
        runner.bus.read_word(addr::BOOT_DRIVE) as i16,
        crate::trap::dispatch::BOOT_VOLUME_REF_NUM,
        "BootDrive should name the startup volume"
    );
    assert_eq!(
        runner.bus.read_word(addr::FS_FCB_LEN),
        HFS_FCB_SIZE,
        "System 7 FCB size should be exposed for direct low-memory readers"
    );
    let fcb_buffer = runner.bus.read_long(addr::FCB_S_PTR);
    assert_ne!(fcb_buffer, 0, "FCBSPtr should point to an FCB buffer");
    assert_eq!(
        runner.bus.read_word(fcb_buffer),
        HFS_FCB_BUFFER_SIZE,
        "FCB buffer length should include the leading length word"
    );
    let fcb = fcb_buffer + APPLICATION_RESOURCE_REFNUM as u32;
    assert_eq!(
        runner.bus.read_word(fcb + 4),
        0x0200,
        "the application access path should describe a resource fork"
    );
    assert_eq!(runner.bus.read_long(fcb + 8), 1234);
    assert_eq!(runner.bus.read_long(fcb + 12), 1234);
    assert_eq!(runner.bus.read_long(fcb + 50), u32::from_be_bytes(*b"APPL"));
    assert_eq!(
        runner.bus.read_long(fcb + 58),
        *runner.dispatcher.default_dir_id
    );

    let vcb = runner.bus.read_long(fcb + 20);
    assert_ne!(vcb, 0, "fcbVPtr should point to the boot volume VCB");
    assert_eq!(runner.bus.read_long(addr::DEF_VCB_PTR), vcb);
    assert_eq!(runner.bus.read_long(addr::VCB_Q_HDR + 2), vcb);
    assert_eq!(runner.bus.read_long(addr::VCB_Q_HDR + 6), vcb);
    assert_eq!(runner.bus.read_word(vcb + 8), 0x4244);
    assert_eq!(
        runner.bus.read_word(vcb + 78) as i16,
        crate::trap::dispatch::BOOT_VOLUME_REF_NUM
    );
    assert_eq!(
        runner
            .dispatcher
            .open_files
            .get(&APPLICATION_RESOURCE_REFNUM),
        Some(&"__rsrc__Games/Armor Alley".to_string())
    );
}

#[test]
fn init_app_sets_legacy_sound_driver_low_memory_defaults() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    use crate::memory::bus::{
        LEGACY_SOUND_ALTERNATE_OFFSET, LEGACY_SOUND_PORT_A_OFFSET, LEGACY_SPEAKER_VOLUME,
    };
    use crate::memory::globals::addr;
    let (floor, _) = runner.bus.synthetic_reservation_range().unwrap();
    let via = runner.bus.read_long(addr::VIA_BASE);
    let sound_base = runner.bus.read_long(addr::SOUND_BASE);
    let alternate = sound_base - LEGACY_SOUND_ALTERNATE_OFFSET;
    // Leave the device as a previous application could have: enabled on the
    // alternate buffer at low volume, muted, with queued audio, nonneutral
    // samples and a stray register value.
    runner.bus.write_byte(via, 0);
    runner
        .bus
        .write_byte(via + LEGACY_SOUND_PORT_A_OFFSET, 0x01);
    runner.bus.write_byte(via + 13 * 0x200, 0x7F); // vIFR
    runner.bus.write_byte(sound_base, 0x55);
    runner.bus.write_byte(alternate, 0x55);
    runner.bus.write_byte(addr::SD_VOLUME, 0);
    runner
        .dispatcher
        .sound_manager
        .with_mut(|manager| manager.legacy_speaker_muted = true);
    runner.force_advance_guest_tick();
    assert!(!runner.legacy_sound.queued().is_empty());

    runner.init_app(&app);

    assert_eq!(
        runner.bus.read_byte(addr::SD_VOLUME),
        LEGACY_SPEAKER_VOLUME,
        "SdVolume ($0260) boots at the volume the compatibility VIA plays"
    );
    assert_eq!(
        runner.bus.read_byte(addr::SOUND_LEVEL),
        0,
        "SoundLevel ($027F) is a distinct Sound Driver amplitude byte"
    );
    assert_eq!(alternate, floor);
    assert_eq!(runner.bus.read_long(addr::SOUND_BASE), sound_base);
    assert_eq!(runner.bus.read_long(addr::VIA_BASE), via);
    assert_eq!(runner.bus.read_byte(via), 0x80, "sound starts disabled");
    assert_eq!(
        runner.bus.read_byte(via + LEGACY_SOUND_PORT_A_OFFSET),
        0x08 | LEGACY_SPEAKER_VOLUME,
        "main buffer at full volume"
    );
    assert_eq!(runner.bus.read_byte(via + 13 * 0x200), 0);
    assert_eq!(
        runner.bus.read_byte(sound_base),
        0x80,
        "legacy SoundBase buffer starts at neutral amplitude"
    );
    assert_eq!(runner.bus.read_byte(alternate), 0x80);
    assert!(!runner.dispatcher.sound_manager.legacy_speaker_muted);
    assert!(runner.legacy_sound.queued().is_empty());
}

#[test]
fn init_app_materializes_the_device_manager_unit_table() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    runner.init_app(&app);

    let table = runner.bus.read_long(addr::U_TABLE_BASE);
    assert_ne!(table, 0, "UTableBase should address the unit table");
    assert_eq!(
        runner.bus.read_word(addr::UNIT_NTRY_CNT),
        DEFAULT_UNIT_TABLE_ENTRY_COUNT
    );
    assert_eq!(
        runner.bus.get_alloc_size(table),
        Some(u32::from(DEFAULT_UNIT_TABLE_ENTRY_COUNT) * 4)
    );
    let display_dce_handle = runner.bus.read_long(table);
    assert_ne!(display_dce_handle, 0, "unit zero should hold the main display DCE");
    let display_dce = runner.bus.read_long(display_dce_handle);
    assert_ne!(display_dce, 0);
    assert_eq!(runner.bus.read_word(display_dce + 24), u16::MAX);
    let gdevice = runner.bus.read_long(runner.bus.read_long(0x8A4));
    assert_eq!(runner.bus.read_word(gdevice), u16::MAX);
    assert!(
        runner
            .bus
            .read_bytes(table + 4, usize::from(DEFAULT_UNIT_TABLE_ENTRY_COUNT - 1) * 4)
            .iter()
            .all(|&byte| byte == 0),
        "other unit-table slots should start empty"
    );
}

#[test]
fn init_app_preserves_tagged_pointer_addresses_with_lo3bytes() {
    for addressing_32_bit in [false, true] {
        let mut runner = FixtureRunner::new(
            8 * 1024 * 1024,
            FixtureRunnerConfig {
                addressing_32_bit,
                ..FixtureRunnerConfig::default()
            },
        );
        let app = LoadedApp {
            ppc: None,
            code0_header: Code0Header {
                above_a5: 0,
                below_a5: 0x2000,
                jump_table_size: 0,
                jump_table_offset: 0,
            },
            a5_base: 0x0040_0000,
            jump_table: Vec::new(),
            segment_bases: HashMap::new(),
            loaded_image_end: 0,
            initial_sp: 0x007F_FFC0,
            size_resource: None,
        };

        for _launch in 0..2 {
            runner.bus.write_long(addr::LO3_BYTES, 0);
            runner.init_app(&app);
            let call_site = 0x0002_0000;
            runner.bus.write_word(call_site, 0xC0B8); // AND.L ($031A).W,D0
            runner.bus.write_word(call_site + 2, 0x031A);
            runner.m68k.cpu.write_reg(Register::PC, call_site);
            runner.m68k.cpu.write_reg(Register::D0, 0xA521_3456);

            let (steps, running) = runner.run_steps(1, None);

            assert!(running);
            assert_eq!(steps, 1);
            assert_eq!(runner.m68k.cpu.read_reg(Register::D0), 0x0021_3456);
        }
    }
}

#[test]
fn init_app_seeds_mmu32bit_low_memory_flag() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    runner.init_app(&app);

    assert_eq!(
        runner
            .bus
            .read_byte(crate::memory::globals::addr::MMU32_BIT),
        1,
        "MMU32Bit ($0CB2) should mirror Systemless's default 32-bit addressing mode"
    );
}

#[test]
fn init_app_can_start_in_twenty_four_bit_addressing_mode() {
    let mut runner = FixtureRunner::new(
        8 * 1024 * 1024,
        FixtureRunnerConfig {
            addressing_32_bit: false,
            ..FixtureRunnerConfig::default()
        },
    );
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };

    runner.init_app(&app);

    assert!(!runner.bus.addressing_32_bit());
    assert_eq!(runner.bus.ram_size(), 8 * 1024 * 1024);
    assert_eq!(runner.dispatcher.mmu_mode, 0);
    assert_eq!(
        runner
            .bus
            .read_byte(crate::memory::globals::addr::MMU32_BIT),
        0
    );
}

#[test]
fn twenty_four_bit_runner_caps_guest_ram_at_sixteen_megabytes() {
    let runner = FixtureRunner::new(
        64 * 1024 * 1024,
        FixtureRunnerConfig {
            addressing_32_bit: false,
            ..FixtureRunnerConfig::default()
        },
    );

    assert_eq!(runner.bus.ram_size(), 0x0100_0000);
}

#[test]
fn init_app_seeds_callable_swap_mmu_mode_trap_table_entry() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };
    runner.init_app(&app);

    let entry = runner
        .bus
        .read_long(crate::memory::globals::addr::SWAP_MMU_MODE_TRAP);
    assert_ne!(entry, 0);
    assert_eq!(
        [runner.bus.read_word(entry), runner.bus.read_word(entry + 2),],
        [0xA05D, 0x4E75],
        "the $0574 OS trap-table entry should target SwapMMUMode followed by RTS"
    );

    let call_site = 0x0002_0000u32;
    let initial_sp = 0x007F_FE00u32;
    runner.bus.write_word(call_site, 0x2078); // MOVEA.L ($0574).W,A0
    runner.bus.write_word(call_site + 2, 0x0574);
    runner.bus.write_word(call_site + 4, 0x4E90); // JSR (A0)
    runner.bus.write_word(call_site + 6, 0x4E71); // NOP
    runner.m68k.cpu.write_reg(Register::PC, call_site);
    runner.m68k.cpu.write_reg(Register::A7, initial_sp);
    runner.m68k.cpu.write_reg(Register::D0, 0);

    let (steps, running) = runner.run_steps(4, None);

    assert!(running);
    assert_eq!(steps, 4);
    assert_eq!(runner.m68k.cpu.read_reg(Register::PC), call_site + 6);
    assert_eq!(runner.m68k.cpu.read_reg(Register::A7), initial_sp);
    assert_eq!(
        runner.m68k.cpu.read_reg(Register::D0),
        1,
        "SwapMMUMode should return the previous 32-bit mode in D0"
    );
    assert_eq!(
        runner
            .bus
            .read_byte(crate::memory::globals::addr::MMU32_BIT),
        0,
        "the indirect call should update the requested addressing mode"
    );
    runner.bus.write_long(0x0002_1000, 0x1234_5678);
    assert_eq!(
        runner.bus.read_long(0xAB02_1000),
        0x1234_5678,
        "SwapMMUMode must change actual guest address translation"
    );
}

#[test]
fn swap_mmu_mode_returns_on_classic_stack_with_more_than_sixteen_megabytes() {
    let code0 = minimal_code0(0, 0x2000, 0, 0);
    let fork_bytes = make_resource_fork_bytes(&[(*b"CODE", 0, &code0)]);
    let fork = ResourceFork::parse(&fork_bytes).expect("parse synthetic app fork");
    let mut runner = FixtureRunner::new(64 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = runner.load_app(&fork).expect("load app");
    runner.init_app(&app);

    let call_site = 0x0002_0000;
    let return_pc = call_site + 8;
    let stack = app.initial_sp - 0x200;
    runner.bus.write_word(call_site, 0xA05D); // SwapMMUMode
    runner.bus.write_word(call_site + 2, 0x4E75); // RTS
    runner.bus.write_long(stack, return_pc);
    runner.m68k.cpu.write_reg(Register::PC, call_site);
    runner.m68k.cpu.write_reg(Register::A7, stack);
    runner.m68k.cpu.write_reg(Register::D0, 0);

    let (steps, running) = runner.run_steps(2, None);

    assert!(running);
    assert_eq!(steps, 2);
    assert_eq!(runner.m68k.cpu.read_reg(Register::PC), return_pc);
    assert_eq!(runner.m68k.cpu.read_reg(Register::A7), stack + 4);
    assert!(!runner.bus.addressing_32_bit());

    // PenMode has a permanent system-owned come-from head. Its raw
    // synthetic identity remains protected provenance while 24-bit guest
    // accesses are masked, so inline dispatch must not mistake the head
    // for an application-installed native patch.
    let pen_mode_site = call_site + 0x10;
    runner.bus.write_word(pen_mode_site, 0xA89C);
    runner.bus.write_word(stack + 4, 8);
    runner.m68k.cpu.write_reg(Register::PC, pen_mode_site);

    let (steps, running) = runner.run_steps(1, None);

    assert!(running);
    assert_eq!(steps, 1);
    assert_eq!(runner.m68k.cpu.read_reg(Register::PC), pen_mode_site + 2);
    assert_eq!(runner.m68k.cpu.read_reg(Register::A7), stack + 6);
}

#[test]
fn init_app_seeds_callable_profile_come_from_head() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };
    let entry = crate::trap::dispatch::OS_TRAP_TABLE_BASE + 0x78 * 4;
    let previous_head = runner.bus.read_long(entry);
    runner
        .dispatcher
        .install_trap_address(&mut runner.bus, 0xA078, 0x0021_1000)
        .unwrap();
    runner.bus.write_long(0x28, 0x0021_2000);
    runner.init_app(&app);
    assert_eq!(
        runner.dispatcher.trap_table_profile,
        Some(TrapTableProfile::M68k68040)
    );
    assert_ne!(runner.bus.read_long(entry), previous_head);
    assert_ne!(
        runner.dispatcher.trap_table_address(&runner.bus, 0xA078),
        Some(0x0021_1000)
    );
    assert!(runner.dispatcher.aline_vector_is_default(&runner.bus));

    let head = runner
        .bus
        .read_long(crate::trap::dispatch::OS_TRAP_TABLE_BASE + 0x78 * 4);
    let successor = runner.bus.read_long(head + 4);
    assert_eq!(runner.bus.read_long(head), 0x6006_4EF9);
    runner.m68k.cpu.write_reg(Register::PC, head);

    let (steps, running) = runner.run_steps(3, None);

    assert!(running);
    assert_eq!(steps, 3);
    assert_eq!(runner.m68k.cpu.read_reg(Register::PC), successor);
}

#[test]
fn profile_unimplemented_gateway_executes_system_error_12() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };
    runner.init_app(&app);

    let gateway = runner
        .dispatcher
        .trap_table_address(&runner.bus, 0xAA6E)
        .unwrap();
    assert_eq!(runner.bus.read_word(gateway), 0xAE6E);
    let call_site = 0x0002_0000;
    let initial_sp = 0x007F_FE00;
    runner.bus.write_word(call_site, 0x4E90); // JSR (A0)
    runner.m68k.cpu.write_reg(Register::A0, gateway);
    runner.m68k.cpu.write_reg(Register::A7, initial_sp);
    runner.m68k.cpu.write_reg(Register::PC, call_site);

    let (steps, running) = runner.run_steps(2, None);

    assert_eq!(steps, 2);
    assert!(!running);
    assert_eq!(runner.dispatcher.current_trap_caller, Some(call_site + 2));
    assert_eq!(
        runner
            .bus
            .read_word(crate::memory::globals::addr::DS_ERR_CODE),
        12
    );
}

#[test]
fn init_app_seeds_callable_swap_font_low_memory_vector() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };
    runner.init_app(&app);

    let swap_font_trampoline = runner
        .bus
        .read_long(crate::memory::globals::addr::J_SWAP_FONT);
    assert_ne!(swap_font_trampoline, 0);
    assert_eq!(
        [
            runner.bus.read_word(swap_font_trampoline),
            runner.bus.read_word(swap_font_trampoline + 2),
            runner.bus.read_word(swap_font_trampoline + 4),
        ],
        [0x205F, 0xA901, 0x4ED0]
    );

    let fm_input_sp = 0x007F_FE00u32;
    let fm_input = 0x0002_1000u32;
    let return_pc = 0x0002_0000u32;
    runner.bus.write_word(fm_input, 3); // family
    runner.bus.write_word(fm_input + 2, 12); // size
    runner.bus.write_byte(fm_input + 4, 0); // face
    runner.bus.write_byte(fm_input + 5, 1); // needBits
    runner.bus.write_word(fm_input + 6, 0); // device
    runner.bus.write_word(fm_input + 8, 1); // numer.v
    runner.bus.write_word(fm_input + 10, 1); // numer.h
    runner.bus.write_word(fm_input + 12, 1); // denom.v
    runner.bus.write_word(fm_input + 14, 1); // denom.h
    runner.bus.write_long(fm_input_sp, fm_input); // CONST VAR inRec
    runner.bus.write_long(fm_input_sp + 4, 0); // result slot
    runner.bus.write_long(fm_input_sp - 4, return_pc);
    runner.bus.write_word(return_pc, 0x4E71); // NOP
    runner
        .m68k
        .cpu
        .write_reg(Register::PC, swap_font_trampoline);
    runner.m68k.cpu.write_reg(Register::A7, fm_input_sp - 4);

    let (steps, running) = runner.run_steps(3, None);

    assert!(running);
    assert_eq!(steps, 3);
    assert_eq!(runner.m68k.cpu.read_reg(Register::PC), return_pc);
    assert_eq!(runner.m68k.cpu.read_reg(Register::A7), fm_input_sp + 4);
    assert_ne!(
        runner.bus.read_long(fm_input_sp + 4),
        0,
        "JSwapFont should return a non-NIL FMOutPtr through the Pascal result slot"
    );
}

#[test]
fn classic_launch_and_relaunch_seed_the_read_only_rom_version_pointer() {
    let mut runner = FixtureRunner::new(8 * 1024 * 1024, FixtureRunnerConfig::default());
    let app = LoadedApp {
        ppc: None,
        code0_header: Code0Header {
            above_a5: 0,
            below_a5: 0x2000,
            jump_table_size: 0,
            jump_table_offset: 0,
        },
        a5_base: 0x0040_0000,
        jump_table: Vec::new(),
        segment_bases: HashMap::new(),
        loaded_image_end: 0,
        initial_sp: 0x007F_FFC0,
        size_resource: None,
    };
    for _ in 0..2 {
        runner.bus.write_long(addr::ROM_BASE, 0);
        runner.init_app(&app);
        let rom_base = runner.bus.read_long(addr::ROM_BASE);
        assert_eq!(rom_base, crate::machine_profile::CLASSIC_ROM_BASE);
        assert_eq!(runner.bus.read_word(rom_base + 8), 0x067C);
        // The original legacy helper returns the ROM version from the low byte.
        assert!(runner.bus.read_word(rom_base + 8) & 0xFF >= 0x75);
    }
}