1 /*
2  * Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  *  Nuked OPL3 emulator.
19  *  Thanks:
20  *      MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
21  *          Feedback and Rhythm part calculation information.
22  *      forums.submarine.org.uk(carbon14, opl3):
23  *          Tremolo and phase generator calculation information.
24  *      OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
25  *          OPL2 ROMs.
26  *
27  * version: 1.7.4
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "nukedopl3_174.h"
34 
35 #define RSM_FRAC    10
36 
37 /* Channel types */
38 
39 enum {
40     ch_2op = 0,
41     ch_4op = 1,
42     ch_4op2 = 2,
43     ch_drum = 3
44 };
45 
46 /* Envelope key types */
47 
48 enum {
49     egk_norm = 0x01,
50     egk_drum = 0x02
51 };
52 
53 
54 /*
55  * logsin table
56  */
57 
58 static const Bit16u logsinrom[512] = {
59     0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
60     0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
61     0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
62     0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
63     0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
64     0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
65     0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
66     0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
67     0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
68     0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
69     0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
70     0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
71     0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
72     0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
73     0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
74     0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
75     0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
76     0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
77     0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
78     0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
79     0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
80     0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
81     0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
82     0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
83     0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
84     0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
85     0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
86     0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
87     0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
88     0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
89     0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
90     0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
91     0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
92     0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x002,
93     0x002, 0x002, 0x002, 0x003, 0x003, 0x003, 0x004, 0x004,
94     0x004, 0x005, 0x005, 0x005, 0x006, 0x006, 0x007, 0x007,
95     0x007, 0x008, 0x008, 0x009, 0x009, 0x00a, 0x00a, 0x00b,
96     0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00f, 0x00f, 0x010,
97     0x011, 0x011, 0x012, 0x013, 0x014, 0x014, 0x015, 0x016,
98     0x017, 0x017, 0x018, 0x019, 0x01a, 0x01b, 0x01c, 0x01d,
99     0x01e, 0x01f, 0x020, 0x021, 0x022, 0x023, 0x024, 0x025,
100     0x026, 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02d, 0x02e,
101     0x02f, 0x030, 0x031, 0x033, 0x034, 0x035, 0x037, 0x038,
102     0x039, 0x03b, 0x03c, 0x03e, 0x03f, 0x040, 0x042, 0x043,
103     0x045, 0x046, 0x048, 0x04a, 0x04b, 0x04d, 0x04e, 0x050,
104     0x052, 0x053, 0x055, 0x057, 0x059, 0x05b, 0x05c, 0x05e,
105     0x060, 0x062, 0x064, 0x066, 0x068, 0x06a, 0x06c, 0x06e,
106     0x070, 0x072, 0x074, 0x076, 0x078, 0x07a, 0x07d, 0x07f,
107     0x081, 0x083, 0x086, 0x088, 0x08a, 0x08d, 0x08f, 0x092,
108     0x094, 0x097, 0x099, 0x09c, 0x09f, 0x0a1, 0x0a4, 0x0a7,
109     0x0a9, 0x0ac, 0x0af, 0x0b2, 0x0b5, 0x0b8, 0x0bb, 0x0be,
110     0x0c1, 0x0c4, 0x0c7, 0x0ca, 0x0cd, 0x0d1, 0x0d4, 0x0d7,
111     0x0db, 0x0de, 0x0e2, 0x0e5, 0x0e9, 0x0ec, 0x0f0, 0x0f4,
112     0x0f8, 0x0fb, 0x0ff, 0x103, 0x107, 0x10b, 0x10f, 0x114,
113     0x118, 0x11c, 0x121, 0x125, 0x129, 0x12e, 0x133, 0x137,
114     0x13c, 0x141, 0x146, 0x14b, 0x150, 0x155, 0x15b, 0x160,
115     0x166, 0x16b, 0x171, 0x177, 0x17c, 0x182, 0x188, 0x18f,
116     0x195, 0x19b, 0x1a2, 0x1a9, 0x1b0, 0x1b7, 0x1be, 0x1c5,
117     0x1cd, 0x1d4, 0x1dc, 0x1e4, 0x1ec, 0x1f5, 0x1fd, 0x206,
118     0x20f, 0x218, 0x222, 0x22c, 0x236, 0x240, 0x24b, 0x256,
119     0x261, 0x26d, 0x279, 0x286, 0x293, 0x2a0, 0x2af, 0x2bd,
120     0x2cd, 0x2dc, 0x2ed, 0x2ff, 0x311, 0x324, 0x339, 0x34e,
121     0x365, 0x37e, 0x398, 0x3b5, 0x3d3, 0x3f5, 0x41a, 0x443,
122     0x471, 0x4a6, 0x4e4, 0x52e, 0x58b, 0x607, 0x6c3, 0x859
123 };
124 
125 /*
126  * exp table
127  */
128 
129 static const Bit16u exprom[256] = {
130     0xff4, 0xfea, 0xfde, 0xfd4, 0xfc8, 0xfbe, 0xfb4, 0xfa8,
131     0xf9e, 0xf92, 0xf88, 0xf7e, 0xf72, 0xf68, 0xf5c, 0xf52,
132     0xf48, 0xf3e, 0xf32, 0xf28, 0xf1e, 0xf14, 0xf08, 0xefe,
133     0xef4, 0xeea, 0xee0, 0xed4, 0xeca, 0xec0, 0xeb6, 0xeac,
134     0xea2, 0xe98, 0xe8e, 0xe84, 0xe7a, 0xe70, 0xe66, 0xe5c,
135     0xe52, 0xe48, 0xe3e, 0xe34, 0xe2a, 0xe20, 0xe16, 0xe0c,
136     0xe04, 0xdfa, 0xdf0, 0xde6, 0xddc, 0xdd2, 0xdca, 0xdc0,
137     0xdb6, 0xdac, 0xda4, 0xd9a, 0xd90, 0xd88, 0xd7e, 0xd74,
138     0xd6a, 0xd62, 0xd58, 0xd50, 0xd46, 0xd3c, 0xd34, 0xd2a,
139     0xd22, 0xd18, 0xd10, 0xd06, 0xcfe, 0xcf4, 0xcec, 0xce2,
140     0xcda, 0xcd0, 0xcc8, 0xcbe, 0xcb6, 0xcae, 0xca4, 0xc9c,
141     0xc92, 0xc8a, 0xc82, 0xc78, 0xc70, 0xc68, 0xc60, 0xc56,
142     0xc4e, 0xc46, 0xc3c, 0xc34, 0xc2c, 0xc24, 0xc1c, 0xc12,
143     0xc0a, 0xc02, 0xbfa, 0xbf2, 0xbea, 0xbe0, 0xbd8, 0xbd0,
144     0xbc8, 0xbc0, 0xbb8, 0xbb0, 0xba8, 0xba0, 0xb98, 0xb90,
145     0xb88, 0xb80, 0xb78, 0xb70, 0xb68, 0xb60, 0xb58, 0xb50,
146     0xb48, 0xb40, 0xb38, 0xb32, 0xb2a, 0xb22, 0xb1a, 0xb12,
147     0xb0a, 0xb02, 0xafc, 0xaf4, 0xaec, 0xae4, 0xade, 0xad6,
148     0xace, 0xac6, 0xac0, 0xab8, 0xab0, 0xaa8, 0xaa2, 0xa9a,
149     0xa92, 0xa8c, 0xa84, 0xa7c, 0xa76, 0xa6e, 0xa68, 0xa60,
150     0xa58, 0xa52, 0xa4a, 0xa44, 0xa3c, 0xa36, 0xa2e, 0xa28,
151     0xa20, 0xa18, 0xa12, 0xa0c, 0xa04, 0x9fe, 0x9f6, 0x9f0,
152     0x9e8, 0x9e2, 0x9da, 0x9d4, 0x9ce, 0x9c6, 0x9c0, 0x9b8,
153     0x9b2, 0x9ac, 0x9a4, 0x99e, 0x998, 0x990, 0x98a, 0x984,
154     0x97c, 0x976, 0x970, 0x96a, 0x962, 0x95c, 0x956, 0x950,
155     0x948, 0x942, 0x93c, 0x936, 0x930, 0x928, 0x922, 0x91c,
156     0x916, 0x910, 0x90a, 0x904, 0x8fc, 0x8f6, 0x8f0, 0x8ea,
157     0x8e4, 0x8de, 0x8d8, 0x8d2, 0x8cc, 0x8c6, 0x8c0, 0x8ba,
158     0x8b4, 0x8ae, 0x8a8, 0x8a2, 0x89c, 0x896, 0x890, 0x88a,
159     0x884, 0x87e, 0x878, 0x872, 0x86c, 0x866, 0x860, 0x85a,
160     0x854, 0x850, 0x84a, 0x844, 0x83e, 0x838, 0x832, 0x82c,
161     0x828, 0x822, 0x81c, 0x816, 0x810, 0x80c, 0x806, 0x800
162 };
163 
164 /*
165  * freq mult table multiplied by 2
166  *
167  * 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15
168  */
169 
170 static const Bit8u mt[16] = {
171     1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
172 };
173 
174 /*
175  * ksl table
176  */
177 
178 static const Bit8u kslrom[16] = {
179     0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
180 };
181 
182 static const Bit8u kslshift[4] = {
183     8, 1, 2, 0
184 };
185 
186 /*
187  * envelope generator constants
188  */
189 
190 static const Bit8u eg_incstep[3][4][8] = {
191     {
192         { 0, 0, 0, 0, 0, 0, 0, 0 },
193         { 0, 0, 0, 0, 0, 0, 0, 0 },
194         { 0, 0, 0, 0, 0, 0, 0, 0 },
195         { 0, 0, 0, 0, 0, 0, 0, 0 }
196     },
197     {
198         { 0, 1, 0, 1, 0, 1, 0, 1 },
199         { 0, 1, 0, 1, 1, 1, 0, 1 },
200         { 0, 1, 1, 1, 0, 1, 1, 1 },
201         { 0, 1, 1, 1, 1, 1, 1, 1 }
202     },
203     {
204         { 1, 1, 1, 1, 1, 1, 1, 1 },
205         { 2, 2, 1, 1, 1, 1, 1, 1 },
206         { 2, 2, 1, 1, 2, 2, 1, 1 },
207         { 2, 2, 2, 2, 2, 2, 1, 1 }
208     }
209 };
210 
211 static const Bit8u eg_incdesc[16] = {
212     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2
213 };
214 
215 static const Bit8s eg_incsh[16] = {
216     0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, -1, -2
217 };
218 
219 /*
220  * address decoding
221  */
222 
223 static const Bit8s ad_slot[0x20] = {
224     0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
225     12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
226 };
227 
228 static const Bit8u ch_slot[18] = {
229     0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
230 };
231 
232 /*
233  * Pan law table
234  */
235 
236 static const Bit16u panlawtable[] =
237 {
238     65535, 65529, 65514, 65489, 65454, 65409, 65354, 65289,
239     65214, 65129, 65034, 64929, 64814, 64689, 64554, 64410,
240     64255, 64091, 63917, 63733, 63540, 63336, 63123, 62901,
241     62668, 62426, 62175, 61914, 61644, 61364, 61075, 60776,
242     60468, 60151, 59825, 59489, 59145, 58791, 58428, 58057,
243     57676, 57287, 56889, 56482, 56067, 55643, 55211, 54770,
244     54320, 53863, 53397, 52923, 52441, 51951, 51453, 50947,
245     50433, 49912, 49383, 48846, 48302, 47750, 47191,
246     46340, /* Center left */
247     46340, /* Center right */
248     45472, 44885, 44291, 43690, 43083, 42469, 41848, 41221,
249     40588, 39948, 39303, 38651, 37994, 37330, 36661, 35986,
250     35306, 34621, 33930, 33234, 32533, 31827, 31116, 30400,
251     29680, 28955, 28225, 27492, 26754, 26012, 25266, 24516,
252     23762, 23005, 22244, 21480, 20713, 19942, 19169, 18392,
253     17613, 16831, 16046, 15259, 14469, 13678, 12884, 12088,
254     11291, 10492, 9691, 8888, 8085, 7280, 6473, 5666,
255     4858, 4050, 3240, 2431, 1620, 810, 0
256 };
257 
258 /*
259  * Envelope generator
260  */
261 
262 static void OPL3_EnvelopeGenOff(opl3_slot *slot);
263 static void OPL3_EnvelopeGenAttack(opl3_slot *slot);
264 static void OPL3_EnvelopeGenDecay(opl3_slot *slot);
265 static void OPL3_EnvelopeGenSustain(opl3_slot *slot);
266 static void OPL3_EnvelopeGenRelease(opl3_slot *slot);
267 
268 typedef void(*envelope_genfunc)(opl3_slot *slot);
269 
270 envelope_genfunc envelope_gen[5] = {
271     OPL3_EnvelopeGenOff,
272     OPL3_EnvelopeGenAttack,
273     OPL3_EnvelopeGenDecay,
274     OPL3_EnvelopeGenSustain,
275     OPL3_EnvelopeGenRelease
276 };
277 
278 enum envelope_gen_num
279 {
280     envelope_gen_num_off = 0,
281     envelope_gen_num_attack = 1,
282     envelope_gen_num_decay = 2,
283     envelope_gen_num_sustain = 3,
284     envelope_gen_num_release = 4
285 };
286 
OPL3_EnvelopeCalcRate(opl3_slot * slot,Bit8u reg_rate)287 static Bit8u OPL3_EnvelopeCalcRate(opl3_slot *slot, Bit8u reg_rate)
288 {
289     Bit8u rate;
290     if (reg_rate == 0x00)
291     {
292         return 0x00;
293     }
294     rate = (reg_rate << 2)
295          + (slot->reg_ksr ? slot->channel->ksv : (slot->channel->ksv >> 2));
296     if (rate > 0x3c)
297     {
298         rate = 0x3c;
299     }
300     return rate;
301 }
302 
OPL3_EnvelopeUpdateKSL(opl3_slot * slot)303 static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
304 {
305     Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2)
306                - ((0x08 - slot->channel->block) << 5);
307     if (ksl < 0)
308     {
309         ksl = 0;
310     }
311     slot->eg_ksl = (Bit8u)ksl;
312 }
313 
OPL3_EnvelopeUpdateRate(opl3_slot * slot)314 static void OPL3_EnvelopeUpdateRate(opl3_slot *slot)
315 {
316     switch (slot->eg_gen)
317     {
318     case envelope_gen_num_off:
319     case envelope_gen_num_attack:
320         slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_ar);
321         break;
322     case envelope_gen_num_decay:
323         slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_dr);
324         break;
325     case envelope_gen_num_sustain:
326     case envelope_gen_num_release:
327         slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_rr);
328         break;
329     }
330 }
331 
OPL3_EnvelopeGenOff(opl3_slot * slot)332 static void OPL3_EnvelopeGenOff(opl3_slot *slot)
333 {
334     slot->eg_rout = 0x1ff;
335 }
336 
OPL3_EnvelopeGenAttack(opl3_slot * slot)337 static void OPL3_EnvelopeGenAttack(opl3_slot *slot)
338 {
339     if (slot->eg_rout == 0x00)
340     {
341         slot->eg_gen = envelope_gen_num_decay;
342         OPL3_EnvelopeUpdateRate(slot);
343         return;
344     }
345     slot->eg_rout += ((~slot->eg_rout) * slot->eg_inc) >> 3;
346     if (slot->eg_rout < 0x00)
347     {
348         slot->eg_rout = 0x00;
349     }
350 }
351 
OPL3_EnvelopeGenDecay(opl3_slot * slot)352 static void OPL3_EnvelopeGenDecay(opl3_slot *slot)
353 {
354     if (slot->eg_rout >= slot->reg_sl << 4)
355     {
356         slot->eg_gen = envelope_gen_num_sustain;
357         OPL3_EnvelopeUpdateRate(slot);
358         return;
359     }
360     slot->eg_rout += slot->eg_inc;
361 }
362 
OPL3_EnvelopeGenSustain(opl3_slot * slot)363 static void OPL3_EnvelopeGenSustain(opl3_slot *slot)
364 {
365     if (!slot->reg_type)
366     {
367         OPL3_EnvelopeGenRelease(slot);
368     }
369 }
370 
OPL3_EnvelopeGenRelease(opl3_slot * slot)371 static void OPL3_EnvelopeGenRelease(opl3_slot *slot)
372 {
373     if (slot->eg_rout >= 0x1ff)
374     {
375         slot->eg_gen = envelope_gen_num_off;
376         slot->eg_rout = 0x1ff;
377         OPL3_EnvelopeUpdateRate(slot);
378         return;
379     }
380     slot->eg_rout += slot->eg_inc;
381 }
382 
OPL3_EnvelopeCalc(opl3_slot * slot)383 static void OPL3_EnvelopeCalc(opl3_slot *slot)
384 {
385     Bit8u rate_h, rate_l;
386     Bit8u inc = 0;
387     rate_h = slot->eg_rate >> 2;
388     rate_l = slot->eg_rate & 3;
389     if (eg_incsh[rate_h] > 0)
390     {
391         if ((slot->chip->timer & ((1 << eg_incsh[rate_h]) - 1)) == 0)
392         {
393             inc = eg_incstep[eg_incdesc[rate_h]][rate_l]
394                             [((slot->chip->timer)>> eg_incsh[rate_h]) & 0x07];
395         }
396     }
397     else
398     {
399         inc = eg_incstep[eg_incdesc[rate_h]][rate_l]
400                         [slot->chip->timer & 0x07] << (-eg_incsh[rate_h]);
401     }
402     slot->eg_inc = inc;
403     slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)
404                  + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;
405     if (slot->eg_out > 0x1ff)  /* TODO: Remove this if possible */
406     {
407         slot->eg_out = 0x1ff;
408     }
409     slot->eg_out <<= 3;
410 
411     envelope_gen[slot->eg_gen](slot);
412 }
413 
OPL3_EnvelopeKeyOn(opl3_slot * slot,Bit8u type)414 static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type)
415 {
416     if (!slot->key)
417     {
418         slot->eg_gen = envelope_gen_num_attack;
419         OPL3_EnvelopeUpdateRate(slot);
420         if ((slot->eg_rate >> 2) == 0x0f)
421         {
422             slot->eg_gen = envelope_gen_num_decay;
423             OPL3_EnvelopeUpdateRate(slot);
424             slot->eg_rout = 0x00;
425         }
426         slot->pg_phase = 0x00;
427     }
428     slot->key |= type;
429 }
430 
OPL3_EnvelopeKeyOff(opl3_slot * slot,Bit8u type)431 static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type)
432 {
433     if (slot->key)
434     {
435         slot->key &= (~type);
436         if (!slot->key)
437         {
438             slot->eg_gen = envelope_gen_num_release;
439             OPL3_EnvelopeUpdateRate(slot);
440         }
441     }
442 }
443 
444 /*
445  * Phase Generator
446  */
447 
OPL3_PhaseGenerate(opl3_slot * slot)448 static void OPL3_PhaseGenerate(opl3_slot *slot)
449 {
450     Bit16u f_num;
451     Bit32u basefreq;
452 
453     f_num = slot->channel->f_num;
454     if (slot->reg_vib)
455     {
456         Bit8s range;
457         Bit8u vibpos;
458 
459         range = (f_num >> 7) & 7;
460         vibpos = slot->chip->vibpos;
461 
462         if (!(vibpos & 3))
463         {
464             range = 0;
465         }
466         else if (vibpos & 1)
467         {
468             range >>= 1;
469         }
470         range >>= slot->chip->vibshift;
471 
472         if (vibpos & 4)
473         {
474             range = -range;
475         }
476         f_num += range;
477     }
478     basefreq = (f_num << slot->channel->block) >> 1;
479     slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;
480 }
481 
482 /*
483  * Noise Generator
484  */
485 
OPL3_NoiseGenerate(opl3_chip * chip)486 static void OPL3_NoiseGenerate(opl3_chip *chip)
487 {
488     if (chip->noise & 0x01)
489     {
490         chip->noise ^= 0x800302;
491     }
492     chip->noise >>= 1;
493 }
494 
495 /*
496  * Slot
497  */
498 
OPL3_SlotWrite20(opl3_slot * slot,Bit8u data)499 static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data)
500 {
501     if ((data >> 7) & 0x01)
502     {
503         slot->trem = &slot->chip->tremolo;
504     }
505     else
506     {
507         slot->trem = (Bit8u*)&slot->chip->zeromod;
508     }
509     slot->reg_vib = (data >> 6) & 0x01;
510     slot->reg_type = (data >> 5) & 0x01;
511     slot->reg_ksr = (data >> 4) & 0x01;
512     slot->reg_mult = data & 0x0f;
513     OPL3_EnvelopeUpdateRate(slot);
514 }
515 
OPL3_SlotWrite40(opl3_slot * slot,Bit8u data)516 static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data)
517 {
518     slot->reg_ksl = (data >> 6) & 0x03;
519     slot->reg_tl = data & 0x3f;
520     OPL3_EnvelopeUpdateKSL(slot);
521 }
522 
OPL3_SlotWrite60(opl3_slot * slot,Bit8u data)523 static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data)
524 {
525     slot->reg_ar = (data >> 4) & 0x0f;
526     slot->reg_dr = data & 0x0f;
527     OPL3_EnvelopeUpdateRate(slot);
528 }
529 
OPL3_SlotWrite80(opl3_slot * slot,Bit8u data)530 static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data)
531 {
532     slot->reg_sl = (data >> 4) & 0x0f;
533     if (slot->reg_sl == 0x0f)
534     {
535         slot->reg_sl = 0x1f;
536     }
537     slot->reg_rr = data & 0x0f;
538     OPL3_EnvelopeUpdateRate(slot);
539 }
540 
OPL3_SlotWriteE0(opl3_slot * slot,Bit8u data)541 static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data)
542 {
543     slot->reg_wf = data & 0x07;
544     if (slot->chip->newm == 0x00)
545     {
546         slot->reg_wf &= 0x03;
547     }
548 
549     switch (slot->reg_wf)
550     {
551     case 1:
552     case 4:
553     case 5:
554         slot->maskzero = 0x200;
555         break;
556     case 3:
557         slot->maskzero = 0x100;
558         break;
559     default:
560         slot->maskzero = 0;
561         break;
562     }
563 
564     switch (slot->reg_wf)
565     {
566     case 4:
567         slot->signpos = (31-8);  /* sigext of (phase & 0x100) */
568         break;
569     case 0:
570     case 6:
571     case 7:
572         slot->signpos = (31-9);  /* sigext of (phase & 0x200) */
573         break;
574     default:
575         slot->signpos = (31-16);  /* set "neg" to zero */
576         break;
577     }
578 
579     switch (slot->reg_wf)
580     {
581     case 4:
582     case 5:
583         slot->phaseshift = 1;
584         break;
585     case 6:
586         slot->phaseshift = 16; /* set phase to zero and flag for non-sin wave */
587         break;
588     case 7:
589         slot->phaseshift = 32; /* no shift (work by mod 32), but flag for non-sin wave */
590         break;
591     default:
592         slot->phaseshift = 0;
593         break;
594     }
595 }
596 
OPL3_SlotGeneratePhase(opl3_slot * slot,Bit16u phase)597 static void OPL3_SlotGeneratePhase(opl3_slot *slot, Bit16u phase)
598 {
599     Bit32u neg, level;
600     Bit8u  phaseshift;
601 
602     /* Fast paths for mute segments */
603     if (phase & slot->maskzero)
604     {
605         slot->out = 0;
606         return;
607     }
608 
609     neg = (Bit32s)((Bit32u)phase << slot->signpos) >> 31;
610     phaseshift = slot->phaseshift;
611     level = slot->eg_out;
612 
613     phase <<= phaseshift;
614     if (phaseshift <= 1)
615     {
616         level += logsinrom[phase & 0x1ff];
617     }
618     else
619     {
620         level += ((phase ^ neg) & 0x3ff) << 3;
621     }
622     slot->out = exprom[level & 0xff] >> (level >> 8) ^ neg;
623 }
624 
OPL3_SlotGenerate(opl3_slot * slot)625 static void OPL3_SlotGenerate(opl3_slot *slot)
626 {
627     OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9) + *slot->mod);
628 }
629 
OPL3_SlotGenerateZM(opl3_slot * slot)630 static void OPL3_SlotGenerateZM(opl3_slot *slot)
631 {
632     OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9));
633 }
634 
OPL3_SlotCalcFB(opl3_slot * slot)635 static void OPL3_SlotCalcFB(opl3_slot *slot)
636 {
637     if (slot->channel->fb != 0x00)
638     {
639         slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
640     }
641     else
642     {
643         slot->fbmod = 0;
644     }
645     slot->prout = slot->out;
646 }
647 
648 /*
649  * Channel
650  */
651 
652 static void OPL3_ChannelSetupAlg(opl3_channel *channel);
653 
OPL3_ChannelUpdateRhythm(opl3_chip * chip,Bit8u data)654 static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data)
655 {
656     opl3_channel *channel6;
657     opl3_channel *channel7;
658     opl3_channel *channel8;
659     Bit8u chnum;
660 
661     chip->rhy = data & 0x3f;
662     if (chip->rhy & 0x20)
663     {
664         channel6 = &chip->channel[6];
665         channel7 = &chip->channel[7];
666         channel8 = &chip->channel[8];
667         channel6->out[0] = &channel6->slotz[1]->out;
668         channel6->out[1] = &channel6->slotz[1]->out;
669         channel6->out[2] = &chip->zeromod;
670         channel6->out[3] = &chip->zeromod;
671         channel7->out[0] = &channel7->slotz[0]->out;
672         channel7->out[1] = &channel7->slotz[0]->out;
673         channel7->out[2] = &channel7->slotz[1]->out;
674         channel7->out[3] = &channel7->slotz[1]->out;
675         channel8->out[0] = &channel8->slotz[0]->out;
676         channel8->out[1] = &channel8->slotz[0]->out;
677         channel8->out[2] = &channel8->slotz[1]->out;
678         channel8->out[3] = &channel8->slotz[1]->out;
679         for (chnum = 6; chnum < 9; chnum++)
680         {
681             chip->channel[chnum].chtype = ch_drum;
682         }
683         OPL3_ChannelSetupAlg(channel6);
684         /*hh*/
685         if (chip->rhy & 0x01)
686         {
687             OPL3_EnvelopeKeyOn(channel7->slotz[0], egk_drum);
688         }
689         else
690         {
691             OPL3_EnvelopeKeyOff(channel7->slotz[0], egk_drum);
692         }
693         /*tc*/
694         if (chip->rhy & 0x02)
695         {
696             OPL3_EnvelopeKeyOn(channel8->slotz[1], egk_drum);
697         }
698         else
699         {
700             OPL3_EnvelopeKeyOff(channel8->slotz[1], egk_drum);
701         }
702         /*tom*/
703         if (chip->rhy & 0x04)
704         {
705             OPL3_EnvelopeKeyOn(channel8->slotz[0], egk_drum);
706         }
707         else
708         {
709             OPL3_EnvelopeKeyOff(channel8->slotz[0], egk_drum);
710         }
711         /*sd*/
712         if (chip->rhy & 0x08)
713         {
714             OPL3_EnvelopeKeyOn(channel7->slotz[1], egk_drum);
715         }
716         else
717         {
718             OPL3_EnvelopeKeyOff(channel7->slotz[1], egk_drum);
719         }
720         /*bd*/
721         if (chip->rhy & 0x10)
722         {
723             OPL3_EnvelopeKeyOn(channel6->slotz[0], egk_drum);
724             OPL3_EnvelopeKeyOn(channel6->slotz[1], egk_drum);
725         }
726         else
727         {
728             OPL3_EnvelopeKeyOff(channel6->slotz[0], egk_drum);
729             OPL3_EnvelopeKeyOff(channel6->slotz[1], egk_drum);
730         }
731     }
732     else
733     {
734         for (chnum = 6; chnum < 9; chnum++)
735         {
736             chip->channel[chnum].chtype = ch_2op;
737             OPL3_ChannelSetupAlg(&chip->channel[chnum]);
738             OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[0], egk_drum);
739             OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[1], egk_drum);
740         }
741     }
742 }
743 
OPL3_ChannelWriteA0(opl3_channel * channel,Bit8u data)744 static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data)
745 {
746     if (channel->chip->newm && channel->chtype == ch_4op2)
747     {
748         return;
749     }
750     channel->f_num = (channel->f_num & 0x300) | data;
751     channel->ksv = (channel->block << 1)
752                  | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
753     OPL3_EnvelopeUpdateKSL(channel->slotz[0]);
754     OPL3_EnvelopeUpdateKSL(channel->slotz[1]);
755     OPL3_EnvelopeUpdateRate(channel->slotz[0]);
756     OPL3_EnvelopeUpdateRate(channel->slotz[1]);
757     if (channel->chip->newm && channel->chtype == ch_4op)
758     {
759         channel->pair->f_num = channel->f_num;
760         channel->pair->ksv = channel->ksv;
761         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]);
762         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]);
763         OPL3_EnvelopeUpdateRate(channel->pair->slotz[0]);
764         OPL3_EnvelopeUpdateRate(channel->pair->slotz[1]);
765     }
766 }
767 
OPL3_ChannelWriteB0(opl3_channel * channel,Bit8u data)768 static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data)
769 {
770     if (channel->chip->newm && channel->chtype == ch_4op2)
771     {
772         return;
773     }
774     channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8);
775     channel->block = (data >> 2) & 0x07;
776     channel->ksv = (channel->block << 1)
777                  | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
778     OPL3_EnvelopeUpdateKSL(channel->slotz[0]);
779     OPL3_EnvelopeUpdateKSL(channel->slotz[1]);
780     OPL3_EnvelopeUpdateRate(channel->slotz[0]);
781     OPL3_EnvelopeUpdateRate(channel->slotz[1]);
782     if (channel->chip->newm && channel->chtype == ch_4op)
783     {
784         channel->pair->f_num = channel->f_num;
785         channel->pair->block = channel->block;
786         channel->pair->ksv = channel->ksv;
787         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]);
788         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]);
789         OPL3_EnvelopeUpdateRate(channel->pair->slotz[0]);
790         OPL3_EnvelopeUpdateRate(channel->pair->slotz[1]);
791     }
792 }
793 
OPL3_ChannelSetupAlg(opl3_channel * channel)794 static void OPL3_ChannelSetupAlg(opl3_channel *channel)
795 {
796     if (channel->chtype == ch_drum)
797     {
798         switch (channel->alg & 0x01)
799         {
800         case 0x00:
801             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
802             channel->slotz[1]->mod = &channel->slotz[0]->out;
803             break;
804         case 0x01:
805             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
806             channel->slotz[1]->mod = &channel->chip->zeromod;
807             break;
808         }
809         return;
810     }
811     if (channel->alg & 0x08)
812     {
813         return;
814     }
815     if (channel->alg & 0x04)
816     {
817         channel->pair->out[0] = &channel->chip->zeromod;
818         channel->pair->out[1] = &channel->chip->zeromod;
819         channel->pair->out[2] = &channel->chip->zeromod;
820         channel->pair->out[3] = &channel->chip->zeromod;
821         switch (channel->alg & 0x03)
822         {
823         case 0x00:
824             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
825             channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out;
826             channel->slotz[0]->mod = &channel->pair->slotz[1]->out;
827             channel->slotz[1]->mod = &channel->slotz[0]->out;
828             channel->out[0] = &channel->slotz[1]->out;
829             channel->out[1] = &channel->chip->zeromod;
830             channel->out[2] = &channel->chip->zeromod;
831             channel->out[3] = &channel->chip->zeromod;
832             break;
833         case 0x01:
834             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
835             channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out;
836             channel->slotz[0]->mod = &channel->chip->zeromod;
837             channel->slotz[1]->mod = &channel->slotz[0]->out;
838             channel->out[0] = &channel->pair->slotz[1]->out;
839             channel->out[1] = &channel->slotz[1]->out;
840             channel->out[2] = &channel->chip->zeromod;
841             channel->out[3] = &channel->chip->zeromod;
842             break;
843         case 0x02:
844             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
845             channel->pair->slotz[1]->mod = &channel->chip->zeromod;
846             channel->slotz[0]->mod = &channel->pair->slotz[1]->out;
847             channel->slotz[1]->mod = &channel->slotz[0]->out;
848             channel->out[0] = &channel->pair->slotz[0]->out;
849             channel->out[1] = &channel->slotz[1]->out;
850             channel->out[2] = &channel->chip->zeromod;
851             channel->out[3] = &channel->chip->zeromod;
852             break;
853         case 0x03:
854             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
855             channel->pair->slotz[1]->mod = &channel->chip->zeromod;
856             channel->slotz[0]->mod = &channel->pair->slotz[1]->out;
857             channel->slotz[1]->mod = &channel->chip->zeromod;
858             channel->out[0] = &channel->pair->slotz[0]->out;
859             channel->out[1] = &channel->slotz[0]->out;
860             channel->out[2] = &channel->slotz[1]->out;
861             channel->out[3] = &channel->chip->zeromod;
862             break;
863         }
864     }
865     else
866     {
867         switch (channel->alg & 0x01)
868         {
869         case 0x00:
870             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
871             channel->slotz[1]->mod = &channel->slotz[0]->out;
872             channel->out[0] = &channel->slotz[1]->out;
873             channel->out[1] = &channel->chip->zeromod;
874             channel->out[2] = &channel->chip->zeromod;
875             channel->out[3] = &channel->chip->zeromod;
876             break;
877         case 0x01:
878             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
879             channel->slotz[1]->mod = &channel->chip->zeromod;
880             channel->out[0] = &channel->slotz[0]->out;
881             channel->out[1] = &channel->slotz[1]->out;
882             channel->out[2] = &channel->chip->zeromod;
883             channel->out[3] = &channel->chip->zeromod;
884             break;
885         }
886     }
887 }
888 
OPL3_ChannelWriteC0(opl3_channel * channel,Bit8u data)889 static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data)
890 {
891     channel->fb = (data & 0x0e) >> 1;
892     channel->con = data & 0x01;
893     channel->alg = channel->con;
894     if (channel->chip->newm)
895     {
896         if (channel->chtype == ch_4op)
897         {
898             channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
899             channel->alg = 0x08;
900             OPL3_ChannelSetupAlg(channel->pair);
901         }
902         else if (channel->chtype == ch_4op2)
903         {
904             channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
905             channel->pair->alg = 0x08;
906             OPL3_ChannelSetupAlg(channel);
907         }
908         else
909         {
910             OPL3_ChannelSetupAlg(channel);
911         }
912     }
913     else
914     {
915         OPL3_ChannelSetupAlg(channel);
916     }
917     if (channel->chip->newm)
918     {
919         channel->cha = ((data >> 4) & 0x01) ? ~0 : 0;
920         channel->chb = ((data >> 5) & 0x01) ? ~0 : 0;
921     }
922     else
923     {
924         channel->cha = channel->chb = ~0;
925     }
926 }
927 
OPL3_ChannelKeyOn(opl3_channel * channel)928 static void OPL3_ChannelKeyOn(opl3_channel *channel)
929 {
930     if (channel->chip->newm)
931     {
932         if (channel->chtype == ch_4op)
933         {
934             OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm);
935             OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm);
936             OPL3_EnvelopeKeyOn(channel->pair->slotz[0], egk_norm);
937             OPL3_EnvelopeKeyOn(channel->pair->slotz[1], egk_norm);
938         }
939         else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
940         {
941             OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm);
942             OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm);
943         }
944     }
945     else
946     {
947         OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm);
948         OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm);
949     }
950 }
951 
OPL3_ChannelKeyOff(opl3_channel * channel)952 static void OPL3_ChannelKeyOff(opl3_channel *channel)
953 {
954     if (channel->chip->newm)
955     {
956         if (channel->chtype == ch_4op)
957         {
958             OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm);
959             OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm);
960             OPL3_EnvelopeKeyOff(channel->pair->slotz[0], egk_norm);
961             OPL3_EnvelopeKeyOff(channel->pair->slotz[1], egk_norm);
962         }
963         else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
964         {
965             OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm);
966             OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm);
967         }
968     }
969     else
970     {
971         OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm);
972         OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm);
973     }
974 }
975 
OPL3_ChannelSet4Op(opl3_chip * chip,Bit8u data)976 static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data)
977 {
978     Bit8u bit;
979     Bit8u chnum;
980     for (bit = 0; bit < 6; bit++)
981     {
982         chnum = bit;
983         if (bit >= 3)
984         {
985             chnum += 9 - 3;
986         }
987         if ((data >> bit) & 0x01)
988         {
989             chip->channel[chnum].chtype = ch_4op;
990             chip->channel[chnum + 3].chtype = ch_4op2;
991         }
992         else
993         {
994             chip->channel[chnum].chtype = ch_2op;
995             chip->channel[chnum + 3].chtype = ch_2op;
996         }
997     }
998 }
999 
OPL3_ClipSample(Bit32s sample)1000 static Bit16s OPL3_ClipSample(Bit32s sample)
1001 {
1002     if (sample > 32767)
1003     {
1004         sample = 32767;
1005     }
1006     else if (sample < -32768)
1007     {
1008         sample = -32768;
1009     }
1010     return (Bit16s)sample;
1011 }
1012 
OPL3_GenerateRhythm1(opl3_chip * chip)1013 static void OPL3_GenerateRhythm1(opl3_chip *chip)
1014 {
1015     opl3_channel *channel6;
1016     opl3_channel *channel7;
1017     opl3_channel *channel8;
1018     Bit16u phase14;
1019     Bit16u phase17;
1020     Bit16u phase;
1021     Bit16u phasebit;
1022 
1023     channel6 = &chip->channel[6];
1024     channel7 = &chip->channel[7];
1025     channel8 = &chip->channel[8];
1026     OPL3_SlotGenerate(channel6->slotz[0]);
1027     phase14 = (channel7->slotz[0]->pg_phase >> 9) & 0x3ff;
1028     phase17 = (channel8->slotz[1]->pg_phase >> 9) & 0x3ff;
1029     phase = 0x00;
1030     /*hh tc phase bit*/
1031     phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)
1032              | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;
1033     /*hh*/
1034     phase = (phasebit << 9)
1035           | (0x34 << ((phasebit ^ (chip->noise & 0x01)) << 1));
1036     OPL3_SlotGeneratePhase(channel7->slotz[0], phase);
1037     /*tt*/
1038     OPL3_SlotGenerateZM(channel8->slotz[0]);
1039 }
1040 
OPL3_GenerateRhythm2(opl3_chip * chip)1041 static void OPL3_GenerateRhythm2(opl3_chip *chip)
1042 {
1043     opl3_channel *channel6;
1044     opl3_channel *channel7;
1045     opl3_channel *channel8;
1046     Bit16u phase14;
1047     Bit16u phase17;
1048     Bit16u phase;
1049     Bit16u phasebit;
1050 
1051     channel6 = &chip->channel[6];
1052     channel7 = &chip->channel[7];
1053     channel8 = &chip->channel[8];
1054     OPL3_SlotGenerate(channel6->slotz[1]);
1055     phase14 = (channel7->slotz[0]->pg_phase >> 9) & 0x3ff;
1056     phase17 = (channel8->slotz[1]->pg_phase >> 9) & 0x3ff;
1057     phase = 0x00;
1058     /*hh tc phase bit*/
1059     phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)
1060              | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;
1061     /*sd*/
1062     phase = (0x100 << ((phase14 >> 8) & 0x01)) ^ ((chip->noise & 0x01) << 8);
1063     OPL3_SlotGeneratePhase(channel7->slotz[1], phase);
1064     /*tc*/
1065     phase = 0x100 | (phasebit << 9);
1066     OPL3_SlotGeneratePhase(channel8->slotz[1], phase);
1067 }
1068 
OPL3v17_Generate(opl3_chip * chip,Bit16s * buf)1069 void OPL3v17_Generate(opl3_chip *chip, Bit16s *buf)
1070 {
1071     Bit8u ii;
1072     Bit8u jj;
1073     Bit16s accm;
1074 
1075     buf[1] = OPL3_ClipSample(chip->mixbuff[1]);
1076 
1077     for (ii = 0; ii < 12; ii++)
1078     {
1079         OPL3_SlotCalcFB(&chip->chipslot[ii]);
1080         OPL3_PhaseGenerate(&chip->chipslot[ii]);
1081         OPL3_EnvelopeCalc(&chip->chipslot[ii]);
1082         OPL3_SlotGenerate(&chip->chipslot[ii]);
1083     }
1084 
1085     for (ii = 12; ii < 15; ii++)
1086     {
1087         OPL3_SlotCalcFB(&chip->chipslot[ii]);
1088         OPL3_PhaseGenerate(&chip->chipslot[ii]);
1089         OPL3_EnvelopeCalc(&chip->chipslot[ii]);
1090     }
1091 
1092     if (chip->rhy & 0x20)
1093     {
1094         OPL3_GenerateRhythm1(chip);
1095     }
1096     else
1097     {
1098         OPL3_SlotGenerate(&chip->chipslot[12]);
1099         OPL3_SlotGenerate(&chip->chipslot[13]);
1100         OPL3_SlotGenerate(&chip->chipslot[14]);
1101     }
1102 
1103     chip->mixbuff[0] = 0;
1104     for (ii = 0; ii < 18; ii++)
1105     {
1106         accm = 0;
1107         for (jj = 0; jj < 4; jj++)
1108         {
1109             accm += *chip->channel[ii].out[jj];
1110         }
1111         chip->mixbuff[0] += (Bit16s)((accm * chip->channel[ii].chl / 65535) & chip->channel[ii].cha);
1112     }
1113 
1114     for (ii = 15; ii < 18; ii++)
1115     {
1116         OPL3_SlotCalcFB(&chip->chipslot[ii]);
1117         OPL3_PhaseGenerate(&chip->chipslot[ii]);
1118         OPL3_EnvelopeCalc(&chip->chipslot[ii]);
1119     }
1120 
1121     if (chip->rhy & 0x20)
1122     {
1123         OPL3_GenerateRhythm2(chip);
1124     }
1125     else
1126     {
1127         OPL3_SlotGenerate(&chip->chipslot[15]);
1128         OPL3_SlotGenerate(&chip->chipslot[16]);
1129         OPL3_SlotGenerate(&chip->chipslot[17]);
1130     }
1131 
1132     buf[0] = OPL3_ClipSample(chip->mixbuff[0]);
1133 
1134     for (ii = 18; ii < 33; ii++)
1135     {
1136         OPL3_SlotCalcFB(&chip->chipslot[ii]);
1137         OPL3_PhaseGenerate(&chip->chipslot[ii]);
1138         OPL3_EnvelopeCalc(&chip->chipslot[ii]);
1139         OPL3_SlotGenerate(&chip->chipslot[ii]);
1140     }
1141 
1142     chip->mixbuff[1] = 0;
1143     for (ii = 0; ii < 18; ii++)
1144     {
1145         accm = 0;
1146         for (jj = 0; jj < 4; jj++)
1147         {
1148             accm += *chip->channel[ii].out[jj];
1149         }
1150         chip->mixbuff[1] += (Bit16s)((accm * chip->channel[ii].chr / 65535) & chip->channel[ii].chb);
1151     }
1152 
1153     for (ii = 33; ii < 36; ii++)
1154     {
1155         OPL3_SlotCalcFB(&chip->chipslot[ii]);
1156         OPL3_PhaseGenerate(&chip->chipslot[ii]);
1157         OPL3_EnvelopeCalc(&chip->chipslot[ii]);
1158         OPL3_SlotGenerate(&chip->chipslot[ii]);
1159     }
1160 
1161     OPL3_NoiseGenerate(chip);
1162 
1163     if ((chip->timer & 0x3f) == 0x3f)
1164     {
1165         chip->tremolopos = (chip->tremolopos + 1) % 210;
1166     }
1167     if (chip->tremolopos < 105)
1168     {
1169         chip->tremolo = chip->tremolopos >> chip->tremoloshift;
1170     }
1171     else
1172     {
1173         chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
1174     }
1175 
1176     if ((chip->timer & 0x3ff) == 0x3ff)
1177     {
1178         chip->vibpos = (chip->vibpos + 1) & 7;
1179     }
1180 
1181     chip->timer++;
1182 
1183     while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt)
1184     {
1185         if (!(chip->writebuf[chip->writebuf_cur].reg & 0x200))
1186         {
1187             break;
1188         }
1189         chip->writebuf[chip->writebuf_cur].reg &= 0x1ff;
1190         OPL3v17_WriteReg(chip, chip->writebuf[chip->writebuf_cur].reg,
1191                       chip->writebuf[chip->writebuf_cur].data);
1192         chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
1193     }
1194     chip->writebuf_samplecnt++;
1195 }
1196 
OPL3v17_GenerateResampled(opl3_chip * chip,Bit16s * buf)1197 void OPL3v17_GenerateResampled(opl3_chip *chip, Bit16s *buf)
1198 {
1199     while (chip->samplecnt >= chip->rateratio)
1200     {
1201         chip->oldsamples[0] = chip->samples[0];
1202         chip->oldsamples[1] = chip->samples[1];
1203         OPL3v17_Generate(chip, chip->samples);
1204         chip->samplecnt -= chip->rateratio;
1205     }
1206     buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
1207                      + chip->samples[0] * chip->samplecnt) / chip->rateratio);
1208     buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
1209                      + chip->samples[1] * chip->samplecnt) / chip->rateratio);
1210     chip->samplecnt += 1 << RSM_FRAC;
1211 }
1212 
OPL3v17_Reset(opl3_chip * chip,Bit32u samplerate)1213 void OPL3v17_Reset(opl3_chip *chip, Bit32u samplerate)
1214 {
1215     Bit8u slotnum;
1216     Bit8u channum;
1217 
1218     memset(chip, 0, sizeof(opl3_chip));
1219     for (slotnum = 0; slotnum < 36; slotnum++)
1220     {
1221         chip->chipslot[slotnum].chip = chip;
1222         chip->chipslot[slotnum].mod = &chip->zeromod;
1223         chip->chipslot[slotnum].eg_rout = 0x1ff;
1224         chip->chipslot[slotnum].eg_out = 0x1ff << 3;
1225         chip->chipslot[slotnum].eg_gen = envelope_gen_num_off;
1226         chip->chipslot[slotnum].trem = (Bit8u*)&chip->zeromod;
1227         chip->chipslot[slotnum].signpos = (31-9);  /* for wf=0 need use sigext of (phase & 0x200) */
1228     }
1229     for (channum = 0; channum < 18; channum++)
1230     {
1231         chip->channel[channum].slotz[0] = &chip->chipslot[ch_slot[channum]];
1232         chip->channel[channum].slotz[1] = &chip->chipslot[ch_slot[channum] + 3];
1233         chip->chipslot[ch_slot[channum]].channel = &chip->channel[channum];
1234         chip->chipslot[ch_slot[channum] + 3].channel = &chip->channel[channum];
1235         if ((channum % 9) < 3)
1236         {
1237             chip->channel[channum].pair = &chip->channel[channum + 3];
1238         }
1239         else if ((channum % 9) < 6)
1240         {
1241             chip->channel[channum].pair = &chip->channel[channum - 3];
1242         }
1243         chip->channel[channum].chip = chip;
1244         chip->channel[channum].out[0] = &chip->zeromod;
1245         chip->channel[channum].out[1] = &chip->zeromod;
1246         chip->channel[channum].out[2] = &chip->zeromod;
1247         chip->channel[channum].out[3] = &chip->zeromod;
1248         chip->channel[channum].chtype = ch_2op;
1249         chip->channel[channum].cha = 0xffff;
1250         chip->channel[channum].chb = 0xffff;
1251         chip->channel[channum].chl = 46340;
1252         chip->channel[channum].chr = 46340;
1253         OPL3_ChannelSetupAlg(&chip->channel[channum]);
1254     }
1255     chip->noise = 0x306600;
1256     chip->rateratio = (samplerate << RSM_FRAC) / 49716;
1257     chip->tremoloshift = 4;
1258     chip->vibshift = 1;
1259 }
1260 
OPL3v17_ChannelWritePan(opl3_channel * channel,Bit8u data)1261 static void OPL3v17_ChannelWritePan(opl3_channel *channel, Bit8u data)
1262 {
1263     channel->chl = panlawtable[data & 0x7F];
1264     channel->chr = panlawtable[0x7F - (data & 0x7F)];
1265 }
1266 
OPL3v17_WritePan(opl3_chip * chip,Bit16u reg,Bit8u v)1267 void OPL3v17_WritePan(opl3_chip *chip, Bit16u reg, Bit8u v)
1268 {
1269     Bit8u high = (reg >> 8) & 0x01;
1270     Bit8u regm = reg & 0xff;
1271     OPL3v17_ChannelWritePan(&chip->channel[9 * high + (regm & 0x0f)], v);
1272 }
1273 
OPL3v17_WriteReg(opl3_chip * chip,Bit16u reg,Bit8u v)1274 void OPL3v17_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v)
1275 {
1276     Bit8u high = (reg >> 8) & 0x01;
1277     Bit8u regm = reg & 0xff;
1278     switch (regm & 0xf0)
1279     {
1280     case 0x00:
1281         if (high)
1282         {
1283             switch (regm & 0x0f)
1284             {
1285             case 0x04:
1286                 OPL3_ChannelSet4Op(chip, v);
1287                 break;
1288             case 0x05:
1289                 chip->newm = v & 0x01;
1290                 break;
1291             }
1292         }
1293         else
1294         {
1295             switch (regm & 0x0f)
1296             {
1297             case 0x08:
1298                 chip->nts = (v >> 6) & 0x01;
1299                 break;
1300             }
1301         }
1302         break;
1303     case 0x20:
1304     case 0x30:
1305         if (ad_slot[regm & 0x1f] >= 0)
1306         {
1307             OPL3_SlotWrite20(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
1308         }
1309         break;
1310     case 0x40:
1311     case 0x50:
1312         if (ad_slot[regm & 0x1f] >= 0)
1313         {
1314             OPL3_SlotWrite40(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
1315         }
1316         break;
1317     case 0x60:
1318     case 0x70:
1319         if (ad_slot[regm & 0x1f] >= 0)
1320         {
1321             OPL3_SlotWrite60(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
1322         }
1323         break;
1324     case 0x80:
1325     case 0x90:
1326         if (ad_slot[regm & 0x1f] >= 0)
1327         {
1328             OPL3_SlotWrite80(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
1329         }
1330         break;
1331     case 0xe0:
1332     case 0xf0:
1333         if (ad_slot[regm & 0x1f] >= 0)
1334         {
1335             OPL3_SlotWriteE0(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
1336         }
1337         break;
1338     case 0xa0:
1339         if ((regm & 0x0f) < 9)
1340         {
1341             OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v);
1342         }
1343         break;
1344     case 0xb0:
1345         if (regm == 0xbd && !high)
1346         {
1347             chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2;
1348             chip->vibshift = ((v >> 6) & 0x01) ^ 1;
1349             OPL3_ChannelUpdateRhythm(chip, v);
1350         }
1351         else if ((regm & 0x0f) < 9)
1352         {
1353             OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v);
1354             if (v & 0x20)
1355             {
1356                 OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]);
1357             }
1358             else
1359             {
1360                 OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]);
1361             }
1362         }
1363         break;
1364     case 0xc0:
1365         if ((regm & 0x0f) < 9)
1366         {
1367             OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v);
1368         }
1369         break;
1370     }
1371 }
1372 
OPL3v17_WriteRegBuffered(opl3_chip * chip,Bit16u reg,Bit8u v)1373 void OPL3v17_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v)
1374 {
1375     Bit64u time1, time2;
1376 
1377     if (chip->writebuf[chip->writebuf_last].reg & 0x200)
1378     {
1379         OPL3v17_WriteReg(chip, chip->writebuf[chip->writebuf_last].reg & 0x1ff,
1380                       chip->writebuf[chip->writebuf_last].data);
1381 
1382         chip->writebuf_cur = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
1383         chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time;
1384     }
1385 
1386     chip->writebuf[chip->writebuf_last].reg = reg | 0x200;
1387     chip->writebuf[chip->writebuf_last].data = v;
1388     time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY;
1389     time2 = chip->writebuf_samplecnt;
1390 
1391     if (time1 < time2)
1392     {
1393         time1 = time2;
1394     }
1395 
1396     chip->writebuf[chip->writebuf_last].time = time1;
1397     chip->writebuf_lasttime = time1;
1398     chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
1399 }
1400 
OPL3v17_GenerateStream(opl3_chip * chip,Bit16s * sndptr,Bit32u numsamples)1401 void OPL3v17_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
1402 {
1403     Bit32u i;
1404 
1405     for(i = 0; i < numsamples; i++)
1406     {
1407         OPL3v17_GenerateResampled(chip, sndptr);
1408         sndptr += 2;
1409     }
1410 }
1411 
1412 #define OPL3_MIN(A, B)          (((A) > (B)) ? (B) : (A))
1413 #define OPL3_MAX(A, B)          (((A) < (B)) ? (B) : (A))
1414 #define OPL3_CLAMP(V, MIN, MAX) OPL3_MAX(OPL3_MIN(V, MAX), MIN)
1415 
OPL3v17_GenerateStreamMix(opl3_chip * chip,Bit16s * sndptr,Bit32u numsamples)1416 void OPL3v17_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
1417 {
1418     Bit32u i;
1419     Bit16s sample[2];
1420     Bit32s mix[2];
1421 
1422     for(i = 0; i < numsamples; i++)
1423     {
1424         OPL3v17_GenerateResampled(chip, sample);
1425         mix[0] = sndptr[0] + sample[0];
1426         mix[1] = sndptr[1] + sample[1];
1427         sndptr[0] = OPL3_CLAMP(mix[0], INT16_MIN, INT16_MAX);
1428         sndptr[1] = OPL3_CLAMP(mix[1], INT16_MIN, INT16_MAX);
1429         sndptr += 2;
1430     }
1431 }
1432 
1433