"""The banked cartridge boards, on synthetic images (no game bytes), against the rules in ``cartridge.py``'s source list ([TM], [CVB], [RC2014], [AA-ACT], [AA-SAVE], [24C02], [24C256]). Written with the clean-room rewrite of 2026-11-26 (docs/provenance.md).""" from __future__ import annotations import pytest from colecovision_python import Board, mame_options from colecovision_python.cartridge import ( ActivisionCartridge, MegaCart, PlainCartridge, SerialEEPROM, cartridge_for, ) BANK = 0x4000 def banked(size: int) -> bytearray: """An image whose every 16K bank is filled with its own number.""" return bytearray(b"".join(bytes((n & 0xFF,)) * BANK for n in range(size // BANK))) # MegaCart ------------------------------------------------------------------------------ @pytest.mark.parametrize("size", [0x10000, 0x20000, 0x40000, 0x80000, 0x100000]) def test_megacart_windows_and_hotspots(size: int) -> None: banks = size // BANK cart = MegaCart(bytes(banked(size))) assert cart.read(0x8000) == banks - 1 and cart.read(0xBFFF) == banks - 1 # last bank fixed [CVB] assert cart.read(0xC000) == 0 # bank 0 at power-on [RC2014] for n in range(banks): cart.read(0xFFC0 | n) # [CVB] $FFC0 | n assert cart.bank == n and cart.read(0xC000) == n and cart.read(0x8000) == banks - 1 cart.read(0xFFFF) assert cart.bank == (0x3F & (banks - 1)) # the address masked to the size [CVB] cart.read(0xFFC0) # the start-up read of every size [CVB] assert cart.bank == 0 def test_megacart_hotspot_read_returns_the_new_banks_byte() -> None: cart = MegaCart(bytes(banked(0x20000))) assert cart.read(0xFFC5) == 5 # HYPOTHESIS: the latch takes the address first def test_megacart_writes_are_accesses_unless_asked_otherwise() -> None: cart = MegaCart(bytes(banked(0x20000))) cart.write(0xFFC3, 0) # the slot has no /WR [TM] assert cart.read(0xC000) == 3 quiet = MegaCart(bytes(banked(0x20000)), switch_on_write=False) quiet.write(0xFFC3, 0) assert quiet.read(0xC000) == 0 cart.write(0xFFBF, 0) # below the hotspots: nothing assert cart.bank == 3 def test_megacart_peek_has_no_side_effect() -> None: cart = MegaCart(bytes(banked(0x20000))) assert cart.peek(0xFFC6) == 0 and cart.bank == 0 def test_megacart_size_not_a_power_of_two_reads_open_bus_past_the_end() -> None: cart = MegaCart(bytes(banked(0xC000))) # 3 banks: mask 3 (HYPOTHESIS) assert cart.read(0x8000) == 2 cart.read(0xFFC3) assert cart.read(0xC000) == 0xFF cart.read(0xFFC1) assert cart.read(0xC000) == 1 def test_a_64k_megacart_reads_as_its_doubled_128k_image() -> None: image = bytes(banked(0x10000)) small, doubled = MegaCart(image), MegaCart(image + image) # [CVA] for n in range(8): small.read(0xFFC0 | n) doubled.read(0xFFC0 | n) assert small.read(0xC123) == doubled.read(0xC123) assert small.read(0x8000) == doubled.read(0x8000) def test_a_megacart_program_switches_banks_on_the_board() -> None: """A program in the fixed bank reads a hotspot, then writes one (an access too).""" code = bytes.fromhex( "3A C2 FF" # LD A,($FFC2) "3A 00 C0" # LD A,($C000) "32 00 70" # LD ($7000),A "32 C5 FF" # LD ($FFC5),A "3A 00 C0" # LD A,($C000) "32 01 70" # LD ($7001),A "76" # HALT ) image = banked(0x20000) image[0x1C000 : 0x1C000 + len(code)] = code # the last bank, seen at $8000 bios = bytearray(b"\xff" * 8192) bios[0:3] = b"\xc3\x00\x80" board = Board(bytes(bios), MegaCart(bytes(image)), options=mame_options()) for _ in range(40): board.step() assert board.ram[0] == 2 and board.ram[1] == 5 # Activision ---------------------------------------------------------------------------- def activision_image(size: int = 0x10000) -> bytearray: """Banked, with the SDA rows a game keeps: 0 in bank 2 and 1 in bank 3 at $FF80 [AA-ACT].""" image = banked(size) for bank, value in ((2, 0), (3, 1)): if bank * BANK < size: image[bank * BANK + 0x3F80 : bank * BANK + 0x3F90] = bytes((value,)) * 16 return image def test_activision_banks() -> None: cart = ActivisionCartridge(bytes(activision_image())) assert cart.read(0x8000) == 0 and cart.read(0xBFFF) == 0 # bank 0 fixed [AA-ACT] assert cart.read(0xC000) == 0 # HYPOTHESIS: power-on bank 0 for address, bank in ((0xFF90, 1), (0xFFA0, 2), (0xFFB0, 3)): cart.write(address, 0) # [AA-ACT] assert cart.read(0xC000) == bank and cart.read(0x8000) == 0 cart.read(0xFF90) # a read is an access too (decode_reads, [TM]) assert cart.read(0xC000) == 1 quiet = ActivisionCartridge(bytes(activision_image()), decode_reads=False) quiet.read(0xFFA0) assert quiet.read(0xC000) == 0 def test_activision_ff80_reads_bank_2_or_3_as_sda() -> None: image = activision_image() image[2 * BANK + 0x3F80] = 0x42 image[3 * BANK + 0x3F80] = 0x43 cart = ActivisionCartridge(bytes(image)) assert cart.read(0xFF80) == 0x43 # SDA idles high: bank 3 [AA-ACT] cart.write(0xFFE0, 0) # SDA low (SCL is high: a start, harmless) assert cart.read(0xFF80) == 0x42 and cart.peek(0xFF80) == 0x42 cart.write(0xFFF0, 0) assert cart.read(0xFF80) == 0x43 assert cart.bank == 0 # the SDA read selects nothing cart.write(0xFFA0, 0) # bank 2 in the window ... image_byte = image[3 * BANK + 0x3FFF] assert cart.read(0xFFFF) == image_byte # ... but $FF80-$FFFF follow SDA (HYPOTHESIS) class Master: """A two-wire master driving the Activision board's latch [AA-ACT] by the data sheets' sequences [24C02; 24C256].""" def __init__(self, cart: ActivisionCartridge) -> None: self.cart = cart def scl(self, level: int) -> None: self.cart.write(0xFFD0 if level else 0xFFC0, 0) def sda(self, level: int) -> None: self.cart.write(0xFFF0 if level else 0xFFE0, 0) def bit(self) -> int: return self.cart.read(0xFF80) def start(self) -> None: self.sda(1) self.scl(1) self.sda(0) self.scl(0) def stop(self) -> None: self.sda(0) self.scl(1) self.sda(1) def send(self, byte: int) -> bool: for i in range(7, -1, -1): self.sda((byte >> i) & 1) self.scl(1) self.scl(0) self.sda(1) self.scl(1) ack = self.bit() == 0 self.scl(0) return ack def receive(self, ack: bool) -> int: self.sda(1) value = 0 for _ in range(8): self.scl(1) value = (value << 1) | self.bit() self.scl(0) self.sda(0 if ack else 1) self.scl(1) self.scl(0) return value def address(self, eeprom: SerialEEPROM, address: int) -> bool: if eeprom.size > 0x100 and not self.send(address >> 8): return False return self.send(address & 0xFF) def write(self, eeprom: SerialEEPROM, address: int, data: bytes) -> bool: self.start() ok = self.send(0xA0) and self.address(eeprom, address) and all(self.send(b) for b in data) self.stop() return ok def read(self, eeprom: SerialEEPROM, address: int, count: int) -> bytes: self.start() assert self.send(0xA0) and self.address(eeprom, address) self.start() assert self.send(0xA1) out = bytes(self.receive(ack=i < count - 1) for i in range(count)) self.stop() return out @pytest.mark.parametrize("size", [0x100, 0x8000]) def test_eeprom_write_and_read_back(size: int) -> None: rom = SerialEEPROM(size) m = Master(ActivisionCartridge(bytes(activision_image()), rom)) where = size - 0x10 assert m.write(rom, where, b"SAVE") assert rom.memory[where : where + 4] == b"SAVE" assert m.read(rom, where, 4) == b"SAVE" assert rom.pointer == where + 4 # the counter after a read [24C256 s. 8.2] @pytest.mark.parametrize(("size", "page"), [(0x100, 8), (0x8000, 64)]) def test_eeprom_page_write_wraps_within_the_page(size: int, page: int) -> None: rom = SerialEEPROM(size) m = Master(ActivisionCartridge(bytes(activision_image()), rom)) start = 2 * page - 2 assert m.write(rom, start, b"ABCD") assert rom.memory[start : start + 2] == b"AB" assert rom.memory[page : page + 2] == b"CD" # wrapped to the page's start assert rom.memory[2 * page] == 0xFF @pytest.mark.parametrize("size", [0x100, 0x8000]) def test_eeprom_sequential_read_rolls_over(size: int) -> None: rom = SerialEEPROM(size, bytes(range(256)) * (size // 256)) m = Master(ActivisionCartridge(bytes(activision_image()), rom)) assert m.read(rom, size - 2, 4) == bytes((254, 255, 0, 1)) def test_eeprom_write_needs_a_stop() -> None: rom = SerialEEPROM(0x100) m = Master(ActivisionCartridge(bytes(activision_image()), rom)) m.start() assert m.send(0xA0) and m.send(0x10) and m.send(0x55) m.start() # a start instead of a stop: nothing written [24C256 s. 6] m.stop() assert rom.memory[0x10] == 0xFF def test_eeprom_control_bytes() -> None: small = SerialEEPROM(0x100) m = Master(ActivisionCartridge(bytes(activision_image()), small)) m.start() assert m.send(0xAE) # 24C02: the block bits are don't-care [24C02] m.stop() m.start() assert not m.send(0xB0) # not the 1010 code m.stop() big = SerialEEPROM(0x8000) m = Master(ActivisionCartridge(bytes(activision_image()), big)) m.start() assert not m.send(0xA2) # 24C256: A2-A0 must match the pins (tied low, HYPOTHESIS) m.stop() def test_eeprom_sizes() -> None: with pytest.raises(ValueError): SerialEEPROM(1024) # Choosing a board ---------------------------------------------------------------------- def test_board_choice_by_size_and_header() -> None: assert isinstance(cartridge_for(b"\x55\xaa" + bytes(0x3FFE)), PlainCartridge) mega = bytearray(0x20000) mega[0x1C000:0x1C002] = b"\xaa\x55" # header in the last 16K [CVB] assert isinstance(cartridge_for(bytes(mega)), MegaCart) act = bytearray(0x10000) act[0:2] = b"\x55\xaa" # header in the first 16K [AA-ACT] assert isinstance(cartridge_for(bytes(act)), ActivisionCartridge) both = bytearray(act) both[0xC000:0xC002] = b"\xaa\x55" assert isinstance(cartridge_for(bytes(both)), MegaCart) assert isinstance(cartridge_for(bytes(0x20000)), MegaCart) # no header: HYPOTHESIS assert isinstance(cartridge_for(bytes(0x100000)), MegaCart) assert isinstance(cartridge_for(bytes(0x10000), "activision"), ActivisionCartridge) assert isinstance(cartridge_for(bytes(act), "megacart"), MegaCart) with pytest.raises(ValueError): cartridge_for(bytes(0x100000), "xin1")