# SPDX-License-Identifier: MIT """Suzy: the sprite engine's registers, the math unit, joystick and switches, the cart data port. Imports nothing from the board. Register map: DOC Appx 2 ("Suzy Addresses") unless tagged. """ from __future__ import annotations from lynx_python.cart.port import CartPort from lynx_python.suzy.engine import SpriteEngine from lynx_python.suzy.math import MathUnit class Suzy: __slots__ = ( "busen", "cart", "engine", "joystick", "leds", "math", "regs", "sprcoll", "sprctl0", "sprctl1", "sprgo", "sprinit", "sprite_busy", "sprsys", "stop_request", "switches", "unsafe", ) # fmt: skip def __init__(self, cart: CartPort) -> None: # $FC00-$FC7F: the SCB registers (24 words $FC00-$FC2F) and the math registers. self.regs = bytearray(0x40) # DOC reset x: HYPOTHESIS 0 self.cart = cart self.math = MathUnit(self.regs) self.sprctl0 = 0 self.sprctl1 = 0 self.sprcoll = 0 self.sprinit = 0 self.busen = 0 # DOC SUZYBUSEN reset 0 self.sprgo = 0 # DOC SPRGO reset 0 self.sprsys = 0 # the written control bits self.sprite_busy = 0 # SPRSYS read B0 self.stop_request = 0 self.unsafe = 0 self.joystick = 0 # buttons pressed = 1 (Appx 2 reads them as bits) self.switches = 0 self.leds = 0 self.engine = SpriteEngine(self) def run_engine(self, bus) -> bool: """Run the sprite engine a step on ``bus``; True when the list is done.""" return self.engine.run(bus) def word(self, offset: int) -> int: return self.regs[offset] | (self.regs[offset + 1] << 8) def set_word(self, offset: int, value: int) -> None: self.regs[offset] = value & 0xFF self.regs[offset + 1] = (value >> 8) & 0xFF # -- registers --------------------------------------------------------------------- def read(self, address: int, now: int) -> int: reg = address & 0xFF if reg < 0x80: if 0x52 <= reg: # DOC §16 "unsafe access detection is broken for math" + TEST ROM math 1-7 # (SPRSYS reads B2 set after math accesses: "unsafe is always set"). # HYPOTHESIS: every CPU access to a math register sets it. self.unsafe = 1 # TEST ROM memio 3: $FC40-$FC7F mirrors $FC00-$FC3F (one 64-byte file). return self.regs[reg & 0x3F] if reg == 0x88: return 0x01 # DOC SUZYHREV = '01' if reg == 0x92: return self.read_sprsys(now) if reg == 0xB0: return self.read_joystick() if reg == 0xB1: return self.switches & 0x07 if reg == 0xB2: return self.cart.read(0) # DOC: RCART0 uses CART0/ as the strobe if reg == 0xB3: return self.cart.read(1) # HYPOTHESIS (U18): write-only and unallocated addresses read 0. return 0 def read_joystick(self) -> int: value = self.joystick if not self.sprsys & 0x08: # DOC Appx 2 JOYSTICK, two columns: with Lefthand = 0 the four direction bits # swap Up<->Down and Left<->Right (C11; U12 for a hardware result). value = ( (value & 0x0F) | ((value & 0x80) >> 1) | ((value & 0x40) << 1) | ((value & 0x20) >> 1) | ((value & 0x10) << 1) ) # fmt: skip return value def read_sprsys(self, now: int) -> int: # DOC Appx 2 SPRSYS (read): B7 math in process, B6 mathbit, B5 last carry, # B4 Vstretch, B3 Lefthand, B2 unsafe access, B1 stop request, B0 sprite busy. return ( (self.math.working(now) << 7) | (self.math.mathbit << 6) | (self.math.last_carry << 5) | (self.sprsys & 0x18) | (self.unsafe << 2) | (self.stop_request << 1) | self.sprite_busy ) def write(self, address: int, value: int, now: int) -> None: reg = address & 0xFF if reg < 0x80: index = reg & 0x3F # TEST ROM memio 3: one 64-byte file, mirrored at $FC40 self.regs[index] = value if not index & 1: # DOC: any CPU write to an LSB sets the MSB to 0 (and the math rule "B, D, # F, H, K, M force 0 into A, C, E, G, J, L" is the same rule). self.regs[index + 1] = 0 if reg >= 0x52: self.unsafe = 1 # as the read above (DOC §16 + TEST ROM math 1-7) self.math.written(index, now) return if reg == 0x80: self.sprctl0 = value elif reg == 0x81: self.sprctl1 = value elif reg == 0x82: self.sprcoll = value elif reg == 0x83: self.sprinit = value elif reg == 0x90: self.busen = value & 1 elif reg == 0x91: self.sprgo = value # DOC Appx 2: setting or clearing SPRGO clears the stop request. self.stop_request = 0 if value & 1: # DOC (start-here "Suzy takes the bus" step 3): busy is set at once. self.sprite_busy = 1 self.engine.start() else: # DOC Spec §6: "forced off by writing a 00 to SPRGO". self.engine.steps = None self.sprite_busy = 0 elif reg == 0x92: self.sprsys = value self.math.signed = (value >> 7) & 1 self.math.accumulate = (value >> 6) & 1 if value & 0x04: self.unsafe = 0 if value & 0x02: self.stop_request = 1 elif reg == 0xB2: self.cart.write(0, value) elif reg == 0xB3: self.cart.write(1, value) elif reg == 0xC0: self.leds = value