//! Rung 0: the contract with the board (docs/validation.md, docs/api.md). //! //! Not a judge of the chip; it keeps the core usable. //! //! The reference's `tests/test_contract.py`. Python's checks of its own object model become //! their Rust meaning: a variant is a `Copy` record (a copy changed leaves the constant as it //! was), state is plain owned data (`Clone + PartialEq + Send + Sync`, no references), and the //! 80-attribute limit is PyPy's (here the test pins that the records have the reference's field //! counts, 20 and 12 plus the four operators). mod helpers; use helpers::*; use ym2612::chip::Channel; use ym2612::operator::Operator; use ym2612::{ FMVariant, BUSY_CLOCKS, CLOCKS_PER_SAMPLE, WAIT_AFTER_ADDRESS, WAIT_AFTER_CHANNEL_DATA, WAIT_AFTER_OPERATOR_DATA, YM2612, YM2612_DISCRETE, YM3438_ASIC, }; #[test] fn the_names_the_brief_promises_exist() { assert_eq!((CLOCKS_PER_SAMPLE, BUSY_CLOCKS), (144, 192)); assert_eq!( ( WAIT_AFTER_ADDRESS, WAIT_AFTER_OPERATOR_DATA, WAIT_AFTER_CHANNEL_DATA ), (17, 83, 47) ); let mut fm = YM2612::new(YM2612_DISCRETE); // The methods exist (this compiles) and do what their names say on a fresh chip. fm.write(0, 0x22); let _: Option = fm.read(0); fm.run(0); let _: Vec<(i32, i32)> = fm.take_samples(); fm.reset_line(1); let _: [i32; 6] = fm.channel_values(); assert!(fm.enabled && !fm.irq()); } #[test] fn variants_are_frozen_records_of_plain_values() { for variant in [YM2612_DISCRETE, YM3438_ASIC] { let mut copy: FMVariant = variant; copy.ladder = !copy.ladder; assert_ne!(copy, variant); let fresh = if variant.name == "YM2612" { YM2612_DISCRETE } else { YM3438_ASIC }; assert_eq!(fresh, variant); // the constant did not change } } #[test] fn one_frame_per_144_clocks_and_the_remainder_is_kept() { let mut fm = chip(); fm.run(143); assert_eq!(fm.take_samples(), vec![]); fm.run(1); assert_eq!(fm.take_samples().len(), 1); fm.run(144 * 10 + 100); fm.run(44); assert_eq!(fm.take_samples().len(), 11); assert_eq!(fm.take_samples(), vec![]); } fn busy_voice(fm: &mut YM2612) { voice( fm, Voice { channel: 0, algorithm: 4, total_levels: [20, 0, 30, 0], feedback: 5, attack: 20, ..V }, ); voice( fm, Voice { channel: 4, algorithm: 0, total_levels: [10, 20, 30, 0], attack: 31, ..V }, ); reg(fm, 0x22, 0x0F); reg(fm, 0xB4, 0xF7); reg(fm, 0x24, 0xFE); reg(fm, 0x26, 0xFD); reg(fm, 0x27, 0x0F); key(fm, 0, 0xF); key(fm, 4, 0xF); } /// Python's `random.Random(1).randrange(1, 700)` stands for "pieces of random length"; the /// lengths need not be Python's, so a small generator gives them here. struct Lcg(u64); impl Lcg { fn below(&mut self, n: u64) -> u64 { self.0 = self .0 .wrapping_mul(6364136223846793005) .wrapping_add(1442695040888963407); (self.0 >> 33) % n } } #[test] fn any_split_of_run_gives_the_same_samples_and_status() { for variant in [YM2612_DISCRETE, YM3438_ASIC] { let (mut whole, mut parts) = (YM2612::new(variant), YM2612::new(variant)); busy_voice(&mut whole); busy_voice(&mut parts); let total = 144 * 2000 + 77; whole.run(total); let mut rng = Lcg(1); let mut left = total; while left > 0 { let piece = left.min(1 + rng.below(699)); parts.run(piece); left -= piece; } assert_eq!(whole.take_samples(), parts.take_samples()); assert_eq!(whole.read(0), parts.read(0)); assert!(whole.status_flags == parts.status_flags && parts.status_flags == 3); } } fn plain() {} #[test] fn state_is_plain_attributes_with_no_callables() { plain::(); plain::(); plain::(); plain::(); let mut fm = chip(); busy_voice(&mut fm); step(&mut fm, 50); assert_eq!(fm.clone(), fm); } #[test] fn a_copied_state_continues_identically() { let mut fm = chip_of(YM2612_DISCRETE); busy_voice(&mut fm); step(&mut fm, 500); let mut twin = fm.clone(); assert_eq!(step(&mut fm, 3000), step(&mut twin, 3000)); } #[test] fn no_record_reaches_80_attributes() { // PyPy turns an instance with 80 or more attributes into a dict. Rust has no such limit; // what is kept is that the records hold the reference's fields: an operator's 20 slots, // a channel's 12 integers and its four operators. assert_eq!(std::mem::size_of::(), 20 * 4); assert_eq!( std::mem::size_of::(), 12 * 4 + 4 * std::mem::size_of::() ); } #[test] fn read_returns_none_where_the_bus_is_not_driven() { let (discrete, asic) = (YM2612::new(YM2612_DISCRETE), YM2612::new(YM3438_ASIC)); let d: Vec> = (0..4).map(|port| discrete.read(port)).collect(); let a: Vec> = (0..4).map(|port| asic.read(port)).collect(); assert_eq!(d, vec![Some(0), None, None, None]); assert_eq!(a, vec![Some(0), Some(0), Some(0), Some(0)]); } #[test] fn outputs_are_integers_within_the_documented_range() { let mut fm = chip_of(YM2612_DISCRETE); for channel in 0..6 { voice( &mut fm, Voice { channel, total_levels: [0, 0, 0, 0], ..V }, ); key(&mut fm, channel, 0xF); } let frames = step(&mut fm, 2000); let peak = frames .iter() .flat_map(|&(l, r)| [l.abs(), r.abs()]) .max() .unwrap(); assert!(peak <= 6 * 259); assert!(fm .channel_values() .iter() .all(|&v| (-256..=255).contains(&v))); } #[test] fn switched_off_the_chip_gives_silent_frames_and_timers_still_run() { let mut fm = chip_of(YM2612_DISCRETE); voice(&mut fm, V); key(&mut fm, 0, 0xF); reg(&mut fm, 0x24, 0xFF); reg(&mut fm, 0x25, 0x03); reg(&mut fm, 0x27, 0x05); fm.enabled = false; assert_eq!(step(&mut fm, 20), vec![(0, 0); 20]); assert!(fm.irq()); fm.enabled = true; assert!(step(&mut fm, 20).iter().any(|&frame| frame != (0, 0))); } #[test] fn the_reset_pin_low_holds_the_chip_and_ignores_writes() { let mut fm = chip(); voice(&mut fm, V); key(&mut fm, 0, 0xF); step(&mut fm, 100); fm.reset_line(0); reg(&mut fm, 0x2B, 0x80); assert!(fm.dac_enabled == 0 && fm.read(0) == Some(0)); assert_eq!(step(&mut fm, 5), vec![(0, 0); 5]); fm.reset_line(1); reg(&mut fm, 0x2B, 0x80); assert_eq!(fm.dac_enabled, 1); }