// 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: //! //! ``` //! use pokey::{rc::PotRC, POKEY}; //! //! let mut chip = POKEY::default(); //! let rc = PotRC::wire(&mut chip, None); //! rc.borrow_mut().target[0] = Some(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. //! //! A transcription of pokey-python's `rc.py` (at 6ed1ffa). Where Rust forced a difference: //! //! - The Python helper keeps a reference to the chip and reads `chip.clock` and //! `chip.dump_active` when it is called. A Rust closure wired into the chip cannot hold the //! chip, so `level` and `dumped` take the [`Look`] the chip hands its `pot_level` and //! `on_dump` callbacks (the same two values at the same moment), and the helper is shared //! between the two callbacks as an `Rc>`. [`PotRC::wire`] does the wiring //! Python's docstring shows (`chip.pot_level = rc.level`, `chip.on_dump = rc.dumped`). //! - Targets are `Option` cycles (Python: `int | None`); clock differences are `i64`. use std::cell::RefCell; use std::rc::Rc; use crate::chip::{Look, POKEY}; /// 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). pub const DRAIN_TAU: f64 = 23.0; /// B5.8: AHRM 5.9 "Overcharge": about 4.8 V against a 2.1 V threshold (HYPOTHESIS). pub const OVERCHARGE: f64 = 2.0; /// Eight pot-pin capacitors following one chip's dump transistors. #[derive(Clone, Debug, PartialEq)] #[allow(clippy::upper_case_acronyms)] pub struct PotRC { pub target: Vec>, pub charge: Vec, /// the chip clock each pin's charge was last brought up to pub when: Vec, } impl PotRC { pub fn new(chip: &POKEY, targets: Option>>) -> Self { let target = match targets { Some(t) if !t.is_empty() => t, _ => vec![None; 8], }; PotRC { target, charge: vec![0.0; 8], when: vec![chip.clock as i64; 8], } } /// `PotRC(chip, targets)`, then `chip.pot_level = rc.level` and `chip.on_dump = rc.dumped`. pub fn wire(chip: &mut POKEY, targets: Option>>) -> Rc> { let rc = Rc::new(RefCell::new(PotRC::new(chip, targets))); let level = Rc::clone(&rc); chip.pot_level = Box::new(move |n, look| level.borrow_mut().level(n, look)); let dumped = Rc::clone(&rc); chip.on_dump = Some(Box::new(move |on, look| { dumped.borrow_mut().dumped(on, look) })); rc } fn advance(&mut self, n: usize, dumping: bool, look: Look) { let now = look.clock as i64; let elapsed = now - self.when[n]; if elapsed <= 0 { return; } self.when[n] = now; let Some(target) = self.target[n] else { return; }; if dumping { self.charge[n] *= (-(elapsed as f64) / DRAIN_TAU).exp(); } else { let a = self.charge[n] + elapsed as f64; let b = OVERCHARGE * target as f64; self.charge[n] = if b < a { b } else { a }; // Python's min(a, b) } } /// `chip.pot_level`: true once pin n's capacitor is above threshold. pub fn level(&mut self, n: usize, look: Look) -> bool { self.advance(n, look.dump_active, look); match self.target[n] { Some(target) => self.charge[n] >= target as f64, None => false, } } /// Wire as `chip.on_dump`: settles the charge up to a dump change (made under `!level`). pub fn dumped(&mut self, level: bool, look: Look) { for n in 0..8 { self.advance(n, !level, look); } } }