xref: /qemu/hw/audio/fmopl.c (revision 37f6be977aefb5de8afe3ba62a95e88918381305)
185571bc7Sbellard /*
285571bc7Sbellard **
385571bc7Sbellard ** File: fmopl.c -- software implementation of FM sound generator
485571bc7Sbellard **
585571bc7Sbellard ** Copyright (C) 1999,2000 Tatsuyuki Satoh , MultiArcadeMachineEmurator development
685571bc7Sbellard **
785571bc7Sbellard ** Version 0.37a
885571bc7Sbellard **
985571bc7Sbellard */
1085571bc7Sbellard 
1185571bc7Sbellard /*
1285571bc7Sbellard 	preliminary :
1385571bc7Sbellard 	Problem :
1485571bc7Sbellard 	note:
1585571bc7Sbellard */
1685571bc7Sbellard 
1785571bc7Sbellard /* This version of fmopl.c is a fork of the MAME one, relicensed under the LGPL.
1885571bc7Sbellard  *
1985571bc7Sbellard  * This library is free software; you can redistribute it and/or
2085571bc7Sbellard  * modify it under the terms of the GNU Lesser General Public
2185571bc7Sbellard  * License as published by the Free Software Foundation; either
2285571bc7Sbellard  * version 2.1 of the License, or (at your option) any later version.
2385571bc7Sbellard  *
2485571bc7Sbellard  * This library is distributed in the hope that it will be useful,
2585571bc7Sbellard  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2685571bc7Sbellard  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2785571bc7Sbellard  * Lesser General Public License for more details.
2885571bc7Sbellard  *
2985571bc7Sbellard  * You should have received a copy of the GNU Lesser General Public
308167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
3185571bc7Sbellard  */
3285571bc7Sbellard 
3385571bc7Sbellard #define HAS_YM3812	1
3485571bc7Sbellard 
3585571bc7Sbellard #include <stdio.h>
3685571bc7Sbellard #include <stdlib.h>
3785571bc7Sbellard #include <string.h>
3885571bc7Sbellard #include <stdarg.h>
3985571bc7Sbellard #include <math.h>
4085571bc7Sbellard //#include "driver.h"		/* use M.A.M.E. */
4147b43a1fSPaolo Bonzini #include "fmopl.h"
4285571bc7Sbellard 
4385571bc7Sbellard #ifndef PI
4485571bc7Sbellard #define PI 3.14159265358979323846
4585571bc7Sbellard #endif
4685571bc7Sbellard 
47913895abSStefan Weil #ifndef ARRAY_SIZE
48913895abSStefan Weil #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
49913895abSStefan Weil #endif
50913895abSStefan Weil 
5185571bc7Sbellard /* -------------------- for debug --------------------- */
5285571bc7Sbellard /* #define OPL_OUTPUT_LOG */
5385571bc7Sbellard #ifdef OPL_OUTPUT_LOG
5485571bc7Sbellard static FILE *opl_dbg_fp = NULL;
5585571bc7Sbellard static FM_OPL *opl_dbg_opl[16];
5685571bc7Sbellard static int opl_dbg_maxchip,opl_dbg_chip;
5785571bc7Sbellard #endif
5885571bc7Sbellard 
5985571bc7Sbellard /* -------------------- preliminary define section --------------------- */
6085571bc7Sbellard /* attack/decay rate time rate */
6185571bc7Sbellard #define OPL_ARRATE     141280  /* RATE 4 =  2826.24ms @ 3.6MHz */
6285571bc7Sbellard #define OPL_DRRATE    1956000  /* RATE 4 = 39280.64ms @ 3.6MHz */
6385571bc7Sbellard 
6485571bc7Sbellard #define DELTAT_MIXING_LEVEL (1) /* DELTA-T ADPCM MIXING LEVEL */
6585571bc7Sbellard 
6685571bc7Sbellard #define FREQ_BITS 24			/* frequency turn          */
6785571bc7Sbellard 
6885571bc7Sbellard /* counter bits = 20 , octerve 7 */
6985571bc7Sbellard #define FREQ_RATE   (1<<(FREQ_BITS-20))
7085571bc7Sbellard #define TL_BITS    (FREQ_BITS+2)
7185571bc7Sbellard 
7285571bc7Sbellard /* final output shift , limit minimum and maximum */
7385571bc7Sbellard #define OPL_OUTSB   (TL_BITS+3-16)		/* OPL output final shift 16bit */
7485571bc7Sbellard #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
7585571bc7Sbellard #define OPL_MINOUT (-0x8000<<OPL_OUTSB)
7685571bc7Sbellard 
7785571bc7Sbellard /* -------------------- quality selection --------------------- */
7885571bc7Sbellard 
7985571bc7Sbellard /* sinwave entries */
8085571bc7Sbellard /* used static memory = SIN_ENT * 4 (byte) */
8185571bc7Sbellard #define SIN_ENT 2048
8285571bc7Sbellard 
8385571bc7Sbellard /* output level entries (envelope,sinwave) */
8485571bc7Sbellard /* envelope counter lower bits */
8585571bc7Sbellard #define ENV_BITS 16
8685571bc7Sbellard /* envelope output entries */
8785571bc7Sbellard #define EG_ENT   4096
8885571bc7Sbellard /* used dynamic memory = EG_ENT*4*4(byte)or EG_ENT*6*4(byte) */
8985571bc7Sbellard /* used static  memory = EG_ENT*4 (byte)                     */
9085571bc7Sbellard 
9185571bc7Sbellard #define EG_OFF   ((2*EG_ENT)<<ENV_BITS)  /* OFF          */
9285571bc7Sbellard #define EG_DED   EG_OFF
9385571bc7Sbellard #define EG_DST   (EG_ENT<<ENV_BITS)      /* DECAY  START */
9485571bc7Sbellard #define EG_AED   EG_DST
9585571bc7Sbellard #define EG_AST   0                       /* ATTACK START */
9685571bc7Sbellard 
9785571bc7Sbellard #define EG_STEP (96.0/EG_ENT) /* OPL is 0.1875 dB step  */
9885571bc7Sbellard 
9985571bc7Sbellard /* LFO table entries */
10085571bc7Sbellard #define VIB_ENT 512
10185571bc7Sbellard #define VIB_SHIFT (32-9)
10285571bc7Sbellard #define AMS_ENT 512
10385571bc7Sbellard #define AMS_SHIFT (32-9)
10485571bc7Sbellard 
10585571bc7Sbellard #define VIB_RATE 256
10685571bc7Sbellard 
10785571bc7Sbellard /* -------------------- local defines , macros --------------------- */
10885571bc7Sbellard 
10985571bc7Sbellard /* register number to channel number , slot offset */
11085571bc7Sbellard #define SLOT1 0
11185571bc7Sbellard #define SLOT2 1
11285571bc7Sbellard 
11385571bc7Sbellard /* envelope phase */
11485571bc7Sbellard #define ENV_MOD_RR  0x00
11585571bc7Sbellard #define ENV_MOD_DR  0x01
11685571bc7Sbellard #define ENV_MOD_AR  0x02
11785571bc7Sbellard 
11885571bc7Sbellard /* -------------------- tables --------------------- */
11985571bc7Sbellard static const int slot_array[32]=
12085571bc7Sbellard {
12185571bc7Sbellard 	 0, 2, 4, 1, 3, 5,-1,-1,
12285571bc7Sbellard 	 6, 8,10, 7, 9,11,-1,-1,
12385571bc7Sbellard 	12,14,16,13,15,17,-1,-1,
12485571bc7Sbellard 	-1,-1,-1,-1,-1,-1,-1,-1
12585571bc7Sbellard };
12685571bc7Sbellard 
12785571bc7Sbellard /* key scale level */
12885571bc7Sbellard /* table is 3dB/OCT , DV converts this in TL step at 6dB/OCT */
12985571bc7Sbellard #define DV (EG_STEP/2)
13085571bc7Sbellard static const UINT32 KSL_TABLE[8*16]=
13185571bc7Sbellard {
13285571bc7Sbellard 	/* OCT 0 */
13385571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
13485571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
13585571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
13685571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
13785571bc7Sbellard 	/* OCT 1 */
13885571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
13985571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
14085571bc7Sbellard 	 0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
14185571bc7Sbellard 	 1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
14285571bc7Sbellard 	/* OCT 2 */
14385571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
14485571bc7Sbellard 	 0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
14585571bc7Sbellard 	 3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
14685571bc7Sbellard 	 4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
14785571bc7Sbellard 	/* OCT 3 */
14885571bc7Sbellard 	 0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
14985571bc7Sbellard 	 3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
15085571bc7Sbellard 	 6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
15185571bc7Sbellard 	 7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
15285571bc7Sbellard 	/* OCT 4 */
15385571bc7Sbellard 	 0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
15485571bc7Sbellard 	 6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
15585571bc7Sbellard 	 9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
15685571bc7Sbellard 	10.875/DV,11.250/DV,11.625/DV,12.000/DV,
15785571bc7Sbellard 	/* OCT 5 */
15885571bc7Sbellard 	 0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
15985571bc7Sbellard 	 9.000/DV,10.125/DV,10.875/DV,11.625/DV,
16085571bc7Sbellard 	12.000/DV,12.750/DV,13.125/DV,13.500/DV,
16185571bc7Sbellard 	13.875/DV,14.250/DV,14.625/DV,15.000/DV,
16285571bc7Sbellard 	/* OCT 6 */
16385571bc7Sbellard 	 0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
16485571bc7Sbellard 	12.000/DV,13.125/DV,13.875/DV,14.625/DV,
16585571bc7Sbellard 	15.000/DV,15.750/DV,16.125/DV,16.500/DV,
16685571bc7Sbellard 	16.875/DV,17.250/DV,17.625/DV,18.000/DV,
16785571bc7Sbellard 	/* OCT 7 */
16885571bc7Sbellard 	 0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
16985571bc7Sbellard 	15.000/DV,16.125/DV,16.875/DV,17.625/DV,
17085571bc7Sbellard 	18.000/DV,18.750/DV,19.125/DV,19.500/DV,
17185571bc7Sbellard 	19.875/DV,20.250/DV,20.625/DV,21.000/DV
17285571bc7Sbellard };
17385571bc7Sbellard #undef DV
17485571bc7Sbellard 
17585571bc7Sbellard /* sustain lebel table (3db per step) */
17685571bc7Sbellard /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
17785571bc7Sbellard #define SC(db) (db*((3/EG_STEP)*(1<<ENV_BITS)))+EG_DST
17885571bc7Sbellard static const INT32 SL_TABLE[16]={
17985571bc7Sbellard  SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
18085571bc7Sbellard  SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
18185571bc7Sbellard };
18285571bc7Sbellard #undef SC
18385571bc7Sbellard 
18485571bc7Sbellard #define TL_MAX (EG_ENT*2) /* limit(tl + ksr + envelope) + sinwave */
18585571bc7Sbellard /* TotalLevel : 48 24 12  6  3 1.5 0.75 (dB) */
18685571bc7Sbellard /* TL_TABLE[ 0      to TL_MAX          ] : plus  section */
18785571bc7Sbellard /* TL_TABLE[ TL_MAX to TL_MAX+TL_MAX-1 ] : minus section */
18885571bc7Sbellard static INT32 *TL_TABLE;
18985571bc7Sbellard 
19085571bc7Sbellard /* pointers to TL_TABLE with sinwave output offset */
19185571bc7Sbellard static INT32 **SIN_TABLE;
19285571bc7Sbellard 
19385571bc7Sbellard /* LFO table */
19485571bc7Sbellard static INT32 *AMS_TABLE;
19585571bc7Sbellard static INT32 *VIB_TABLE;
19685571bc7Sbellard 
19785571bc7Sbellard /* envelope output curve table */
19885571bc7Sbellard /* attack + decay + OFF */
19985571bc7Sbellard static INT32 ENV_CURVE[2*EG_ENT+1];
20085571bc7Sbellard 
20185571bc7Sbellard /* multiple table */
20285571bc7Sbellard #define ML 2
20385571bc7Sbellard static const UINT32 MUL_TABLE[16]= {
20485571bc7Sbellard /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 */
20585571bc7Sbellard    0.50*ML, 1.00*ML, 2.00*ML, 3.00*ML, 4.00*ML, 5.00*ML, 6.00*ML, 7.00*ML,
20685571bc7Sbellard    8.00*ML, 9.00*ML,10.00*ML,10.00*ML,12.00*ML,12.00*ML,15.00*ML,15.00*ML
20785571bc7Sbellard };
20885571bc7Sbellard #undef ML
20985571bc7Sbellard 
21085571bc7Sbellard /* dummy attack / decay rate ( when rate == 0 ) */
21185571bc7Sbellard static INT32 RATE_0[16]=
21285571bc7Sbellard {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
21385571bc7Sbellard 
21485571bc7Sbellard /* -------------------- static state --------------------- */
21585571bc7Sbellard 
21685571bc7Sbellard /* lock level of common table */
21785571bc7Sbellard static int num_lock = 0;
21885571bc7Sbellard 
21985571bc7Sbellard /* work table */
22085571bc7Sbellard static void *cur_chip = NULL;	/* current chip point */
22185571bc7Sbellard /* currenct chip state */
22285571bc7Sbellard /* static OPLSAMPLE  *bufL,*bufR; */
22385571bc7Sbellard static OPL_CH *S_CH;
22485571bc7Sbellard static OPL_CH *E_CH;
22569df1c3cSStefan Weil static OPL_SLOT *SLOT7_1, *SLOT7_2, *SLOT8_1, *SLOT8_2;
22685571bc7Sbellard 
22785571bc7Sbellard static INT32 outd[1];
22885571bc7Sbellard static INT32 ams;
22985571bc7Sbellard static INT32 vib;
23069df1c3cSStefan Weil static INT32 *ams_table;
23169df1c3cSStefan Weil static INT32 *vib_table;
23285571bc7Sbellard static INT32 amsIncr;
23385571bc7Sbellard static INT32 vibIncr;
23485571bc7Sbellard static INT32 feedback2;		/* connect for SLOT 2 */
23585571bc7Sbellard 
23685571bc7Sbellard /* log output level */
23785571bc7Sbellard #define LOG_ERR  3      /* ERROR       */
23885571bc7Sbellard #define LOG_WAR  2      /* WARNING     */
23985571bc7Sbellard #define LOG_INF  1      /* INFORMATION */
24085571bc7Sbellard 
24185571bc7Sbellard //#define LOG_LEVEL LOG_INF
24285571bc7Sbellard #define LOG_LEVEL	LOG_ERR
24385571bc7Sbellard 
24485571bc7Sbellard //#define LOG(n,x) if( (n)>=LOG_LEVEL ) logerror x
24585571bc7Sbellard #define LOG(n,x)
24685571bc7Sbellard 
24785571bc7Sbellard /* --------------------- subroutines  --------------------- */
24885571bc7Sbellard 
249*37f6be97SLuiz Capitulino static inline int Limit( int val, int max, int min ) {
25085571bc7Sbellard 	if ( val > max )
25185571bc7Sbellard 		val = max;
25285571bc7Sbellard 	else if ( val < min )
25385571bc7Sbellard 		val = min;
25485571bc7Sbellard 
25585571bc7Sbellard 	return val;
25685571bc7Sbellard }
25785571bc7Sbellard 
25885571bc7Sbellard /* status set and IRQ handling */
259*37f6be97SLuiz Capitulino static inline void OPL_STATUS_SET(FM_OPL *OPL,int flag)
26085571bc7Sbellard {
26185571bc7Sbellard 	/* set status flag */
26285571bc7Sbellard 	OPL->status |= flag;
26385571bc7Sbellard 	if(!(OPL->status & 0x80))
26485571bc7Sbellard 	{
26585571bc7Sbellard 		if(OPL->status & OPL->statusmask)
26685571bc7Sbellard 		{	/* IRQ on */
26785571bc7Sbellard 			OPL->status |= 0x80;
26885571bc7Sbellard 			/* callback user interrupt handler (IRQ is OFF to ON) */
26985571bc7Sbellard 			if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,1);
27085571bc7Sbellard 		}
27185571bc7Sbellard 	}
27285571bc7Sbellard }
27385571bc7Sbellard 
27485571bc7Sbellard /* status reset and IRQ handling */
275*37f6be97SLuiz Capitulino static inline void OPL_STATUS_RESET(FM_OPL *OPL,int flag)
27685571bc7Sbellard {
27785571bc7Sbellard 	/* reset status flag */
27885571bc7Sbellard 	OPL->status &=~flag;
27985571bc7Sbellard 	if((OPL->status & 0x80))
28085571bc7Sbellard 	{
28185571bc7Sbellard 		if (!(OPL->status & OPL->statusmask) )
28285571bc7Sbellard 		{
28385571bc7Sbellard 			OPL->status &= 0x7f;
28485571bc7Sbellard 			/* callback user interrupt handler (IRQ is ON to OFF) */
28585571bc7Sbellard 			if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0);
28685571bc7Sbellard 		}
28785571bc7Sbellard 	}
28885571bc7Sbellard }
28985571bc7Sbellard 
29085571bc7Sbellard /* IRQ mask set */
291*37f6be97SLuiz Capitulino static inline void OPL_STATUSMASK_SET(FM_OPL *OPL,int flag)
29285571bc7Sbellard {
29385571bc7Sbellard 	OPL->statusmask = flag;
29485571bc7Sbellard 	/* IRQ handling check */
29585571bc7Sbellard 	OPL_STATUS_SET(OPL,0);
29685571bc7Sbellard 	OPL_STATUS_RESET(OPL,0);
29785571bc7Sbellard }
29885571bc7Sbellard 
29985571bc7Sbellard /* ----- key on  ----- */
300*37f6be97SLuiz Capitulino static inline void OPL_KEYON(OPL_SLOT *SLOT)
30185571bc7Sbellard {
30285571bc7Sbellard 	/* sin wave restart */
30385571bc7Sbellard 	SLOT->Cnt = 0;
30485571bc7Sbellard 	/* set attack */
30585571bc7Sbellard 	SLOT->evm = ENV_MOD_AR;
30685571bc7Sbellard 	SLOT->evs = SLOT->evsa;
30785571bc7Sbellard 	SLOT->evc = EG_AST;
30885571bc7Sbellard 	SLOT->eve = EG_AED;
30985571bc7Sbellard }
31085571bc7Sbellard /* ----- key off ----- */
311*37f6be97SLuiz Capitulino static inline void OPL_KEYOFF(OPL_SLOT *SLOT)
31285571bc7Sbellard {
31385571bc7Sbellard 	if( SLOT->evm > ENV_MOD_RR)
31485571bc7Sbellard 	{
31585571bc7Sbellard 		/* set envelope counter from envleope output */
31685571bc7Sbellard 		SLOT->evm = ENV_MOD_RR;
31785571bc7Sbellard 		if( !(SLOT->evc&EG_DST) )
31885571bc7Sbellard 			//SLOT->evc = (ENV_CURVE[SLOT->evc>>ENV_BITS]<<ENV_BITS) + EG_DST;
31985571bc7Sbellard 			SLOT->evc = EG_DST;
32085571bc7Sbellard 		SLOT->eve = EG_DED;
32185571bc7Sbellard 		SLOT->evs = SLOT->evsr;
32285571bc7Sbellard 	}
32385571bc7Sbellard }
32485571bc7Sbellard 
32585571bc7Sbellard /* ---------- calcrate Envelope Generator & Phase Generator ---------- */
32685571bc7Sbellard /* return : envelope output */
327*37f6be97SLuiz Capitulino static inline UINT32 OPL_CALC_SLOT( OPL_SLOT *SLOT )
32885571bc7Sbellard {
32985571bc7Sbellard 	/* calcrate envelope generator */
33085571bc7Sbellard 	if( (SLOT->evc+=SLOT->evs) >= SLOT->eve )
33185571bc7Sbellard 	{
33285571bc7Sbellard 		switch( SLOT->evm ){
33385571bc7Sbellard 		case ENV_MOD_AR: /* ATTACK -> DECAY1 */
33485571bc7Sbellard 			/* next DR */
33585571bc7Sbellard 			SLOT->evm = ENV_MOD_DR;
33685571bc7Sbellard 			SLOT->evc = EG_DST;
33785571bc7Sbellard 			SLOT->eve = SLOT->SL;
33885571bc7Sbellard 			SLOT->evs = SLOT->evsd;
33985571bc7Sbellard 			break;
34085571bc7Sbellard 		case ENV_MOD_DR: /* DECAY -> SL or RR */
34185571bc7Sbellard 			SLOT->evc = SLOT->SL;
34285571bc7Sbellard 			SLOT->eve = EG_DED;
34385571bc7Sbellard 			if(SLOT->eg_typ)
34485571bc7Sbellard 			{
34585571bc7Sbellard 				SLOT->evs = 0;
34685571bc7Sbellard 			}
34785571bc7Sbellard 			else
34885571bc7Sbellard 			{
34985571bc7Sbellard 				SLOT->evm = ENV_MOD_RR;
35085571bc7Sbellard 				SLOT->evs = SLOT->evsr;
35185571bc7Sbellard 			}
35285571bc7Sbellard 			break;
35385571bc7Sbellard 		case ENV_MOD_RR: /* RR -> OFF */
35485571bc7Sbellard 			SLOT->evc = EG_OFF;
35585571bc7Sbellard 			SLOT->eve = EG_OFF+1;
35685571bc7Sbellard 			SLOT->evs = 0;
35785571bc7Sbellard 			break;
35885571bc7Sbellard 		}
35985571bc7Sbellard 	}
36085571bc7Sbellard 	/* calcrate envelope */
36185571bc7Sbellard 	return SLOT->TLL+ENV_CURVE[SLOT->evc>>ENV_BITS]+(SLOT->ams ? ams : 0);
36285571bc7Sbellard }
36385571bc7Sbellard 
364c11e80e2SStefan Weil /* set algorithm connection */
365c11e80e2SStefan Weil static void set_algorithm( OPL_CH *CH)
36685571bc7Sbellard {
36785571bc7Sbellard 	INT32 *carrier = &outd[0];
36885571bc7Sbellard 	CH->connect1 = CH->CON ? carrier : &feedback2;
36985571bc7Sbellard 	CH->connect2 = carrier;
37085571bc7Sbellard }
37185571bc7Sbellard 
37285571bc7Sbellard /* ---------- frequency counter for operater update ---------- */
373*37f6be97SLuiz Capitulino static inline void CALC_FCSLOT(OPL_CH *CH,OPL_SLOT *SLOT)
37485571bc7Sbellard {
37585571bc7Sbellard 	int ksr;
37685571bc7Sbellard 
37785571bc7Sbellard 	/* frequency step counter */
37885571bc7Sbellard 	SLOT->Incr = CH->fc * SLOT->mul;
37985571bc7Sbellard 	ksr = CH->kcode >> SLOT->KSR;
38085571bc7Sbellard 
38185571bc7Sbellard 	if( SLOT->ksr != ksr )
38285571bc7Sbellard 	{
38385571bc7Sbellard 		SLOT->ksr = ksr;
38485571bc7Sbellard 		/* attack , decay rate recalcration */
38585571bc7Sbellard 		SLOT->evsa = SLOT->AR[ksr];
38685571bc7Sbellard 		SLOT->evsd = SLOT->DR[ksr];
38785571bc7Sbellard 		SLOT->evsr = SLOT->RR[ksr];
38885571bc7Sbellard 	}
38985571bc7Sbellard 	SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
39085571bc7Sbellard }
39185571bc7Sbellard 
39285571bc7Sbellard /* set multi,am,vib,EG-TYP,KSR,mul */
393*37f6be97SLuiz Capitulino static inline void set_mul(FM_OPL *OPL,int slot,int v)
39485571bc7Sbellard {
39585571bc7Sbellard 	OPL_CH   *CH   = &OPL->P_CH[slot/2];
39685571bc7Sbellard 	OPL_SLOT *SLOT = &CH->SLOT[slot&1];
39785571bc7Sbellard 
39885571bc7Sbellard 	SLOT->mul    = MUL_TABLE[v&0x0f];
39985571bc7Sbellard 	SLOT->KSR    = (v&0x10) ? 0 : 2;
40085571bc7Sbellard 	SLOT->eg_typ = (v&0x20)>>5;
40185571bc7Sbellard 	SLOT->vib    = (v&0x40);
40285571bc7Sbellard 	SLOT->ams    = (v&0x80);
40385571bc7Sbellard 	CALC_FCSLOT(CH,SLOT);
40485571bc7Sbellard }
40585571bc7Sbellard 
40685571bc7Sbellard /* set ksl & tl */
407*37f6be97SLuiz Capitulino static inline void set_ksl_tl(FM_OPL *OPL,int slot,int v)
40885571bc7Sbellard {
40985571bc7Sbellard 	OPL_CH   *CH   = &OPL->P_CH[slot/2];
41085571bc7Sbellard 	OPL_SLOT *SLOT = &CH->SLOT[slot&1];
41185571bc7Sbellard 	int ksl = v>>6; /* 0 / 1.5 / 3 / 6 db/OCT */
41285571bc7Sbellard 
41385571bc7Sbellard 	SLOT->ksl = ksl ? 3-ksl : 31;
41485571bc7Sbellard 	SLOT->TL  = (v&0x3f)*(0.75/EG_STEP); /* 0.75db step */
41585571bc7Sbellard 
41685571bc7Sbellard 	if( !(OPL->mode&0x80) )
41785571bc7Sbellard 	{	/* not CSM latch total level */
41885571bc7Sbellard 		SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
41985571bc7Sbellard 	}
42085571bc7Sbellard }
42185571bc7Sbellard 
42285571bc7Sbellard /* set attack rate & decay rate  */
423*37f6be97SLuiz Capitulino static inline void set_ar_dr(FM_OPL *OPL,int slot,int v)
42485571bc7Sbellard {
42585571bc7Sbellard 	OPL_CH   *CH   = &OPL->P_CH[slot/2];
42685571bc7Sbellard 	OPL_SLOT *SLOT = &CH->SLOT[slot&1];
42785571bc7Sbellard 	int ar = v>>4;
42885571bc7Sbellard 	int dr = v&0x0f;
42985571bc7Sbellard 
43085571bc7Sbellard 	SLOT->AR = ar ? &OPL->AR_TABLE[ar<<2] : RATE_0;
43185571bc7Sbellard 	SLOT->evsa = SLOT->AR[SLOT->ksr];
43285571bc7Sbellard 	if( SLOT->evm == ENV_MOD_AR ) SLOT->evs = SLOT->evsa;
43385571bc7Sbellard 
43485571bc7Sbellard 	SLOT->DR = dr ? &OPL->DR_TABLE[dr<<2] : RATE_0;
43585571bc7Sbellard 	SLOT->evsd = SLOT->DR[SLOT->ksr];
43685571bc7Sbellard 	if( SLOT->evm == ENV_MOD_DR ) SLOT->evs = SLOT->evsd;
43785571bc7Sbellard }
43885571bc7Sbellard 
43985571bc7Sbellard /* set sustain level & release rate */
440*37f6be97SLuiz Capitulino static inline void set_sl_rr(FM_OPL *OPL,int slot,int v)
44185571bc7Sbellard {
44285571bc7Sbellard 	OPL_CH   *CH   = &OPL->P_CH[slot/2];
44385571bc7Sbellard 	OPL_SLOT *SLOT = &CH->SLOT[slot&1];
44485571bc7Sbellard 	int sl = v>>4;
44585571bc7Sbellard 	int rr = v & 0x0f;
44685571bc7Sbellard 
44785571bc7Sbellard 	SLOT->SL = SL_TABLE[sl];
44885571bc7Sbellard 	if( SLOT->evm == ENV_MOD_DR ) SLOT->eve = SLOT->SL;
44985571bc7Sbellard 	SLOT->RR = &OPL->DR_TABLE[rr<<2];
45085571bc7Sbellard 	SLOT->evsr = SLOT->RR[SLOT->ksr];
45185571bc7Sbellard 	if( SLOT->evm == ENV_MOD_RR ) SLOT->evs = SLOT->evsr;
45285571bc7Sbellard }
45385571bc7Sbellard 
45485571bc7Sbellard /* operator output calcrator */
45585571bc7Sbellard #define OP_OUT(slot,env,con)   slot->wavetable[((slot->Cnt+con)/(0x1000000/SIN_ENT))&(SIN_ENT-1)][env]
45685571bc7Sbellard /* ---------- calcrate one of channel ---------- */
457*37f6be97SLuiz Capitulino static inline void OPL_CALC_CH( OPL_CH *CH )
45885571bc7Sbellard {
45985571bc7Sbellard 	UINT32 env_out;
46085571bc7Sbellard 	OPL_SLOT *SLOT;
46185571bc7Sbellard 
46285571bc7Sbellard 	feedback2 = 0;
46385571bc7Sbellard 	/* SLOT 1 */
46485571bc7Sbellard 	SLOT = &CH->SLOT[SLOT1];
46585571bc7Sbellard 	env_out=OPL_CALC_SLOT(SLOT);
46685571bc7Sbellard 	if( env_out < EG_ENT-1 )
46785571bc7Sbellard 	{
46885571bc7Sbellard 		/* PG */
46985571bc7Sbellard 		if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
47085571bc7Sbellard 		else          SLOT->Cnt += SLOT->Incr;
47185571bc7Sbellard 		/* connectoion */
47285571bc7Sbellard 		if(CH->FB)
47385571bc7Sbellard 		{
47485571bc7Sbellard 			int feedback1 = (CH->op1_out[0]+CH->op1_out[1])>>CH->FB;
47585571bc7Sbellard 			CH->op1_out[1] = CH->op1_out[0];
47685571bc7Sbellard 			*CH->connect1 += CH->op1_out[0] = OP_OUT(SLOT,env_out,feedback1);
47785571bc7Sbellard 		}
47885571bc7Sbellard 		else
47985571bc7Sbellard 		{
48085571bc7Sbellard 			*CH->connect1 += OP_OUT(SLOT,env_out,0);
48185571bc7Sbellard 		}
48285571bc7Sbellard 	}else
48385571bc7Sbellard 	{
48485571bc7Sbellard 		CH->op1_out[1] = CH->op1_out[0];
48585571bc7Sbellard 		CH->op1_out[0] = 0;
48685571bc7Sbellard 	}
48785571bc7Sbellard 	/* SLOT 2 */
48885571bc7Sbellard 	SLOT = &CH->SLOT[SLOT2];
48985571bc7Sbellard 	env_out=OPL_CALC_SLOT(SLOT);
49085571bc7Sbellard 	if( env_out < EG_ENT-1 )
49185571bc7Sbellard 	{
49285571bc7Sbellard 		/* PG */
49385571bc7Sbellard 		if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
49485571bc7Sbellard 		else          SLOT->Cnt += SLOT->Incr;
49585571bc7Sbellard 		/* connectoion */
49685571bc7Sbellard 		outd[0] += OP_OUT(SLOT,env_out, feedback2);
49785571bc7Sbellard 	}
49885571bc7Sbellard }
49985571bc7Sbellard 
500c11e80e2SStefan Weil /* ---------- calcrate rhythm block ---------- */
50185571bc7Sbellard #define WHITE_NOISE_db 6.0
502*37f6be97SLuiz Capitulino static inline void OPL_CALC_RH( OPL_CH *CH )
50385571bc7Sbellard {
50485571bc7Sbellard 	UINT32 env_tam,env_sd,env_top,env_hh;
50585571bc7Sbellard 	int whitenoise = (rand()&1)*(WHITE_NOISE_db/EG_STEP);
50685571bc7Sbellard 	INT32 tone8;
50785571bc7Sbellard 
50885571bc7Sbellard 	OPL_SLOT *SLOT;
50985571bc7Sbellard 	int env_out;
51085571bc7Sbellard 
51185571bc7Sbellard 	/* BD : same as FM serial mode and output level is large */
51285571bc7Sbellard 	feedback2 = 0;
51385571bc7Sbellard 	/* SLOT 1 */
51485571bc7Sbellard 	SLOT = &CH[6].SLOT[SLOT1];
51585571bc7Sbellard 	env_out=OPL_CALC_SLOT(SLOT);
51685571bc7Sbellard 	if( env_out < EG_ENT-1 )
51785571bc7Sbellard 	{
51885571bc7Sbellard 		/* PG */
51985571bc7Sbellard 		if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
52085571bc7Sbellard 		else          SLOT->Cnt += SLOT->Incr;
52185571bc7Sbellard 		/* connectoion */
52285571bc7Sbellard 		if(CH[6].FB)
52385571bc7Sbellard 		{
52485571bc7Sbellard 			int feedback1 = (CH[6].op1_out[0]+CH[6].op1_out[1])>>CH[6].FB;
52585571bc7Sbellard 			CH[6].op1_out[1] = CH[6].op1_out[0];
52685571bc7Sbellard 			feedback2 = CH[6].op1_out[0] = OP_OUT(SLOT,env_out,feedback1);
52785571bc7Sbellard 		}
52885571bc7Sbellard 		else
52985571bc7Sbellard 		{
53085571bc7Sbellard 			feedback2 = OP_OUT(SLOT,env_out,0);
53185571bc7Sbellard 		}
53285571bc7Sbellard 	}else
53385571bc7Sbellard 	{
53485571bc7Sbellard 		feedback2 = 0;
53585571bc7Sbellard 		CH[6].op1_out[1] = CH[6].op1_out[0];
53685571bc7Sbellard 		CH[6].op1_out[0] = 0;
53785571bc7Sbellard 	}
53885571bc7Sbellard 	/* SLOT 2 */
53985571bc7Sbellard 	SLOT = &CH[6].SLOT[SLOT2];
54085571bc7Sbellard 	env_out=OPL_CALC_SLOT(SLOT);
54185571bc7Sbellard 	if( env_out < EG_ENT-1 )
54285571bc7Sbellard 	{
54385571bc7Sbellard 		/* PG */
54485571bc7Sbellard 		if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
54585571bc7Sbellard 		else          SLOT->Cnt += SLOT->Incr;
54685571bc7Sbellard 		/* connectoion */
54785571bc7Sbellard 		outd[0] += OP_OUT(SLOT,env_out, feedback2)*2;
54885571bc7Sbellard 	}
54985571bc7Sbellard 
55085571bc7Sbellard 	// SD  (17) = mul14[fnum7] + white noise
55185571bc7Sbellard 	// TAM (15) = mul15[fnum8]
55285571bc7Sbellard 	// TOP (18) = fnum6(mul18[fnum8]+whitenoise)
55385571bc7Sbellard 	// HH  (14) = fnum7(mul18[fnum8]+whitenoise) + white noise
55485571bc7Sbellard 	env_sd =OPL_CALC_SLOT(SLOT7_2) + whitenoise;
55585571bc7Sbellard 	env_tam=OPL_CALC_SLOT(SLOT8_1);
55685571bc7Sbellard 	env_top=OPL_CALC_SLOT(SLOT8_2);
55785571bc7Sbellard 	env_hh =OPL_CALC_SLOT(SLOT7_1) + whitenoise;
55885571bc7Sbellard 
55985571bc7Sbellard 	/* PG */
56085571bc7Sbellard 	if(SLOT7_1->vib) SLOT7_1->Cnt += (2*SLOT7_1->Incr*vib/VIB_RATE);
56185571bc7Sbellard 	else             SLOT7_1->Cnt += 2*SLOT7_1->Incr;
56285571bc7Sbellard 	if(SLOT7_2->vib) SLOT7_2->Cnt += ((CH[7].fc*8)*vib/VIB_RATE);
56385571bc7Sbellard 	else             SLOT7_2->Cnt += (CH[7].fc*8);
56485571bc7Sbellard 	if(SLOT8_1->vib) SLOT8_1->Cnt += (SLOT8_1->Incr*vib/VIB_RATE);
56585571bc7Sbellard 	else             SLOT8_1->Cnt += SLOT8_1->Incr;
56685571bc7Sbellard 	if(SLOT8_2->vib) SLOT8_2->Cnt += ((CH[8].fc*48)*vib/VIB_RATE);
56785571bc7Sbellard 	else             SLOT8_2->Cnt += (CH[8].fc*48);
56885571bc7Sbellard 
56985571bc7Sbellard 	tone8 = OP_OUT(SLOT8_2,whitenoise,0 );
57085571bc7Sbellard 
57185571bc7Sbellard 	/* SD */
57285571bc7Sbellard 	if( env_sd < EG_ENT-1 )
57385571bc7Sbellard 		outd[0] += OP_OUT(SLOT7_1,env_sd, 0)*8;
57485571bc7Sbellard 	/* TAM */
57585571bc7Sbellard 	if( env_tam < EG_ENT-1 )
57685571bc7Sbellard 		outd[0] += OP_OUT(SLOT8_1,env_tam, 0)*2;
57785571bc7Sbellard 	/* TOP-CY */
57885571bc7Sbellard 	if( env_top < EG_ENT-1 )
57985571bc7Sbellard 		outd[0] += OP_OUT(SLOT7_2,env_top,tone8)*2;
58085571bc7Sbellard 	/* HH */
58185571bc7Sbellard 	if( env_hh  < EG_ENT-1 )
58285571bc7Sbellard 		outd[0] += OP_OUT(SLOT7_2,env_hh,tone8)*2;
58385571bc7Sbellard }
58485571bc7Sbellard 
58585571bc7Sbellard /* ----------- initialize time tabls ----------- */
58685571bc7Sbellard static void init_timetables( FM_OPL *OPL , int ARRATE , int DRRATE )
58785571bc7Sbellard {
58885571bc7Sbellard 	int i;
58985571bc7Sbellard 	double rate;
59085571bc7Sbellard 
59185571bc7Sbellard 	/* make attack rate & decay rate tables */
59285571bc7Sbellard 	for (i = 0;i < 4;i++) OPL->AR_TABLE[i] = OPL->DR_TABLE[i] = 0;
59385571bc7Sbellard 	for (i = 4;i <= 60;i++){
59485571bc7Sbellard 		rate  = OPL->freqbase;						/* frequency rate */
59585571bc7Sbellard 		if( i < 60 ) rate *= 1.0+(i&3)*0.25;		/* b0-1 : x1 , x1.25 , x1.5 , x1.75 */
59685571bc7Sbellard 		rate *= 1<<((i>>2)-1);						/* b2-5 : shift bit */
59785571bc7Sbellard 		rate *= (double)(EG_ENT<<ENV_BITS);
59885571bc7Sbellard 		OPL->AR_TABLE[i] = rate / ARRATE;
59985571bc7Sbellard 		OPL->DR_TABLE[i] = rate / DRRATE;
60085571bc7Sbellard 	}
601913895abSStefan Weil 	for (i = 60; i < ARRAY_SIZE(OPL->AR_TABLE); i++)
60285571bc7Sbellard 	{
60385571bc7Sbellard 		OPL->AR_TABLE[i] = EG_AED-1;
60485571bc7Sbellard 		OPL->DR_TABLE[i] = OPL->DR_TABLE[60];
60585571bc7Sbellard 	}
60685571bc7Sbellard #if 0
60785571bc7Sbellard 	for (i = 0;i < 64 ;i++){	/* make for overflow area */
60885571bc7Sbellard 		LOG(LOG_WAR, ("rate %2d , ar %f ms , dr %f ms\n", i,
60985571bc7Sbellard 			((double)(EG_ENT<<ENV_BITS) / OPL->AR_TABLE[i]) * (1000.0 / OPL->rate),
61085571bc7Sbellard 			((double)(EG_ENT<<ENV_BITS) / OPL->DR_TABLE[i]) * (1000.0 / OPL->rate) ));
61185571bc7Sbellard 	}
61285571bc7Sbellard #endif
61385571bc7Sbellard }
61485571bc7Sbellard 
61585571bc7Sbellard /* ---------- generic table initialize ---------- */
61685571bc7Sbellard static int OPLOpenTable( void )
61785571bc7Sbellard {
61885571bc7Sbellard 	int s,t;
61985571bc7Sbellard 	double rate;
62085571bc7Sbellard 	int i,j;
62185571bc7Sbellard 	double pom;
62285571bc7Sbellard 
62385571bc7Sbellard 	/* allocate dynamic tables */
624809c130cSaliguori 	if( (TL_TABLE = malloc(TL_MAX*2*sizeof(INT32))) == NULL)
625809c130cSaliguori 		return 0;
626809c130cSaliguori 	if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(INT32 *))) == NULL)
627809c130cSaliguori 	{
628809c130cSaliguori 		free(TL_TABLE);
629809c130cSaliguori 		return 0;
630809c130cSaliguori 	}
631809c130cSaliguori 	if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(INT32))) == NULL)
632809c130cSaliguori 	{
633809c130cSaliguori 		free(TL_TABLE);
634809c130cSaliguori 		free(SIN_TABLE);
635809c130cSaliguori 		return 0;
636809c130cSaliguori 	}
637809c130cSaliguori 	if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(INT32))) == NULL)
638809c130cSaliguori 	{
639809c130cSaliguori 		free(TL_TABLE);
640809c130cSaliguori 		free(SIN_TABLE);
641809c130cSaliguori 		free(AMS_TABLE);
642809c130cSaliguori 		return 0;
643809c130cSaliguori 	}
64485571bc7Sbellard 	/* make total level table */
64585571bc7Sbellard 	for (t = 0;t < EG_ENT-1 ;t++){
64685571bc7Sbellard 		rate = ((1<<TL_BITS)-1)/pow(10,EG_STEP*t/20);	/* dB -> voltage */
64785571bc7Sbellard 		TL_TABLE[       t] =  (int)rate;
64885571bc7Sbellard 		TL_TABLE[TL_MAX+t] = -TL_TABLE[t];
64985571bc7Sbellard /*		LOG(LOG_INF,("TotalLevel(%3d) = %x\n",t,TL_TABLE[t]));*/
65085571bc7Sbellard 	}
65185571bc7Sbellard 	/* fill volume off area */
65285571bc7Sbellard 	for ( t = EG_ENT-1; t < TL_MAX ;t++){
65385571bc7Sbellard 		TL_TABLE[t] = TL_TABLE[TL_MAX+t] = 0;
65485571bc7Sbellard 	}
65585571bc7Sbellard 
65685571bc7Sbellard 	/* make sinwave table (total level offet) */
65785571bc7Sbellard 	/* degree 0 = degree 180                   = off */
65885571bc7Sbellard 	SIN_TABLE[0] = SIN_TABLE[SIN_ENT/2]         = &TL_TABLE[EG_ENT-1];
65985571bc7Sbellard 	for (s = 1;s <= SIN_ENT/4;s++){
66085571bc7Sbellard 		pom = sin(2*PI*s/SIN_ENT); /* sin     */
66185571bc7Sbellard 		pom = 20*log10(1/pom);	   /* decibel */
66285571bc7Sbellard 		j = pom / EG_STEP;         /* TL_TABLE steps */
66385571bc7Sbellard 
66485571bc7Sbellard         /* degree 0   -  90    , degree 180 -  90 : plus section */
66585571bc7Sbellard 		SIN_TABLE[          s] = SIN_TABLE[SIN_ENT/2-s] = &TL_TABLE[j];
66685571bc7Sbellard         /* degree 180 - 270    , degree 360 - 270 : minus section */
66785571bc7Sbellard 		SIN_TABLE[SIN_ENT/2+s] = SIN_TABLE[SIN_ENT  -s] = &TL_TABLE[TL_MAX+j];
66885571bc7Sbellard /*		LOG(LOG_INF,("sin(%3d) = %f:%f db\n",s,pom,(double)j * EG_STEP));*/
66985571bc7Sbellard 	}
67085571bc7Sbellard 	for (s = 0;s < SIN_ENT;s++)
67185571bc7Sbellard 	{
67285571bc7Sbellard 		SIN_TABLE[SIN_ENT*1+s] = s<(SIN_ENT/2) ? SIN_TABLE[s] : &TL_TABLE[EG_ENT];
67385571bc7Sbellard 		SIN_TABLE[SIN_ENT*2+s] = SIN_TABLE[s % (SIN_ENT/2)];
67485571bc7Sbellard 		SIN_TABLE[SIN_ENT*3+s] = (s/(SIN_ENT/4))&1 ? &TL_TABLE[EG_ENT] : SIN_TABLE[SIN_ENT*2+s];
67585571bc7Sbellard 	}
67685571bc7Sbellard 
67785571bc7Sbellard 	/* envelope counter -> envelope output table */
67885571bc7Sbellard 	for (i=0; i<EG_ENT; i++)
67985571bc7Sbellard 	{
68085571bc7Sbellard 		/* ATTACK curve */
68185571bc7Sbellard 		pom = pow( ((double)(EG_ENT-1-i)/EG_ENT) , 8 ) * EG_ENT;
68285571bc7Sbellard 		/* if( pom >= EG_ENT ) pom = EG_ENT-1; */
68385571bc7Sbellard 		ENV_CURVE[i] = (int)pom;
68485571bc7Sbellard 		/* DECAY ,RELEASE curve */
68585571bc7Sbellard 		ENV_CURVE[(EG_DST>>ENV_BITS)+i]= i;
68685571bc7Sbellard 	}
68785571bc7Sbellard 	/* off */
68885571bc7Sbellard 	ENV_CURVE[EG_OFF>>ENV_BITS]= EG_ENT-1;
68985571bc7Sbellard 	/* make LFO ams table */
69085571bc7Sbellard 	for (i=0; i<AMS_ENT; i++)
69185571bc7Sbellard 	{
69285571bc7Sbellard 		pom = (1.0+sin(2*PI*i/AMS_ENT))/2; /* sin */
69385571bc7Sbellard 		AMS_TABLE[i]         = (1.0/EG_STEP)*pom; /* 1dB   */
69485571bc7Sbellard 		AMS_TABLE[AMS_ENT+i] = (4.8/EG_STEP)*pom; /* 4.8dB */
69585571bc7Sbellard 	}
69685571bc7Sbellard 	/* make LFO vibrate table */
69785571bc7Sbellard 	for (i=0; i<VIB_ENT; i++)
69885571bc7Sbellard 	{
69985571bc7Sbellard 		/* 100cent = 1seminote = 6% ?? */
70085571bc7Sbellard 		pom = (double)VIB_RATE*0.06*sin(2*PI*i/VIB_ENT); /* +-100sect step */
70185571bc7Sbellard 		VIB_TABLE[i]         = VIB_RATE + (pom*0.07); /* +- 7cent */
70285571bc7Sbellard 		VIB_TABLE[VIB_ENT+i] = VIB_RATE + (pom*0.14); /* +-14cent */
70385571bc7Sbellard 		/* LOG(LOG_INF,("vib %d=%d\n",i,VIB_TABLE[VIB_ENT+i])); */
70485571bc7Sbellard 	}
70585571bc7Sbellard 	return 1;
70685571bc7Sbellard }
70785571bc7Sbellard 
70885571bc7Sbellard 
70985571bc7Sbellard static void OPLCloseTable( void )
71085571bc7Sbellard {
71185571bc7Sbellard 	free(TL_TABLE);
71285571bc7Sbellard 	free(SIN_TABLE);
71385571bc7Sbellard 	free(AMS_TABLE);
71485571bc7Sbellard 	free(VIB_TABLE);
71585571bc7Sbellard }
71685571bc7Sbellard 
71766a0a2cbSDong Xu Wang /* CSM Key Control */
718*37f6be97SLuiz Capitulino static inline void CSMKeyControll(OPL_CH *CH)
71985571bc7Sbellard {
72085571bc7Sbellard 	OPL_SLOT *slot1 = &CH->SLOT[SLOT1];
72185571bc7Sbellard 	OPL_SLOT *slot2 = &CH->SLOT[SLOT2];
72285571bc7Sbellard 	/* all key off */
72385571bc7Sbellard 	OPL_KEYOFF(slot1);
72485571bc7Sbellard 	OPL_KEYOFF(slot2);
72585571bc7Sbellard 	/* total level latch */
72685571bc7Sbellard 	slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
72785571bc7Sbellard 	slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
72885571bc7Sbellard 	/* key on */
72985571bc7Sbellard 	CH->op1_out[0] = CH->op1_out[1] = 0;
73085571bc7Sbellard 	OPL_KEYON(slot1);
73185571bc7Sbellard 	OPL_KEYON(slot2);
73285571bc7Sbellard }
73385571bc7Sbellard 
73485571bc7Sbellard /* ---------- opl initialize ---------- */
73531de8314SStefan Weil static void OPL_initialize(FM_OPL *OPL)
73685571bc7Sbellard {
73785571bc7Sbellard 	int fn;
73885571bc7Sbellard 
73985571bc7Sbellard 	/* frequency base */
74085571bc7Sbellard 	OPL->freqbase = (OPL->rate) ? ((double)OPL->clock / OPL->rate) / 72  : 0;
74185571bc7Sbellard 	/* Timer base time */
74285571bc7Sbellard 	OPL->TimerBase = 1.0/((double)OPL->clock / 72.0 );
74385571bc7Sbellard 	/* make time tables */
74485571bc7Sbellard 	init_timetables( OPL , OPL_ARRATE , OPL_DRRATE );
74585571bc7Sbellard 	/* make fnumber -> increment counter table */
74685571bc7Sbellard 	for( fn=0 ; fn < 1024 ; fn++ )
74785571bc7Sbellard 	{
74885571bc7Sbellard 		OPL->FN_TABLE[fn] = OPL->freqbase * fn * FREQ_RATE * (1<<7) / 2;
74985571bc7Sbellard 	}
75085571bc7Sbellard 	/* LFO freq.table */
75185571bc7Sbellard 	OPL->amsIncr = OPL->rate ? (double)AMS_ENT*(1<<AMS_SHIFT) / OPL->rate * 3.7 * ((double)OPL->clock/3600000) : 0;
75285571bc7Sbellard 	OPL->vibIncr = OPL->rate ? (double)VIB_ENT*(1<<VIB_SHIFT) / OPL->rate * 6.4 * ((double)OPL->clock/3600000) : 0;
75385571bc7Sbellard }
75485571bc7Sbellard 
75585571bc7Sbellard /* ---------- write a OPL registers ---------- */
75685571bc7Sbellard static void OPLWriteReg(FM_OPL *OPL, int r, int v)
75785571bc7Sbellard {
75885571bc7Sbellard 	OPL_CH *CH;
75985571bc7Sbellard 	int slot;
76085571bc7Sbellard 	int block_fnum;
76185571bc7Sbellard 
76285571bc7Sbellard 	switch(r&0xe0)
76385571bc7Sbellard 	{
76466a0a2cbSDong Xu Wang 	case 0x00: /* 00-1f:control */
76585571bc7Sbellard 		switch(r&0x1f)
76685571bc7Sbellard 		{
76785571bc7Sbellard 		case 0x01:
76885571bc7Sbellard 			/* wave selector enable */
76985571bc7Sbellard 			if(OPL->type&OPL_TYPE_WAVESEL)
77085571bc7Sbellard 			{
77185571bc7Sbellard 				OPL->wavesel = v&0x20;
77285571bc7Sbellard 				if(!OPL->wavesel)
77385571bc7Sbellard 				{
77485571bc7Sbellard 					/* preset compatible mode */
77585571bc7Sbellard 					int c;
77685571bc7Sbellard 					for(c=0;c<OPL->max_ch;c++)
77785571bc7Sbellard 					{
77885571bc7Sbellard 						OPL->P_CH[c].SLOT[SLOT1].wavetable = &SIN_TABLE[0];
77985571bc7Sbellard 						OPL->P_CH[c].SLOT[SLOT2].wavetable = &SIN_TABLE[0];
78085571bc7Sbellard 					}
78185571bc7Sbellard 				}
78285571bc7Sbellard 			}
78385571bc7Sbellard 			return;
78485571bc7Sbellard 		case 0x02:	/* Timer 1 */
78585571bc7Sbellard 			OPL->T[0] = (256-v)*4;
78685571bc7Sbellard 			break;
78785571bc7Sbellard 		case 0x03:	/* Timer 2 */
78885571bc7Sbellard 			OPL->T[1] = (256-v)*16;
78985571bc7Sbellard 			return;
79085571bc7Sbellard 		case 0x04:	/* IRQ clear / mask and Timer enable */
79185571bc7Sbellard 			if(v&0x80)
79285571bc7Sbellard 			{	/* IRQ flag clear */
79385571bc7Sbellard 				OPL_STATUS_RESET(OPL,0x7f);
79485571bc7Sbellard 			}
79585571bc7Sbellard 			else
79685571bc7Sbellard 			{	/* set IRQ mask ,timer enable*/
79785571bc7Sbellard 				UINT8 st1 = v&1;
79885571bc7Sbellard 				UINT8 st2 = (v>>1)&1;
79985571bc7Sbellard 				/* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
80085571bc7Sbellard 				OPL_STATUS_RESET(OPL,v&0x78);
80185571bc7Sbellard 				OPL_STATUSMASK_SET(OPL,((~v)&0x78)|0x01);
80285571bc7Sbellard 				/* timer 2 */
80385571bc7Sbellard 				if(OPL->st[1] != st2)
80485571bc7Sbellard 				{
80585571bc7Sbellard 					double interval = st2 ? (double)OPL->T[1]*OPL->TimerBase : 0.0;
80685571bc7Sbellard 					OPL->st[1] = st2;
80785571bc7Sbellard 					if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+1,interval);
80885571bc7Sbellard 				}
80985571bc7Sbellard 				/* timer 1 */
81085571bc7Sbellard 				if(OPL->st[0] != st1)
81185571bc7Sbellard 				{
81285571bc7Sbellard 					double interval = st1 ? (double)OPL->T[0]*OPL->TimerBase : 0.0;
81385571bc7Sbellard 					OPL->st[0] = st1;
81485571bc7Sbellard 					if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+0,interval);
81585571bc7Sbellard 				}
81685571bc7Sbellard 			}
81785571bc7Sbellard 			return;
81885571bc7Sbellard #if BUILD_Y8950
81985571bc7Sbellard 		case 0x06:		/* Key Board OUT */
82085571bc7Sbellard 			if(OPL->type&OPL_TYPE_KEYBOARD)
82185571bc7Sbellard 			{
82285571bc7Sbellard 				if(OPL->keyboardhandler_w)
82385571bc7Sbellard 					OPL->keyboardhandler_w(OPL->keyboard_param,v);
82485571bc7Sbellard 				else
82585571bc7Sbellard 					LOG(LOG_WAR,("OPL:write unmapped KEYBOARD port\n"));
82685571bc7Sbellard 			}
82785571bc7Sbellard 			return;
82866a0a2cbSDong Xu Wang 		case 0x07:	/* DELTA-T control : START,REC,MEMDATA,REPT,SPOFF,x,x,RST */
82985571bc7Sbellard 			if(OPL->type&OPL_TYPE_ADPCM)
83085571bc7Sbellard 				YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
83185571bc7Sbellard 			return;
83285571bc7Sbellard 		case 0x08:	/* MODE,DELTA-T : CSM,NOTESEL,x,x,smpl,da/ad,64k,rom */
83385571bc7Sbellard 			OPL->mode = v;
83485571bc7Sbellard 			v&=0x1f;	/* for DELTA-T unit */
83585571bc7Sbellard 		case 0x09:		/* START ADD */
83685571bc7Sbellard 		case 0x0a:
83785571bc7Sbellard 		case 0x0b:		/* STOP ADD  */
83885571bc7Sbellard 		case 0x0c:
83985571bc7Sbellard 		case 0x0d:		/* PRESCALE   */
84085571bc7Sbellard 		case 0x0e:
84185571bc7Sbellard 		case 0x0f:		/* ADPCM data */
84285571bc7Sbellard 		case 0x10: 		/* DELTA-N    */
84385571bc7Sbellard 		case 0x11: 		/* DELTA-N    */
84485571bc7Sbellard 		case 0x12: 		/* EG-CTRL    */
84585571bc7Sbellard 			if(OPL->type&OPL_TYPE_ADPCM)
84685571bc7Sbellard 				YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
84785571bc7Sbellard 			return;
84885571bc7Sbellard #if 0
84985571bc7Sbellard 		case 0x15:		/* DAC data    */
85085571bc7Sbellard 		case 0x16:
85185571bc7Sbellard 		case 0x17:		/* SHIFT    */
85285571bc7Sbellard 			return;
85385571bc7Sbellard 		case 0x18:		/* I/O CTRL (Direction) */
85485571bc7Sbellard 			if(OPL->type&OPL_TYPE_IO)
85585571bc7Sbellard 				OPL->portDirection = v&0x0f;
85685571bc7Sbellard 			return;
85785571bc7Sbellard 		case 0x19:		/* I/O DATA */
85885571bc7Sbellard 			if(OPL->type&OPL_TYPE_IO)
85985571bc7Sbellard 			{
86085571bc7Sbellard 				OPL->portLatch = v;
86185571bc7Sbellard 				if(OPL->porthandler_w)
86285571bc7Sbellard 					OPL->porthandler_w(OPL->port_param,v&OPL->portDirection);
86385571bc7Sbellard 			}
86485571bc7Sbellard 			return;
86585571bc7Sbellard 		case 0x1a:		/* PCM data */
86685571bc7Sbellard 			return;
86785571bc7Sbellard #endif
86885571bc7Sbellard #endif
86985571bc7Sbellard 		}
87085571bc7Sbellard 		break;
87185571bc7Sbellard 	case 0x20:	/* am,vib,ksr,eg type,mul */
87285571bc7Sbellard 		slot = slot_array[r&0x1f];
87385571bc7Sbellard 		if(slot == -1) return;
87485571bc7Sbellard 		set_mul(OPL,slot,v);
87585571bc7Sbellard 		return;
87685571bc7Sbellard 	case 0x40:
87785571bc7Sbellard 		slot = slot_array[r&0x1f];
87885571bc7Sbellard 		if(slot == -1) return;
87985571bc7Sbellard 		set_ksl_tl(OPL,slot,v);
88085571bc7Sbellard 		return;
88185571bc7Sbellard 	case 0x60:
88285571bc7Sbellard 		slot = slot_array[r&0x1f];
88385571bc7Sbellard 		if(slot == -1) return;
88485571bc7Sbellard 		set_ar_dr(OPL,slot,v);
88585571bc7Sbellard 		return;
88685571bc7Sbellard 	case 0x80:
88785571bc7Sbellard 		slot = slot_array[r&0x1f];
88885571bc7Sbellard 		if(slot == -1) return;
88985571bc7Sbellard 		set_sl_rr(OPL,slot,v);
89085571bc7Sbellard 		return;
89185571bc7Sbellard 	case 0xa0:
89285571bc7Sbellard 		switch(r)
89385571bc7Sbellard 		{
89485571bc7Sbellard 		case 0xbd:
89585571bc7Sbellard 			/* amsep,vibdep,r,bd,sd,tom,tc,hh */
89685571bc7Sbellard 			{
897c11e80e2SStefan Weil 			UINT8 rkey = OPL->rhythm^v;
89885571bc7Sbellard 			OPL->ams_table = &AMS_TABLE[v&0x80 ? AMS_ENT : 0];
89985571bc7Sbellard 			OPL->vib_table = &VIB_TABLE[v&0x40 ? VIB_ENT : 0];
900c11e80e2SStefan Weil 			OPL->rhythm  = v&0x3f;
901c11e80e2SStefan Weil 			if(OPL->rhythm&0x20)
90285571bc7Sbellard 			{
90385571bc7Sbellard #if 0
904c11e80e2SStefan Weil 				usrintf_showmessage("OPL Rhythm mode select");
90585571bc7Sbellard #endif
90685571bc7Sbellard 				/* BD key on/off */
90785571bc7Sbellard 				if(rkey&0x10)
90885571bc7Sbellard 				{
90985571bc7Sbellard 					if(v&0x10)
91085571bc7Sbellard 					{
91185571bc7Sbellard 						OPL->P_CH[6].op1_out[0] = OPL->P_CH[6].op1_out[1] = 0;
91285571bc7Sbellard 						OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT1]);
91385571bc7Sbellard 						OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT2]);
91485571bc7Sbellard 					}
91585571bc7Sbellard 					else
91685571bc7Sbellard 					{
91785571bc7Sbellard 						OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1]);
91885571bc7Sbellard 						OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2]);
91985571bc7Sbellard 					}
92085571bc7Sbellard 				}
92185571bc7Sbellard 				/* SD key on/off */
92285571bc7Sbellard 				if(rkey&0x08)
92385571bc7Sbellard 				{
92485571bc7Sbellard 					if(v&0x08) OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT2]);
92585571bc7Sbellard 					else       OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2]);
92685571bc7Sbellard 				}/* TAM key on/off */
92785571bc7Sbellard 				if(rkey&0x04)
92885571bc7Sbellard 				{
92985571bc7Sbellard 					if(v&0x04) OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT1]);
93085571bc7Sbellard 					else       OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1]);
93185571bc7Sbellard 				}
93285571bc7Sbellard 				/* TOP-CY key on/off */
93385571bc7Sbellard 				if(rkey&0x02)
93485571bc7Sbellard 				{
93585571bc7Sbellard 					if(v&0x02) OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT2]);
93685571bc7Sbellard 					else       OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2]);
93785571bc7Sbellard 				}
93885571bc7Sbellard 				/* HH key on/off */
93985571bc7Sbellard 				if(rkey&0x01)
94085571bc7Sbellard 				{
94185571bc7Sbellard 					if(v&0x01) OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT1]);
94285571bc7Sbellard 					else       OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1]);
94385571bc7Sbellard 				}
94485571bc7Sbellard 			}
94585571bc7Sbellard 			}
94685571bc7Sbellard 			return;
94785571bc7Sbellard 		}
94885571bc7Sbellard 		/* keyon,block,fnum */
94985571bc7Sbellard 		if( (r&0x0f) > 8) return;
95085571bc7Sbellard 		CH = &OPL->P_CH[r&0x0f];
95185571bc7Sbellard 		if(!(r&0x10))
95285571bc7Sbellard 		{	/* a0-a8 */
95385571bc7Sbellard 			block_fnum  = (CH->block_fnum&0x1f00) | v;
95485571bc7Sbellard 		}
95585571bc7Sbellard 		else
95685571bc7Sbellard 		{	/* b0-b8 */
95785571bc7Sbellard 			int keyon = (v>>5)&1;
95885571bc7Sbellard 			block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff);
95985571bc7Sbellard 			if(CH->keyon != keyon)
96085571bc7Sbellard 			{
96185571bc7Sbellard 				if( (CH->keyon=keyon) )
96285571bc7Sbellard 				{
96385571bc7Sbellard 					CH->op1_out[0] = CH->op1_out[1] = 0;
96485571bc7Sbellard 					OPL_KEYON(&CH->SLOT[SLOT1]);
96585571bc7Sbellard 					OPL_KEYON(&CH->SLOT[SLOT2]);
96685571bc7Sbellard 				}
96785571bc7Sbellard 				else
96885571bc7Sbellard 				{
96985571bc7Sbellard 					OPL_KEYOFF(&CH->SLOT[SLOT1]);
97085571bc7Sbellard 					OPL_KEYOFF(&CH->SLOT[SLOT2]);
97185571bc7Sbellard 				}
97285571bc7Sbellard 			}
97385571bc7Sbellard 		}
97485571bc7Sbellard 		/* update */
97585571bc7Sbellard 		if(CH->block_fnum != block_fnum)
97685571bc7Sbellard 		{
97785571bc7Sbellard 			int blockRv = 7-(block_fnum>>10);
97885571bc7Sbellard 			int fnum   = block_fnum&0x3ff;
97985571bc7Sbellard 			CH->block_fnum = block_fnum;
98085571bc7Sbellard 
98185571bc7Sbellard 			CH->ksl_base = KSL_TABLE[block_fnum>>6];
98285571bc7Sbellard 			CH->fc = OPL->FN_TABLE[fnum]>>blockRv;
98385571bc7Sbellard 			CH->kcode = CH->block_fnum>>9;
98485571bc7Sbellard 			if( (OPL->mode&0x40) && CH->block_fnum&0x100) CH->kcode |=1;
98585571bc7Sbellard 			CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
98685571bc7Sbellard 			CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
98785571bc7Sbellard 		}
98885571bc7Sbellard 		return;
98985571bc7Sbellard 	case 0xc0:
99085571bc7Sbellard 		/* FB,C */
99185571bc7Sbellard 		if( (r&0x0f) > 8) return;
99285571bc7Sbellard 		CH = &OPL->P_CH[r&0x0f];
99385571bc7Sbellard 		{
99485571bc7Sbellard 		int feedback = (v>>1)&7;
99585571bc7Sbellard 		CH->FB   = feedback ? (8+1) - feedback : 0;
99685571bc7Sbellard 		CH->CON = v&1;
997c11e80e2SStefan Weil 		set_algorithm(CH);
99885571bc7Sbellard 		}
99985571bc7Sbellard 		return;
100085571bc7Sbellard 	case 0xe0: /* wave type */
100185571bc7Sbellard 		slot = slot_array[r&0x1f];
100285571bc7Sbellard 		if(slot == -1) return;
100385571bc7Sbellard 		CH = &OPL->P_CH[slot/2];
100485571bc7Sbellard 		if(OPL->wavesel)
100585571bc7Sbellard 		{
100685571bc7Sbellard 			/* LOG(LOG_INF,("OPL SLOT %d wave select %d\n",slot,v&3)); */
100785571bc7Sbellard 			CH->SLOT[slot&1].wavetable = &SIN_TABLE[(v&0x03)*SIN_ENT];
100885571bc7Sbellard 		}
100985571bc7Sbellard 		return;
101085571bc7Sbellard 	}
101185571bc7Sbellard }
101285571bc7Sbellard 
101385571bc7Sbellard /* lock/unlock for common table */
101485571bc7Sbellard static int OPL_LockTable(void)
101585571bc7Sbellard {
101685571bc7Sbellard 	num_lock++;
101785571bc7Sbellard 	if(num_lock>1) return 0;
101885571bc7Sbellard 	/* first time */
101985571bc7Sbellard 	cur_chip = NULL;
102085571bc7Sbellard 	/* allocate total level table (128kb space) */
102185571bc7Sbellard 	if( !OPLOpenTable() )
102285571bc7Sbellard 	{
102385571bc7Sbellard 		num_lock--;
102485571bc7Sbellard 		return -1;
102585571bc7Sbellard 	}
102685571bc7Sbellard 	return 0;
102785571bc7Sbellard }
102885571bc7Sbellard 
102985571bc7Sbellard static void OPL_UnLockTable(void)
103085571bc7Sbellard {
103185571bc7Sbellard 	if(num_lock) num_lock--;
103285571bc7Sbellard 	if(num_lock) return;
103385571bc7Sbellard 	/* last time */
103485571bc7Sbellard 	cur_chip = NULL;
103585571bc7Sbellard 	OPLCloseTable();
103685571bc7Sbellard }
103785571bc7Sbellard 
103885571bc7Sbellard #if (BUILD_YM3812 || BUILD_YM3526)
103985571bc7Sbellard /*******************************************************************************/
104085571bc7Sbellard /*		YM3812 local section                                                   */
104185571bc7Sbellard /*******************************************************************************/
104285571bc7Sbellard 
104385571bc7Sbellard /* ---------- update one of chip ----------- */
104485571bc7Sbellard void YM3812UpdateOne(FM_OPL *OPL, INT16 *buffer, int length)
104585571bc7Sbellard {
104685571bc7Sbellard     int i;
104785571bc7Sbellard 	int data;
104885571bc7Sbellard 	OPLSAMPLE *buf = buffer;
104985571bc7Sbellard 	UINT32 amsCnt  = OPL->amsCnt;
105085571bc7Sbellard 	UINT32 vibCnt  = OPL->vibCnt;
1051c11e80e2SStefan Weil 	UINT8 rhythm = OPL->rhythm&0x20;
105285571bc7Sbellard 	OPL_CH *CH,*R_CH;
105385571bc7Sbellard 
105485571bc7Sbellard 	if( (void *)OPL != cur_chip ){
105585571bc7Sbellard 		cur_chip = (void *)OPL;
105685571bc7Sbellard 		/* channel pointers */
105785571bc7Sbellard 		S_CH = OPL->P_CH;
105885571bc7Sbellard 		E_CH = &S_CH[9];
1059c11e80e2SStefan Weil 		/* rhythm slot */
106085571bc7Sbellard 		SLOT7_1 = &S_CH[7].SLOT[SLOT1];
106185571bc7Sbellard 		SLOT7_2 = &S_CH[7].SLOT[SLOT2];
106285571bc7Sbellard 		SLOT8_1 = &S_CH[8].SLOT[SLOT1];
106385571bc7Sbellard 		SLOT8_2 = &S_CH[8].SLOT[SLOT2];
106485571bc7Sbellard 		/* LFO state */
106585571bc7Sbellard 		amsIncr = OPL->amsIncr;
106685571bc7Sbellard 		vibIncr = OPL->vibIncr;
106785571bc7Sbellard 		ams_table = OPL->ams_table;
106885571bc7Sbellard 		vib_table = OPL->vib_table;
106985571bc7Sbellard 	}
1070c11e80e2SStefan Weil 	R_CH = rhythm ? &S_CH[6] : E_CH;
107185571bc7Sbellard     for( i=0; i < length ; i++ )
107285571bc7Sbellard 	{
107385571bc7Sbellard 		/*            channel A         channel B         channel C      */
107485571bc7Sbellard 		/* LFO */
107585571bc7Sbellard 		ams = ams_table[(amsCnt+=amsIncr)>>AMS_SHIFT];
107685571bc7Sbellard 		vib = vib_table[(vibCnt+=vibIncr)>>VIB_SHIFT];
107785571bc7Sbellard 		outd[0] = 0;
107885571bc7Sbellard 		/* FM part */
107985571bc7Sbellard 		for(CH=S_CH ; CH < R_CH ; CH++)
108085571bc7Sbellard 			OPL_CALC_CH(CH);
108185571bc7Sbellard 		/* Rythn part */
1082c11e80e2SStefan Weil 		if(rhythm)
108385571bc7Sbellard 			OPL_CALC_RH(S_CH);
108485571bc7Sbellard 		/* limit check */
108585571bc7Sbellard 		data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
108685571bc7Sbellard 		/* store to sound buffer */
108785571bc7Sbellard 		buf[i] = data >> OPL_OUTSB;
108885571bc7Sbellard 	}
108985571bc7Sbellard 
109085571bc7Sbellard 	OPL->amsCnt = amsCnt;
109185571bc7Sbellard 	OPL->vibCnt = vibCnt;
109285571bc7Sbellard #ifdef OPL_OUTPUT_LOG
109385571bc7Sbellard 	if(opl_dbg_fp)
109485571bc7Sbellard 	{
109585571bc7Sbellard 		for(opl_dbg_chip=0;opl_dbg_chip<opl_dbg_maxchip;opl_dbg_chip++)
109685571bc7Sbellard 			if( opl_dbg_opl[opl_dbg_chip] == OPL) break;
109785571bc7Sbellard 		fprintf(opl_dbg_fp,"%c%c%c",0x20+opl_dbg_chip,length&0xff,length/256);
109885571bc7Sbellard 	}
109985571bc7Sbellard #endif
110085571bc7Sbellard }
110185571bc7Sbellard #endif /* (BUILD_YM3812 || BUILD_YM3526) */
110285571bc7Sbellard 
110385571bc7Sbellard #if BUILD_Y8950
110485571bc7Sbellard 
110585571bc7Sbellard void Y8950UpdateOne(FM_OPL *OPL, INT16 *buffer, int length)
110685571bc7Sbellard {
110785571bc7Sbellard     int i;
110885571bc7Sbellard 	int data;
110985571bc7Sbellard 	OPLSAMPLE *buf = buffer;
111085571bc7Sbellard 	UINT32 amsCnt  = OPL->amsCnt;
111185571bc7Sbellard 	UINT32 vibCnt  = OPL->vibCnt;
1112c11e80e2SStefan Weil 	UINT8 rhythm = OPL->rhythm&0x20;
111385571bc7Sbellard 	OPL_CH *CH,*R_CH;
111485571bc7Sbellard 	YM_DELTAT *DELTAT = OPL->deltat;
111585571bc7Sbellard 
111685571bc7Sbellard 	/* setup DELTA-T unit */
111785571bc7Sbellard 	YM_DELTAT_DECODE_PRESET(DELTAT);
111885571bc7Sbellard 
111985571bc7Sbellard 	if( (void *)OPL != cur_chip ){
112085571bc7Sbellard 		cur_chip = (void *)OPL;
112185571bc7Sbellard 		/* channel pointers */
112285571bc7Sbellard 		S_CH = OPL->P_CH;
112385571bc7Sbellard 		E_CH = &S_CH[9];
1124c11e80e2SStefan Weil 		/* rhythm slot */
112585571bc7Sbellard 		SLOT7_1 = &S_CH[7].SLOT[SLOT1];
112685571bc7Sbellard 		SLOT7_2 = &S_CH[7].SLOT[SLOT2];
112785571bc7Sbellard 		SLOT8_1 = &S_CH[8].SLOT[SLOT1];
112885571bc7Sbellard 		SLOT8_2 = &S_CH[8].SLOT[SLOT2];
112985571bc7Sbellard 		/* LFO state */
113085571bc7Sbellard 		amsIncr = OPL->amsIncr;
113185571bc7Sbellard 		vibIncr = OPL->vibIncr;
113285571bc7Sbellard 		ams_table = OPL->ams_table;
113385571bc7Sbellard 		vib_table = OPL->vib_table;
113485571bc7Sbellard 	}
1135c11e80e2SStefan Weil 	R_CH = rhythm ? &S_CH[6] : E_CH;
113685571bc7Sbellard     for( i=0; i < length ; i++ )
113785571bc7Sbellard 	{
113885571bc7Sbellard 		/*            channel A         channel B         channel C      */
113985571bc7Sbellard 		/* LFO */
114085571bc7Sbellard 		ams = ams_table[(amsCnt+=amsIncr)>>AMS_SHIFT];
114185571bc7Sbellard 		vib = vib_table[(vibCnt+=vibIncr)>>VIB_SHIFT];
114285571bc7Sbellard 		outd[0] = 0;
114385571bc7Sbellard 		/* deltaT ADPCM */
114485571bc7Sbellard 		if( DELTAT->portstate )
114585571bc7Sbellard 			YM_DELTAT_ADPCM_CALC(DELTAT);
114685571bc7Sbellard 		/* FM part */
114785571bc7Sbellard 		for(CH=S_CH ; CH < R_CH ; CH++)
114885571bc7Sbellard 			OPL_CALC_CH(CH);
114985571bc7Sbellard 		/* Rythn part */
1150c11e80e2SStefan Weil 		if(rhythm)
115185571bc7Sbellard 			OPL_CALC_RH(S_CH);
115285571bc7Sbellard 		/* limit check */
115385571bc7Sbellard 		data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
115485571bc7Sbellard 		/* store to sound buffer */
115585571bc7Sbellard 		buf[i] = data >> OPL_OUTSB;
115685571bc7Sbellard 	}
115785571bc7Sbellard 	OPL->amsCnt = amsCnt;
115885571bc7Sbellard 	OPL->vibCnt = vibCnt;
115985571bc7Sbellard 	/* deltaT START flag */
116085571bc7Sbellard 	if( !DELTAT->portstate )
116185571bc7Sbellard 		OPL->status &= 0xfe;
116285571bc7Sbellard }
116385571bc7Sbellard #endif
116485571bc7Sbellard 
116585571bc7Sbellard /* ---------- reset one of chip ---------- */
116685571bc7Sbellard void OPLResetChip(FM_OPL *OPL)
116785571bc7Sbellard {
116885571bc7Sbellard 	int c,s;
116985571bc7Sbellard 	int i;
117085571bc7Sbellard 
117185571bc7Sbellard 	/* reset chip */
117285571bc7Sbellard 	OPL->mode   = 0;	/* normal mode */
117385571bc7Sbellard 	OPL_STATUS_RESET(OPL,0x7f);
117485571bc7Sbellard 	/* reset with register write */
117585571bc7Sbellard 	OPLWriteReg(OPL,0x01,0); /* wabesel disable */
117685571bc7Sbellard 	OPLWriteReg(OPL,0x02,0); /* Timer1 */
117785571bc7Sbellard 	OPLWriteReg(OPL,0x03,0); /* Timer2 */
117885571bc7Sbellard 	OPLWriteReg(OPL,0x04,0); /* IRQ mask clear */
117985571bc7Sbellard 	for(i = 0xff ; i >= 0x20 ; i-- ) OPLWriteReg(OPL,i,0);
118085571bc7Sbellard 	/* reset OPerator paramater */
118185571bc7Sbellard 	for( c = 0 ; c < OPL->max_ch ; c++ )
118285571bc7Sbellard 	{
118385571bc7Sbellard 		OPL_CH *CH = &OPL->P_CH[c];
118485571bc7Sbellard 		/* OPL->P_CH[c].PAN = OPN_CENTER; */
118585571bc7Sbellard 		for(s = 0 ; s < 2 ; s++ )
118685571bc7Sbellard 		{
118785571bc7Sbellard 			/* wave table */
118885571bc7Sbellard 			CH->SLOT[s].wavetable = &SIN_TABLE[0];
118985571bc7Sbellard 			/* CH->SLOT[s].evm = ENV_MOD_RR; */
119085571bc7Sbellard 			CH->SLOT[s].evc = EG_OFF;
119185571bc7Sbellard 			CH->SLOT[s].eve = EG_OFF+1;
119285571bc7Sbellard 			CH->SLOT[s].evs = 0;
119385571bc7Sbellard 		}
119485571bc7Sbellard 	}
119585571bc7Sbellard #if BUILD_Y8950
119685571bc7Sbellard 	if(OPL->type&OPL_TYPE_ADPCM)
119785571bc7Sbellard 	{
119885571bc7Sbellard 		YM_DELTAT *DELTAT = OPL->deltat;
119985571bc7Sbellard 
120085571bc7Sbellard 		DELTAT->freqbase = OPL->freqbase;
120185571bc7Sbellard 		DELTAT->output_pointer = outd;
120285571bc7Sbellard 		DELTAT->portshift = 5;
120385571bc7Sbellard 		DELTAT->output_range = DELTAT_MIXING_LEVEL<<TL_BITS;
120485571bc7Sbellard 		YM_DELTAT_ADPCM_Reset(DELTAT,0);
120585571bc7Sbellard 	}
120685571bc7Sbellard #endif
120785571bc7Sbellard }
120885571bc7Sbellard 
120985571bc7Sbellard /* ----------  Create one of vietual YM3812 ----------       */
121085571bc7Sbellard /* 'rate'  is sampling rate and 'bufsiz' is the size of the  */
121185571bc7Sbellard FM_OPL *OPLCreate(int type, int clock, int rate)
121285571bc7Sbellard {
121385571bc7Sbellard 	char *ptr;
121485571bc7Sbellard 	FM_OPL *OPL;
121585571bc7Sbellard 	int state_size;
121685571bc7Sbellard 	int max_ch = 9; /* normaly 9 channels */
121785571bc7Sbellard 
121885571bc7Sbellard 	if( OPL_LockTable() ==-1) return NULL;
121985571bc7Sbellard 	/* allocate OPL state space */
122085571bc7Sbellard 	state_size  = sizeof(FM_OPL);
122185571bc7Sbellard 	state_size += sizeof(OPL_CH)*max_ch;
122285571bc7Sbellard #if BUILD_Y8950
122385571bc7Sbellard 	if(type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT);
122485571bc7Sbellard #endif
122585571bc7Sbellard 	/* allocate memory block */
1226809c130cSaliguori 	ptr = malloc(state_size);
1227809c130cSaliguori 	if(ptr==NULL) return NULL;
122885571bc7Sbellard 	/* clear */
122985571bc7Sbellard 	memset(ptr,0,state_size);
123085571bc7Sbellard 	OPL        = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL);
123185571bc7Sbellard 	OPL->P_CH  = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch;
123285571bc7Sbellard #if BUILD_Y8950
123385571bc7Sbellard 	if(type&OPL_TYPE_ADPCM) OPL->deltat = (YM_DELTAT *)ptr; ptr+=sizeof(YM_DELTAT);
123485571bc7Sbellard #endif
123585571bc7Sbellard 	/* set channel state pointer */
123685571bc7Sbellard 	OPL->type  = type;
123785571bc7Sbellard 	OPL->clock = clock;
123885571bc7Sbellard 	OPL->rate  = rate;
123985571bc7Sbellard 	OPL->max_ch = max_ch;
124085571bc7Sbellard 	/* init grobal tables */
124131de8314SStefan Weil 	OPL_initialize(OPL);
124285571bc7Sbellard 	/* reset chip */
124385571bc7Sbellard 	OPLResetChip(OPL);
124485571bc7Sbellard #ifdef OPL_OUTPUT_LOG
124585571bc7Sbellard 	if(!opl_dbg_fp)
124685571bc7Sbellard 	{
124785571bc7Sbellard 		opl_dbg_fp = fopen("opllog.opl","wb");
124885571bc7Sbellard 		opl_dbg_maxchip = 0;
124985571bc7Sbellard 	}
125085571bc7Sbellard 	if(opl_dbg_fp)
125185571bc7Sbellard 	{
125285571bc7Sbellard 		opl_dbg_opl[opl_dbg_maxchip] = OPL;
125385571bc7Sbellard 		fprintf(opl_dbg_fp,"%c%c%c%c%c%c",0x00+opl_dbg_maxchip,
125485571bc7Sbellard 			type,
125585571bc7Sbellard 			clock&0xff,
125685571bc7Sbellard 			(clock/0x100)&0xff,
125785571bc7Sbellard 			(clock/0x10000)&0xff,
125885571bc7Sbellard 			(clock/0x1000000)&0xff);
125985571bc7Sbellard 		opl_dbg_maxchip++;
126085571bc7Sbellard 	}
126185571bc7Sbellard #endif
126285571bc7Sbellard 	return OPL;
126385571bc7Sbellard }
126485571bc7Sbellard 
126585571bc7Sbellard /* ----------  Destroy one of vietual YM3812 ----------       */
126685571bc7Sbellard void OPLDestroy(FM_OPL *OPL)
126785571bc7Sbellard {
126885571bc7Sbellard #ifdef OPL_OUTPUT_LOG
126985571bc7Sbellard 	if(opl_dbg_fp)
127085571bc7Sbellard 	{
127185571bc7Sbellard 		fclose(opl_dbg_fp);
127285571bc7Sbellard 		opl_dbg_fp = NULL;
127385571bc7Sbellard 	}
127485571bc7Sbellard #endif
127585571bc7Sbellard 	OPL_UnLockTable();
127685571bc7Sbellard 	free(OPL);
127785571bc7Sbellard }
127885571bc7Sbellard 
127985571bc7Sbellard /* ----------  Option handlers ----------       */
128085571bc7Sbellard 
128185571bc7Sbellard void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset)
128285571bc7Sbellard {
128385571bc7Sbellard 	OPL->TimerHandler   = TimerHandler;
128485571bc7Sbellard 	OPL->TimerParam = channelOffset;
128585571bc7Sbellard }
128685571bc7Sbellard void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,int param)
128785571bc7Sbellard {
128885571bc7Sbellard 	OPL->IRQHandler     = IRQHandler;
128985571bc7Sbellard 	OPL->IRQParam = param;
129085571bc7Sbellard }
129185571bc7Sbellard void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,int param)
129285571bc7Sbellard {
129385571bc7Sbellard 	OPL->UpdateHandler = UpdateHandler;
129485571bc7Sbellard 	OPL->UpdateParam = param;
129585571bc7Sbellard }
129685571bc7Sbellard #if BUILD_Y8950
129785571bc7Sbellard void OPLSetPortHandler(FM_OPL *OPL,OPL_PORTHANDLER_W PortHandler_w,OPL_PORTHANDLER_R PortHandler_r,int param)
129885571bc7Sbellard {
129985571bc7Sbellard 	OPL->porthandler_w = PortHandler_w;
130085571bc7Sbellard 	OPL->porthandler_r = PortHandler_r;
130185571bc7Sbellard 	OPL->port_param = param;
130285571bc7Sbellard }
130385571bc7Sbellard 
130485571bc7Sbellard void OPLSetKeyboardHandler(FM_OPL *OPL,OPL_PORTHANDLER_W KeyboardHandler_w,OPL_PORTHANDLER_R KeyboardHandler_r,int param)
130585571bc7Sbellard {
130685571bc7Sbellard 	OPL->keyboardhandler_w = KeyboardHandler_w;
130785571bc7Sbellard 	OPL->keyboardhandler_r = KeyboardHandler_r;
130885571bc7Sbellard 	OPL->keyboard_param = param;
130985571bc7Sbellard }
131085571bc7Sbellard #endif
131185571bc7Sbellard /* ---------- YM3812 I/O interface ---------- */
131285571bc7Sbellard int OPLWrite(FM_OPL *OPL,int a,int v)
131385571bc7Sbellard {
131485571bc7Sbellard 	if( !(a&1) )
131585571bc7Sbellard 	{	/* address port */
131685571bc7Sbellard 		OPL->address = v & 0xff;
131785571bc7Sbellard 	}
131885571bc7Sbellard 	else
131985571bc7Sbellard 	{	/* data port */
132085571bc7Sbellard 		if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
132185571bc7Sbellard #ifdef OPL_OUTPUT_LOG
132285571bc7Sbellard 	if(opl_dbg_fp)
132385571bc7Sbellard 	{
132485571bc7Sbellard 		for(opl_dbg_chip=0;opl_dbg_chip<opl_dbg_maxchip;opl_dbg_chip++)
132585571bc7Sbellard 			if( opl_dbg_opl[opl_dbg_chip] == OPL) break;
132685571bc7Sbellard 		fprintf(opl_dbg_fp,"%c%c%c",0x10+opl_dbg_chip,OPL->address,v);
132785571bc7Sbellard 	}
132885571bc7Sbellard #endif
132985571bc7Sbellard 		OPLWriteReg(OPL,OPL->address,v);
133085571bc7Sbellard 	}
133185571bc7Sbellard 	return OPL->status>>7;
133285571bc7Sbellard }
133385571bc7Sbellard 
133485571bc7Sbellard unsigned char OPLRead(FM_OPL *OPL,int a)
133585571bc7Sbellard {
133685571bc7Sbellard 	if( !(a&1) )
133785571bc7Sbellard 	{	/* status port */
133885571bc7Sbellard 		return OPL->status & (OPL->statusmask|0x80);
133985571bc7Sbellard 	}
134085571bc7Sbellard 	/* data port */
134185571bc7Sbellard 	switch(OPL->address)
134285571bc7Sbellard 	{
134385571bc7Sbellard 	case 0x05: /* KeyBoard IN */
134485571bc7Sbellard 		if(OPL->type&OPL_TYPE_KEYBOARD)
134585571bc7Sbellard 		{
134685571bc7Sbellard 			if(OPL->keyboardhandler_r)
134785571bc7Sbellard 				return OPL->keyboardhandler_r(OPL->keyboard_param);
1348c973a36dSmalc 			else {
134985571bc7Sbellard 				LOG(LOG_WAR,("OPL:read unmapped KEYBOARD port\n"));
135085571bc7Sbellard 			}
1351c973a36dSmalc 		}
135285571bc7Sbellard 		return 0;
135385571bc7Sbellard #if 0
135485571bc7Sbellard 	case 0x0f: /* ADPCM-DATA  */
135585571bc7Sbellard 		return 0;
135685571bc7Sbellard #endif
135785571bc7Sbellard 	case 0x19: /* I/O DATA    */
135885571bc7Sbellard 		if(OPL->type&OPL_TYPE_IO)
135985571bc7Sbellard 		{
136085571bc7Sbellard 			if(OPL->porthandler_r)
136185571bc7Sbellard 				return OPL->porthandler_r(OPL->port_param);
1362c973a36dSmalc 			else {
136385571bc7Sbellard 				LOG(LOG_WAR,("OPL:read unmapped I/O port\n"));
136485571bc7Sbellard 			}
1365c973a36dSmalc 		}
136685571bc7Sbellard 		return 0;
136785571bc7Sbellard 	case 0x1a: /* PCM-DATA    */
136885571bc7Sbellard 		return 0;
136985571bc7Sbellard 	}
137085571bc7Sbellard 	return 0;
137185571bc7Sbellard }
137285571bc7Sbellard 
137385571bc7Sbellard int OPLTimerOver(FM_OPL *OPL,int c)
137485571bc7Sbellard {
137585571bc7Sbellard 	if( c )
137685571bc7Sbellard 	{	/* Timer B */
137785571bc7Sbellard 		OPL_STATUS_SET(OPL,0x20);
137885571bc7Sbellard 	}
137985571bc7Sbellard 	else
138085571bc7Sbellard 	{	/* Timer A */
138185571bc7Sbellard 		OPL_STATUS_SET(OPL,0x40);
138266a0a2cbSDong Xu Wang 		/* CSM mode key,TL control */
138385571bc7Sbellard 		if( OPL->mode & 0x80 )
138485571bc7Sbellard 		{	/* CSM mode total level latch and auto key on */
138585571bc7Sbellard 			int ch;
138685571bc7Sbellard 			if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
138785571bc7Sbellard 			for(ch=0;ch<9;ch++)
138885571bc7Sbellard 				CSMKeyControll( &OPL->P_CH[ch] );
138985571bc7Sbellard 		}
139085571bc7Sbellard 	}
139185571bc7Sbellard 	/* reload timer */
139285571bc7Sbellard 	if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+c,(double)OPL->T[c]*OPL->TimerBase);
139385571bc7Sbellard 	return OPL->status>>7;
139485571bc7Sbellard }
1395