# SPDX-License-Identifier: MIT """Milestone 5's schemes, rule by rule, as their documents state them (cartridge/*.py).""" from __future__ import annotations from a2600_python.cartridge.dpc import DPC from a2600_python.cartridge.licensed import ( E0, UA, ActivisionFE, CommaVid, MegaboyF0, MNetworkE7, ThirtyTwoInOne, Tigervision3F, ) from a2600_python.cartridge.supercharger import Supercharger, parse_load def tagged(size: int, unit: int) -> bytes: """Every ``unit``-byte page filled with its own index.""" return bytes(i // unit & 0xFF for i in range(size)) def test_e0_slices_and_fixed_last(): cart = E0(tagged(0x2000, 0x400)) assert [cart.access(a, 0, 0) for a in (0x1000, 0x1400, 0x1800, 0x1C00)] == [4, 5, 6, 7] cart.access(0x1FE1, 0, 0) # slice 1 into $1000 cart.access(0x1FEA, 0, 0) # slice 2 into $1400 cart.access(0x1FF3, 0, 0) # slice 3 into $1800 assert [cart.access(a, 0, 0) for a in (0x1000, 0x1400, 0x1800, 0x1C00)] == [1, 2, 3, 7] def test_3f_latches_on_writes_below_40_only(): cart = Tigervision3F(tagged(0x2000, 0x800)) assert cart.access(0x1000, 0, 0) == 3 and cart.access(0x1800, 0, 0) == 3 cart.access(0x003F, 0x01, 0, True) assert cart.access(0x1000, 0, 0) == 1 and cart.access(0x1FFF, 0, 0) == 3 cart.access(0x003F, 0x02, 0, False) # a read with $02 floating: no latch cart.access(0x0040, 0x02, 0, True) # a TIA mirror above $3F: no latch assert cart.access(0x1000, 0, 0) == 1 def test_fe_takes_d5_of_the_byte_after_01fe(): cart = ActivisionFE(tagged(0x2000, 0x1000)) assert cart.access(0x1000, 0, 0) == 0 cart.access(0x01FE, 0x34, 0, True) # a JSR's PCL push cart.access(0x1001, 0, 1) cart.snoop(0xD0) # the new PC high byte, $Dxxx: bank 1 assert cart.access(0x1000, 0, 2) == 1 cart.access(0x01FE, 0, 3) # an RTS's PCL pull cart.access(0x01FF, 0, 4) cart.snoop(0xF2) # PCH pulled from RIOT RAM, $Fxxx: bank 0 assert cart.access(0x1000, 0, 5) == 0 cart.access(0x01FF, 0, 6) cart.snoop(0xD0) # not after $01FE: nothing assert cart.access(0x1000, 0, 7) == 0 def test_e7_rom_pages_ram_windows_and_fixed_area(): cart = MNetworkE7(tagged(0x4000, 0x800)) cart.access(0x1FE3, 0, 0) assert cart.access(0x1000, 0, 0) == 3 assert cart.access(0x1A00, 0, 0) == 7 cart.access(0x1FE7, 0, 0) # 1K of RAM at $1000 cart.access(0x1010, 0x5A, 0, True) assert cart.access(0x1410, 0, 0) == 0x5A cart.access(0x1FE9, 0, 0) # RAM block 1 at $1800 cart.access(0x1820, 0xA5, 0, True) assert cart.access(0x1920, 0, 0) == 0xA5 cart.access(0x1FE8, 0, 0) assert cart.access(0x1920, 0, 0) == 0x00 def test_e7_12k_maps_pages_0_1_twice(): cart = MNetworkE7(tagged(0x3000, 0x800)) pages = [] for page in range(7): cart.access(0x1FE0 + page, 0, 0) pages.append(cart.access(0x1000, 0, 0)) assert pages == [0, 1, 0, 1, 2, 3, 4] assert cart.access(0x1A00, 0, 0) == 5 def test_commavid_ram_read_low_write_high(): cart = CommaVid(bytes(range(256)) * 8) cart.access(0x1400, 0x42, 0, True) assert cart.access(0x1000, 0, 0) == 0x42 assert cart.access(0x1801, 0, 0) == 1 def test_ua_banks_from_0220_and_0240(): cart = UA(tagged(0x2000, 0x1000)) cart.access(0x0240, 0, 0) assert cart.access(0x1000, 0, 0) == 1 cart.access(0x0220, 0, 0) assert cart.access(0x1000, 0, 0) == 0 def test_f0_steps_on_each_1ff0_access(): cart = MegaboyF0(tagged(0x10000, 0x1000)) for expected in (1, 2, 3): cart.access(0x1FF0, 0, 0) assert cart.access(0x1000, 0, 0) == expected def test_32in1_next_game_on_each_reset(): cart = ThirtyTwoInOne(tagged(0x10000, 0x800)) assert cart.access(0x1000, 0, 0) == 0 and cart.access(0x1800, 0, 0) == 0 cart.reset() assert cart.access(0x1FFF, 0, 0) == 1 def dpc_image() -> bytes: graphics = bytes(range(256)) * 8 return bytes(0x2000) + graphics + bytes(255) def test_dpc_random_number_generator_has_255_states(): cart = DPC(dpc_image()) cart.access(0x1070, 0, 0, True) # reset to 0 values = [cart.access(0x1000, 0, 0) for _ in range(256)] # Horton's taps worked by hand from 0: the inverse of bits 7, 5, 4, 3 shifted in. assert values[:6] == [0x01, 0x03, 0x07, 0x0F, 0x1E, 0x3D] assert values[255] == values[0] assert len(set(values[:255])) == 255 and 0xFF not in values def test_dpc_fetcher_reads_decrement_and_mask_by_the_flag(): cart = DPC(dpc_image()) cart.access(0x1050, 0x10, 0, True) # fetcher 0 pointer low cart.access(0x1058, 0x00, 0, True) # pointer high cart.access(0x1048, 0x0E, 0, True) # end count cart.access(0x1040, 0x10, 0, True) # start count = pointer low: the compare sets the flag first = cart.access(0x1010, 0, 0) # masked read, flag set assert first == (~0x010 & 0x7FF) & 0xFF assert cart.access(0x1038, 0, 0) == 0xFF # flag cart.access(0x1008, 0, 0) # pointer now $00E: the end count clears the flag assert cart.access(0x1038, 0, 0) == 0x00 assert cart.access(0x1010, 0, 0) == 0 # masked def test_dpc_program_banks_switch_as_f8(): image = bytearray(dpc_image()) image[0x0100], image[0x1100] = 0xAA, 0xBB cart = DPC(bytes(image)) assert cart.access(0x1100, 0, 0) == 0xBB cart.access(0x1FF8, 0, 0) assert cart.access(0x1100, 0, 0) == 0xAA def supercharger_image(start=0xF800, control=0x0E) -> bytes: pages = bytearray(0x2000) header = bytearray(0x100) header[0:8] = bytes((start & 0xFF, start >> 8, control, 1, 0, 0, 0x0C, 0x01)) header[4] = (0x55 - sum(header[0:4]) - sum(header[5:8])) & 0xFF header[0x10] = 0x00 # RAM bank 1, block 0 header[0x40] = (0x55 - sum(pages[0:256])) & 0xFF return bytes(pages + header) def test_supercharger_header_and_pages_check(): info, pages = parse_load(supercharger_image()) assert info["start"] == 0xF800 and info["pages"] == 1 and pages[0][0] == 0 def test_supercharger_writes_on_the_fifth_address_transition(): """Horton's example: LDA $F000,X (X = $66), then LDA ($8B),Y to $1100.""" cart = Supercharger(supercharger_image()) cart.set_control(0x0E) # mode 3: RAM bank 1 at $1000, bank 3 at $1800; writes on bus = [0x1800, 0x1801, 0x1802, 0x1066, 0x1803, 0x1804, 0x008B, 0x008C, 0x1100] driven = [cart.access(address, 0, t) for t, address in enumerate(bus)] assert driven[-1] == 0x66 and cart.ram[0x100] == 0x66 def test_supercharger_counts_transitions_not_cycles(): """Horton's second example: a NOP's repeated fetch is one transition.""" cart = Supercharger(supercharger_image()) cart.set_control(0x0E) bus = [0x1FC9, 0x1FCA, 0x1FCB, 0x1008, 0x1FCC, 0x1FCD, 0x1FCD, 0x1FCE, 0x1FCF, 0x17E3] for t, address in enumerate(bus): cart.access(address, 0, t) assert cart.ram[0x7E3] == 0x08 # bank 1's $7E3 def test_supercharger_control_register_through_the_latch(): cart = Supercharger(supercharger_image()) cart.access(0x1016, 0, 0) # read $F016 cart.access(0x1FF8, 0, 1) assert cart.control == 0x16 and (cart.lower, cart.upper) == (1, 3)