// SPDX-License-Identifier: MIT //! The bank-switching decoders as Stolberg's guide states them (tier 3), on synthetic ROMs //! (the reference's tests/test_schemes.py; its two `scripts/scan_4000.py` tests test a //! research script, not the board, and are not transcribed). mod common; use a7800::cartridge::connector::Cartridge; use a7800::cartridge::description::CartDescription; use a7800::cartridge::formats::build; use a7800::cartridge::schemes::Banked; use a7800::cartridge::table; /// n 16 KiB banks, every byte of bank k equal to k, each 8 KiB half marked in its first byte. fn banks(n: u8) -> Vec { let mut rom = Vec::new(); for k in 0..n { let mut bank = vec![k; 0x4000]; bank[0] = 0x80 | k; // first half bank[0x2000] = 0xC0 | k; // second half rom.extend(bank); } rom } fn cart(scheme: &str, at_4000: &str, rom: Option>) -> Box { let mut desc = CartDescription::new(&rom.unwrap_or_else(|| banks(8))); desc.scheme = scheme.to_string(); desc.at_4000 = at_4000.to_string(); build(&desc).unwrap() } fn rd(c: &mut Box, address: u16) -> Option { c.access(address, 0, true, false, 0) } fn wr(c: &mut Box, address: u16, value: u8) { c.access(address, value, false, false, 0); } #[test] fn supergame_switches_8000_and_fixes_the_last_bank() { let mut c = cart("supergame", "none", None); assert!(rd(&mut c, 0xC001) == Some(7) && rd(&mut c, 0x8001) == Some(0)); wr(&mut c, 0xBFFF, 3); // Planet Smashers' hot spot: any write to $8000-$BFFF assert_eq!(rd(&mut c, 0x8001), Some(3)); assert_eq!(rd(&mut c, 0x4000), None); // nothing connected } #[test] fn supergame_options_at_4000() { assert_eq!(rd(&mut cart("supergame", "fixed-bank", None), 0x4001), Some(6)); let mut ram = cart("supergame", "ram16", None); wr(&mut ram, 0x7FFF, 0x5A); assert_eq!(rd(&mut ram, 0x7FFF), Some(0x5A)); let mut exrom = cart("supergame", "rom", Some(banks(9))); assert!(rd(&mut exrom, 0x4001) == Some(0) && rd(&mut exrom, 0xC001) == Some(8)); wr(&mut exrom, 0x8000, 0); assert_eq!(rd(&mut exrom, 0x8001), Some(1)); // switchable banks are 1-8 (C9) } #[test] fn activision_layout() { let mut c = cart("activision", "none", None); wr(&mut c, 0xFF83, 0); assert!(rd(&mut c, 0xA000) == Some(0x83) && rd(&mut c, 0xC000) == Some(0xC3)); assert!(rd(&mut c, 0x4000) == Some(0xC6) && rd(&mut c, 0x6000) == Some(0x86)); // bank 6: second, then first half assert!(rd(&mut c, 0x8000) == Some(0xC7) && rd(&mut c, 0xE000) == Some(0x87)); // bank 7: second, then first half } #[test] fn absolute() { let mut c = cart("absolute", "none", Some(banks(4))); wr(&mut c, 0x8000, 2); assert_eq!(rd(&mut c, 0x4001), Some(1)); wr(&mut c, 0x8000, 1); assert_eq!(rd(&mut c, 0x4001), Some(0)); assert!(rd(&mut c, 0x8001) == Some(2) && rd(&mut c, 0xC001) == Some(3)); } #[test] fn mirror_ram_blocks_repeat_twice() { let mut c = cart("linear", "mirror-ram-2k", Some(banks(2))); wr(&mut c, 0x4005, 0x11); assert_eq!(rd(&mut c, 0x4105), Some(0x11)); wr(&mut c, 0x4205, 0x22); assert!(rd(&mut c, 0x4305) == Some(0x22) && rd(&mut c, 0x4005) == Some(0x11)); } #[test] fn mirror_ram_is_2k_in_4000_4fff_as_the_dump_uses_it() { // Rescue on Fractalus: the even pages written, MARIA reads all sixteen. let mut c = cart("linear", "mirror-ram-2k", Some(banks(2))); for block in 0..8u16 { wr(&mut c, 0x4000 + block * 0x200 + 0x7F, 0x10 + block as u8); // the even copy, as the program does } for block in 0..8u16 { let odd = 0x4100 + block * 0x200 + 0x7F; assert_eq!(c.access(odd, 0, true, true, 0), Some(0x10 + block as u8)); // a DMA read of the odd copy } let distinct: std::collections::BTreeSet<_> = (0..8u16).map(|b| rd(&mut c, 0x4000 + b * 0x200 + 0x7F)).collect(); assert_eq!(distinct.len(), 8); // 2 KiB, no fold wr(&mut c, 0x5000, 0x55); assert!(rd(&mut c, 0x5000).is_none() && rd(&mut c, 0x4000) == Some(0)); // nothing at $5000-$7FFF assert!(rd(&mut c, 0x7F7F).is_none()); } #[test] fn activision_rows_are_confirmed_by_the_dumps_own_bytes() { let Some(payload) = common::licensed("Double Dragon (USA).zip") else { return; }; let row = table::lookup(&payload).unwrap(); assert!(row.source == "confirmed" && row.scheme == "activision"); let swapped: Vec = (0..payload.len()) .step_by(0x4000) .flat_map(|i| [&payload[i + 0x2000..i + 0x4000], &payload[i..i + 0x2000]].concat()) .collect(); let softlist_row = table::lookup(&swapped).unwrap(); assert_eq!(softlist_row.key, "ddragonu"); } #[test] fn bank6_rows_are_confirmed_rows_with_bank_6_at_4000() { let Some(payload) = common::licensed("Meltdown (USA).zip") else { return; }; let row = table::lookup(&payload).unwrap(); assert!(row.source == "confirmed" && row.at_4000 == "fixed-bank"); assert!(row.citation.contains("own bytes") && row.key == "meltdownu"); } #[test] fn bankset_maria_reads_her_set_and_bank_ram() { // The wiki's "Bankset Bankswitching": SALLY's image first, MARIA's second; one // bank select for both; bank RAM: SALLY's writes to $C000-$FFFF land in MARIA's. let mut rom = banks(8); rom.extend(banks(8).iter().map(|b| b ^ 0x20)); let mut desc = CartDescription::new(&rom); desc.scheme = "supergame".to_string(); desc.at_4000 = "ram32-halt".to_string(); desc.bankset = true; let mut c = build(&desc).unwrap(); assert!(rd(&mut c, 0x8001) == Some(0) && c.access(0x8001, 0, true, true, 0) == Some(0x20)); wr(&mut c, 0x8000, 3); assert!(rd(&mut c, 0x8001) == Some(3) && c.access(0x8001, 0, true, true, 0) == Some(0x23)); wr(&mut c, 0x4005, 0x11); // SALLY's RAM wr(&mut c, 0xC005, 0x22); // MARIA's RAM, written by SALLY assert!(rd(&mut c, 0x4005) == Some(0x11) && c.access(0x4005, 0, true, true, 0) == Some(0x22)); } #[test] fn pokey_socket_access_then_tick_and_snapshot_round_trip() { // pokey-python's docs/adoption.md: register = A3-A0, the access before the tick of // its own cycle, snapshot()/restore() of the chip; the cartridge snapshot carries it. let mut desc = CartDescription::new(&banks(8)); desc.scheme = "supergame".to_string(); desc.at_4000 = "pokey".to_string(); desc.pokey = vec![0x4000]; let mut c = build(&desc).unwrap(); for (address, value) in [(0x400F, 0x03), (0x4010, 0x20), (0x4001, 0xAF)] { // $4010: mirror wr(&mut c, address, value); // SKCTL, AUDF1, AUDC1 (pure tone, volume 15) c.tick(0); } for _ in 0..500 { c.tick(0); } let state = c.snapshot(); let mut fresh = build(&desc).unwrap(); fresh.restore(&state).unwrap(); for _ in 0..2000 { c.tick(0); fresh.tick(0); let a = c.as_any().downcast_ref::().unwrap().pokeys[0] .1 .chip .borrow() .levels(); let b = fresh.as_any().downcast_ref::().unwrap().pokeys[0] .1 .chip .borrow() .levels(); assert_eq!(a, b); assert_eq!(rd(&mut c, 0x400A), rd(&mut fresh, 0x400A)); // RANDOM } }