// SPDX-License-Identifier: MIT //! Rung 2: the manufacturers' documents (docs/validation.md). //! //! S2 is Yamaha's YM3438 application manual (April 1990), cited by printed page; S1 //! the YM2612 manual from the Sega binder (cited, not copied); S4 the YM2608 manual. //! Where S1 and S2 disagree the test says which is followed and why. //! //! The reference's `tests/test_manuals.py`; parametrized cases are loops. Python's `round()` //! rounds half to even: `round_ties_even` here. // Loops kept in the reference's form (`for key_code in range(32)`). #![allow(clippy::needless_range_loop)] mod helpers; use helpers::*; use std::collections::BTreeSet; use ym2612::operator::{Operator, ATTACK, DECAY, RELEASE, SUSTAIN}; use ym2612::tables::{vibrato_offset, DETUNE, KEY_CODE_NOTE, LFO_DIVIDERS}; use ym2612::{YM2612, YM2612_DISCRETE}; const MANUAL_CLOCK: f64 = 8_000_000.0; // the manuals' figures assume 8 MHz // -- register map (S2 pages 9 to 16, table 2.1) ---------------------------------- #[test] fn operator_registers_decode_every_field() { let mut fm = chip(); for (base, value) in [ (0x30, 0x7F), (0x40, 0xFF), (0x50, 0xFF), (0x60, 0xFF), (0x70, 0xFF), (0x80, 0xFF), (0x90, 0x0F), ] { op_reg(&mut fm, 0, 0, base, value); } let op = &fm.channels[0].operators[0]; assert_eq!((op.detune, op.multiple, op.total_level), (7, 15, 127)); assert_eq!( (op.key_scale, op.attack_rate, op.tremolo, op.decay_rate), (3, 31, 1, 31) ); assert_eq!( (op.sustain_rate, op.sustain_level, op.release_rate, op.ssg), (31, 15, 15, 15) ); } #[test] fn register_addresses_run_operators_1_3_2_4_and_both_parts() { // S2 table 2.1, S4 table 2-2: address bits 3-2 choose 1, 3, 2, 4; bits 1-0 the // channel; part II holds channels 4 to 6. let mut fm = chip(); for part in 0..2u8 { for low in 0..16u8 { reg_part(&mut fm, 0x40 + low, 1 + low + 16 * part, part); } } for part in 0..2 { for index in 0..3 { let levels: Vec = fm.channels[index + 3 * part] .operators .iter() .map(|op| op.total_level) .collect(); let base = 1 + index as i32 + 16 * part as i32; assert_eq!(levels, vec![base, base + 8, base + 4, base + 12]); } } } #[test] fn channel_registers_decode_every_field() { let mut fm = chip(); reg_part(&mut fm, 0xB1, 0x3D, 1); reg_part(&mut fm, 0xB5, 0xB5, 1); frequency(&mut fm, 4, 0x555, 5); let channel = &fm.channels[4]; assert_eq!((channel.feedback, channel.algorithm), (7, 5)); assert_eq!( ( channel.left, channel.right, channel.tremolo_depth, channel.vibrato_depth ), (1, 0, 3, 5) ); assert_eq!((channel.f_number, channel.block), (0x555, 5)); } #[test] fn global_registers_decode_every_field() { let mut fm = chip(); reg(&mut fm, 0x22, 0x0D); reg(&mut fm, 0x24, 0xAB); reg(&mut fm, 0x25, 0x02); reg(&mut fm, 0x26, 0xCD); reg(&mut fm, 0x27, 0x8F); reg(&mut fm, 0x2A, 0x12); reg(&mut fm, 0x2B, 0x80); assert_eq!((fm.lfo_enabled, fm.lfo_rate), (1, 5)); assert_eq!( (fm.timer_a_register, fm.timer_b_register), ((0xAB << 2) | 2, 0xCD) ); assert_eq!( (fm.channel3_mode, fm.timer_enable, fm.timer_load), (2, 3, 3) ); assert_eq!((fm.dac_data, fm.dac_enabled), (0x12, 1)); } #[test] fn key_register_chooses_channels_0_1_2_and_4_5_6() { // S1, S2: $28 bits 2-0, values 0, 1, 2 and 4, 5, 6; bits 7-4 operators 4 to 1. let mut fm = chip(); for (channel, select) in [0, 1, 2, 4, 5, 6].into_iter().enumerate() { reg(&mut fm, 0x28, 0x50 | select); let requests: Vec = fm.channels[channel] .operators .iter() .map(|op| op.key_request) .collect(); assert_eq!(requests, vec![1, 0, 1, 0]); } } // -- phase generator (S2 pages 23 to 25) ----------------------------------------- #[test] fn f_number_formula_reproduces_the_manuals_note_table() { // S2 printed pages 23 and 24: F-Number = (144 x f x 2^20 / fM) / 2^(B-1), block // 4 at 8 MHz, six of the table's rows (A4 = 1038.1 is the worked example). The // model's phase increment for the rounded F-number gives the note back. for (note_hz, printed) in [ (277.2, 654.0), (293.7, 692.9), (440.0, 1038.1), (466.2, 1099.9), (493.9, 1165.3), (523.3, 1234.6), ] { let f_number = 144.0 * note_hz * 2f64.powi(20) / MANUAL_CLOCK / 2f64.powi(4 - 1); assert!((f_number - printed).abs() < 0.06); let mut op = Operator::new(); op.block = 4; op.multiple = 1; let played = op.phase_increment(printed.round_ties_even() as i32) as f64 * MANUAL_CLOCK / (144.0 * 2f64.powi(20)); assert!((played - note_hz).abs() < MANUAL_CLOCK / (144.0 * 2f64.powi(20)) * 8.0); // one F-number step } } #[test] fn key_code_for_all_2048_f_numbers() { // S2 page 24, S4 2-4-1 B: N4 = F11; N3 = F11(F10 + F9 + F8) + !F11 F10 F9 F8. let mut fm = chip(); for f_number in 0..2048 { let (f11, f10, f9, f8) = ( (f_number >> 10) & 1, (f_number >> 9) & 1, (f_number >> 8) & 1, (f_number >> 7) & 1, ); let n3 = if (f11 != 0 && (f10 != 0 || f9 != 0 || f8 != 0)) || (f11 == 0 && f10 != 0 && f9 != 0 && f8 != 0) { 1 } else { 0 }; frequency(&mut fm, 0, f_number, 5); assert_eq!( fm.channels[0].operators[0].key_code, (5 << 2) | (f11 << 1) | n3 ); } assert_eq!( KEY_CODE_NOTE, [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3] ); } /// The distinct values printed in S2's table 2.4, in millihertz. const PRINTED_DETUNE_MILLIHERTZ: [i64; 20] = [ 0, 53, 106, 159, 212, 264, 317, 370, 423, 476, 529, 582, 635, 688, 741, 846, 899, 1005, 1058, 1164, ]; #[test] fn detune_table_in_hertz_at_the_manuals_clock() { // S2 table 2.4 (printed page 25), at fM = 7.9872 MHz: every amount the model // adds, turned into hertz, is a value the table prints, and the corner entries // are where the table has them. let unit = 7_987_200.0 / (144.0 * 2f64.powi(20)); for magnitude in [1, 2, 3] { for key_code in 0..32 { let millihertz = (DETUNE[magnitude][key_code] as f64 * unit * 1000.0).round_ties_even() as i64; assert!( PRINTED_DETUNE_MILLIHERTZ.contains(&millihertz), "{magnitude} {key_code}" ); } } assert_eq!([1, 2, 3].map(|m| DETUNE[m][0]), [0, 1, 2]); // 0.000, 0.053, 0.106 assert_eq!([1, 2, 3].map(|m| DETUNE[m][31]), [8, 16, 22]); // 0.423, 0.846, 1.164 assert_eq!([1, 2, 3].map(|m| DETUNE[m][16]), [2, 5, 8]); // block 4 note 0 assert!(DETUNE .iter() .all(|row| row.windows(2).all(|p| p[0] <= p[1]))); } #[test] fn detune_register_is_a_sign_and_a_magnitude() { // S2 table 2.4: values 0 to 7 mean 0, 1, 2, 3, 0, -1, -2, -3. let mut fm = chip(); frequency(&mut fm, 0, 0x400, 4); let op = &mut fm.channels[0].operators[0]; op.multiple = 1; let plain = (0x400 << 4) >> 1; let mut amounts = Vec::new(); for detune in 0..8 { op.detune = detune; amounts.push(op.phase_increment(0x400) - plain); } let row: Vec = (0..4).map(|m| DETUNE[m][op.key_code as usize]).collect(); assert_eq!( amounts, vec![0, row[1], row[2], row[3], 0, -row[1], -row[2], -row[3]] ); } #[test] fn multiple_table() { // S2 table 2.3: 0 is one half, 1 to 15 are themselves. let mut op = Operator::new(); op.block = 4; let base = (0x200 << 4) >> 1; for multiple in 0..16 { op.multiple = multiple; assert_eq!( op.phase_increment(0x200), if multiple == 0 { base / 2 } else { base * multiple } ); } } // -- envelope generator (S2 pages 26 to 29) -------------------------------------- #[test] fn rate_is_2r_plus_rks_zero_for_r_zero_and_at_most_63() { // S2 page 28: Rate = 2R + Rks; R = 0 gives 0; the limit is 63. let mut op = Operator::new(); for key_scale in 0..4 { for key_code in 0..32 { op.key_scale = key_scale; op.key_code = key_code; assert_eq!(op.rate(0), 0); for register in 1..32 { assert_eq!( op.rate(register), 63.min(2 * register + (key_code >> (3 - key_scale))) ); } } } } #[test] fn key_scaling_table() { // S2 table 2.6: Rks is the key code divided by 8, 4, 2, 1 for KS 0 to 3: all // 128 entries, through the register. let mut fm = chip(); for key_scale in 0..4u8 { op_reg(&mut fm, 0, 0, 0x50, (key_scale << 6) | 1); for block in 0..8 { for f_top in [0x000, 0x380, 0x400, 0x480] { frequency(&mut fm, 0, f_top, block); let op = &fm.channels[0].operators[0]; assert_eq!( op.rate(1) - 2, op.key_code / [8, 4, 2, 1][key_scale as usize] ); } } } } #[test] fn total_level_is_three_quarters_of_a_db_and_sustain_level_3_db_a_step() { // S2 table 2.5: total level 0.75 dB a step (8 envelope units); sustain level // 3 dB a step ($20 units), where a unit is 96 dB / 1024. let mut op = Operator::new(); op.envelope_phase = SUSTAIN; op.attenuation = 0; for total_level in 0..128 { op.total_level = total_level; assert_eq!( (op.level(0) * 96) as f64 / 1024.0, total_level as f64 * 0.75 ); } for sustain_level in 0..15 { let mut op = Operator::new(); op.envelope_phase = DECAY; op.attenuation = sustain_level * 0x20; op.sustain_level = sustain_level; op.envelope_step(1); assert_eq!(op.envelope_phase, SUSTAIN); assert_eq!( (op.attenuation * 96) as f64 / 1024.0, sustain_level as f64 * 3.0 ); } } #[test] fn attack_rate_31_reaches_the_total_level_at_once() { // S2 figure 2.4's note, S1: with SSG-EG "AR is fixed at 1F"; p5650 calls $1F // "infinity". Key scale 0, lowest note: the rate is 62. let mut fm = chip(); voice( &mut fm, Voice { f_number: 0, block: 0, ..V }, ); key(&mut fm, 0, 0xF); step(&mut fm, 1); assert_eq!(fm.channels[0].operators[3].attenuation, 0); } /// The carrier's output level, sample by sample, of one SSG-EG note. fn envelope_shape(ssg_bits: u8, samples: usize) -> Vec { let mut fm = chip(); voice(&mut fm, V); op_reg(&mut fm, 0, 3, 0x60, 22); // decay op_reg(&mut fm, 0, 3, 0x70, 22); // sustain rate op_reg(&mut fm, 0, 3, 0x80, 0x0F); op_reg(&mut fm, 0, 3, 0x90, 0x08 | ssg_bits); key(&mut fm, 0, 0xF); let mut trace = Vec::new(); for _ in 0..samples { step(&mut fm, 1); trace.push(fm.channels[0].operators[3].level(0)); } trace } /// Classify a trace of attenuation: count the falling and rising ramps and say /// where it ends. A 'fall' is the sound getting quieter. fn describe(trace: &[i32]) -> (Vec<&'static str>, &'static str) { let mut ramps = Vec::new(); let mut direction = 0; for pair in trace.windows(2) { let (before, after) = (pair[0], pair[1]); if (after - before).abs() > 0x100 { direction = 0; // a jump between ramps continue; } if after > before && direction != 1 { direction = 1; ramps.push("fall"); } else if after < before && direction != -1 { direction = -1; ramps.push("rise"); } } let last = *trace.last().unwrap(); let end = if last >= 0x3F0 { "silent" } else if last == 0 { "loud" } else { "moving" }; (ramps, end) } #[test] fn the_eight_ssg_eg_shapes() { // S2 figure 2.4 (printed page 29), with the attack rate at 31 as it asks. for (bits, first, repeats, end) in [ (0b000, "fall", true, "moving"), // repeated falling ramps (0b001, "fall", false, "silent"), // one ramp, then silence (0b010, "fall", true, "moving"), // down, up, down, up (0b011, "fall", false, "loud"), // one ramp, then held loud (0b100, "rise", true, "moving"), // repeated rising ramps (0b101, "rise", false, "loud"), // one rise, then held loud (0b110, "rise", true, "moving"), // up, down, up, down (0b111, "rise", false, "silent"), // one rise, then silence ] { let (ramps, ending) = describe(&envelope_shape(bits, 6000)); assert_eq!(ramps[0], first); assert_eq!(ramps.len() > 3, repeats); if !repeats { assert!(ramps.len() == 1 && ending == end); } let alternating = bits & 0b010 != 0 && repeats; let kinds: BTreeSet<&str> = ramps.iter().copied().collect(); assert_eq!(kinds.len() == 2, alternating); } } // -- algorithms and feedback (S2 figure 2.2, table 2.2) -------------------------- const CARRIERS: [&[usize]; 8] = [ &[3], &[3], &[3], &[3], &[1, 3], &[1, 2, 3], &[1, 2, 3], &[0, 1, 2, 3], ]; const MODULATIONS: [&[(usize, usize)]; 8] = [ &[(0, 1), (1, 2), (2, 3)], &[(0, 2), (1, 2), (2, 3)], &[(0, 3), (1, 2), (2, 3)], &[(0, 1), (1, 3), (2, 3)], &[(0, 1), (2, 3)], &[(0, 1), (0, 2), (0, 3)], &[(0, 1)], &[], ]; fn heard_with(algorithm: u8, loud: &BTreeSet, keyed: &BTreeSet) -> Vec { let mut fm = chip(); let levels: [u8; 4] = std::array::from_fn(|operator| if loud.contains(&operator) { 0 } else { 127 }); voice( &mut fm, Voice { algorithm, total_levels: levels, ..V }, ); key(&mut fm, 0, keyed.iter().map(|&op| 1u8 << op).sum()); values(&mut fm, 400, 0) } fn ops(items: &[usize]) -> BTreeSet { items.iter().copied().collect() } #[test] fn algorithm_carriers() { // S2 figure 2.2: an operator alone is heard only if it is a carrier. for algorithm in 0..8 { for operator in 0..4 { let heard = heard_with(algorithm as u8, &ops(&[operator]), &ops(&[operator])); assert_eq!( *heard.iter().max().unwrap() > 100, CARRIERS[algorithm].contains(&operator), "{algorithm} {operator}" ); } } } #[test] fn algorithm_connections() { // S2 figure 2.2: adding operator a changes what is heard of operator b exactly // when a modulates b directly or through another (the path is opened by keying // everything on the way). for algorithm in 0..8 { let mut reach: BTreeSet<(usize, usize)> = MODULATIONS[algorithm].iter().copied().collect(); let mut grew = true; while grew { grew = false; for (a, b) in reach.clone() { for &(c, d) in MODULATIONS[algorithm] { if b == c && !reach.contains(&(a, d)) { reach.insert((a, d)); grew = true; } } } } let carriers = ops(CARRIERS[algorithm]); for &carrier in CARRIERS[algorithm] { let alone = heard_with(algorithm as u8, &ops(&[carrier]), &ops(&[carrier])); for other in 0..4 { if other == carrier || carriers.contains(&other) { continue; } let between: BTreeSet = (0..4).filter(|o| !carriers.contains(o)).collect(); let mut loud = between.clone(); loud.insert(carrier); let mut keyed_both = loud.clone(); keyed_both.insert(other); let both = heard_with(algorithm as u8, &loud, &keyed_both); let mut keyed_silent: BTreeSet = between.clone(); keyed_silent.remove(&other); keyed_silent.insert(carrier); let silent_other = heard_with(algorithm as u8, &loud, &keyed_silent); let changed = both != silent_other; assert_eq!( changed, reach.contains(&(other, carrier)), "{algorithm} {other} {carrier}" ); } assert!(*alone.iter().max().unwrap() > 100); } } } #[test] fn feedback_levels_are_the_manuals() { // S2 table 2.2: levels 1 to 7 are pi/16, pi/8, pi/4, pi/2, pi, 2 pi, 4 pi: the // peak phase offset of operator 1 on itself at full output, where 1,024 phase // units are 2 pi. (The shift that gives them is arithmetic from these levels: // unknown U7.) let peak = 8168 + 8168; for (feedback, fraction) in [ (1, 1.0 / 16.0), (2, 1.0 / 8.0), (3, 1.0 / 4.0), (4, 1.0 / 2.0), (5, 1.0), (6, 2.0), (7, 4.0), ] { let offset = peak >> (10 - feedback); let pi = std::f64::consts::PI; assert!((offset as f64 * 2.0 * pi / 1024.0 / (fraction * pi) - 1.0).abs() < 0.04); // 31 for 32 } let mut fm = chip(); voice( &mut fm, Voice { algorithm: 7, total_levels: [0, 127, 127, 127], feedback: 7, ..V }, ); key(&mut fm, 0, 0xF); let with_feedback = values(&mut fm, 300, 0); let mut fm = chip(); voice( &mut fm, Voice { algorithm: 7, total_levels: [0, 127, 127, 127], feedback: 0, ..V }, ); key(&mut fm, 0, 0xF); assert_ne!(values(&mut fm, 300, 0), with_feedback); } // -- LFO (S2 printed page 31) ---------------------------------------------------- #[test] fn lfo_rates() { // S2 page 31: the eight rates at 8 MHz. The model's divider is the manual's // (decision T2); emulators use one fewer, which is 0.9 % to 20 % faster. The // test states both and holds the model to the manual's within 0.5 %. for (rate, hertz) in [3.98, 5.56, 6.02, 6.37, 6.88, 9.63, 48.1, 72.2] .into_iter() .enumerate() { let mut fm = chip(); reg(&mut fm, 0x22, 0x08 | rate as u8); let mut wraps = 0; let mut last = 0; let samples = 128 * LFO_DIVIDERS[rate] * 3; for _ in 0..samples { step(&mut fm, 1); if fm.lfo_counter < last { wraps += 1; } last = fm.lfo_counter; } assert_eq!(wraps, 3); let model_hertz = MANUAL_CLOCK / 144.0 / (128 * LFO_DIVIDERS[rate]) as f64; let other_hertz = MANUAL_CLOCK / 144.0 / (128 * (LFO_DIVIDERS[rate] - 1)) as f64; assert!((model_hertz / hertz - 1.0).abs() < 0.005); assert!((other_hertz / hertz - 1.0).abs() > (model_hertz / hertz - 1.0).abs()); } } #[test] fn tremolo_depths() { // S2 page 31: 0, 1.4, 5.9 and 11.8 dB, heard as the swing of a steady output // (one operator held at its peak) over the LFO's cycle. for (depth, decibels) in [(0u8, 0.0), (1, 1.4), (2, 5.9), (3, 11.8)] { let mut fm = chip(); voice( &mut fm, Voice { f_number: 0, block: 0, ..V }, ); op_reg(&mut fm, 0, 3, 0x60, 0x80); reg(&mut fm, 0xB4, 0xC0 | (depth << 4)); reg(&mut fm, 0x22, 0x0F); key(&mut fm, 0, 0xF); step(&mut fm, 1); fm.channels[0].operators[3].phase = 0x40000; // a quarter turn, and it stays there let heard = values(&mut fm, 128 * 6 + 10, 0); let (max, min) = (*heard.iter().max().unwrap(), *heard.iter().min().unwrap()); assert_eq!(max, 255); let swing = 20.0 * (max as f64 / min as f64).log10(); assert!((swing - decibels).abs() < 0.15, "{depth}"); } } #[test] fn vibrato_depths() { // S2 page 31: plus and minus 3.4 to 80 cents, as the peak deviation of an // F-number of $400 (the table's last column is this row of the manual). for (depth, cents) in [ (1, 3.4), (2, 6.7), (3, 10.0), (4, 14.0), (5, 20.0), (6, 40.0), (7, 80.0), ] { let f_number = 0x400; let offsets: Vec = (0..128) .map(|counter| vibrato_offset(f_number, depth, counter)) .collect(); let (max, min) = ( *offsets.iter().max().unwrap(), *offsets.iter().min().unwrap(), ); assert_eq!(max, -min); let peak_cents = 1200.0 * ((f_number + max) as f64 / f_number as f64).log2(); assert!((peak_cents / cents - 1.0).abs() < 0.05); // whole F-number steps: 13.5 for 14 assert_eq!(offsets[0], 0); } } // -- timers, status, DAC (S2 pages 11 to 15) ------------------------------------- #[test] fn timer_formulas_are_the_ym3438_manuals_not_the_ym2612_manuals() { // S2 page 13: tA = 144 (1024 - NA) / fM, tB = 2304 (256 - NB) / fM: at 8 MHz, // 18 microseconds to 18.4 ms, and steps of 288 microseconds. S1 and S4 print 72 // and 1152 (contradiction C1): S2 is followed because Nemesis measured it // (p5680). assert_eq!( (144.0 * (1024.0 - 1023.0) / MANUAL_CLOCK * 1e6).round_ties_even(), 18.0 ); assert_eq!( (144.0 * 1024.0 / MANUAL_CLOCK * 1e6).round_ties_even(), 18432.0 ); assert_eq!((2304.0 / MANUAL_CLOCK * 1e6).round_ties_even(), 288.0); for register in [0, 0x155, 0x3FE] { let mut fm = chip(); reg(&mut fm, 0x24, (register >> 2) as u8); reg(&mut fm, 0x25, (register & 3) as u8); reg(&mut fm, 0x27, 0x05); let mut count = 0; while status(&fm) & 1 == 0 { step(&mut fm, 1); count += 1; } assert_eq!(count, 1024 - register); } } #[test] fn timer_flags_follow_the_enable_bits_and_clear_by_the_reset_bits() { // S1, S2 page 14: a flag is set on overflow if its enable bit is set; writing // 1 to its reset bit clears it; the reset bit does not stay set. let mut fm = chip(); reg(&mut fm, 0x24, 0xFF); reg(&mut fm, 0x26, 0xFF); reg(&mut fm, 0x27, 0x03); // loaded, not enabled step(&mut fm, 40); assert_eq!(status(&fm), 0); reg(&mut fm, 0x27, 0x0F); step(&mut fm, 40); assert_eq!(status(&fm), 0x03); reg(&mut fm, 0x27, 0x1F); assert_eq!(status(&fm) & 0x7F, 0x02); step(&mut fm, 40); assert_eq!(status(&fm), 0x03); // the reset bit did not stay set reg(&mut fm, 0x27, 0x2F); assert_eq!(status(&fm) & 0x7F, 0x01); } #[test] fn status_byte_layout() { // S1 printed page 11, S2 page 11: bit 7 BUSY, bit 1 flag B, bit 0 flag A. let mut fm = chip(); reg(&mut fm, 0x24, 0xFF); reg(&mut fm, 0x25, 0x03); reg(&mut fm, 0x27, 0x05); assert_eq!(status(&fm), 0x80); step(&mut fm, 2); assert_eq!(status(&fm), 0x01); } #[test] fn dac_is_eight_bits_offset_binary_and_replaces_channel_6() { // S2 page 15: $2B bit 7 switches channel 6 to the DAC; $2A is offset binary, // bit 7 the most significant. Sega's overview: only channel 6's panning acts. let mut fm = chip(); reg(&mut fm, 0x2B, 0x80); let mut seen = Vec::new(); for data in [0x00, 0x7F, 0x80, 0x81, 0xFF] { reg(&mut fm, 0x2A, data); seen.push(step(&mut fm, 1)[0].0); } let mut sorted = seen.clone(); sorted.sort(); assert!(seen == sorted && seen[2] == 0 && seen[0] == -seen[4] - 2); reg_part(&mut fm, 0xB6, 0x80, 1); assert_eq!(step(&mut fm, 1)[0], (254, 0)); reg(&mut fm, 0x2B, 0x00); assert_eq!(step(&mut fm, 1)[0], (0, 0)); } #[test] fn panning_bits() { // S1, S2: $B4 bit 7 left, bit 6 right. for (bits, (left, right)) in [ (0xC0, (1, 1)), (0x80, (1, 0)), (0x40, (0, 1)), (0x00, (0, 0)), ] { let mut fm = chip(); voice(&mut fm, V); reg(&mut fm, 0xB4, bits); key(&mut fm, 0, 0xF); let frames = step(&mut fm, 300); assert_eq!(frames.iter().any(|f| f.0 != 0), left != 0); assert_eq!(frames.iter().any(|f| f.1 != 0), right != 0); } } #[test] fn a_channel_panned_off_is_faintly_audible_on_the_discrete_chip() { // S1 section 2-7-2: a channel with both bits clear "may be faintly audible, // depending on the voice parameters and total level". The model gives an idle // slot a fixed level, so only the presence of an offset is checked here (its size // is provisional: tests/provisional.rs). let mut fm = chip_of(YM2612_DISCRETE); voice(&mut fm, V); reg(&mut fm, 0xB4, 0x00); key(&mut fm, 0, 0xF); assert!(step(&mut fm, 50).iter().all(|&frame| frame != (0, 0))); } #[test] fn total_level_advice_about_overflow() { // S2 page 42: algorithms 0 to 3 need no care; algorithm 7 with all four // operators at total level 0 overflows (here: sits on the limit). fn at_limit(heard: &[i32]) -> usize { heard.iter().filter(|&&v| v == 255 || v == -256).count() } for algorithm in 0..4 { let mut fm = chip(); voice( &mut fm, Voice { algorithm, total_levels: [0, 0, 0, 0], ..V }, ); key(&mut fm, 0, 0xF); let heard = values(&mut fm, 3000, 0); assert!(at_limit(&heard) < 600); // peaks only } 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, 3000, 0); assert!(at_limit(&heard) > 1500); } #[test] fn reset_pin_clears_what_was_playing() { // S1, S2 page 37: /IC resets the chip. let mut fm: YM2612 = chip(); voice(&mut fm, V); key(&mut fm, 0, 0xF); step(&mut fm, 100); fm.reset_line(0); fm.reset_line(1); let op = &fm.channels[0].operators[3]; assert_eq!( (op.envelope_phase, op.attenuation, op.key_state), (RELEASE, 0x3FF, 0) ); assert_eq!(step(&mut fm, 20), vec![(0, 0); 20]); assert_eq!(ATTACK, 0); }