# SPDX-License-Identifier: MIT """A cart's serial EEPROM (93C46 family), wired to the cart port, saved under the user's data directory (never beside a ROM). Wiring: DOC, the BLL kit's ``includes/eeprom.inc`` (Bastian Schick, Apache-2.0) and cc65's ``libsrc/lynx/eeprom.s`` (zlib; the same author's routine): "CS = A7 (18), CLK = A1 (11), DI/DO = AUDIN (32)". A1 and A7 are bits of the cart address counter (so a program clocks the chip by accessing RCART, and drops CS and CLK together by pulsing the address strobe, which clears the counter); DI and DO share the AUDIN pin, driven by Mikey when IODIR bit 4 makes it an output, read through IODAT bit 4 when it is an input. (This is a community convention, not an Atari document: U8.) Protocol: the 93Cxx serial EEPROM as its data sheets describe it and as those drivers drive it (DOC): with CS high, DI is taken on each rising CLK; a 1 is the start bit, then two opcode bits and the address (6 bits for a 93C46 in 16-bit words, one more in 8-bit organisation; 8 for 93C56/66, 10 for 93C76/86), then for WRITE the data, MSB first. READ: DO gives a dummy 0 after the last address bit, then the word MSB first on each following rising CLK (and the next word after it). EWEN/EWDS (opcode 00, address 11xxxx/00xxxx) set and clear the write enable, which is clear at power-on; ERASE (11), WRITE (01), ERAL (00 10xxxx) and WRAL (00 01xxxx) need it. A write or erase is done when CS falls after its last bit; then, with CS high again, DO reads 1 for ready. HYPOTHESIS: programming takes no time (the chips take milliseconds; the drivers poll DO until ready, so a program sees no difference unless it times the wait). DO floats (AUDIN reads its pull-up, 1) when the chip is not driving it. Erased cells read 1. The save file holds the chip's cells in order, 16-bit words high byte first (8-bit organisation: bytes in order). """ from __future__ import annotations import os from pathlib import Path #: Header EEPROM type -> (address bits in 16-bit organisation, size in bits) (cartridge.md #: byte 60: 1 = 93C46 ... 5 = 93C86). CHIPS = { "93C46": (6, 1024), "93C56": (8, 2048), "93C66": (8, 4096), "93C76": (10, 8192), "93C86": (10, 16384), } def save_dir() -> Path: """``$XDG_DATA_HOME/lynx-python/eeprom`` (default ``~/.local/share/...``).""" base = os.environ.get("XDG_DATA_HOME") or str(Path.home() / ".local" / "share") return Path(base) / "lynx-python" / "eeprom" def save_path(payload_sha1: str, chip: str) -> Path: return save_dir() / f"{payload_sha1}.{chip.lower()}.eep" class Eeprom: __slots__ = ( "address_bits", "bits", "cells", "clk", "count", "cs", "do", "driving", "enabled", "mode", "opcode", "out", "path", "pending", "reads", "word_bits", "writes", ) # fmt: skip def __init__(self, chip: str = "93C46", eight_bit: bool = False, path: Path | None = None): address_bits, size = CHIPS[chip] self.word_bits = 8 if eight_bit else 16 self.address_bits = address_bits + (1 if eight_bit else 0) self.cells = [(1 << self.word_bits) - 1] * (size // self.word_bits) # erased = 1s self.path = path if path is not None and path.exists(): self.load(path.read_bytes()) self.cs = self.clk = 0 self.enabled = False # DOC: write enable is clear at power-on self.mode = "idle" self.bits = self.count = self.opcode = 0 self.out = 0 self.do = 1 self.driving = False self.pending = None # a write or erase waiting for CS to fall self.writes = 0 # completed writes and erases (for tools and tests) self.reads = 0 # READ commands received # -- the save file --------------------------------------------------------------------- def image(self) -> bytes: if self.word_bits == 8: return bytes(self.cells) return b"".join(w.to_bytes(2, "big") for w in self.cells) def load(self, data: bytes) -> None: if self.word_bits == 8: for i, b in enumerate(data[: len(self.cells)]): self.cells[i] = b else: for i in range(min(len(self.cells), len(data) // 2)): self.cells[i] = int.from_bytes(data[2 * i : 2 * i + 2], "big") def _save(self) -> None: if self.path is not None: self.path.parent.mkdir(parents=True, exist_ok=True) self.path.write_bytes(self.image()) # -- the pins ---------------------------------------------------------------------------- def pins(self, cs: int, clk: int, di: int) -> None: """New levels on CS, CLK and DI (called whenever the counter or AUDIN changes).""" if cs and not self.cs: self.mode = "idle" self.driving = False if self.pending is None: self.do = 1 # ready (HYPOTHESIS: programming takes no time) elif not cs and self.cs: if self.pending is not None: self._program() self.mode = "idle" self.driving = False self.do = 1 if cs and clk and not self.clk: self._rise(di) self.cs, self.clk = cs, clk def _rise(self, di: int) -> None: mode = self.mode if mode == "idle": if di: self.mode = "command" self.bits = self.count = 0 return if mode == "command": self.bits = (self.bits << 1) | di self.count += 1 if self.count == 2 + self.address_bits: self._command() return if mode == "write": self.out = (self.out << 1) | di self.count += 1 if self.count == self.word_bits: self.pending = ("write", self.opcode, self.out) self.mode = "done" return if mode == "read": self._shift_out() def _command(self) -> None: opcode = self.bits >> self.address_bits address = self.bits & ((1 << self.address_bits) - 1) top = address >> (self.address_bits - 2) self.opcode = address self.count = self.out = 0 if opcode == 0b10: # READ: a dummy 0, then the word self.reads += 1 self.mode = "read" self.out = address self.count = -1 self.driving = True self.do = 0 elif opcode == 0b01: self.mode = "write" elif opcode == 0b11: self.pending = ("erase", address, 0) self.mode = "done" elif top == 0b11: self.enabled = True # EWEN self.mode = "done" elif top == 0b00: self.enabled = False # EWDS self.mode = "done" elif top == 0b10: self.pending = ("eral", 0, 0) self.mode = "done" else: self.mode = "write" # WRAL: data follows self.opcode = -1 def _shift_out(self) -> None: """The next data bit on DO (rising CLK during READ).""" self.count += 1 if self.count == self.word_bits: # sequential read: the next word self.out = (self.out + 1) % len(self.cells) self.count = 0 word = self.cells[self.out] self.do = (word >> (self.word_bits - 1 - self.count)) & 1 def _program(self) -> None: kind, address, value = self.pending self.pending = None if not self.enabled: return full = (1 << self.word_bits) - 1 if kind == "write" and address >= 0: self.cells[address % len(self.cells)] = value elif kind == "write": # WRAL self.cells = [value] * len(self.cells) elif kind == "erase": self.cells[address % len(self.cells)] = full else: # ERAL self.cells = [full] * len(self.cells) self.writes += 1 self._save() def for_cart(description) -> Eeprom | None: """The cart's EEPROM from its description (``eeprom`` = (type, organisation bits)).""" if not description.eeprom: return None chip, organisation = description.eeprom return Eeprom(chip, organisation == 8, save_path(description.sha1, chip))