DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/hardware/nukedopl.cpp
00001 //
00002 // Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 //
00015 //  Nuked OPL3 emulator.
00016 //  Thanks:
00017 //      MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
00018 //          Feedback and Rhythm part calculation information.
00019 //      forums.submarine.org.uk(carbon14, opl3):
00020 //          Tremolo and phase generator calculation information.
00021 //      OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
00022 //          OPL2 ROMs.
00023 //
00024 // version: 1.7.4
00025 //
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include "nukedopl.h"
00031 
00032 #define RSM_FRAC    10
00033 
00034 // Channel types
00035 
00036 enum {
00037     ch_2op = 0,
00038     ch_4op = 1,
00039     ch_4op2 = 2,
00040     ch_drum = 3
00041 };
00042 
00043 // Envelope key types
00044 
00045 enum {
00046     egk_norm = 0x01,
00047     egk_drum = 0x02
00048 };
00049 
00050 
00051 //
00052 // logsin table
00053 //
00054 
00055 static const Bit16u logsinrom[256] = {
00056     0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
00057     0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
00058     0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
00059     0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
00060     0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
00061     0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
00062     0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
00063     0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
00064     0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
00065     0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
00066     0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
00067     0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
00068     0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
00069     0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
00070     0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
00071     0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
00072     0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
00073     0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
00074     0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
00075     0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
00076     0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
00077     0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
00078     0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
00079     0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
00080     0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
00081     0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
00082     0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
00083     0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
00084     0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
00085     0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
00086     0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
00087     0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
00088 };
00089 
00090 //
00091 // exp table
00092 //
00093 
00094 static const Bit16u exprom[256] = {
00095     0x000, 0x003, 0x006, 0x008, 0x00b, 0x00e, 0x011, 0x014,
00096     0x016, 0x019, 0x01c, 0x01f, 0x022, 0x025, 0x028, 0x02a,
00097     0x02d, 0x030, 0x033, 0x036, 0x039, 0x03c, 0x03f, 0x042,
00098     0x045, 0x048, 0x04b, 0x04e, 0x051, 0x054, 0x057, 0x05a,
00099     0x05d, 0x060, 0x063, 0x066, 0x069, 0x06c, 0x06f, 0x072,
00100     0x075, 0x078, 0x07b, 0x07e, 0x082, 0x085, 0x088, 0x08b,
00101     0x08e, 0x091, 0x094, 0x098, 0x09b, 0x09e, 0x0a1, 0x0a4,
00102     0x0a8, 0x0ab, 0x0ae, 0x0b1, 0x0b5, 0x0b8, 0x0bb, 0x0be,
00103     0x0c2, 0x0c5, 0x0c8, 0x0cc, 0x0cf, 0x0d2, 0x0d6, 0x0d9,
00104     0x0dc, 0x0e0, 0x0e3, 0x0e7, 0x0ea, 0x0ed, 0x0f1, 0x0f4,
00105     0x0f8, 0x0fb, 0x0ff, 0x102, 0x106, 0x109, 0x10c, 0x110,
00106     0x114, 0x117, 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c,
00107     0x130, 0x134, 0x137, 0x13b, 0x13e, 0x142, 0x146, 0x149,
00108     0x14d, 0x151, 0x154, 0x158, 0x15c, 0x160, 0x163, 0x167,
00109     0x16b, 0x16f, 0x172, 0x176, 0x17a, 0x17e, 0x181, 0x185,
00110     0x189, 0x18d, 0x191, 0x195, 0x199, 0x19c, 0x1a0, 0x1a4,
00111     0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc, 0x1c0, 0x1c4,
00112     0x1c8, 0x1cc, 0x1d0, 0x1d4, 0x1d8, 0x1dc, 0x1e0, 0x1e4,
00113     0x1e8, 0x1ec, 0x1f0, 0x1f5, 0x1f9, 0x1fd, 0x201, 0x205,
00114     0x209, 0x20e, 0x212, 0x216, 0x21a, 0x21e, 0x223, 0x227,
00115     0x22b, 0x230, 0x234, 0x238, 0x23c, 0x241, 0x245, 0x249,
00116     0x24e, 0x252, 0x257, 0x25b, 0x25f, 0x264, 0x268, 0x26d,
00117     0x271, 0x276, 0x27a, 0x27f, 0x283, 0x288, 0x28c, 0x291,
00118     0x295, 0x29a, 0x29e, 0x2a3, 0x2a8, 0x2ac, 0x2b1, 0x2b5,
00119     0x2ba, 0x2bf, 0x2c4, 0x2c8, 0x2cd, 0x2d2, 0x2d6, 0x2db,
00120     0x2e0, 0x2e5, 0x2e9, 0x2ee, 0x2f3, 0x2f8, 0x2fd, 0x302,
00121     0x306, 0x30b, 0x310, 0x315, 0x31a, 0x31f, 0x324, 0x329,
00122     0x32e, 0x333, 0x338, 0x33d, 0x342, 0x347, 0x34c, 0x351,
00123     0x356, 0x35b, 0x360, 0x365, 0x36a, 0x370, 0x375, 0x37a,
00124     0x37f, 0x384, 0x38a, 0x38f, 0x394, 0x399, 0x39f, 0x3a4,
00125     0x3a9, 0x3ae, 0x3b4, 0x3b9, 0x3bf, 0x3c4, 0x3c9, 0x3cf,
00126     0x3d4, 0x3da, 0x3df, 0x3e4, 0x3ea, 0x3ef, 0x3f5, 0x3fa
00127 };
00128 
00129 //
00130 // freq mult table multiplied by 2
00131 //
00132 // 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15
00133 //
00134 
00135 static const Bit8u mt[16] = {
00136     1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
00137 };
00138 
00139 //
00140 // ksl table
00141 //
00142 
00143 static const Bit8u kslrom[16] = {
00144     0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
00145 };
00146 
00147 static const Bit8u kslshift[4] = {
00148     8, 1, 2, 0
00149 };
00150 
00151 //
00152 // envelope generator constants
00153 //
00154 
00155 static const Bit8u eg_incstep[3][4][8] = {
00156     {
00157         { 0, 0, 0, 0, 0, 0, 0, 0 },
00158         { 0, 0, 0, 0, 0, 0, 0, 0 },
00159         { 0, 0, 0, 0, 0, 0, 0, 0 },
00160         { 0, 0, 0, 0, 0, 0, 0, 0 }
00161     },
00162     {
00163         { 0, 1, 0, 1, 0, 1, 0, 1 },
00164         { 0, 1, 0, 1, 1, 1, 0, 1 },
00165         { 0, 1, 1, 1, 0, 1, 1, 1 },
00166         { 0, 1, 1, 1, 1, 1, 1, 1 }
00167     },
00168     {
00169         { 1, 1, 1, 1, 1, 1, 1, 1 },
00170         { 2, 2, 1, 1, 1, 1, 1, 1 },
00171         { 2, 2, 1, 1, 2, 2, 1, 1 },
00172         { 2, 2, 2, 2, 2, 2, 1, 1 }
00173     }
00174 };
00175 
00176 static const Bit8u eg_incdesc[16] = {
00177     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2
00178 };
00179 
00180 static const Bit8s eg_incsh[16] = {
00181     0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, -1, -2
00182 };
00183 
00184 //
00185 // address decoding
00186 //
00187 
00188 static const Bit8s ad_slot[0x20] = {
00189     0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
00190     12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
00191 };
00192 
00193 static const Bit8u ch_slot[18] = {
00194     0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
00195 };
00196 
00197 //
00198 // Envelope generator
00199 //
00200 
00201 typedef Bit16s(*envelope_sinfunc)(Bit16u phase, Bit16u envelope);
00202 typedef void(*envelope_genfunc)(opl3_slot *slott);
00203 
00204 static Bit16s OPL3_EnvelopeCalcExp(Bit32u level)
00205 {
00206     if (level > 0x1fff)
00207     {
00208         level = 0x1fff;
00209     }
00210     return ((exprom[(level & 0xff) ^ 0xff] | 0x400) << 1) >> (level >> 8);
00211 }
00212 
00213 static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope)
00214 {
00215     Bit16u out = 0;
00216     Bit16u neg = 0;
00217     phase &= 0x3ff;
00218     if (phase & 0x200)
00219     {
00220         neg = ((Bit16u)(~0u));
00221     }
00222     if (phase & 0x100)
00223     {
00224         out = logsinrom[(phase & 0xff) ^ 0xff];
00225     }
00226     else
00227     {
00228         out = logsinrom[phase & 0xff];
00229     }
00230     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u)) ^ neg;
00231 }
00232 
00233 static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope)
00234 {
00235     Bit16u out = 0;
00236     phase &= 0x3ff;
00237     if (phase & 0x200)
00238     {
00239         out = 0x1000;
00240     }
00241     else if (phase & 0x100)
00242     {
00243         out = logsinrom[(phase & 0xff) ^ 0xff];
00244     }
00245     else
00246     {
00247         out = logsinrom[phase & 0xff];
00248     }
00249     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u));
00250 }
00251 
00252 static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope)
00253 {
00254     Bit16u out = 0;
00255     phase &= 0x3ff;
00256     if (phase & 0x100)
00257     {
00258         out = logsinrom[(phase & 0xff) ^ 0xff];
00259     }
00260     else
00261     {
00262         out = logsinrom[phase & 0xff];
00263     }
00264     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u));
00265 }
00266 
00267 static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope)
00268 {
00269     Bit16u out = 0;
00270     phase &= 0x3ff;
00271     if (phase & 0x100)
00272     {
00273         out = 0x1000;
00274     }
00275     else
00276     {
00277         out = logsinrom[phase & 0xff];
00278     }
00279     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u));
00280 }
00281 
00282 static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope)
00283 {
00284     Bit16u out = 0;
00285     Bit16u neg = 0;
00286     phase &= 0x3ff;
00287     if ((phase & 0x300) == 0x100)
00288     {
00289         neg = ((Bit16u)(~0u));
00290     }
00291     if (phase & 0x200)
00292     {
00293         out = 0x1000;
00294     }
00295     else if (phase & 0x80)
00296     {
00297         out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
00298     }
00299     else
00300     {
00301         out = logsinrom[(phase << 1) & 0xff];
00302     }
00303     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u)) ^ neg;
00304 }
00305 
00306 static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope)
00307 {
00308     Bit16u out = 0;
00309     phase &= 0x3ff;
00310     if (phase & 0x200)
00311     {
00312         out = 0x1000;
00313     }
00314     else if (phase & 0x80)
00315     {
00316         out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
00317     }
00318     else
00319     {
00320         out = logsinrom[(phase << 1) & 0xff];
00321     }
00322     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u));
00323 }
00324 
00325 static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope)
00326 {
00327     Bit16u neg = 0;
00328     phase &= 0x3ff;
00329     if (phase & 0x200)
00330     {
00331         neg = ((Bit16u)(~0u));
00332     }
00333     return OPL3_EnvelopeCalcExp((unsigned int)envelope << 3u) ^ neg;
00334 }
00335 
00336 static Bit16s OPL3_EnvelopeCalcSin7(Bit16u phase, Bit16u envelope)
00337 {
00338     Bit16u out = 0;
00339     Bit16u neg = 0;
00340     phase &= 0x3ff;
00341     if (phase & 0x200)
00342     {
00343         neg = ((Bit16u)(~0u));
00344         phase = (phase & 0x1ff) ^ 0x1ff;
00345     }
00346     out = phase << 3;
00347     return OPL3_EnvelopeCalcExp((unsigned int)out + ((unsigned int)envelope << 3u)) ^ neg;
00348 }
00349 
00350 static const envelope_sinfunc envelope_sin[8] = {
00351     OPL3_EnvelopeCalcSin0,
00352     OPL3_EnvelopeCalcSin1,
00353     OPL3_EnvelopeCalcSin2,
00354     OPL3_EnvelopeCalcSin3,
00355     OPL3_EnvelopeCalcSin4,
00356     OPL3_EnvelopeCalcSin5,
00357     OPL3_EnvelopeCalcSin6,
00358     OPL3_EnvelopeCalcSin7
00359 };
00360 
00361 static void OPL3_EnvelopeGenOff(opl3_slot *slot);
00362 static void OPL3_EnvelopeGenAttack(opl3_slot *slot);
00363 static void OPL3_EnvelopeGenDecay(opl3_slot *slot);
00364 static void OPL3_EnvelopeGenSustain(opl3_slot *slot);
00365 static void OPL3_EnvelopeGenRelease(opl3_slot *slot);
00366 
00367 envelope_genfunc envelope_gen[5] = {
00368     OPL3_EnvelopeGenOff,
00369     OPL3_EnvelopeGenAttack,
00370     OPL3_EnvelopeGenDecay,
00371     OPL3_EnvelopeGenSustain,
00372     OPL3_EnvelopeGenRelease
00373 };
00374 
00375 enum envelope_gen_num
00376 {
00377     envelope_gen_num_off = 0,
00378     envelope_gen_num_attack = 1,
00379     envelope_gen_num_decay = 2,
00380     envelope_gen_num_sustain = 3,
00381     envelope_gen_num_release = 4
00382 };
00383 
00384 static Bit8u OPL3_EnvelopeCalcRate(opl3_slot *slot, Bit8u reg_rate)
00385 {
00386     Bit8u rate;
00387     if (reg_rate == 0x00)
00388     {
00389         return 0x00;
00390     }
00391     rate = (reg_rate << 2)
00392          + (slot->reg_ksr ? slot->channel->ksv : (slot->channel->ksv >> 2));
00393     if (rate > 0x3c)
00394     {
00395         rate = 0x3c;
00396     }
00397     return rate;
00398 }
00399 
00400 static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
00401 {
00402     Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2)
00403                - ((0x08 - slot->channel->block) << 5);
00404     if (ksl < 0)
00405     {
00406         ksl = 0;
00407     }
00408     slot->eg_ksl = (Bit8u)ksl;
00409 }
00410 
00411 static void OPL3_EnvelopeUpdateRate(opl3_slot *slot)
00412 {
00413     switch (slot->eg_gen)
00414     {
00415     case envelope_gen_num_off:
00416     case envelope_gen_num_attack:
00417         slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_ar);
00418         break;
00419     case envelope_gen_num_decay:
00420         slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_dr);
00421         break;
00422     case envelope_gen_num_sustain:
00423     case envelope_gen_num_release:
00424         slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_rr);
00425         break;
00426     }
00427 }
00428 
00429 static void OPL3_EnvelopeGenOff(opl3_slot *slot)
00430 {
00431     slot->eg_rout = 0x1ff;
00432 }
00433 
00434 static void OPL3_EnvelopeGenAttack(opl3_slot *slot)
00435 {
00436     if (slot->eg_rout == 0x00)
00437     {
00438         slot->eg_gen = envelope_gen_num_decay;
00439         OPL3_EnvelopeUpdateRate(slot);
00440         return;
00441     }
00442     slot->eg_rout += ((~slot->eg_rout) * slot->eg_inc) >> 3;
00443     if (slot->eg_rout < 0x00)
00444     {
00445         slot->eg_rout = 0x00;
00446     }
00447 }
00448 
00449 static void OPL3_EnvelopeGenDecay(opl3_slot *slot)
00450 {
00451     if (slot->eg_rout >= slot->reg_sl << 4)
00452     {
00453         slot->eg_gen = envelope_gen_num_sustain;
00454         OPL3_EnvelopeUpdateRate(slot);
00455         return;
00456     }
00457     slot->eg_rout += slot->eg_inc;
00458 }
00459 
00460 static void OPL3_EnvelopeGenSustain(opl3_slot *slot)
00461 {
00462     if (!slot->reg_type)
00463     {
00464         OPL3_EnvelopeGenRelease(slot);
00465     }
00466 }
00467 
00468 static void OPL3_EnvelopeGenRelease(opl3_slot *slot)
00469 {
00470     if (slot->eg_rout >= 0x1ff)
00471     {
00472         slot->eg_gen = envelope_gen_num_off;
00473         slot->eg_rout = 0x1ff;
00474         OPL3_EnvelopeUpdateRate(slot);
00475         return;
00476     }
00477     slot->eg_rout += slot->eg_inc;
00478 }
00479 
00480 static void OPL3_EnvelopeCalc(opl3_slot *slot)
00481 {
00482     Bit8u rate_h, rate_l;
00483     Bit8u inc = 0;
00484     rate_h = slot->eg_rate >> 2;
00485     rate_l = slot->eg_rate & 3;
00486     if (eg_incsh[rate_h] > 0)
00487     {
00488         if ((slot->chip->timer & ((1 << eg_incsh[rate_h]) - 1)) == 0)
00489         {
00490             inc = eg_incstep[eg_incdesc[rate_h]][rate_l]
00491                             [((slot->chip->timer)>> eg_incsh[rate_h]) & 0x07];
00492         }
00493     }
00494     else
00495     {
00496         inc = eg_incstep[eg_incdesc[rate_h]][rate_l]
00497                         [slot->chip->timer & 0x07] << (-eg_incsh[rate_h]);
00498     }
00499     slot->eg_inc = inc;
00500     slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)
00501                  + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;
00502     envelope_gen[slot->eg_gen](slot);
00503 }
00504 
00505 static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type)
00506 {
00507     if (!slot->key)
00508     {
00509         slot->eg_gen = envelope_gen_num_attack;
00510         OPL3_EnvelopeUpdateRate(slot);
00511         if ((slot->eg_rate >> 2) == 0x0f)
00512         {
00513             slot->eg_gen = envelope_gen_num_decay;
00514             OPL3_EnvelopeUpdateRate(slot);
00515             slot->eg_rout = 0x00;
00516         }
00517         slot->pg_phase = 0x00;
00518     }
00519     slot->key |= type;
00520 }
00521 
00522 static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type)
00523 {
00524     if (slot->key)
00525     {
00526         slot->key &= (~type);
00527         if (!slot->key)
00528         {
00529             slot->eg_gen = envelope_gen_num_release;
00530             OPL3_EnvelopeUpdateRate(slot);
00531         }
00532     }
00533 }
00534 
00535 //
00536 // Phase Generator
00537 //
00538 
00539 static void OPL3_PhaseGenerate(opl3_slot *slot)
00540 {
00541     Bit16u f_num;
00542     Bit32u basefreq;
00543 
00544     f_num = slot->channel->f_num;
00545     if (slot->reg_vib)
00546     {
00547         Bit8s range;
00548         Bit8u vibpos;
00549 
00550         range = (f_num >> 7) & 7;
00551         vibpos = slot->chip->vibpos;
00552 
00553         if (!(vibpos & 3))
00554         {
00555             range = 0;
00556         }
00557         else if (vibpos & 1)
00558         {
00559             range >>= 1;
00560         }
00561         range >>= slot->chip->vibshift;
00562 
00563         if (vibpos & 4)
00564         {
00565             range = -range;
00566         }
00567         f_num += range;
00568     }
00569     basefreq = (Bit32u)((f_num << slot->channel->block) >> 1);
00570     slot->pg_phase += (Bit32u)((basefreq * mt[slot->reg_mult]) >> 1);
00571 }
00572 
00573 //
00574 // Noise Generator
00575 //
00576 
00577 static void OPL3_NoiseGenerate(opl3_chip *chip)
00578 {
00579     if (chip->noise & 0x01)
00580     {
00581         chip->noise ^= 0x800302;
00582     }
00583     chip->noise >>= 1;
00584 }
00585 
00586 //
00587 // Slot
00588 //
00589 
00590 static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data)
00591 {
00592     if ((data >> 7) & 0x01)
00593     {
00594         slot->trem = &slot->chip->tremolo;
00595     }
00596     else
00597     {
00598         slot->trem = (Bit8u*)&slot->chip->zeromod;
00599     }
00600     slot->reg_vib = (data >> 6) & 0x01;
00601     slot->reg_type = (data >> 5) & 0x01;
00602     slot->reg_ksr = (data >> 4) & 0x01;
00603     slot->reg_mult = data & 0x0f;
00604     OPL3_EnvelopeUpdateRate(slot);
00605 }
00606 
00607 static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data)
00608 {
00609     slot->reg_ksl = (data >> 6) & 0x03;
00610     slot->reg_tl = data & 0x3f;
00611     OPL3_EnvelopeUpdateKSL(slot);
00612 }
00613 
00614 static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data)
00615 {
00616     slot->reg_ar = (data >> 4) & 0x0f;
00617     slot->reg_dr = data & 0x0f;
00618     OPL3_EnvelopeUpdateRate(slot);
00619 }
00620 
00621 static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data)
00622 {
00623     slot->reg_sl = (data >> 4) & 0x0f;
00624     if (slot->reg_sl == 0x0f)
00625     {
00626         slot->reg_sl = 0x1f;
00627     }
00628     slot->reg_rr = data & 0x0f;
00629     OPL3_EnvelopeUpdateRate(slot);
00630 }
00631 
00632 static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data)
00633 {
00634     slot->reg_wf = data & 0x07;
00635     if (slot->chip->newm == 0x00)
00636     {
00637         slot->reg_wf &= 0x03;
00638     }
00639 }
00640 
00641 static void OPL3_SlotGeneratePhase(opl3_slot *slot, Bit16u phase)
00642 {
00643     slot->out = envelope_sin[slot->reg_wf](phase, (Bit16u)slot->eg_out);
00644 }
00645 
00646 static void OPL3_SlotGenerate(opl3_slot *slot)
00647 {
00648     OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9) + *slot->mod);
00649 }
00650 
00651 static void OPL3_SlotGenerateZM(opl3_slot *slot)
00652 {
00653     OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9));
00654 }
00655 
00656 static void OPL3_SlotCalcFB(opl3_slot *slot)
00657 {
00658     if (slot->channel->fb != 0x00)
00659     {
00660         slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
00661     }
00662     else
00663     {
00664         slot->fbmod = 0;
00665     }
00666     slot->prout = slot->out;
00667 }
00668 
00669 //
00670 // Channel
00671 //
00672 
00673 static void OPL3_ChannelSetupAlg(opl3_channel *channel);
00674 
00675 static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data)
00676 {
00677     opl3_channel *channel6;
00678     opl3_channel *channel7;
00679     opl3_channel *channel8;
00680     Bit8u chnum;
00681 
00682     chip->rhy = data & 0x3f;
00683     if (chip->rhy & 0x20)
00684     {
00685         channel6 = &chip->channel[6];
00686         channel7 = &chip->channel[7];
00687         channel8 = &chip->channel[8];
00688         channel6->out[0] = &channel6->slots[1]->out;
00689         channel6->out[1] = &channel6->slots[1]->out;
00690         channel6->out[2] = &chip->zeromod;
00691         channel6->out[3] = &chip->zeromod;
00692         channel7->out[0] = &channel7->slots[0]->out;
00693         channel7->out[1] = &channel7->slots[0]->out;
00694         channel7->out[2] = &channel7->slots[1]->out;
00695         channel7->out[3] = &channel7->slots[1]->out;
00696         channel8->out[0] = &channel8->slots[0]->out;
00697         channel8->out[1] = &channel8->slots[0]->out;
00698         channel8->out[2] = &channel8->slots[1]->out;
00699         channel8->out[3] = &channel8->slots[1]->out;
00700         for (chnum = 6; chnum < 9; chnum++)
00701         {
00702             chip->channel[chnum].chtype = ch_drum;
00703         }
00704         OPL3_ChannelSetupAlg(channel6);
00705         //hh
00706         if (chip->rhy & 0x01)
00707         {
00708             OPL3_EnvelopeKeyOn(channel7->slots[0], egk_drum);
00709         }
00710         else
00711         {
00712             OPL3_EnvelopeKeyOff(channel7->slots[0], egk_drum);
00713         }
00714         //tc
00715         if (chip->rhy & 0x02)
00716         {
00717             OPL3_EnvelopeKeyOn(channel8->slots[1], egk_drum);
00718         }
00719         else
00720         {
00721             OPL3_EnvelopeKeyOff(channel8->slots[1], egk_drum);
00722         }
00723         //tom
00724         if (chip->rhy & 0x04)
00725         {
00726             OPL3_EnvelopeKeyOn(channel8->slots[0], egk_drum);
00727         }
00728         else
00729         {
00730             OPL3_EnvelopeKeyOff(channel8->slots[0], egk_drum);
00731         }
00732         //sd
00733         if (chip->rhy & 0x08)
00734         {
00735             OPL3_EnvelopeKeyOn(channel7->slots[1], egk_drum);
00736         }
00737         else
00738         {
00739             OPL3_EnvelopeKeyOff(channel7->slots[1], egk_drum);
00740         }
00741         //bd
00742         if (chip->rhy & 0x10)
00743         {
00744             OPL3_EnvelopeKeyOn(channel6->slots[0], egk_drum);
00745             OPL3_EnvelopeKeyOn(channel6->slots[1], egk_drum);
00746         }
00747         else
00748         {
00749             OPL3_EnvelopeKeyOff(channel6->slots[0], egk_drum);
00750             OPL3_EnvelopeKeyOff(channel6->slots[1], egk_drum);
00751         }
00752     }
00753     else
00754     {
00755         for (chnum = 6; chnum < 9; chnum++)
00756         {
00757             chip->channel[chnum].chtype = ch_2op;
00758             OPL3_ChannelSetupAlg(&chip->channel[chnum]);
00759             OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[0], egk_drum);
00760             OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[1], egk_drum);
00761         }
00762     }
00763 }
00764 
00765 static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data)
00766 {
00767     if (channel->chip->newm && channel->chtype == ch_4op2)
00768     {
00769         return;
00770     }
00771     channel->f_num = (channel->f_num & 0x300) | data;
00772     channel->ksv = (channel->block << 1)
00773                  | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
00774     OPL3_EnvelopeUpdateKSL(channel->slots[0]);
00775     OPL3_EnvelopeUpdateKSL(channel->slots[1]);
00776     OPL3_EnvelopeUpdateRate(channel->slots[0]);
00777     OPL3_EnvelopeUpdateRate(channel->slots[1]);
00778     if (channel->chip->newm && channel->chtype == ch_4op)
00779     {
00780         channel->pair->f_num = channel->f_num;
00781         channel->pair->ksv = channel->ksv;
00782         OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
00783         OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
00784         OPL3_EnvelopeUpdateRate(channel->pair->slots[0]);
00785         OPL3_EnvelopeUpdateRate(channel->pair->slots[1]);
00786     }
00787 }
00788 
00789 static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data)
00790 {
00791     if (channel->chip->newm && channel->chtype == ch_4op2)
00792     {
00793         return;
00794     }
00795     channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8);
00796     channel->block = (data >> 2) & 0x07;
00797     channel->ksv = (channel->block << 1)
00798                  | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
00799     OPL3_EnvelopeUpdateKSL(channel->slots[0]);
00800     OPL3_EnvelopeUpdateKSL(channel->slots[1]);
00801     OPL3_EnvelopeUpdateRate(channel->slots[0]);
00802     OPL3_EnvelopeUpdateRate(channel->slots[1]);
00803     if (channel->chip->newm && channel->chtype == ch_4op)
00804     {
00805         channel->pair->f_num = channel->f_num;
00806         channel->pair->block = channel->block;
00807         channel->pair->ksv = channel->ksv;
00808         OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
00809         OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
00810         OPL3_EnvelopeUpdateRate(channel->pair->slots[0]);
00811         OPL3_EnvelopeUpdateRate(channel->pair->slots[1]);
00812     }
00813 }
00814 
00815 static void OPL3_ChannelSetupAlg(opl3_channel *channel)
00816 {
00817     if (channel->chtype == ch_drum)
00818     {
00819         switch (channel->alg & 0x01)
00820         {
00821         case 0x00:
00822             channel->slots[0]->mod = &channel->slots[0]->fbmod;
00823             channel->slots[1]->mod = &channel->slots[0]->out;
00824             break;
00825         case 0x01:
00826             channel->slots[0]->mod = &channel->slots[0]->fbmod;
00827             channel->slots[1]->mod = &channel->chip->zeromod;
00828             break;
00829         }
00830         return;
00831     }
00832     if (channel->alg & 0x08)
00833     {
00834         return;
00835     }
00836     if (channel->alg & 0x04)
00837     {
00838         channel->pair->out[0] = &channel->chip->zeromod;
00839         channel->pair->out[1] = &channel->chip->zeromod;
00840         channel->pair->out[2] = &channel->chip->zeromod;
00841         channel->pair->out[3] = &channel->chip->zeromod;
00842         switch (channel->alg & 0x03)
00843         {
00844         case 0x00:
00845             channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
00846             channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
00847             channel->slots[0]->mod = &channel->pair->slots[1]->out;
00848             channel->slots[1]->mod = &channel->slots[0]->out;
00849             channel->out[0] = &channel->slots[1]->out;
00850             channel->out[1] = &channel->chip->zeromod;
00851             channel->out[2] = &channel->chip->zeromod;
00852             channel->out[3] = &channel->chip->zeromod;
00853             break;
00854         case 0x01:
00855             channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
00856             channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
00857             channel->slots[0]->mod = &channel->chip->zeromod;
00858             channel->slots[1]->mod = &channel->slots[0]->out;
00859             channel->out[0] = &channel->pair->slots[1]->out;
00860             channel->out[1] = &channel->slots[1]->out;
00861             channel->out[2] = &channel->chip->zeromod;
00862             channel->out[3] = &channel->chip->zeromod;
00863             break;
00864         case 0x02:
00865             channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
00866             channel->pair->slots[1]->mod = &channel->chip->zeromod;
00867             channel->slots[0]->mod = &channel->pair->slots[1]->out;
00868             channel->slots[1]->mod = &channel->slots[0]->out;
00869             channel->out[0] = &channel->pair->slots[0]->out;
00870             channel->out[1] = &channel->slots[1]->out;
00871             channel->out[2] = &channel->chip->zeromod;
00872             channel->out[3] = &channel->chip->zeromod;
00873             break;
00874         case 0x03:
00875             channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
00876             channel->pair->slots[1]->mod = &channel->chip->zeromod;
00877             channel->slots[0]->mod = &channel->pair->slots[1]->out;
00878             channel->slots[1]->mod = &channel->chip->zeromod;
00879             channel->out[0] = &channel->pair->slots[0]->out;
00880             channel->out[1] = &channel->slots[0]->out;
00881             channel->out[2] = &channel->slots[1]->out;
00882             channel->out[3] = &channel->chip->zeromod;
00883             break;
00884         }
00885     }
00886     else
00887     {
00888         switch (channel->alg & 0x01)
00889         {
00890         case 0x00:
00891             channel->slots[0]->mod = &channel->slots[0]->fbmod;
00892             channel->slots[1]->mod = &channel->slots[0]->out;
00893             channel->out[0] = &channel->slots[1]->out;
00894             channel->out[1] = &channel->chip->zeromod;
00895             channel->out[2] = &channel->chip->zeromod;
00896             channel->out[3] = &channel->chip->zeromod;
00897             break;
00898         case 0x01:
00899             channel->slots[0]->mod = &channel->slots[0]->fbmod;
00900             channel->slots[1]->mod = &channel->chip->zeromod;
00901             channel->out[0] = &channel->slots[0]->out;
00902             channel->out[1] = &channel->slots[1]->out;
00903             channel->out[2] = &channel->chip->zeromod;
00904             channel->out[3] = &channel->chip->zeromod;
00905             break;
00906         }
00907     }
00908 }
00909 
00910 static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data)
00911 {
00912     channel->fb = (data & 0x0e) >> 1;
00913     channel->con = data & 0x01;
00914     channel->alg = channel->con;
00915     if (channel->chip->newm)
00916     {
00917         if (channel->chtype == ch_4op)
00918         {
00919             channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
00920             channel->alg = 0x08;
00921             OPL3_ChannelSetupAlg(channel->pair);
00922         }
00923         else if (channel->chtype == ch_4op2)
00924         {
00925             channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
00926             channel->pair->alg = 0x08;
00927             OPL3_ChannelSetupAlg(channel);
00928         }
00929         else
00930         {
00931             OPL3_ChannelSetupAlg(channel);
00932         }
00933     }
00934     else
00935     {
00936         OPL3_ChannelSetupAlg(channel);
00937     }
00938     if (channel->chip->newm)
00939     {
00940         channel->cha = (Bit16u)(((data >> 4) & 0x01) ? ~0 : 0);
00941         channel->chb = (Bit16u)(((data >> 5) & 0x01) ? ~0 : 0);
00942     }
00943     else
00944     {
00945         channel->cha = channel->chb = (Bit16u)(~0);
00946     }
00947 }
00948 
00949 static void OPL3_ChannelKeyOn(opl3_channel *channel)
00950 {
00951     if (channel->chip->newm)
00952     {
00953         if (channel->chtype == ch_4op)
00954         {
00955             OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
00956             OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
00957             OPL3_EnvelopeKeyOn(channel->pair->slots[0], egk_norm);
00958             OPL3_EnvelopeKeyOn(channel->pair->slots[1], egk_norm);
00959         }
00960         else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
00961         {
00962             OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
00963             OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
00964         }
00965     }
00966     else
00967     {
00968         OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
00969         OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
00970     }
00971 }
00972 
00973 static void OPL3_ChannelKeyOff(opl3_channel *channel)
00974 {
00975     if (channel->chip->newm)
00976     {
00977         if (channel->chtype == ch_4op)
00978         {
00979             OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
00980             OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
00981             OPL3_EnvelopeKeyOff(channel->pair->slots[0], egk_norm);
00982             OPL3_EnvelopeKeyOff(channel->pair->slots[1], egk_norm);
00983         }
00984         else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
00985         {
00986             OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
00987             OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
00988         }
00989     }
00990     else
00991     {
00992         OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
00993         OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
00994     }
00995 }
00996 
00997 static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data)
00998 {
00999     Bit8u bit;
01000     Bit8u chnum;
01001     for (bit = 0; bit < 6; bit++)
01002     {
01003         chnum = bit;
01004         if (bit >= 3)
01005         {
01006             chnum += 9 - 3;
01007         }
01008         if ((data >> bit) & 0x01)
01009         {
01010             chip->channel[chnum].chtype = ch_4op;
01011             chip->channel[chnum + 3].chtype = ch_4op2;
01012         }
01013         else
01014         {
01015             chip->channel[chnum].chtype = ch_2op;
01016             chip->channel[chnum + 3].chtype = ch_2op;
01017         }
01018     }
01019 }
01020 
01021 static Bit16s OPL3_ClipSample(Bit32s sample)
01022 {
01023     if (sample > 32767)
01024     {
01025         sample = 32767;
01026     }
01027     else if (sample < -32768)
01028     {
01029         sample = -32768;
01030     }
01031     return (Bit16s)sample;
01032 }
01033 
01034 static void OPL3_GenerateRhythm1(opl3_chip *chip)
01035 {
01036     opl3_channel *channel6;
01037     opl3_channel *channel7;
01038     opl3_channel *channel8;
01039     Bit16u phase14;
01040     Bit16u phase17;
01041     Bit16u phase;
01042     Bit16u phasebit;
01043 
01044     channel6 = &chip->channel[6];
01045     channel7 = &chip->channel[7];
01046     channel8 = &chip->channel[8];
01047     OPL3_SlotGenerate(channel6->slots[0]);
01048     phase14 = (channel7->slots[0]->pg_phase >> 9) & 0x3ff;
01049     phase17 = (channel8->slots[1]->pg_phase >> 9) & 0x3ff;
01050     phase = 0x00;
01051     //hh tc phase bit
01052     phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)
01053              | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;
01054     //hh
01055     phase = (phasebit << 9)
01056           | (0x34 << ((phasebit ^ (chip->noise & 0x01)) << 1));
01057     OPL3_SlotGeneratePhase(channel7->slots[0], phase);
01058     //tt
01059     OPL3_SlotGenerateZM(channel8->slots[0]);
01060 }
01061 
01062 static void OPL3_GenerateRhythm2(opl3_chip *chip)
01063 {
01064     opl3_channel *channel6;
01065     opl3_channel *channel7;
01066     opl3_channel *channel8;
01067     Bit16u phase14;
01068     Bit16u phase17;
01069     Bit16u phase;
01070     Bit16u phasebit;
01071 
01072     channel6 = &chip->channel[6];
01073     channel7 = &chip->channel[7];
01074     channel8 = &chip->channel[8];
01075     OPL3_SlotGenerate(channel6->slots[1]);
01076     phase14 = (channel7->slots[0]->pg_phase >> 9) & 0x3ff;
01077     phase17 = (channel8->slots[1]->pg_phase >> 9) & 0x3ff;
01078     phase = 0x00;
01079     //hh tc phase bit
01080     phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)
01081              | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;
01082     //sd
01083     phase = (Bit32u)(0x100 << ((phase14 >> 8) & 0x01)) ^ (Bit32u)((chip->noise & 0x01) << 8);
01084     OPL3_SlotGeneratePhase(channel7->slots[1], phase);
01085     //tc
01086     phase = 0x100u | (Bit32u)(phasebit << 9);
01087     OPL3_SlotGeneratePhase(channel8->slots[1], phase);
01088 }
01089 
01090 void OPL3_Generate(opl3_chip *chip, Bit16s *buf)
01091 {
01092     Bit8u ii;
01093     Bit8u jj;
01094     Bit16s accm;
01095 
01096     buf[1] = OPL3_ClipSample(chip->mixbuff[1]);
01097 
01098     for (ii = 0; ii < 12; ii++)
01099     {
01100         OPL3_SlotCalcFB(&chip->slot[ii]);
01101         OPL3_PhaseGenerate(&chip->slot[ii]);
01102         OPL3_EnvelopeCalc(&chip->slot[ii]);
01103         OPL3_SlotGenerate(&chip->slot[ii]);
01104     }
01105 
01106     for (ii = 12; ii < 15; ii++)
01107     {
01108         OPL3_SlotCalcFB(&chip->slot[ii]);
01109         OPL3_PhaseGenerate(&chip->slot[ii]);
01110         OPL3_EnvelopeCalc(&chip->slot[ii]);
01111     }
01112 
01113     if (chip->rhy & 0x20)
01114     {
01115         OPL3_GenerateRhythm1(chip);
01116     }
01117     else
01118     {
01119         OPL3_SlotGenerate(&chip->slot[12]);
01120         OPL3_SlotGenerate(&chip->slot[13]);
01121         OPL3_SlotGenerate(&chip->slot[14]);
01122     }
01123 
01124     chip->mixbuff[0] = 0;
01125     for (ii = 0; ii < 18; ii++)
01126     {
01127         accm = 0;
01128         for (jj = 0; jj < 4; jj++)
01129         {
01130             accm += *chip->channel[ii].out[jj];
01131         }
01132         chip->mixbuff[0] += (Bit16s)(accm & chip->channel[ii].cha);
01133     }
01134 
01135     for (ii = 15; ii < 18; ii++)
01136     {
01137         OPL3_SlotCalcFB(&chip->slot[ii]);
01138         OPL3_PhaseGenerate(&chip->slot[ii]);
01139         OPL3_EnvelopeCalc(&chip->slot[ii]);
01140     }
01141 
01142     if (chip->rhy & 0x20)
01143     {
01144         OPL3_GenerateRhythm2(chip);
01145     }
01146     else
01147     {
01148         OPL3_SlotGenerate(&chip->slot[15]);
01149         OPL3_SlotGenerate(&chip->slot[16]);
01150         OPL3_SlotGenerate(&chip->slot[17]);
01151     }
01152 
01153     buf[0] = OPL3_ClipSample(chip->mixbuff[0]);
01154 
01155     for (ii = 18; ii < 33; ii++)
01156     {
01157         OPL3_SlotCalcFB(&chip->slot[ii]);
01158         OPL3_PhaseGenerate(&chip->slot[ii]);
01159         OPL3_EnvelopeCalc(&chip->slot[ii]);
01160         OPL3_SlotGenerate(&chip->slot[ii]);
01161     }
01162 
01163     chip->mixbuff[1] = 0;
01164     for (ii = 0; ii < 18; ii++)
01165     {
01166         accm = 0;
01167         for (jj = 0; jj < 4; jj++)
01168         {
01169             accm += *chip->channel[ii].out[jj];
01170         }
01171         chip->mixbuff[1] += (Bit16s)(accm & chip->channel[ii].chb);
01172     }
01173 
01174     for (ii = 33; ii < 36; ii++)
01175     {
01176         OPL3_SlotCalcFB(&chip->slot[ii]);
01177         OPL3_PhaseGenerate(&chip->slot[ii]);
01178         OPL3_EnvelopeCalc(&chip->slot[ii]);
01179         OPL3_SlotGenerate(&chip->slot[ii]);
01180     }
01181 
01182     OPL3_NoiseGenerate(chip);
01183 
01184     if ((chip->timer & 0x3f) == 0x3f)
01185     {
01186         chip->tremolopos = (chip->tremolopos + 1) % 210;
01187     }
01188     if (chip->tremolopos < 105)
01189     {
01190         chip->tremolo = chip->tremolopos >> chip->tremoloshift;
01191     }
01192     else
01193     {
01194         chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
01195     }
01196 
01197     if ((chip->timer & 0x3ff) == 0x3ff)
01198     {
01199         chip->vibpos = (chip->vibpos + 1) & 7;
01200     }
01201 
01202     chip->timer++;
01203 }
01204 
01205 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf)
01206 {
01207     while (chip->samplecnt >= chip->rateratio)
01208     {
01209         chip->oldsamples[0] = chip->samples[0];
01210         chip->oldsamples[1] = chip->samples[1];
01211         OPL3_Generate(chip, chip->samples);
01212         chip->samplecnt -= chip->rateratio;
01213     }
01214     buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
01215                      + chip->samples[0] * chip->samplecnt) / chip->rateratio);
01216     buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
01217                      + chip->samples[1] * chip->samplecnt) / chip->rateratio);
01218     chip->samplecnt += 1 << RSM_FRAC;
01219 }
01220 
01221 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate)
01222 {
01223     Bit8u slotnum;
01224     Bit8u channum;
01225 
01226     memset(chip, 0, sizeof(opl3_chip));
01227     for (slotnum = 0; slotnum < 36; slotnum++)
01228     {
01229         chip->slot[slotnum].chip = chip;
01230         chip->slot[slotnum].mod = &chip->zeromod;
01231         chip->slot[slotnum].eg_rout = 0x1ff;
01232         chip->slot[slotnum].eg_out = 0x1ff;
01233         chip->slot[slotnum].eg_gen = envelope_gen_num_off;
01234         chip->slot[slotnum].trem = (Bit8u*)&chip->zeromod;
01235     }
01236     for (channum = 0; channum < 18; channum++)
01237     {
01238         chip->channel[channum].slots[0] = &chip->slot[ch_slot[channum]];
01239         chip->channel[channum].slots[1] = &chip->slot[ch_slot[channum] + 3];
01240         chip->slot[ch_slot[channum]].channel = &chip->channel[channum];
01241         chip->slot[ch_slot[channum] + 3].channel = &chip->channel[channum];
01242         if ((channum % 9) < 3)
01243         {
01244             chip->channel[channum].pair = &chip->channel[channum + 3];
01245         }
01246         else if ((channum % 9) < 6)
01247         {
01248             chip->channel[channum].pair = &chip->channel[channum - 3];
01249         }
01250         chip->channel[channum].chip = chip;
01251         chip->channel[channum].out[0] = &chip->zeromod;
01252         chip->channel[channum].out[1] = &chip->zeromod;
01253         chip->channel[channum].out[2] = &chip->zeromod;
01254         chip->channel[channum].out[3] = &chip->zeromod;
01255         chip->channel[channum].chtype = ch_2op;
01256         chip->channel[channum].cha = (Bit16u)(~0);
01257         chip->channel[channum].chb = (Bit16u)(~0);
01258         OPL3_ChannelSetupAlg(&chip->channel[channum]);
01259     }
01260     chip->noise = 0x306600;
01261     chip->rateratio = (Bit32s)((samplerate << RSM_FRAC) / 49716);
01262     chip->tremoloshift = 4;
01263     chip->vibshift = 1;
01264 }
01265 
01266 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v)
01267 {
01268     Bit8u high = (reg >> 8) & 0x01;
01269     Bit8u regm = reg & 0xff;
01270     switch (regm & 0xf0)
01271     {
01272     case 0x00:
01273         if (high)
01274         {
01275             switch (regm & 0x0f)
01276             {
01277             case 0x04:
01278                 OPL3_ChannelSet4Op(chip, v);
01279                 break;
01280             case 0x05:
01281                 chip->newm = v & 0x01;
01282                 break;
01283             }
01284         }
01285         else
01286         {
01287             switch (regm & 0x0f)
01288             {
01289             case 0x08:
01290                 chip->nts = (v >> 6) & 0x01;
01291                 break;
01292             }
01293         }
01294         break;
01295     case 0x20:
01296     case 0x30:
01297         if (ad_slot[regm & 0x1f] >= 0)
01298         {
01299             OPL3_SlotWrite20(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
01300         }
01301         break;
01302     case 0x40:
01303     case 0x50:
01304         if (ad_slot[regm & 0x1f] >= 0)
01305         {
01306             OPL3_SlotWrite40(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
01307         }
01308         break;
01309     case 0x60:
01310     case 0x70:
01311         if (ad_slot[regm & 0x1f] >= 0)
01312         {
01313             OPL3_SlotWrite60(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
01314         }
01315         break;
01316     case 0x80:
01317     case 0x90:
01318         if (ad_slot[regm & 0x1f] >= 0)
01319         {
01320             OPL3_SlotWrite80(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
01321         }
01322         break;
01323     case 0xe0:
01324     case 0xf0:
01325         if (ad_slot[regm & 0x1f] >= 0)
01326         {
01327             OPL3_SlotWriteE0(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
01328         }
01329         break;
01330     case 0xa0:
01331         if ((regm & 0x0f) < 9)
01332         {
01333             OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v);
01334         }
01335         break;
01336     case 0xb0:
01337         if (regm == 0xbd && !high)
01338         {
01339             chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2;
01340             chip->vibshift = ((v >> 6) & 0x01) ^ 1;
01341             OPL3_ChannelUpdateRhythm(chip, v);
01342         }
01343         else if ((regm & 0x0f) < 9)
01344         {
01345             OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v);
01346             if (v & 0x20)
01347             {
01348                 OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]);
01349             }
01350             else
01351             {
01352                 OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]);
01353             }
01354         }
01355         break;
01356     case 0xc0:
01357         if ((regm & 0x0f) < 9)
01358         {
01359             OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v);
01360         }
01361         break;
01362     }
01363 }
01364 
01365 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
01366 {
01367     Bit32u i;
01368 
01369     for(i = 0; i < numsamples; i++)
01370     {
01371         OPL3_GenerateResampled(chip, sndptr);
01372         sndptr += 2;
01373     }
01374 }