1 /*
2 * Copyright (C) 1999/2000 Tatsuyuki Satoh
3 * Copyright (C) 2001-2016 The ScummVM project
4 * Copyright (C) 2002/2016 The Exult Team
5 * Copyright (C) 2003 The Pentagram Team
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * LGPL licensed version of MAMEs fmopl (V0.37a modified) by
22 * Tatsuyuki Satoh. Included from LGPL'ed AdPlug.
23 *
24 */
25
26 #include "pent_include.h"
27
28 #ifdef USE_FMOPL_MIDI
29
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstring>
33 #include <cstdarg>
34 #include <cmath>
35 #include <iostream>
36 #include "fmopl.h"
37 #include "array_size.h"
38
39 #ifndef M_PI
40 #define M_PI 3.14159265358979323846
41 #endif
42
43 #ifndef ARRAYSIZE
44 #define ARRAYSIZE(x) (array_size(x))
45 #endif
46
47 namespace FMOpl_Pentagram {
48
49 /* -------------------- preliminary define section --------------------- */
50 /* attack/decay rate time rate */
51 #define OPL_ARRATE 141280 /* RATE 4 = 2826.24ms @ 3.6MHz */
52 #define OPL_DRRATE 1956000 /* RATE 4 = 39280.64ms @ 3.6MHz */
53
54 #define FREQ_BITS 24 /* frequency turn */
55
56 /* counter bits = 20 , octerve 7 */
57 #define FREQ_RATE (1<<(FREQ_BITS-20))
58 #define TL_BITS (FREQ_BITS+2)
59
60 /* final output shift , limit minimum and maximum */
61 #define OPL_OUTSB (TL_BITS+3-16) /* OPL output final shift 16bit */
62 #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
63 #define OPL_MINOUT (-(0x8000<<OPL_OUTSB))
64
65 /* -------------------- quality selection --------------------- */
66
67 /* sinwave entries */
68 /* used static memory = SIN_ENT * 4 (byte) */
69 #define SIN_ENT_SHIFT 11
70 #define SIN_ENT (1<<SIN_ENT_SHIFT)
71
72 /* output level entries (envelope,sinwave) */
73 /* envelope counter lower bits */
74 uint32 ENV_BITS;
75 /* envelope output entries */
76 uint32 EG_ENT;
77
78 /* used dynamic memory = EG_ENT*4*4(byte)or EG_ENT*6*4(byte) */
79 /* used static memory = EG_ENT*4 (byte) */
80 uint32 EG_OFF; /* OFF */
81 uint32 EG_DED;
82 uint32 EG_DST; /* DECAY START */
83 uint32 EG_AED;
84 #define EG_AST 0 /* ATTACK START */
85
86 #define EG_STEP (96.0/EG_ENT) /* OPL is 0.1875 dB step */
87
88 /* LFO table entries */
89 #define VIB_ENT 512
90 #define VIB_SHIFT (32-9)
91 #define AMS_ENT 512
92 #define AMS_SHIFT (32-9)
93
94 #define VIB_RATE_SHIFT 8
95 #define VIB_RATE (1<<VIB_RATE_SHIFT)
96
97 /* -------------------- local defines , macros --------------------- */
98
99 /* register number to channel number , slot offset */
100 #define SLOT1 0
101 #define SLOT2 1
102
103 /* envelope phase */
104 #define ENV_MOD_RR 0x00
105 #define ENV_MOD_DR 0x01
106 #define ENV_MOD_AR 0x02
107
108 /* -------------------- tables --------------------- */
109 static const int slot_array[32] = {
110 0, 2, 4, 1, 3, 5,-1,-1,
111 6, 8,10, 7, 9,11,-1,-1,
112 12,14,16,13,15,17,-1,-1,
113 -1,-1,-1,-1,-1,-1,-1,-1
114 };
115
116 static uint32 KSL_TABLE[8 * 16];
117
118 static const double KSL_TABLE_SEED[8 * 16] = {
119 /* OCT 0 */
120 0.000, 0.000, 0.000, 0.000,
121 0.000, 0.000, 0.000, 0.000,
122 0.000, 0.000, 0.000, 0.000,
123 0.000, 0.000, 0.000, 0.000,
124 /* OCT 1 */
125 0.000, 0.000, 0.000, 0.000,
126 0.000, 0.000, 0.000, 0.000,
127 0.000, 0.750, 1.125, 1.500,
128 1.875, 2.250, 2.625, 3.000,
129 /* OCT 2 */
130 0.000, 0.000, 0.000, 0.000,
131 0.000, 1.125, 1.875, 2.625,
132 3.000, 3.750, 4.125, 4.500,
133 4.875, 5.250, 5.625, 6.000,
134 /* OCT 3 */
135 0.000, 0.000, 0.000, 1.875,
136 3.000, 4.125, 4.875, 5.625,
137 6.000, 6.750, 7.125, 7.500,
138 7.875, 8.250, 8.625, 9.000,
139 /* OCT 4 */
140 0.000, 0.000, 3.000, 4.875,
141 6.000, 7.125, 7.875, 8.625,
142 9.000, 9.750, 10.125, 10.500,
143 10.875, 11.250, 11.625, 12.000,
144 /* OCT 5 */
145 0.000, 3.000, 6.000, 7.875,
146 9.000, 10.125, 10.875, 11.625,
147 12.000, 12.750, 13.125, 13.500,
148 13.875, 14.250, 14.625, 15.000,
149 /* OCT 6 */
150 0.000, 6.000, 9.000, 10.875,
151 12.000, 13.125, 13.875, 14.625,
152 15.000, 15.750, 16.125, 16.500,
153 16.875, 17.250, 17.625, 18.000,
154 /* OCT 7 */
155 0.000, 9.000, 12.000, 13.875,
156 15.000, 16.125, 16.875, 17.625,
157 18.000, 18.750, 19.125, 19.500,
158 19.875, 20.250, 20.625, 21.000
159 };
160
161 /* sustain level table (3db per step) */
162 /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
163
164 static int SL_TABLE[16];
165
166 static const uint32 SL_TABLE_SEED[16] = {
167 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 31
168 };
169
170 #define TL_MAX (EG_ENT * 2) /* limit(tl + ksr + envelope) + sinwave */
171 /* TotalLevel : 48 24 12 6 3 1.5 0.75 (dB) */
172 /* TL_TABLE[ 0 to TL_MAX ] : plus section */
173 /* TL_TABLE[ TL_MAX to TL_MAX+TL_MAX-1 ] : minus section */
174 static int *TL_TABLE;
175
176 /* pointers to TL_TABLE with sinwave output offset */
177 static int **SIN_TABLE;
178
179 /* LFO table */
180 static int *AMS_TABLE;
181 static int *VIB_TABLE;
182
183 /* envelope output curve table */
184 /* attack + decay + OFF */
185 //static int ENV_CURVE[2*EG_ENT+1];
186 //static int ENV_CURVE[2 * 4096 + 1]; // to keep it static ...
187 static int *ENV_CURVE;
188
189
190 /* multiple table */
191 #define ML(a) static_cast<uint32>((a) * 2)
192 static const uint32 MUL_TABLE[16]= {
193 /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 */
194 ML(0.50), ML(1.00), ML(2.00), ML(3.00), ML(4.00), ML(5.00), ML(6.00), ML(7.00),
195 ML(8.00), ML(9.00), ML(10.00), ML(10.00),ML(12.00),ML(12.00),ML(15.00),ML(15.00)
196 };
197 #undef ML
198
199 /* dummy attack / decay rate ( when rate == 0 ) */
200 static int RATE_0[16]=
201 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
202
203 /* -------------------- static state --------------------- */
204
205 /* lock level of common table */
206 static int num_lock = 0;
207
208 /* work table */
209 static void *cur_chip = nullptr; /* current chip point */
210 /* currenct chip state */
211 /* static OPLSAMPLE *bufL,*bufR; */
212 static OPL_CH *S_CH;
213 static OPL_CH *E_CH;
214 OPL_SLOT *SLOT7_1, *SLOT7_2, *SLOT8_1, *SLOT8_2;
215
216 static int outd[1];
217 static int ams;
218 static int vib;
219 int *ams_table;
220 int *vib_table;
221 static int amsIncr;
222 static int vibIncr;
223 static int feedback2; /* connect for SLOT 2 */
224
225 /* --------------------- subroutines --------------------- */
226
CLIP(int val,int min,int max)227 inline int CLIP( int val, int min, int max) {
228 if (val > max)
229 val = max;
230 else if (val < min)
231 val = min;
232
233 return val;
234 }
235
236 /* --------------------- rebuild tables ------------------- */
237
238 #define SC_KSL(mydb) (static_cast<uint32>((mydb) / (EG_STEP / 2)))
239 #define SC_SL(db) (static_cast<int>((db) * ((3 / EG_STEP) * (1 << ENV_BITS))) + EG_DST)
240
OPLBuildTables(uint32 ENV_BITS_PARAM,uint32 EG_ENT_PARAM)241 void OPLBuildTables(uint32 ENV_BITS_PARAM, uint32 EG_ENT_PARAM) {
242 ENV_BITS = ENV_BITS_PARAM;
243 EG_ENT = EG_ENT_PARAM;
244 EG_OFF = ((2u * EG_ENT)<<ENV_BITS); /* OFF */
245 EG_DED = EG_OFF;
246 EG_DST = (EG_ENT << ENV_BITS); /* DECAY START */
247 EG_AED = EG_DST;
248 //EG_STEP = (96.0/EG_ENT);
249
250 for (unsigned i = 0; i < ARRAYSIZE(KSL_TABLE_SEED); i++)
251 KSL_TABLE[i] = SC_KSL(KSL_TABLE_SEED[i]);
252
253 for (unsigned i = 0; i < ARRAYSIZE(SL_TABLE_SEED); i++)
254 SL_TABLE[i] = SC_SL(SL_TABLE_SEED[i]);
255 }
256
257 #undef SC_KSL
258 #undef SC_SL
259
260 /* --------------------- subroutines --------------------- */
261
262 /* status set and IRQ handling */
OPL_STATUS_SET(FM_OPL * OPL,int flag)263 inline void OPL_STATUS_SET(FM_OPL *OPL, int flag) {
264 /* set status flag */
265 OPL->status |= flag;
266 if (!(OPL->status & 0x80)) {
267 if (OPL->status & OPL->statusmask) { /* IRQ on */
268 OPL->status |= 0x80;
269 /* callback user interrupt handler (IRQ is OFF to ON) */
270 if (OPL->IRQHandler)
271 (OPL->IRQHandler)(OPL->IRQParam,1);
272 }
273 }
274 }
275
276 /* status reset and IRQ handling */
OPL_STATUS_RESET(FM_OPL * OPL,int flag)277 inline void OPL_STATUS_RESET(FM_OPL *OPL, int flag) {
278 /* reset status flag */
279 OPL->status &= ~flag;
280 if ((OPL->status & 0x80)) {
281 if (!(OPL->status & OPL->statusmask)) {
282 OPL->status &= 0x7f;
283 /* callback user interrupt handler (IRQ is ON to OFF) */
284 if (OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0);
285 }
286 }
287 }
288
289 /* IRQ mask set */
OPL_STATUSMASK_SET(FM_OPL * OPL,int flag)290 inline void OPL_STATUSMASK_SET(FM_OPL *OPL, int flag) {
291 OPL->statusmask = flag;
292 /* IRQ handling check */
293 OPL_STATUS_SET(OPL,0);
294 OPL_STATUS_RESET(OPL,0);
295 }
296
297 /* ----- key on ----- */
OPL_KEYON(OPL_SLOT * SLOT)298 inline void OPL_KEYON(OPL_SLOT *SLOT) {
299 /* sin wave restart */
300 SLOT->Cnt = 0;
301 /* set attack */
302 SLOT->evm = ENV_MOD_AR;
303 SLOT->evs = SLOT->evsa;
304 SLOT->evc = EG_AST;
305 SLOT->eve = EG_AED;
306 }
307
308 /* ----- key off ----- */
OPL_KEYOFF(OPL_SLOT * SLOT)309 inline void OPL_KEYOFF(OPL_SLOT *SLOT) {
310 if (SLOT->evm > ENV_MOD_RR) {
311 /* set envelope counter from envleope output */
312
313 // WORKAROUND: The Kyra engine does something very strange when
314 // starting a new song. For each channel:
315 //
316 // * The release rate is set to "fastest".
317 // * Any note is keyed off.
318 // * A very low-frequency note is keyed on.
319 //
320 // Usually, what happens next is that the real notes is keyed
321 // on immediately, in which case there's no problem.
322 //
323 // However, if the note is again keyed off (because the channel
324 // begins on a rest rather than a note), the envelope counter
325 // was moved from the very lowest point on the attack curve to
326 // the very highest point on the release curve.
327 //
328 // Again, this might not be a problem, if the release rate is
329 // still set to "fastest". But in many cases, it had already
330 // been increased. And, possibly because of inaccuracies in the
331 // envelope generator, that would cause the note to "fade out"
332 // for quite a long time.
333 //
334 // What we really need is a way to find the correct starting
335 // point for the envelope counter, and that may be what the
336 // commented-out line below is meant to do. For now, simply
337 // handle the pathological case.
338
339 if (SLOT->evm == ENV_MOD_AR && SLOT->evc == EG_AST)
340 SLOT->evc = EG_DED;
341 else if (!(SLOT->evc & EG_DST))
342 //SLOT->evc = (ENV_CURVE[SLOT->evc>>ENV_BITS]<<ENV_BITS) + EG_DST;
343 SLOT->evc = EG_DST;
344 SLOT->eve = EG_DED;
345 SLOT->evs = SLOT->evsr;
346 SLOT->evm = ENV_MOD_RR;
347 }
348 }
349
350 /* ---------- calcrate Envelope Generator & Phase Generator ---------- */
351
352 /* return : envelope output */
OPL_CALC_SLOT(OPL_SLOT * SLOT)353 inline uint32 OPL_CALC_SLOT(OPL_SLOT *SLOT) {
354 /* calcrate envelope generator */
355 if ((SLOT->evc += SLOT->evs) >= SLOT->eve) {
356 switch (SLOT->evm) {
357 case ENV_MOD_AR: /* ATTACK -> DECAY1 */
358 /* next DR */
359 SLOT->evm = ENV_MOD_DR;
360 SLOT->evc = EG_DST;
361 SLOT->eve = SLOT->SL;
362 SLOT->evs = SLOT->evsd;
363 break;
364 case ENV_MOD_DR: /* DECAY -> SL or RR */
365 SLOT->evc = SLOT->SL;
366 SLOT->eve = EG_DED;
367 if (SLOT->eg_typ) {
368 SLOT->evs = 0;
369 } else {
370 SLOT->evm = ENV_MOD_RR;
371 SLOT->evs = SLOT->evsr;
372 }
373 break;
374 case ENV_MOD_RR: /* RR -> OFF */
375 SLOT->evc = EG_OFF;
376 SLOT->eve = EG_OFF + 1;
377 SLOT->evs = 0;
378 break;
379 }
380 }
381 /* calcrate envelope */
382 return SLOT->TLL + ENV_CURVE[SLOT->evc>>ENV_BITS] + (SLOT->ams ? ams : 0);
383 }
384
385 /* set algorythm connection */
set_algorythm(OPL_CH * CH)386 static void set_algorythm(OPL_CH *CH) {
387 int *carrier = &outd[0];
388 CH->connect1 = CH->CON ? carrier : &feedback2;
389 CH->connect2 = carrier;
390 }
391
392 /* ---------- frequency counter for operater update ---------- */
CALC_FCSLOT(OPL_CH * CH,OPL_SLOT * SLOT)393 inline void CALC_FCSLOT(OPL_CH *CH, OPL_SLOT *SLOT) {
394 int ksr;
395
396 /* frequency step counter */
397 SLOT->Incr = CH->fc * SLOT->mul;
398 ksr = CH->kcode >> SLOT->KSR;
399
400 if (SLOT->ksr != ksr) {
401 SLOT->ksr = ksr;
402 /* attack , decay rate recalcration */
403 SLOT->evsa = SLOT->AR[ksr];
404 SLOT->evsd = SLOT->DR[ksr];
405 SLOT->evsr = SLOT->RR[ksr];
406 }
407 SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
408 }
409
410 /* set multi,am,vib,EG-TYP,KSR,mul */
set_mul(FM_OPL * OPL,int slot,int v)411 inline void set_mul(FM_OPL *OPL, int slot, int v) {
412 OPL_CH *CH = &OPL->P_CH[slot>>1];
413 OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
414
415 SLOT->mul = MUL_TABLE[v & 0x0f];
416 SLOT->KSR = (v & 0x10) ? 0 : 2;
417 SLOT->eg_typ = (v & 0x20) >> 5;
418 SLOT->vib = (v & 0x40);
419 SLOT->ams = (v & 0x80);
420 CALC_FCSLOT(CH, SLOT);
421 }
422
423 /* set ksl & tl */
set_ksl_tl(FM_OPL * OPL,int slot,int v)424 inline void set_ksl_tl(FM_OPL *OPL, int slot, int v) {
425 OPL_CH *CH = &OPL->P_CH[slot>>1];
426 OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
427 int ksl = v >> 6; /* 0 / 1.5 / 3 / 6 db/OCT */
428
429 SLOT->ksl = ksl ? 3-ksl : 31;
430 SLOT->TL = static_cast<int>((v & 0x3f) * (0.75 / EG_STEP)); /* 0.75db step */
431
432 if (!(OPL->mode & 0x80)) { /* not CSM latch total level */
433 SLOT->TLL = SLOT->TL + (CH->ksl_base >> SLOT->ksl);
434 }
435 }
436
437 /* set attack rate & decay rate */
set_ar_dr(FM_OPL * OPL,int slot,int v)438 inline void set_ar_dr(FM_OPL *OPL, int slot, int v) {
439 OPL_CH *CH = &OPL->P_CH[slot>>1];
440 OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
441 int ar = v >> 4;
442 int dr = v & 0x0f;
443
444 SLOT->AR = ar ? &OPL->AR_TABLE[ar << 2] : RATE_0;
445 SLOT->evsa = SLOT->AR[SLOT->ksr];
446 if (SLOT->evm == ENV_MOD_AR)
447 SLOT->evs = SLOT->evsa;
448
449 SLOT->DR = dr ? &OPL->DR_TABLE[dr<<2] : RATE_0;
450 SLOT->evsd = SLOT->DR[SLOT->ksr];
451 if (SLOT->evm == ENV_MOD_DR)
452 SLOT->evs = SLOT->evsd;
453 }
454
455 /* set sustain level & release rate */
set_sl_rr(FM_OPL * OPL,int slot,int v)456 inline void set_sl_rr(FM_OPL *OPL, int slot, int v) {
457 OPL_CH *CH = &OPL->P_CH[slot>>1];
458 OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
459 int sl = v >> 4;
460 int rr = v & 0x0f;
461
462 SLOT->SL = SL_TABLE[sl];
463 if (SLOT->evm == ENV_MOD_DR)
464 SLOT->eve = SLOT->SL;
465 SLOT->RR = &OPL->DR_TABLE[rr<<2];
466 SLOT->evsr = SLOT->RR[SLOT->ksr];
467 if (SLOT->evm == ENV_MOD_RR)
468 SLOT->evs = SLOT->evsr;
469 }
470
471 /* operator output calcrator */
472
473 #define OP_OUT(slot,env,con) slot->wavetable[(((slot)->Cnt + (con))>>(24-SIN_ENT_SHIFT)) & (SIN_ENT-1)][env]
474 /* ---------- calcrate one of channel ---------- */
OPL_CALC_CH(OPL_CH * CH)475 inline void OPL_CALC_CH(OPL_CH *CH) {
476 uint32 env_out;
477 OPL_SLOT *SLOT;
478
479 feedback2 = 0;
480 /* SLOT 1 */
481 SLOT = &CH->SLOT[SLOT1];
482 env_out=OPL_CALC_SLOT(SLOT);
483 if (env_out < (EG_ENT - 1)) {
484 /* PG */
485 if (SLOT->vib)
486 SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
487 else
488 SLOT->Cnt += SLOT->Incr;
489 /* connection */
490 if (CH->FB) {
491 int feedback1 = (CH->op1_out[0] + CH->op1_out[1]) >> CH->FB;
492 CH->op1_out[1] = CH->op1_out[0];
493 *CH->connect1 += CH->op1_out[0] = OP_OUT(SLOT, env_out, feedback1);
494 } else {
495 *CH->connect1 += OP_OUT(SLOT, env_out, 0);
496 }
497 } else {
498 CH->op1_out[1] = CH->op1_out[0];
499 CH->op1_out[0] = 0;
500 }
501 /* SLOT 2 */
502 SLOT = &CH->SLOT[SLOT2];
503 env_out=OPL_CALC_SLOT(SLOT);
504 if (env_out < (EG_ENT - 1)) {
505 /* PG */
506 if (SLOT->vib)
507 SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
508 else
509 SLOT->Cnt += SLOT->Incr;
510 /* connection */
511 outd[0] += OP_OUT(SLOT, env_out, feedback2);
512 }
513 }
514
515 /* ---------- calcrate rythm block ---------- */
516 #define WHITE_NOISE_db 6.0
OPL_CALC_RH(OPL_CH * CH)517 inline void OPL_CALC_RH(OPL_CH *CH) {
518 uint32 env_tam;
519 uint32 env_sd;
520 uint32 env_top;
521 uint32 env_hh;
522 // This code used to do int(OPL->rnd.getRandomBit() * (WHITE_NOISE_db / EG_STEP)),
523 // but EG_STEP = 96.0/EG_ENT, and WHITE_NOISE_db=6.0. So, that's equivalent to
524 // int(OPL->rnd.getRandomBit() * EG_ENT/16). We know that EG_ENT is 4096, or 1024,
525 // or 128, so we can safely avoid any FP ops.
526 int whitenoise = int((std::rand()&1)*(WHITE_NOISE_db/EG_STEP));
527
528 int tone8;
529
530 OPL_SLOT *SLOT;
531 uint32 env_out;
532
533 /* BD : same as FM serial mode and output level is large */
534 feedback2 = 0;
535 /* SLOT 1 */
536 SLOT = &CH[6].SLOT[SLOT1];
537 env_out = OPL_CALC_SLOT(SLOT);
538 if (env_out < EG_ENT-1) {
539 /* PG */
540 if (SLOT->vib)
541 SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
542 else
543 SLOT->Cnt += SLOT->Incr;
544 /* connection */
545 if (CH[6].FB) {
546 int feedback1 = (CH[6].op1_out[0] + CH[6].op1_out[1]) >> CH[6].FB;
547 CH[6].op1_out[1] = CH[6].op1_out[0];
548 feedback2 = CH[6].op1_out[0] = OP_OUT(SLOT, env_out, feedback1);
549 }
550 else {
551 feedback2 = OP_OUT(SLOT, env_out, 0);
552 }
553 } else {
554 feedback2 = 0;
555 CH[6].op1_out[1] = CH[6].op1_out[0];
556 CH[6].op1_out[0] = 0;
557 }
558 /* SLOT 2 */
559 SLOT = &CH[6].SLOT[SLOT2];
560 env_out = OPL_CALC_SLOT(SLOT);
561 if (env_out < EG_ENT-1) {
562 /* PG */
563 if (SLOT->vib)
564 SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
565 else
566 SLOT->Cnt += SLOT->Incr;
567 /* connection */
568 outd[0] += OP_OUT(SLOT, env_out, feedback2) * 2;
569 }
570
571 // SD (17) = mul14[fnum7] + white noise
572 // TAM (15) = mul15[fnum8]
573 // TOP (18) = fnum6(mul18[fnum8]+whitenoise)
574 // HH (14) = fnum7(mul18[fnum8]+whitenoise) + white noise
575 env_sd = OPL_CALC_SLOT(SLOT7_2) + whitenoise;
576 env_tam =OPL_CALC_SLOT(SLOT8_1);
577 env_top = OPL_CALC_SLOT(SLOT8_2);
578 env_hh = OPL_CALC_SLOT(SLOT7_1) + whitenoise;
579
580 /* PG */
581 if (SLOT7_1->vib)
582 SLOT7_1->Cnt += (SLOT7_1->Incr * vib) >> (VIB_RATE_SHIFT-1);
583 else
584 SLOT7_1->Cnt += 2 * SLOT7_1->Incr;
585 if (SLOT7_2->vib)
586 SLOT7_2->Cnt += (CH[7].fc * vib) >> (VIB_RATE_SHIFT-3);
587 else
588 SLOT7_2->Cnt += (CH[7].fc * 8);
589 if (SLOT8_1->vib)
590 SLOT8_1->Cnt += (SLOT8_1->Incr * vib) >> VIB_RATE_SHIFT;
591 else
592 SLOT8_1->Cnt += SLOT8_1->Incr;
593 if (SLOT8_2->vib)
594 SLOT8_2->Cnt += ((CH[8].fc * 3) * vib) >> (VIB_RATE_SHIFT-4);
595 else
596 SLOT8_2->Cnt += (CH[8].fc * 48);
597
598 tone8 = OP_OUT(SLOT8_2,whitenoise,0 );
599
600 /* SD */
601 if (env_sd < (EG_ENT - 1))
602 outd[0] += OP_OUT(SLOT7_1, env_sd, 0) * 8;
603 /* TAM */
604 if (env_tam < (EG_ENT - 1))
605 outd[0] += OP_OUT(SLOT8_1, env_tam, 0) * 2;
606 /* TOP-CY */
607 if (env_top < (EG_ENT - 1))
608 outd[0] += OP_OUT(SLOT7_2, env_top, tone8) * 2;
609 /* HH */
610 if (env_hh < (EG_ENT-1))
611 outd[0] += OP_OUT(SLOT7_2, env_hh, tone8) * 2;
612 }
613
614 /* ----------- initialize time tabls ----------- */
init_timetables(FM_OPL * OPL,int ARRATE,int DRRATE)615 static void init_timetables(FM_OPL *OPL, int ARRATE, int DRRATE) {
616 /* make attack rate & decay rate tables */
617 for (int i = 0; i < 4; i++)
618 OPL->AR_TABLE[i] = OPL->DR_TABLE[i] = 0;
619 for (int i = 4; i <= 60; i++) {
620 double rate = OPL->freqbase; /* frequency rate */
621 if (i < 60)
622 rate *= 1.0 + (i & 3) * 0.25; /* b0-1 : x1 , x1.25 , x1.5 , x1.75 */
623 rate *= 1 << ((i >> 2) - 1); /* b2-5 : shift bit */
624 rate *= static_cast<double>(EG_ENT << ENV_BITS);
625 OPL->AR_TABLE[i] = static_cast<int>(rate / ARRATE);
626 OPL->DR_TABLE[i] = static_cast<int>(rate / DRRATE);
627 }
628 for (int i = 60; i < 76; i++) {
629 OPL->AR_TABLE[i] = EG_AED-1;
630 OPL->DR_TABLE[i] = OPL->DR_TABLE[60];
631 }
632 }
633
634 /* ---------- generic table initialize ---------- */
OPLOpenTable()635 static int OPLOpenTable() {
636 /* allocate dynamic tables */
637 TL_TABLE = new int[TL_MAX * 2];
638 SIN_TABLE = new int *[SIN_ENT * 4];
639 AMS_TABLE = new int[AMS_ENT * 2];
640 VIB_TABLE = new int[VIB_ENT * 2];
641 ENV_CURVE = new int[2*EG_ENT+1];
642 /* make total level table */
643 for (uint32 t = 0; t < EG_ENT - 1; t++) {
644 double rate = ((1 << TL_BITS) - 1) / pow(10.0, EG_STEP * t / 20); /* dB -> voltage */
645 TL_TABLE[ t] = static_cast<int>(rate);
646 TL_TABLE[TL_MAX + t] = -TL_TABLE[t];
647 }
648 /* fill volume off area */
649 for (uint32 t = EG_ENT - 1; t < TL_MAX; t++) {
650 TL_TABLE[t] = TL_TABLE[TL_MAX + t] = 0;
651 }
652
653 /* make sinwave table (total level offet) */
654 /* degree 0 = degree 180 = off */
655 SIN_TABLE[0] = SIN_TABLE[SIN_ENT /2 ] = &TL_TABLE[EG_ENT - 1];
656 for (uint32 s = 1; s <= SIN_ENT / 4; s++) {
657 double pom = sin(2 * M_PI * s / SIN_ENT); /* sin */
658 pom = 20 * log10(1 / pom); /* decibel */
659 int j = int(pom / EG_STEP); /* TL_TABLE steps */
660
661 /* degree 0 - 90 , degree 180 - 90 : plus section */
662 SIN_TABLE[ s] = SIN_TABLE[SIN_ENT / 2 - s] = &TL_TABLE[j];
663 /* degree 180 - 270 , degree 360 - 270 : minus section */
664 SIN_TABLE[SIN_ENT / 2 + s] = SIN_TABLE[SIN_ENT - s] = &TL_TABLE[TL_MAX + j];
665 }
666 for (uint32 s = 0; s < SIN_ENT; s++) {
667 SIN_TABLE[SIN_ENT * 1 + s] = s < (SIN_ENT / 2) ? SIN_TABLE[s] : &TL_TABLE[EG_ENT];
668 SIN_TABLE[SIN_ENT * 2 + s] = SIN_TABLE[s % (SIN_ENT / 2)];
669 SIN_TABLE[SIN_ENT * 3 + s] = (s / (SIN_ENT / 4)) & 1 ? &TL_TABLE[EG_ENT] : SIN_TABLE[SIN_ENT * 2 + s];
670 }
671
672 /* envelope counter -> envelope output table */
673 for (uint32 i = 0; i < EG_ENT; i++) {
674 /* ATTACK curve */
675 double pom = pow((static_cast<double>(EG_ENT - 1 - i) / EG_ENT), 8) * EG_ENT;
676 /* if (pom >= EG_ENT) pom = EG_ENT-1; */
677 ENV_CURVE[i] = static_cast<int>(pom);
678 /* DECAY ,RELEASE curve */
679 ENV_CURVE[(EG_DST >> ENV_BITS) + i]= i;
680 }
681 /* off */
682 ENV_CURVE[EG_OFF >> ENV_BITS]= EG_ENT - 1;
683 /* make LFO ams table */
684 for (uint32 i = 0; i < AMS_ENT; i++) {
685 double pom = (1.0 + sin(2 * M_PI * i / AMS_ENT)) / 2; /* sin */
686 AMS_TABLE[i] = static_cast<int>((1.0 / EG_STEP) * pom); /* 1dB */
687 AMS_TABLE[AMS_ENT + i] = static_cast<int>((4.8 / EG_STEP) * pom); /* 4.8dB */
688 }
689 /* make LFO vibrate table */
690 for (uint32 i = 0; i < VIB_ENT; i++) {
691 /* 100cent = 1seminote = 6% ?? */
692 double pom = VIB_RATE * 0.06 * sin(2 * M_PI * i / VIB_ENT); /* +-100sect step */
693 VIB_TABLE[i] = static_cast<int>(VIB_RATE + (pom * 0.07)); /* +- 7cent */
694 VIB_TABLE[VIB_ENT + i] = static_cast<int>(VIB_RATE + (pom * 0.14)); /* +-14cent */
695 }
696 return 1;
697 }
698
OPLCloseTable()699 static void OPLCloseTable() {
700 delete [] TL_TABLE;
701 delete [] SIN_TABLE;
702 delete [] AMS_TABLE;
703 delete [] VIB_TABLE;
704 delete [] ENV_CURVE;
705 }
706
707 /* CSM Key Controll */
CSMKeyControll(OPL_CH * CH)708 inline void CSMKeyControll(OPL_CH *CH) {
709 OPL_SLOT *slot1 = &CH->SLOT[SLOT1];
710 OPL_SLOT *slot2 = &CH->SLOT[SLOT2];
711 /* all key off */
712 OPL_KEYOFF(slot1);
713 OPL_KEYOFF(slot2);
714 /* total level latch */
715 slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
716 slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
717 /* key on */
718 CH->op1_out[0] = CH->op1_out[1] = 0;
719 OPL_KEYON(slot1);
720 OPL_KEYON(slot2);
721 }
722
723 /* ---------- opl initialize ---------- */
OPL_initalize(FM_OPL * OPL)724 static void OPL_initalize(FM_OPL *OPL) {
725 int fn;
726
727 /* frequency base */
728 OPL->freqbase = (OPL->rate) ? (static_cast<double>(OPL->clock) / OPL->rate) / 72 : 0;
729 /* Timer base time */
730 OPL->TimerBase = 1.0/(static_cast<double>(OPL->clock) / 72.0 );
731 /* make time tables */
732 init_timetables(OPL, OPL_ARRATE, OPL_DRRATE);
733 /* make fnumber -> increment counter table */
734 for (fn=0; fn < 1024; fn++) {
735 OPL->FN_TABLE[fn] = static_cast<uint32>(OPL->freqbase * fn * FREQ_RATE * (1<<7) / 2);
736 }
737 /* LFO freq.table */
738 OPL->amsIncr = static_cast<int>(OPL->rate ? static_cast<double>(AMS_ENT) * (1 << AMS_SHIFT) / OPL->rate * 3.7 * (OPL->clock/3600000.0) : 0);
739 OPL->vibIncr = static_cast<int>(OPL->rate ? static_cast<double>(VIB_ENT) * (1 << VIB_SHIFT) / OPL->rate * 6.4 * (OPL->clock/3600000.0) : 0);
740 }
741
742 /* ---------- write a OPL registers ---------- */
OPLWriteReg(FM_OPL * OPL,int r,int v)743 void OPLWriteReg(FM_OPL *OPL, int r, int v) {
744 OPL_CH *CH;
745 int slot;
746 uint32 block_fnum;
747
748 switch (r & 0xe0) {
749 case 0x00: /* 00-1f:controll */
750 switch (r & 0x1f) {
751 case 0x01:
752 /* wave selector enable */
753 if (OPL->type&OPL_TYPE_WAVESEL) {
754 OPL->wavesel = v & 0x20;
755 if (!OPL->wavesel) {
756 /* preset compatible mode */
757 int c;
758 for (c = 0; c < OPL->max_ch; c++) {
759 OPL->P_CH[c].SLOT[SLOT1].wavetable = &SIN_TABLE[0];
760 OPL->P_CH[c].SLOT[SLOT2].wavetable = &SIN_TABLE[0];
761 }
762 }
763 }
764 return;
765 case 0x02: /* Timer 1 */
766 OPL->T[0] = (256-v) * 4;
767 break;
768 case 0x03: /* Timer 2 */
769 OPL->T[1] = (256-v) * 16;
770 return;
771 case 0x04: /* IRQ clear / mask and Timer enable */
772 if (v & 0x80) { /* IRQ flag clear */
773 OPL_STATUS_RESET(OPL, 0x7f);
774 } else { /* set IRQ mask ,timer enable*/
775 uint8 st1 = v & 1;
776 uint8 st2 = (v >> 1) & 1;
777 /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
778 OPL_STATUS_RESET(OPL, v & 0x78);
779 OPL_STATUSMASK_SET(OPL,((~v) & 0x78) | 0x01);
780 /* timer 2 */
781 if (OPL->st[1] != st2) {
782 double interval = st2 ? (OPL->T[1] * OPL->TimerBase) : 0.0;
783 OPL->st[1] = st2;
784 if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam + 1, interval);
785 }
786 /* timer 1 */
787 if (OPL->st[0] != st1) {
788 double interval = st1 ? (OPL->T[0] * OPL->TimerBase) : 0.0;
789 OPL->st[0] = st1;
790 if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam + 0, interval);
791 }
792 }
793 return;
794 }
795 break;
796 case 0x20: /* am,vib,ksr,eg type,mul */
797 slot = slot_array[r&0x1f];
798 if (slot == -1)
799 return;
800 set_mul(OPL,slot,v);
801 return;
802 case 0x40:
803 slot = slot_array[r&0x1f];
804 if (slot == -1)
805 return;
806 set_ksl_tl(OPL,slot,v);
807 return;
808 case 0x60:
809 slot = slot_array[r&0x1f];
810 if (slot == -1)
811 return;
812 set_ar_dr(OPL,slot,v);
813 return;
814 case 0x80:
815 slot = slot_array[r&0x1f];
816 if (slot == -1)
817 return;
818 set_sl_rr(OPL,slot,v);
819 return;
820 case 0xa0:
821 switch (r) {
822 case 0xbd:
823 /* amsep,vibdep,r,bd,sd,tom,tc,hh */
824 {
825 uint8 rkey = OPL->rythm ^ v;
826 OPL->ams_table = &AMS_TABLE[v & 0x80 ? AMS_ENT : 0];
827 OPL->vib_table = &VIB_TABLE[v & 0x40 ? VIB_ENT : 0];
828 OPL->rythm = v & 0x3f;
829 if (OPL->rythm & 0x20) {
830 /* BD key on/off */
831 if (rkey & 0x10) {
832 if (v & 0x10) {
833 OPL->P_CH[6].op1_out[0] = OPL->P_CH[6].op1_out[1] = 0;
834 OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT1]);
835 OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT2]);
836 } else {
837 OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1]);
838 OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2]);
839 }
840 }
841 /* SD key on/off */
842 if (rkey & 0x08) {
843 if (v & 0x08)
844 OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT2]);
845 else
846 OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2]);
847 }/* TAM key on/off */
848 if (rkey & 0x04) {
849 if (v & 0x04)
850 OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT1]);
851 else
852 OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1]);
853 }
854 /* TOP-CY key on/off */
855 if (rkey & 0x02) {
856 if (v & 0x02)
857 OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT2]);
858 else
859 OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2]);
860 }
861 /* HH key on/off */
862 if (rkey & 0x01) {
863 if (v & 0x01)
864 OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT1]);
865 else
866 OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1]);
867 }
868 }
869 }
870 return;
871
872 default:
873 break;
874 }
875 /* keyon,block,fnum */
876 if ((r & 0x0f) > 8)
877 return;
878 CH = &OPL->P_CH[r & 0x0f];
879 if (!(r&0x10)) { /* a0-a8 */
880 block_fnum = (CH->block_fnum & 0x1f00) | v;
881 } else { /* b0-b8 */
882 int keyon = (v >> 5) & 1;
883 block_fnum = ((v & 0x1f) << 8) | (CH->block_fnum & 0xff);
884 if (CH->keyon != keyon) {
885 if ((CH->keyon=keyon)) {
886 CH->op1_out[0] = CH->op1_out[1] = 0;
887 OPL_KEYON(&CH->SLOT[SLOT1]);
888 OPL_KEYON(&CH->SLOT[SLOT2]);
889 } else {
890 OPL_KEYOFF(&CH->SLOT[SLOT1]);
891 OPL_KEYOFF(&CH->SLOT[SLOT2]);
892 }
893 }
894 }
895 /* update */
896 if (CH->block_fnum != block_fnum) {
897 int blockRv = 7 - (block_fnum >> 10);
898 int fnum = block_fnum & 0x3ff;
899 CH->block_fnum = block_fnum;
900 CH->ksl_base = KSL_TABLE[block_fnum >> 6];
901 CH->fc = OPL->FN_TABLE[fnum] >> blockRv;
902 CH->kcode = CH->block_fnum >> 9;
903 if ((OPL->mode & 0x40) && CH->block_fnum & 0x100)
904 CH->kcode |=1;
905 CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
906 CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
907 }
908 return;
909 case 0xc0:
910 /* FB,C */
911 if ((r & 0x0f) > 8)
912 return;
913 CH = &OPL->P_CH[r&0x0f];
914 {
915 int feedback = (v >> 1) & 7;
916 CH->FB = feedback ? (8 + 1) - feedback : 0;
917 CH->CON = v & 1;
918 set_algorythm(CH);
919 }
920 return;
921 case 0xe0: /* wave type */
922 slot = slot_array[r & 0x1f];
923 if (slot == -1)
924 return;
925 CH = &OPL->P_CH[slot>>1];
926 if (OPL->wavesel) {
927 CH->SLOT[slot&1].wavetable = &SIN_TABLE[(v & 0x03) * SIN_ENT];
928 }
929 return;
930 }
931 }
932
933 /* lock/unlock for common table */
OPL_LockTable()934 static int OPL_LockTable() {
935 num_lock++;
936 if (num_lock>1)
937 return 0;
938 /* first time */
939 cur_chip = nullptr;
940 /* allocate total level table (128kb space) */
941 if (!OPLOpenTable()) {
942 num_lock--;
943 return -1;
944 }
945 return 0;
946 }
947
OPL_UnLockTable()948 static void OPL_UnLockTable() {
949 if (num_lock)
950 num_lock--;
951 if (num_lock)
952 return;
953 /* last time */
954 cur_chip = nullptr;
955 OPLCloseTable();
956 }
957
958 /*******************************************************************************/
959 /* YM3812 local section */
960 /*******************************************************************************/
961
962 /* ---------- update one of chip ----------- */
YM3812UpdateOne_Mono(FM_OPL * OPL,sint16 * buffer,int length)963 void YM3812UpdateOne_Mono(FM_OPL *OPL, sint16 *buffer, int length) {
964 sint16 *buf = buffer;
965 uint32 amsCnt = OPL->amsCnt;
966 uint32 vibCnt = OPL->vibCnt;
967 uint8 rythm = OPL->rythm & 0x20;
968
969 if (OPL != cur_chip) {
970 cur_chip = OPL;
971 /* channel pointers */
972 S_CH = OPL->P_CH;
973 E_CH = &S_CH[9];
974 /* rythm slot */
975 SLOT7_1 = &S_CH[7].SLOT[SLOT1];
976 SLOT7_2 = &S_CH[7].SLOT[SLOT2];
977 SLOT8_1 = &S_CH[8].SLOT[SLOT1];
978 SLOT8_2 = &S_CH[8].SLOT[SLOT2];
979 /* LFO state */
980 amsIncr = OPL->amsIncr;
981 vibIncr = OPL->vibIncr;
982 ams_table = OPL->ams_table;
983 vib_table = OPL->vib_table;
984 }
985 OPL_CH *R_CH = rythm ? &S_CH[6] : E_CH;
986 for (int i = 0; i < length; i++) {
987 /* channel A channel B channel C */
988 /* LFO */
989 ams = ams_table[(amsCnt += amsIncr) >> AMS_SHIFT];
990 vib = vib_table[(vibCnt += vibIncr) >> VIB_SHIFT];
991 outd[0] = 0;
992 /* FM part */
993 for (OPL_CH *CH = S_CH; CH < R_CH; CH++)
994 OPL_CALC_CH(CH);
995 /* Rythn part */
996 if (rythm)
997 OPL_CALC_RH(S_CH);
998 /* limit check */
999 int data = CLIP(outd[0], OPL_MINOUT, OPL_MAXOUT);
1000 /* store to sound buffer */
1001 buf[i] = data >> OPL_OUTSB;
1002 }
1003
1004 OPL->amsCnt = amsCnt;
1005 OPL->vibCnt = vibCnt;
1006 }
1007
YM3812UpdateOne_Stereo(FM_OPL * OPL,sint16 * buffer,int length)1008 void YM3812UpdateOne_Stereo(FM_OPL *OPL, sint16 *buffer, int length) {
1009 sint16 *buf = buffer;
1010 uint32 amsCnt = OPL->amsCnt;
1011 uint32 vibCnt = OPL->vibCnt;
1012 uint8 rythm = OPL->rythm & 0x20;
1013
1014 if (OPL != cur_chip) {
1015 cur_chip = OPL;
1016 /* channel pointers */
1017 S_CH = OPL->P_CH;
1018 E_CH = &S_CH[9];
1019 /* rythm slot */
1020 SLOT7_1 = &S_CH[7].SLOT[SLOT1];
1021 SLOT7_2 = &S_CH[7].SLOT[SLOT2];
1022 SLOT8_1 = &S_CH[8].SLOT[SLOT1];
1023 SLOT8_2 = &S_CH[8].SLOT[SLOT2];
1024 /* LFO state */
1025 amsIncr = OPL->amsIncr;
1026 vibIncr = OPL->vibIncr;
1027 ams_table = OPL->ams_table;
1028 vib_table = OPL->vib_table;
1029 }
1030 OPL_CH *R_CH = rythm ? &S_CH[6] : E_CH;
1031 for (int i = 0; i < length; i++) {
1032 /* channel A channel B channel C */
1033 /* LFO */
1034 ams = ams_table[(amsCnt += amsIncr) >> AMS_SHIFT];
1035 vib = vib_table[(vibCnt += vibIncr) >> VIB_SHIFT];
1036 int left = 0;
1037 int right = 0;
1038 /* FM part */
1039 for(OPL_CH *CH = S_CH; CH < R_CH; CH++) {
1040 outd[0] = 0;
1041 OPL_CALC_CH(CH);
1042 if (CH->PAN <= 64)
1043 left += outd[0];
1044 else
1045 left += (outd[0]>>6)*(127-CH->PAN);
1046 if (CH->PAN >= 64)
1047 right += outd[0];
1048 else
1049 right += (outd[0]>>6)*(CH->PAN);
1050 }
1051 /* Rythn part */
1052 if (rythm) {
1053 outd[0] = 0;
1054 OPL_CALC_RH(S_CH);
1055 left += outd[0];
1056 right += outd[0];
1057 }
1058 /* limit check */
1059 int data = CLIP(left , OPL_MINOUT, OPL_MAXOUT);
1060 /* store to sound buffer */
1061 buf[i * 2] = data >> OPL_OUTSB;
1062
1063 /* limit check */
1064 data = CLIP(right, OPL_MINOUT, OPL_MAXOUT);
1065 /* store to sound buffer */
1066 buf[i * 2+1] = data >> OPL_OUTSB;
1067 }
1068
1069 OPL->amsCnt = amsCnt;
1070 OPL->vibCnt = vibCnt;
1071 }
1072
1073 /* ---------- reset a chip ---------- */
OPLResetChip(FM_OPL * OPL)1074 void OPLResetChip(FM_OPL *OPL) {
1075 int c;
1076 int s;
1077 int i;
1078
1079 /* reset chip */
1080 OPL->mode = 0; /* normal mode */
1081 OPL_STATUS_RESET(OPL, 0x7f);
1082 /* reset with register write */
1083 OPLWriteReg(OPL, 0x01,0); /* wabesel disable */
1084 OPLWriteReg(OPL, 0x02,0); /* Timer1 */
1085 OPLWriteReg(OPL, 0x03,0); /* Timer2 */
1086 OPLWriteReg(OPL, 0x04,0); /* IRQ mask clear */
1087 for (i = 0xff; i >= 0x20; i--)
1088 OPLWriteReg(OPL,i,0);
1089 /* reset OPerator parameter */
1090 for (c = 0; c < OPL->max_ch; c++) {
1091 OPL_CH *CH = &OPL->P_CH[c];
1092 OPL->P_CH[c].PAN = 64;
1093 for (s = 0; s < 2; s++) {
1094 /* wave table */
1095 CH->SLOT[s].wavetable = &SIN_TABLE[0];
1096 /* CH->SLOT[s].evm = ENV_MOD_RR; */
1097 CH->SLOT[s].evc = EG_OFF;
1098 CH->SLOT[s].eve = EG_OFF + 1;
1099 CH->SLOT[s].evs = 0;
1100 }
1101 }
1102 }
1103
1104 /* ---------- Create a virtual YM3812 ---------- */
1105 /* 'rate' is sampling rate and 'bufsiz' is the size of the */
OPLCreate(int type,int clock,int rate)1106 FM_OPL *OPLCreate(int type, int clock, int rate) {
1107 if (OPL_LockTable() == -1)
1108 return nullptr;
1109
1110 auto *OPL = new FM_OPL{};
1111 OPL->P_CH = OPL->channels;
1112
1113 /* set channel state pointer */
1114 OPL->type = type;
1115 OPL->clock = clock;
1116 OPL->rate = rate;
1117 OPL->max_ch = max_opl_channels;
1118
1119 /* init grobal tables */
1120 OPL_initalize(OPL);
1121
1122 /* reset chip */
1123 OPLResetChip(OPL);
1124 return OPL;
1125 }
1126
1127 /* ---------- Destroy one of virtual YM3812 ---------- */
OPLDestroy(FM_OPL * OPL)1128 void OPLDestroy(FM_OPL *OPL) {
1129 OPL_UnLockTable();
1130 delete OPL;
1131 }
1132
1133 /* ---------- Option handlers ---------- */
OPLSetTimerHandler(FM_OPL * OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset)1134 void OPLSetTimerHandler(FM_OPL *OPL, OPL_TIMERHANDLER TimerHandler,int channelOffset) {
1135 OPL->TimerHandler = TimerHandler;
1136 OPL->TimerParam = channelOffset;
1137 }
1138
OPLSetIRQHandler(FM_OPL * OPL,OPL_IRQHANDLER IRQHandler,int param)1139 void OPLSetIRQHandler(FM_OPL *OPL, OPL_IRQHANDLER IRQHandler, int param) {
1140 OPL->IRQHandler = IRQHandler;
1141 OPL->IRQParam = param;
1142 }
1143
OPLSetUpdateHandler(FM_OPL * OPL,OPL_UPDATEHANDLER UpdateHandler,int param)1144 void OPLSetUpdateHandler(FM_OPL *OPL, OPL_UPDATEHANDLER UpdateHandler,int param) {
1145 OPL->UpdateHandler = UpdateHandler;
1146 OPL->UpdateParam = param;
1147 }
1148
1149 /* ---------- YM3812 I/O interface ---------- */
OPLWrite(FM_OPL * OPL,int a,int v)1150 int OPLWrite(FM_OPL *OPL,int a,int v) {
1151 if (!(a & 1)) { /* address port */
1152 OPL->address = v & 0xff;
1153 } else { /* data port */
1154 if (OPL->UpdateHandler)
1155 OPL->UpdateHandler(OPL->UpdateParam,0);
1156 OPLWriteReg(OPL, OPL->address,v);
1157 }
1158 return OPL->status >> 7;
1159 }
1160
OPLRead(FM_OPL * OPL,int a)1161 unsigned char OPLRead(FM_OPL *OPL,int a) {
1162 if (!(a & 1)) { /* status port */
1163 return OPL->status & (OPL->statusmask | 0x80);
1164 }
1165 /* data port */
1166 switch (OPL->address) {
1167 case 0x05: /* KeyBoard IN */
1168 PERR(("OPL:read unmapped KEYBOARD port\n"));
1169 return 0;
1170 case 0x19: /* I/O DATA */
1171 PERR(("OPL:read unmapped I/O port\n"));
1172 return 0;
1173 case 0x1a: /* PCM-DATA */
1174 return 0;
1175 default:
1176 break;
1177 }
1178 return 0;
1179 }
1180
OPLTimerOver(FM_OPL * OPL,int c)1181 int OPLTimerOver(FM_OPL *OPL, int c) {
1182 if (c) { /* Timer B */
1183 OPL_STATUS_SET(OPL, 0x20);
1184 } else { /* Timer A */
1185 OPL_STATUS_SET(OPL, 0x40);
1186 /* CSM mode key,TL controll */
1187 if (OPL->mode & 0x80) { /* CSM mode total level latch and auto key on */
1188 int ch;
1189 if (OPL->UpdateHandler)
1190 OPL->UpdateHandler(OPL->UpdateParam,0);
1191 for (ch = 0; ch < 9; ch++)
1192 CSMKeyControll(&OPL->P_CH[ch]);
1193 }
1194 }
1195 /* reload timer */
1196 if (OPL->TimerHandler)
1197 (OPL->TimerHandler)(OPL->TimerParam + c, static_cast<double>(OPL->T[c]) * OPL->TimerBase);
1198 return OPL->status >> 7;
1199 }
1200
OPLSetPan(FM_OPL * OPL,int c,int pan)1201 void OPLSetPan(FM_OPL *OPL, int c, int pan) {
1202 if (c > 0 && c < OPL->max_ch)
1203 OPL->P_CH[c].PAN = pan;
1204 }
1205
makeAdLibOPL(int rate)1206 FM_OPL *makeAdLibOPL(int rate) {
1207 // We need to emulate one YM3812 chip
1208 OPLBuildTables(FMOPL_ENV_BITS_HQ, FMOPL_EG_ENT_HQ);
1209 return OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
1210 }
1211
1212 }
1213
1214 #endif //USE_FMOPL_MIDI
1215
1216