FR
games/alis / tools / port / gen_emu8950_fast.php · commit 9c722cf
<?php// Generates src/Alis/fn/emu8950_fast.php: opl_calc_block_melodic(), the unrolled inner loop of OPL_calc_block()// (emu8950 melodic mode, 9 channels x 2 slots with every slot field in a scalar local variable: PHP has no JIT// in the browser and array accesses cost more than scalar ones).//   php games/alis/tools/port/gen_emu8950_fast.php$slotCode = function (int $s, bool $isCar, int $c): string {    $v = fn ($n) => '$' . $n . $s;    $e = $v('eo'); $es = $v('es'); $rh = $v('rh'); $mk = $v('mk'); $sh = $v('sh'); $st = $v('st');    $ph = $v('ph'); $o0 = $v('oa'); $o1 = $v('ob'); $dd = $v('dd');    $out = $isCar ? '$co' : '$mo';    $keep = ($s == 14 || $s == 17) ? '' : " && $o1 == 0";    $code = "if ($dd) {\n    $out = 0;\n} else {\n";    $code .= <<<PHP    if ($es == 0) {        if ($e > 0 && $rh > 0 && (\$cnt & $mk) == 0) {            $e += ((~$e) * {$st}[(\$cnt >> $sh) & 7]) >> 3;        }        if ($e == 0) {            $es = 1;            [$rh, $mk, $sh, $st] = \$envP[$s][1];        }    } else {        if ($rh > 0 && (\$cnt & $mk) == 0) {            $e += {$st}[(\$cnt >> $sh) & 7];            if ($e > 511) {                $e = 511;            }        }        if ($es == 1 && \$SL$s != 15 && ($e >> 4) == \$SL$s) {            $es = 2;            [$rh, $mk, $sh, $st] = \$envP[$s][2];        }    }    if (\$pmOn$s) {        \$pm = \$pmT[(\$fn$s >> 7) & 7][\$pmPhase >> 19];        if (!\$pmMode) {            \$pm >>= 1;        }        $ph = ($ph + (((((\$fn$s & 0x3ff) + \$pm) * \$ml$s) << \$bk$s) >> 1)) & 0xfffff;    } else {        $ph = ($ph + \$dph$s) & 0xfffff;    }PHP;    if ($isCar) {        $code .= "    \$fm = \$alg$c ? 0 : ((\$mo >> 1) << 1);\n";    } else {        $code .= "    \$fm = \$fb$c > 0 ? (($o1 + $o0) >> (9 - \$fb$c)) : 0;\n";    }    $code .= <<<PHP    $o1 = $o0;    if ($e >= 0x1f0) {        $out = 0;    } else {        \$att = $e + \$tll$s + (\$am$s ? \$lfoAm : 0);        $out = \$lin[\$wt{$s}[(($ph >> 10) + \$fm) & 1023] + ((\$att > 511 ? 511 : \$att) << 3)];    }    $o0 = $out;PHP;    if ($s != 14 && $s != 17) {        $code .= "    if ($e == 511 && $out == 0$keep && $es != 0) {\n        $dd = true;\n    }\n";    }    $code .= "}\n";    return $code;};$indent = fn (string $code, int $n) => implode("\n", array_map(fn ($l) => $l === '' ? '' : str_repeat(' ', $n) . $l, explode("\n", rtrim($code)))) . "\n";$fields = ['eo' => 'eg_out', 'es' => 'eg_state', 'ph' => 'pg_phase', 'oa' => 'output0', 'ob' => 'output1'];$load = '';$store = '';for ($s = 0; $s < 18; $s++) {    foreach ($fields as $p => $f) {        $load .= "\$$p$s = \$opl->{$f}[$s]; ";    }    $load .= "\n[\$rh$s, \$mk$s, \$sh$s, \$st$s] = \$cur[$s]; \$dd$s = \$dead[$s]; \$SL$s = \$opl->SL[$s]; \$pmOn$s = \$opl->PM[$s]; "        . "\$fn$s = \$opl->fnum[$s]; \$ml$s = \$mlv[$s]; \$bk$s = \$opl->blk[$s]; \$dph$s = \$dph[$s]; \$tll$s = \$opl->tll[$s]; "        . "\$am$s = \$opl->AM[$s]; \$wt$s = \$wt[$s];\n";    $store .= "\$opl->eg_out[$s] = \$eo$s; \$opl->eg_state[$s] = \$es$s; \$opl->pg_phase[$s] = \$ph$s; \$opl->pg_out[$s] = \$ph$s >> 10; "        . "\$opl->output0[$s] = \$oa$s; \$opl->output1[$s] = \$ob$s; \$opl->eg_rate_h[$s] = \$rh$s; \$opl->eg_shift[$s] = \$sh$s; "        . "\$opl->eg_rate_l[$s] = \$rh$s ? (\$opl->rks[$s] & 3) : 0;\n";}for ($c = 0; $c < 9; $c++) {    $load .= "\$fb$c = \$opl->FB[" . ($c * 2) . "]; \$alg$c = \$opl->ch_alg[$c]; \$co$c = \$opl->ch_out[$c];\n";    $store .= "\$opl->ch_out[$c] = \$co$c;\n";}$body = '';for ($c = 0; $c < 9; $c++) {    $m = $c * 2;    $t = $m + 1;    $body .= "// ---- channel $c: modulator slot $m, carrier slot $t ----\n";    $body .= $slotCode($m, false, $c);    $body .= $slotCode($t, true, $c);    $body .= "\$co$c = (-(\$alg$c ? \$co + \$mo : \$co)) >> 1;\n";}$sum = '$rest';for ($c = 0; $c < 9; $c++) {    $sum .= " + \$co$c";}$src = <<<PHP<?phpnamespace App\Alis;// GENERATED by tools/port/gen_emu8950_fast.php — do not edit: change the generator.// Port of alis/src/emu8950/emu8950.c (emu8950 v1.1.4, MIT, Mitsutaka Okazaki): the per-sample work of// update_output() + mix_output() + OPL_RateConv_getData() for the melodic mode, unrolled (see OPL_calc_block())./** @internal called by OPL_calc_block() once the block state is prepared. */function opl_calc_block_melodic(OPL \$opl, int \$n, array \$envP, array \$cur, array \$dead, array \$mlv, array \$dph, array \$wt, int \$rest): array{    \$lin = Emu8950Vars::\$lin;    \$pmT = Emu8950Vars::pm_table;    \$amT = Emu8950Vars::am_table;{$indent($load, 4)}    \$pmMode = \$opl->pm_mode;    \$pmPhase = \$opl->pm_phase;    \$pmInc = \$opl->pm_dphase;    \$amPhase = \$opl->am_phase % 13440;    if (\$amPhase < 0) {        \$amPhase += 13440;    }    \$amShift = \$opl->am_mode ? 0 : 2;    \$lfoAm = \$opl->lfo_am;    \$egCounter = \$opl->eg_counter;    \$noise = \$opl->noise;    [\$nj0, \$nj1, \$nj2] = Emu8950Vars::\$noise_jump[18];    \$reg4 = \$opl->reg[0x04];    \$t1 = \$opl->timer1_counter;    \$t2 = \$opl->timer2_counter;    \$status = \$opl->status;    \$r2 = \$opl->reg[0x02] << 2;    \$r3 = \$opl->reg[0x03] << 4;    \$outStep = \$opl->out_step;    \$inpStep = \$opl->inp_step;    \$outTime = \$opl->out_time;    \$conv = \$opl->conv;    if (\$conv) {        \$hist = \$conv->buf[0];        \$timer = \$conv->timer;        \$fratio = \$conv->f_ratio;        \$coef = \$conv->coef;        \$sinc = \$conv->sinc_table;    } else {        \$hist = [];        \$timer = 0.0;        \$fratio = 0.0;        \$coef = [];        \$sinc = [];    }    \$hp = count(\$hist);    \$mix = \$opl->mix_out[0];    \$res = [];    for (\$k = 0; \$k < \$n; \$k++) {        while (\$outStep > \$outTime) {            \$outTime += \$inpStep;            // update_timer() (no CSM here)            if (\$reg4 & 3) {                if (\$reg4 & 1) {                    \$t1++;                    if (\$t1 >> 10) {                        \$status |= 0x40;                        \$t1 = \$r2;                    }                }                if (\$reg4 & 2) {                    \$t2++;                    if (\$t2 >> 12) {                        \$status |= 0x20;                        \$t2 = \$r3;                    }                }            }            // update_ampm() (test_flag = 0)            \$pmPhase = (\$pmPhase + \$pmInc) & 0x3fffff;            if (++\$amPhase >= 13440) {                \$amPhase -= 13440;            }            \$lfoAm = \$amT[\$amPhase >> 6] >> \$amShift;            \$egCounter++;            \$cnt = \$egCounter & 0xffff;{$indent($body, 12)}            // update_noise(14 + 2 + 2)            \$noise = \$nj0[\$noise & 0xff] ^ \$nj1[(\$noise >> 8) & 0xff] ^ \$nj2[\$noise >> 16];            // mix_output(): int16 sum            \$mix = (({$sum} + 0x8000) & 0xffff) - 0x8000;            \$hist[\$hp++] = \$mix;        }        \$outTime -= \$outStep;        if (\$conv) {            // OPL_RateConv_getData()            \$timer += \$fratio;            \$dn = \$timer - floor(\$timer);            \$timer = \$dn;            \$d = \$dn * 256.0;            \$fd = (int) \$d;            \$fr = \$d - \$fd;            \$b = \$hp - 16;            if (\$fr > 1e-9 && \$fr < 0.999999999) {                \$cf = \$coef[\$fd];                \$acc = \$hist[\$b] * \$cf[0] + \$hist[\$b + 1] * \$cf[1] + \$hist[\$b + 2] * \$cf[2] + \$hist[\$b + 3] * \$cf[3]                    + \$hist[\$b + 4] * \$cf[4] + \$hist[\$b + 5] * \$cf[5] + \$hist[\$b + 6] * \$cf[6] + \$hist[\$b + 7] * \$cf[7]                    + \$hist[\$b + 8] * \$cf[8] + \$hist[\$b + 9] * \$cf[9] + \$hist[\$b + 10] * \$cf[10] + \$hist[\$b + 11] * \$cf[11]                    + \$hist[\$b + 12] * \$cf[12] + \$hist[\$b + 13] * \$cf[13] + \$hist[\$b + 14] * \$cf[14] + \$hist[\$b + 15] * \$cf[15];            } else {                \$acc = 0;                for (\$j = 0; \$j < 16; \$j++) {                    \$idx = (int) (((\$j - 7.0) - \$dn) * 256.0);                    \$idx = ((\$idx + 0x8000) & 0xffff) - 0x8000;                    if (\$idx < 0) {                        \$idx = -\$idx;                    }                    \$acc += \$hist[\$b + \$j] * \$sinc[\$idx > 2047 ? 2047 : \$idx];                }            }            \$res[] = (((\$acc >> 12) + 0x8000) & 0xffff) - 0x8000;            if (\$hp > 2048) {                \$hist = array_slice(\$hist, \$hp - 16);                \$hp = 16;            }        } else {            \$res[] = \$mix;        }    }{$indent($store, 4)}    \$opl->pm_phase = \$pmPhase;    \$opl->am_phase = \$amPhase;    \$opl->lfo_am = \$lfoAm;    \$opl->eg_counter = s32(\$egCounter);    \$opl->noise = \$noise;    \$opl->timer1_counter = \$t1;    \$opl->timer2_counter = \$t2;    \$opl->status = \$status;    \$opl->out_time = \$outTime;    if (\$conv) {        \$conv->buf[0] = array_slice(\$hist, \$hp - 16, 16);        \$conv->timer = \$timer;        \$opl->mix_out[0] = \$res[\$n - 1];    } else {        \$opl->mix_out[0] = \$mix;    }    return \$res;}PHP;file_put_contents(__DIR__ . '/../../src/Alis/fn/emu8950_fast.php', $src);echo "written src/Alis/fn/emu8950_fast.php (" . substr_count($src, "\n") . " lines)\n";