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