Debian Codegraph @

android/app/jni/extern/opl/opl3.c

raw
1 /* Nuked OPL3
2 * Copyright (C) 2013-2020 Nuke.YKT
3 *
4 * This file is part of Nuked OPL3.
5 *
6 * Nuked OPL3 is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 2.1
9 * of the License, or (at your option) any later version.
10 *
11 * Nuked OPL3 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with Nuked OPL3. If not, see <https://www.gnu.org/licenses/>.
18
19 * Nuked OPL3 emulator.
20 * Thanks:
21 * MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
22 * Feedback and Rhythm part calculation information.
23 * forums.submarine.org.uk(carbon14, opl3):
24 * Tremolo and phase generator calculation information.
25 * OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
26 * OPL2 ROMs.
27 * siliconpr0n.org(John McMaster, digshadow):
28 * YMF262 and VRC VII decaps and die shots.
29 *
30 * version: 1.8
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "opl3.h"
37
38 #if OPL_ENABLE_STEREOEXT && !defined OPL_SIN
39 #ifndef _USE_MATH_DEFINES
40 #define _USE_MATH_DEFINES 1
41 #endif
42 #include <math.h>
43 /* input: [0, 256), output: [0, 65536] */
44 #define OPL_SIN(x) ((int32_t)(sin((x) * M_PI / 512.0) * 65536.0))
45 #endif
46
47 /* Quirk: Some FM channels are output one sample later on the left side than the right. */
48 #ifndef OPL_QUIRK_CHANNELSAMPLEDELAY
49 #define OPL_QUIRK_CHANNELSAMPLEDELAY (!OPL_ENABLE_STEREOEXT)
50 #endif
51
52 #define RSM_FRAC 10
53
54 /* Channel types */
55
56 enum {
57 ch_2op = 0,
58 ch_4op = 1,
59 ch_4op2 = 2,
60 ch_drum = 3
61 };
62
63 /* Envelope key types */
64
65 enum {
66 egk_norm = 0x01,
67 egk_drum = 0x02
68 };
69
70
71 /*
72 logsin table
73 */
74
75 static const uint16_t logsinrom[256] = {
76 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
77 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
78 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
79 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
80 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
81 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
82 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
83 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
84 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
85 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
86 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
87 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
88 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
89 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
90 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
91 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
92 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
93 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
94 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
95 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
96 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
97 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
98 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
99 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
100 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
101 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
102 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
103 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
104 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
105 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
106 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
107 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
108 };
109
110 /*
111 exp table
112 */
113
114 static const uint16_t exprom[256] = {
115 0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
116 0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
117 0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
118 0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
119 0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
120 0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
121 0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
122 0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
123 0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
124 0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
125 0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
126 0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
127 0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
128 0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
129 0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
130 0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
131 0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
132 0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
133 0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
134 0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
135 0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
136 0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
137 0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
138 0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
139 0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
140 0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
141 0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
142 0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
143 0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
144 0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
145 0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
146 0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
147 };
148
149 /*
150 freq mult table multiplied by 2
151
152 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15
153 */
154
155 static const uint8_t mt[16] = {
156 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
157 };
158
159 /*
160 ksl table
161 */
162
163 static const uint8_t kslrom[16] = {
164 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
165 };
166
167 static const uint8_t kslshift[4] = {
168 8, 1, 2, 0
169 };
170
171 /*
172 envelope generator constants
173 */
174
175 static const uint8_t eg_incstep[4][4] = {
176 { 0, 0, 0, 0 },
177 { 1, 0, 0, 0 },
178 { 1, 0, 1, 0 },
179 { 1, 1, 1, 0 }
180 };
181
182 /*
183 address decoding
184 */
185
186 static const int8_t ad_slot[0x20] = {
187 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
188 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
189 };
190
191 static const uint8_t ch_slot[18] = {
192 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
193 };
194
195 #if OPL_ENABLE_STEREOEXT
196 /*
197 stereo extension panning table
198 */
199
200 static int32_t panpot_lut[256];
201 static uint8_t panpot_lut_build = 0;
202 #endif
203
204 /*
205 Envelope generator
206 */
207
208 typedef int16_t(*envelope_sinfunc)(uint16_t phase, uint16_t envelope);
209 typedef void(*envelope_genfunc)(opl3_slot *slott);
210
211 static int16_t OPL3_EnvelopeCalcExp(uint32_t level)
212 {
213 if (level > 0x1fff)
214 {
215 level = 0x1fff;
216 }
217 return (exprom[level & 0xff] << 1) >> (level >> 8);
218 }
219
220 static int16_t OPL3_EnvelopeCalcSin0(uint16_t phase, uint16_t envelope)
221 {
222 uint16_t out = 0;
223 uint16_t neg = 0;
224 phase &= 0x3ff;
225 if (phase & 0x200)
226 {
227 neg = 0xffff;
228 }
229 if (phase & 0x100)
230 {
231 out = logsinrom[(phase & 0xff) ^ 0xff];
232 }
233 else
234 {
235 out = logsinrom[phase & 0xff];
236 }
237 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
238 }
239
240 static int16_t OPL3_EnvelopeCalcSin1(uint16_t phase, uint16_t envelope)
241 {
242 uint16_t out = 0;
243 phase &= 0x3ff;
244 if (phase & 0x200)
245 {
246 out = 0x1000;
247 }
248 else if (phase & 0x100)
249 {
250 out = logsinrom[(phase & 0xff) ^ 0xff];
251 }
252 else
253 {
254 out = logsinrom[phase & 0xff];
255 }
256 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
257 }
258
259 static int16_t OPL3_EnvelopeCalcSin2(uint16_t phase, uint16_t envelope)
260 {
261 uint16_t out = 0;
262 phase &= 0x3ff;
263 if (phase & 0x100)
264 {
265 out = logsinrom[(phase & 0xff) ^ 0xff];
266 }
267 else
268 {
269 out = logsinrom[phase & 0xff];
270 }
271 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
272 }
273
274 static int16_t OPL3_EnvelopeCalcSin3(uint16_t phase, uint16_t envelope)
275 {
276 uint16_t out = 0;
277 phase &= 0x3ff;
278 if (phase & 0x100)
279 {
280 out = 0x1000;
281 }
282 else
283 {
284 out = logsinrom[phase & 0xff];
285 }
286 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
287 }
288
289 static int16_t OPL3_EnvelopeCalcSin4(uint16_t phase, uint16_t envelope)
290 {
291 uint16_t out = 0;
292 uint16_t neg = 0;
293 phase &= 0x3ff;
294 if ((phase & 0x300) == 0x100)
295 {
296 neg = 0xffff;
297 }
298 if (phase & 0x200)
299 {
300 out = 0x1000;
301 }
302 else if (phase & 0x80)
303 {
304 out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
305 }
306 else
307 {
308 out = logsinrom[(phase << 1) & 0xff];
309 }
310 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
311 }
312
313 static int16_t OPL3_EnvelopeCalcSin5(uint16_t phase, uint16_t envelope)
314 {
315 uint16_t out = 0;
316 phase &= 0x3ff;
317 if (phase & 0x200)
318 {
319 out = 0x1000;
320 }
321 else if (phase & 0x80)
322 {
323 out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
324 }
325 else
326 {
327 out = logsinrom[(phase << 1) & 0xff];
328 }
329 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
330 }
331
332 static int16_t OPL3_EnvelopeCalcSin6(uint16_t phase, uint16_t envelope)
333 {
334 uint16_t neg = 0;
335 phase &= 0x3ff;
336 if (phase & 0x200)
337 {
338 neg = 0xffff;
339 }
340 return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg;
341 }
342
343 static int16_t OPL3_EnvelopeCalcSin7(uint16_t phase, uint16_t envelope)
344 {
345 uint16_t out = 0;
346 uint16_t neg = 0;
347 phase &= 0x3ff;
348 if (phase & 0x200)
349 {
350 neg = 0xffff;
351 phase = (phase & 0x1ff) ^ 0x1ff;
352 }
353 out = phase << 3;
354 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
355 }
356
357 static const envelope_sinfunc envelope_sin[8] = {
358 OPL3_EnvelopeCalcSin0,
359 OPL3_EnvelopeCalcSin1,
360 OPL3_EnvelopeCalcSin2,
361 OPL3_EnvelopeCalcSin3,
362 OPL3_EnvelopeCalcSin4,
363 OPL3_EnvelopeCalcSin5,
364 OPL3_EnvelopeCalcSin6,
365 OPL3_EnvelopeCalcSin7
366 };
367
368 enum envelope_gen_num
369 {
370 envelope_gen_num_attack = 0,
371 envelope_gen_num_decay = 1,
372 envelope_gen_num_sustain = 2,
373 envelope_gen_num_release = 3
374 };
375
376 static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
377 {
378 int16_t ksl = (kslrom[slot->channel->f_num >> 6] << 2)
379 - ((0x08 - slot->channel->block) << 5);
380 if (ksl < 0)
381 {
382 ksl = 0;
383 }
384 slot->eg_ksl = (uint8_t)ksl;
385 }
386
387 static void OPL3_EnvelopeCalc(opl3_slot *slot)
388 {
389 uint8_t nonzero;
390 uint8_t rate;
391 uint8_t rate_hi;
392 uint8_t rate_lo;
393 uint8_t reg_rate = 0;
394 uint8_t ks;
395 uint8_t eg_shift, shift;
396 uint16_t eg_rout;
397 int16_t eg_inc;
398 uint8_t eg_off;
399 uint8_t reset = 0;
400 slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)
401 + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;
402 if (slot->key && slot->eg_gen == envelope_gen_num_release)
403 {
404 reset = 1;
405 reg_rate = slot->reg_ar;
406 }
407 else
408 {
409 switch (slot->eg_gen)
410 {
411 case envelope_gen_num_attack:
412 reg_rate = slot->reg_ar;
413 break;
414 case envelope_gen_num_decay:
415 reg_rate = slot->reg_dr;
416 break;
417 case envelope_gen_num_sustain:
418 if (!slot->reg_type)
419 {
420 reg_rate = slot->reg_rr;
421 }
422 break;
423 case envelope_gen_num_release:
424 reg_rate = slot->reg_rr;
425 break;
426 }
427 }
428 slot->pg_reset = reset;
429 ks = slot->channel->ksv >> ((slot->reg_ksr ^ 1) << 1);
430 nonzero = (reg_rate != 0);
431 rate = ks + (reg_rate << 2);
432 rate_hi = rate >> 2;
433 rate_lo = rate & 0x03;
434 if (rate_hi & 0x10)
435 {
436 rate_hi = 0x0f;
437 }
438 eg_shift = rate_hi + slot->chip->eg_add;
439 shift = 0;
440 if (nonzero)
441 {
442 if (rate_hi < 12)
443 {
444 if (slot->chip->eg_state)
445 {
446 switch (eg_shift)
447 {
448 case 12:
449 shift = 1;
450 break;
451 case 13:
452 shift = (rate_lo >> 1) & 0x01;
453 break;
454 case 14:
455 shift = rate_lo & 0x01;
456 break;
457 default:
458 break;
459 }
460 }
461 }
462 else
463 {
464 shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->timer & 0x03];
465 if (shift & 0x04)
466 {
467 shift = 0x03;
468 }
469 if (!shift)
470 {
471 shift = slot->chip->eg_state;
472 }
473 }
474 }
475 eg_rout = slot->eg_rout;
476 eg_inc = 0;
477 eg_off = 0;
478 /* Instant attack */
479 if (reset && rate_hi == 0x0f)
480 {
481 eg_rout = 0x00;
482 }
483 /* Envelope off */
484 if ((slot->eg_rout & 0x1f8) == 0x1f8)
485 {
486 eg_off = 1;
487 }
488 if (slot->eg_gen != envelope_gen_num_attack && !reset && eg_off)
489 {
490 eg_rout = 0x1ff;
491 }
492 switch (slot->eg_gen)
493 {
494 case envelope_gen_num_attack:
495 if (!slot->eg_rout)
496 {
497 slot->eg_gen = envelope_gen_num_decay;
498 }
499 else if (slot->key && shift > 0 && rate_hi != 0x0f)
500 {
501 eg_inc = ~slot->eg_rout >> (4 - shift);
502 }
503 break;
504 case envelope_gen_num_decay:
505 if ((slot->eg_rout >> 4) == slot->reg_sl)
506 {
507 slot->eg_gen = envelope_gen_num_sustain;
508 }
509 else if (!eg_off && !reset && shift > 0)
510 {
511 eg_inc = 1 << (shift - 1);
512 }
513 break;
514 case envelope_gen_num_sustain:
515 case envelope_gen_num_release:
516 if (!eg_off && !reset && shift > 0)
517 {
518 eg_inc = 1 << (shift - 1);
519 }
520 break;
521 }
522 slot->eg_rout = (eg_rout + eg_inc) & 0x1ff;
523 /* Key off */
524 if (reset)
525 {
526 slot->eg_gen = envelope_gen_num_attack;
527 }
528 if (!slot->key)
529 {
530 slot->eg_gen = envelope_gen_num_release;
531 }
532 }
533
534 static void OPL3_EnvelopeKeyOn(opl3_slot *slot, uint8_t type)
535 {
536 slot->key |= type;
537 }
538
539 static void OPL3_EnvelopeKeyOff(opl3_slot *slot, uint8_t type)
540 {
541 slot->key &= ~type;
542 }
543
544 /*
545 Phase Generator
546 */
547
548 static void OPL3_PhaseGenerate(opl3_slot *slot)
549 {
550 opl3_chip *chip;
551 uint16_t f_num;
552 uint32_t basefreq;
553 uint8_t rm_xor, n_bit;
554 uint32_t noise;
555 uint16_t phase;
556
557 chip = slot->chip;
558 f_num = slot->channel->f_num;
559 if (slot->reg_vib)
560 {
561 int8_t range;
562 uint8_t vibpos;
563
564 range = (f_num >> 7) & 7;
565 vibpos = slot->chip->vibpos;
566
567 if (!(vibpos & 3))
568 {
569 range = 0;
570 }
571 else if (vibpos & 1)
572 {
573 range >>= 1;
574 }
575 range >>= slot->chip->vibshift;
576
577 if (vibpos & 4)
578 {
579 range = -range;
580 }
581 f_num += range;
582 }
583 basefreq = (f_num << slot->channel->block) >> 1;
584 phase = (uint16_t)(slot->pg_phase >> 9);
585 if (slot->pg_reset)
586 {
587 slot->pg_phase = 0;
588 }
589 slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;
590 /* Rhythm mode */
591 noise = chip->noise;
592 slot->pg_phase_out = phase;
593 if (slot->slot_num == 13) /* hh */
594 {
595 chip->rm_hh_bit2 = (phase >> 2) & 1;
596 chip->rm_hh_bit3 = (phase >> 3) & 1;
597 chip->rm_hh_bit7 = (phase >> 7) & 1;
598 chip->rm_hh_bit8 = (phase >> 8) & 1;
599 }
600 if (slot->slot_num == 17 && (chip->rhy & 0x20)) /* tc */
601 {
602 chip->rm_tc_bit3 = (phase >> 3) & 1;
603 chip->rm_tc_bit5 = (phase >> 5) & 1;
604 }
605 if (chip->rhy & 0x20)
606 {
607 rm_xor = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7)
608 | (chip->rm_hh_bit3 ^ chip->rm_tc_bit5)
609 | (chip->rm_tc_bit3 ^ chip->rm_tc_bit5);
610 switch (slot->slot_num)
611 {
612 case 13: /* hh */
613 slot->pg_phase_out = rm_xor << 9;
614 if (rm_xor ^ (noise & 1))
615 {
616 slot->pg_phase_out |= 0xd0;
617 }
618 else
619 {
620 slot->pg_phase_out |= 0x34;
621 }
622 break;
623 case 16: /* sd */
624 slot->pg_phase_out = (chip->rm_hh_bit8 << 9)
625 | ((chip->rm_hh_bit8 ^ (noise & 1)) << 8);
626 break;
627 case 17: /* tc */
628 slot->pg_phase_out = (rm_xor << 9) | 0x80;
629 break;
630 default:
631 break;
632 }
633 }
634 n_bit = ((noise >> 14) ^ noise) & 0x01;
635 chip->noise = (noise >> 1) | (n_bit << 22);
636 }
637
638 /*
639 Slot
640 */
641
642 static void OPL3_SlotWrite20(opl3_slot *slot, uint8_t data)
643 {
644 if ((data >> 7) & 0x01)
645 {
646 slot->trem = &slot->chip->tremolo;
647 }
648 else
649 {
650 slot->trem = (uint8_t*)&slot->chip->zeromod;
651 }
652 slot->reg_vib = (data >> 6) & 0x01;
653 slot->reg_type = (data >> 5) & 0x01;
654 slot->reg_ksr = (data >> 4) & 0x01;
655 slot->reg_mult = data & 0x0f;
656 }
657
658 static void OPL3_SlotWrite40(opl3_slot *slot, uint8_t data)
659 {
660 slot->reg_ksl = (data >> 6) & 0x03;
661 slot->reg_tl = data & 0x3f;
662 OPL3_EnvelopeUpdateKSL(slot);
663 }
664
665 static void OPL3_SlotWrite60(opl3_slot *slot, uint8_t data)
666 {
667 slot->reg_ar = (data >> 4) & 0x0f;
668 slot->reg_dr = data & 0x0f;
669 }
670
671 static void OPL3_SlotWrite80(opl3_slot *slot, uint8_t data)
672 {
673 slot->reg_sl = (data >> 4) & 0x0f;
674 if (slot->reg_sl == 0x0f)
675 {
676 slot->reg_sl = 0x1f;
677 }
678 slot->reg_rr = data & 0x0f;
679 }
680
681 static void OPL3_SlotWriteE0(opl3_slot *slot, uint8_t data)
682 {
683 slot->reg_wf = data & 0x07;
684 if (slot->chip->newm == 0x00)
685 {
686 slot->reg_wf &= 0x03;
687 }
688 }
689
690 static void OPL3_SlotGenerate(opl3_slot *slot)
691 {
692 slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out);
693 }
694
695 static void OPL3_SlotCalcFB(opl3_slot *slot)
696 {
697 if (slot->channel->fb != 0x00)
698 {
699 slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
700 }
701 else
702 {
703 slot->fbmod = 0;
704 }
705 slot->prout = slot->out;
706 }
707
708 /*
709 Channel
710 */
711
712 static void OPL3_ChannelSetupAlg(opl3_channel *channel);
713
714 static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, uint8_t data)
715 {
716 opl3_channel *channel6;
717 opl3_channel *channel7;
718 opl3_channel *channel8;
719 uint8_t chnum;
720
721 chip->rhy = data & 0x3f;
722 if (chip->rhy & 0x20)
723 {
724 channel6 = &chip->channel[6];
725 channel7 = &chip->channel[7];
726 channel8 = &chip->channel[8];
727 channel6->out[0] = &channel6->slots[1]->out;
728 channel6->out[1] = &channel6->slots[1]->out;
729 channel6->out[2] = &chip->zeromod;
730 channel6->out[3] = &chip->zeromod;
731 channel7->out[0] = &channel7->slots[0]->out;
732 channel7->out[1] = &channel7->slots[0]->out;
733 channel7->out[2] = &channel7->slots[1]->out;
734 channel7->out[3] = &channel7->slots[1]->out;
735 channel8->out[0] = &channel8->slots[0]->out;
736 channel8->out[1] = &channel8->slots[0]->out;
737 channel8->out[2] = &channel8->slots[1]->out;
738 channel8->out[3] = &channel8->slots[1]->out;
739 for (chnum = 6; chnum < 9; chnum++)
740 {
741 chip->channel[chnum].chtype = ch_drum;
742 }
743 OPL3_ChannelSetupAlg(channel6);
744 OPL3_ChannelSetupAlg(channel7);
745 OPL3_ChannelSetupAlg(channel8);
746 /* hh */
747 if (chip->rhy & 0x01)
748 {
749 OPL3_EnvelopeKeyOn(channel7->slots[0], egk_drum);
750 }
751 else
752 {
753 OPL3_EnvelopeKeyOff(channel7->slots[0], egk_drum);
754 }
755 /* tc */
756 if (chip->rhy & 0x02)
757 {
758 OPL3_EnvelopeKeyOn(channel8->slots[1], egk_drum);
759 }
760 else
761 {
762 OPL3_EnvelopeKeyOff(channel8->slots[1], egk_drum);
763 }
764 /* tom */
765 if (chip->rhy & 0x04)
766 {
767 OPL3_EnvelopeKeyOn(channel8->slots[0], egk_drum);
768 }
769 else
770 {
771 OPL3_EnvelopeKeyOff(channel8->slots[0], egk_drum);
772 }
773 /* sd */
774 if (chip->rhy & 0x08)
775 {
776 OPL3_EnvelopeKeyOn(channel7->slots[1], egk_drum);
777 }
778 else
779 {
780 OPL3_EnvelopeKeyOff(channel7->slots[1], egk_drum);
781 }
782 /* bd */
783 if (chip->rhy & 0x10)
784 {
785 OPL3_EnvelopeKeyOn(channel6->slots[0], egk_drum);
786 OPL3_EnvelopeKeyOn(channel6->slots[1], egk_drum);
787 }
788 else
789 {
790 OPL3_EnvelopeKeyOff(channel6->slots[0], egk_drum);
791 OPL3_EnvelopeKeyOff(channel6->slots[1], egk_drum);
792 }
793 }
794 else
795 {
796 for (chnum = 6; chnum < 9; chnum++)
797 {
798 chip->channel[chnum].chtype = ch_2op;
799 OPL3_ChannelSetupAlg(&chip->channel[chnum]);
800 OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[0], egk_drum);
801 OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[1], egk_drum);
802 }
803 }
804 }
805
806 static void OPL3_ChannelWriteA0(opl3_channel *channel, uint8_t data)
807 {
808 if (channel->chip->newm && channel->chtype == ch_4op2)
809 {
810 return;
811 }
812 channel->f_num = (channel->f_num & 0x300) | data;
813 channel->ksv = (channel->block << 1)
814 | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
815 OPL3_EnvelopeUpdateKSL(channel->slots[0]);
816 OPL3_EnvelopeUpdateKSL(channel->slots[1]);
817 if (channel->chip->newm && channel->chtype == ch_4op)
818 {
819 channel->pair->f_num = channel->f_num;
820 channel->pair->ksv = channel->ksv;
821 OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
822 OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
823 }
824 }
825
826 static void OPL3_ChannelWriteB0(opl3_channel *channel, uint8_t data)
827 {
828 if (channel->chip->newm && channel->chtype == ch_4op2)
829 {
830 return;
831 }
832 channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8);
833 channel->block = (data >> 2) & 0x07;
834 channel->ksv = (channel->block << 1)
835 | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
836 OPL3_EnvelopeUpdateKSL(channel->slots[0]);
837 OPL3_EnvelopeUpdateKSL(channel->slots[1]);
838 if (channel->chip->newm && channel->chtype == ch_4op)
839 {
840 channel->pair->f_num = channel->f_num;
841 channel->pair->block = channel->block;
842 channel->pair->ksv = channel->ksv;
843 OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
844 OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
845 }
846 }
847
848 static void OPL3_ChannelSetupAlg(opl3_channel *channel)
849 {
850 if (channel->chtype == ch_drum)
851 {
852 if (channel->ch_num == 7 || channel->ch_num == 8)
853 {
854 channel->slots[0]->mod = &channel->chip->zeromod;
855 channel->slots[1]->mod = &channel->chip->zeromod;
856 return;
857 }
858 switch (channel->alg & 0x01)
859 {
860 case 0x00:
861 channel->slots[0]->mod = &channel->slots[0]->fbmod;
862 channel->slots[1]->mod = &channel->slots[0]->out;
863 break;
864 case 0x01:
865 channel->slots[0]->mod = &channel->slots[0]->fbmod;
866 channel->slots[1]->mod = &channel->chip->zeromod;
867 break;
868 }
869 return;
870 }
871 if (channel->alg & 0x08)
872 {
873 return;
874 }
875 if (channel->alg & 0x04)
876 {
877 channel->pair->out[0] = &channel->chip->zeromod;
878 channel->pair->out[1] = &channel->chip->zeromod;
879 channel->pair->out[2] = &channel->chip->zeromod;
880 channel->pair->out[3] = &channel->chip->zeromod;
881 switch (channel->alg & 0x03)
882 {
883 case 0x00:
884 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
885 channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
886 channel->slots[0]->mod = &channel->pair->slots[1]->out;
887 channel->slots[1]->mod = &channel->slots[0]->out;
888 channel->out[0] = &channel->slots[1]->out;
889 channel->out[1] = &channel->chip->zeromod;
890 channel->out[2] = &channel->chip->zeromod;
891 channel->out[3] = &channel->chip->zeromod;
892 break;
893 case 0x01:
894 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
895 channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
896 channel->slots[0]->mod = &channel->chip->zeromod;
897 channel->slots[1]->mod = &channel->slots[0]->out;
898 channel->out[0] = &channel->pair->slots[1]->out;
899 channel->out[1] = &channel->slots[1]->out;
900 channel->out[2] = &channel->chip->zeromod;
901 channel->out[3] = &channel->chip->zeromod;
902 break;
903 case 0x02:
904 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
905 channel->pair->slots[1]->mod = &channel->chip->zeromod;
906 channel->slots[0]->mod = &channel->pair->slots[1]->out;
907 channel->slots[1]->mod = &channel->slots[0]->out;
908 channel->out[0] = &channel->pair->slots[0]->out;
909 channel->out[1] = &channel->slots[1]->out;
910 channel->out[2] = &channel->chip->zeromod;
911 channel->out[3] = &channel->chip->zeromod;
912 break;
913 case 0x03:
914 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
915 channel->pair->slots[1]->mod = &channel->chip->zeromod;
916 channel->slots[0]->mod = &channel->pair->slots[1]->out;
917 channel->slots[1]->mod = &channel->chip->zeromod;
918 channel->out[0] = &channel->pair->slots[0]->out;
919 channel->out[1] = &channel->slots[0]->out;
920 channel->out[2] = &channel->slots[1]->out;
921 channel->out[3] = &channel->chip->zeromod;
922 break;
923 }
924 }
925 else
926 {
927 switch (channel->alg & 0x01)
928 {
929 case 0x00:
930 channel->slots[0]->mod = &channel->slots[0]->fbmod;
931 channel->slots[1]->mod = &channel->slots[0]->out;
932 channel->out[0] = &channel->slots[1]->out;
933 channel->out[1] = &channel->chip->zeromod;
934 channel->out[2] = &channel->chip->zeromod;
935 channel->out[3] = &channel->chip->zeromod;
936 break;
937 case 0x01:
938 channel->slots[0]->mod = &channel->slots[0]->fbmod;
939 channel->slots[1]->mod = &channel->chip->zeromod;
940 channel->out[0] = &channel->slots[0]->out;
941 channel->out[1] = &channel->slots[1]->out;
942 channel->out[2] = &channel->chip->zeromod;
943 channel->out[3] = &channel->chip->zeromod;
944 break;
945 }
946 }
947 }
948
949 static void OPL3_ChannelUpdateAlg(opl3_channel *channel)
950 {
951 channel->alg = channel->con;
952 if (channel->chip->newm)
953 {
954 if (channel->chtype == ch_4op)
955 {
956 channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
957 channel->alg = 0x08;
958 OPL3_ChannelSetupAlg(channel->pair);
959 }
960 else if (channel->chtype == ch_4op2)
961 {
962 channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
963 channel->pair->alg = 0x08;
964 OPL3_ChannelSetupAlg(channel);
965 }
966 else
967 {
968 OPL3_ChannelSetupAlg(channel);
969 }
970 }
971 else
972 {
973 OPL3_ChannelSetupAlg(channel);
974 }
975 }
976
977 static void OPL3_ChannelWriteC0(opl3_channel *channel, uint8_t data)
978 {
979 channel->fb = (data & 0x0e) >> 1;
980 channel->con = data & 0x01;
981 OPL3_ChannelUpdateAlg(channel);
982 if (channel->chip->newm)
983 {
984 channel->cha = ((data >> 4) & 0x01) ? ~0 : 0;
985 channel->chb = ((data >> 5) & 0x01) ? ~0 : 0;
986 channel->chc = ((data >> 6) & 0x01) ? ~0 : 0;
987 channel->chd = ((data >> 7) & 0x01) ? ~0 : 0;
988 }
989 else
990 {
991 channel->cha = channel->chb = (uint16_t)~0;
992 // TODO: Verify on real chip if DAC2 output is disabled in compat mode
993 channel->chc = channel->chd = 0;
994 }
995 #if OPL_ENABLE_STEREOEXT
996 if (!channel->chip->stereoext)
997 {
998 channel->leftpan = channel->cha << 16;
999 channel->rightpan = channel->chb << 16;
1000 }
1001 #endif
1002 }
1003
1004 #if OPL_ENABLE_STEREOEXT
1005 static void OPL3_ChannelWriteD0(opl3_channel* channel, uint8_t data)
1006 {
1007 if (channel->chip->stereoext)
1008 {
1009 channel->leftpan = panpot_lut[data ^ 0xff];
1010 channel->rightpan = panpot_lut[data];
1011 }
1012 }
1013 #endif
1014
1015 static void OPL3_ChannelKeyOn(opl3_channel *channel)
1016 {
1017 if (channel->chip->newm)
1018 {
1019 if (channel->chtype == ch_4op)
1020 {
1021 OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
1022 OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
1023 OPL3_EnvelopeKeyOn(channel->pair->slots[0], egk_norm);
1024 OPL3_EnvelopeKeyOn(channel->pair->slots[1], egk_norm);
1025 }
1026 else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
1027 {
1028 OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
1029 OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
1030 }
1031 }
1032 else
1033 {
1034 OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
1035 OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
1036 }
1037 }
1038
1039 static void OPL3_ChannelKeyOff(opl3_channel *channel)
1040 {
1041 if (channel->chip->newm)
1042 {
1043 if (channel->chtype == ch_4op)
1044 {
1045 OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
1046 OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
1047 OPL3_EnvelopeKeyOff(channel->pair->slots[0], egk_norm);
1048 OPL3_EnvelopeKeyOff(channel->pair->slots[1], egk_norm);
1049 }
1050 else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
1051 {
1052 OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
1053 OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
1054 }
1055 }
1056 else
1057 {
1058 OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
1059 OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
1060 }
1061 }
1062
1063 static void OPL3_ChannelSet4Op(opl3_chip *chip, uint8_t data)
1064 {
1065 uint8_t bit;
1066 uint8_t chnum;
1067 for (bit = 0; bit < 6; bit++)
1068 {
1069 chnum = bit;
1070 if (bit >= 3)
1071 {
1072 chnum += 9 - 3;
1073 }
1074 if ((data >> bit) & 0x01)
1075 {
1076 chip->channel[chnum].chtype = ch_4op;
1077 chip->channel[chnum + 3].chtype = ch_4op2;
1078 OPL3_ChannelUpdateAlg(&chip->channel[chnum]);
1079 }
1080 else
1081 {
1082 chip->channel[chnum].chtype = ch_2op;
1083 chip->channel[chnum + 3].chtype = ch_2op;
1084 OPL3_ChannelUpdateAlg(&chip->channel[chnum]);
1085 OPL3_ChannelUpdateAlg(&chip->channel[chnum+3]);
1086 }
1087 }
1088 }
1089
1090 static int16_t OPL3_ClipSample(int32_t sample)
1091 {
1092 if (sample > 32767)
1093 {
1094 sample = 32767;
1095 }
1096 else if (sample < -32768)
1097 {
1098 sample = -32768;
1099 }
1100 return (int16_t)sample;
1101 }
1102
1103 static void OPL3_ProcessSlot(opl3_slot *slot)
1104 {
1105 OPL3_SlotCalcFB(slot);
1106 OPL3_EnvelopeCalc(slot);
1107 OPL3_PhaseGenerate(slot);
1108 OPL3_SlotGenerate(slot);
1109 }
1110
1111 inline void OPL3_Generate4Ch(opl3_chip *chip, int16_t *buf4)
1112 {
1113 opl3_channel *channel;
1114 opl3_writebuf *writebuf;
1115 int16_t **out;
1116 int32_t mix[2];
1117 uint8_t ii;
1118 int16_t accm;
1119 uint8_t shift = 0;
1120
1121 buf4[1] = OPL3_ClipSample(chip->mixbuff[1]);
1122 buf4[3] = OPL3_ClipSample(chip->mixbuff[3]);
1123
1124 #if OPL_QUIRK_CHANNELSAMPLEDELAY
1125 for (ii = 0; ii < 15; ii++)
1126 #else
1127 for (ii = 0; ii < 36; ii++)
1128 #endif
1129 {
1130 OPL3_ProcessSlot(&chip->slot[ii]);
1131 }
1132
1133 mix[0] = mix[1] = 0;
1134 for (ii = 0; ii < 18; ii++)
1135 {
1136 channel = &chip->channel[ii];
1137 if (channel->muted) continue;
1138 out = channel->out;
1139 accm = *out[0] + *out[1] + *out[2] + *out[3];
1140 #if OPL_ENABLE_STEREOEXT
1141 mix[0] += (int16_t)((accm * channel->leftpan) >> 16);
1142 #else
1143 mix[0] += (int16_t)(accm & channel->cha);
1144 #endif
1145 mix[1] += (int16_t)(accm & channel->chc);
1146 }
1147 chip->mixbuff[0] = mix[0];
1148 chip->mixbuff[2] = mix[1];
1149
1150 #if OPL_QUIRK_CHANNELSAMPLEDELAY
1151 for (ii = 15; ii < 18; ii++)
1152 {
1153 OPL3_ProcessSlot(&chip->slot[ii]);
1154 }
1155 #endif
1156
1157 buf4[0] = OPL3_ClipSample(chip->mixbuff[0]);
1158 buf4[2] = OPL3_ClipSample(chip->mixbuff[2]);
1159
1160 #if OPL_QUIRK_CHANNELSAMPLEDELAY
1161 for (ii = 18; ii < 33; ii++)
1162 {
1163 OPL3_ProcessSlot(&chip->slot[ii]);
1164 }
1165 #endif
1166
1167 mix[0] = mix[1] = 0;
1168 for (ii = 0; ii < 18; ii++)
1169 {
1170 channel = &chip->channel[ii];
1171 if (channel->muted) continue;
1172 out = channel->out;
1173 accm = *out[0] + *out[1] + *out[2] + *out[3];
1174 #if OPL_ENABLE_STEREOEXT
1175 mix[0] += (int16_t)((accm * channel->rightpan) >> 16);
1176 #else
1177 mix[0] += (int16_t)(accm & channel->chb);
1178 #endif
1179 mix[1] += (int16_t)(accm & channel->chd);
1180 }
1181 chip->mixbuff[1] = mix[0];
1182 chip->mixbuff[3] = mix[1];
1183
1184 #if OPL_QUIRK_CHANNELSAMPLEDELAY
1185 for (ii = 33; ii < 36; ii++)
1186 {
1187 OPL3_ProcessSlot(&chip->slot[ii]);
1188 }
1189 #endif
1190
1191 if ((chip->timer & 0x3f) == 0x3f)
1192 {
1193 chip->tremolopos = (chip->tremolopos + 1) % 210;
1194 }
1195 if (chip->tremolopos < 105)
1196 {
1197 chip->tremolo = chip->tremolopos >> chip->tremoloshift;
1198 }
1199 else
1200 {
1201 chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
1202 }
1203
1204 if ((chip->timer & 0x3ff) == 0x3ff)
1205 {
1206 chip->vibpos = (chip->vibpos + 1) & 7;
1207 }
1208
1209 chip->timer++;
1210
1211 chip->eg_add = 0;
1212 if (chip->eg_timer)
1213 {
1214 while (shift < 36 && ((chip->eg_timer >> shift) & 1) == 0)
1215 {
1216 shift++;
1217 }
1218 if (shift > 12)
1219 {
1220 chip->eg_add = 0;
1221 }
1222 else
1223 {
1224 chip->eg_add = shift + 1;
1225 }
1226 }
1227
1228 if (chip->eg_timerrem || chip->eg_state)
1229 {
1230 if (chip->eg_timer == 0xfffffffff)
1231 {
1232 chip->eg_timer = 0;
1233 chip->eg_timerrem = 1;
1234 }
1235 else
1236 {
1237 chip->eg_timer++;
1238 chip->eg_timerrem = 0;
1239 }
1240 }
1241
1242 chip->eg_state ^= 1;
1243
1244 while ((writebuf = &chip->writebuf[chip->writebuf_cur]), writebuf->time <= chip->writebuf_samplecnt)
1245 {
1246 if (!(writebuf->reg & 0x200))
1247 {
1248 break;
1249 }
1250 writebuf->reg &= 0x1ff;
1251 OPL3_WriteReg(chip, writebuf->reg, writebuf->data);
1252 chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
1253 }
1254 chip->writebuf_samplecnt++;
1255 }
1256
1257 void OPL3_Generate(opl3_chip *chip, int16_t *buf)
1258 {
1259 int16_t samples[4];
1260 OPL3_Generate4Ch(chip, samples);
1261 buf[0] = samples[0];
1262 buf[1] = samples[1];
1263 }
1264
1265 void OPL3_Generate4ChResampled(opl3_chip *chip, int16_t *buf4)
1266 {
1267 while (chip->samplecnt >= chip->rateratio)
1268 {
1269 chip->oldsamples[0] = chip->samples[0];
1270 chip->oldsamples[1] = chip->samples[1];
1271 chip->oldsamples[2] = chip->samples[2];
1272 chip->oldsamples[3] = chip->samples[3];
1273 OPL3_Generate4Ch(chip, chip->samples);
1274 chip->samplecnt -= chip->rateratio;
1275 }
1276 buf4[0] = (int16_t)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
1277 + chip->samples[0] * chip->samplecnt) / chip->rateratio);
1278 buf4[1] = (int16_t)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
1279 + chip->samples[1] * chip->samplecnt) / chip->rateratio);
1280 buf4[2] = (int16_t)((chip->oldsamples[2] * (chip->rateratio - chip->samplecnt)
1281 + chip->samples[2] * chip->samplecnt) / chip->rateratio);
1282 buf4[3] = (int16_t)((chip->oldsamples[3] * (chip->rateratio - chip->samplecnt)
1283 + chip->samples[3] * chip->samplecnt) / chip->rateratio);
1284 chip->samplecnt += 1 << RSM_FRAC;
1285 }
1286
1287 void OPL3_GenerateResampled(opl3_chip *chip, int16_t *buf)
1288 {
1289 int16_t samples[4];
1290 OPL3_Generate4ChResampled(chip, samples);
1291 buf[0] = samples[0];
1292 buf[1] = samples[1];
1293 }
1294
1295 void OPL3_Reset(opl3_chip *chip, uint32_t samplerate)
1296 {
1297 opl3_slot *slot;
1298 opl3_channel *channel;
1299 uint8_t slotnum;
1300 uint8_t channum;
1301 uint8_t local_ch_slot;
1302
1303 memset(chip, 0, sizeof(opl3_chip));
1304 for (slotnum = 0; slotnum < 36; slotnum++)
1305 {
1306 slot = &chip->slot[slotnum];
1307 slot->chip = chip;
1308 slot->mod = &chip->zeromod;
1309 slot->eg_rout = 0x1ff;
1310 slot->eg_out = 0x1ff;
1311 slot->eg_gen = envelope_gen_num_release;
1312 slot->trem = (uint8_t*)&chip->zeromod;
1313 slot->slot_num = slotnum;
1314 }
1315 for (channum = 0; channum < 18; channum++)
1316 {
1317 channel = &chip->channel[channum];
1318 local_ch_slot = ch_slot[channum];
1319 channel->slots[0] = &chip->slot[local_ch_slot];
1320 channel->slots[1] = &chip->slot[local_ch_slot + 3];
1321 chip->slot[local_ch_slot].channel = channel;
1322 chip->slot[local_ch_slot + 3].channel = channel;
1323 if ((channum % 9) < 3)
1324 {
1325 channel->pair = &chip->channel[channum + 3];
1326 }
1327 else if ((channum % 9) < 6)
1328 {
1329 channel->pair = &chip->channel[channum - 3];
1330 }
1331 channel->chip = chip;
1332 channel->out[0] = &chip->zeromod;
1333 channel->out[1] = &chip->zeromod;
1334 channel->out[2] = &chip->zeromod;
1335 channel->out[3] = &chip->zeromod;
1336 channel->muted = 0;
1337 channel->chtype = ch_2op;
1338 channel->cha = 0xffff;
1339 channel->chb = 0xffff;
1340 #if OPL_ENABLE_STEREOEXT
1341 channel->leftpan = 0x10000;
1342 channel->rightpan = 0x10000;
1343 #endif
1344 channel->ch_num = channum;
1345 OPL3_ChannelSetupAlg(channel);
1346 }
1347 chip->noise = 1;
1348 chip->rateratio = (samplerate << RSM_FRAC) / 49716;
1349 chip->tremoloshift = 4;
1350 chip->vibshift = 1;
1351
1352 #if OPL_ENABLE_STEREOEXT
1353 if (!panpot_lut_build)
1354 {
1355 int32_t i;
1356 for (i = 0; i < 256; i++)
1357 {
1358 panpot_lut[i] = OPL_SIN(i);
1359 }
1360 panpot_lut_build = 1;
1361 }
1362 #endif
1363 }
1364
1365 void OPL3_Resample(opl3_chip *chip, uint32_t samplerate)
1366 {
1367 chip->rateratio = (samplerate << RSM_FRAC) / 49716;
1368 }
1369
1370 void OPL3_WriteReg(opl3_chip *chip, uint16_t reg, uint8_t v)
1371 {
1372 uint8_t high = (reg >> 8) & 0x01;
1373 uint8_t regm = reg & 0xff;
1374 switch (regm & 0xf0)
1375 {
1376 case 0x00:
1377 if (high)
1378 {
1379 switch (regm & 0x0f)
1380 {
1381 case 0x04:
1382 OPL3_ChannelSet4Op(chip, v);
1383 break;
1384 case 0x05:
1385 chip->newm = v & 0x01;
1386 #if OPL_ENABLE_STEREOEXT
1387 chip->stereoext = (v >> 1) & 0x01;
1388 #endif
1389 break;
1390 }
1391 }
1392 else
1393 {
1394 switch (regm & 0x0f)
1395 {
1396 case 0x08:
1397 chip->nts = (v >> 6) & 0x01;
1398 break;
1399 }
1400 }
1401 break;
1402 case 0x20:
1403 case 0x30:
1404 if (ad_slot[regm & 0x1f] >= 0)
1405 {
1406 OPL3_SlotWrite20(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1407 }
1408 break;
1409 case 0x40:
1410 case 0x50:
1411 if (ad_slot[regm & 0x1f] >= 0)
1412 {
1413 OPL3_SlotWrite40(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1414 }
1415 break;
1416 case 0x60:
1417 case 0x70:
1418 if (ad_slot[regm & 0x1f] >= 0)
1419 {
1420 OPL3_SlotWrite60(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1421 }
1422 break;
1423 case 0x80:
1424 case 0x90:
1425 if (ad_slot[regm & 0x1f] >= 0)
1426 {
1427 OPL3_SlotWrite80(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1428 }
1429 break;
1430 case 0xe0:
1431 case 0xf0:
1432 if (ad_slot[regm & 0x1f] >= 0)
1433 {
1434 OPL3_SlotWriteE0(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1435 }
1436 break;
1437 case 0xa0:
1438 if ((regm & 0x0f) < 9)
1439 {
1440 OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v);
1441 }
1442 break;
1443 case 0xb0:
1444 if (regm == 0xbd && !high)
1445 {
1446 chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2;
1447 chip->vibshift = ((v >> 6) & 0x01) ^ 1;
1448 OPL3_ChannelUpdateRhythm(chip, v);
1449 }
1450 else if ((regm & 0x0f) < 9)
1451 {
1452 OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v);
1453 if (v & 0x20)
1454 {
1455 OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]);
1456 }
1457 else
1458 {
1459 OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]);
1460 }
1461 }
1462 break;
1463 case 0xc0:
1464 if ((regm & 0x0f) < 9)
1465 {
1466 OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v);
1467 }
1468 break;
1469 #if OPL_ENABLE_STEREOEXT
1470 case 0xd0:
1471 if ((regm & 0x0f) < 9)
1472 {
1473 OPL3_ChannelWriteD0(&chip->channel[9 * high + (regm & 0x0f)], v);
1474 }
1475 break;
1476 #endif
1477 }
1478 }
1479
1480 void OPL3_WriteRegBuffered(opl3_chip *chip, uint16_t reg, uint8_t v)
1481 {
1482 uint64_t time1, time2;
1483 opl3_writebuf *writebuf;
1484 uint32_t writebuf_last;
1485
1486 writebuf_last = chip->writebuf_last;
1487 writebuf = &chip->writebuf[writebuf_last];
1488
1489 if (writebuf->reg & 0x200)
1490 {
1491 OPL3_WriteReg(chip, writebuf->reg & 0x1ff, writebuf->data);
1492
1493 chip->writebuf_cur = (writebuf_last + 1) % OPL_WRITEBUF_SIZE;
1494 chip->writebuf_samplecnt = writebuf->time;
1495 }
1496
1497 writebuf->reg = reg | 0x200;
1498 writebuf->data = v;
1499 time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY;
1500 time2 = chip->writebuf_samplecnt;
1501
1502 if (time1 < time2)
1503 {
1504 time1 = time2;
1505 }
1506
1507 writebuf->time = time1;
1508 chip->writebuf_lasttime = time1;
1509 chip->writebuf_last = (writebuf_last + 1) % OPL_WRITEBUF_SIZE;
1510 }
1511
1512 void OPL3_Generate4ChStream(opl3_chip *chip, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples)
1513 {
1514 uint_fast32_t i;
1515 int16_t samples[4];
1516
1517 for(i = 0; i < numsamples; i++)
1518 {
1519 OPL3_Generate4ChResampled(chip, samples);
1520 sndptr1[0] = samples[0];
1521 sndptr1[1] = samples[1];
1522 sndptr2[0] = samples[2];
1523 sndptr2[1] = samples[3];
1524 sndptr1 += 2;
1525 sndptr2 += 2;
1526 }
1527 }
1528
1529 void OPL3_GenerateStream(opl3_chip *chip, int16_t *sndptr, uint32_t numsamples)
1530 {
1531 uint_fast32_t i;
1532
1533 for(i = 0; i < numsamples; i++)
1534 {
1535 OPL3_GenerateResampled(chip, sndptr);
1536 sndptr += 2;
1537 }
1538 }