<?phpnamespace App\Alis;// Port of alis/src/emu2149/emu2149.c (emu2149 v1.41, MIT, Mitsutaka Okazaki), used by the ALIS port// (MIT, Olivier Huguenot, Vadim Kindl).// PORT: PSG_calc_block() is an addition of the port: n successive PSG_calc() in one call, with the// state in local variables (the per-sample function call is too slow in PHP).function internal_refresh(PSG $psg): void{ $f_master = $psg->clk; if ($psg->clk_div) { $f_master = intdiv($f_master, 2); } if ($psg->quality) { $psg->base_incr = 1 << Emu2149Vars::GETA_BITS; $psg->realstep = $f_master; $psg->psgstep = $psg->rate * 8; $psg->psgtime = 0; $psg->freq_limit = intdiv(intdiv($f_master, 16), intdiv($psg->rate, 2)); } else { $psg->base_incr = (int) ((float) $f_master * (1 << Emu2149Vars::GETA_BITS) / 8 / $psg->rate); $psg->freq_limit = 0; }}function PSG_setClock(PSG $psg, int $clock): void{ if ($psg->clk != $clock) { $psg->clk = $clock; internal_refresh($psg); }}function PSG_setClockDivider(PSG $psg, int $enable): void{ if ($psg->clk_div != $enable) { $psg->clk_div = $enable; internal_refresh($psg); }}function PSG_setRate(PSG $psg, int $rate): void{ $r = $rate ? $rate : 44100; if ($psg->rate != $r) { $psg->rate = $r; internal_refresh($psg); }}function PSG_setQuality(PSG $psg, int $q): void{ if ($psg->quality != $q) { $psg->quality = $q; internal_refresh($psg); }}function PSG_new(int $clock, int $rate): PSG{ $psg = new PSG(); PSG_setVolumeMode($psg, 0); $psg->clk = $clock; $psg->clk_div = 0; $psg->rate = $rate ? $rate : 44100; $psg->quality = 0; internal_refresh($psg); PSG_setMask($psg, 0x00); return $psg;}function PSG_setVolumeMode(PSG $psg, int $type): void{ switch ($type) { case 1: $psg->voltbl = 0; /* YM2149 */ break; case 2: $psg->voltbl = 1; /* AY-3-8910 */ break; default: $psg->voltbl = 0; /* fallback: YM2149 */ break; }}function PSG_setMask(?PSG $psg, int $mask): int{ $ret = 0; if ($psg) { $ret = $psg->mask; $psg->mask = $mask; } return $ret;}function PSG_toggleMask(?PSG $psg, int $mask): int{ $ret = 0; if ($psg) { $ret = $psg->mask; $psg->mask ^= $mask; } return $ret;}function PSG_reset(PSG $psg): void{ $psg->base_count = 0; for ($i = 0; $i < 3; $i++) { $psg->count[$i] = 0; $psg->freq[$i] = 0; $psg->edge[$i] = 0; $psg->volume[$i] = 0; $psg->ch_out[$i] = 0; } $psg->mask = 0; for ($i = 0; $i < 16; $i++) { $psg->reg[$i] = 0; } $psg->adr = 0; $psg->noise_seed = 0xffff; $psg->noise_scaler = 0; $psg->noise_count = 0; $psg->noise_freq = 0; $psg->env_ptr = 0; $psg->env_freq = 0; $psg->env_count = 0; $psg->env_pause = 1; $psg->out = 0;}function PSG_delete(PSG $psg): void{}function PSG_readIO(PSG $psg): int{ return $psg->reg[$psg->adr] & 0xff;}function PSG_readReg(PSG $psg, int $reg): int{ return $psg->reg[$reg & 0x1f] & 0xff;}function PSG_writeIO(PSG $psg, int $adr, int $val): void{ if ($adr & 1) { PSG_writeReg($psg, $psg->adr, $val); } else { $psg->adr = $val & 0x1f; }}/** C static inline update_output(): one step of the chip (kept for PSG_calc's quality=1 path). */function psg_update_output(PSG $psg): void{ $psg->base_count += $psg->base_incr; $incr = ($psg->base_count >> Emu2149Vars::GETA_BITS) & 0xff; $psg->base_count &= (1 << Emu2149Vars::GETA_BITS) - 1; /* Envelope */ $psg->env_count = s32($psg->env_count + $incr); if (!ult($psg->env_count, $psg->env_freq)) { if (!$psg->env_pause) { if ($psg->env_face) { $psg->env_ptr = ($psg->env_ptr + 1) & 0x3f; } else { $psg->env_ptr = ($psg->env_ptr + 0x3f) & 0x3f; } } if ($psg->env_ptr & 0x20) { /* if carry or borrow */ if ($psg->env_continue) { if ($psg->env_alternate ^ $psg->env_hold) { $psg->env_face ^= 1; } if ($psg->env_hold) { $psg->env_pause = 1; } $psg->env_ptr = $psg->env_face ? 0 : 0x1f; } else { $psg->env_pause = 1; $psg->env_ptr = 0; } } if ($psg->env_freq >= $incr) { $psg->env_count = s32($psg->env_count - $psg->env_freq); } else { $psg->env_count = 0; } } /* Noise */ $psg->noise_count = ($psg->noise_count + $incr) & 0xff; if ($psg->noise_count >= $psg->noise_freq) { $psg->noise_scaler ^= 1; if ($psg->noise_scaler) { if ($psg->noise_seed & 1) { $psg->noise_seed ^= 0x24000; } $psg->noise_seed >>= 1; } if ($psg->noise_freq >= $incr) { $psg->noise_count = ($psg->noise_count - $psg->noise_freq) & 0xff; } else { $psg->noise_count = 0; } } $noise = $psg->noise_seed & 1; $voltbl = Emu2149Vars::voltbl[$psg->voltbl]; /* Tone */ for ($i = 0; $i < 3; $i++) { $psg->count[$i] = ($psg->count[$i] + $incr) & 0xffff; if ($psg->count[$i] >= $psg->freq[$i]) { $psg->edge[$i] = (int) !$psg->edge[$i]; if ($psg->freq[$i] >= $incr) { $psg->count[$i] = ($psg->count[$i] - $psg->freq[$i]) & 0xffff; } else { $psg->count[$i] = 0; } } if (0 < $psg->freq_limit && $psg->freq[$i] <= $psg->freq_limit) { /* Mute the channel if the pitch is higher than the Nyquist frequency at the current sample rate */ continue; } if ($psg->mask & (1 << $i)) { $psg->ch_out[$i] = 0; continue; } if (($psg->tmask[$i] || $psg->edge[$i]) && ($psg->nmask[$i] || $noise)) { if (!($psg->volume[$i] & 32)) { $psg->ch_out[$i] = $voltbl[$psg->volume[$i] & 31] << 4; } else { $psg->ch_out[$i] = $voltbl[$psg->env_ptr] << 4; } } else { $psg->ch_out[$i] = 0; } }}function psg_mix_output(PSG $psg): int{ return s16($psg->ch_out[0] + $psg->ch_out[1] + $psg->ch_out[2]);}function PSG_calc(PSG $psg): int{ if (!$psg->quality) { return PSG_calc_block($psg, 1)[0]; } /* Simple rate converter (See README for detail). */ while ($psg->realstep > $psg->psgtime) { $psg->psgtime += $psg->psgstep; psg_update_output($psg); $psg->out = s32($psg->out + psg_mix_output($psg)); $psg->out >>= 1; } $psg->psgtime -= $psg->realstep; return $psg->out;}/** * PORT: $n successive PSG_calc() results (int16 each) in one call. * The quality=0 path (the one ALIS uses: sys_init_psg) runs with the chip state in local variables. */function PSG_calc_block(PSG $psg, int $n): array{ if ($psg->quality) { $res = []; for ($k = 0; $k < $n; $k++) { $res[] = PSG_calc($psg); } return $res; } $res = []; $base_count = $psg->base_count; $base_incr = $psg->base_incr; $env_count = $psg->env_count; $env_freq = $psg->env_freq; $env_pause = $psg->env_pause; $env_face = $psg->env_face; $env_ptr = $psg->env_ptr; $env_continue = $psg->env_continue; $env_alternate = $psg->env_alternate; $env_hold = $psg->env_hold; $noise_count = $psg->noise_count; $noise_freq = $psg->noise_freq; $noise_scaler = $psg->noise_scaler; $noise_seed = $psg->noise_seed; [$c0, $c1, $c2] = $psg->count; [$f0, $f1, $f2] = $psg->freq; [$e0, $e1, $e2] = $psg->edge; [$o0, $o1, $o2] = $psg->ch_out; [$v0, $v1, $v2] = $psg->volume; $tm0 = $psg->tmask[0]; $tm1 = $psg->tmask[1]; $tm2 = $psg->tmask[2]; $nm0 = $psg->nmask[0]; $nm1 = $psg->nmask[1]; $nm2 = $psg->nmask[2]; $mask = $psg->mask; $voltbl = Emu2149Vars::voltbl[$psg->voltbl]; // fixed levels of the channels not using the envelope (volume bit 5 clear): voltbl[volume & 31] << 4 $fx0 = $voltbl[$v0 & 31] << 4; $fx1 = $voltbl[$v1 & 31] << 4; $fx2 = $voltbl[$v2 & 31] << 4; $ev0 = $v0 & 32; $ev1 = $v1 & 32; $ev2 = $v2 & 32; // env_count is a u32 compared with env_freq (u16): values below 0 can only come from a wrap (never in practice) for ($k = 0; $k < $n; $k++) { $base_count += $base_incr; $incr = $base_count >> 24; $base_count &= 0xffffff; /* Envelope */ $env_count += $incr; if ($env_count >= $env_freq) { if (!$env_pause) { $env_ptr = $env_face ? (($env_ptr + 1) & 0x3f) : (($env_ptr + 0x3f) & 0x3f); } if ($env_ptr & 0x20) { if ($env_continue) { if ($env_alternate ^ $env_hold) { $env_face ^= 1; } if ($env_hold) { $env_pause = 1; } $env_ptr = $env_face ? 0 : 0x1f; } else { $env_pause = 1; $env_ptr = 0; } } if ($env_freq >= $incr) { $env_count -= $env_freq; } else { $env_count = 0; } } /* Noise */ $noise_count = ($noise_count + $incr) & 0xff; if ($noise_count >= $noise_freq) { $noise_scaler ^= 1; if ($noise_scaler) { if ($noise_seed & 1) { $noise_seed ^= 0x24000; } $noise_seed >>= 1; } if ($noise_freq >= $incr) { $noise_count = ($noise_count - $noise_freq) & 0xff; } else { $noise_count = 0; } } $noise = $noise_seed & 1; /* Tone */ $c0 = ($c0 + $incr) & 0xffff; if ($c0 >= $f0) { $e0 = $e0 ? 0 : 1; $c0 = $f0 >= $incr ? (($c0 - $f0) & 0xffff) : 0; } $c1 = ($c1 + $incr) & 0xffff; if ($c1 >= $f1) { $e1 = $e1 ? 0 : 1; $c1 = $f1 >= $incr ? (($c1 - $f1) & 0xffff) : 0; } $c2 = ($c2 + $incr) & 0xffff; if ($c2 >= $f2) { $e2 = $e2 ? 0 : 1; $c2 = $f2 >= $incr ? (($c2 - $f2) & 0xffff) : 0; } if ($mask & 1) { $o0 = 0; } elseif (($tm0 || $e0) && ($nm0 || $noise)) { $o0 = $ev0 ? ($voltbl[$env_ptr] << 4) : $fx0; } else { $o0 = 0; } if ($mask & 2) { $o1 = 0; } elseif (($tm1 || $e1) && ($nm1 || $noise)) { $o1 = $ev1 ? ($voltbl[$env_ptr] << 4) : $fx1; } else { $o1 = 0; } if ($mask & 4) { $o2 = 0; } elseif (($tm2 || $e2) && ($nm2 || $noise)) { $o2 = $ev2 ? ($voltbl[$env_ptr] << 4) : $fx2; } else { $o2 = 0; } // mix_output(): (int16_t) of at most 3 * 0xff0: no wrap $res[] = $o0 + $o1 + $o2; } $psg->base_count = $base_count; $psg->env_count = $env_count; $psg->env_pause = $env_pause; $psg->env_face = $env_face; $psg->env_ptr = $env_ptr; $psg->noise_count = $noise_count; $psg->noise_scaler = $noise_scaler; $psg->noise_seed = $noise_seed; $psg->count = [$c0, $c1, $c2]; $psg->edge = [$e0, $e1, $e2]; $psg->ch_out = [$o0, $o1, $o2]; if ($n > 0) { $psg->out = $res[$n - 1]; } return $res;}function PSG_writeReg(PSG $psg, int $reg, int $val): void{ if ($reg > 15 || $reg < 0) { return; } $val &= Emu2149Vars::regmsk[$reg]; $psg->reg[$reg] = $val; switch ($reg) { case 0: case 2: case 4: case 1: case 3: case 5: $c = $reg >> 1; $psg->freq[$c] = (($psg->reg[$c * 2 + 1] & 15) << 8) + $psg->reg[$c * 2]; break; case 6: $psg->noise_freq = $val & 31; break; case 7: $psg->tmask[0] = ($val & 1); $psg->tmask[1] = ($val & 2); $psg->tmask[2] = ($val & 4); $psg->nmask[0] = ($val & 8); $psg->nmask[1] = ($val & 16); $psg->nmask[2] = ($val & 32); break; case 8: case 9: case 10: $psg->volume[$reg - 8] = ($val << 1) & 0xff; break; case 11: case 12: $psg->env_freq = ($psg->reg[12] << 8) + $psg->reg[11]; break; case 13: $psg->env_continue = ($val >> 3) & 1; $psg->env_attack = ($val >> 2) & 1; $psg->env_alternate = ($val >> 1) & 1; $psg->env_hold = $val & 1; $psg->env_face = $psg->env_attack; $psg->env_pause = 0; $psg->env_ptr = $psg->env_face ? 0 : 0x1f; break; case 14: case 15: default: break; }}