// SPDX-License-Identifier: MIT //! Behaviours no source settles: what the core does, so that a change is seen. //! //! Nothing here is evidence about hardware. Each test pins either a PROVISIONAL //! value (a decision in docs/provenance.md, held as data on the variant so a //! measurement changes one value) or a CHOICE (nothing speaks to it). If a recording //! overturns one, the test changes with the value. //! //! A transcription of sn76489-python's `tests/test_provisional.py` at f8157f4. //! Parametrized tests are loops; `dataclasses.replace(variant, f=x)` is //! `PSGVariant { f: x, ..variant }`. mod common; use common::{bits, changes, left, rises, solo, write_tone, ALL, SEGA_PARTS}; use sn76489::{PSGVariant, SEGA_VDP_PSG, SN76489, SN76489AN, YM7101_PSG}; // -- provisional: decisions D3 to D7 --------------------------------------------- /// PROVISIONAL (D3; spec 5.6, C2, U1). MAME's note: Sega's chips "act as if the /// frequency was set to 0", "Verified on SMS hardware", and its model then /// toggles every tick. Maxim says a constant +1 instead. /// /// Seed: `test_tone_register_zero_or_one_holds_the_output_high`, CORRECTED for /// value 0 by decision D3: the seed held the output at 1. #[test] fn test_tone_value_zero_behaves_as_one_on_segas_parts() { for variant in SEGA_PARTS { assert_eq!(variant.tone_zero_period, 1); let (mut zero, mut one) = (solo(variant, 0), solo(variant, 0)); write_tone(&mut zero, 0, 0); write_tone(&mut one, 0, 1); let stream = bits(&mut zero, 200); assert_eq!(stream, bits(&mut one, 200)); assert_eq!(stream, [0, 1].repeat(100)); } } /// What D3 does to what is heard (spec 5.6's observation): a channel at tone /// value 0 or 1 averages half its volume level, where the seed's and Maxim's /// held +1 gave the whole of it. Games that play samples by writing volumes are /// 6 dB quieter beside ordinary tones than with the seed. PROVISIONAL. #[test] fn test_sample_playback_sits_at_half_the_channels_volume() { for variant in SEGA_PARTS { let mut psg = solo(variant, 0); let levels = left(&mut psg, 1_000); assert_eq!(levels.iter().sum::() / levels.len() as f64, 0.5); } } /// PROVISIONAL (D4; spec 5.7, C3, U2): MAME's note, "All the TI-made PSG chips /// act as if the frequency was set to 0x400 if 0 is written", and what TI's /// description of a 10-bit down counter reloaded on borrow implies. Emulator /// tier. /// /// Seed: CORRECTED: the seed held a constant 1 on the discrete chip as on Sega's. #[test] fn test_tone_value_zero_is_a_period_of_1024_on_the_discrete_chip() { assert_eq!(SN76489AN.tone_zero_period, 1024); let mut psg = solo(SN76489AN, 0); write_tone(&mut psg, 0, 0); let edges = changes(&bits(&mut psg, 1024 * 6)); assert!(edges.len() >= 2 && edges.windows(2).all(|w| w[1] - w[0] == 1024)); } /// PROVISIONAL, following from D4 (spec 6.1.5): one shift per 2,048 ticks. /// MAME's change log says this case was "tested on hardware for noise". #[test] fn test_noise_at_rate_three_with_the_third_tone_at_zero_on_the_discrete_chip() { let mut psg = solo(SN76489AN, 3); psg.write(0xE3); // The tone falls on the first tick and rises 1,024 ticks on; the set bit // reaches the output on the 14th shift, then every 15th. assert_eq!( rises(&bits(&mut psg, 2048 * 15 * 2 + 8))[..2], [1024 + 2048 * 13, 1024 + 2048 * 28] ); } /// PROVISIONAL (D5; spec 10.4, U13). That it sounds is tier 3 (S10); that it is /// all four channels at attenuation 0 and tone value 0 is MAME's rendering. #[test] fn test_the_discrete_chip_powers_on_at_full_volume_with_a_low_tone() { let mut psg = SN76489::new(SN76489AN); assert_eq!(psg.volume, [0, 0, 0, 0]); assert_eq!(psg.tone, [0, 0, 0]); let levels = left(&mut psg, 1024 * 4); assert!(levels[0] == 0.0 && levels[1024] == 3.0); // three tones in step, 109 Hz assert_eq!(levels.iter().copied().fold(f64::MIN, f64::max), 4.0); // with the noise channel's periodic pulse on top } /// PROVISIONAL (D6; spec 10.5, C8, U14). Maxim's 2003 text: "The initially /// latched register is Tone0". MAME says the second tone's attenuation instead. /// Seen only by a program whose first write is a data byte. #[test] fn test_the_first_tone_value_is_latched_at_power_on() { for variant in ALL { assert_eq!(variant.power_on_latch, 0); let mut psg = SN76489::new(variant); psg.write(0x3F); assert_eq!(psg.tone, [0x3F0, 0, 0]); assert_eq!(psg.volume, [variant.power_on_attenuation; 4]); let mut other = SN76489::new(PSGVariant { power_on_latch: 3, ..variant }); // MAME's account other.write(0x05); assert!(other.volume[1] == 5 && other.tone == [0, 0, 0]); } } /// PROVISIONAL (U8; spec 3.6): only MAME says so for the TI chip. Held as /// variant data; with the flag off the byte is dropped. #[test] fn test_a_data_byte_reaches_every_register_on_the_discrete_chip_too() { let mut psg = SN76489::new(SN76489AN); psg.write(0x95); psg.write(0x0A); assert_eq!(psg.volume[0], 0x0A); let mut strict = SN76489::new(PSGVariant { data_byte_reaches_all_registers: false, ..SN76489AN }); strict.write(0x95); strict.write(0x0A); assert_eq!(strict.volume[0], 5); strict.write(0xE5); strict.lfsr = 0x1234; strict.write(0x02); assert!(strict.noise == 5 && strict.lfsr == 0x1234); strict.write(0x81); strict.write(0x02); assert_eq!(strict.tone[0], 0x021); // tone data bytes are TI's own two-byte load } /// PROVISIONAL and untested (D7; spec section 11, U15). Tier 3, one paragraph /// on Plutiedev, with no word on what hardware it was tried on: "When bit 9 is /// clear, PSG works as normal. When it's set, however, all PSG channels get /// replaced with the volume level of one of them. Bits 11-10 pick which channel /// is the source of the volume level." Taken literally: four times the chosen /// channel's level, held, whatever the square waves do. #[test] fn test_the_mega_drives_test_override() { for source in 0..4u32 { let mut psg = SN76489::new(YM7101_PSG); for (channel, attenuation) in [2u32, 5, 8, 11].into_iter().enumerate() { write_tone(&mut psg, channel as u32, 3 + channel as u32); psg.write(0x90 | ((channel as u32) << 5) | attenuation); } let mut normal = left(&mut psg, 200); normal.sort_by(f64::total_cmp); normal.dedup(); assert!(normal.len() > 2); psg.write_test(0x0200 | (source << 10)).unwrap(); let level = YM7101_PSG.volume_table[[2, 5, 8, 11][source as usize]]; let held = left(&mut psg, 200); assert!(!held.is_empty() && held.iter().all(|&l| l == level * 4.0)); psg.write_test(source << 10).unwrap(); // bit 9 clear: "PSG works as normal" let mut reference = SN76489::new(YM7101_PSG); for (channel, attenuation) in [2u32, 5, 8, 11].into_iter().enumerate() { write_tone(&mut reference, channel as u32, 3 + channel as u32); reference.write(0x90 | ((channel as u32) << 5) | attenuation); } left(&mut reference, 400); assert_eq!(left(&mut psg, 200), left(&mut reference, 200)); // the generators ran on underneath } } /// Spec section 11: "Debug register 0. Bits 11-9: PSG override". #[test] fn test_only_bits_11_to_9_of_the_test_value_are_the_psgs() { let mut psg = SN76489::new(YM7101_PSG); psg.write_test(0xF1FF).unwrap(); assert_eq!(psg.test, 0); psg.write_test(0x0E00).unwrap(); assert_eq!(psg.test, 7); } // -- choices: nothing speaks to these (U6, U7, U10, U11) ------------------------- /// CHOICE that follows Maxim's wording of the counter (spec 5.4, U11): "the /// counter is reset to the value currently in the corresponding register" when /// it reaches zero. No source tests it. #[test] fn test_a_new_tone_value_waits_for_the_next_reload() { for variant in ALL { let mut psg = solo(variant, 0); write_tone(&mut psg, 0, 100); let mut stream = bits(&mut psg, 250); write_tone(&mut psg, 0, 10); stream.extend(bits(&mut psg, 100)); // Written at tick 250, mid-count: the edge at 300 still comes on the old value. assert_eq!(changes(&stream), [100, 200, 300, 310, 320, 330, 340]); } } /// CHOICE between accounts (spec 6.4.3, C5, U7): a plain register, 14 shifts on /// the discrete chip and 15 on Sega's. MAME's SN76489A carries two more stages. /// Until the bit arrives the output is 0, at once from the write (spec 6.4.4). #[test] fn test_the_set_bit_reaches_the_output_after_a_plain_registers_shifts() { for variant in ALL { let mut psg = solo(variant, 3); write_tone(&mut psg, 2, 1); psg.write(0xE7); bits(&mut psg, 9_000); psg.write(0xE7); let stream = bits(&mut psg, 200); assert_eq!(stream[0], 0); // The third tone toggles every tick; the register shifts when it rises, which // is the first or second tick after the write, then every other tick. let first_one = stream.iter().position(|&b| b == 1).unwrap(); let n = 2 * (variant.lfsr_bits as usize - 2); assert!(first_one == n || first_one == n + 1); } } /// CHOICE (spec 6.1.4, U6): no source says which edge. Held as variant data. #[test] fn test_noise_at_rate_three_shifts_when_the_third_tone_rises() { for variant in ALL { assert_eq!(variant.noise_shift_level, 1); for level in [1, 0] { let mut psg = SN76489::with_silent_start( PSGVariant { noise_shift_level: level, ..variant }, true, ); write_tone(&mut psg, 2, 9); psg.write(0xE7); for _ in 0..400 { let before = (psg.lfsr, psg.outputs[2]); psg.run(16); if psg.lfsr != before.0 { assert_eq!((before.1, psg.outputs[2]), (1 - level, level)); } } } } } /// CHOICE (spec 6.4.5, U7). #[test] fn test_a_noise_write_does_not_restart_the_rate_divider() { for variant in ALL { let mut psg = solo(variant, 3); psg.write(0xE0); bits(&mut psg, 7); let (counter, flip_flop) = (psg.noise_counter, psg.noise_input); psg.write(0xE0); assert_eq!((psg.noise_counter, psg.noise_input), (counter, flip_flop)); } } /// CHOICE (spec 10.6, U10), the seed's: counters at zero and tone flip-flops /// set, so each tone falls on the first tick; the shift register as after a noise /// write; the divide-by-16 at the start of its cycle. The Nuked-PSG closed box /// starts in the same phase (docs/validation.md, rung 5), which is why the seed's /// value was kept over any other; it is not evidence about a console. #[test] fn test_the_starting_state_no_source_describes() { let psg = SN76489::new(SEGA_VDP_PSG); assert!(psg.counters == [0, 0, 0] && psg.outputs == [1, 1, 1]); assert_eq!( (psg.noise_counter, psg.noise_input, psg.lfsr), (0, 0, 0x8000) ); assert!(psg.clock_remainder == 0 && psg.test == 0); }