rsemu 0.0.2

A multiplatform emulator in pure Rust, built bottom-up on a generic framework.
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
//! Tests for the Game Boy's devices.
//!
//! Every device here carries a save/load round trip, because CLAUDE.md asks for
//! one with the device rather than later, and the interesting behaviours — the
//! divider's side effects, the LCD's variable mode 3, MBC1's unreachable banks —
//! get a test each because none of them is visible in a screenshot.

use alloc::sync::Arc;
use alloc::vec;
use alloc::vec::Vec;

use crate::core::device::{Device, ResetKind};
use crate::core::props::Props;
use crate::core::space::{MemAttrs, MemOps, RegionKind, RegionRef};
use crate::core::state::{MachineShape, Migrations, StateReader, StateWriter};

use super::apu::GbApu;
use super::cart::{Cartridge, GbCart, Mapper, RTC_HZ, synthetic_image};
use super::joypad::{Button, GbJoypad};
use super::ppu::{self, GbPpu, Mode, lcdc, stat};
use super::serial::{GbSerial, TRANSFER_CLOCKS};
use super::timer::GbTimer;

/// The [`MemOps`] behind an I/O region.
///
/// A test reaches a device's registers the way the address space does, rather
/// than through a private method, so that what is tested is the surface a guest
/// actually sees.
fn io(region: &RegionRef) -> &Arc<dyn MemOps> {
    match region.kind() {
        RegionKind::Io(ops) => ops,
        other => panic!("expected an I/O region, found {other:?}"),
    }
}

/// Save a device and load it back into a fresh one, asserting the bytes match.
///
/// The round trip CLAUDE.md asks for, as one function: a device whose `save` and
/// `load` disagree produces a different chunk the second time round, and this
/// catches that without every test having to spell it out.
fn round_trip<D: Device>(saved: &D, restored: &D, path: &str) {
    let class = saved.class();
    let encode = |d: &D| {
        let mut writer = StateWriter::new(MachineShape::new());
        {
            let mut chunk = writer
                .chunk(path, class.name, class.version)
                .expect("a chunk");
            d.save(&mut chunk).expect("saves");
        }
        writer.to_vec().expect("serialises")
    };
    let bytes = encode(saved);
    let reader = StateReader::new(&bytes).expect("well formed");
    let chunk = reader
        .load(path, class.name, class.version, &Migrations::new())
        .expect("the chunk is there");
    restored.load(&mut chunk.reader()).expect("loads");
    assert_eq!(
        encode(restored),
        bytes,
        "`{}` does not round-trip",
        class.name
    );
}

// ---------------------------------------------------------------------------
// The cartridge header
// ---------------------------------------------------------------------------

#[test]
fn the_header_decides_the_mapper_the_ram_and_the_battery() {
    let rom = synthetic_image(2, 0x00, 0x00, &[0x00]);
    let cart = Cartridge::parse(rom).expect("a valid image");
    assert_eq!(cart.kind().mapper, Mapper::None);
    assert!(!cart.kind().ram);
    assert_eq!(cart.rom_banks(), 2);
    assert_eq!(cart.ram_len(), 0);
    assert!(cart.header_checksum_ok(), "the generator computes it");

    // $13 is MBC3 with RAM and a battery but no clock; $03 in $0149 is 32 KiB.
    let rom = synthetic_image(8, 0x13, 0x03, &[0x00]);
    let cart = Cartridge::parse(rom).expect("a valid image");
    assert_eq!(cart.kind().mapper, Mapper::Mbc3);
    assert!(cart.kind().ram && cart.kind().battery && !cart.kind().rtc);
    assert_eq!(cart.ram_len(), 0x8000);
    assert_eq!(cart.rom_banks(), 8);
}

#[test]
fn an_image_that_disagrees_with_its_own_header_is_refused() {
    // A header claiming eight banks in an image holding two: the mistake that
    // otherwise shows up as a game jumping into nothing.
    let mut rom = synthetic_image(2, 0x00, 0x00, &[0x00]);
    rom[0x0148] = 2;
    let e = Cartridge::parse(rom).expect_err("the size disagrees");
    assert!(alloc::format!("{e}").contains("declares"), "{e}");

    // A cartridge type that was never assigned.
    let mut rom = synthetic_image(2, 0x00, 0x00, &[0x00]);
    rom[0x0147] = 0x77;
    let e = Cartridge::parse(rom).expect_err("no such controller");
    assert!(
        alloc::format!("{e}").contains("0x77") || alloc::format!("{e}").contains("$77"),
        "{e}"
    );

    // Too short to hold a header at all.
    assert!(Cartridge::parse(vec![0u8; 16]).is_err());
}

#[test]
fn a_wrong_header_checksum_is_recorded_rather_than_refused() {
    // The boot ROM would refuse to start this cartridge; rsemu runs it anyway,
    // because a test ROM with a deliberately broken header is still worth
    // running. What matters is that we know.
    let mut rom = synthetic_image(2, 0x00, 0x00, &[0x00]);
    rom[0x014d] ^= 0xff;
    let cart = Cartridge::parse(rom).expect("still parses");
    assert!(!cart.header_checksum_ok());
}

