// SPDX-License-Identifier: MIT //! The chip's tables, each built from the formula or rule its source states. //! //! "spec 6.3" is a section of `docs/spec.md`; `docs/provenance.md` gives the //! chain behind each statement (source, author, how established). Tiers: *measured* (a //! real chip, stated in prose), *documented* (Yamaha), *emulator tier* (no document or //! measurement gives it; taken from ymfm, BSD-3-Clause, by Aaron Giles, as a //! PROVISIONAL default). Nothing here was copied from a program: the tables are //! generated at import from the rules written out below. //! //! Transcribed from ym2612-python's `tables.py`. Where Rust forced a difference: //! //! - Python builds every table at import. The two built with floating point (`LOG_SINE`, //! `EXPONENT`) are built here on first use (`LazyLock`), from the same formulas in `f64`; //! rung 2 compares every entry with the reference's. The integer tables are `const`s built by //! `const fn`s from the same rules, so they exist at compile time. //! - Python's tuples are arrays of `i32`. //! - The import-time `assert` on the exponent table's two ends runs when it is first built. use std::sync::LazyLock; fn round(value: f64) -> i32 { (value + 0.5).floor() as i32 } /// Log-sine ROM: 256 entries, a quarter wave, each a base-2 attenuation in 4.8 fixed /// point (spec 6.3). Source: Gambrell and Niemitalo, "OPLx decapsulated" (2008), who /// read the ROM bit by bit from a decapped YM3812 and give this formula as /// reproducing it exactly. Tied to the YM2612 by Nemesis, who read his own table out /// of the chip through the test register and found theirs "a perfect match" (p6741). /// Tier: measured (ROM read-out of a sister chip, confirmed on this one in prose). pub static LOG_SINE: LazyLock<[i32; 256]> = LazyLock::new(|| { std::array::from_fn(|index| { round( -((index as f64 + 0.5) * std::f64::consts::PI / 512.0) .sin() .log2() * 256.0, ) }) }); /// Exponent ROM: 256 entries of 11 bits (spec 6.5). The same source gives the ROM as /// `round((2 ** (x / 256) - 1) * 1024)` with a hidden bit worth 1024. Nemesis says /// in prose that his YM2612 table is that one with the hidden bit put back and the /// order reversed, running "0x7FA-0x400" (p6741): which is this. /// Tier: measured, as above. pub static EXPONENT: LazyLock<[i32; 256]> = LazyLock::new(|| { let table: [i32; 256] = std::array::from_fn(|index| { round((2f64.powf((255 - index) as f64 / 256.0) - 1.0) * 1024.0) + 1024 }); assert!(table[0] == 0x7FA && table[255] == 0x400); // Nemesis's two end values, p6741 table }); /// The low two bits of the key code from the F-number's top four bits (spec 5.5). /// /// Documented: Yamaha's YM3438 manual page 24, YM2608 manual 2-4-1 B. /// N4 = F11; N3 = F11 and (F10 or F9 or F8), or not F11 and F10 and F9 and F8. const fn key_code_note(top_four: i32) -> i32 { let (f11, f10, f9, f8) = ( (top_four >> 3) & 1, (top_four >> 2) & 1, (top_four >> 1) & 1, top_four & 1, ); let n3 = (f11 & (f10 | f9 | f8)) | ((f11 ^ 1) & f10 & f9 & f8); (f11 << 1) | n3 } /// Indexed by the F-number shifted right seven places. pub const KEY_CODE_NOTE: [i32; 16] = { let mut table = [0; 16]; let mut top_four = 0; while top_four < 16 { table[top_four] = key_code_note(top_four as i32); top_four += 1; } table }; /// Detune amounts in units of the 17-bit phase increment, by magnitude (1 to 3) and /// key code (0 to 31) (spec 5.6). Documented: Yamaha's YM3438 manual, table 2.4 /// (printed page 25), which gives them in hertz at 7.9872 MHz; one unit is /// 7,987,200 / (144 * 2**20) = 0.0529 Hz (the conversion is Nemesis's, p6177). The /// provenance audit converted the printed table again, entry by entry. #[rustfmt::skip] pub const DETUNE: [[i32; 32]; 4] = [ [0; 32], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8], [1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 16, 16, 16, 16], [2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 20, 22, 22, 22, 22], ]; /// Eight attenuation increments for each of the 64 rates (spec 7.3.3). /// /// PROVISIONAL, emulator tier (unknown U21): the table as a whole is ymfm's. What /// Nemesis states in prose from his measurements, and this table agrees with: the /// eight-step loop; rate 7 is 0,1,1,1,0,1,1,1; rates 2 and 3, 4 and 5, 6 and 7 are /// pairs with identical output; the largest increment is 8 and rates 60 to 63 use /// it (p5716, p7967). Rates 0 and 1 cannot be observed (p5724). #[rustfmt::skip] const fn envelope_increments() -> [[i32; 8]; 64] { let slow = [[0, 1, 0, 1, 0, 1, 0, 1], [0, 1, 0, 1, 1, 1, 0, 1], [0, 1, 1, 1, 0, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1]]; let fast = [[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1, 2], [1, 2, 1, 2, 1, 2, 1, 2], [1, 2, 2, 2, 1, 2, 2, 2]]; let mut rows = [[0; 8]; 64]; let mut rate = 0; while rate < 64 { if rate < 2 { rows[rate] = [0; 8]; } else if rate < 6 { rows[rate] = slow[0]; // the irregular low rates } else if rate < 8 { rows[rate] = slow[2]; } else if rate < 48 { rows[rate] = slow[rate & 3]; } else if rate < 60 { let scale = 1 << ((rate - 48) >> 2); let mut step = 0; while step < 8 { rows[rate][step] = fast[rate & 3][step] * scale; step += 1; } } else { rows[rate] = [8; 8]; } rate += 1; } rows } pub const ENVELOPE_INCREMENTS: [[i32; 8]; 64] = envelope_increments(); /// How many low bits of the envelope counter must be zero for a rate to step /// (spec 7.3.2). The range 0 to 11 is measured (p5716, prose); which rate has which /// shift is PROVISIONAL, emulator tier (ymfm), with both ends fixed by p5716's prose. pub const ENVELOPE_SHIFT: [i32; 64] = { let mut table = [0; 64]; let mut rate = 0; while rate < 64 { let shift = 11 - (rate as i32 >> 2); table[rate] = if shift > 0 { shift } else { 0 }; rate += 1; } table }; /// Samples per step of the LFO's 7-bit counter, by rate (spec 8.2). Documented: /// Yamaha's rates of 3.98, 5.56, 6.02, 6.37, 6.88, 9.63, 48.1, 72.2 Hz at 8 MHz are /// 8,000,000 / 144 / 128 / rate = 109.0, 78.1, 72.1, 68.1, 63.1, 45.1, 9.02, 6.01. /// PROVISIONAL (decision T2, contradiction C14): emulators use one fewer. pub const LFO_DIVIDERS: [i32; 8] = [109, 78, 72, 68, 63, 45, 9, 6]; /// Right shift of the doubled tremolo triangle (0 to 126) by tremolo depth /// (spec 8.4). The maxima 0, 15, 63, 126 units of 0.09375 dB are the manual's 0, 1.4, /// 5.9 and 11.8 dB (YM3438 manual page 31): documented. The shifts as such are from /// Nemesis's core; he could not say he measured them (p16459). PROVISIONAL (T12). pub const TREMOLO_SHIFT: [i32; 4] = [8, 3, 1, 0]; /// Vibrato amounts for F-number bit 9, by depth (0 to 7) and step of the quarter /// wave (0 to 7) (spec 8.6). The LAST COLUMN is documented: the manual's depths of /// 3.4, 6.7, 10, 14, 20, 40, 80 cents applied to an F-number of $200, /// 512 * (2 ** (cents / 1200) - 1), round to 1, 2, 3, 4, 6, 12, 24. The other seven /// columns are PROVISIONAL, emulator tier (unknown U24): they are what ymfm's /// pair-of-shifts table gives for that F-number, and ymfm says it was written to /// match Nuked-OPN2, which this project has not read. pub const VIBRATO: [[i32; 8]; 8] = [ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 1, 2, 2], [0, 0, 1, 1, 2, 2, 3, 3], [0, 0, 1, 2, 2, 2, 3, 4], [0, 0, 2, 3, 4, 4, 5, 6], [0, 0, 4, 6, 8, 8, 10, 12], [0, 0, 8, 12, 16, 16, 20, 24], ]; /// The signed amount the LFO adds to an 11-bit F-number (spec 8.5, 8.6). /// /// Measured, in prose (Nemesis, p8908, p8911): the amount depends on the F-number's /// set bits, the table for each bit being the table for bit 9 shifted, and only the /// upper six bits can contribute. PROVISIONAL (decision T1, contradiction C16): /// each bit's amount is truncated on its own, then summed. /// /// The counter's bits 4 to 2 are the step, bit 5 mirrors it and bit 6 negates: /// eight steps up, eight back, and the same negated (spec 8.6; emulator tier). pub fn vibrato_offset(f_number: i32, depth: i32, lfo_counter: i32) -> i32 { let mut step = (lfo_counter >> 2) & 7; if lfo_counter & 0x20 != 0 { step ^= 7; } let base = VIBRATO[depth as usize][step as usize]; let mut amount = 0; if f_number & 0x400 != 0 { amount += base << 1; } if f_number & 0x200 != 0 { amount += base; } if f_number & 0x100 != 0 { amount += base >> 1; } if f_number & 0x080 != 0 { amount += base >> 2; } if f_number & 0x040 != 0 { amount += base >> 3; } if f_number & 0x020 != 0 { amount += base >> 4; } if lfo_counter & 0x40 != 0 { -amount } else { amount } } /// The eight algorithms as data (spec 9.2: documented, Yamaha's figure 2.2), with /// which sample each modulator supplies (spec 9.6: a reading of the die by Sauraen, /// p29471; decision T4, PROVISIONAL). Operators are numbered as Yamaha numbers /// them, so that algorithm 0 is the chain 1 -> 2 -> 3 -> 4. Each row: /// /// `(2 from 1, 3 from 1 (previous sample), 3 from 2 (previous sample), /// 4 from 1, 4 from 2 (previous sample), 4 from 3, /// 1 is a carrier, 2 is a carrier, 3 is a carrier)` /// /// Operator 4 is always a carrier. Operator 1 modulates itself (feedback) in all. pub const ALGORITHMS: [[i32; 9]; 8] = [ [1, 0, 1, 0, 0, 1, 0, 0, 0], // 0: 1 -> 2 -> 3 -> 4 [0, 1, 1, 0, 0, 1, 0, 0, 0], // 1: (1 + 2) -> 3 -> 4 [0, 0, 1, 1, 0, 1, 0, 0, 0], // 2: (1 + (2 -> 3)) -> 4 [1, 0, 0, 0, 1, 1, 0, 0, 0], // 3: ((1 -> 2) + 3) -> 4 [1, 0, 0, 0, 0, 1, 0, 1, 0], // 4: (1 -> 2) + (3 -> 4) [1, 1, 0, 1, 0, 0, 0, 1, 1], // 5: 1 -> 2, 1 -> 3, 1 -> 4 [1, 0, 0, 0, 0, 0, 0, 1, 1], // 6: (1 -> 2) + 3 + 4 [0, 0, 0, 0, 0, 0, 1, 1, 1], // 7: 1 + 2 + 3 + 4 ];