// SPDX-License-Identifier: MIT //! The README's two examples, so that they cannot drift from the API (this crate's own). use ay8910_rust::{PSG, SUNSOFT_5B, YM2149}; #[test] fn the_minimal_host() { const CLOCK: u64 = 2_000_000; // an Atari ST's let mut chip = PSG::new(YM2149); chip.write_register(7, 0x3E); // tone A alone chip.write_register(0, (284 & 0xFF) as u8); // 2,000,000 / (16 * 284) = 440 Hz chip.write_register(1, (284 >> 8) as u8); chip.write_register(8, 15); // full volume chip.run(CLOCK / 10); // a tenth of a second let codes = chip.take_outputs(); // [[a, b, c], ...], one per input clock, 0-31 let levels: Vec = codes .iter() .map(|[a, _, _]| YM2149.dac[*a as usize]) .collect(); assert_eq!(levels.len(), 200_000); // 440 Hz: 44 cycles in a tenth of a second, so 88 edges, give or take one. let edges = codes.windows(2).filter(|w| w[0] != w[1]).count(); assert!((87..=89).contains(&edges), "{edges}"); } #[test] fn the_sunsoft_5b_for_a_board() { let mut sound = PSG::with_record(SUNSOFT_5B, false); // polled: nothing queued for (register, value) in [(7u8, 0x3Eu8), (0, 0xFE), (8, 12)] { sound.select(register); // $C000 sound.write(value); // $E000 } let mut changes = 0; let mut last = sound.levels(); for _ in 0..1_789_773 / 60 { sound.tick(); if sound.levels() != last { last = sound.levels(); changes += 1; } } let [a, b, c] = sound.levels(); assert!(a == 25 || a == 0); assert_eq!((b, c), (1, 1)); // volume 0 drives code 1 on a Yamaha part: silence assert!(sound.mix() <= 3.0 && changes > 0); assert!(sound.take_outputs().is_empty()); sound.reset(); assert_eq!(sound.levels(), [1, 1, 1]); // every volume 0: code 1, silent }