// ---------------------------------------------------------------------------
// Bank switching
// ---------------------------------------------------------------------------

/// Build a cartridge device whose every bank is filled with its own number, so
/// a read says which bank answered.
fn banked_cart(banks: u32, kind: u8, ram: u8) -> GbCart {
    let mut rom = synthetic_image(banks, kind, ram, &[0x00]);
    for bank in 0..banks as usize {
        // Byte $2000 of each bank, chosen because it is past the header.
        rom[bank * 0x4000 + 0x2000] = bank as u8;
    }
    GbCart::new(Cartridge::parse(rom).expect("valid"))
}

/// Read one byte of the `$0000-$7FFF` window.
fn rom_read(cart: &GbCart, offset: u64) -> u8 {
    let region = Device::region(cart, super::cart::ROM_REGION).expect("the ROM window");
    let mut byte = [0u8; 1];
    io(&region)
        .read(offset, &mut byte, MemAttrs::DEFAULT)
        .expect("answers");
    byte[0]
}

/// Write one byte of the `$0000-$7FFF` window — a mapper register.
fn rom_write(cart: &GbCart, offset: u64, value: u8) {
    let region = Device::region(cart, super::cart::ROM_REGION).expect("the ROM window");
    io(&region)
        .write(offset, &[value], MemAttrs::DEFAULT)
        .expect("accepts");
}

#[test]
fn mbc1_cannot_select_bank_zero_in_the_switchable_window() {
    // The quirk every large MBC1 game works around: writing 0 to $2000 selects
    // bank 1, and so do $20, $40 and $60 in the upper groups.
    let cart = banked_cart(4, 0x01, 0x00);
    assert_eq!(cart.rom_bank(), 1, "bank 1 out of reset");
    rom_write(&cart, 0x2000, 0);
    assert_eq!(cart.rom_bank(), 1);
    assert_eq!(rom_read(&cart, 0x6000), 1);
    rom_write(&cart, 0x2000, 2);
    assert_eq!(cart.rom_bank(), 2);
    assert_eq!(rom_read(&cart, 0x6000), 2);
    // Bank 0 still answers at $0000 whatever is selected.
    assert_eq!(rom_read(&cart, 0x2000), 0);
}

#[test]
fn mbc1_advanced_mode_banks_the_low_window_too() {
    // 1 MiB, so the two high bits reach banks $20 and $40. In simple mode the
    // low window is always bank 0; in advanced mode it follows the high bits.
    let cart = banked_cart(64, 0x01, 0x00);
    rom_write(&cart, 0x4000, 1); // the high bits
    assert_eq!(cart.rom_bank_low(), 0, "simple mode pins the low window");
    assert_eq!(cart.rom_bank(), 0x21, "and $20 becomes $21");
    rom_write(&cart, 0x6000, 1); // advanced mode
    assert_eq!(cart.rom_bank_low(), 0x20);
    assert_eq!(rom_read(&cart, 0x2000), 0x20);
}

#[test]
fn mbc5_is_the_first_controller_where_bank_zero_means_bank_zero() {
    let cart = banked_cart(4, 0x19, 0x00);
    rom_write(&cart, 0x2000, 0);
    assert_eq!(cart.rom_bank(), 0);
    assert_eq!(rom_read(&cart, 0x6000), 0);
    rom_write(&cart, 0x2000, 3);
    assert_eq!(rom_read(&cart, 0x6000), 3);
}

#[test]
fn cartridge_ram_answers_only_while_it_is_enabled() {
    let cart = banked_cart(4, 0x03, 0x02); // MBC1, 8 KiB, battery
    let region = Device::region(&cart, super::cart::RAM_REGION).expect("the RAM window");
    let ops = io(&region);
    let mut byte = [0u8; 1];

    // Disabled out of reset: reads are the idle bus and writes go nowhere.
    ops.write(0, &[0x42], MemAttrs::DEFAULT).expect("accepts");
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0xff);

    // $0A in the low nibble is the magic value; nothing else enables it.
    rom_write(&cart, 0x0000, 0x0f);
    assert!(!cart.ram_enabled());
    rom_write(&cart, 0x0000, 0x0a);
    assert!(cart.ram_enabled());
    ops.write(0, &[0x42], MemAttrs::DEFAULT).expect("accepts");
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0x42);
}

#[test]
fn the_mbc3_clock_counts_its_own_crystals_ticks() {
    // $10 is MBC3 with RAM, a battery and the timer.
    let cart = banked_cart(4, 0x10, 0x02);
    rom_write(&cart, 0x0000, 0x0a); // enable RAM and the clock registers
    rom_write(&cart, 0x4000, 0x08); // select the seconds register

    // One second is exactly RTC_HZ ticks of the cartridge's own 32.768 kHz can.
    // No floating point anywhere: the residual is carried in whole ticks.
    Device::advance_to(&cart, RTC_HZ * 90 + RTC_HZ / 2);
    // The program has not latched, so it still reads the old copy.
    let region = Device::region(&cart, super::cart::RAM_REGION).expect("the RAM window");
    let ops = io(&region);
    let mut byte = [0u8; 1];
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0, "nothing latched yet");

    // The latch is a 0-then-1 edge, so a program reading five registers sees
    // one consistent instant.
    rom_write(&cart, 0x6000, 0);
    rom_write(&cart, 0x6000, 1);
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 30, "90 seconds is one minute and thirty");
    rom_write(&cart, 0x4000, 0x09); // minutes
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 1);

    let rtc = cart.rtc().expect("this cartridge has a clock");
    assert_eq!((rtc.minutes, rtc.seconds), (1, 30));
}

