# SPDX-License-Identifier: MIT """Quick tests of the board pieces that need no ROM (milestones 1 and 2).""" from __future__ import annotations from lynx_python.board import Lynx from lynx_python.cart.port import CartPort from lynx_python.machine import LYNX1, LYNX2 def test_hot_objects_stay_under_pypys_attribute_limit(): # The family's PyPy rule: an instance with 80+ attributes devolves to a dictionary. board = Lynx() for obj in (board, board.mikey, board.suzy, board.port, board.mikey.timers, board.suzy.math, board.mikey.timers.timers[0], board.suzy.engine, board.mikey.display): # fmt: skip assert len(obj.__slots__) < 80, type(obj).__name__ def test_mapctl_readback_rows(): # TEST ROM (LT page-mode): Lynx II $03 -> $F3, $83 -> $73; Lynx I returns what was written. assert LYNX2.mapctl_readback(0x03) == 0xF3 assert LYNX2.mapctl_readback(0x83) == 0x73 assert LYNX1.mapctl_readback(0x83) == 0x83 def test_cart_port_shift_and_counter(): data = bytes(range(256)) * 1024 # 256 KiB, 1024-byte pages port = CartPort(data, 1024) port.set_data(1) port.set_strobe(1) port.set_strobe(0) port.set_data(0) for _ in range(7): port.set_strobe(1) port.set_strobe(0) assert port.shift == 0x80 # MSB first assert [port.read(0) for _ in range(3)] == [data[0x80 * 1024 + i] for i in range(3)] port.set_strobe(1) # the strobe high resets the counter assert port.counter == 0 def test_timer_borrows_on_the_n_plus_first_clock(): board = Lynx() timers = board.mikey.timers t6 = timers.timers[6] timers.write(6, 2, 2, 0) # CNT = 2 timers.write(6, 1, 0x88, 0) # interrupt enable, count, 1 µs, no reload timers.run_until(16 * 2) assert t6.count == 0 and not timers.pending timers.run_until(16 * 3) assert t6.done and timers.pending == 0x40 def test_ctlb_write_injects_one_clock(): # TEST ROM (LT timers "CTLB RD/WR"): CNT=$80, write $0A -> CTLB=$08, CNT=$7F. board = Lynx() timers = board.mikey.timers timers.write(0, 2, 0x80, 0) timers.write(0, 1, 0x18, 0) timers.write(0, 3, 0x0A, 0) # Read in the low half of the 1 us source clock (B2 is the clock's level, Timer.ctlb). assert timers.read(0, 3, 8) & 0x0F == 0x08 and timers.read(0, 2, 8) == 0x7F assert timers.read(0, 3, 16) & 0x0F == 0x0C # just after a counting edge: B2 = 1 def test_every_mikey_and_suzy_address_reads_without_error(): # The board never crashes on a bus read or write (sweep 2026-11-19: Toki, Bill & Ted, # Geoduel read $FDC0-$FDFF). DOC Appx 2: $FDC0-$FDFF not yet allocated; HYPOTHESIS # (U18): reads 0, writes ignored, the palette not mirrored there. board = Lynx() mikey = board.mikey for reg in range(0x20): mikey.write(0xFDA0 + reg, 0x5A) for reg in range(0xC0, 0x100): mikey.write(0xFD00 + reg, 0xFF) assert mikey.read(0xFD00 + reg) == 0 assert mikey.palette == bytearray([0x0A] * 16 + [0x5A] * 16) for reg in range(0x100): mikey.read(0xFD00 + reg) board.suzy.read(0xFC00 + reg, 0)