// SPDX-License-Identifier: MIT //! Rung 4: community statements checked on hardware (docs/validation.md). //! //! Prose rules without a printed value; each test pins the rule and gives its tier. //! Tier 1: the author measured it on a chip (or read it off the die: "die"). Tier 3: //! a community author says so without describing a test. //! //! The reference's `tests/test_community.py`. // Kept in the reference's form: `5 * (variant.ladder_idle + 0)`. #![allow(clippy::identity_op)] mod helpers; use helpers::*; use std::collections::{BTreeMap, BTreeSet}; use ym2612::operator::ATTACK; use ym2612::tables::vibrato_offset; use ym2612::{YM2612_DISCRETE, YM3438_ASIC}; #[test] fn one_address_latch_holds_the_part_and_either_data_port_serves() { // Spec 2.5, tier 1 (Nemesis, p7673): "you can ... write an address to $A00000, // then write the data to $A00003, and the data will in fact be written to the // part 1 register block". let mut fm = chip(); fm.write(0, 0x40); fm.write(3, 0x55); assert_eq!(fm.channels[0].operators[0].total_level, 0x55); assert_eq!(fm.channels[3].operators[0].total_level, 0); fm.write(2, 0x40); fm.write(1, 0x33); assert_eq!(fm.channels[3].operators[0].total_level, 0x33); assert_eq!(fm.channels[0].operators[0].total_level, 0x55); } #[test] fn an_address_stays_latched_for_several_data_bytes() { // Spec 2.4, documented (S1, S2 page 7). let mut fm = chip(); fm.write(0, 0x2A); for data in [1, 2, 3] { fm.write(1, data); assert_eq!(fm.dac_data, data as i32); } } #[test] fn key_on_does_not_reset_the_attenuation() { // Spec 7.4.3, tier 3 (Snake, p7999: "the level continues from where it was"; // Nemesis, p5724). let mut fm = chip(); voice(&mut fm, Voice { attack: 14, ..V }); op_reg(&mut fm, 0, 3, 0x80, 0x02); // a slow release key(&mut fm, 0, 0xF); step(&mut fm, 4000); key(&mut fm, 0, 0); step(&mut fm, 300); let level = fm.channels[0].operators[3].attenuation; assert!(0 < level && level < 0x3FF); key(&mut fm, 0, 0xF); step(&mut fm, 1); let op = &fm.channels[0].operators[3]; assert!(op.envelope_phase == ATTACK && op.attenuation == level); } #[test] fn total_level_feedback_and_algorithm_are_heard_at_the_next_sample() { // Spec 7.1.2, 9.5: tier 1 for feedback (Shiru, p5720: "it changes sound // immediately"), reasoned for total level (Nemesis, p8020). let mut fm = chip(); voice( &mut fm, Voice { f_number: 0x400, block: 6, ..V }, ); key(&mut fm, 0, 0xF); step(&mut fm, 9); let mut twin = chip(); voice( &mut twin, Voice { f_number: 0x400, block: 6, ..V }, ); key(&mut twin, 0, 0xF); step(&mut twin, 9); op_reg(&mut fm, 0, 3, 0x40, 16); assert_eq!( values(&mut fm, 1, 0)[0].abs(), values(&mut twin, 1, 0)[0].abs() >> 2 ); } #[test] fn tremolo_is_still_applied_with_the_lfo_off() { // Spec 8.3, tier 1 (Nemesis, p7490): "Even when the LFO is disabled, amplitude // ... modulation are still applied ... it applies a large amount of attenuation". // That it rests at the maximum is provisional (provisional.rs). let (mut loud, mut quiet) = (chip(), chip()); for fm in [&mut loud, &mut quiet] { voice(fm, V); reg(fm, 0xB4, 0xF0); key(fm, 0, 0xF); } op_reg(&mut quiet, 0, 3, 0x60, 0x80); let quiet_max = *values(&mut quiet, 400, 0).iter().max().unwrap(); let loud_max = *values(&mut loud, 400, 0).iter().max().unwrap(); assert!(quiet_max < loud_max / 2); } #[test] fn vibrato_is_applied_to_the_f_number_before_the_block() { // Spec 8.5, tier 1 (Nemesis, p8905, p8908: "applied directly to fnum, before // block data is applied, and this is an 11-bit calculation"; "just mask the // result ... with an 11-bit mask"). let mut fm = chip(); voice( &mut fm, Voice { f_number: 0x7FF, block: 3, ..V }, ); reg(&mut fm, 0xB4, 0xC7); reg(&mut fm, 0x22, 0x0F); key(&mut fm, 0, 0xF); let mut increments = BTreeSet::new(); let mut last = 0; for _ in 0..128 * 6 { step(&mut fm, 1); let phase = fm.channels[0].operators[3].phase; increments.insert((phase - last) & 0xFFFFF); last = phase; } let expected: BTreeSet = (0..128) .map(|counter| (((0x7FF + vibrato_offset(0x7FF, 7, counter)) & 0x7FF) << 3) >> 1) .collect(); let mut allowed = expected.clone(); allowed.insert(0); increments.remove(&((-last) & 0xFFFFF)); assert!(increments.is_subset(&allowed)); assert!(*expected.first().unwrap() < 0x100); // the positive swing wraps to a very small F-number assert_eq!(fm.channels[0].operators[3].key_code, (3 << 2) | 3); // untouched by the LFO (die, p31049) } #[test] fn vibrato_cannot_take_a_small_f_number_below_zero() { // Spec 8.6, tier 1 (Nemesis, p8908): "If all upper six bits of fnum are 0, // phase modulation won't adjust fnum at all"; "fnum will always be larger than // the amount that would be subtracted". for f_number in 0..0x20 { assert!((0..128).all(|counter| vibrato_offset(f_number, 7, counter) == 0)); } for f_number in [0x20, 0x155, 0x400, 0x7FF] { let lowest = (0..128) .map(|counter| vibrato_offset(f_number, 7, counter)) .min() .unwrap(); assert!(lowest > -f_number); } } #[test] fn modulation_is_the_output_shifted_right_once() { // Spec 9.3, tier 1 (GManiac, p18814: the "sum shifts RIGHT for ONE bit and // this value is added to input phase of modulated operator. No exceptions"). let mut fm = chip(); voice( &mut fm, Voice { algorithm: 4, total_levels: [0, 0, 127, 127], f_number: 0, block: 0, ..V }, ); key(&mut fm, 0, 0b0011); step(&mut fm, 1); fm.channels[0].operators[0].phase = 0x40000; // a quarter turn: operator 1's peak step(&mut fm, 1); let out1 = fm.channels[0].feedback_newer; assert!(out1 > 8000); let angle = (out1 >> 1) & 0x3FF; let mut probe = chip(); voice( &mut probe, Voice { algorithm: 7, total_levels: [127, 0, 127, 127], f_number: 0, block: 0, ..V }, ); key(&mut probe, 0, 0b0010); step(&mut probe, 1); probe.channels[0].operators[1].phase = angle << 10; step(&mut probe, 1); let here = fm.channels[0].operator2_previous; assert!(here == probe.channels[0].operator2_previous && here != 0); assert_eq!(fm.channels[0].operators[1].phase, 0); } #[test] fn feedback_uses_operator_1s_last_two_outputs() { // Spec 9.4, tier 1 (GManiac, p18814: "there are really two feedback registers, // I made it sure by special tests"), die (Sauraen, p27319: a 15-bit sum). let mut fm = chip(); voice( &mut fm, Voice { algorithm: 7, total_levels: [0, 127, 127, 127], feedback: 7, f_number: 0, block: 0, ..V }, ); key(&mut fm, 0, 0b0001); step(&mut fm, 1); (fm.channels[0].feedback_older, fm.channels[0].feedback_newer) = (4000, 0); step(&mut fm, 1); let from_older = fm.channels[0].feedback_newer; (fm.channels[0].feedback_older, fm.channels[0].feedback_newer) = (0, 4000); step(&mut fm, 1); let from_newer = fm.channels[0].feedback_newer; assert!(from_older == from_newer && from_newer != 0); assert_eq!(fm.channels[0].feedback_older, 4000); // the newer became the older } #[test] fn the_delayed_modulation_paths_of_the_die_reading() { // Spec 9.6, die (Sauraen, p29471: operator 3 "is evaluated with the ... // operator 1 value" of the previous round and "operator 4 is evaluated with the // result of operator 2 which was calculated 5 phases ago"; operator 2 and the // 3-to-4 path use the most recent). Decision T4, provisional. // Algorithm 5: operator 1 feeds 2, 3 and 4. Operators 2 and 4 hear operator 1's // output of this sample; operator 3 hears the one before. let mut fm = chip(); voice( &mut fm, Voice { algorithm: 5, total_levels: [0, 0, 0, 0], f_number: 0x300, block: 5, ..V }, ); key(&mut fm, 0, 0xF); step(&mut fm, 50); for op in fm.channels[0].operators[1..].iter_mut() { op.phase = 0; } let before = fm.channels[0].feedback_newer; step(&mut fm, 1); let now = fm.channels[0].feedback_newer; assert_ne!(before, now); let mut probe = chip(); voice( &mut probe, Voice { algorithm: 7, total_levels: [127, 0, 0, 0], f_number: 0x300, block: 5, ..V }, ); key(&mut probe, 0, 0xF); step(&mut probe, 5); let mut outputs = BTreeMap::new(); for (name, modulator) in [("current", now), ("previous", before)] { let op = &mut probe.channels[0].operators[1]; op.phase = (-op.phase_increment(0x300)) & 0xFFFFF; let angle = ((op.phase_increment(0x300) >> 10) + (modulator >> 1)) & 0x3FF; op.phase = ((angle << 10) - op.phase_increment(0x300)) & 0xFFFFF; step(&mut probe, 1); outputs.insert(name, probe.channels[0].operator2_previous); } assert_eq!(fm.channels[0].operator2_previous, outputs["current"]); assert_ne!(outputs["current"], outputs["previous"]); } #[test] fn special_mode_gives_each_operator_its_own_key_code() { // Spec 10.2, die (Sauraen, p31049: the key code is from "the FNUM for the // current operator, including the cases of the operators of voice 3 with their // own frequencies"). let mut fm = chip(); reg(&mut fm, 0x27, 0x40); frequency(&mut fm, 2, 0x100, 1); reg(&mut fm, 0xAD, (6 << 3) | 0x07); reg(&mut fm, 0xA9, 0xFF); let ops = &fm.channels[2].operators; assert!(ops[0].key_code == (6 << 2) | 3 && ops[3].key_code == 1 << 2); reg(&mut fm, 0x27, 0x00); let ops = &fm.channels[2].operators; assert!(ops[0].key_code == 1 << 2 && ops[0].f_number == 0x100); reg(&mut fm, 0x27, 0x80); assert_eq!(fm.channels[2].operators[0].f_number, 0x7FF); } #[test] fn channel_3s_own_frequencies_exist_in_part_i_only() { // Spec 3.3 (documented), 2.3. let mut fm = chip(); reg(&mut fm, 0x27, 0x40); reg_part(&mut fm, 0xAD, 0x3F, 1); reg_part(&mut fm, 0xA9, 0xFF, 1); assert_eq!(fm.channel3_f_number, [0, 0, 0]); assert!(fm.channels[5].operators.iter().all(|op| op.f_number == 0)); } #[test] fn dac_ninth_bit_is_2c_bit_3() { // Spec 13.3, die (Sauraen, p26996: "$2C:3 - Ch6 DAC ninth (least significant) // bit"). let mut fm = chip(); reg(&mut fm, 0x2B, 0x80); reg(&mut fm, 0x2A, 0x90); assert_eq!(step(&mut fm, 1)[0].0, 32); reg(&mut fm, 0x2C, 0x08); assert_eq!(step(&mut fm, 1)[0].0, 33); reg(&mut fm, 0x2C, 0xF7); assert_eq!(step(&mut fm, 1)[0].0, 32); } #[test] fn every_dac_write_one_sample_apart_is_heard() { // Spec 13.5, tier 1 (TmEE, p31027, p34262: "I could get unique samples out // every 144 cycles"). let mut fm = chip(); reg(&mut fm, 0x2B, 0x80); let mut heard = Vec::new(); for data in 0x80..0x90u8 { reg(&mut fm, 0x2A, data); heard.push(step(&mut fm, 1)[0].0); } let expected: Vec = (0x80..0x90).map(|data| 2 * (data - 0x80)).collect(); assert_eq!(heard, expected); } #[test] fn dac_level_is_about_one_fm_channels() { // Spec 13.3, contradiction C10, tier 1 (Nemesis, p5958: "the maximum DAC // output is slightly lower than that of an fm channel"). GManiac's factor of 4 // (p14109) would make it twice an FM channel's. let mut fm = chip(); reg(&mut fm, 0x2B, 0x80); reg(&mut fm, 0x2A, 0xFF); let left = step(&mut fm, 1)[0].0; assert!(200 < left && left <= 255); } #[test] fn the_quiet_sine_of_gmaniacs_test() { // Spec 15.3.2, tier 1 (TmEE, p12981, on a model 2: "There seems to be -2, -1, // 0 and +1 here"; p12991, GManiac, on a model 1: "the step -1 to 0 will be // HUGE"). One operator at a total level that leaves two bits of swing. let mut seen = BTreeMap::new(); for variant in [YM3438_ASIC, YM2612_DISCRETE] { let mut fm = chip_of(variant); voice( &mut fm, Voice { total_levels: [127, 127, 127, 0x3C], f_number: 0x100, block: 2, ..V }, ); key(&mut fm, 0, 0xF); let frames = step(&mut fm, 6000); assert!(fm .channel_values() .iter() .all(|v| [-2, -1, 0, 1].contains(v))); let idle = 5 * (variant.ladder_idle + 0); let levels: BTreeSet = frames.iter().map(|frame| frame.0 - idle).collect(); seen.insert(variant.name, levels.into_iter().collect::>()); } assert_eq!(seen["YM3438"], vec![-2, -1, 0, 1]); let levels = &seen["YM2612"]; let gaps: Vec = levels.windows(2).map(|p| p[1] - p[0]).collect(); let max_gap = *gaps.iter().max().unwrap(); assert!( levels.len() == 4 && max_gap > 3 && gaps.iter().position(|&g| g == max_gap).unwrap() == 1 ); } #[test] fn six_slots_are_summed_not_wrapped() { // Spec 15.1, tier 1 (Nemesis, p7516: each channel has its own time slot). The // core gives the sum of the six slots (decision T11): six channels at their // limit give six times one. let mut fm = chip(); for channel in 0..6 { voice( &mut fm, Voice { channel, algorithm: 7, total_levels: [0, 0, 0, 0], ..V }, ); key(&mut fm, channel, 0xF); } let frames = step(&mut fm, 2000); assert_eq!(frames.iter().map(|f| f.0).max().unwrap(), 6 * 255); assert_eq!(frames.iter().map(|f| f.0).min().unwrap(), 6 * -256); } #[test] fn psg_balance_is_the_boards_business() { // Spec 15.4 (Nemesis, p5960): "All 4 PSG channels at max combined is // equivalent to the maximum output of a single YM2612 channel at max". The core // exports the unit: one channel's maximum is 255 steps. assert_eq!(YM3438_ASIC.accumulator_max, 255); }