#[test]
fn a_halted_clock_does_not_run() {
    let cart = banked_cart(4, 0x10, 0x02);
    rom_write(&cart, 0x0000, 0x0a);
    rom_write(&cart, 0x4000, 0x0c); // the day-high register, which holds the halt bit
    let region = Device::region(&cart, super::cart::RAM_REGION).expect("RAM");
    let ops = io(&region);
    ops.write(0, &[0x40], MemAttrs::DEFAULT).expect("halts it");
    Device::advance_to(&cart, RTC_HZ * 10);
    assert_eq!(cart.rtc().expect("a clock").seconds, 0);
}

#[test]
fn a_cartridge_round_trips_its_banks_its_ram_and_its_clock() {
    let cart = banked_cart(4, 0x10, 0x02);
    rom_write(&cart, 0x0000, 0x0a);
    rom_write(&cart, 0x2000, 3);
    cart.poke_ram(0x10, 0x5a);
    Device::advance_to(&cart, RTC_HZ * 5);
    let restored = banked_cart(4, 0x10, 0x02);
    round_trip(&cart, &restored, "cart");
    assert_eq!(restored.rom_bank(), 3);
    assert_eq!(restored.peek_ram(0x10), Some(0x5a));
    assert_eq!(restored.rtc().expect("a clock").seconds, 5);
}

#[test]
fn the_cartridge_class_needs_its_media_slot() {
    let e = GbCart::from_props(&Props::new()).expect_err("no rom");
    assert!(alloc::format!("{e}").contains("rom"), "{e}");
}

// ---------------------------------------------------------------------------
// The divider and timer
// ---------------------------------------------------------------------------

#[test]
fn div_is_the_top_byte_of_a_counter_running_at_the_crystal_rate() {
    let timer = GbTimer::new();
    timer.advance_by(255);
    assert_eq!(timer.div(), 0);
    timer.advance_by(1);
    assert_eq!(timer.div(), 1);
    timer.advance_by(256 * 10);
    assert_eq!(timer.div(), 11);
}

#[test]
fn writing_div_resets_all_sixteen_bits() {
    let timer = GbTimer::new();
    timer.advance_by(1000);
    assert_ne!(timer.counter(), 0);
    timer.write_register(0, 0xff);
    assert_eq!(timer.counter(), 0, "the whole counter, not just DIV");
    assert_eq!(timer.div(), 0);
}

#[test]
fn tima_counts_at_the_rate_tac_selects() {
    // The four rates are four *bit positions*, and the order is not monotonic:
    // 00 is the slowest and 01 the fastest (Pan Docs, "Timer and Divider").
    for (tac, period) in [(0u8, 1024u64), (1, 16), (2, 64), (3, 256)] {
        let timer = GbTimer::new();
        timer.write_register(3, 0x04 | tac);
        timer.advance_by(period * 5);
        assert_eq!(timer.tima(), 5, "TAC={tac:#04x}");
    }
}

#[test]
fn a_div_write_can_clock_tima_through_the_falling_edge_detector() {
    // The famous one. With TAC = $05 the selected bit is bit 3, so a counter of
    // 8 has it set; zeroing the counter drops that bit, and a falling edge is a
    // TIMA increment however recently the last one was.
    let timer = GbTimer::new();
    timer.write_register(3, 0x05);
    timer.advance_by(8);
    assert_eq!(timer.tima(), 0, "no edge yet — the bit only just went up");
    timer.write_register(0, 0);
    assert_eq!(timer.tima(), 1, "the DIV write was the falling edge");

    // And with the bit already clear, a DIV write does nothing.
    let timer = GbTimer::new();
    timer.write_register(3, 0x05);
    timer.advance_by(4);
    timer.write_register(0, 0);
    assert_eq!(timer.tima(), 0);
}

#[test]
fn disabling_the_timer_while_the_selected_bit_is_high_also_clocks_it() {
    // Same detector, different input: clearing TAC's enable bit makes the
    // detector's input fall, and the increment happens for the same reason.
    let timer = GbTimer::new();
    timer.write_register(3, 0x05);
    timer.advance_by(8);
    timer.write_register(3, 0x01); // enable cleared, rate unchanged
    assert_eq!(timer.tima(), 1);
}

