// SPDX-License-Identifier: MIT //! Rung 3: community documents whose authors checked them on hardware (tier 3). //! //! - Maxim's SN76489 page on the SMS Power development wiki (phase0.md S6) and his //! 2003 text (S7): the only public account of most of what is particular to Sega's //! part. Its captures are unpublished, and the one file it offers as Sega's noise //! is BBC Micro data (spec C9). //! - Charles MacDonald's SC-3000H notes (S10). //! - SMS Power forum thread 17116 (S24). //! //! Passing these proves agreement with that account. It does not prove the account. //! //! A transcription of sn76489-python's `tests/test_community.py` at f8157f4. Parametrized //! tests are loops; Python's `set(levels) == {x}` is "not empty and every level is x". //! The binary literals keep Python's grouping, which follows a byte's fields (latch bit, //! register, type, data), hence the allowed lint. #![allow(clippy::unusual_byte_groupings)] mod common; use common::{bits, find, left, noise_bits, solo, write_tone, write_volume, ALL, SEGA_PARTS}; use sn76489::{SEGA_VDP_PSG, SN76489, SN76489AN}; fn all_equal(levels: &[f64], value: f64) -> bool { !levels.is_empty() && levels.iter().all(|&l| l == value) } fn max(values: &[f64]) -> f64 { values.iter().copied().fold(f64::MIN, f64::max) } /// Tier 3 (S6, "SN76489 register writes"): its five worked examples. #[test] fn test_maxims_worked_write_sequences() { let mut psg = SN76489::new(SEGA_VDP_PSG); psg.write(0b1_00_0_1110); psg.write(0b0_0_001111); assert_eq!(psg.tone[0], 0b0011111110); // "= 0xfe (440Hz @ 3579545Hz clock)" psg.write(0b1_01_1_1111); assert_eq!(psg.volume[1], 0xF); // "silent" psg.write(0b1_10_1_1111); assert_eq!(psg.volume[2], 0xF); psg.write(0b0_0_000000); assert_eq!(psg.volume[2], 0x0); // "THEN update it to %0000 = 0x0 (full)" psg.write(0b1_11_0_0101); assert_eq!(psg.noise, 0b101); // "white noise, medium shift rate" psg.write(0b1_11_0_0101); psg.write(0b0_0_000100); assert_eq!(psg.noise, 0b100); // "THEN update it to %100 (white noise, high shift rate)" } /// Tier 3 (S6): "the tone registers update immediately when a byte is written; /// they do not wait until all 10 bits are written", with its four-row table. #[test] fn test_maxims_table_of_tone0_after_each_byte() { let mut psg = SN76489::new(SEGA_VDP_PSG); psg.tone[0] = 0b1010101010; // "whatever was previously in the register" psg.write(0b1_00_0_0000); assert_eq!(psg.tone[0], 0b1010100000); psg.write(0b0_0_000000); assert_eq!(psg.tone[0], 0b0000000000); psg.write(0b1_00_0_1111); assert_eq!(psg.tone[0], 0b0000001111); psg.write(0b0_0_111111); assert_eq!(psg.tone[0], 0b1111111111); } /// Tier 3 (S6, as above; spec 3.2), heard at the output: after the latch byte /// and before any data byte, the tone already runs at the new value. #[test] fn test_a_latch_byte_alone_changes_the_pitch() { for variant in SEGA_PARTS { let mut psg = solo(variant, 0); write_tone(&mut psg, 0, 0x123); psg.write(0x80 | 0x7); // low four bits only: the value is now $127 let stream = bits(&mut psg, 0x127 * 8); let edges: Vec = (1..stream.len()) .filter(|&i| stream[i] != stream[i - 1]) .collect(); assert_eq!(edges[edges.len() - 1] - edges[edges.len() - 2], 0x127); } } /// Tier 3 (S6): "The data byte is NOT ignored. If it is, you will hear a /// sustained tone while reading a message box in Alex Kidd in Miracle World." /// /// Seed: `test_data_byte_after_a_volume_latch_sets_the_volume`, kept. #[test] fn test_a_data_byte_reaches_the_attenuation_register() { for variant in SEGA_PARTS { let mut psg = SN76489::new(variant); write_tone(&mut psg, 0, 4); psg.write(0x90 | 0x3); assert_eq!(psg.volume[0], 3); psg.write(0x07); assert_eq!(psg.volume[0], 7); assert_eq!(max(&left(&mut psg, 16)), variant.volume_table[7]); psg.write(0x7F); // only the low four bits count assert_eq!(psg.volume[0], 15); } } /// Tier 3 (S6): "The data byte is NOT ignored. If it is, some games (e.g. Micro /// Machines) produce the wrong sound on their noise channel." /// /// Seed: `test_data_byte_after_a_noise_latch_is_not_ignored`, kept. #[test] fn test_a_data_byte_reaches_the_noise_register() { for variant in SEGA_PARTS { let mut psg = SN76489::new(variant); psg.write(0xE5); assert_eq!(psg.noise, 0b101); psg.lfsr = 0x1234; psg.write(0x04); assert_eq!(psg.noise, 0b100); assert_eq!(psg.lfsr, 0x8000); // and it restarts the register like any noise write } } /// Tier 3 (S6): "For the SMS (1 and 2), Genesis and Game Gear, the tapped bits /// are bits 0 and 3 ($0009), fed back into bit 15", reset to "all bits ... zero /// except for the highest bit", with its code: `Output=ShiftRegister&1`. /// /// The reference sequence is built here from that rule in the plainest way. No /// capture of a Sega part's noise is in hand (U3): this checks the rule, at tier /// 3, and nothing measured. #[test] fn test_segas_white_noise_is_sixteen_stages_tapped_at_bits_0_and_3() { for variant in SEGA_PARTS { let (mut register, mut expected) = (0x8000u32, vec![]); for _ in 0..3_000 { let feedback = (register ^ (register >> 3)) & 1; register = (register >> 1) | (feedback << 15); expected.push((register & 1) as u8); } assert_eq!(noise_bits(variant, 0x04, 3_000), expected); } } /// Arithmetic on S6's rule, not a measurement (spec 6.2.4): the sequence is not /// the longest a 16-stage register can give. /// /// Seed: `test_white_noise_periods`, kept; the discrete half of it is now rung 1. #[test] fn test_segas_white_noise_repeats_after_57337_shifts() { let stream = noise_bits(SEGA_VDP_PSG, 0x04, 2 * 57_337); assert_eq!(stream[57_337..], stream[..57_337]); assert_eq!( find(&stream[..57_337].repeat(2), &stream[..57_337], 1), Some(57_337) ); } /// Tier 3 (S6): "the output bit is also the input bit ... a loop of the same /// length (16 bits for the SMS (1 and 2), Genesis and Game Gear ...)". /// /// Seed: `test_periodic_noise_is_the_register_width`, kept. #[test] fn test_segas_periodic_noise_goes_round_sixteen_stages() { for variant in SEGA_PARTS { let stream = noise_bits(variant, 0x00, 64); let mut lap = vec![0u8; 14]; lap.extend([1, 0]); assert_eq!(stream, lap.repeat(4)); } } /// Tier 3 (S6: "15 bits for SG-1000, OMV, BBC Micro, SC-3000H ...") and tier 1 /// (Kortink, S9: "a cycle of 15 clocks"). #[test] fn test_the_discrete_chips_periodic_noise_goes_round_fifteen_stages() { let stream = noise_bits(SN76489AN, 0x00, 60); let mut lap = vec![0u8; 13]; lap.extend([1, 0]); assert_eq!(stream, lap.repeat(4)); } /// Tier 3 (S6: "When the noise register is written to, the shift register is /// reset"; S24, Maxim: a ROM "writes to the PSG registers every frame instead of /// only when the values change, which affects the noise channel"). Spec 6.4.2. #[test] fn test_rewriting_the_same_noise_value_restarts_the_register() { for variant in ALL { for control in [0xE4, 0xE0, 0xE5, 0xE7] { let mut psg = solo(variant, 3); write_tone(&mut psg, 2, 3); psg.write(control); bits(&mut psg, 9_000); let before = psg.lfsr; psg.write(control); assert_eq!(psg.noise, control & 7); assert_eq!(psg.lfsr, 1 << (variant.lfsr_bits - 1)); assert_ne!(psg.lfsr, before); } } } /// Tier 3 and hedged (S6): the integrated versions "seem to start initialised /// with zeroes in the tone/noise registers and ones in the volume registers /// (silence)". For the Mega Drive nothing specific exists (U12). /// /// Seed: `test_power_up_is_silent_with_zeroed_registers`, kept. #[test] fn test_segas_parts_start_silent_with_zeroed_registers() { for variant in SEGA_PARTS { let mut psg = SN76489::new(variant); assert_eq!(psg.tone, [0, 0, 0]); assert_eq!(psg.noise, 0); assert_eq!(psg.volume, [15, 15, 15, 15]); assert!(all_equal(&left(&mut psg, 3_000), 0.0)); } } /// Tier 3 (S10, MacDonald on the SC-3000H): "The SN76489A is not reset when the /// SC-3000H is powered on ... from a cold boot the PSG emits a medium volume /// tone". Only that it sounds is checked here; what it sounds like is /// provisional (test_provisional.py). #[test] fn test_the_discrete_chip_is_not_silent_at_power_on() { let mut psg = SN76489::new(SN76489AN); let levels = left(&mut psg, 5_000); assert!(max(&levels) > 0.0); assert!(levels.iter().any(|&l| l != levels[0])); // a tone, not a held level } /// Decision D5: "a board may ask for a silent start". #[test] fn test_a_board_may_ask_the_discrete_chip_to_start_silent() { let mut psg = SN76489::with_silent_start(SN76489AN, true); assert!(all_equal(&left(&mut psg, 5_000), 0.0)); psg.reset(false); assert!(max(&left(&mut psg, 5_000)) > 0.0); psg.reset(true); assert!(all_equal(&left(&mut psg, 5_000), 0.0)); } /// Tier 3 (S6, "Volume/attenuation"): the mixer "multiplies each channel's /// output by the corresponding volume ... and sums them"; a channel is 0 or +1. #[test] fn test_all_four_at_full_volume_with_the_same_tone_reach_four() { let mut psg = SN76489::new(SEGA_VDP_PSG); for channel in 0..4 { write_volume(&mut psg, channel, 0); } assert!(left(&mut psg, 64) .iter() .all(|l| [0.0, 1.0, 3.0, 4.0].contains(l))); }