# SPDX-License-Identifier: MIT """The cartridge port: an 8-bit address shift register, an 11-bit counter, two strobes. Every rule here is DOC: Spec §9 ("ROM Cart") and Appx 2 (``$FD87`` SYSCTL1, ``$FD8B`` IODAT, ``$FCB2/3`` RCART0/1). - The shift register takes CartAddressData (IODAT bit 1, as Mikey drives it) on a rising edge of CartAddressStrobe (SYSCTL1 bit 0), most significant bit first. - The counter is held at 0 while the strobe is high. - A read of RCART0 or RCART1 enables CART0/ or CART1/; its trailing edge advances the one shared counter. Address = page * block + (counter mod block). The port is the channel between Mikey (who drives the address lines) and Suzy (who strobes the chip enables and reads the data on her switch lines): both chips touch it, neither touches the other. """ from __future__ import annotations class CartPort: __slots__ = ( "audin", "banks", "blocks", "counter", "data", "eeprom", "shift", "strobe", "writes", ) # fmt: skip def __init__(self, bank0: bytes = b"", block0: int = 1024, bank1: bytes = b"", block1: int = 0): self.banks = (bytes(bank0), bytes(bank1)) self.blocks = (block0 or 1024, block1 or 1024) self.shift = 0 self.counter = 0 self.strobe = 0 self.data = 0 #: AUDIN driven as an output (IODAT bit 4) selects the upper half on AUDIN carts (M7). self.audin = 1 self.writes = 0 #: A serial EEPROM on the cart (lynx_python.cart.eeprom): CS = counter bit 7 (A7), #: CLK = counter bit 1 (A1), DI/DO = AUDIN (DOC: BLL eeprom.inc, cc65 eeprom.s). self.eeprom = None def _eeprom_lines(self) -> None: counter = self.counter self.eeprom.pins((counter >> 7) & 1, (counter >> 1) & 1, self.audin) def set_audin(self, level: int) -> None: """AUDIN as Mikey drives it (1, the pull-up, when it is an input).""" self.audin = level if self.eeprom is not None: self._eeprom_lines() def audin_pin(self) -> int: """What IODAT bit 4 reads with AUDIN as an input: the EEPROM's DO while it drives it, else the pull-up (HYPOTHESIS U17: 1).""" eeprom = self.eeprom if eeprom is not None and eeprom.cs: return eeprom.do return 1 def set_strobe(self, level: int) -> None: if level and not self.strobe: # DOC Spec §9: data accepted on the 0 -> 1 transition, MSB first. self.shift = ((self.shift << 1) | self.data) & 0xFF self.strobe = level if level: self.counter = 0 # DOC: the counter is reset whenever the strobe is high if self.eeprom is not None: self._eeprom_lines() def set_data(self, level: int) -> None: self.data = level def address(self, bank: int) -> int: block = self.blocks[bank] return self.shift * block + (self.counter & (block - 1)) def read(self, bank: int) -> int: data = self.banks[bank] address = self.address(bank) # HYPOTHESIS: an address past the image reads $FF (no chip answers; the data lines # are Suzy's switch lines behind resistors). Settle with a probe ROM on a console. value = data[address] if address < len(data) else 0xFF if not self.strobe: self.counter = (self.counter + 1) & 0x7FF # DOC: trailing edge advances it if self.eeprom is not None: self._eeprom_lines() return value def write(self, bank: int, value: int) -> None: # DOC Spec §9: writes use the same addressing; a ROM ignores them. Counted only. self.writes += 1 if not self.strobe: self.counter = (self.counter + 1) & 0x7FF if self.eeprom is not None: self._eeprom_lines()