#[test]
fn an_overflow_reloads_from_tma_one_machine_cycle_late() {
    let timer = GbTimer::new();
    timer.write_register(2, 0x37); // TMA
    timer.write_register(1, 0xff); // TIMA, one from the top
    timer.write_register(3, 0x05); // enabled, bit 3
    timer.advance_by(16);
    // Inside the reload window TIMA genuinely reads zero: the counter has
    // wrapped and the reload has not happened yet.
    assert_eq!(timer.tima(), 0);
    timer.advance_by(4);
    assert_eq!(timer.tima(), 0x37, "TMA arrived");
}

#[test]
fn writing_tima_inside_the_reload_window_cancels_the_reload() {
    let timer = GbTimer::new();
    timer.write_register(2, 0x37);
    timer.write_register(1, 0xff);
    timer.write_register(3, 0x05);
    timer.advance_by(16);
    assert_eq!(timer.tima(), 0);
    timer.write_register(1, 0x11);
    timer.advance_by(8);
    assert_eq!(timer.tima(), 0x11, "the write won, not TMA");
}

#[test]
fn the_next_event_is_never_in_the_past_and_never_further_than_a_div_step() {
    // What makes a mid-quantum read correct: between two of this device's own
    // events nothing it publishes changes.
    let timer = GbTimer::new();
    for tac in [0u8, 1, 2, 3, 4, 5, 6, 7] {
        timer.write_register(3, tac);
        for _ in 0..40 {
            let now = Device::current_tick(&timer);
            let next = Device::next_event_tick(&timer).expect("always something");
            assert!(next > now, "TAC={tac:#04x}: {next} <= {now}");
            assert!(next - now <= 256, "TAC={tac:#04x}: {} clocks", next - now);
            timer.advance_by(3);
        }
    }
}

#[test]
fn a_reset_leaves_the_divider_where_a_boot_rom_would() {
    // `new` is the honest power-on state; `reset` is what a machine performs,
    // and on a console that means "after the boot ROM has run". Pan Docs gives
    // `DIV` as `$AB` at the handoff, and a game that seeds itself from `DIV`
    // gets the same number every run without this.
    let timer = GbTimer::new();
    assert_eq!(timer.counter(), 0, "power-on is zero");
    timer.reset(ResetKind::Cold);
    assert_eq!(timer.div(), 0xab);
    assert_eq!(timer.counter(), super::timer::POST_BOOT_COUNTER);
    // And the edge detectors start from the counter they are now looking at,
    // not from zero: the first tick after a reset must not look like an edge.
    timer.write_register(3, 0x04);
    timer.advance_by(1);
    assert_eq!(timer.tima(), 0);
}

#[test]
fn a_timer_round_trips() {
    let timer = GbTimer::new();
    timer.write_register(3, 0x05);
    timer.write_register(2, 0x42);
    timer.advance_by(1234);
    let restored = GbTimer::new();
    round_trip(&timer, &restored, "timer");
    assert_eq!(restored.counter(), timer.counter());
    assert_eq!(restored.tima(), timer.tima());
    assert_eq!(restored.tma(), 0x42);
}

// ---------------------------------------------------------------------------
// The LCD controller
// ---------------------------------------------------------------------------

/// A controller with the LCD on and the given `STAT` enables.
fn lcd(stat_bits: u8) -> GbPpu {
    let ppu = GbPpu::new();
    ppu.write_register(0x01, stat_bits);
    ppu
}

#[test]
fn a_line_walks_mode_two_then_three_then_zero() {
    let ppu = lcd(0);
    assert_eq!(ppu.mode(), Mode::OamScan);
    ppu.advance_by(ppu::OAM_SCAN_DOTS - 1);
    assert_eq!(ppu.mode(), Mode::OamScan, "80 dots of it");
    ppu.advance_by(1);
    assert_eq!(ppu.mode(), Mode::Drawing);
    ppu.advance_by(ppu::MODE3_MIN_DOTS);
    assert_eq!(ppu.mode(), Mode::HBlank);
    ppu.advance_by(ppu::DOTS_PER_LINE - ppu::OAM_SCAN_DOTS - ppu::MODE3_MIN_DOTS);
    assert_eq!(ppu.position(), (1, 0));
    assert_eq!(ppu.mode(), Mode::OamScan, "and the next line begins");
}

#[test]
fn vblank_starts_on_line_144_and_the_frame_ends_on_154() {
    let ppu = lcd(0);
    ppu.advance_by(ppu::DOTS_PER_LINE * 144);
    assert_eq!(ppu.position().0, 144);
    assert_eq!(ppu.mode(), Mode::VBlank);
    ppu.advance_by(ppu::DOTS_PER_LINE * 10);
    assert_eq!(ppu.position(), (0, 0));
    assert_eq!(ppu.frame(), 1);
    assert_eq!(ppu.dots(), ppu::DOTS_PER_FRAME);
}

#[test]
fn ly_reads_zero_for_most_of_line_153() {
    // The quirk every accuracy suite tests: `LY` reads 153 for four dots and
    // then 0, while the frame has not ended.
    let ppu = lcd(0);
    ppu.advance_by(ppu::DOTS_PER_LINE * 153);
    assert_eq!(ppu.read_register(0x04), 153);
    ppu.advance_by(4);
    assert_eq!(ppu.read_register(0x04), 0, "but the frame is still running");
    assert_eq!(ppu.frame(), 0);
    ppu.advance_by(ppu::DOTS_PER_LINE - 4);
    assert_eq!(ppu.frame(), 1);
}

