//! Rung 0: the contract with the boards (docs/api.md, docs/validation.md). //! //! Not a judge of the chip: these keep the core usable by sms-python and //! megadrive-python as api.md describes it. //! //! A transcription of sn76489-python's `tests/test_contract.py` at f8157f4. Parametrized //! tests are loops. Where a test is about Python itself, the Rust counterpart is said at //! the test; `test_an_instance_stays_under_pypys_eighty_attributes` has none (PyPy's limit //! on attributes does not exist for a Rust struct) and is left out. mod common; use common::{write_tone, write_volume, Rng, ALL, SEGA_PARTS}; use sn76489::{ PSGVariant, CLOCKS_PER_SAMPLE, GAME_GEAR_PSG, SEGA_VDP_PSG, SN76489, SN76489AN, YM7101_PSG, }; /// (input clocks to run first, port, byte); port 1 is the stereo register. fn random_stream(seed: u64, writes: usize) -> Vec<(u64, u32, u32)> { let mut rng = Rng::new(seed); (0..writes) .map(|_| { let clocks = rng.range(0, 700); let port = if rng.random() < 0.05 { 1 } else { 0 }; (clocks, port, rng.range(0, 256) as u32) }) .collect() } fn play(psg: &mut SN76489, stream: &[(u64, u32, u32)]) { for &(clocks, port, value) in stream { psg.run(clocks); if port != 0 { psg.write_stereo(value); } else { psg.write(value); } } } #[test] fn test_the_names_api_md_lists_are_the_public_surface() { // api.md: the import block. In Rust the `use` above is the check: each of the seven // names must exist for this file to build. let _: [PSGVariant; 4] = [SN76489AN, SEGA_VDP_PSG, GAME_GEAR_PSG, YM7101_PSG]; let _ = SN76489::new; assert_eq!(CLOCKS_PER_SAMPLE, 16); // spec 4.1, 4.3 } #[test] fn test_the_default_variant_is_the_master_systems() { // Carried from the seed, whose tests build `PSG()` for a Master System. assert_eq!(SN76489::default().variant, SEGA_VDP_PSG); } #[test] fn test_one_pair_per_sixteen_input_clocks() { // api.md: "one entry per CLOCKS_PER_SAMPLE input clocks". for variant in ALL { let mut psg = SN76489::new(variant); psg.run(15); assert!(psg.take_samples().is_empty()); psg.run(1); assert_eq!(psg.take_samples().len(), 1); psg.run(3_579_545); assert_eq!(psg.take_samples().len(), 3_579_545 / 16); psg.run(16 - 3_579_545 % 16); assert_eq!(psg.take_samples().len(), 1); assert!(psg.take_samples().is_empty()); // taking clears } } #[test] fn test_levels_run_from_zero_to_four() { // api.md: "levels from 0.0 to 4.0 (four channels at full volume)". for variant in ALL { let mut psg = SN76489::new(variant); play(&mut psg, &random_stream(1, 300)); for channel in 0..4 { write_volume(&mut psg, channel, 0); } psg.run(16 * 20_000); let samples = psg.take_samples(); assert!(samples .iter() .all(|&(l, r)| (0.0..=4.0).contains(&l) && (0.0..=4.0).contains(&r))); assert_eq!(samples.iter().map(|p| p.0).fold(f64::MIN, f64::max), 4.0); // Python also checks each entry is a 2-tuple: a `(f64, f64)` is one by its type. } } #[test] fn test_any_split_of_a_run_gives_the_same_samples() { // validation.md rung 0: `run(a)` then `run(b)` equals `run(a + b)`. for variant in ALL { let mut rng = Rng::new(7); let stream = random_stream(2, 300); let (mut whole, mut pieces) = (SN76489::new(variant), SN76489::new(variant)); for &(clocks, port, value) in &stream { whole.run(clocks); let mut remaining = clocks; while remaining != 0 { let step = rng.range(1, remaining + 1); pieces.run(step); remaining -= step; } for psg in [&mut whole, &mut pieces] { if port != 0 { psg.write_stereo(value) } else { psg.write(value) } } } assert_eq!(whole.take_samples(), pieces.take_samples()); assert_eq!(whole, pieces); // Python: vars(whole) == vars(pieces) } } #[test] fn test_state_is_plain_attributes_and_copies_out_and_back() { // api.md: "state must be plain attributes (numbers, lists, bytearrays); a // frozen dataclass is taken for configuration". In Rust the state is the struct's // public fields, integers and arrays by their types; each is copied by hand into a // fresh chip, as a save-state walker would set Python's attributes. for variant in ALL { let mut psg = SN76489::new(variant); let stream = random_stream(3, 300); play(&mut psg, &stream[..150]); let mut restored = SN76489::new(variant); restored.tone = psg.tone; restored.noise = psg.noise; restored.volume = psg.volume; restored.stereo = psg.stereo; restored.latched = psg.latched; restored.counters = psg.counters; restored.outputs = psg.outputs; restored.noise_counter = psg.noise_counter; restored.noise_input = psg.noise_input; restored.lfsr = psg.lfsr; restored.test = psg.test; restored.clock_remainder = psg.clock_remainder; restored.samples = psg.samples.clone(); psg.take_samples(); restored.take_samples(); play(&mut psg, &stream[150..]); play(&mut restored, &stream[150..]); assert_eq!(psg.take_samples(), restored.take_samples()); } } #[test] fn test_a_variant_is_a_frozen_value_of_plain_fields() { // api.md: "a variant is a value, not a subclass". Rust: a `Copy` struct; changing a // copy leaves the part's constant as it was (Python: FrozenInstanceError), and every // field is a number, a flag or an array of numbers by its type. Like Python's, the // check pins no field's value: Python's assignment of 15 raises whatever the field // holds. let before = SEGA_VDP_PSG; let mut copy = SEGA_VDP_PSG; copy.lfsr_bits = 15; assert_eq!(SEGA_VDP_PSG, before); assert_eq!(copy.lfsr_bits, 15); // Python: type(SN76489(GAME_GEAR_PSG)) is type(SN76489(SN76489AN)). let _same_type: [SN76489; 2] = [SN76489::new(GAME_GEAR_PSG), SN76489::new(SN76489AN)]; } #[test] fn test_the_logic_never_asks_which_variant_it_is() { // The brief: "Variant differences are data on PSGVariant, never // `if variant is ...` branches". The Rust spellings of the same questions are added. let source = include_str!("../src/chip.rs"); for forbidden in [ "variant is", "variant ==", "variant !=", "variant.name ==", "variant in", "matches!", "SN76489AN", "GAME_GEAR_PSG", "YM7101_PSG", ] { assert!(!source.contains(forbidden), "{forbidden}"); } } #[test] fn test_stereo_is_readable() { // api.md: "psg.stereo: the stereo register, readable by tests". let mut psg = SN76489::new(GAME_GEAR_PSG); psg.write_stereo(0x5A); assert_eq!(psg.stereo, 0x5A); } #[test] fn test_write_test_exists_only_on_the_mega_drives_part() { // Decision D7 (handoff-brief.md), correcting api.md. Python raises AttributeError; // here the call returns an error. let mut mega_drive = SN76489::new(YM7101_PSG); mega_drive.write_test(0x0200).unwrap(); assert_eq!(mega_drive.test, 1); for variant in [SN76489AN, SEGA_VDP_PSG, GAME_GEAR_PSG] { assert!(SN76489::new(variant).write_test(0x0200).is_err()); } } #[test] fn test_a_measured_volume_curve_is_a_table_a_board_passes_in() { // Decision D11: the core ships the ideal table; a board replaces it as data. for variant in SEGA_PARTS { let flat: [f64; 16] = std::array::from_fn(|step| if step < 15 { 1.0 } else { 0.0 }); let mut psg = SN76489::new(PSGVariant { volume_table: flat, ..variant }); write_tone(&mut psg, 0, 1); write_volume(&mut psg, 0, 9); psg.run(16 * 4); let max = psg .take_samples() .iter() .map(|p| p.0) .fold(f64::MIN, f64::max); assert_eq!(max, 1.0); } }