// SPDX-License-Identifier: MIT //! Rung 5: the pot scan (B5.x in docs/behaviour.md), from AHRM 5.9's numbers. //! //! Transcribed from pokey-python's `tests/test_pots.py` (at 6ed1ffa). The wiring closures //! take the chip's `Look` (Rust-forced, `src/chip.rs`); a Python lambda over a shared set is a //! closure over an `Rc>`. use std::cell::RefCell; use std::collections::BTreeSet; use std::rc::Rc; use pokey::chip::{ALLPOT, POTGO, SKCTL}; use pokey::rc::PotRC; use pokey::POKEY; /// AHRM 5.9 "Reading live counters" const LIVE: [u8; 17] = [ 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x04, 0x06, 0x00, 0x08, 0x08, 0x0A, 0x08, 0x0C, 0x0C, 0x0E, 0x00, ]; fn step(chip: &mut POKEY, register: Option, value: u8) { if let Some(register) = register { chip.write(register, value); } chip.tick(); } fn fast_chip() -> POKEY { let mut chip = POKEY::default(); step(&mut chip, Some(SKCTL), 0x07); // fast scan, out of init mode step(&mut chip, None, 0); chip } #[test] fn test_fast_scan_live_reads() { // B5.1, B5.2, B5.5: the 17 values read from the cycle after POTGO let mut chip = fast_chip(); step(&mut chip, Some(POTGO), 0); let mut seen = Vec::new(); for _ in 0..17 { seen.push(chip.read(0)); chip.tick(); } assert_eq!(seen, LIVE.to_vec()); } #[test] fn test_fast_scan_end() { // B5.4: AHRM 5.9 "Scan timing": 229 latched, ALLPOT $00 from 231 cycles after POTGO let mut chip = fast_chip(); step(&mut chip, Some(POTGO), 0); for _ in 0..229 { // cycles W+1 .. W+229 chip.tick(); } assert!(chip.read(ALLPOT) == 0xFF && chip.read(0) == 0xE4); // W+230: transitional 228 AND 229 chip.tick(); assert!(chip.read(ALLPOT) == 0x00 && chip.read(0) == 0xE5); // W+231 chip.run(300); assert!(chip.read(0) == 0xE5 && chip.read(ALLPOT) == 0); } #[test] fn test_slow_scan_first_increment_and_end() { // B5.2, B5.4, B5.5: AHRM 5.9 "Scan timing": first increment 81 cycles after the SKCTL write that // leaves init mode (transitional $00, then $01); 227 x 114 later $E0, then $E4, then ALLPOT $00 let mut chip = POKEY::default(); step(&mut chip, Some(POTGO), 0); chip.run(5); step(&mut chip, Some(SKCTL), 3); // T chip.run(80); // ticks T+1 .. T+80 assert_eq!(chip.read(0), 0x00); // T+81: 0 AND 1 chip.tick(); assert_eq!(chip.read(0), 0x01); // T+82 chip.run(113); assert_eq!(chip.read(0), 0x00); // T+81+114: 1 AND 2 chip.tick(); assert_eq!(chip.read(0), 0x02); chip.run(227 * 114 - 115); // to cycle I = T+81+227*114 assert!(chip.read(0) == 0xE0 && chip.read(ALLPOT) == 0xFF); chip.tick(); assert!(chip.read(0) == 0xE4 && chip.read(ALLPOT) == 0xFF); chip.tick(); assert!(chip.read(0) == 0xE4 && chip.read(ALLPOT) == 0x00); } #[test] fn test_crossings_latch_on_any_cycle_and_resume() { // B5.3: a pin above threshold holds its register; falling back below resumes updating let mut chip = fast_chip(); let above = Rc::new(RefCell::new(BTreeSet::new())); let pins = Rc::clone(&above); chip.pot_level = Box::new(move |n, _look| pins.borrow().contains(&n)); step(&mut chip, Some(POTGO), 0); chip.run(50); // counter 49 after tick W+50 above.borrow_mut().insert(3); chip.tick(); // tick W+51 steps to 50; pot 3 is above threshold and keeps 49 assert!(chip.read(ALLPOT) == 0xF7 && chip.read(3) == 49); chip.run(20); assert_eq!(chip.read(3), 49); above.borrow_mut().clear(); chip.tick(); assert!( chip.read(ALLPOT) == 0xFF && chip.pot[3] as u32 == chip.pot_count && chip.pot_count == 71 ); } #[test] fn test_pins_already_above_threshold_never_update() { // AHRM 5.9 "Truncated scans": inputs over threshold at the restart are not updated at all let mut chip = fast_chip(); chip.pot[5] = 0x55; chip.pot_level = Box::new(|n, _look| n == 5); step(&mut chip, Some(POTGO), 0); chip.run(300); assert!(chip.read(5) == 0x55 && chip.read(ALLPOT) == 0); } #[test] fn test_slow_scan_suspended_in_init_mode() { // B5.2: AHRM 5.9 "Suspended scans": init mode stops the 15 kHz clock let mut chip = POKEY::default(); step(&mut chip, Some(SKCTL), 3); step(&mut chip, Some(POTGO), 0); chip.run(1000); let held = chip.read(0); step(&mut chip, Some(SKCTL), 0); chip.run(1000); assert!(chip.read(0) == held && chip.read(ALLPOT) == 0xFF); } #[test] fn test_dump_transistors() { // B5.6: on while slow scan is selected and no scan runs; off during a scan and in fast scan let mut chip = POKEY::default(); let changes = Rc::new(RefCell::new(Vec::new())); let sink = Rc::clone(&changes); chip.on_dump = Some(Box::new(move |level, _look| sink.borrow_mut().push(level))); assert!(chip.dump_active); step(&mut chip, Some(SKCTL), 3); step(&mut chip, Some(POTGO), 0); assert!(chip.dump_active); // the scan starts in the next tick chip.tick(); assert!(!chip.dump_active); step(&mut chip, Some(SKCTL), 0x07); step(&mut chip, Some(POTGO), 0); chip.run(240); // the fast scan ends assert!(!chip.dump_active); // fast scan disables them step(&mut chip, Some(SKCTL), 0x03); assert!(chip.dump_active); assert_eq!(*changes.borrow(), vec![false, true]); } #[test] fn test_snapshot_keeps_the_scan() { let mut chip = fast_chip(); step(&mut chip, Some(POTGO), 0); chip.run(40); let mut other = POKEY::default(); other.restore(&chip.snapshot()); chip.run(10); other.run(10); assert!(other.read(0) == chip.read(0) && other.snapshot() == chip.snapshot()); } #[test] fn test_rc_helper_gives_a_paddle_count() { // B5.8: a target of 100 lines of charging reads about 100 in a slow scan; then a second // scan after the dump transistors have drained the capacitor reads the same let mut chip = POKEY::default(); let mut targets = vec![Some(114 * 100)]; targets.extend([None; 7]); let _rc = PotRC::wire(&mut chip, Some(targets)); step(&mut chip, Some(SKCTL), 3); for _ in 0..2 { step(&mut chip, Some(POTGO), 0); chip.run(229 * 114 + 400); // the scan ends, then the dump transistors drain the pin let count = chip.read(0); assert!((99..=101).contains(&count)); assert_eq!(chip.read(1), 228); // unconnected: never crosses } } #[test] fn test_the_chip_never_imports_the_rc_helper() { // decision 4: rc.rs is board-side let source = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/src/chip.rs")) .expect("src/chip.rs"); assert!(!source.lines().any(|line| { let line = line.trim_start(); line.starts_with("use ") && (line.contains("crate::rc") || line.contains("super::rc")) })); }