#[test]
fn mode_three_is_extended_by_scroll_the_window_and_objects() {
    // A bare line is the minimum.
    let ppu = lcd(0);
    ppu.advance_by(ppu::OAM_SCAN_DOTS);
    assert_eq!(ppu.mode(), Mode::Drawing);
    ppu.advance_by(ppu::MODE3_MIN_DOTS - 1);
    assert_eq!(ppu.mode(), Mode::Drawing);
    ppu.advance_by(1);
    assert_eq!(ppu.mode(), Mode::HBlank);

    // `SCX & 7` discards that many pixels at the left edge, and the controller
    // pays for them.
    let ppu = lcd(0);
    ppu.write_register(0x03, 5); // SCX
    ppu.advance_by(ppu::OAM_SCAN_DOTS + ppu::MODE3_MIN_DOTS + 4);
    assert_eq!(ppu.mode(), Mode::Drawing, "five dots longer");
    ppu.advance_by(1);
    assert_eq!(ppu.mode(), Mode::HBlank);

    // One object on the line costs at least six more.
    let ppu = lcd(0);
    ppu.write_register(0x00, lcdc::LCD_ENABLE | lcdc::BG_ENABLE | lcdc::OBJ_ENABLE);
    ppu.poke_oam(0, 16); // y = 16 puts it on line 0
    ppu.poke_oam(1, 32); // x
    ppu.advance_by(ppu::OAM_SCAN_DOTS + ppu::MODE3_MIN_DOTS + 5);
    assert_eq!(ppu.mode(), Mode::Drawing, "an object extends mode 3");
}

#[test]
fn video_ram_reads_as_ff_during_mode_three_and_not_otherwise() {
    let ppu = lcd(0);
    ppu.poke_vram(0, 0x5a);
    let region = Device::region(&ppu, ppu::VRAM_REGION).expect("VRAM");
    let ops = io(&region);
    let mut byte = [0u8; 1];

    // Mode 2: readable.
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0x5a);

    ppu.advance_by(ppu::OAM_SCAN_DOTS);
    assert_eq!(ppu.mode(), Mode::Drawing);
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0xff, "blocked");
    // A write is dropped rather than faulting: the write really does go nowhere.
    ops.write(0, &[0x11], MemAttrs::DEFAULT).expect("accepts");
    assert_eq!(ppu.peek_vram(0), 0x5a);

    // A debugger sees through the blocking, because a monitor showing the tile
    // map during mode 3 should show the tile map (invariant 5).
    ops.read(0, &mut byte, MemAttrs::DEBUG).expect("answers");
    assert_eq!(byte[0], 0x5a);
}

#[test]
fn object_memory_is_blocked_during_both_the_scan_and_the_drawing() {
    let ppu = lcd(0);
    ppu.poke_oam(0, 0x5a);
    let region = Device::region(&ppu, ppu::OAM_REGION).expect("OAM");
    let ops = io(&region);
    let mut byte = [0u8; 1];
    assert_eq!(ppu.mode(), Mode::OamScan);
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0xff);
    ppu.advance_by(ppu::OAM_SCAN_DOTS + ppu::MODE3_MIN_DOTS);
    assert_eq!(ppu.mode(), Mode::HBlank);
    ops.read(0, &mut byte, MemAttrs::DEFAULT).expect("answers");
    assert_eq!(byte[0], 0x5a);
}

#[test]
fn switching_the_lcd_off_parks_ly_and_unblocks_everything() {
    let ppu = lcd(0);
    ppu.advance_by(ppu::DOTS_PER_LINE * 3 + 100);
    assert_eq!(ppu.position().0, 3);
    ppu.write_register(0x00, 0);
    assert_eq!(ppu.read_register(0x04), 0, "LY parks at zero");
    assert_eq!(ppu.mode(), Mode::HBlank, "and the mode reads as 0");
    assert_eq!(
        Device::next_event_tick(&ppu),
        None,
        "nothing changes while it is off"
    );
    // Advancing does nothing but move the clock.
    let before = ppu.dots();
    ppu.advance_by(10_000);
    assert_eq!(ppu.dots(), before + 10_000);
    assert_eq!(ppu.position().0, 0);
}

#[test]
fn the_stat_line_is_the_or_of_whatever_is_enabled() {
    // With only the mode-0 interrupt enabled the line follows H-blank.
    let ppu = lcd(stat::HBLANK_INT);
    assert_eq!(ppu.mode(), Mode::OamScan);
    assert_eq!(ppu.read_register(0x01) & 3, 2);
    ppu.advance_by(ppu::OAM_SCAN_DOTS + ppu::MODE3_MIN_DOTS);
    assert_eq!(ppu.read_register(0x01) & 3, 0);

    // The coincidence flag is in `STAT` whether or not its interrupt is on.
    let ppu = lcd(0);
    ppu.write_register(0x05, 2); // LYC
    assert_eq!(ppu.read_register(0x01) & stat::LYC_EQUAL, 0);
    ppu.advance_by(ppu::DOTS_PER_LINE * 2);
    assert_eq!(ppu.read_register(0x01) & stat::LYC_EQUAL, stat::LYC_EQUAL);
    // Bit 7 is not implemented and reads as one.
    assert_eq!(ppu.read_register(0x01) & 0x80, 0x80);
}

