// SPDX-License-Identifier: MIT //! The parts and their DAC tables. //! //! ay8910-python's `tests/test_variants.py`, transcribed test for test, and one test of this //! crate's own: the tables written out in `src/variants.rs` are, bit for bit, the formulas //! Python computes them by. use ay8910_rust::variants::{normalised, ALL_BITS, REGISTER_WIDTH_MASKS, WESTCOTT_VOLTS}; use ay8910_rust::{ AY8910, AY8912, AY8913, DAC_5B, DAC_AY_LOGARITHMIC, DAC_AY_MEASURED, DAC_YM_DATASHEET, DAC_YM_MEASURED, SUNSOFT_5B, VARIANTS, YM2149, }; fn decibels(table: &[f64], code: usize) -> f64 { 20.0 * (table[code] / table[code - 1]).log10() } fn approx(a: f64, b: f64, abs: f64) -> bool { (a - b).abs() <= abs } /// pytest.approx's default tolerance. fn approx_default(a: f64, b: f64) -> bool { (a - b).abs() <= (1e-6 * b.abs()).max(1e-12) } #[test] fn test_the_five_parts() { let names: Vec = VARIANTS.iter().map(|v| v.name.to_string()).collect(); assert_eq!( names, [ "AY-3-8910", "AY-3-8912", "AY-3-8913", "YM2149", "Sunsoft 5B" ] ); for v in VARIANTS.iter() { assert_eq!(ay8910_rust::variant(&v.name).as_ref(), Some(v)); } } #[test] fn test_what_differs_between_the_parts() { let parts = [AY8910, AY8912, AY8913, YM2149, SUNSOFT_5B]; let steps: Vec = parts.iter().map(|v| v.envelope_steps).collect(); assert_eq!(steps, [16, 16, 16, 32, 32]); let ports: Vec<&str> = parts.iter().map(|v| v.ports.as_ref()).collect(); assert_eq!(ports, ["AB", "A", "", "AB", ""]); let dividers: Vec = VARIANTS.iter().map(|v| v.clock_divider).collect(); assert_eq!(dividers, [1, 1, 1, 1, 2]); let sel: Vec = VARIANTS.iter().map(|v| v.has_sel_pin).collect(); assert_eq!(sel, [false, false, false, true, false]); let codes: Vec = VARIANTS.iter().map(|v| v.codes()).collect(); assert_eq!(codes, [16, 16, 16, 32, 32]); assert!(AY8910.register_masks[1] == 0x0F && YM2149.register_masks[1] == 0xFF); } #[test] fn test_a_dac_table_rises_from_silence_to_one() { for table in [ &DAC_AY_MEASURED[..], &DAC_AY_LOGARITHMIC[..], &DAC_YM_MEASURED[..], &DAC_YM_DATASHEET[..], &DAC_5B[..], ] { assert!(table.len() == 16 || table.len() == 32); assert!(table[0] == 0.0 && approx_default(table[table.len() - 1], 1.0)); assert!(table.windows(2).all(|pair| pair[0] <= pair[1])); } } #[test] fn test_the_ladders_are_3_db_a_volume_and_half_that_an_envelope_step() { for code in 2..16 { assert!(approx(decibels(&DAC_AY_LOGARITHMIC, code), 3.0103, 1e-4)); } for code in 3..32 { assert!(approx(decibels(&DAC_YM_DATASHEET, code), 1.50515, 1e-5)); } // The catalog's own figures, Fig. 1. for (code, volts) in [ (31, 1.0), (30, 0.841), (29, 0.707), (28, 0.595), (27, 0.5), (19, 0.125), ] { assert!(approx(DAC_YM_DATASHEET[code], volts, 5e-4)); } assert_eq!(DAC_YM_DATASHEET[1], 0.0); // a volume of 0 drives code 1: silence } #[test] fn test_how_far_the_measured_tables_stand_from_the_ladders() { // The AY's measured levels sit above the 3 dB ladder everywhere below the top, // by up to 4.8 dB; the YM2149's stay within 2.6 dB of Yamaha's from code 4 up. let ay: Vec = (1..16) .map(|c| 20.0 * (DAC_AY_MEASURED[c] / DAC_AY_LOGARITHMIC[c]).log10()) .collect(); let max = ay.iter().cloned().fold(f64::MIN, f64::max); assert!(ay.iter().cloned().fold(f64::MAX, f64::min) >= 0.0 && 4.7 < max && max < 4.8); let ym: Vec = (4..32) .map(|c| 20.0 * (DAC_YM_MEASURED[c] / DAC_YM_DATASHEET[c]).log10()) .collect(); assert!( ym.iter().cloned().fold(f64::MAX, f64::min) >= 0.0 && ym.iter().cloned().fold(f64::MIN, f64::max) < 2.6 ); } #[test] fn test_a_yamaha_volume_lands_on_the_ays_3_db_steps() { // Volume v drives code 2v + 1, two 1.5 dB steps apart: the AY's ladder. for volume in 2..16 { let step = 20.0 * (DAC_5B[2 * volume + 1] / DAC_5B[2 * volume - 1]).log10(); assert!(approx(step, 3.0103, 1e-4)); } } #[test] fn test_the_measured_ay_table_is_westcotts_voltages() { let (low, high) = (1.147, 2.58); assert!(approx_default( DAC_AY_MEASURED[8], (1.336 - low) / (high - low) )); assert!(approx_default( DAC_AY_MEASURED[1], (1.162 - low) / (high - low) )); } // -- this crate's own --------------------------------------------------------------------- /// Bit for bit, the tables are the formulas of `variants.py`. #[test] fn the_written_out_tables_are_pythons_formulas() { let bits = |table: &[f64]| table.iter().map(|x| x.to_bits()).collect::>(); assert_eq!(bits(&normalised(&WESTCOTT_VOLTS)), bits(&DAC_AY_MEASURED)); let mut logarithmic = vec![0.0]; logarithmic.extend((1..16).map(|code| 2f64.powf((code as f64 - 15.0) / 2.0))); assert_eq!(bits(&logarithmic), bits(&DAC_AY_LOGARITHMIC)); let mut datasheet = vec![0.0, 0.0]; datasheet.extend((2..32).map(|code| 2f64.powf((code as f64 - 31.0) / 4.0))); assert_eq!(bits(&datasheet), bits(&DAC_YM_DATASHEET)); assert_eq!(bits(&DAC_5B), bits(&DAC_YM_DATASHEET)); assert_eq!(DAC_YM_MEASURED.len(), 32); assert_eq!( REGISTER_WIDTH_MASKS, [ 0xFF, 0x0F, 0xFF, 0x0F, 0xFF, 0x0F, 0x1F, 0xFF, 0x1F, 0x1F, 0x1F, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF ] ); assert_eq!(ALL_BITS, [0xFF; 16]); }