"""M5: controllers through the BIOS's own routines (OS-7 listing REV 7PRIME, reprinted in the Coding Guide: CONTROLLER_SCAN $1F76, DECODER $1F79, UPDATE_SPINNER $1F88), and the spinner circuit's /INT pulse and bits 4, 5, 7. Test cartridges are assembled here: a `55 AA` header (the BIOS jumps straight through $800A) and the IRQ vector at $801E (the BIOS's $0038 jumps there). """ from __future__ import annotations import pytest from colecovision_python import Board, BoardOptions, HandController, PlainCartridge from colecovision_python.controllers import ( DrivingModule, RollerController, Spinner, SuperActionController, ) START = 0x8030 def cart(program: bytes, irq: bytes = b"\xfb\xed\x4d") -> PlainCartridge: """`program` at $8030; the IRQ handler (default EI; RETI) at $8100.""" image = bytearray(b"\x00" * 0x4000) image[0:2] = b"\x55\xaa" image[0x0A:0x0C] = START.to_bytes(2, "little") image[0x1E:0x21] = b"\xc3\x00\x81" # IRQ_INT_VECT: JP $8100 image[0x21:0x24] = b"\xc3\x00\x82" # NMI_INT_VECT: JP $8200 image[0x200] = 0xED # $8200: RETN image[0x201] = 0x45 image[START - 0x8000 : START - 0x8000 + len(program)] = program image[0x100 : 0x100 + len(irq)] = irq return PlainCartridge(bytes(image)) def run_until_halt(board, limit=200_000): for _ in range(limit): board.step() if board.cpu.halted: return raise AssertionError("no HALT") # LD SP,$73B9; LD HL,nn; CALL DECODER; HALT def decoder(controller, segment): return bytes((0x31, 0xB9, 0x73, 0x21, segment, controller, 0xCD, 0x79, 0x1F, 0x76)) @pytest.mark.parametrize( ("key", "decoded"), [(str(n), n) for n in range(10)] + [("*", 0x0A), ("#", 0x0B)], ) def test_bios_decoder_keypad(bios, key, decoded): pad = HandController() pad.keys = {key} pad.right_fire = True board = Board(bios, cart(decoder(0, 1)), port1=pad) run_until_halt(board) assert board.cpu.l == decoded assert board.cpu.h == 0x40 # "Arm" (the right button), complemented by CONT_READ def test_bios_decoder_no_key_and_port_2(bios): pad2 = HandController() pad2.keys = {"7"} board = Board(bios, cart(decoder(1, 1)), port1=HandController(), port2=pad2) run_until_halt(board) assert (board.cpu.l, board.cpu.h) == (7, 0) board = Board(bios, cart(decoder(0, 1)), port1=HandController()) run_until_halt(board) assert board.cpu.l == 0x0F # no key def test_bios_decoder_joystick_and_fire(bios): pad = HandController() pad.directions = {"up", "left"} pad.left_fire = True # OUT ($C0),A first: DECODER reads whichever segment is selected, and a `55 AA` # cartridge is entered before the BIOS's own $C0 write. board = Board(bios, cart(b"\xd3\xc0" + decoder(0, 0)), port1=pad) run_until_halt(board) assert board.cpu.l == 0x09 # complemented: bit 0 up, bit 3 left assert board.cpu.h == 0x40 def test_super_action_extra_buttons_read_as_codes_4_and_8(): sac = SuperActionController() board = Board(b"\xff" * 8192, port1=sac) board.write_port(0x80, 0) sac.keys = {"purple"} assert board.read_port(0xFC) & 0x0F == 0x08 sac.keys = {"blue"} assert board.read_port(0xFC) & 0x0F == 0x04 # The spinner: IM 1; EI; loop forever. The handler calls UPDATE_SPINNER then EI; RETI. SPIN_PROGRAM = bytes((0x31, 0xB9, 0x73, 0xED, 0x56, 0xFB, 0x18, 0xFE)) SPIN_HANDLER = bytes((0xF5, 0xE5, 0xCD, 0x88, 0x1F, 0xE1, 0xF1, 0xFB, 0xED, 0x4D)) SPIN_SW0_CT, SPIN_SW1_CT = 0x73EB, 0x73EC # [OS7 p. 141] @pytest.mark.parametrize(("rate", "sign"), [(200.0, 1), (-200.0, -1)]) def test_spinner_counts_through_the_bios(bios, rate, sign): dev = SuperActionController() board = Board(bios, cart(SPIN_PROGRAM, SPIN_HANDLER), port1=dev) dev.spinner.rate = rate while board.clock < 3_579_545 // 4: # a quarter of a second: 50 edges board.step() count = board.read_byte(SPIN_SW0_CT) signed = count - 256 if count > 127 else count assert signed * sign >= 45, count assert board.read_byte(SPIN_SW1_CT) == 0 def test_roller_controller_drives_both_ports(bios): roller = RollerController() board = Board(bios, cart(SPIN_PROGRAM, SPIN_HANDLER), port1=roller.ports[0], port2=roller.ports[1]) roller.x, roller.y = 100.0, -100.0 while board.clock < 3_579_545 // 4: board.step() x, y = board.read_byte(SPIN_SW0_CT), board.read_byte(SPIN_SW1_CT) assert 20 <= x <= 30 and 226 <= y <= 236, (x, y) def test_driving_module_pedal_is_the_joystick_fire(): wheel = DrivingModule() board = Board(b"\xff" * 8192, port1=wheel) board.write_port(0xC0, 0) wheel.pedal = True assert not board.read_port(0xFC) & 0x40 def test_spinner_pulse_one_shot_and_bits(): spin = Spinner() spin.rate = 1000.0 edges = spin.spinner_edges(3_579_545 // 100) # 10 ms: edges at 1..9 periods (10 is past it) assert len(edges) == 9 and all(right for _, right in edges) dev = SuperActionController() board = Board(b"\xff" * 8192, port1=dev, options=BoardOptions()) dev.spinner.rate = 1000.0 period = 3_579_545 // 1000 board.clock = period + 5 # just after the first edge board._update_int(board.clock) assert board.cpu.maskable_interrupt_pending # the /INT pulse (U15: 39 clocks) value = board.read_port(0xFC) assert value & 0x10 == 0 # bit 4: the one-shot is running assert value & 0x80 # bit 7: the NAND output held high by it (C12) assert value & 0x20 # bit 5: pin 7 high, turning right board.clock = period + 100 board._update_int(board.clock) assert not board.cpu.maskable_interrupt_pending # the pulse is over board.clock = period + 2000 assert board.read_port(0xFC) & 0x90 == 0x10 # one-shot over (U15: 1,790 clocks)