#[test]
fn a_flat_background_renders_the_palette_shade_it_asks_for() {
    let ppu = lcd(0);
    // Tile 0, every pixel colour 3: both bitplanes all ones.
    for i in 0..16u64 {
        ppu.poke_vram(i, 0xff);
    }
    // The whole $9800 map is tile 0 already (VRAM is zeroed).
    // BGP: map colour 3 to shade 1 and leave the rest.
    ppu.write_register(0x07, 0b01_00_00_00);
    ppu.advance_by(ppu::DOTS_PER_LINE);
    assert_eq!(ppu.pixel(0, 0), Some(1));
    assert_eq!(ppu.pixel(159, 0), Some(1));
    assert_eq!(ppu.pixel(0, 143), Some(0), "not drawn yet");

    // With LCDC bit 0 clear the background is blank whatever the tiles say.
    let ppu = lcd(0);
    for i in 0..16u64 {
        ppu.poke_vram(i, 0xff);
    }
    ppu.write_register(0x07, 0b01_00_00_00);
    ppu.write_register(0x00, lcdc::LCD_ENABLE);
    ppu.advance_by(ppu::DOTS_PER_LINE);
    assert_eq!(ppu.pixel(0, 0), Some(0));
}

#[test]
fn an_object_draws_over_the_background_and_colour_zero_is_transparent() {
    let ppu = lcd(0);
    // Tile 1: the left four pixels of every row are colour 1, the rest colour 0.
    for row in 0..8u64 {
        ppu.poke_vram(16 + row * 2, 0xf0);
        ppu.poke_vram(16 + row * 2 + 1, 0x00);
    }
    ppu.write_register(
        0x00,
        lcdc::LCD_ENABLE | lcdc::BG_ENABLE | lcdc::TILE_DATA | lcdc::OBJ_ENABLE,
    );
    // Object 0 at the top left, using tile 1 and palette OBP0.
    ppu.poke_oam(0, 16);
    ppu.poke_oam(1, 8);
    ppu.poke_oam(2, 1);
    ppu.poke_oam(3, 0);
    // OBP0 maps colour 1 to shade 3.
    ppu.write_register(0x08, 0b00_00_11_00);
    ppu.advance_by(ppu::DOTS_PER_LINE);
    assert_eq!(ppu.pixel(0, 0), Some(3), "the object's colour 1");
    assert_eq!(ppu.pixel(4, 0), Some(0), "its colour 0 is transparent");
}

#[test]
fn an_oam_transfer_copies_a_page_over_160_machine_cycles() {
    use crate::core::space::{AddressSpace, RamStore, Region};

    let ppu = GbPpu::new();
    let space = Arc::new(AddressSpace::new("cpubus", 16));
    let ram = Arc::new(RamStore::new(0x10000));
    for i in 0..160u64 {
        ram.write_u8(0xc000 + i, i as u8).expect("in range");
    }
    space
        .topology()
        .map(Region::ram("ram", ram), 0)
        .expect("maps");
    ppu.attach_space(space);

    ppu.write_register(0x06, 0xc0);
    assert_eq!(ppu.read_register(0x06), 0xc0, "the register reads back");
    // Nothing has moved yet: the first byte goes one machine cycle later.
    assert_eq!(ppu.peek_oam(5), 0);
    ppu.advance_by(4 * 6);
    assert_eq!(ppu.peek_oam(5), 5, "six machine cycles, six bytes");
    assert_eq!(ppu.peek_oam(6), 0, "and not one more");
    ppu.advance_by(4 * 154);
    assert_eq!(ppu.peek_oam(159), 159);
    // And it stops there rather than running off the end of OAM.
    ppu.advance_by(4 * 160);
    assert_eq!(ppu.peek_oam(159), 159);
}

#[test]
fn an_lcd_controller_round_trips() {
    let ppu = lcd(stat::LYC_INT | stat::HBLANK_INT);
    ppu.poke_vram(0x100, 0x5a);
    ppu.poke_oam(4, 0x42);
    ppu.write_register(0x03, 7);
    ppu.advance_by(ppu::DOTS_PER_LINE * 5 + 123);
    let restored = GbPpu::new();
    round_trip(&ppu, &restored, "ppu");
    assert_eq!(restored.position(), ppu.position());
    assert_eq!(restored.peek_vram(0x100), 0x5a);
    assert_eq!(restored.peek_oam(4), 0x42);
    assert_eq!(
        Device::next_event_tick(&restored),
        Device::next_event_tick(&ppu),
        "the derived next event follows the load"
    );
}

// ---------------------------------------------------------------------------
// The joypad
// ---------------------------------------------------------------------------

