//! Rung 1: digital values published from real chips (docs/validation.md). //! //! Each test is one fact someone read out of a chip, or saw at its pin, and stated in //! the prose of a post of SpritesMind thread 386 ("p7967" is a post number). The //! provenance audit (docs/provenance-audit.md) read each post cited here. //! //! The reference's `tests/test_measured.py`; parametrized cases are loops. // The reference parametrizes one test over a single case; the loop keeps that form. #![allow(clippy::single_element_loop)] mod helpers; use helpers::*; use std::collections::BTreeSet; use ym2612::operator::{Operator, ATTACK, DECAY, RELEASE, SUSTAIN}; use ym2612::tables::{ENVELOPE_INCREMENTS, EXPONENT, LOG_SINE}; use ym2612::{YM2612, YM2612_DISCRETE, YM3438_ASIC}; fn operator(f_number: i32, block: i32, detune: i32, multiple: i32) -> Operator { let mut fm = chip(); let op = &mut fm.channels[0].operators[0]; YM2612::set_frequency(op, f_number, block); op.detune = detune; op.multiple = multiple; op.clone() } fn set(values: &[i32]) -> BTreeSet { values.iter().copied().collect() } // -- phase generator: Nemesis, p6177 (spec 5) ------------------------------------ #[test] fn phase_increments_nemesis_works_through() { // Spec 5.3, 5.6, 5.8: the worked examples of p6177. for (f_number, block, detune, multiple, expected) in [ (0x7FF, 7, 0, 1, 0x1FFC0), // "0x7FF<<6=0x1FFC0" (0x7FF, 0, 0, 1, 0x3FF), // "0x7FF>>1=0x3FF" (0x100, 5, 3, 1, 0x100B), // key code $14, detune 3 adds 11 (0x100, 5, 7, 1, 0xFF5), // detune -3 (0x1FF, 4, 0, 8, 0x7FC0), (0x1FF, 4, 0, 0, 0x7FC), // multiple 0 halves ] { let op = operator(f_number, block, detune, multiple); assert_eq!(op.phase_increment(f_number), expected); } } #[test] fn negative_detune_on_f_number_0_wraps_to_1ffff() { // Spec 5.7 (p6177): "if detune attempted to subtract a value of 1 from an fnum // of 0, the result would be 0x1FFFF". Key code 4 gives a detune of 1. let op = operator(0, 1, 5, 1); assert_eq!(op.phase_increment(0), 0x1FFFF); } #[test] fn detune_overflow_is_a_continuous_tone_with_every_f_number_0() { // Spec 5.7 (p5618): the test ROM "generates a continuous tone on the real // system ... even though all slots ... have a frequency number of 0". let mut fm = chip(); voice( &mut fm, Voice { f_number: 0, block: 1, ..V }, ); op_reg(&mut fm, 0, 3, 0x30, 0x51); key(&mut fm, 0, 0xF); let heard = values(&mut fm, 200, 0); assert!(*heard.iter().max().unwrap() > 200 && *heard.iter().min().unwrap() < -200); let mut fm = chip(); voice( &mut fm, Voice { f_number: 0, block: 1, ..V }, ); key(&mut fm, 0, 0xF); assert_eq!(set(&values(&mut fm, 200, 0)), BTreeSet::from([0])); } #[test] fn multiple_keeps_20_bits() { // Spec 5.8 (p6177): $1FFFF times 15 is $1DFFF1, "equivalent to 0xDFFF1". let op = operator(0, 1, 5, 15); assert_eq!(op.phase_increment(0), 0xDFFF1); } #[test] fn block_0_loses_the_f_numbers_low_bit() { // Spec 5.3 (p6177): "when block is set to 0, an fnum value of 1 = 0, 3 = 2". let op = operator(0, 0, 0, 1); assert_eq!(op.phase_increment(1), op.phase_increment(0)); assert_eq!(op.phase_increment(3), op.phase_increment(2)); } #[test] fn key_on_clears_the_phase_counter() { // Spec 5.9 (p7967 item 4). let mut fm = chip(); voice(&mut fm, V); key(&mut fm, 0, 0xF); step(&mut fm, 100); assert_ne!(fm.channels[0].operators[3].phase, 0); key(&mut fm, 0, 0); step(&mut fm, 1); key(&mut fm, 0, 0xF); step(&mut fm, 1); let op = &fm.channels[0].operators[3]; assert_eq!(op.phase, op.phase_increment(0x269)); // one increment after the clear } #[test] fn high_f_number_byte_waits_for_the_low_byte() { // Spec 3.3.5 (p5621): after $A6 = $1E, $A2 = $FF, $A6 = $24 "the effective // settings for the channel will remain as 0xA6 = 0x1E, 0xA2 = 0xFF". let mut fm = chip(); reg(&mut fm, 0xA6, 0x1E); reg(&mut fm, 0xA2, 0xFF); reg(&mut fm, 0xA6, 0x24); let channel = &fm.channels[2]; assert_eq!((channel.block, channel.f_number), (3, 0x6FF)); reg(&mut fm, 0xA2, 0xFF); let channel = &fm.channels[2]; assert_eq!((channel.block, channel.f_number), (4, 0x4FF)); } // -- operator unit (spec 6) ------------------------------------------------------ #[test] fn the_tables_ends_as_nemesis_gives_them() { // Spec 6.5 (p6741): the power table's entries "effectively run from // 0x7FA-0x400"; the ROM, without bit 10, "from 0x3FA-0x0". assert_eq!((EXPONENT[0], EXPONENT[255]), (0x7FA, 0x400)); assert!(LOG_SINE.len() == 256 && EXPONENT.len() == 256); let max = *LOG_SINE.iter().max().unwrap(); assert!(LOG_SINE[255] == 0 && max == LOG_SINE[0] && LOG_SINE[0] < 4096); // 12 bits } fn full_level_operator() -> Operator { let mut op = Operator::new(); op.attenuation = 0; op.envelope_phase = SUSTAIN; op } #[test] fn one_operator_spans_minus_8168_to_plus_8168() { // Spec 6.1 (GManiac, p11876): "operator's output is -8168..+8168". let mut op = full_level_operator(); let outputs: Vec = (0..1024).map(|_| op.clock(1 << 10, 0, 0)).collect(); assert_eq!( ( *outputs.iter().min().unwrap(), *outputs.iter().max().unwrap() ), (-8168, 8168) ); } #[test] fn eight_168_cut_to_nine_bits_is_plus_255_and_minus_256() { // Spec 14.1 (p11876): "+8168 asr 5 = +255", "-8168 asr 5 = -256". let mut fm = chip(); voice(&mut fm, V); key(&mut fm, 0, 0xF); let heard = values(&mut fm, 2000, 0); assert_eq!( (*heard.iter().min().unwrap(), *heard.iter().max().unwrap()), (-256, 255) ); } #[test] fn eight_total_level_steps_halve_the_output() { // Spec 6.8 (p11876): "each 8 steps of TL are equal to 6 dB (division by 2)". let mut op = full_level_operator(); op.phase = 0xFF << 10; let loud = op.clock(0, 0, 0); op.total_level = 8; let quiet = op.clock(0, 0, 0); assert!(quiet == loud >> 1 && loud >> 1 == 4084); } #[test] fn output_is_zero_past_about_40_db() { // Spec 6.6 (p6114): past "around 40db attenuation ... just outputs 0". let mut op = full_level_operator(); op.phase = 0xFF << 10; op.total_level = 0x7F; assert_eq!(op.clock(0, 0, 0), 0); op.total_level = 0x60; // $300: 36 of the envelope's 48 dB (72 by the manual's) assert_ne!(op.clock(0, 0, 0), 0); } // -- envelope generator: Nemesis, p5716, p7967 (spec 7) -------------------------- fn envelope_operator(phase: i32, attenuation: i32) -> Operator { let mut op = Operator::new(); op.envelope_phase = phase; op.attenuation = attenuation; op } #[test] fn attack_from_3ff_at_increment_8_reaches_0_in_ten_updates() { // Spec 7.4.2 (p7967 item 8): at an 8x increment "from a starting attenuation // level of 0x3FF, it would take 10 update steps to achieve minimum attenuation". // Rate 60 is the fastest attack that is not instant. let mut op = envelope_operator(ATTACK, 0x3FF); op.attack_rate = 30; assert_eq!(op.rate(op.attack_rate), 60); let mut updates = 0; while op.attenuation != 0 { updates += 1; op.envelope_step(updates); } assert_eq!(updates, 10); } #[test] fn first_step_of_a_slow_attack_from_200_gives_1df() { // Spec 7.4.2 (p8037): the attenuation "sits on 0x200 until the attack phase // advances. When it does advance, it will drop the attenuation to 0x1DF". let mut op = envelope_operator(ATTACK, 0x200); op.attack_rate = 10; let mut counter = 1; while op.attenuation == 0x200 { op.envelope_step(counter); counter += 1; } assert_eq!(op.attenuation, 0x1DF); } #[test] fn attack_rate_62_or_63_at_key_on_is_instant() { // Spec 7.4.4 (p7967 item 8): "special case handling to force the attenuation // level directly to 0 ... immediately when the key-on event is processed". for rate_register in [31] { let mut fm = chip(); voice( &mut fm, Voice { attack: rate_register, ..V }, ); let op = &fm.channels[0].operators[3]; assert!(op.attenuation == 0x3FF && op.rate(op.attack_rate) >= 62); key(&mut fm, 0, 0xF); step(&mut fm, 1); assert_eq!(fm.channels[0].operators[3].attenuation, 0); } } #[test] fn switching_to_rate_62_during_an_attack_stalls_it() { // Spec 7.4.5 (p7967 item 8): "the attack phase will NOT finish ... it won't // advance at all ... until the attack rate is modified again to something less // than 62 ... or until key-off occurs". let mut fm = chip(); voice(&mut fm, Voice { attack: 12, ..V }); key(&mut fm, 0, 0xF); step(&mut fm, 600); let op = &fm.channels[0].operators[3]; assert!(op.envelope_phase == ATTACK && 0 < op.attenuation && op.attenuation < 0x3FF); let stalled_at = op.attenuation; op_reg(&mut fm, 0, 3, 0x50, 31); step(&mut fm, 3000); let op = &fm.channels[0].operators[3]; assert_eq!((op.envelope_phase, op.attenuation), (ATTACK, stalled_at)); op_reg(&mut fm, 0, 3, 0x50, 12); step(&mut fm, 600); assert!(fm.channels[0].operators[3].attenuation < stalled_at); } #[test] fn decay_that_passes_the_sustain_level_is_not_cut_back() { // Spec 7.4.6 (p7967 item 6): "the sustain phase begins on the next cycle, but // the attenuation value is not clamped to SL". let mut op = envelope_operator(DECAY, 0x1C); (op.decay_rate, op.sustain_level, op.sustain_rate) = (30, 1, 0); op.envelope_step(1); assert_eq!((op.envelope_phase, op.attenuation), (DECAY, 0x24)); // past $20 op.envelope_step(2); assert_eq!((op.envelope_phase, op.attenuation), (SUSTAIN, 0x24)); } #[test] fn a_sustain_level_write_after_decay_ended_does_nothing() { // Spec 7.4.6 (p7967 item 5): "the operator does NOT re-enter the decay phase". let mut op = envelope_operator(DECAY, 0x40); (op.decay_rate, op.sustain_level, op.sustain_rate) = (30, 2, 0); op.envelope_step(1); assert_eq!(op.envelope_phase, SUSTAIN); op.sustain_level = 8; for counter in 2..50 { op.envelope_step(counter); } assert_eq!((op.envelope_phase, op.attenuation), (SUSTAIN, 0x40)); } #[test] fn the_rate_is_worked_out_at_every_update() { // Spec 7.2.3 (p7967 item 5): rate changes "can cause an immediate change in // the effective rate for the current phase". let mut op = envelope_operator(DECAY, 0); (op.decay_rate, op.sustain_level) = (0, 15); op.envelope_step(1); assert_eq!(op.attenuation, 0); op.decay_rate = 30; op.envelope_step(2); assert_eq!(op.attenuation, 8); } #[test] fn release_rate_is_never_0() { // Spec 7.2.1 (p5724): RR is "a 5-bit value with the LSB set to 1 ... so Rate // can never equal 0 in the release phase". let mut op = envelope_operator(RELEASE, 0); op.release_rate = 0; for counter in 1..4096 { op.envelope_step(counter); } assert!(op.attenuation > 0); } #[test] fn rate_7_steps_0_1_1_1_0_1_1_1() { // Spec 7.3.3 (p5716): "a calculated rate value of 7 ... gives it an update // cycle pattern of 0,1,1,1,0,1,1,1". assert_eq!(ENVELOPE_INCREMENTS[7], [0, 1, 1, 1, 0, 1, 1, 1]); } #[test] fn low_rates_pair_up() { // Spec 7.3.3 (p5716): "rate values of 2 and 3, 4 and 5, 6 and 7 do produce an // identical output. That's not an error". for low in [2, 4, 6] { assert_eq!(ENVELOPE_INCREMENTS[low], ENVELOPE_INCREMENTS[low + 1]); } } #[test] fn the_largest_increment_is_8_and_the_top_rates_use_it() { // Spec 7.3.3 (p7967 items 6, 8): "the largest increment value which can be // applied ... in a single update cycle is 8"; rates 62 and 63 "would be the same // as rate values 60 and 61, namely ... an 8x increment". let max = ENVELOPE_INCREMENTS .iter() .map(|row| *row.iter().max().unwrap()) .max() .unwrap(); assert_eq!(max, 8); assert!((60..64).all(|rate| ENVELOPE_INCREMENTS[rate] == [8; 8])); } #[test] fn the_slowest_and_fastest_decays_last_what_nemesis_measured() { // Spec 7.3.2 (p5716, restated in envelope updates): rate 63 decays in 128 // updates and rate 2 in 2,048 times 2,046 or so. let mut op = envelope_operator(DECAY, 0); (op.decay_rate, op.sustain_rate, op.sustain_level) = (31, 31, 15); let mut updates = 0; while op.attenuation < 0x3FF && updates < 1000 { updates += 1; op.envelope_step(updates); } assert_eq!(updates, 128); let mut op = envelope_operator(DECAY, 0); (op.decay_rate, op.sustain_level) = (1, 15); assert_eq!(op.rate(1), 2); for counter in 1..4096 { op.envelope_step(counter); } assert_eq!(op.attenuation, 1); // one step in 4,095 updates: every other 2,048th } #[test] fn the_envelope_advances_every_third_sample() { // Spec 4.5 (p6224, p7967 item 1): "the envelope generator is updated once // every 3 fm update cycles". let mut fm = chip(); voice(&mut fm, V); op_reg(&mut fm, 0, 3, 0x60, 31); // decay rate 62: 8 per update op_reg(&mut fm, 0, 3, 0x80, 0xFF); key(&mut fm, 0, 0xF); let mut seen = Vec::new(); for _ in 0..30 { step(&mut fm, 1); seen.push(fm.channels[0].operators[3].attenuation); } let changes: Vec = (1..30).filter(|&i| seen[i] != seen[i - 1]).collect(); assert!(changes.windows(2).all(|pair| pair[1] - pair[0] == 3)); assert!(changes.len() >= 8); } #[test] fn keys_are_acted_on_every_sample() { // Spec 7.4.9 (p7967 item 7): "key-on changes are processed every fm update // cycle". let mut fm = chip(); voice(&mut fm, V); for _ in 0..3 { // whichever sample of the three it is key(&mut fm, 0, 0xF); step(&mut fm, 1); assert_ne!(fm.channels[0].operators[3].envelope_phase, RELEASE); key(&mut fm, 0, 0); step(&mut fm, 1); assert_eq!(fm.channels[0].operators[3].envelope_phase, RELEASE); } } // -- SSG-EG: Nemesis, p7967, p8025, p8037 (spec 7.6) ----------------------------- #[test] fn ssg_eg_inversion_is_centred_on_200() { // Spec 7.6.2 (p7967): "0x001 should invert to 0x1FF, 0x110 should invert to // 0xF0, 0x250 should invert to 0x3B0". for (level, inverted) in [(0x001, 0x1FF), (0x110, 0x0F0), (0x250, 0x3B0)] { let mut op = envelope_operator(ATTACK, level); op.ssg = 0x0C; assert_eq!(op.level(0), inverted); } } #[test] fn a_write_to_the_attack_bit_inverts_the_output_at_once() { // Spec 7.6.2 (p8025): "A change to the attack bit always causes an immediate // inversion of the output". let mut op = envelope_operator(DECAY, 0x040); op.ssg = 0x08; assert_eq!(op.level(0), 0x040); op.ssg = 0x0C; assert_eq!(op.level(0), 0x1C0); op.ssg_flag = 1; // toggled once: the attack bit now gives a plain output assert_eq!(op.level(0), 0x040); } #[test] fn no_inversion_in_release_whatever_the_bits() { // Spec 7.6.2 (p8025): "Output inversion is always disabled during the release // phase"; "toggling the ATT bit during the release phase doesn't cause output // inversion to kick in". let mut op = envelope_operator(RELEASE, 0x040); op.ssg = 0x0C; assert_eq!(op.level(0), 0x040); } #[test] fn ssg_eg_steps_four_times_as_far_in_decay_sustain_and_release() { // Spec 7.6.1 (p7967, SSG-EG correction 1): "only multiplied by a factor of 4", // and "should indeed be applied during the release phase". for phase in [DECAY, SUSTAIN, RELEASE] { let mut plain = envelope_operator(phase, 0); (plain.decay_rate, plain.sustain_rate, plain.release_rate) = (31, 31, 15); let mut ssg = envelope_operator(phase, 0); (ssg.decay_rate, ssg.sustain_rate, ssg.release_rate, ssg.ssg) = (31, 31, 15, 8); plain.sustain_level = 15; ssg.sustain_level = 15; plain.envelope_step(1); ssg.envelope_step(1); assert_eq!((plain.attenuation, ssg.attenuation), (8, 32)); } } #[test] fn one_sample_is_output_at_200_before_the_pattern_repeats() { // Spec 7.6.4 (p7967): "a single output sample is generated at this attenuation // level before the envelope loops back to the attack phase". let mut op = envelope_operator(DECAY, 0x1E0); (op.decay_rate, op.attack_rate, op.sustain_level, op.ssg) = (31, 31, 15, 8); op.ssg_step(); op.envelope_step(1); assert_eq!(op.level(0), 0x200); op.ssg_step(); assert_eq!((op.envelope_phase, op.attenuation), (ATTACK, 0)); } #[test] fn looping_with_a_slow_attack_rests_at_200_and_toggles_every_sample() { // Spec 7.6.6 (p7967, p8037): "the output inversion state is toggled each // sample where the alternate bit is set, and the hold bit is unset" while the // attenuation stays at or above $200. let mut op = envelope_operator(DECAY, 0x200); (op.decay_rate, op.attack_rate, op.sustain_level, op.ssg) = (31, 4, 15, 0x0A); let mut flags = Vec::new(); for _ in 0..6 { op.ssg_step(); flags.push(op.ssg_flag); } assert!(op.envelope_phase == ATTACK && op.attenuation == 0x200); assert_eq!(flags, vec![1, 0, 1, 0, 1, 0]); } #[test] fn release_reaching_200_under_ssg_eg_goes_silent() { // Spec 7.6.3 (p8025, p8032): "the attenuation level is still forced directly // to 0x3FF when the release phase reaches 0x200". let mut op = envelope_operator(RELEASE, 0x200); (op.release_rate, op.ssg) = (15, 8); op.ssg_step(); assert_eq!(op.attenuation, 0x3FF); } #[test] fn phase_is_held_in_patterns_without_alternate_or_hold() { // Spec 5.10 (p7967 item 4): the phase counter is reset "in cases where both // the HLD and ALT bits are unset". for (ssg, held) in [(0x08, true), (0x0C, true), (0x0A, false), (0x09, false)] { let mut op = envelope_operator(DECAY, 0x200); (op.attack_rate, op.ssg) = (4, ssg); op.phase = 0x12345; op.ssg_step(); assert_eq!(op.phase == 0, held); } } #[test] fn upper_bits_of_the_ssg_register_do_nothing() { // Spec 3.2.3 (p8037, p14196): "The upper 4 bits had no observable effect". let mut fm = chip(); op_reg(&mut fm, 0, 0, 0x90, 0xF5); assert_eq!(fm.channels[0].operators[0].ssg, 0x05); } // -- timers: Nemesis, p5680, p5687 (spec 11) ------------------------------------- fn samples_until_flag(fm: &mut YM2612, mask: u8) -> usize { samples_until_flag_limit(fm, mask, 20000) } fn samples_until_flag_limit(fm: &mut YM2612, mask: u8, limit: usize) -> usize { for count in 1..limit { step(fm, 1); if status(fm) & mask != 0 { return count; } } panic!("no overflow"); } #[test] fn timer_a_at_3ff_overflows_every_sample() { // Spec 11.1 (p7967): "timer A runs at the same rate as the phase generator // update cycle, setting the timer A period to the shortest possible value // (0x3FF)". let mut fm = chip(); reg(&mut fm, 0x24, 0xFF); reg(&mut fm, 0x25, 0x03); reg(&mut fm, 0x27, 0x05); for _ in 0..5 { assert_eq!(samples_until_flag(&mut fm, 1), 1); reg(&mut fm, 0x27, 0x15); } } #[test] fn timer_periods_are_twice_the_ym2608_manuals() { // Spec 11.1, contradiction C1 (p5680): "the YM2612 timers take twice as long // to count down as the timers in the YM2608": 144 and 2,304 clocks a step. let mut fm = chip(); reg(&mut fm, 0x24, 0xF0); // NA = $3C0: 64 samples reg(&mut fm, 0x27, 0x05); assert_eq!(samples_until_flag(&mut fm, 1), 64); let mut fm = chip(); reg(&mut fm, 0x26, 0xFC); // NB = 252: 4 steps of 16 samples reg(&mut fm, 0x27, 0x0A); assert!((4 * 16 - 15..4 * 16 + 1).contains(&samples_until_flag(&mut fm, 2))); } #[test] fn timer_reload_rules_one_by_one() { // Spec 11.2 (p5687): reloaded on underflow and when the load bit goes 0 to 1; // NOT when 1 is written over 1, when reset or enable change, or when the timer // registers are written. let mut fm = chip(); reg(&mut fm, 0x24, 0xF0); reg(&mut fm, 0x27, 0x05); step(&mut fm, 40); reg(&mut fm, 0x27, 0x05); // load written 1 over 1 reg(&mut fm, 0x27, 0x15); // reset bit reg(&mut fm, 0x27, 0x01); // enable off reg(&mut fm, 0x27, 0x05); // and on reg(&mut fm, 0x24, 0xF8); // a new value: used at the next reload assert_eq!(samples_until_flag(&mut fm, 1), 24); reg(&mut fm, 0x27, 0x15); assert_eq!(samples_until_flag(&mut fm, 1), 32); // $3E0: the new value reg(&mut fm, 0x27, 0x10); // load 0: stopped step(&mut fm, 500); assert_eq!(status(&fm) & 1, 0); reg(&mut fm, 0x27, 0x05); // 0 to 1: restarts from the register value assert_eq!(samples_until_flag(&mut fm, 1), 32); } #[test] fn timer_a_bytes_are_not_ganged_like_the_f_number() { // Spec 11.2 (p5680): "they are NOT 'ganged together' like the frequency // registers are". let mut fm = chip(); reg(&mut fm, 0x25, 0x02); assert_eq!(fm.timer_a_register, 0x002); reg(&mut fm, 0x24, 0x80); assert_eq!(fm.timer_a_register, 0x202); } #[test] fn the_interrupt_line_is_the_or_of_the_two_flags() { // Spec 11.4 (p7967): "generated in real-time by a logical OR operation between // the overflow bits of the two timers", until "the reset bit is flagged". let mut fm = chip(); reg(&mut fm, 0x24, 0xFF); reg(&mut fm, 0x25, 0x03); reg(&mut fm, 0x27, 0x05); assert!(!fm.irq()); step(&mut fm, 1); assert!(fm.irq()); reg(&mut fm, 0x27, 0x10); assert!(!fm.irq()); } // -- CSM: Nemesis, p5650, p7967 (spec 10) ---------------------------------------- /// Returns the chip; the operator the Python returns beside it is /// `fm.channels[2].operators[3]`. fn csm_chip(mode_bits: u8, timer_a: i32) -> YM2612 { let mut fm = chip(); voice(&mut fm, Voice { channel: 2, ..V }); reg(&mut fm, 0x24, (timer_a >> 2) as u8); reg(&mut fm, 0x25, (timer_a & 3) as u8); reg(&mut fm, 0x27, (mode_bits << 6) | 0x01); fm } fn csm_op(fm: &YM2612) -> &Operator { &fm.channels[2].operators[3] } #[test] fn csm_acts_in_mode_10_only() { // Spec 10.1 (p5650, p7967): "CSM mode is only active when the mode bits are // set to 10, NOT when they are set to 11". for (mode, active) in [(0, false), (1, false), (2, true), (3, false)] { let mut fm = csm_chip(mode, 0x3F0); let mut phases = BTreeSet::new(); for _ in 0..40 { step(&mut fm, 1); phases.insert(csm_op(&fm).envelope_phase); } assert_eq!(phases != BTreeSet::from([RELEASE]), active, "{mode}"); } } #[test] fn csm_needs_only_timer_as_load_bit() { // Spec 10.3 (p5650): "None of the other timer-related flags ... have any // impact on CSM mode". let mut fm = csm_chip(2, 0x3F0); assert_eq!(fm.timer_enable, 0); let mut keyed = 0; for _ in 0..64 { step(&mut fm, 1); keyed += csm_op(&fm).key_state; } assert!(keyed == 4 && status(&fm) == 0); } #[test] fn a_lone_overflow_keys_on_for_one_sample() { // Spec 10.4 (p7967): the flag "remains set until the next ... update cycle"; // "If timer A does not overflow again before the next update, the CSM key-on // flag will appear negated, which would key-off the operator". let mut fm = csm_chip(2, 0x3F0); let mut states = Vec::new(); for _ in 0..48 { step(&mut fm, 1); states.push(csm_op(&fm).key_state); } assert_eq!(states.iter().filter(|&&s| s == 1).count(), 3); assert!(!states.windows(2).any(|pair| pair == [1, 1])); } #[test] fn overflow_on_every_sample_holds_the_key() { // Spec 10.4 (p7967): "If timer A overflows before every update cycle ... the // CSM key-on flag appears to be permanently set". let mut fm = csm_chip(2, 0x3FF); step(&mut fm, 1); let mut states = BTreeSet::new(); for _ in 0..30 { step(&mut fm, 1); states.insert(csm_op(&fm).key_state); } assert_eq!(states, BTreeSet::from([1])); } #[test] fn a_key_held_through_28_is_not_disturbed_and_can_be_handed_over() { // Spec 10.4 (p7967): the two flags "are OR'd together"; you can "key on an // operator manually, trigger a CSM key-on event in a 'held' state (timer A at // max rate), key-off the operator manually, and find that the operator does not // actually key-off". let mut fm = csm_chip(2, 0x3F0); key(&mut fm, 2, 0xF); step(&mut fm, 100); assert_ne!(csm_op(&fm).envelope_phase, RELEASE); let mut fm = csm_chip(2, 0x3FF); key(&mut fm, 2, 0xF); step(&mut fm, 10); key(&mut fm, 2, 0); step(&mut fm, 10); assert!(csm_op(&fm).key_state == 1 && csm_op(&fm).envelope_phase != RELEASE); } #[test] fn leaving_mode_10_releases_a_held_csm_key() { // Spec 10.4 (p7967): "if the channel 3 mode doesn't equal 10 ... the CSM // key-on state is forced to 0. This causes a CSM key-off event". let mut fm = csm_chip(2, 0x3FF); step(&mut fm, 10); assert_eq!(csm_op(&fm).key_state, 1); reg(&mut fm, 0x27, 0x41); step(&mut fm, 2); assert!(csm_op(&fm).key_state == 0 && csm_op(&fm).envelope_phase == RELEASE); } #[test] fn csm_key_on_clears_the_phase_too() { // Spec 5.9 (p7967 item 4): "This of course also includes key-on events // triggered by CSM mode". let mut fm = csm_chip(2, 0x3F0); fm.channels[2].operators[3].phase = 0x54321; for _ in 0..20 { step(&mut fm, 1); if csm_op(&fm).key_state != 0 { break; } } let op = csm_op(&fm); assert_eq!(op.phase, op.phase_increment(op.f_number)); } // -- accumulator, DAC, status (spec 12 to 14) ------------------------------------ #[test] fn carriers_are_cut_to_nine_bits_before_they_are_summed() { // Spec 14.1 (GManiac, p11862, p11876): "operators are shifted first, then are // summed"; "shifted arithmetically (not logically) by 5 bits". Four carriers each // at -1 of 14 bits sum to -4, not to (-4 >> 5) = -1. let mut fm = chip(); voice( &mut fm, Voice { algorithm: 7, total_levels: [0x48, 0x48, 0x48, 0x48], f_number: 0x400, block: 4, ..V }, ); key(&mut fm, 0, 0xF); let seen = set(&values(&mut fm, 3000, 0)); assert!(*seen.first().unwrap() == -4 && *seen.last().unwrap() == 0); // each carrier swings -1 to 0 } #[test] fn the_sum_is_limited_not_wrapped() { // Spec 14.2 (p11876; p5958: "Clipping DOES occur when operators are // combined"). let mut fm = chip(); voice( &mut fm, Voice { algorithm: 7, total_levels: [0, 0, 0, 0], ..V }, ); key(&mut fm, 0, 0xF); let heard = values(&mut fm, 2000, 0); assert_eq!( (*heard.iter().min().unwrap(), *heard.iter().max().unwrap()), (-256, 255) ); assert!(heard.iter().filter(|&&v| v == 255).count() > 400); assert!(heard.iter().filter(|&&v| v == -256).count() > 400); } #[test] fn channel_6_keeps_running_under_the_dac() { // Spec 13.2 (GManiac, p14109): "FM Channel 6 and Reg $2A are independent of // each other. 'DAC mode' (Reg $2B) only chose output of one of them". let (mut with_dac, mut without) = (chip(), chip()); for fm in [&mut with_dac, &mut without] { voice(fm, Voice { channel: 5, ..V }); key(fm, 5, 0xF); } reg(&mut with_dac, 0x2B, 0x80); step(&mut with_dac, 777); step(&mut without, 777); assert_eq!(with_dac.channel_values()[5], 0); // $2A is $80 after reset: mid-scale reg(&mut with_dac, 0x2B, 0x00); assert_eq!(values(&mut with_dac, 50, 5), values(&mut without, 50, 5)); } #[test] fn busy_is_set_by_a_data_write_only_and_lasts_192_clocks() { // Spec 12.2 (Eke, p31026): "BUSY flag is only set on DATA port writes"; // "duration seems to be 32 internal clocks (32*6 68k clocks)"; "does not depend // on the written register". for address in [0x22, 0x30, 0xA0, 0x28] { let mut fm = chip(); fm.write(0, address); assert_eq!(status(&fm) & 0x80, 0); fm.write(1, 0); assert_ne!(status(&fm) & 0x80, 0); fm.run(191); assert_ne!(status(&fm) & 0x80, 0); fm.run(1); assert_eq!(status(&fm) & 0x80, 0); } } #[test] fn absent_registers_do_nothing() { // Spec 3.1.2 (p5680): $2D to $2F "had no noticeable effect"; the "interrupt // control flags ... in register $29 do not exist". Spec 2.3 (Shiru, p5648): // special mode cannot be set through part II. let (mut fm, untouched) = (chip(), chip()); for address in [0x29, 0x2D, 0x2E, 0x2F] { reg(&mut fm, address, 0xFF); } reg_part(&mut fm, 0x27, 0x40, 1); reg_part(&mut fm, 0x22, 0x0F, 1); assert!(fm.channel3_mode == 0 && fm.lfo_enabled == 0); fm.busy_clocks = 0; // Every attribute but the latch ("channels", "address", "address_part"). let mut compared = fm.clone(); compared.channels = untouched.channels.clone(); compared.address = untouched.address; compared.address_part = untouched.address_part; assert_eq!(compared, untouched); } #[test] fn channel_3s_operators_take_the_registers_shiru_found() { // Spec 3.3.4 (p5605, Shiru's hardware test): operator 4 is $A6/$A2, operator // 3 $AC/$A8, operator 2 $AE/$AA, operator 1 $AD/$A9. let mut fm = chip(); reg(&mut fm, 0x27, 0x40); for (high, low, f_number) in [ (0xA6, 0xA2, 0x104), (0xAC, 0xA8, 0x103), (0xAE, 0xAA, 0x102), (0xAD, 0xA9, 0x101), ] { reg(&mut fm, high, 0x08 | (f_number >> 8) as u8); reg(&mut fm, low, (f_number & 0xFF) as u8); } let f_numbers: Vec = fm.channels[2] .operators .iter() .map(|op| op.f_number) .collect(); assert_eq!(f_numbers, vec![0x101, 0x102, 0x103, 0x104]); } #[test] fn status_is_the_same_whatever_register_is_latched() { // Spec 12.1 (p5680): the status is returned "regardless of ... the register // that is latched at the time"; there is no chip ID at $FF. let mut fm = chip(); fm.write(0, 0xFF); assert_eq!(fm.read(0), Some(0)); } #[test] fn after_reset_the_channel_outputs_read_0() { // Spec 16.2 (Sauraen, p30088, on a YM3438): "if there's nothing playing (or // after power-up), the values is 0". for variant in [YM2612_DISCRETE, YM3438_ASIC] { let mut fm = chip_of(variant); step(&mut fm, 10); assert_eq!(fm.channel_values(), [0; 6]); } } #[test] fn frequency_changes_are_used_from_the_next_sample() { // Spec 5.9 (p5655): "the new frequency is used for the very next sample". let mut fm = chip(); voice(&mut fm, V); key(&mut fm, 0, 0xF); step(&mut fm, 10); let before = fm.channels[0].operators[3].phase; frequency(&mut fm, 0, 0x100, 2); step(&mut fm, 1); assert_eq!( (fm.channels[0].operators[3].phase - before) & 0xFFFFF, 0x100 << 1 ); }