diff options
Diffstat (limited to 'src/chips/nuked/nukedopl3.c')
-rw-r--r-- | src/chips/nuked/nukedopl3.c | 600 |
1 files changed, 365 insertions, 235 deletions
diff --git a/src/chips/nuked/nukedopl3.c b/src/chips/nuked/nukedopl3.c index ef8291a..8cbfd2a 100644 --- a/src/chips/nuked/nukedopl3.c +++ b/src/chips/nuked/nukedopl3.c @@ -1,20 +1,21 @@ -/* - * Copyright (C) 2013-2018 Alexey Khokholov (Nuke.YKT) +/* Nuked OPL3 + * Copyright (C) 2013-2020 Nuke.YKT * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * This file is part of Nuked OPL3. * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. + * Nuked OPL3 is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 + * of the License, or (at your option) any later version. * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Nuked OPL3 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * + * You should have received a copy of the GNU Lesser General Public License + * along with Nuked OPL3. If not, see <https://www.gnu.org/licenses/>. + * Nuked OPL3 emulator. * Thanks: * MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh): @@ -34,6 +35,20 @@ #include <string.h> #include "nukedopl3.h" +#if OPL_ENABLE_STEREOEXT && !defined OPL_SIN +#ifndef _USE_MATH_DEFINES +#define _USE_MATH_DEFINES 1 +#endif +#include <math.h> +/* input: [0, 256), output: [0, 65536] */ +#define OPL_SIN(x) ((int32_t)(sin((x) * M_PI / 512.0) * 65536.0)) +#endif + +/* Quirk: Some FM channels are output one sample later on the left side than the right. */ +#ifndef OPL_QUIRK_CHANNELSAMPLEDELAY +#define OPL_QUIRK_CHANNELSAMPLEDELAY (!OPL_ENABLE_STEREOEXT) +#endif + #define RSM_FRAC 10 /* Channel types */ @@ -55,10 +70,10 @@ enum { #if OPL_FAST_WAVEGEN /* - * logsin table - */ + logsin table +*/ -static const Bit16u logsinrom[512] = { +static const uint16_t logsinrom[512] = { 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471, 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365, 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd, @@ -126,10 +141,10 @@ static const Bit16u logsinrom[512] = { }; /* - * exp table - */ + exp table +*/ -static const Bit16u exprom[256] = { +static const uint16_t exprom[256] = { 0xff4, 0xfea, 0xfde, 0xfd4, 0xfc8, 0xfbe, 0xfb4, 0xfa8, 0xf9e, 0xf92, 0xf88, 0xf7e, 0xf72, 0xf68, 0xf5c, 0xf52, 0xf48, 0xf3e, 0xf32, 0xf28, 0xf1e, 0xf14, 0xf08, 0xefe, @@ -165,10 +180,10 @@ static const Bit16u exprom[256] = { }; #else /* - * logsin table - */ + logsin table +*/ -static const Bit16u logsinrom[256] = { +static const uint16_t logsinrom[256] = { 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471, 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365, 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd, @@ -204,10 +219,10 @@ static const Bit16u logsinrom[256] = { }; /* - * exp table - */ + exp table +*/ -static const Bit16u exprom[256] = { +static const uint16_t exprom[256] = { 0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4, 0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9, 0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f, @@ -244,32 +259,32 @@ static const Bit16u exprom[256] = { #endif /* - * freq mult table multiplied by 2 - * - * 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15 - */ + freq mult table multiplied by 2 + + 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15 +*/ -static const Bit8u mt[16] = { +static const uint8_t mt[16] = { 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30 }; /* - * ksl table - */ + ksl table +*/ -static const Bit8u kslrom[16] = { +static const uint8_t kslrom[16] = { 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64 }; -static const Bit8u kslshift[4] = { +static const uint8_t kslshift[4] = { 8, 1, 2, 0 }; /* - * envelope generator constants - */ + envelope generator constants +*/ -static const Bit8u eg_incstep[4][4] = { +static const uint8_t eg_incstep[4][4] = { { 0, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 0, 1, 0 }, @@ -277,23 +292,32 @@ static const Bit8u eg_incstep[4][4] = { }; /* - * address decoding - */ + address decoding +*/ -static const Bit8s ad_slot[0x20] = { +static const int8_t ad_slot[0x20] = { 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1, 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; -static const Bit8u ch_slot[18] = { +static const uint8_t ch_slot[18] = { 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32 }; +#if OPL_ENABLE_STEREOEXT /* - * Pan law table - */ + stereo extension panning table +*/ + +static int32_t panpot_lut[256]; +static uint8_t panpot_lut_build = 0; +#endif -static const Bit16u panlawtable[] = +/* + Pan law table +*/ + +static const uint16_t panlawtable[] = { 65535, 65529, 65514, 65489, 65454, 65409, 65354, 65289, 65214, 65129, 65034, 64929, 64814, 64689, 64554, 64410, @@ -316,13 +340,14 @@ static const Bit16u panlawtable[] = }; /* - * Envelope generator - */ + Envelope generator +*/ #if !OPL_FAST_WAVEGEN -typedef Bit16s(*envelope_sinfunc)(Bit16u phase, Bit16u envelope); +typedef int16_t(*envelope_sinfunc)(uint16_t phase, uint16_t envelope); +typedef void(*envelope_genfunc)(opl3_slot *slott); -static Bit16s OPL3_EnvelopeCalcExp(Bit32u level) +static int16_t OPL3_EnvelopeCalcExp(uint32_t level) { if (level > 0x1fff) { @@ -331,10 +356,10 @@ static Bit16s OPL3_EnvelopeCalcExp(Bit32u level) return (exprom[level & 0xff] << 1) >> (level >> 8); } -static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin0(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; - Bit16u neg = 0; + uint16_t out = 0; + uint16_t neg = 0; phase &= 0x3ff; if (phase & 0x200) { @@ -351,9 +376,9 @@ static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg; } -static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin1(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; + uint16_t out = 0; phase &= 0x3ff; if (phase & 0x200) { @@ -370,9 +395,9 @@ static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(out + (envelope << 3)); } -static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin2(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; + uint16_t out = 0; phase &= 0x3ff; if (phase & 0x100) { @@ -385,9 +410,9 @@ static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(out + (envelope << 3)); } -static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin3(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; + uint16_t out = 0; phase &= 0x3ff; if (phase & 0x100) { @@ -400,10 +425,10 @@ static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(out + (envelope << 3)); } -static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin4(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; - Bit16u neg = 0; + uint16_t out = 0; + uint16_t neg = 0; phase &= 0x3ff; if ((phase & 0x300) == 0x100) { @@ -424,9 +449,9 @@ static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg; } -static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin5(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; + uint16_t out = 0; phase &= 0x3ff; if (phase & 0x200) { @@ -443,9 +468,9 @@ static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(out + (envelope << 3)); } -static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin6(uint16_t phase, uint16_t envelope) { - Bit16u neg = 0; + uint16_t neg = 0; phase &= 0x3ff; if (phase & 0x200) { @@ -454,10 +479,10 @@ static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope) return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg; } -static Bit16s OPL3_EnvelopeCalcSin7(Bit16u phase, Bit16u envelope) +static int16_t OPL3_EnvelopeCalcSin7(uint16_t phase, uint16_t envelope) { - Bit16u out = 0; - Bit16u neg = 0; + uint16_t out = 0; + uint16_t neg = 0; phase &= 0x3ff; if (phase & 0x200) { @@ -490,28 +515,28 @@ enum envelope_gen_num static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot) { - Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2) + int16_t ksl = (kslrom[slot->channel->f_num >> 6] << 2) - ((0x08 - slot->channel->block) << 5); if (ksl < 0) { ksl = 0; } - slot->eg_ksl = (Bit8u)ksl; + slot->eg_ksl = (uint8_t)ksl; } static void OPL3_EnvelopeCalc(opl3_slot *slot) { - Bit8u nonzero; - Bit8u rate; - Bit8u rate_hi; - Bit8u rate_lo; - Bit8u reg_rate = 0; - Bit8u ks; - Bit8u eg_shift, shift; - Bit16u eg_rout; - Bit16s eg_inc; - Bit8u eg_off; - Bit8u reset = 0; + uint8_t nonzero; + uint8_t rate; + uint8_t rate_hi; + uint8_t rate_lo; + uint8_t reg_rate = 0; + uint8_t ks; + uint8_t eg_shift, shift; + uint16_t eg_rout; + int16_t eg_inc; + uint8_t eg_off; + uint8_t reset = 0; slot->eg_out = slot->eg_rout + (slot->reg_tl << 2) + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem; @@ -622,7 +647,7 @@ static void OPL3_EnvelopeCalc(opl3_slot *slot) } else if (slot->key && shift > 0 && rate_hi != 0x0f) { - eg_inc = ((~slot->eg_rout) << shift) >> 4; + eg_inc = ~slot->eg_rout >> (4 - shift); } break; case envelope_gen_num_decay: @@ -655,35 +680,35 @@ static void OPL3_EnvelopeCalc(opl3_slot *slot) } } -static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type) +static void OPL3_EnvelopeKeyOn(opl3_slot *slot, uint8_t type) { slot->key |= type; } -static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type) +static void OPL3_EnvelopeKeyOff(opl3_slot *slot, uint8_t type) { slot->key &= ~type; } /* - * Phase Generator - */ + Phase Generator +*/ static void OPL3_PhaseGenerate(opl3_slot *slot) { opl3_chip *chip; - Bit16u f_num; - Bit32u basefreq; - Bit8u rm_xor, n_bit; - Bit32u noise; - Bit16u phase; + uint16_t f_num; + uint32_t basefreq; + uint8_t rm_xor, n_bit; + uint32_t noise; + uint16_t phase; chip = slot->chip; f_num = slot->channel->f_num; if (slot->reg_vib) { - Bit8s range; - Bit8u vibpos; + int8_t range; + uint8_t vibpos; range = (f_num >> 7) & 7; vibpos = slot->chip->vibpos; @@ -705,7 +730,7 @@ static void OPL3_PhaseGenerate(opl3_slot *slot) f_num += range; } basefreq = (f_num << slot->channel->block) >> 1; - phase = (Bit16u)(slot->pg_phase >> 9); + phase = (uint16_t)(slot->pg_phase >> 9); if (slot->pg_reset) { slot->pg_phase = 0; @@ -760,10 +785,10 @@ static void OPL3_PhaseGenerate(opl3_slot *slot) } /* - * Slot - */ + Slot +*/ -static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data) +static void OPL3_SlotWrite20(opl3_slot *slot, uint8_t data) { if ((data >> 7) & 0x01) { @@ -771,7 +796,7 @@ static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data) } else { - slot->trem = (Bit8u*)&slot->chip->zeromod; + slot->trem = (uint8_t*)&slot->chip->zeromod; } slot->reg_vib = (data >> 6) & 0x01; slot->reg_type = (data >> 5) & 0x01; @@ -779,20 +804,20 @@ static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data) slot->reg_mult = data & 0x0f; } -static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data) +static void OPL3_SlotWrite40(opl3_slot *slot, uint8_t data) { slot->reg_ksl = (data >> 6) & 0x03; slot->reg_tl = data & 0x3f; OPL3_EnvelopeUpdateKSL(slot); } -static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data) +static void OPL3_SlotWrite60(opl3_slot *slot, uint8_t data) { slot->reg_ar = (data >> 4) & 0x0f; slot->reg_dr = data & 0x0f; } -static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data) +static void OPL3_SlotWrite80(opl3_slot *slot, uint8_t data) { slot->reg_sl = (data >> 4) & 0x0f; if (slot->reg_sl == 0x0f) @@ -802,7 +827,7 @@ static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data) slot->reg_rr = data & 0x0f; } -static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data) +static void OPL3_SlotWriteE0(opl3_slot *slot, uint8_t data) { slot->reg_wf = data & 0x07; if (slot->chip->newm == 0x00) @@ -863,9 +888,9 @@ static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data) #if OPL_FAST_WAVEGEN static void OPL3_SlotGenerate(opl3_slot *slot) { - Bit16u phase = slot->pg_phase_out + *slot->mod; - Bit32u neg, level; - Bit8u phaseshift; + uint16_t phase = slot->pg_phase_out + *slot->mod; + uint32_t neg, level; + uint8_t phaseshift; /* Fast paths for mute segments */ if (phase & slot->maskzero) @@ -874,7 +899,7 @@ static void OPL3_SlotGenerate(opl3_slot *slot) return; } - neg = (Bit32s)((Bit32u)phase << slot->signpos) >> 31; + neg = (int32_t)((uint32_t)phase << slot->signpos) >> 31; phaseshift = slot->phaseshift; level = slot->eg_out; @@ -910,17 +935,17 @@ static void OPL3_SlotCalcFB(opl3_slot *slot) } /* - * Channel - */ + Channel +*/ static void OPL3_ChannelSetupAlg(opl3_channel *channel); -static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data) +static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, uint8_t data) { opl3_channel *channel6; opl3_channel *channel7; opl3_channel *channel8; - Bit8u chnum; + uint8_t chnum; chip->rhy = data & 0x3f; if (chip->rhy & 0x20) @@ -1007,7 +1032,7 @@ static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data) } } -static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data) +static void OPL3_ChannelWriteA0(opl3_channel *channel, uint8_t data) { if (channel->chip->newm && channel->chtype == ch_4op2) { @@ -1027,7 +1052,7 @@ static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data) } } -static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data) +static void OPL3_ChannelWriteB0(opl3_channel *channel, uint8_t data) { if (channel->chip->newm && channel->chtype == ch_4op2) { @@ -1150,10 +1175,8 @@ static void OPL3_ChannelSetupAlg(opl3_channel *channel) } } -static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data) +static void OPL3_ChannelUpdateAlg(opl3_channel *channel) { - channel->fb = (data & 0x0e) >> 1; - channel->con = data & 0x01; channel->alg = channel->con; if (channel->chip->newm) { @@ -1178,17 +1201,46 @@ static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data) { OPL3_ChannelSetupAlg(channel); } +} + +static void OPL3_ChannelWriteC0(opl3_channel *channel, uint8_t data) +{ + channel->fb = (data & 0x0e) >> 1; + channel->con = data & 0x01; + OPL3_ChannelUpdateAlg(channel); if (channel->chip->newm) { channel->cha = ((data >> 4) & 0x01) ? ~0 : 0; channel->chb = ((data >> 5) & 0x01) ? ~0 : 0; + channel->chc = ((data >> 6) & 0x01) ? ~0 : 0; + channel->chd = ((data >> 7) & 0x01) ? ~0 : 0; } else { - channel->cha = channel->chb = (Bit16u)~0; + channel->cha = channel->chb = (uint16_t)~0; + /* TODO: Verify on real chip if DAC2 output is disabled in compat mode */ + channel->chc = channel->chd = 0; + } +#if OPL_ENABLE_STEREOEXT + if (!channel->chip->stereoext) + { + channel->leftpan = channel->cha << 16; + channel->rightpan = channel->chb << 16; } +#endif } +#if OPL_ENABLE_STEREOEXT +static void OPL3_ChannelWriteD0(opl3_channel* channel, uint8_t data) +{ + if (channel->chip->stereoext) + { + channel->leftpan = panpot_lut[data ^ 0xff]; + channel->rightpan = panpot_lut[data]; + } +} +#endif + static void OPL3_ChannelKeyOn(opl3_channel *channel) { if (channel->chip->newm) @@ -1237,10 +1289,10 @@ static void OPL3_ChannelKeyOff(opl3_channel *channel) } } -static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data) +static void OPL3_ChannelSet4Op(opl3_chip *chip, uint8_t data) { - Bit8u bit; - Bit8u chnum; + uint8_t bit; + uint8_t chnum; for (bit = 0; bit < 6; bit++) { chnum = bit; @@ -1252,16 +1304,19 @@ static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data) { chip->channel[chnum].chtype = ch_4op; chip->channel[chnum + 3].chtype = ch_4op2; + OPL3_ChannelUpdateAlg(&chip->channel[chnum]); } else { chip->channel[chnum].chtype = ch_2op; chip->channel[chnum + 3].chtype = ch_2op; + OPL3_ChannelUpdateAlg(&chip->channel[chnum]); + OPL3_ChannelUpdateAlg(&chip->channel[chnum+3]); } } } -static Bit16s OPL3_ClipSample(Bit32s sample) +static int16_t OPL3_ClipSample(int32_t sample) { if (sample > 32767) { @@ -1271,73 +1326,94 @@ static Bit16s OPL3_ClipSample(Bit32s sample) { sample = -32768; } - return (Bit16s)sample; + return (int16_t)sample; } -void OPL3_Generate(opl3_chip *chip, Bit16s *buf) +static void OPL3_ProcessSlot(opl3_slot *slot) { - Bit8u ii; - Bit8u jj; - Bit16s accm; - Bit8u shift = 0; - - buf[1] = OPL3_ClipSample(chip->mixbuff[1]); + OPL3_SlotCalcFB(slot); + OPL3_EnvelopeCalc(slot); + OPL3_PhaseGenerate(slot); + OPL3_SlotGenerate(slot); +} +inline void OPL3_Generate4Ch(opl3_chip *chip, int16_t *buf4) +{ + opl3_channel *channel; + opl3_writebuf *writebuf; + int16_t **out; + int32_t mix[2]; + uint8_t ii; + int16_t accm; + uint8_t shift = 0; + + buf4[1] = OPL3_ClipSample(chip->mixbuff[1]); + buf4[3] = OPL3_ClipSample(chip->mixbuff[3]); + +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (ii = 0; ii < 15; ii++) +#else + for (ii = 0; ii < 36; ii++) +#endif { - OPL3_SlotCalcFB(&chip->slot[ii]); - OPL3_EnvelopeCalc(&chip->slot[ii]); - OPL3_PhaseGenerate(&chip->slot[ii]); - OPL3_SlotGenerate(&chip->slot[ii]); + OPL3_ProcessSlot(&chip->slot[ii]); } - chip->mixbuff[0] = 0; + mix[0] = mix[1] = 0; for (ii = 0; ii < 18; ii++) { - accm = 0; - for (jj = 0; jj < 4; jj++) - { - accm += *chip->channel[ii].out[jj]; - } - chip->mixbuff[0] += (Bit16s)((accm * chip->channel[ii].chl / 65535) & chip->channel[ii].cha); + channel = &chip->channel[ii]; + out = channel->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix[0] += (int16_t)((accm * channel->leftpan) >> 16); +#else + mix[0] += (int16_t)((accm * chip->channel[ii].chl / 65535) & channel->cha); +#endif + mix[1] += (int16_t)(accm & channel->chc); } + chip->mixbuff[0] = mix[0]; + chip->mixbuff[2] = mix[1]; +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (ii = 15; ii < 18; ii++) { - OPL3_SlotCalcFB(&chip->slot[ii]); - OPL3_EnvelopeCalc(&chip->slot[ii]); - OPL3_PhaseGenerate(&chip->slot[ii]); - OPL3_SlotGenerate(&chip->slot[ii]); + OPL3_ProcessSlot(&chip->slot[ii]); } +#endif - buf[0] = OPL3_ClipSample(chip->mixbuff[0]); + buf4[0] = OPL3_ClipSample(chip->mixbuff[0]); + buf4[2] = OPL3_ClipSample(chip->mixbuff[2]); +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (ii = 18; ii < 33; ii++) { - OPL3_SlotCalcFB(&chip->slot[ii]); - OPL3_EnvelopeCalc(&chip->slot[ii]); - OPL3_PhaseGenerate(&chip->slot[ii]); - OPL3_SlotGenerate(&chip->slot[ii]); + OPL3_ProcessSlot(&chip->slot[ii]); } +#endif - chip->mixbuff[1] = 0; + mix[0] = mix[1] = 0; for (ii = 0; ii < 18; ii++) { - accm = 0; - for (jj = 0; jj < 4; jj++) - { - accm += *chip->channel[ii].out[jj]; - } - chip->mixbuff[1] += (Bit16s)((accm * chip->channel[ii].chr / 65535) & chip->channel[ii].chb); + channel = &chip->channel[ii]; + out = channel->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix[0] += (int16_t)((accm * channel->rightpan) >> 16); +#else + mix[0] += (int16_t)((accm * chip->channel[ii].chr / 65535) & channel->chb); +#endif + mix[1] += (int16_t)(accm & channel->chd); } + chip->mixbuff[1] = mix[0]; + chip->mixbuff[3] = mix[1]; +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (ii = 33; ii < 36; ii++) { - OPL3_SlotCalcFB(&chip->slot[ii]); - OPL3_EnvelopeCalc(&chip->slot[ii]); - OPL3_PhaseGenerate(&chip->slot[ii]); - OPL3_SlotGenerate(&chip->slot[ii]); + OPL3_ProcessSlot(&chip->slot[ii]); } +#endif if ((chip->timer & 0x3f) == 0x3f) { @@ -1378,7 +1454,7 @@ void OPL3_Generate(opl3_chip *chip, Bit16s *buf) if (chip->eg_timerrem || chip->eg_state) { - if (chip->eg_timer == 0xfffffffffULL) + if (chip->eg_timer == UINT64_C(0xfffffffff)) { chip->eg_timer = 0; chip->eg_timerrem = 1; @@ -1392,109 +1468,152 @@ void OPL3_Generate(opl3_chip *chip, Bit16s *buf) chip->eg_state ^= 1; - while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt) + while ((writebuf = &chip->writebuf[chip->writebuf_cur]), writebuf->time <= chip->writebuf_samplecnt) { - if (!(chip->writebuf[chip->writebuf_cur].reg & 0x200)) + if (!(writebuf->reg & 0x200)) { break; } - chip->writebuf[chip->writebuf_cur].reg &= 0x1ff; - OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_cur].reg, - chip->writebuf[chip->writebuf_cur].data); + writebuf->reg &= 0x1ff; + OPL3_WriteReg(chip, writebuf->reg, writebuf->data); chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE; } chip->writebuf_samplecnt++; } -void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf) +void OPL3_Generate(opl3_chip *chip, int16_t *buf) +{ + int16_t samples[4]; + OPL3_Generate4Ch(chip, samples); + buf[0] = samples[0]; + buf[1] = samples[1]; +} + +void OPL3_Generate4ChResampled(opl3_chip *chip, int16_t *buf4) { while (chip->samplecnt >= chip->rateratio) { chip->oldsamples[0] = chip->samples[0]; chip->oldsamples[1] = chip->samples[1]; - OPL3_Generate(chip, chip->samples); + chip->oldsamples[2] = chip->samples[2]; + chip->oldsamples[3] = chip->samples[3]; + OPL3_Generate4Ch(chip, chip->samples); chip->samplecnt -= chip->rateratio; } - buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt) - + chip->samples[0] * chip->samplecnt) / chip->rateratio); - buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt) - + chip->samples[1] * chip->samplecnt) / chip->rateratio); + buf4[0] = (int16_t)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt) + + chip->samples[0] * chip->samplecnt) / chip->rateratio); + buf4[1] = (int16_t)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt) + + chip->samples[1] * chip->samplecnt) / chip->rateratio); + buf4[2] = (int16_t)((chip->oldsamples[2] * (chip->rateratio - chip->samplecnt) + + chip->samples[2] * chip->samplecnt) / chip->rateratio); + buf4[3] = (int16_t)((chip->oldsamples[3] * (chip->rateratio - chip->samplecnt) + + chip->samples[3] * chip->samplecnt) / chip->rateratio); chip->samplecnt += 1 << RSM_FRAC; } -void OPL3_Reset(opl3_chip *chip, Bit32u samplerate) +void OPL3_GenerateResampled(opl3_chip *chip, int16_t *buf) +{ + int16_t samples[4]; + OPL3_Generate4ChResampled(chip, samples); + buf[0] = samples[0]; + buf[1] = samples[1]; +} + +void OPL3_Reset(opl3_chip *chip, uint32_t samplerate) { - Bit8u slotnum; - Bit8u channum; + opl3_slot *slot; + opl3_channel *channel; + uint8_t slotnum; + uint8_t channum; + uint8_t local_ch_slot; memset(chip, 0, sizeof(opl3_chip)); for (slotnum = 0; slotnum < 36; slotnum++) { - chip->slot[slotnum].chip = chip; - chip->slot[slotnum].mod = &chip->zeromod; - chip->slot[slotnum].eg_rout = 0x1ff; + slot = &chip->slot[slotnum]; + slot->chip = chip; + slot->mod = &chip->zeromod; + slot->eg_rout = 0x1ff; #if OPL_FAST_WAVEGEN - chip->slot[slotnum].eg_out = 0x1ff << 3; + slot->eg_out = 0x1ff << 3; #else - chip->slot[slotnum].eg_out = 0x1ff; + slot->eg_out = 0x1ff; #endif - chip->slot[slotnum].eg_gen = envelope_gen_num_release; - chip->slot[slotnum].trem = (Bit8u*)&chip->zeromod; - chip->slot[slotnum].slot_num = slotnum; + slot->eg_gen = envelope_gen_num_release; + slot->trem = (uint8_t*)&chip->zeromod; + slot->slot_num = slotnum; #if OPL_FAST_WAVEGEN - chip->slot[slotnum].signpos = (31-9); /* for wf=0 need use sigext of (phase & 0x200) */ + slot->signpos = (31-9); /* for wf=0 need use sigext of (phase & 0x200) */ #endif } for (channum = 0; channum < 18; channum++) { - chip->channel[channum].slotz[0] = &chip->slot[ch_slot[channum]]; - chip->channel[channum].slotz[1] = &chip->slot[ch_slot[channum] + 3]; - chip->slot[ch_slot[channum]].channel = &chip->channel[channum]; - chip->slot[ch_slot[channum] + 3].channel = &chip->channel[channum]; + channel = &chip->channel[channum]; + local_ch_slot = ch_slot[channum]; + channel->slotz[0] = &chip->slot[local_ch_slot]; + channel->slotz[1] = &chip->slot[local_ch_slot + 3]; + chip->slot[local_ch_slot].channel = channel; + chip->slot[local_ch_slot + 3].channel = channel; if ((channum % 9) < 3) { - chip->channel[channum].pair = &chip->channel[channum + 3]; + channel->pair = &chip->channel[channum + 3]; } else if ((channum % 9) < 6) { - chip->channel[channum].pair = &chip->channel[channum - 3]; + channel->pair = &chip->channel[channum - 3]; } - chip->channel[channum].chip = chip; - chip->channel[channum].out[0] = &chip->zeromod; - chip->channel[channum].out[1] = &chip->zeromod; - chip->channel[channum].out[2] = &chip->zeromod; - chip->channel[channum].out[3] = &chip->zeromod; - chip->channel[channum].chtype = ch_2op; - chip->channel[channum].cha = 0xffff; - chip->channel[channum].chb = 0xffff; - chip->channel[channum].chl = 46340; - chip->channel[channum].chr = 46340; - chip->channel[channum].ch_num = channum; - OPL3_ChannelSetupAlg(&chip->channel[channum]); + channel->chip = chip; + channel->out[0] = &chip->zeromod; + channel->out[1] = &chip->zeromod; + channel->out[2] = &chip->zeromod; + channel->out[3] = &chip->zeromod; + channel->chtype = ch_2op; + channel->cha = 0xffff; + channel->chb = 0xffff; + channel->chl = 46340; + channel->chr = 46340; +#if OPL_ENABLE_STEREOEXT + channel->leftpan = 0x10000; + channel->rightpan = 0x10000; +#endif + channel->ch_num = channum; + OPL3_ChannelSetupAlg(channel); } chip->noise = 1; chip->rateratio = (samplerate << RSM_FRAC) / 49716; chip->tremoloshift = 4; chip->vibshift = 1; + +#if OPL_ENABLE_STEREOEXT + if (!panpot_lut_build) + { + int32_t i; + for (i = 0; i < 256; i++) + { + panpot_lut[i] = OPL_SIN(i); + } + panpot_lut_build = 1; + } +#endif } -static void OPL3_ChannelWritePan(opl3_channel *channel, Bit8u data) +static void OPL3_ChannelWritePan(opl3_channel *channel, uint8_t data) { channel->chl = panlawtable[data & 0x7F]; channel->chr = panlawtable[0x7F - (data & 0x7F)]; } -void OPL3_WritePan(opl3_chip *chip, Bit16u reg, Bit8u v) +void OPL3_WritePan(opl3_chip *chip, uint16_t reg, uint8_t v) { - Bit8u high = (reg >> 8) & 0x01; - Bit8u regm = reg & 0xff; + uint8_t high = (reg >> 8) & 0x01; + uint8_t regm = reg & 0xff; OPL3_ChannelWritePan(&chip->channel[9 * high + (regm & 0x0f)], v); } -void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v) +void OPL3_WriteReg(opl3_chip *chip, uint16_t reg, uint8_t v) { - Bit8u high = (reg >> 8) & 0x01; - Bit8u regm = reg & 0xff; + uint8_t high = (reg >> 8) & 0x01; + uint8_t regm = reg & 0xff; switch (regm & 0xf0) { case 0x00: @@ -1507,6 +1626,9 @@ void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v) break; case 0x05: chip->newm = v & 0x01; +#if OPL_ENABLE_STEREOEXT + chip->stereoext = (v >> 1) & 0x01; +#endif break; } } @@ -1587,24 +1709,36 @@ void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v) OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v); } break; +#if OPL_ENABLE_STEREOEXT + case 0xd0: + if ((regm & 0x0f) < 9) + { + OPL3_ChannelWriteD0(&chip->channel[9 * high + (regm & 0x0f)], v); + } + break; +#endif } } -void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v) +void OPL3_WriteRegBuffered(opl3_chip *chip, uint16_t reg, uint8_t v) { - Bit64u time1, time2; + uint64_t time1, time2; + opl3_writebuf *writebuf; + uint32_t writebuf_last; - if (chip->writebuf[chip->writebuf_last].reg & 0x200) + writebuf_last = chip->writebuf_last; + writebuf = &chip->writebuf[writebuf_last]; + + if (writebuf->reg & 0x200) { - OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_last].reg & 0x1ff, - chip->writebuf[chip->writebuf_last].data); + OPL3_WriteReg(chip, writebuf->reg & 0x1ff, writebuf->data); - chip->writebuf_cur = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE; - chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time; + chip->writebuf_cur = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; + chip->writebuf_samplecnt = writebuf->time; } - chip->writebuf[chip->writebuf_last].reg = reg | 0x200; - chip->writebuf[chip->writebuf_last].data = v; + writebuf->reg = reg | 0x200; + writebuf->data = v; time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY; time2 = chip->writebuf_samplecnt; @@ -1613,39 +1747,35 @@ void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v) time1 = time2; } - chip->writebuf[chip->writebuf_last].time = time1; + writebuf->time = time1; chip->writebuf_lasttime = time1; - chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE; + chip->writebuf_last = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; } -void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples) +void OPL3_Generate4ChStream(opl3_chip *chip, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) { - Bit32u i; + uint_fast32_t i; + int16_t samples[4]; for(i = 0; i < numsamples; i++) { - OPL3_GenerateResampled(chip, sndptr); - sndptr += 2; + OPL3_Generate4ChResampled(chip, samples); + sndptr1[0] = samples[0]; + sndptr1[1] = samples[1]; + sndptr2[0] = samples[2]; + sndptr2[1] = samples[3]; + sndptr1 += 2; + sndptr2 += 2; } } -#define OPL3_MIN(A, B) (((A) > (B)) ? (B) : (A)) -#define OPL3_MAX(A, B) (((A) < (B)) ? (B) : (A)) -#define OPL3_CLAMP(V, MIN, MAX) OPL3_MAX(OPL3_MIN(V, MAX), MIN) - -void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples) +void OPL3_GenerateStream(opl3_chip *chip, int16_t *sndptr, uint32_t numsamples) { - Bit32u i; - Bit16s sample[2]; - Bit32s mix[2]; + uint_fast32_t i; for(i = 0; i < numsamples; i++) { - OPL3_GenerateResampled(chip, sample); - mix[0] = sndptr[0] + sample[0]; - mix[1] = sndptr[1] + sample[1]; - sndptr[0] = OPL3_CLAMP(mix[0], INT16_MIN, INT16_MAX); - sndptr[1] = OPL3_CLAMP(mix[1], INT16_MIN, INT16_MAX); + OPL3_GenerateResampled(chip, sndptr); sndptr += 2; } } |