#[test]
fn the_joypad_is_a_matrix_and_zero_means_pressed() {
    let pad = GbJoypad::new();
    let region = Device::region(&pad, super::joypad::REGISTER_REGION).expect("the register");
    let ops = io(&region);

    // Nothing held: the low nibble is all ones however the rows are selected.
    assert_eq!(pad.read() & 0x0f, 0x0f);

    // Both select bits high means *neither* row is selected, and then nothing
    // can pull a column low.
    ops.write(0, &[0x30], MemAttrs::DEFAULT).expect("accepts");
    pad.set_pressed(Button::Start, true);
    assert_eq!(pad.read() & 0x0f, 0x0f, "neither row is selected");

    // Bit 5 low selects the action buttons. Start is bit 3 of that row.
    ops.write(0, &[0x10], MemAttrs::DEFAULT).expect("accepts");
    assert_eq!(pad.read() & 0x0f, 0b0111);

    // Bit 4 low selects the directions instead, and Start is not in that row.
    ops.write(0, &[0x20], MemAttrs::DEFAULT).expect("accepts");
    assert_eq!(pad.read() & 0x0f, 0x0f);
    pad.set_pressed(Button::Right, true);
    assert_eq!(pad.read() & 0x0f, 0b1110);

    // Both low at once: the two rows are wired together, and a button held in
    // either pulls its column down. This is the state the boot ROM leaves
    // behind — `$FF00` reads `$CF` on a DMG with nothing pressed.
    ops.write(0, &[0x00], MemAttrs::DEFAULT).expect("accepts");
    assert_eq!(pad.read() & 0x0f, 0b0110);
    // Bits 7 and 6 are not implemented.
    assert_eq!(pad.read() & 0xc0, 0xc0);
}

#[test]
fn a_joypad_round_trips_its_buttons_and_its_select_lines() {
    let pad = GbJoypad::new();
    pad.set_buttons(0b1010_0101);
    let region = Device::region(&pad, super::joypad::REGISTER_REGION).expect("the register");
    io(&region)
        .write(0, &[0x10], MemAttrs::DEFAULT)
        .expect("accepts");
    let restored = GbJoypad::new();
    round_trip(&pad, &restored, "pad");
    assert_eq!(restored.buttons(), 0b1010_0101);
    assert_eq!(restored.read(), pad.read());
}

#[test]
fn buttons_are_named_both_ways() {
    for button in Button::ALL {
        assert_eq!(Button::from_name(button.name()), Some(button));
    }
    assert_eq!(Button::from_name("turbo"), None);
}

// ---------------------------------------------------------------------------
// The serial link
// ---------------------------------------------------------------------------

#[test]
fn a_transfer_takes_its_time_and_shifts_in_ones() {
    let link = GbSerial::new();
    let region = Device::region(&link, super::serial::REGISTER_REGION).expect("the registers");
    let ops = io(&region);
    ops.write(0, b"A", MemAttrs::DEFAULT).expect("SB");
    ops.write(1, &[0x81], MemAttrs::DEFAULT).expect("SC: start");
    // The transcript records the byte as it starts to go out.
    assert_eq!(link.transcript(), vec![b'A']);
    // But the transfer is still in flight.
    assert_eq!(link.control() & 0x80, 0x80);
    link.advance_to(TRANSFER_CLOCKS - 1);
    assert_eq!(link.control() & 0x80, 0x80);
    link.advance_to(TRANSFER_CLOCKS);
    assert_eq!(link.control() & 0x80, 0, "and now it is done");
    assert_eq!(link.data(), 0xff, "nothing on the other end of the cable");
}

#[test]
fn an_external_clock_transfer_never_completes_on_its_own() {
    // Which is what hardware does with no cable: the other console supplies the
    // clock, and there is no other console.
    let link = GbSerial::new();
    let region = Device::region(&link, super::serial::REGISTER_REGION).expect("the registers");
    let ops = io(&region);
    ops.write(1, &[0x80], MemAttrs::DEFAULT).expect("external");
    assert_eq!(Device::next_event_tick(&link), None);
    link.advance_to(TRANSFER_CLOCKS * 10);
    assert_eq!(link.control() & 0x80, 0x80, "still waiting");
}

#[test]
fn a_serial_link_round_trips_its_transcript() {
    let link = GbSerial::new();
    let region = Device::region(&link, super::serial::REGISTER_REGION).expect("the registers");
    let ops = io(&region);
    let mut sent = 0u64;
    for byte in b"hi" {
        ops.write(0, &[*byte], MemAttrs::DEFAULT).expect("SB");
        ops.write(1, &[0x81], MemAttrs::DEFAULT).expect("SC");
        sent += TRANSFER_CLOCKS;
        link.advance_to(sent);
    }
    assert_eq!(link.transcript_text(), "hi");
    let restored = GbSerial::new();
    round_trip(&link, &restored, "link");
    assert_eq!(restored.transcript_text(), "hi");
}

// ---------------------------------------------------------------------------
// The sound unit
// ---------------------------------------------------------------------------

