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