# SPDX-License-Identifier: MIT """Optional board-side helper: the capacitor on each pot pin (B5.8, decision 4). The chip never imports this module. A board that has no capacitor model of its own wires it as:: rc = PotRC(chip) chip.pot_level = rc.level chip.on_dump = rc.dumped rc.target[0] = 114 * 100 # paddle 0: cycles of charging to threshold Each pin's charge is kept in "cycles of charging": it grows while the dump transistors are off (``chip.dump_active`` False) and drains exponentially while they are on, with a time constant of ``DRAIN_TAU`` cycles (FITTED to AHRM 5.9 "Discharge time"). A pin is above threshold once its charge reaches its target. A target of ``None`` is an unconnected pin that never crosses. """ from __future__ import annotations import math #: B5.8: AHRM 5.9 "Discharge time": 4.6 V to 0.2 V in 40 us = ln(23) time constants in #: 72 cycles at 1.79 MHz; FITTED, charge taken as proportional to voltage (HYPOTHESIS). DRAIN_TAU = 23.0 #: B5.8: AHRM 5.9 "Overcharge": about 4.8 V against a 2.1 V threshold (HYPOTHESIS). OVERCHARGE = 2.0 class PotRC: """Eight pot-pin capacitors following one chip's dump transistors.""" def __init__(self, chip, targets: list[int | None] | None = None) -> None: self.chip = chip self.target: list[int | None] = list(targets) if targets else [None] * 8 self.charge = [0.0] * 8 self.when = [chip.clock] * 8 # the chip clock each pin's charge was last brought up to def _advance(self, n: int, dumping: bool) -> None: now = self.chip.clock elapsed = now - self.when[n] if elapsed <= 0: return self.when[n] = now target = self.target[n] if target is None: return if dumping: self.charge[n] *= math.exp(-elapsed / DRAIN_TAU) else: self.charge[n] = min(self.charge[n] + elapsed, OVERCHARGE * target) def level(self, n: int) -> bool: """``chip.pot_level``: True once pin n's capacitor is above threshold.""" self._advance(n, self.chip.dump_active) target = self.target[n] return target is not None and self.charge[n] >= target def dumped(self, level: bool) -> None: """Wire as ``chip.on_dump``: settles the charge up to a dump change (made under ``not level``).""" for n in range(8): self._advance(n, not level)