#[test]
fn the_sound_unit_ignores_every_register_while_it_is_powered_down() {
    let apu = GbApu::new();
    assert!(!apu.powered());
    apu.write_register(0x02, 0xf0); // NR12
    assert_eq!(apu.read_register(0x02), 0x00, "the write went nowhere");
    apu.write_register(0x16, 0x80); // NR52: power on
    assert!(apu.powered());
    apu.write_register(0x02, 0xf0);
    assert_eq!(apu.read_register(0x02), 0xf0);
}

#[test]
fn powering_down_zeroes_everything_except_the_wave_ram() {
    let apu = GbApu::new();
    apu.write_register(0x16, 0x80);
    apu.write_register(0x14, 0x77); // NR50
    apu.write_register(0x20, 0xab); // wave RAM
    apu.write_register(0x16, 0x00);
    assert_eq!(apu.read_register(0x14), 0x00);
    assert_eq!(apu.read_register(0x20), 0xab, "the waveform survives");
}

#[test]
fn a_channel_reports_itself_in_nr52_until_its_length_runs_out() {
    let apu = GbApu::new();
    apu.write_register(0x16, 0x80); // power
    apu.write_register(0x07, 0xf0); // NR22: full volume, DAC on
    apu.write_register(0x06, 0x3f); // NR21: length 63, so one step to go
    apu.write_register(0x09, 0xc0); // NR24: trigger with length enabled
    assert_eq!(apu.status() & 0x02, 0x02, "channel 2 is on");
    // The length counter is stepped by the frame sequencer's even steps, and
    // the sequencer is clocked from the divider — not from anything of its own.
    apu.step_frame_sequencer();
    assert_eq!(apu.status() & 0x02, 0x00, "and now it is not");
}

#[test]
fn a_dac_that_is_switched_off_switches_its_channel_off_with_it() {
    let apu = GbApu::new();
    apu.write_register(0x16, 0x80);
    apu.write_register(0x07, 0xf0);
    apu.write_register(0x09, 0x80); // trigger
    assert_eq!(apu.status() & 0x02, 0x02);
    // The top five bits of NRx2 drive the DAC directly: all zero and it is off.
    apu.write_register(0x07, 0x07);
    assert_eq!(apu.status() & 0x02, 0x00);
}

#[test]
fn the_frame_sequencer_walks_its_eight_steps() {
    let apu = GbApu::new();
    apu.write_register(0x16, 0x80);
    for expected in 1..=8u8 {
        apu.step_frame_sequencer();
        assert_eq!(apu.frame_step(), expected % 8);
    }
}

#[test]
fn samples_come_out_at_a_power_of_two_divisor_of_the_crystal() {
    let apu = GbApu::new();
    apu.set_recording(true);
    apu.write_register(0x16, 0x80);
    apu.advance_to(super::apu::SAMPLE_DIVISOR * 100);
    assert_eq!(apu.queued_samples(), 100);
    assert_eq!(apu.take_samples().len(), 100);
    assert_eq!(apu.queued_samples(), 0);
    // 4194304 / 128 is exact — no rounding anywhere in the time path.
    assert_eq!(super::apu::SAMPLE_RATE, 32_768);
}

#[test]
fn a_sound_unit_round_trips() {
    let apu = GbApu::new();
    apu.write_register(0x16, 0x80);
    apu.write_register(0x07, 0xf0);
    apu.write_register(0x08, 0x55);
    apu.write_register(0x09, 0x87);
    apu.write_register(0x22, 0xab);
    apu.advance_to(5000);
    let restored = GbApu::new();
    round_trip(&apu, &restored, "apu");
    assert_eq!(restored.status(), apu.status());
    assert_eq!(restored.read_register(0x22), 0xab);
}

// ---------------------------------------------------------------------------
// The classes themselves
// ---------------------------------------------------------------------------

#[test]
fn every_class_registers_binds_and_has_a_schema() {
    let mut registry = crate::core::Registry::new();
    super::register(&mut registry).expect("no collisions");
    let mut bindings = crate::machine::Bindings::new();
    super::bind(&mut bindings).expect("no collisions");
    let schemas = super::schemas();
    let names: Vec<&str> = schemas.iter().map(|s| s.class.as_str()).collect();
    for class in [
        "gb.cart",
        "gb.ppu",
        "gb.timer",
        "gb.apu",
        "gb.joypad",
        "gb.serial",
    ] {
        assert!(registry.get(class).is_some(), "{class} is not registered");
        assert!(bindings.get(class).is_some(), "{class} is not bound");
        assert!(names.contains(&class), "{class} has no schema");
    }
}

#[test]
fn every_device_resets_to_a_documented_state() {
    let devices: Vec<alloc::boxed::Box<dyn Device>> = vec![
        alloc::boxed::Box::new(GbPpu::new()),
        alloc::boxed::Box::new(GbTimer::new()),
        alloc::boxed::Box::new(GbApu::new()),
        alloc::boxed::Box::new(GbJoypad::new()),
        alloc::boxed::Box::new(GbSerial::new()),
    ];
    for device in devices {
        device.reset(ResetKind::Cold);
        device.reset(ResetKind::Warm);
        device.reset(ResetKind::Bus);
    }
}