# Phase generator: KC/KF to phase step, DT1, DT2, MUL, PM Citation forms: [start-here.md](start-here.md). Line numbers are IKAOPM 08a5b40's `IKAOPM_pg.v` unless another file is named. A "step" is the amount added to a slot's 20-bit phase once per sample, so one unit is 2^-20 turn per sample, which is fM / 64 / 2^20 = 0.05334 Hz at fM = 3.579545 MHz. ## Pitch registers | Rule | Evidence | | --- | --- | | KC: octave in bits 6-4 (8 octaves), note code in bits 3-0; codes 0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14 are C#, D, D#, E, F, F#, G, G#, A, A#, B, C | DOC (AM p.7; Fig. 2.4, p.18) | | KF: 6 bits dividing the semitone (100 cents) into 64 steps of about 1.6 cents | DOC (AM p.7; Fig. 2.4) | | 440.0 Hz: KC 0x4A (octave 4, note A), KF 0, MUL 1, DT1 0, DT2 0, PMS 0, fM 3.579545 MHz | DOC (AM p.7) | | The phase accumulator is 20 bits per slot; the operator takes the top 10 | DIE (:705-754) | | A key-on edge clears the slot's phase (the step added in that round is also 0) | DIE (:640-707; IKAOPM_eg.v:186-188) | | Test bit 3 holds every phase at 0 | DIE (:689) | ## KC and KF to a step 1. **Unused note codes.** A note code whose low two bits are 11 (3, 7, 11, 15) is treated as the next code (code 15 becomes code 0 of the next octave); past KC 0x7E, KF 63 the pitch saturates there. DIE (:144-183, :216-247). The same rule is in ymfm, stated as "confirmed" (ymfm 81aec25, ymfm_fm.ipp:282-286). 2. **The ROM.** A 64-row ROM (48 rows used) is addressed by the note code and KF bits 5-4. Each row holds a 12-bit base step, a 4-bit increment and a mode bit (stored in a scrambled bit order). DIE (:363-386, ROM :797-878). 3. **Interpolation.** KF bits 3-0 multiply the row's increment by shift-and-add (two variants chosen by the mode bit); half of that product is added to the base. DIE (:392-456). 4. **Octave.** The result is shifted by the octave: right 2 for octave 0, right 1 for 1, unchanged for 2, left 1 to 5 for octaves 3 to 7 (low bits are lost below octave 2). DIE (:467-526). The resulting 8,192-entry table (every KC x KF) is identical to the table ymfm uses (`s_phase_step`, credited there to David Viens' analysis of a real chip; ymfm 81aec25, ymfm_fm.ipp:206-321): 8192 of 8192 equal, computed by `scripts/referee_freq.py`'s two functions. The closed box measures the same 8192 of 8192 (see [Measured](#measured)). ## DT1 DT1 bits 1-0 choose the size, bit 2 the sign. The displacement depends only on DT1 and KC' = the top five bits of the final pitch (octave and the note code's top two bits). DIE (:478-537, :384). In the die it is computed, not stored: an intensity of (KC' >> 2) + 1, + 9, + 11 or + 12 (for sizes 0-3) picks a shift, and KC' bits 1-0 (forced to 0 for KC' >= 28) pick one of eight 5-bit bases; the base shifted right by 9 - intensity / 2 is the displacement when intensity / 2 is 5 to 9, else 0. As a table (units of steps, size 1, 2, 3; size 0 is 0; sizes 5-7 are the negatives of 1-3): | KC' | 0-3 | 4 | 5-7 | 8 | 9-10 | 11 | 12 | 13-14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28-31 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | DT1 1 | 0 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 3 | 3 | 3 | 4 | 4 | 4 | 5 | 5 | 6 | 6 | 7 | 8 | | DT1 2 | 1 | 2 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 6 | 6 | 7 | 8 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | | DT1 3 | 2 | 2 | 3 | 4 | 4 | 5 | 5 | 6 | 7 | 8 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 19 | 20 | 22 | ymfm's `s_detune_adjustment` is the same table (ymfm_fm.ipp:179-194; its comment says it was checked against another model's equations, so it is not independent evidence). **C3.** The manual's Fig. 2.6 (AM p.18), whose D-FREQ column in Hz divided by 0.05334 Hz gives the same units, agrees in every cell except five: for KC' 29, 30, 31 it gives 8, 9, 10 (DT1 1) and 17, 19, 20 (DT1 2) against the die's 8 and 16 (DT1 3 is 22 in both). The closed box sides with the die and ymfm in all 448 cases measured (see [Measured](#measured)), so the core follows the die; the manual's last rows are recorded, not followed. | Rule | Evidence | | --- | --- | | DT1 is added to the octave-shifted step, before MUL; the sum keeps 17 bits (a negative DT1 larger than the step would wrap, which no valid KC reaches: the smallest step is 324) | DIE (:546-563) | | The manual: DT1 "undergoes scaling via the key code" | DOC (AM p.7) | ## DT2 DT2 1, 2, 3 raise the pitch by 600, 781 and 950 cents (x1.41, x1.57, x1.73). DOC (AM p.19, Fig. 2.7). In the die they are added to the pitch before the ROM: + 8 note codes (two groups of three semitones = 600 cents), + 9 codes and 52/64 of a semitone (781 cents), + 12 codes and 32/64 (950 cents), with a carry correction that keeps the result out of the unused codes, then saturation at 0x7E, 63. DIE (:254-361). ymfm adds the same amounts in a gapless semitone scale (ymfm_opm.cpp:318); whether the two agree at every KC/KF is not yet checked (owed to rung 1 against the closed box). ## MUL MUL 0 halves the step (the lowest bit is dropped); MUL 1-15 multiply it; the product keeps 20 bits. DOC (AM p.18, Fig. 2.5: 0.5, 1 ... 15); DIE (:597-616). ## Phase modulation (LFO PM) | Rule | Evidence | | --- | --- | | The LFO's PM value (sign and 7-bit magnitude) is scaled by PMS and added to KC/KF as 1/64-semitone steps (subtracted when negative), before DT2 | DIE (:56-139) | | PMS 1-5 take the top 2, 3, 4, 5, 6 bits of the magnitude; PMS 6 and 7 use the whole magnitude plus small correction terms, shifted left 1 and 2 | DIE (:59-111) | | The sum is corrected so that it skips the unused note codes, then saturated (0 at the bottom, 0x7E, 63 at the top) | DIE (:143-251) | | Maximum depth at PMS 1-7: +-5, 10, 20, 50, 100, 400, 700 cents | DOC (AM p.19, Fig. 2.8) | | PMD = 0 forces the PM value to 0 | DIE (IKAOPM_lfo.v:527, :544) | ymfm approximates this with shifts of a +-200-cent value and says so ("roughly"; ymfm_opm.cpp:325-333); it is a detector here, not a source (C7). ## Measured `scripts/referee_freq.py` measures the step of a single sine carrier on the closed-box referee from the times its sign changes (each change pins the phase to a multiple of 2^19; the inequalities leave one integer step). OBSERVED (Nuked-OPM f209e6e): - every KC x KF step (8,192 runs) equals IKAOPM's ROM and rules and ymfm's table; - every DT1 displacement measured (448: 32 KC' x KF 0, 63 x DT1 1-7) equals IKAOPM's rule and ymfm's table; 428 equal the manual's Fig. 2.6 (C3); - negative DT1 is the exact negative, and DT1 does not depend on KF; - against equal temperament from 440 Hz the steps are within -5.86 cents (mean -1.20). Details: [validation.md](validation.md#frequency-and-dt1).