"""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-audit.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. """ from __future__ import annotations import math def _round(value: float) -> int: return math.floor(value + 0.5) #: 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). LOG_SINE = tuple( _round(-math.log2(math.sin((index + 0.5) * math.pi / 512)) * 256) for index in range(256) ) #: 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. EXPONENT = tuple(_round((2 ** ((255 - index) / 256) - 1) * 1024) + 1024 for index in range(256)) assert EXPONENT[0] == 0x7FA and EXPONENT[255] == 0x400 # Nemesis's two end values, p6741 def _key_code_note(top_four: int) -> int: """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. """ f11, f10, f9, f8 = (top_four >> 3) & 1, (top_four >> 2) & 1, (top_four >> 1) & 1, top_four & 1 n3 = (f11 & (f10 | f9 | f8)) | ((f11 ^ 1) & f10 & f9 & f8) return (f11 << 1) | n3 #: Indexed by the F-number shifted right seven places. KEY_CODE_NOTE = tuple(_key_code_note(top_four) for top_four in range(16)) #: 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. DETUNE = ( (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), ) # fmt: skip def _envelope_increments() -> tuple[tuple[int, ...], ...]: """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). """ 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)) # fmt: skip 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)) # fmt: skip rows: list[tuple[int, ...]] = [] for rate in range(64): if rate < 2: rows.append((0,) * 8) elif rate < 6: rows.append(slow[0]) # the irregular low rates elif rate < 8: rows.append(slow[2]) elif rate < 48: rows.append(slow[rate & 3][1:] + slow[rate & 3][:1]) elif rate < 60: scale = 1 << ((rate - 48) >> 2) rows.append(tuple(step * scale for step in fast[rate & 3])) else: rows.append((8,) * 8) return tuple(rows) ENVELOPE_INCREMENTS = _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. ENVELOPE_SHIFT = tuple(max(0, 11 - (rate >> 2)) for rate in range(64)) #: 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. LFO_DIVIDERS = (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). TREMOLO_SHIFT = (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. VIBRATO = ( (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), ) def vibrato_offset(f_number: int, depth: int, lfo_counter: int) -> int: """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). """ step = (lfo_counter >> 2) & 7 if lfo_counter & 0x20: step ^= 7 base = VIBRATO[depth][step] amount = 0 if f_number & 0x400: amount += base << 1 if f_number & 0x200: amount += base if f_number & 0x100: amount += base >> 1 if f_number & 0x080: amount += base >> 2 if f_number & 0x040: amount += base >> 3 if f_number & 0x020: amount += base >> 4 return -amount if lfo_counter & 0x40 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. ALGORITHMS = ( (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 )