# SPDX-License-Identifier: MIT """The bank-switching decoders as Stolberg's guide states them (tier 3), on synthetic ROMs.""" from __future__ import annotations from conftest import LICENSED, read_zip from a7800_python.cartridge import table from a7800_python.cartridge.description import CartDescription from a7800_python.cartridge.formats import build def banks(n: int) -> bytes: """n 16 KiB banks, every byte of bank k equal to k, each 8 KiB half marked in its first byte.""" rom = bytearray() for k in range(n): bank = bytearray([k]) * 0x4000 bank[0] = 0x80 | k # first half bank[0x2000] = 0xC0 | k # second half rom += bank return bytes(rom) def cart(scheme: str, at_4000: str = "none", rom: bytes | None = None): desc = CartDescription(payload=rom or banks(8), scheme=scheme, at_4000=at_4000) return build(desc) def rd(c, address): return c.access(address, 0, True, False, 0) def wr(c, address, value): c.access(address, value, False, False, 0) def test_supergame_switches_8000_and_fixes_the_last_bank(): c = cart("supergame") assert rd(c, 0xC001) == 7 and rd(c, 0x8001) == 0 wr(c, 0xBFFF, 3) # Planet Smashers' hot spot: any write to $8000-$BFFF assert rd(c, 0x8001) == 3 assert rd(c, 0x4000) is None # nothing connected def test_supergame_options_at_4000(): assert rd(cart("supergame", "fixed-bank"), 0x4001) == 6 ram = cart("supergame", "ram16") wr(ram, 0x7FFF, 0x5A) assert rd(ram, 0x7FFF) == 0x5A exrom = cart("supergame", "rom", banks(9)) assert rd(exrom, 0x4001) == 0 and rd(exrom, 0xC001) == 8 wr(exrom, 0x8000, 0) assert rd(exrom, 0x8001) == 1 # switchable banks are 1-8 (C9) def test_activision_layout(): c = cart("activision") wr(c, 0xFF83, 0) assert rd(c, 0xA000) == 0x83 and rd(c, 0xC000) == 0xC3 assert rd(c, 0x4000) == 0xC6 and rd(c, 0x6000) == 0x86 # bank 6: second, then first half assert rd(c, 0x8000) == 0xC7 and rd(c, 0xE000) == 0x87 # bank 7: second, then first half def test_absolute(): c = cart("absolute", rom=banks(4)) wr(c, 0x8000, 2) assert rd(c, 0x4001) == 1 wr(c, 0x8000, 1) assert rd(c, 0x4001) == 0 assert rd(c, 0x8001) == 2 and rd(c, 0xC001) == 3 def test_mirror_ram_blocks_repeat_twice(): c = cart("linear", "mirror-ram-2k", rom=banks(2)) wr(c, 0x4005, 0x11) assert rd(c, 0x4105) == 0x11 wr(c, 0x4205, 0x22) assert rd(c, 0x4305) == 0x22 and rd(c, 0x4005) == 0x11 def test_mirror_ram_is_2k_in_4000_4fff_as_the_dump_uses_it(): """Rescue on Fractalus (session 8): its code writes and reads back only the even pages $40-$4E (the first copy of each block) while MARIA reads all sixteen pages $40-$4F, so each odd page must show the even page's bytes (Stolberg's double-scanline purpose); it never touches $5000-$7FFF (900 frames with a press, and its code). Eight distinct blocks: the last at $4E00/$4F00.""" c = cart("linear", "mirror-ram-2k", rom=banks(2)) for block in range(8): wr(c, 0x4000 + block * 0x200 + 0x7F, 0x10 + block) # the even copy, as the program does for block in range(8): odd = 0x4100 + block * 0x200 + 0x7F assert c.access(odd, 0, True, True, 0) == 0x10 + block # a DMA read of the odd copy assert len({rd(c, 0x4000 + b * 0x200 + 0x7F) for b in range(8)}) == 8 # 2 KiB, no fold wr(c, 0x5000, 0x55) assert rd(c, 0x5000) is None and rd(c, 0x4000) == 0 # nothing at $5000-$7FFF assert rd(c, 0x7F7F) is None def test_activision_rows_are_confirmed_by_the_dumps_own_bytes(): payload = read_zip(LICENSED / "Double Dragon (USA).zip") row = table.lookup(payload) assert row is not None and row.source == "confirmed" and row.scheme == "activision" swapped = b"".join(payload[i + 0x2000 : i + 0x4000] + payload[i : i + 0x2000] for i in range(0, len(payload), 0x4000)) # fmt: skip softlist_row = table.lookup(swapped) assert softlist_row is not None and softlist_row.key == "ddragonu" def _scan_4000(): import importlib.util from pathlib import Path path = Path(__file__).resolve().parents[1] / "scripts" / "scan_4000.py" spec = importlib.util.spec_from_file_location("scan_4000", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_scan_4000_finds_a_call_from_the_fixed_bank_into_bank_6(): rom = bytearray(0x20000) bank7, bank6 = 0x1C000, 0x18000 rom[bank7 : bank7 + 4] = bytes([0x20, 0x10, 0x40, 0x60]) # $C000: JSR $4010 / RTS rom[bank7 + 0x3FFC : bank7 + 0x3FFE] = bytes([0x00, 0xC0]) # reset -> $C000 rom[bank6 + 0x10 : bank6 + 0x13] = bytes([0xA9, 0x01, 0x60]) # $4010: LDA #1 / RTS found = _scan_4000().scan(bytes(rom)) assert found["calls"] == [(0xC000, 0x4010)] and found["bank6_code"] == 2 assert found["bank6_undocumented"] == 0 rom[bank7 : bank7 + 4] = bytes([0x20, 0x10, 0xC0, 0x60]) # seeded: the call stays in bank 7 assert _scan_4000().scan(bytes(rom))["calls"] == [] def test_bank6_rows_are_confirmed_rows_with_bank_6_at_4000(): payload = read_zip(LICENSED / "Meltdown (USA).zip") row = table.lookup(payload) assert row is not None and row.source == "confirmed" and row.at_4000 == "fixed-bank" assert "own bytes" in row.citation and row.key == "meltdownu" def test_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. rom = banks(8) + bytes(b ^ 0x20 for b in banks(8)) c = build(CartDescription(payload=rom, scheme="supergame", at_4000="ram32-halt", bankset=True)) assert rd(c, 0x8001) == 0 and c.access(0x8001, 0, True, True, 0) == 0x20 wr(c, 0x8000, 3) assert rd(c, 0x8001) == 3 and c.access(0x8001, 0, True, True, 0) == 0x23 wr(c, 0x4005, 0x11) # SALLY's RAM wr(c, 0xC005, 0x22) # MARIA's RAM, written by SALLY assert rd(c, 0x4005) == 0x11 and c.access(0x4005, 0, True, True, 0) == 0x22 def test_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. desc = CartDescription(payload=banks(8), scheme="supergame", at_4000="pokey", pokey=(0x4000,)) c = build(desc) for address, value in ((0x400F, 0x03), (0x4010, 0x20), (0x4001, 0xAF)): # $4010: mirror wr(c, address, value) # SKCTL, AUDF1, AUDC1 (pure tone, volume 15) c.tick() for _ in range(500): c.tick() state = c.snapshot() fresh = build(desc) fresh.restore(state) for _ in range(2000): c.tick() fresh.tick() assert c.pokeys[0x4000].chip.levels() == fresh.pokeys[0x4000].chip.levels() assert rd(c, 0x400A) == rd(fresh, 0x400A) # RANDOM