//! PROVISIONAL behaviours, pinned so that a change is seen. They prove nothing. //! //! Each is a decision (T numbers: docs/handoff-brief.md) or an emulator-tier default //! the provenance audit had to take (U21 to U26: docs/provenance-audit.md). The value //! is data or one marked rule, so a measurement later changes a value, not the //! structure. //! //! The reference's `tests/test_provisional.py`. Its T4 test assigns the module attribute //! `DELAYED_MODULATION`; the constant cannot be assigned here, so the test runs the chip //! through `run_with_delayed_modulation::()` (see `chip.rs`'s header). // The tests pin constant records as the Python does, with runtime assertions. #![allow(clippy::assertions_on_constants)] mod helpers; use helpers::*; use ym2612::chip::{DELAYED_MODULATION, ENVELOPE_COUNTER_SKIPS_ZERO, FNUM_LATCH_SHARED}; use ym2612::operator::{Operator, DECAY, RELEASE, SUSTAIN, SUSTAIN_LEVEL_15}; use ym2612::tables::{ vibrato_offset, ENVELOPE_INCREMENTS, ENVELOPE_SHIFT, LFO_DIVIDERS, TREMOLO_SHIFT, VIBRATO, }; use ym2612::{CLOCKS_PER_SAMPLE, YM2612_DISCRETE, YM3438_ASIC}; #[test] fn t1_vibrato_sums_each_bits_truncated_amount_in_11_bits() { // PROVISIONAL (T1, C16): Nemesis's method. The die reading suggests 12 bits. assert_eq!(vibrato_offset(0x7E0, 7, 0x1C), 48 + 24 + 12 + 6 + 3 + 1); assert_eq!(vibrato_offset(0x7E0, 7, 0x5C), -(48 + 24 + 12 + 6 + 3 + 1)); assert_eq!(vibrato_offset(0x01F, 7, 0x1C), 0); } #[test] fn u24_vibrato_tables_lower_columns_are_ymfms() { // PROVISIONAL, emulator tier (U24): only the last column is documented. assert_eq!(VIBRATO.map(|row| row[7]), [0, 1, 2, 3, 4, 6, 12, 24]); // documented assert_eq!(VIBRATO[4], [0, 0, 1, 2, 2, 2, 3, 4]); assert_eq!(VIBRATO[7], [0, 0, 8, 12, 16, 16, 20, 24]); assert!(VIBRATO .iter() .all(|row| row.windows(2).all(|p| p[0] <= p[1]))); } #[test] fn t2_lfo_divider_is_the_manuals() { // PROVISIONAL (T2, C14): 109, 78, ... samples a step; emulators use one fewer. assert_eq!(LFO_DIVIDERS, [109, 78, 72, 68, 63, 45, 9, 6]); let mut fm = chip(); reg(&mut fm, 0x22, 0x0F); step(&mut fm, 5); assert_eq!(fm.lfo_counter, 0); step(&mut fm, 1); assert_eq!(fm.lfo_counter, 1); } #[test] fn t3_status_addresses() { // PROVISIONAL (T3, C4): the discrete chip at address 0 only, the ASIC at all. assert_eq!(YM2612_DISCRETE.status_ports, [true, false, false, false]); assert_eq!(YM3438_ASIC.status_ports, [true, true, true, true]); } #[test] fn t4_delayed_modulation_is_on_and_one_constant_away() { // PROVISIONAL (T4, C11). assert!(DELAYED_MODULATION); fn heard() -> Vec { let mut fm = chip(); voice( &mut fm, Voice { algorithm: 0, total_levels: [10, 10, 10, 0], f_number: 0x300, block: 5, ..V }, ); key(&mut fm, 0, 0xF); // values(fm, 300), with the switch given explicitly let mut out = Vec::new(); for _ in 0..300 { fm.run_with_delayed_modulation::(CLOCKS_PER_SAMPLE); out.push(fm.channel_values()[0]); } out } let delayed = heard::(); assert_ne!(heard::(), delayed); } #[test] fn t5_accumulator_limit_is_plus_255_on_both() { // PROVISIONAL (T5, C8): GManiac measured +256, "AFAIR". assert!(YM2612_DISCRETE.accumulator_max == YM3438_ASIC.accumulator_max); assert_eq!(YM3438_ASIC.accumulator_max, 255); } #[test] fn t6_sustain_level_15_is_3e0() { // PROVISIONAL (T6, C9): the manual's 93 dB; Nemesis measured $3FF. assert_eq!(SUSTAIN_LEVEL_15, 0x3E0); let mut op = Operator::new(); op.envelope_phase = DECAY; op.attenuation = 0x3E0; op.sustain_level = 15; op.envelope_step(1); assert_eq!(op.envelope_phase, SUSTAIN); } #[test] fn t7_one_shared_f_number_latch_per_part() { // PROVISIONAL, emulator tier (T7, C12): a high byte written for one channel is // taken by the next low byte written in that part, whatever its channel. assert!(FNUM_LATCH_SHARED); let mut fm = chip(); reg(&mut fm, 0xA4, 0x22); // high byte for channel 1 reg(&mut fm, 0xA1, 0x10); // low byte for channel 2 takes it assert_eq!((fm.channels[1].block, fm.channels[1].f_number), (4, 0x210)); reg(&mut fm, 0xA0, 0x10); // and channel 1 still gets it: the latch is not cleared assert_eq!((fm.channels[0].block, fm.channels[0].f_number), (4, 0x210)); reg_part(&mut fm, 0xA4, 0x09, 1); assert_eq!(fm.fnum_latch[0], 0x22); // part I's latch is its own } #[test] fn t8_test_registers_are_stored_and_only_2c_bit_3_acts() { // PROVISIONAL (T8). let mut fm = chip(); voice(&mut fm, V); key(&mut fm, 0, 0xF); reg(&mut fm, 0x21, 0xFF); reg(&mut fm, 0x2C, 0xF7); let mut twin = chip(); voice(&mut twin, V); key(&mut twin, 0, 0xF); assert_eq!(step(&mut fm, 200), step(&mut twin, 200)); assert_eq!((fm.test_21, fm.test_2c), (0xFF, 0xF7)); } #[test] fn t9_a_write_is_heard_from_the_next_sample_wherever_it_falls() { // PROVISIONAL (T9, U1): no position inside the sample. let (mut early, mut late) = (chip(), chip()); for (fm, clocks) in [(&mut early, 1), (&mut late, 143)] { voice(fm, V); fm.run(clocks); key(fm, 0, 0xF); fm.run(144 * 20 - clocks); } assert_eq!(early.take_samples(), late.take_samples()); } #[test] fn t10_ladder_constants_are_ymfms_and_the_asic_has_none() { // PROVISIONAL, emulator tier (T10, U12): +4, -3, and +4 for a slot panned off. // Not measured and not fitted. Two hardware reports of the mid-scale step alone // are smaller. let discrete = YM2612_DISCRETE; assert_eq!( ( discrete.ladder_positive, discrete.ladder_negative, discrete.ladder_idle ), (4, -3, 4) ); assert!(!YM3438_ASIC.ladder); assert_eq!(step(&mut chip_of(discrete), 1), vec![(24, 24)]); // six silent slots assert_eq!(step(&mut chip_of(YM3438_ASIC), 1), vec![(0, 0)]); let mut fm = chip_of(discrete); reg(&mut fm, 0x2B, 0x80); for (data, expected) in [(0x80, 4), (0x7F, -2 - 3), (0x81, 2 + 4)] { reg(&mut fm, 0x2A, data); assert_eq!(step(&mut fm, 1)[0].0 - 20, expected); } reg_part(&mut fm, 0xB6, 0x00, 1); assert_eq!(step(&mut fm, 1), vec![(24, 24)]); } #[test] fn t12_u26_lfo_off_holds_the_counter_at_0_so_tremolo_rests_at_its_maximum() { // PROVISIONAL, emulator tier (T12, U26, C15). assert_eq!(TREMOLO_SHIFT, [8, 3, 1, 0]); let mut fm = chip(); voice(&mut fm, V); reg(&mut fm, 0x22, 0x0F); step(&mut fm, 300); assert_ne!(fm.lfo_counter, 0); reg(&mut fm, 0x22, 0x07); step(&mut fm, 1); assert_eq!(fm.lfo_counter, 0); op_reg(&mut fm, 0, 3, 0x60, 0x80); key(&mut fm, 0, 0xF); let mut quiet = Operator::new(); quiet.envelope_phase = SUSTAIN; quiet.attenuation = 0; for (depth, rest) in [(0u8, 0), (1, 15), (2, 63), (3, 126)] { reg(&mut fm, 0xB4, 0xC0 | (depth << 4)); // What the chip plays is one operator attenuated by the resting amount. quiet.phase = 0; let _expected: Vec = (0..2000).map(|_| quiet.clock(0x269 << 3, 0, 0)).collect(); quiet.attenuation = rest; let expected: Vec = (0..2000).map(|_| quiet.clock(0x269 << 3, 0, 0)).collect(); let heard = values(&mut fm, 2000, 0); assert_eq!( *heard.iter().max().unwrap(), *expected.iter().max().unwrap() >> 5, "{depth}" ); } } #[test] fn t13_envelope_counter_is_12_bits_and_skips_0() { // PROVISIONAL (T13, U3, U4): and every operator updates on the same sample. assert!(ENVELOPE_COUNTER_SKIPS_ZERO); let mut fm = chip(); fm.envelope_counter = 4094; step(&mut fm, 3); assert_eq!(fm.envelope_counter, 4095); step(&mut fm, 3); assert_eq!(fm.envelope_counter, 1); } #[test] fn t14_reset_state() { // PROVISIONAL (T14, U14): registers 0, panning on, release at $3FF. let mut fm = chip(); voice(&mut fm, Voice { feedback: 5, ..V }); reg(&mut fm, 0x22, 0x0F); reg(&mut fm, 0x27, 0xFF); reg(&mut fm, 0x2B, 0x80); key(&mut fm, 0, 0xF); step(&mut fm, 500); fm.reset_line(0); fm.reset_line(1); let fresh = chip(); assert!(fm.envelope_counter == 0 && fm.lfo_counter == 0 && fm.status_flags == 0); for (mine, new) in fm.channels.iter().zip(fresh.channels.iter()) { assert_eq!((mine.left, mine.right, mine.feedback), (1, 1, 0)); for (a, b) in mine.operators.iter().zip(new.operators.iter()) { assert_eq!(a, b); assert_eq!((a.envelope_phase, a.attenuation), (RELEASE, 0x3FF)); } } } #[test] fn t15_key_on_channel_values_3_and_7_are_ignored() { // PROVISIONAL (T15, U2). let mut fm = chip(); reg(&mut fm, 0x28, 0xF3); reg(&mut fm, 0x28, 0xF7); assert!(fm .channels .iter() .all(|channel| channel.operators.iter().all(|op| op.key_request == 0))); } #[test] fn t16_timer_bs_prescaler_runs_free_of_the_load_bit() { // PROVISIONAL, emulator tier (T16, U9): a first period can be up to 15 samples // short. let mut fm = chip(); step(&mut fm, 10); // the prescaler is at 10 of 16 reg(&mut fm, 0x26, 0xFF); reg(&mut fm, 0x27, 0x0A); let mut count = 0; while status(&fm) & 2 == 0 { step(&mut fm, 1); count += 1; } assert_eq!(count, 6); } #[test] fn u21_envelope_table_is_ymfms() { // PROVISIONAL, emulator tier (U21): pinned whole by its sums. The prose facts // it agrees with are in tests/measured.rs. let sum = |rate: usize| ENVELOPE_INCREMENTS[rate].iter().sum::(); assert_eq!( (0..8).map(sum).collect::>(), vec![0, 0, 4, 4, 4, 4, 6, 6] ); for rate in 8..48 { assert_eq!(sum(rate), 4 + (rate as i32 & 3)); } for rate in 48..60 { assert_eq!(sum(rate), (8 + 2 * (rate as i32 & 3)) << ((rate - 48) >> 2)); } assert_eq!(ENVELOPE_INCREMENTS[9], [0, 1, 0, 1, 1, 1, 0, 1]); assert_eq!(ENVELOPE_INCREMENTS[49], [1, 1, 1, 2, 1, 1, 1, 2]); assert_eq!(ENVELOPE_SHIFT[..8], [11, 11, 11, 11, 10, 10, 10, 10]); assert_eq!(ENVELOPE_SHIFT[44..], [0; 20]); } #[test] fn u22_attack_curve_is_ymfms() { // PROVISIONAL, emulator tier (U22): attenuation += (~attenuation * step) >> 4. let mut op = Operator::new(); (op.envelope_phase, op.attenuation, op.attack_rate) = (0, 0x3FF, 26); // rate 52: steps of 2 let mut seen = Vec::new(); for counter in 1..6 { op.envelope_step(counter); seen.push(op.attenuation); } assert_eq!(seen, vec![0x37F, 0x30F, 0x2AD, 0x257, 0x20C]); } #[test] fn u23_key_off_while_inverted_carries_the_heard_level_into_release() { // PROVISIONAL, emulator tier (U23). let mut op = Operator::new(); (op.ssg, op.envelope_phase, op.attenuation, op.key_state) = (0x0C, DECAY, 0x040, 1); let heard = op.level(0); op.set_key(0); assert!(op.envelope_phase == RELEASE && op.attenuation == heard && heard == 0x1C0); assert_eq!(op.level(0), heard); } #[test] fn u25_channel_3_mode_11_gives_separate_frequencies_without_csm() { // PROVISIONAL, emulator tier (U25). let mut fm = chip(); reg(&mut fm, 0xAD, 0x12); reg(&mut fm, 0xA9, 0x34); reg(&mut fm, 0x27, 0xC0); assert_eq!(fm.channels[2].operators[0].f_number, 0x234); } #[test] fn choice_data_written_through_the_other_part_reaches_global_registers() { // PROVISIONAL (C13): Nemesis's one latch is taken for the $2x registers too. let mut fm = chip(); fm.write(0, 0x2B); fm.write(3, 0x80); assert_eq!(fm.dac_enabled, 1); }