DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/mame/fmopl.cpp
00001 // license:GPL-2.0+
00002 // copyright-holders:Jarek Burczynski,Tatsuyuki Satoh
00003 /*
00004 **
00005 ** File: fmopl.c - software implementation of FM sound generator
00006 **                                            types OPL and OPL2
00007 **
00008 ** Copyright Jarek Burczynski (bujar at mame dot net)
00009 ** Copyright Tatsuyuki Satoh , MultiArcadeMachineEmulator development
00010 **
00011 ** Version 0.72
00012 **
00013 
00014 Revision History:
00015 
00016 04-08-2003 Jarek Burczynski:
00017  - removed BFRDY hack. BFRDY is busy flag, and it should be 0 only when the chip
00018    handles memory read/write or during the adpcm synthesis when the chip
00019    requests another byte of ADPCM data.
00020 
00021 24-07-2003 Jarek Burczynski:
00022  - added a small hack for Y8950 status BFRDY flag (bit 3 should be set after
00023    some (unknown) delay). Right now it's always set.
00024 
00025 14-06-2003 Jarek Burczynski:
00026  - implemented all of the status register flags in Y8950 emulation
00027  - renamed y8950_set_delta_t_memory() parameters from _rom_ to _mem_ since
00028    they can be either RAM or ROM
00029 
00030 08-10-2002 Jarek Burczynski (thanks to Dox for the YM3526 chip)
00031  - corrected ym3526_read() to always set bit 2 and bit 1
00032    to HIGH state - identical to ym3812_read (verified on real YM3526)
00033 
00034 04-28-2002 Jarek Burczynski:
00035  - binary exact Envelope Generator (verified on real YM3812);
00036    compared to YM2151: the EG clock is equal to internal_clock,
00037    rates are 2 times slower and volume resolution is one bit less
00038  - modified interface functions (they no longer return pointer -
00039    that's internal to the emulator now):
00040     - new wrapper functions for OPLCreate: ym3526_init(), ym3812_init() and y8950_init()
00041  - corrected 'off by one' error in feedback calculations (when feedback is off)
00042  - enabled waveform usage (credit goes to Vlad Romascanu and zazzal22)
00043  - speeded up noise generator calculations (Nicola Salmoria)
00044 
00045 03-24-2002 Jarek Burczynski (thanks to Dox for the YM3812 chip)
00046  Complete rewrite (all verified on real YM3812):
00047  - corrected sin_tab and tl_tab data
00048  - corrected operator output calculations
00049  - corrected waveform_select_enable register;
00050    simply: ignore all writes to waveform_select register when
00051    waveform_select_enable == 0 and do not change the waveform previously selected.
00052  - corrected KSR handling
00053  - corrected Envelope Generator: attack shape, Sustain mode and
00054    Percussive/Non-percussive modes handling
00055  - Envelope Generator rates are two times slower now
00056  - LFO amplitude (tremolo) and phase modulation (vibrato)
00057  - rhythm sounds phase generation
00058  - white noise generator (big thanks to Olivier Galibert for mentioning Berlekamp-Massey algorithm)
00059  - corrected key on/off handling (the 'key' signal is ORed from three sources: FM, rhythm and CSM)
00060  - funky details (like ignoring output of operator 1 in BD rhythm sound when connect == 1)
00061 
00062 12-28-2001 Acho A. Tang
00063  - reflected Delta-T EOS status on Y8950 status port.
00064  - fixed subscription range of attack/decay tables
00065 
00066 
00067     To do:
00068         add delay before key off in CSM mode (see CSMKeyControll)
00069         verify volume of the FM part on the Y8950
00070 */
00071 
00072 #include "emu.h"
00073 #include "ymdeltat.h"
00074 #include "fmopl.h"
00075 
00076 
00077 
00078 /* output final shift */
00079 #if (OPL_SAMPLE_BITS==16)
00080         #define FINAL_SH    (0)
00081         #define MAXOUT      (+32767)
00082         #define MINOUT      (-32768)
00083 #else
00084         #define FINAL_SH    (8)
00085         #define MAXOUT      (+127)
00086         #define MINOUT      (-128)
00087 #endif
00088 
00089 
00090 #define FREQ_SH         16  /* 16.16 fixed point (frequency calculations) */
00091 #define EG_SH           16  /* 16.16 fixed point (EG timing)              */
00092 #define LFO_SH          24  /*  8.24 fixed point (LFO calculations)       */
00093 #define TIMER_SH        16  /* 16.16 fixed point (timers calculations)    */
00094 
00095 #define FREQ_MASK       ((1<<FREQ_SH)-1)
00096 
00097 /* envelope output entries */
00098 #define ENV_BITS        10
00099 #define ENV_LEN         (1<<ENV_BITS)
00100 #define ENV_STEP        (128.0/ENV_LEN)
00101 
00102 #define MAX_ATT_INDEX   ((1<<(ENV_BITS-1))-1) /*511*/
00103 #define MIN_ATT_INDEX   (0)
00104 
00105 /* sinwave entries */
00106 #define SIN_BITS        10
00107 #define SIN_LEN         (1<<SIN_BITS)
00108 #define SIN_MASK        (SIN_LEN-1)
00109 #define DV                              (0.1875 / 2.0 )
00110 
00111 #define TL_RES_LEN      (256)   /* 8 bits addressing (real chip) */
00112 #define TL_TAB_LEN              (12 * 2 * TL_RES_LEN)
00113 
00114 /*  TL_TAB_LEN is calculated as:
00115 *   12 - sinus amplitude bits     (Y axis)
00116 *   2  - sinus sign bit           (Y axis)
00117 *   TL_RES_LEN - sinus resolution (X axis)
00118 */
00119 
00120 
00121 #define ENV_QUIET      ( TL_TAB_LEN >> 4 )
00122 #define LFO_AM_TAB_ELEMENTS 210
00123 
00124 
00125 
00126 /* register number to channel number , slot offset */
00127 #define SLOT1 0
00128 #define SLOT2 1
00129 
00130 /* Envelope Generator phases */
00131 
00132 #define EG_ATT          4
00133 #define EG_DEC          3
00134 #define EG_SUS          2
00135 #define EG_REL          1
00136 #define EG_OFF          0
00137 
00138 
00139 /* save output as raw 16-bit sample */
00140 
00141 /*#define SAVE_SAMPLE*/
00142 
00143 #ifdef SAVE_SAMPLE
00144 static inline signed int acc_calc(signed int value)
00145 {
00146         if (value>=0)
00147         {
00148                 if (value < 0x0200)
00149                         return (value & ~0);
00150                 if (value < 0x0400)
00151                         return (value & ~1);
00152                 if (value < 0x0800)
00153                         return (value & ~3);
00154                 if (value < 0x1000)
00155                         return (value & ~7);
00156                 if (value < 0x2000)
00157                         return (value & ~15);
00158                 if (value < 0x4000)
00159                         return (value & ~31);
00160                 return (value & ~63);
00161         }
00162         /*else value < 0*/
00163         if (value > -0x0200)
00164                 return (~abs(value) & ~0);
00165         if (value > -0x0400)
00166                 return (~abs(value) & ~1);
00167         if (value > -0x0800)
00168                 return (~abs(value) & ~3);
00169         if (value > -0x1000)
00170                 return (~abs(value) & ~7);
00171         if (value > -0x2000)
00172                 return (~abs(value) & ~15);
00173         if (value > -0x4000)
00174                 return (~abs(value) & ~31);
00175         return (~abs(value) & ~63);
00176 }
00177 
00178 
00179 static FILE *sample[1];
00180         #if 1   /*save to MONO file */
00181                 #define SAVE_ALL_CHANNELS \
00182                 {   signed int pom = acc_calc(lt); \
00183                         fputc((unsigned short)pom&0xff,sample[0]); \
00184                         fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
00185                 }
00186         #else   /*save to STEREO file */
00187                 #define SAVE_ALL_CHANNELS \
00188                 {   signed int pom = lt; \
00189                         fputc((unsigned short)pom&0xff,sample[0]); \
00190                         fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
00191                         pom = rt; \
00192                         fputc((unsigned short)pom&0xff,sample[0]); \
00193                         fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
00194                 }
00195         #endif
00196 #endif
00197 
00198 #define OPL_TYPE_WAVESEL   0x01  /* waveform select     */
00199 #define OPL_TYPE_ADPCM     0x02  /* DELTA-T ADPCM unit  */
00200 #define OPL_TYPE_KEYBOARD  0x04  /* keyboard interface  */
00201 #define OPL_TYPE_IO        0x08  /* I/O port            */
00202 
00203 /* ---------- Generic interface section ---------- */
00204 #define OPL_TYPE_YM3526 (0)
00205 #define OPL_TYPE_YM3812 (OPL_TYPE_WAVESEL)
00206 #define OPL_TYPE_Y8950  (OPL_TYPE_ADPCM|OPL_TYPE_KEYBOARD|OPL_TYPE_IO)
00207 
00208 
00209 namespace {
00210 
00211 // TODO: make these static members
00212 
00213 #define RATE_STEPS (8)
00214 extern const unsigned char eg_rate_shift[16+64+16];
00215 extern const unsigned char eg_rate_select[16+64+16];
00216 
00217 
00218 struct OPL_SLOT
00219 {
00220         uint32_t  ar;         /* attack rate: AR<<2           */
00221         uint32_t  dr;         /* decay rate:  DR<<2           */
00222         uint32_t  rr;         /* release rate:RR<<2           */
00223         uint8_t   KSR;        /* key scale rate               */
00224         uint8_t   ksl;        /* keyscale level               */
00225         uint8_t   ksr;        /* key scale rate: kcode>>KSR   */
00226         uint8_t   mul;        /* multiple: mul_tab[ML]        */
00227 
00228         /* Phase Generator */
00229         uint32_t  Cnt;        /* frequency counter            */
00230         uint32_t  Incr;       /* frequency counter step       */
00231         uint8_t   FB;         /* feedback shift value         */
00232         int32_t   *connect1;  /* slot1 output pointer         */
00233         int32_t   op1_out[2]; /* slot1 output for feedback    */
00234         uint8_t   CON;        /* connection (algorithm) type  */
00235 
00236         /* Envelope Generator */
00237         uint8_t   eg_type;    /* percussive/non-percussive mode */
00238         uint8_t   state;      /* phase type                   */
00239         uint32_t  TL;         /* total level: TL << 2         */
00240         int32_t   TLL;        /* adjusted now TL              */
00241         int32_t   volume;     /* envelope counter             */
00242         int32_t   sl;         /* sustain level: sl_tab[SL]    */
00243         uint8_t   eg_sh_ar;   /* (attack state)               */
00244         uint8_t   eg_sel_ar;  /* (attack state)               */
00245         uint8_t   eg_sh_dr;   /* (decay state)                */
00246         uint8_t   eg_sel_dr;  /* (decay state)                */
00247         uint8_t   eg_sh_rr;   /* (release state)              */
00248         uint8_t   eg_sel_rr;  /* (release state)              */
00249         uint32_t  key;        /* 0 = KEY OFF, >0 = KEY ON     */
00250 
00251         /* LFO */
00252         uint32_t  AMmask;     /* LFO Amplitude Modulation enable mask */
00253         uint8_t   vib;        /* LFO Phase Modulation enable flag (active high)*/
00254 
00255         /* waveform select */
00256         uint16_t  wavetable;
00257 
00258         void KEYON(uint32_t key_set)
00259         {
00260                 if( !key )
00261                 {
00262                         /* restart Phase Generator */
00263                         Cnt = 0;
00264                         /* phase -> Attack */
00265                         state = EG_ATT;
00266                 }
00267                 key |= key_set;
00268         }
00269 
00270         void KEYOFF(uint32_t key_clr)
00271         {
00272                 if( key )
00273                 {
00274                         key &= key_clr;
00275 
00276                         if( !key )
00277                         {
00278                                 /* phase -> Release */
00279                                 if (state>EG_REL)
00280                                         state = EG_REL;
00281                         }
00282                 }
00283         }
00284 };
00285 
00286 struct OPL_CH
00287 {
00288         OPL_SLOT SLOT[2];
00289         /* phase generator state */
00290         uint32_t  block_fnum; /* block+fnum                   */
00291         uint32_t  fc;         /* Freq. Increment base         */
00292         uint32_t  ksl_base;   /* KeyScaleLevel Base step      */
00293         uint8_t   kcode;      /* key code (for key scaling)   */
00294 
00295 
00296         /* update phase increment counter of operator (also update the EG rates if necessary) */
00297         void CALC_FCSLOT(OPL_SLOT &SLOT)
00298         {
00299                 /* (frequency) phase increment counter */
00300                 SLOT.Incr = fc * SLOT.mul;
00301                 int const ksr = kcode >> SLOT.KSR;
00302 
00303                 if( SLOT.ksr != ksr )
00304                 {
00305                         SLOT.ksr = ksr;
00306 
00307                         /* calculate envelope generator rates */
00308                         if ((SLOT.ar + SLOT.ksr) < 16+62)
00309                         {
00310                                 SLOT.eg_sh_ar  = eg_rate_shift [SLOT.ar + SLOT.ksr ];
00311                                 SLOT.eg_sel_ar = eg_rate_select[SLOT.ar + SLOT.ksr ];
00312                         }
00313                         else
00314                         {
00315                                 SLOT.eg_sh_ar  = 0;
00316                                 SLOT.eg_sel_ar = 13*RATE_STEPS;
00317                         }
00318                         SLOT.eg_sh_dr  = eg_rate_shift [SLOT.dr + SLOT.ksr ];
00319                         SLOT.eg_sel_dr = eg_rate_select[SLOT.dr + SLOT.ksr ];
00320                         SLOT.eg_sh_rr  = eg_rate_shift [SLOT.rr + SLOT.ksr ];
00321                         SLOT.eg_sel_rr = eg_rate_select[SLOT.rr + SLOT.ksr ];
00322                 }
00323         }
00324 };
00325 
00326 /* OPL state */
00327 struct FM_OPL
00328 {
00329         /* FM channel slots */
00330         OPL_CH  P_CH[9];                /* OPL/OPL2 chips have 9 channels*/
00331 
00332         uint32_t  eg_cnt;                 /* global envelope generator counter    */
00333         uint32_t  eg_timer;               /* global envelope generator counter works at frequency = chipclock/72 */
00334         uint32_t  eg_timer_add;           /* step of eg_timer                     */
00335         uint32_t  eg_timer_overflow;      /* envelope generator timer overflows every 1 sample (on real chip) */
00336 
00337         uint8_t   rhythm;                 /* Rhythm mode                  */
00338 
00339         uint32_t  fn_tab[1024];           /* fnumber->increment counter   */
00340 
00341         /* LFO */
00342         uint32_t  LFO_AM;
00343         int32_t   LFO_PM;
00344 
00345         uint8_t   lfo_am_depth;
00346         uint8_t   lfo_pm_depth_range;
00347         uint32_t  lfo_am_cnt;
00348         uint32_t  lfo_am_inc;
00349         uint32_t  lfo_pm_cnt;
00350         uint32_t  lfo_pm_inc;
00351 
00352         uint32_t  noise_rng;              /* 23 bit noise shift register  */
00353         uint32_t  noise_p;                /* current noise 'phase'        */
00354         uint32_t  noise_f;                /* current noise period         */
00355 
00356         uint8_t   wavesel;                /* waveform select enable flag  */
00357 
00358         uint32_t  T[2];                   /* timer counters               */
00359         uint8_t   st[2];                  /* timer enable                 */
00360 
00361 #if BUILD_Y8950
00362         /* Delta-T ADPCM unit (Y8950) */
00363 
00364         YM_DELTAT *deltat;
00365 
00366         /* Keyboard and I/O ports interface */
00367         uint8_t   portDirection;
00368         uint8_t   portLatch;
00369         OPL_PORTHANDLER_R porthandler_r;
00370         OPL_PORTHANDLER_W porthandler_w;
00371         device_t *  port_param;
00372         OPL_PORTHANDLER_R keyboardhandler_r;
00373         OPL_PORTHANDLER_W keyboardhandler_w;
00374         device_t *  keyboard_param;
00375 #endif
00376 
00377         /* external event callback handlers */
00378         OPL_TIMERHANDLER  timer_handler;    /* TIMER handler                */
00379         device_t *TimerParam;                   /* TIMER parameter              */
00380         OPL_IRQHANDLER    IRQHandler;   /* IRQ handler                  */
00381         device_t *IRQParam;                 /* IRQ parameter                */
00382         OPL_UPDATEHANDLER UpdateHandler;/* stream update handler        */
00383         device_t *UpdateParam;              /* stream update parameter      */
00384 
00385         uint8_t type;                     /* chip type                    */
00386         uint8_t address;                  /* address register             */
00387         uint8_t status;                   /* status flag                  */
00388         uint8_t statusmask;               /* status mask                  */
00389         uint8_t mode;                     /* Reg.08 : CSM,notesel,etc.    */
00390 
00391         uint32_t clock;                   /* master clock  (Hz)           */
00392         uint32_t rate;                    /* sampling rate (Hz)           */
00393         double freqbase;                /* frequency base               */
00394         //attotime TimerBase;         /* Timer base time (==sampling time)*/
00395         device_t *device;
00396 
00397         signed int phase_modulation;    /* phase modulation input (SLOT 2) */
00398         signed int output[1];
00399 #if BUILD_Y8950
00400         int32_t output_deltat[4];     /* for Y8950 DELTA-T, chip is mono, that 4 here is just for safety */
00401 #endif
00402 
00403 
00404         /* status set and IRQ handling */
00405         void STATUS_SET(int flag)
00406         {
00407                 /* set status flag */
00408                 status |= flag;
00409                 if(!(status & 0x80))
00410                 {
00411                         if(status & statusmask)
00412                         {   /* IRQ on */
00413                                 status |= 0x80;
00414                                 /* callback user interrupt handler (IRQ is OFF to ON) */
00415                                 if(IRQHandler) (IRQHandler)(IRQParam,1);
00416                         }
00417                 }
00418         }
00419 
00420         /* status reset and IRQ handling */
00421         void STATUS_RESET(int flag)
00422         {
00423                 /* reset status flag */
00424                 status &=~flag;
00425                 if(status & 0x80)
00426                 {
00427                         if (!(status & statusmask) )
00428                         {
00429                                 status &= 0x7f;
00430                                 /* callback user interrupt handler (IRQ is ON to OFF) */
00431                                 if(IRQHandler) (IRQHandler)(IRQParam,0);
00432                         }
00433                 }
00434         }
00435 
00436         /* IRQ mask set */
00437         void STATUSMASK_SET(int flag)
00438         {
00439                 statusmask = flag;
00440                 /* IRQ handling check */
00441                 STATUS_SET(0);
00442                 STATUS_RESET(0);
00443         }
00444 
00445 
00446         /* advance LFO to next sample */
00447         void advance_lfo()
00448         {
00449                 /* LFO */
00450                 lfo_am_cnt += lfo_am_inc;
00451                 if (lfo_am_cnt >= (uint32_t(LFO_AM_TAB_ELEMENTS) << LFO_SH))  /* lfo_am_table is 210 elements long */
00452                         lfo_am_cnt -= (uint32_t(LFO_AM_TAB_ELEMENTS) << LFO_SH);
00453 
00454                 uint8_t const tmp = lfo_am_table[ lfo_am_cnt >> LFO_SH ];
00455 
00456                 LFO_AM = lfo_am_depth ? tmp : tmp >> 2;
00457 
00458                 lfo_pm_cnt += lfo_pm_inc;
00459                 LFO_PM = (lfo_pm_cnt>>LFO_SH & 7) | lfo_pm_depth_range;
00460         }
00461 
00462         /* advance to next sample */
00463         void advance()
00464         {
00465                 eg_timer += eg_timer_add;
00466 
00467                 while (eg_timer >= eg_timer_overflow)
00468                 {
00469                         eg_timer -= eg_timer_overflow;
00470 
00471                         eg_cnt++;
00472 
00473                         for (int i=0; i<9*2; i++)
00474                         {
00475                                 OPL_CH   &CH  = P_CH[i/2];
00476                                 OPL_SLOT &op  = CH.SLOT[i&1];
00477 
00478                                 /* Envelope Generator */
00479                                 switch(op.state)
00480                                 {
00481                                 case EG_ATT:        /* attack phase */
00482                                         if ( !(eg_cnt & ((1<<op.eg_sh_ar)-1) ) )
00483                                         {
00484                                                 op.volume += (~op.volume *
00485                                                                                                         (eg_inc[op.eg_sel_ar + ((eg_cnt>>op.eg_sh_ar)&7)])
00486                                                                                                         ) >>3;
00487 
00488                                                 if (op.volume <= MIN_ATT_INDEX)
00489                                                 {
00490                                                         op.volume = MIN_ATT_INDEX;
00491                                                         op.state = EG_DEC;
00492                                                 }
00493 
00494                                         }
00495                                         break;
00496 
00497                                 case EG_DEC:    /* decay phase */
00498                                         if ( !(eg_cnt & ((1<<op.eg_sh_dr)-1) ) )
00499                                         {
00500                                                 op.volume += eg_inc[op.eg_sel_dr + ((eg_cnt>>op.eg_sh_dr)&7)];
00501 
00502                                                 if ( op.volume >= op.sl )
00503                                                         op.state = EG_SUS;
00504 
00505                                         }
00506                                         break;
00507 
00508                                 case EG_SUS:    /* sustain phase */
00509 
00510                                         /* this is important behaviour:
00511                                         one can change percusive/non-percussive modes on the fly and
00512                                         the chip will remain in sustain phase - verified on real YM3812 */
00513 
00514                                         if(op.eg_type)     /* non-percussive mode */
00515                                         {
00516                                                                                 /* do nothing */
00517                                         }
00518                                         else                /* percussive mode */
00519                                         {
00520                                                 /* during sustain phase chip adds Release Rate (in percussive mode) */
00521                                                 if ( !(eg_cnt & ((1<<op.eg_sh_rr)-1) ) )
00522                                                 {
00523                                                         op.volume += eg_inc[op.eg_sel_rr + ((eg_cnt>>op.eg_sh_rr)&7)];
00524 
00525                                                         if ( op.volume >= MAX_ATT_INDEX )
00526                                                                 op.volume = MAX_ATT_INDEX;
00527                                                 }
00528                                                 /* else do nothing in sustain phase */
00529                                         }
00530                                         break;
00531 
00532                                 case EG_REL:    /* release phase */
00533                                         if ( !(eg_cnt & ((1<<op.eg_sh_rr)-1) ) )
00534                                         {
00535                                                 op.volume += eg_inc[op.eg_sel_rr + ((eg_cnt>>op.eg_sh_rr)&7)];
00536 
00537                                                 if ( op.volume >= MAX_ATT_INDEX )
00538                                                 {
00539                                                         op.volume = MAX_ATT_INDEX;
00540                                                         op.state = EG_OFF;
00541                                                 }
00542 
00543                                         }
00544                                         break;
00545 
00546                                 default:
00547                                         break;
00548                                 }
00549                         }
00550                 }
00551 
00552                 for (int i=0; i<9*2; i++)
00553                 {
00554                         OPL_CH   &CH  = P_CH[i/2];
00555                         OPL_SLOT &op  = CH.SLOT[i&1];
00556 
00557                         /* Phase Generator */
00558                         if(op.vib)
00559                         {
00560                                 unsigned int block_fnum                    = CH.block_fnum;
00561                                 unsigned int const fnum_lfo                = (block_fnum&0x0380) >> 7;
00562 
00563                                 signed int const lfo_fn_table_index_offset = lfo_pm_table[LFO_PM + 16*fnum_lfo ];
00564 
00565                                 if (lfo_fn_table_index_offset)  /* LFO phase modulation active */
00566                                 {
00567                                         block_fnum += lfo_fn_table_index_offset;
00568                                         uint8_t const block = (block_fnum&0x1c00) >> 10;
00569                                         op.Cnt += (fn_tab[block_fnum&0x03ff] >> (7-block)) * op.mul;
00570                                 }
00571                                 else    /* LFO phase modulation  = zero */
00572                                 {
00573                                         op.Cnt += op.Incr;
00574                                 }
00575                         }
00576                         else    /* LFO phase modulation disabled for this operator */
00577                         {
00578                                 op.Cnt += op.Incr;
00579                         }
00580                 }
00581 
00582                 /*  The Noise Generator of the YM3812 is 23-bit shift register.
00583                 *   Period is equal to 2^23-2 samples.
00584                 *   Register works at sampling frequency of the chip, so output
00585                 *   can change on every sample.
00586                 *
00587                 *   Output of the register and input to the bit 22 is:
00588                 *   bit0 XOR bit14 XOR bit15 XOR bit22
00589                 *
00590                 *   Simply use bit 22 as the noise output.
00591                 */
00592 
00593                 noise_p += noise_f;
00594                 int i = noise_p >> FREQ_SH;        /* number of events (shifts of the shift register) */
00595                 noise_p &= FREQ_MASK;
00596                 while (i)
00597                 {
00598                         /*
00599                         uint32_t j;
00600                         j = ( (noise_rng) ^ (noise_rng>>14) ^ (noise_rng>>15) ^ (noise_rng>>22) ) & 1;
00601                         noise_rng = (j<<22) | (noise_rng>>1);
00602                         */
00603 
00604                         /*
00605                             Instead of doing all the logic operations above, we
00606                             use a trick here (and use bit 0 as the noise output).
00607                             The difference is only that the noise bit changes one
00608                             step ahead. This doesn't matter since we don't know
00609                             what is real state of the noise_rng after the reset.
00610                         */
00611 
00612                         if (noise_rng & 1) noise_rng ^= 0x800302;
00613                         noise_rng >>= 1;
00614 
00615                         i--;
00616                 }
00617         }
00618 
00619         /* calculate output */
00620         void CALC_CH(OPL_CH &CH)
00621         {
00622                 OPL_SLOT *SLOT;
00623                 unsigned int env;
00624                 signed int out;
00625 
00626                 phase_modulation = 0;
00627 
00628                 /* SLOT 1 */
00629                 SLOT = &CH.SLOT[SLOT1];
00630                 env  = volume_calc(*SLOT);
00631                 out  = SLOT->op1_out[0] + SLOT->op1_out[1];
00632                 SLOT->op1_out[0] = SLOT->op1_out[1];
00633                 *SLOT->connect1 += SLOT->op1_out[0];
00634                 SLOT->op1_out[1] = 0;
00635                 if( env < ENV_QUIET )
00636                 {
00637                         if (!SLOT->FB)
00638                                 out = 0;
00639                         SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
00640                 }
00641 
00642                 /* SLOT 2 */
00643                 SLOT++;
00644                 env = volume_calc(*SLOT);
00645                 if( env < ENV_QUIET )
00646                         output[0] += op_calc(SLOT->Cnt, env, phase_modulation, SLOT->wavetable);
00647         }
00648 
00649         /*
00650             operators used in the rhythm sounds generation process:
00651 
00652             Envelope Generator:
00653 
00654         channel  operator  register number   Bass  High  Snare Tom  Top
00655         / slot   number    TL ARDR SLRR Wave Drum  Hat   Drum  Tom  Cymbal
00656          6 / 0   12        50  70   90   f0  +
00657          6 / 1   15        53  73   93   f3  +
00658          7 / 0   13        51  71   91   f1        +
00659          7 / 1   16        54  74   94   f4              +
00660          8 / 0   14        52  72   92   f2                    +
00661          8 / 1   17        55  75   95   f5                          +
00662 
00663             Phase Generator:
00664 
00665         channel  operator  register number   Bass  High  Snare Tom  Top
00666         / slot   number    MULTIPLE          Drum  Hat   Drum  Tom  Cymbal
00667          6 / 0   12        30                +
00668          6 / 1   15        33                +
00669          7 / 0   13        31                      +     +           +
00670          7 / 1   16        34                -----  n o t  u s e d -----
00671          8 / 0   14        32                                  +
00672          8 / 1   17        35                      +                 +
00673 
00674         channel  operator  register number   Bass  High  Snare Tom  Top
00675         number   number    BLK/FNUM2 FNUM    Drum  Hat   Drum  Tom  Cymbal
00676            6     12,15     B6        A6      +
00677 
00678            7     13,16     B7        A7            +     +           +
00679 
00680            8     14,17     B8        A8            +           +     +
00681 
00682         */
00683 
00684         /* calculate rhythm */
00685 
00686         void CALC_RH()
00687         {
00688                 unsigned int const noise = BIT(noise_rng, 0);
00689 
00690                 OPL_SLOT *SLOT;
00691                 signed int out;
00692                 unsigned int env;
00693 
00694 
00695                 /* Bass Drum (verified on real YM3812):
00696                   - depends on the channel 6 'connect' register:
00697                       when connect = 0 it works the same as in normal (non-rhythm) mode (op1->op2->out)
00698                       when connect = 1 _only_ operator 2 is present on output (op2->out), operator 1 is ignored
00699                   - output sample always is multiplied by 2
00700                 */
00701 
00702                 phase_modulation = 0;
00703                 /* SLOT 1 */
00704                 SLOT = &P_CH[6].SLOT[SLOT1];
00705                 env = volume_calc(*SLOT);
00706 
00707                 out = SLOT->op1_out[0] + SLOT->op1_out[1];
00708                 SLOT->op1_out[0] = SLOT->op1_out[1];
00709 
00710                 if (!SLOT->CON)
00711                         phase_modulation = SLOT->op1_out[0];
00712                 /* else ignore output of operator 1 */
00713 
00714                 SLOT->op1_out[1] = 0;
00715                 if( env < ENV_QUIET )
00716                 {
00717                         if (!SLOT->FB)
00718                                 out = 0;
00719                         SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
00720                 }
00721 
00722                 /* SLOT 2 */
00723                 SLOT++;
00724                 env = volume_calc(*SLOT);
00725                 if( env < ENV_QUIET )
00726                         output[0] += op_calc(SLOT->Cnt, env, phase_modulation, SLOT->wavetable) * 2;
00727 
00728 
00729                 /* Phase generation is based on: */
00730                 /* HH  (13) channel 7->slot 1 combined with channel 8->slot 2 (same combination as TOP CYMBAL but different output phases) */
00731                 /* SD  (16) channel 7->slot 1 */
00732                 /* TOM (14) channel 8->slot 1 */
00733                 /* TOP (17) channel 7->slot 1 combined with channel 8->slot 2 (same combination as HIGH HAT but different output phases) */
00734 
00735                 /* Envelope generation based on: */
00736                 /* HH  channel 7->slot1 */
00737                 /* SD  channel 7->slot2 */
00738                 /* TOM channel 8->slot1 */
00739                 /* TOP channel 8->slot2 */
00740 
00741 
00742                 /* The following formulas can be well optimized.
00743                    I leave them in direct form for now (in case I've missed something).
00744                 */
00745 
00746                 /* High Hat (verified on real YM3812) */
00747                 OPL_SLOT const &SLOT7_1 = P_CH[7].SLOT[SLOT1];
00748                 OPL_SLOT const &SLOT8_2 = P_CH[8].SLOT[SLOT2];
00749                 env = volume_calc(SLOT7_1);
00750                 if( env < ENV_QUIET )
00751                 {
00752                         /* high hat phase generation:
00753                             phase = d0 or 234 (based on frequency only)
00754                             phase = 34 or 2d0 (based on noise)
00755                         */
00756 
00757                         /* base frequency derived from operator 1 in channel 7 */
00758                         unsigned char const bit7 = BIT(SLOT7_1.Cnt >> FREQ_SH, 7);
00759                         unsigned char const bit3 = BIT(SLOT7_1.Cnt >> FREQ_SH, 3);
00760                         unsigned char const bit2 = BIT(SLOT7_1.Cnt >> FREQ_SH, 2);
00761 
00762                         unsigned char const res1 = (bit2 ^ bit7) | bit3;
00763 
00764                         /* when res1 = 0 phase = 0x000 | 0xd0; */
00765                         /* when res1 = 1 phase = 0x200 | (0xd0>>2); */
00766                         uint32_t phase = res1 ? (0x200|(0xd0>>2)) : 0xd0;
00767 
00768                         /* enable gate based on frequency of operator 2 in channel 8 */
00769                         unsigned char const bit5e= BIT(SLOT8_2.Cnt >> FREQ_SH, 5);
00770                         unsigned char const bit3e= BIT(SLOT8_2.Cnt >> FREQ_SH, 3);
00771 
00772                         unsigned char const res2 = bit3e ^ bit5e;
00773 
00774                         /* when res2 = 0 pass the phase from calculation above (res1); */
00775                         /* when res2 = 1 phase = 0x200 | (0xd0>>2); */
00776                         if (res2)
00777                                 phase = (0x200|(0xd0>>2));
00778 
00779 
00780                         /* when phase & 0x200 is set and noise=1 then phase = 0x200|0xd0 */
00781                         /* when phase & 0x200 is set and noise=0 then phase = 0x200|(0xd0>>2), ie no change */
00782                         if (phase&0x200)
00783                         {
00784                                 if (noise)
00785                                         phase = 0x200|0xd0;
00786                         }
00787                         else
00788                         /* when phase & 0x200 is clear and noise=1 then phase = 0xd0>>2 */
00789                         /* when phase & 0x200 is clear and noise=0 then phase = 0xd0, ie no change */
00790                         {
00791                                 if (noise)
00792                                         phase = 0xd0>>2;
00793                         }
00794 
00795                         output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_1.wavetable) * 2;
00796                 }
00797 
00798                 /* Snare Drum (verified on real YM3812) */
00799                 OPL_SLOT const &SLOT7_2 = P_CH[7].SLOT[SLOT2];
00800                 env = volume_calc(SLOT7_2);
00801                 if( env < ENV_QUIET )
00802                 {
00803                         /* base frequency derived from operator 1 in channel 7 */
00804                         unsigned char const bit8 = BIT(SLOT7_1.Cnt >> FREQ_SH, 8);
00805 
00806                         /* when bit8 = 0 phase = 0x100; */
00807                         /* when bit8 = 1 phase = 0x200; */
00808                         uint32_t phase = bit8 ? 0x200 : 0x100;
00809 
00810                         /* Noise bit XOR'es phase by 0x100 */
00811                         /* when noisebit = 0 pass the phase from calculation above */
00812                         /* when noisebit = 1 phase ^= 0x100; */
00813                         /* in other words: phase ^= (noisebit<<8); */
00814                         if (noise)
00815                                 phase ^= 0x100;
00816 
00817                         output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_2.wavetable) * 2;
00818                 }
00819 
00820                 /* Tom Tom (verified on real YM3812) */
00821                 OPL_SLOT const &SLOT8_1 = P_CH[8].SLOT[SLOT1];
00822                 env = volume_calc(SLOT8_1);
00823                 if( env < ENV_QUIET )
00824                         output[0] += op_calc(SLOT8_1.Cnt, env, 0, SLOT8_1.wavetable) * 2;
00825 
00826                 /* Top Cymbal (verified on real YM3812) */
00827                 env = volume_calc(SLOT8_2);
00828                 if( env < ENV_QUIET )
00829                 {
00830                         /* base frequency derived from operator 1 in channel 7 */
00831                         unsigned char const bit7 = BIT(SLOT7_1.Cnt >> FREQ_SH, 7);
00832                         unsigned char const bit3 = BIT(SLOT7_1.Cnt >> FREQ_SH, 3);
00833                         unsigned char const bit2 = BIT(SLOT7_1.Cnt >> FREQ_SH, 2);
00834 
00835                         unsigned char const res1 = (bit2 ^ bit7) | bit3;
00836 
00837                         /* when res1 = 0 phase = 0x000 | 0x100; */
00838                         /* when res1 = 1 phase = 0x200 | 0x100; */
00839                         uint32_t phase = res1 ? 0x300 : 0x100;
00840 
00841                         /* enable gate based on frequency of operator 2 in channel 8 */
00842                         unsigned char const bit5e= BIT(SLOT8_2.Cnt >> FREQ_SH, 5);
00843                         unsigned char const bit3e= BIT(SLOT8_2.Cnt >> FREQ_SH, 3);
00844 
00845                         unsigned char const res2 = bit3e ^ bit5e;
00846                         /* when res2 = 0 pass the phase from calculation above (res1); */
00847                         /* when res2 = 1 phase = 0x200 | 0x100; */
00848                         if (res2)
00849                                 phase = 0x300;
00850 
00851                         output[0] += op_calc(phase<<FREQ_SH, env, 0, SLOT8_2.wavetable) * 2;
00852                 }
00853         }
00854 
00855 
00856         void initialize();
00857 
00858 
00859         /* set multi,am,vib,EG-TYP,KSR,mul */
00860         void set_mul(int slot, int v)
00861         {
00862                 OPL_CH   &CH   = P_CH[slot/2];
00863                 OPL_SLOT &SLOT = CH.SLOT[slot&1];
00864 
00865                 SLOT.mul     = mul_tab[v&0x0f];
00866                 SLOT.KSR     = (v & 0x10) ? 0 : 2;
00867                 SLOT.eg_type = (v & 0x20);
00868                 SLOT.vib     = (v & 0x40);
00869                 SLOT.AMmask  = (v & 0x80) ? ~0 : 0;
00870                 CH.CALC_FCSLOT(SLOT);
00871         }
00872 
00873         /* set ksl & tl */
00874         void set_ksl_tl(int slot, int v)
00875         {
00876                 OPL_CH   &CH   = P_CH[slot/2];
00877                 OPL_SLOT &SLOT = CH.SLOT[slot&1];
00878 
00879                 SLOT.ksl = ksl_shift[v >> 6];
00880                 SLOT.TL  = (v&0x3f)<<(ENV_BITS-1-7); /* 7 bits TL (bit 6 = always 0) */
00881 
00882                 SLOT.TLL = SLOT.TL + (CH.ksl_base >> SLOT.ksl);
00883         }
00884 
00885         /* set attack rate & decay rate  */
00886         void set_ar_dr(int slot, int v)
00887         {
00888                 OPL_CH   &CH   = P_CH[slot/2];
00889                 OPL_SLOT &SLOT = CH.SLOT[slot&1];
00890 
00891                 SLOT.ar = (v>>4)  ? 16 + ((v>>4)  <<2) : 0;
00892 
00893                 if ((SLOT.ar + SLOT.ksr) < 16+62)
00894                 {
00895                         SLOT.eg_sh_ar  = eg_rate_shift [SLOT.ar + SLOT.ksr ];
00896                         SLOT.eg_sel_ar = eg_rate_select[SLOT.ar + SLOT.ksr ];
00897                 }
00898                 else
00899                 {
00900                         SLOT.eg_sh_ar  = 0;
00901                         SLOT.eg_sel_ar = 13*RATE_STEPS;
00902                 }
00903 
00904                 SLOT.dr    = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
00905                 SLOT.eg_sh_dr  = eg_rate_shift [SLOT.dr + SLOT.ksr ];
00906                 SLOT.eg_sel_dr = eg_rate_select[SLOT.dr + SLOT.ksr ];
00907         }
00908 
00909         /* set sustain level & release rate */
00910         void set_sl_rr(int slot, int v)
00911         {
00912                 OPL_CH   &CH   = P_CH[slot/2];
00913                 OPL_SLOT &SLOT = CH.SLOT[slot&1];
00914 
00915                 SLOT.sl  = sl_tab[ v>>4 ];
00916 
00917                 SLOT.rr  = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
00918                 SLOT.eg_sh_rr  = eg_rate_shift [SLOT.rr + SLOT.ksr ];
00919                 SLOT.eg_sel_rr = eg_rate_select[SLOT.rr + SLOT.ksr ];
00920         }
00921 
00922 
00923         void WriteReg(int r, int v);
00924         void ResetChip();
00925         //void postload();
00926 
00927 
00928         /* lock/unlock for common table */
00929         static int LockTable(device_t *device)
00930         {
00931         (void)device;
00932                 num_lock++;
00933                 if(num_lock>1) return 0;
00934 
00935                 /* first time */
00936 
00937                 /* allocate total level table (128kb space) */
00938                 if( !init_tables() )
00939                 {
00940                         num_lock--;
00941                         return -1;
00942                 }
00943 
00944                 return 0;
00945         }
00946 
00947         static void UnLockTable()
00948         {
00949                 if(num_lock) num_lock--;
00950                 if(num_lock) return;
00951 
00952                 /* last time */
00953                 CloseTable();
00954         }
00955 
00956 private:
00957         uint32_t volume_calc(OPL_SLOT const &OP) const
00958         {
00959                 return OP.TLL + uint32_t(OP.volume) + (LFO_AM & OP.AMmask);
00960         }
00961 
00962         static inline signed int op_calc(uint32_t phase, unsigned int env, signed int pm, unsigned int wave_tab)
00963         {
00964                 uint32_t const p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + (pm<<16))) >> FREQ_SH ) & SIN_MASK) ];
00965 
00966                 return (p >= TL_TAB_LEN) ? 0 : tl_tab[p];
00967         }
00968 
00969         static inline signed int op_calc1(uint32_t phase, unsigned int env, signed int pm, unsigned int wave_tab)
00970         {
00971                 uint32_t const p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + pm      )) >> FREQ_SH ) & SIN_MASK) ];
00972 
00973                 return (p >= TL_TAB_LEN) ? 0 : tl_tab[p];
00974         }
00975 
00976 
00977         static int init_tables();
00978 
00979         static void CloseTable()
00980         {
00981 #ifdef SAVE_SAMPLE
00982                 fclose(sample[0]);
00983 #endif
00984         }
00985 
00986         static const double ksl_tab[8*16];
00987         static const uint32_t ksl_shift[4];
00988         static const int32_t sl_tab[16];
00989         static const unsigned char eg_inc[15 * RATE_STEPS];
00990 
00991         static const uint8_t mul_tab[16];
00992         static signed int tl_tab[TL_TAB_LEN];
00993         static unsigned int sin_tab[SIN_LEN * 4];
00994 
00995         static const uint8_t lfo_am_table[LFO_AM_TAB_ELEMENTS];
00996         static const int8_t lfo_pm_table[8 * 8 * 2];
00997 
00998         static int num_lock;
00999 };
01000 
01001 
01002 
01003 /* mapping of register number (offset) to slot number used by the emulator */
01004 static const int slot_array[32]=
01005 {
01006                 0, 2, 4, 1, 3, 5,-1,-1,
01007                 6, 8,10, 7, 9,11,-1,-1,
01008         12,14,16,13,15,17,-1,-1,
01009         -1,-1,-1,-1,-1,-1,-1,-1
01010 };
01011 
01012 /* key scale level */
01013 /* table is 3dB/octave , DV converts this into 6dB/octave */
01014 /* 0.1875 is bit 0 weight of the envelope counter (volume) expressed in the 'decibel' scale */
01015 const double FM_OPL::ksl_tab[8*16]=
01016 {
01017         /* OCT 0 */
01018                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01019                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01020                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01021                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01022         /* OCT 1 */
01023                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01024                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01025                 0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
01026                 1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
01027         /* OCT 2 */
01028                 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
01029                 0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
01030                 3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
01031                 4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
01032         /* OCT 3 */
01033                 0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
01034                 3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
01035                 6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
01036                 7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
01037         /* OCT 4 */
01038                 0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
01039                 6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
01040                 9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
01041                 10.875/DV,11.250/DV,11.625/DV,12.000/DV,
01042         /* OCT 5 */
01043                 0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
01044                 9.000/DV,10.125/DV,10.875/DV,11.625/DV,
01045                 12.000/DV,12.750/DV,13.125/DV,13.500/DV,
01046                 13.875/DV,14.250/DV,14.625/DV,15.000/DV,
01047         /* OCT 6 */
01048                 0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
01049                 12.000/DV,13.125/DV,13.875/DV,14.625/DV,
01050                 15.000/DV,15.750/DV,16.125/DV,16.500/DV,
01051                 16.875/DV,17.250/DV,17.625/DV,18.000/DV,
01052         /* OCT 7 */
01053                 0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
01054                 15.000/DV,16.125/DV,16.875/DV,17.625/DV,
01055                 18.000/DV,18.750/DV,19.125/DV,19.500/DV,
01056                 19.875/DV,20.250/DV,20.625/DV,21.000/DV
01057 };
01058 
01059 /* 0 / 3.0 / 1.5 / 6.0 dB/OCT */
01060 const uint32_t FM_OPL::ksl_shift[4] = { 31, 1, 2, 0 };
01061 
01062 
01063 /* sustain level table (3dB per step) */
01064 /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
01065 #define SC(db)                  int32_t(db * (2.0 / ENV_STEP))
01066 const int32_t FM_OPL::sl_tab[16]={
01067         SC( 0),SC( 1),SC( 2),SC( 3),SC( 4),SC( 5),SC( 6),SC( 7),
01068         SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
01069 };
01070 
01071 
01072 const unsigned char FM_OPL::eg_inc[15*RATE_STEPS]={
01073 /*cycle:0 1  2 3  4 5  6 7*/
01074 
01075 /* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..12 0 (increment by 0 or 1) */
01076 /* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..12 1 */
01077 /* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..12 2 */
01078 /* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..12 3 */
01079 
01080 /* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 13 0 (increment by 1) */
01081 /* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 13 1 */
01082 /* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 13 2 */
01083 /* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 13 3 */
01084 
01085 /* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 14 0 (increment by 2) */
01086 /* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 14 1 */
01087 /*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 14 2 */
01088 /*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 14 3 */
01089 
01090 /*12 */ 4,4, 4,4, 4,4, 4,4, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 4) */
01091 /*13 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 2, 15 3 for attack */
01092 /*14 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
01093 };
01094 
01095 
01096 #define O(a) (a*RATE_STEPS)
01097 
01098 /*note that there is no O(13) in this table - it's directly in the code */
01099 const unsigned char eg_rate_select[16+64+16]={   /* Envelope Generator rates (16 + 64 rates + 16 RKS) */
01100 /* 16 infinite time rates */
01101 O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
01102 O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
01103 
01104 /* rates 00-12 */
01105 O( 0),O( 1),O( 2),O( 3),
01106 O( 0),O( 1),O( 2),O( 3),
01107 O( 0),O( 1),O( 2),O( 3),
01108 O( 0),O( 1),O( 2),O( 3),
01109 O( 0),O( 1),O( 2),O( 3),
01110 O( 0),O( 1),O( 2),O( 3),
01111 O( 0),O( 1),O( 2),O( 3),
01112 O( 0),O( 1),O( 2),O( 3),
01113 O( 0),O( 1),O( 2),O( 3),
01114 O( 0),O( 1),O( 2),O( 3),
01115 O( 0),O( 1),O( 2),O( 3),
01116 O( 0),O( 1),O( 2),O( 3),
01117 O( 0),O( 1),O( 2),O( 3),
01118 
01119 /* rate 13 */
01120 O( 4),O( 5),O( 6),O( 7),
01121 
01122 /* rate 14 */
01123 O( 8),O( 9),O(10),O(11),
01124 
01125 /* rate 15 */
01126 O(12),O(12),O(12),O(12),
01127 
01128 /* 16 dummy rates (same as 15 3) */
01129 O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
01130 O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
01131 
01132 };
01133 #undef O
01134 
01135 /*rate  0,    1,    2,    3,   4,   5,   6,  7,  8,  9,  10, 11, 12, 13, 14, 15 */
01136 /*shift 12,   11,   10,   9,   8,   7,   6,  5,  4,  3,  2,  1,  0,  0,  0,  0  */
01137 /*mask  4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7,  3,  1,  0,  0,  0,  0  */
01138 
01139 #define O(a) (a*1)
01140 const unsigned char eg_rate_shift[16+64+16]={    /* Envelope Generator counter shifts (16 + 64 rates + 16 RKS) */
01141 /* 16 infinite time rates */
01142 O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
01143 O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
01144 
01145 /* rates 00-12 */
01146 O(12),O(12),O(12),O(12),
01147 O(11),O(11),O(11),O(11),
01148 O(10),O(10),O(10),O(10),
01149 O( 9),O( 9),O( 9),O( 9),
01150 O( 8),O( 8),O( 8),O( 8),
01151 O( 7),O( 7),O( 7),O( 7),
01152 O( 6),O( 6),O( 6),O( 6),
01153 O( 5),O( 5),O( 5),O( 5),
01154 O( 4),O( 4),O( 4),O( 4),
01155 O( 3),O( 3),O( 3),O( 3),
01156 O( 2),O( 2),O( 2),O( 2),
01157 O( 1),O( 1),O( 1),O( 1),
01158 O( 0),O( 0),O( 0),O( 0),
01159 
01160 /* rate 13 */
01161 O( 0),O( 0),O( 0),O( 0),
01162 
01163 /* rate 14 */
01164 O( 0),O( 0),O( 0),O( 0),
01165 
01166 /* rate 15 */
01167 O( 0),O( 0),O( 0),O( 0),
01168 
01169 /* 16 dummy rates (same as 15 3) */
01170 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
01171 O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
01172 
01173 };
01174 #undef O
01175 
01176 
01177 /* multiple table */
01178 #define ML 2
01179 const uint8_t FM_OPL::mul_tab[16]= {
01180 /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,10,12,12,15,15 */
01181         ML/2, 1*ML, 2*ML, 3*ML, 4*ML, 5*ML, 6*ML, 7*ML,
01182         8*ML, 9*ML,10*ML,10*ML,12*ML,12*ML,15*ML,15*ML
01183 };
01184 #undef ML
01185 
01186 signed int FM_OPL::tl_tab[TL_TAB_LEN];
01187 
01188 /* sin waveform table in 'decibel' scale */
01189 /* four waveforms on OPL2 type chips */
01190 unsigned int FM_OPL::sin_tab[SIN_LEN * 4];
01191 
01192 
01193 /* LFO Amplitude Modulation table (verified on real YM3812)
01194    27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples
01195 
01196    Length: 210 elements.
01197 
01198     Each of the elements has to be repeated
01199     exactly 64 times (on 64 consecutive samples).
01200     The whole table takes: 64 * 210 = 13440 samples.
01201 
01202     When AM = 1 data is used directly
01203     When AM = 0 data is divided by 4 before being used (losing precision is important)
01204 */
01205 
01206 const uint8_t FM_OPL::lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
01207 0,0,0,0,0,0,0,
01208 1,1,1,1,
01209 2,2,2,2,
01210 3,3,3,3,
01211 4,4,4,4,
01212 5,5,5,5,
01213 6,6,6,6,
01214 7,7,7,7,
01215 8,8,8,8,
01216 9,9,9,9,
01217 10,10,10,10,
01218 11,11,11,11,
01219 12,12,12,12,
01220 13,13,13,13,
01221 14,14,14,14,
01222 15,15,15,15,
01223 16,16,16,16,
01224 17,17,17,17,
01225 18,18,18,18,
01226 19,19,19,19,
01227 20,20,20,20,
01228 21,21,21,21,
01229 22,22,22,22,
01230 23,23,23,23,
01231 24,24,24,24,
01232 25,25,25,25,
01233 26,26,26,
01234 25,25,25,25,
01235 24,24,24,24,
01236 23,23,23,23,
01237 22,22,22,22,
01238 21,21,21,21,
01239 20,20,20,20,
01240 19,19,19,19,
01241 18,18,18,18,
01242 17,17,17,17,
01243 16,16,16,16,
01244 15,15,15,15,
01245 14,14,14,14,
01246 13,13,13,13,
01247 12,12,12,12,
01248 11,11,11,11,
01249 10,10,10,10,
01250 9,9,9,9,
01251 8,8,8,8,
01252 7,7,7,7,
01253 6,6,6,6,
01254 5,5,5,5,
01255 4,4,4,4,
01256 3,3,3,3,
01257 2,2,2,2,
01258 1,1,1,1
01259 };
01260 
01261 /* LFO Phase Modulation table (verified on real YM3812) */
01262 const int8_t FM_OPL::lfo_pm_table[8*8*2] = {
01263 /* FNUM2/FNUM = 00 0xxxxxxx (0x0000) */
01264 0, 0, 0, 0, 0, 0, 0, 0, /*LFO PM depth = 0*/
01265 0, 0, 0, 0, 0, 0, 0, 0, /*LFO PM depth = 1*/
01266 
01267 /* FNUM2/FNUM = 00 1xxxxxxx (0x0080) */
01268 0, 0, 0, 0, 0, 0, 0, 0, /*LFO PM depth = 0*/
01269 1, 0, 0, 0,-1, 0, 0, 0, /*LFO PM depth = 1*/
01270 
01271 /* FNUM2/FNUM = 01 0xxxxxxx (0x0100) */
01272 1, 0, 0, 0,-1, 0, 0, 0, /*LFO PM depth = 0*/
01273 2, 1, 0,-1,-2,-1, 0, 1, /*LFO PM depth = 1*/
01274 
01275 /* FNUM2/FNUM = 01 1xxxxxxx (0x0180) */
01276 1, 0, 0, 0,-1, 0, 0, 0, /*LFO PM depth = 0*/
01277 3, 1, 0,-1,-3,-1, 0, 1, /*LFO PM depth = 1*/
01278 
01279 /* FNUM2/FNUM = 10 0xxxxxxx (0x0200) */
01280 2, 1, 0,-1,-2,-1, 0, 1, /*LFO PM depth = 0*/
01281 4, 2, 0,-2,-4,-2, 0, 2, /*LFO PM depth = 1*/
01282 
01283 /* FNUM2/FNUM = 10 1xxxxxxx (0x0280) */
01284 2, 1, 0,-1,-2,-1, 0, 1, /*LFO PM depth = 0*/
01285 5, 2, 0,-2,-5,-2, 0, 2, /*LFO PM depth = 1*/
01286 
01287 /* FNUM2/FNUM = 11 0xxxxxxx (0x0300) */
01288 3, 1, 0,-1,-3,-1, 0, 1, /*LFO PM depth = 0*/
01289 6, 3, 0,-3,-6,-3, 0, 3, /*LFO PM depth = 1*/
01290 
01291 /* FNUM2/FNUM = 11 1xxxxxxx (0x0380) */
01292 3, 1, 0,-1,-3,-1, 0, 1, /*LFO PM depth = 0*/
01293 7, 3, 0,-3,-7,-3, 0, 3  /*LFO PM depth = 1*/
01294 };
01295 
01296 
01297 /* lock level of common table */
01298 int FM_OPL::num_lock = 0;
01299 
01300 
01301 
01302 static inline int limit( int val, int max, int min ) {
01303         if ( val > max )
01304                 val = max;
01305         else if ( val < min )
01306                 val = min;
01307 
01308         return val;
01309 }
01310 
01311 
01312 /* generic table initialize */
01313 int FM_OPL::init_tables()
01314 {
01315         signed int i,x;
01316         signed int n;
01317         double o,m;
01318 
01319 
01320         for (x=0; x<TL_RES_LEN; x++)
01321         {
01322                 m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
01323                 m = floor(m);
01324 
01325                 /* we never reach (1<<16) here due to the (x+1) */
01326                 /* result fits within 16 bits at maximum */
01327 
01328                 n = (int)m;     /* 16 bits here */
01329                 n >>= 4;        /* 12 bits here */
01330                 if (n&1)        /* round to nearest */
01331                         n = (n>>1)+1;
01332                 else
01333                         n = n>>1;
01334                                                 /* 11 bits here (rounded) */
01335                 n <<= 1;        /* 12 bits here (as in real chip) */
01336                 tl_tab[ x*2 + 0 ] = n;
01337                 tl_tab[ x*2 + 1 ] = -tl_tab[ x*2 + 0 ];
01338 
01339                 for (i=1; i<12; i++)
01340                 {
01341                         tl_tab[ x*2+0 + i*2*TL_RES_LEN ] =  tl_tab[ x*2+0 ]>>i;
01342                         tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = -tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
01343                 }
01344         #if 0
01345                         logerror("tl %04i", x*2);
01346                         for (i=0; i<12; i++)
01347                                 logerror(", [%02i] %5i", i*2, tl_tab[ x*2 /*+1*/ + i*2*TL_RES_LEN ] );
01348                         logerror("\n");
01349         #endif
01350         }
01351         /*logerror("FMOPL.C: TL_TAB_LEN = %i elements (%i bytes)\n",TL_TAB_LEN, (int)sizeof(tl_tab));*/
01352 
01353 
01354         for (i=0; i<SIN_LEN; i++)
01355         {
01356                 /* non-standard sinus */
01357                 m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
01358 
01359                 /* we never reach zero here due to ((i*2)+1) */
01360 
01361                 if (m>0.0)
01362                         o = 8*log(1.0/m)/log(2.0);  /* convert to 'decibels' */
01363                 else
01364                         o = 8*log(-1.0/m)/log(2.0); /* convert to 'decibels' */
01365 
01366                 o = o / (ENV_STEP/4);
01367 
01368                 n = (int)(2.0*o);
01369                 if (n&1)                        /* round to nearest */
01370                         n = (n>>1)+1;
01371                 else
01372                         n = n>>1;
01373 
01374                 sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
01375 
01376                 /*logerror("FMOPL.C: sin [%4i (hex=%03x)]= %4i (tl_tab value=%5i)\n", i, i, sin_tab[i], tl_tab[sin_tab[i]] );*/
01377         }
01378 
01379         for (i=0; i<SIN_LEN; i++)
01380         {
01381                 /* waveform 1:  __      __     */
01382                 /*             /  \____/  \____*/
01383                 /* output only first half of the sinus waveform (positive one) */
01384 
01385                 if (i & (1<<(SIN_BITS-1)) )
01386                         sin_tab[1*SIN_LEN+i] = TL_TAB_LEN;
01387                 else
01388                         sin_tab[1*SIN_LEN+i] = sin_tab[i];
01389 
01390                 /* waveform 2:  __  __  __  __ */
01391                 /*             /  \/  \/  \/  \*/
01392                 /* abs(sin) */
01393 
01394                 sin_tab[2*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>1) ];
01395 
01396                 /* waveform 3:  _   _   _   _  */
01397                 /*             / |_/ |_/ |_/ |_*/
01398                 /* abs(output only first quarter of the sinus waveform) */
01399 
01400                 if (i & (1<<(SIN_BITS-2)) )
01401                         sin_tab[3*SIN_LEN+i] = TL_TAB_LEN;
01402                 else
01403                         sin_tab[3*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>2)];
01404 
01405                 /*logerror("FMOPL.C: sin1[%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[1*SIN_LEN+i], tl_tab[sin_tab[1*SIN_LEN+i]] );
01406                 logerror("FMOPL.C: sin2[%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[2*SIN_LEN+i], tl_tab[sin_tab[2*SIN_LEN+i]] );
01407                 logerror("FMOPL.C: sin3[%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[3*SIN_LEN+i], tl_tab[sin_tab[3*SIN_LEN+i]] );*/
01408         }
01409         /*logerror("FMOPL.C: ENV_QUIET= %08x (dec*8=%i)\n", ENV_QUIET, ENV_QUIET*8 );*/
01410 
01411 
01412 #ifdef SAVE_SAMPLE
01413         sample[0]=fopen("sampsum.pcm","wb");
01414 #endif
01415 
01416         return 1;
01417 }
01418 
01419 
01420 void FM_OPL::initialize()
01421 {
01422         int i;
01423 
01424         /* frequency base */
01425         freqbase  = (rate) ? ((double)clock / 72.0) / rate  : 0;
01426 #if 0
01427         rate = (double)clock / 72.0;
01428         freqbase  = 1.0;
01429 #endif
01430 
01431         /*logerror("freqbase=%f\n", freqbase);*/
01432 
01433         /* Timer base time */
01434         //TimerBase = attotime::from_hz(clock) * 72;
01435 
01436         /* make fnumber -> increment counter table */
01437         for( i=0 ; i < 1024 ; i++ )
01438         {
01439                 /* opn phase increment counter = 20bit */
01440                 fn_tab[i] = (uint32_t)( (double)i * 64 * freqbase * (1<<(FREQ_SH-10)) ); /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
01441 #if 0
01442                 logerror("FMOPL.C: fn_tab[%4i] = %08x (dec=%8i)\n",
01443                                         i, fn_tab[i]>>6, fn_tab[i]>>6 );
01444 #endif
01445         }
01446 
01447 #if 0
01448         for( i=0 ; i < 16 ; i++ )
01449         {
01450                 logerror("FMOPL.C: sl_tab[%i] = %08x\n",
01451                         i, sl_tab[i] );
01452         }
01453         for( i=0 ; i < 8 ; i++ )
01454         {
01455                 int j;
01456                 logerror("FMOPL.C: ksl_tab[oct=%2i] =",i);
01457                 for (j=0; j<16; j++)
01458                 {
01459                         logerror("%08x ", static_cast<uint32_t>(ksl_tab[i*16+j]) );
01460                 }
01461                 logerror("\n");
01462         }
01463 #endif
01464 
01465 
01466         /* Amplitude modulation: 27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples */
01467         /* One entry from LFO_AM_TABLE lasts for 64 samples */
01468         lfo_am_inc = (uint32_t)((1.0 / 64.0 ) * (1<<LFO_SH) * freqbase);
01469 
01470         /* Vibrato: 8 output levels (triangle waveform); 1 level takes 1024 samples */
01471         lfo_pm_inc = (uint32_t)((1.0 / 1024.0) * (1<<LFO_SH) * freqbase);
01472 
01473         /*logerror ("lfo_am_inc = %8x ; lfo_pm_inc = %8x\n", lfo_am_inc, lfo_pm_inc);*/
01474 
01475         /* Noise generator: a step takes 1 sample */
01476         noise_f = (uint32_t)((1.0 / 1.0) * (1<<FREQ_SH) * freqbase);
01477 
01478         eg_timer_add = (uint32_t)((1<<EG_SH) * freqbase);
01479         eg_timer_overflow = ( 1 ) * (1<<EG_SH);
01480         /*logerror("OPLinit eg_timer_add=%8x eg_timer_overflow=%8x\n", eg_timer_add, eg_timer_overflow);*/
01481 }
01482 
01483 
01484 /* write a value v to register r on OPL chip */
01485 void FM_OPL::WriteReg(int r, int v)
01486 {
01487         OPL_CH *CH;
01488         int slot;
01489         int block_fnum;
01490 
01491 
01492         /* adjust bus to 8 bits */
01493         r &= 0xff;
01494         v &= 0xff;
01495 
01496         switch(r&0xe0)
01497         {
01498         case 0x00:  /* 00-1f:control */
01499                 switch(r&0x1f)
01500                 {
01501                 case 0x01:  /* waveform select enable */
01502                         if(type&OPL_TYPE_WAVESEL)
01503                         {
01504                                 wavesel = v&0x20;
01505                                 /* do not change the waveform previously selected */
01506                         }
01507                         break;
01508                 case 0x02:  /* Timer 1 */
01509                         T[0] = (256-v)*4;
01510                         break;
01511                 case 0x03:  /* Timer 2 */
01512                         T[1] = (256-v)*16;
01513                         break;
01514 #if 0
01515                 case 0x04:  /* IRQ clear / mask and Timer enable */
01516                         if(v&0x80)
01517                         {   /* IRQ flag clear */
01518                                 STATUS_RESET(0x7f-0x08); /* don't reset BFRDY flag or we will have to call deltat module to set the flag */
01519                         }
01520                         else
01521                         {   /* set IRQ mask ,timer enable*/
01522                                 uint8_t st1 = v&1;
01523                                 uint8_t st2 = (v>>1)&1;
01524 
01525                                 /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
01526                                 STATUS_RESET(v & (0x78-0x08));
01527                                 STATUSMASK_SET((~v) & 0x78);
01528 
01529                                 /* timer 2 */
01530                                 if(st[1] != st2)
01531                                 {
01532                                         attotime period = st2 ? (TimerBase * T[1]) : attotime::zero;
01533                                         st[1] = st2;
01534                                         if (timer_handler) (timer_handler)(TimerParam,1,period);
01535                                 }
01536                                 /* timer 1 */
01537                                 if(st[0] != st1)
01538                                 {
01539                                         attotime period = st1 ? (TimerBase * T[0]) : attotime::zero;
01540                                         st[0] = st1;
01541                                         if (timer_handler) (timer_handler)(TimerParam,0,period);
01542                                 }
01543                         }
01544                         break;
01545 #endif
01546 #if BUILD_Y8950
01547                 case 0x06:      /* Key Board OUT */
01548                         if(type&OPL_TYPE_KEYBOARD)
01549                         {
01550                                 if(keyboardhandler_w)
01551                                         keyboardhandler_w(keyboard_param,v);
01552                                 else
01553                                         device->logerror("Y8950: write unmapped KEYBOARD port\n");
01554                         }
01555                         break;
01556                 case 0x07:  /* DELTA-T control 1 : START,REC,MEMDATA,REPT,SPOFF,x,x,RST */
01557                         if(type&OPL_TYPE_ADPCM)
01558                                 deltat->ADPCM_Write(r-0x07,v);
01559                         break;
01560 #endif
01561                 case 0x08:  /* MODE,DELTA-T control 2 : CSM,NOTESEL,x,x,smpl,da/ad,64k,rom */
01562                         mode = v;
01563 #if BUILD_Y8950
01564                         if(type&OPL_TYPE_ADPCM)
01565                                 deltat->ADPCM_Write(r-0x07,v&0x0f); /* mask 4 LSBs in register 08 for DELTA-T unit */
01566 #endif
01567                         break;
01568 
01569 #if BUILD_Y8950
01570                 case 0x09:      /* START ADD */
01571                 case 0x0a:
01572                 case 0x0b:      /* STOP ADD  */
01573                 case 0x0c:
01574                 case 0x0d:      /* PRESCALE   */
01575                 case 0x0e:
01576                 case 0x0f:      /* ADPCM data write */
01577                 case 0x10:      /* DELTA-N    */
01578                 case 0x11:      /* DELTA-N    */
01579                 case 0x12:      /* ADPCM volume */
01580                         if(type&OPL_TYPE_ADPCM)
01581                                 deltat->ADPCM_Write(r-0x07,v);
01582                         break;
01583 
01584                 case 0x15:      /* DAC data high 8 bits (F7,F6...F2) */
01585                 case 0x16:      /* DAC data low 2 bits (F1, F0 in bits 7,6) */
01586                 case 0x17:      /* DAC data shift (S2,S1,S0 in bits 2,1,0) */
01587                         device->logerror("FMOPL.C: DAC data register written, but not implemented reg=%02x val=%02x\n",r,v);
01588                         break;
01589 
01590                 case 0x18:      /* I/O CTRL (Direction) */
01591                         if(type&OPL_TYPE_IO)
01592                                 portDirection = v&0x0f;
01593                         break;
01594                 case 0x19:      /* I/O DATA */
01595                         if(type&OPL_TYPE_IO)
01596                         {
01597                                 portLatch = v;
01598                                 if(porthandler_w)
01599                                         porthandler_w(port_param,v&portDirection);
01600                         }
01601                         break;
01602 #endif
01603                 default:
01604                         device->logerror("FMOPL.C: write to unknown register: %02x\n",r);
01605                         break;
01606                 }
01607                 break;
01608         case 0x20:  /* am ON, vib ON, ksr, eg_type, mul */
01609                 slot = slot_array[r&0x1f];
01610                 if(slot < 0) return;
01611                 set_mul(slot,v);
01612                 break;
01613         case 0x40:
01614                 slot = slot_array[r&0x1f];
01615                 if(slot < 0) return;
01616                 set_ksl_tl(slot,v);
01617                 break;
01618         case 0x60:
01619                 slot = slot_array[r&0x1f];
01620                 if(slot < 0) return;
01621                 set_ar_dr(slot,v);
01622                 break;
01623         case 0x80:
01624                 slot = slot_array[r&0x1f];
01625                 if(slot < 0) return;
01626                 set_sl_rr(slot,v);
01627                 break;
01628         case 0xa0:
01629                 if (r == 0xbd)          /* am depth, vibrato depth, r,bd,sd,tom,tc,hh */
01630                 {
01631                         lfo_am_depth = v & 0x80;
01632                         lfo_pm_depth_range = (v&0x40) ? 8 : 0;
01633 
01634                         rhythm  = v&0x3f;
01635 
01636                         if(rhythm&0x20)
01637                         {
01638                                 /* BD key on/off */
01639                                 if(v&0x10)
01640                                 {
01641                                         P_CH[6].SLOT[SLOT1].KEYON(2);
01642                                         P_CH[6].SLOT[SLOT2].KEYON(2);
01643                                 }
01644                                 else
01645                                 {
01646                                         P_CH[6].SLOT[SLOT1].KEYOFF(~2);
01647                                         P_CH[6].SLOT[SLOT2].KEYOFF(~2);
01648                                 }
01649                                 /* HH key on/off */
01650                                 if(v&0x01) P_CH[7].SLOT[SLOT1].KEYON ( 2);
01651                                 else       P_CH[7].SLOT[SLOT1].KEYOFF(~2);
01652                                 /* SD key on/off */
01653                                 if(v&0x08) P_CH[7].SLOT[SLOT2].KEYON ( 2);
01654                                 else       P_CH[7].SLOT[SLOT2].KEYOFF(~2);
01655                                 /* TOM key on/off */
01656                                 if(v&0x04) P_CH[8].SLOT[SLOT1].KEYON ( 2);
01657                                 else       P_CH[8].SLOT[SLOT1].KEYOFF(~2);
01658                                 /* TOP-CY key on/off */
01659                                 if(v&0x02) P_CH[8].SLOT[SLOT2].KEYON ( 2);
01660                                 else       P_CH[8].SLOT[SLOT2].KEYOFF(~2);
01661                         }
01662                         else
01663                         {
01664                                 /* BD key off */
01665                                 P_CH[6].SLOT[SLOT1].KEYOFF(~2);
01666                                 P_CH[6].SLOT[SLOT2].KEYOFF(~2);
01667                                 /* HH key off */
01668                                 P_CH[7].SLOT[SLOT1].KEYOFF(~2);
01669                                 /* SD key off */
01670                                 P_CH[7].SLOT[SLOT2].KEYOFF(~2);
01671                                 /* TOM key off */
01672                                 P_CH[8].SLOT[SLOT1].KEYOFF(~2);
01673                                 /* TOP-CY off */
01674                                 P_CH[8].SLOT[SLOT2].KEYOFF(~2);
01675                         }
01676                         return;
01677                 }
01678                 /* keyon,block,fnum */
01679                 if( (r&0x0f) > 8) return;
01680                 CH = &P_CH[r&0x0f];
01681                 if(!(r&0x10))
01682                 {   /* a0-a8 */
01683                         block_fnum  = (CH->block_fnum&0x1f00) | v;
01684                 }
01685                 else
01686                 {   /* b0-b8 */
01687                         block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff);
01688 
01689                         if(v&0x20)
01690                         {
01691                                 CH->SLOT[SLOT1].KEYON ( 1);
01692                                 CH->SLOT[SLOT2].KEYON ( 1);
01693                         }
01694                         else
01695                         {
01696                                 CH->SLOT[SLOT1].KEYOFF(~1);
01697                                 CH->SLOT[SLOT2].KEYOFF(~1);
01698                         }
01699                 }
01700                 /* update */
01701                 if(CH->block_fnum != (unsigned int)block_fnum)
01702                 {
01703                         uint8_t block  = block_fnum >> 10;
01704 
01705                         CH->block_fnum = block_fnum;
01706 
01707                         CH->ksl_base = static_cast<uint32_t>(ksl_tab[block_fnum>>6]);
01708                         CH->fc       = fn_tab[block_fnum&0x03ff] >> (7-block);
01709 
01710                         /* BLK 2,1,0 bits -> bits 3,2,1 of kcode */
01711                         CH->kcode    = (CH->block_fnum&0x1c00)>>9;
01712 
01713                                 /* the info below is actually opposite to what is stated in the Manuals (verifed on real YM3812) */
01714                         /* if notesel == 0 -> lsb of kcode is bit 10 (MSB) of fnum  */
01715                         /* if notesel == 1 -> lsb of kcode is bit 9 (MSB-1) of fnum */
01716                         if (mode&0x40)
01717                                 CH->kcode |= (CH->block_fnum&0x100)>>8; /* notesel == 1 */
01718                         else
01719                                 CH->kcode |= (CH->block_fnum&0x200)>>9; /* notesel == 0 */
01720 
01721                         /* refresh Total Level in both SLOTs of this channel */
01722                         CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
01723                         CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
01724 
01725                         /* refresh frequency counter in both SLOTs of this channel */
01726                         CH->CALC_FCSLOT(CH->SLOT[SLOT1]);
01727                         CH->CALC_FCSLOT(CH->SLOT[SLOT2]);
01728                 }
01729                 break;
01730         case 0xc0:
01731                 /* FB,C */
01732                 if( (r&0x0f) > 8) return;
01733                 CH = &P_CH[r&0x0f];
01734                 CH->SLOT[SLOT1].FB  = (v>>1)&7 ? ((v>>1)&7) + 7 : 0;
01735                 CH->SLOT[SLOT1].CON = v&1;
01736                 CH->SLOT[SLOT1].connect1 = CH->SLOT[SLOT1].CON ? &output[0] : &phase_modulation;
01737                 break;
01738         case 0xe0: /* waveform select */
01739                 /* simply ignore write to the waveform select register if selecting not enabled in test register */
01740                 if(wavesel)
01741                 {
01742                         slot = slot_array[r&0x1f];
01743                         if(slot < 0) return;
01744                         CH = &P_CH[slot/2];
01745 
01746                         CH->SLOT[slot&1].wavetable = (v&0x03)*SIN_LEN;
01747                 }
01748                 break;
01749         }
01750 }
01751 
01752 
01753 void FM_OPL::ResetChip()
01754 {
01755         eg_timer = 0;
01756         eg_cnt   = 0;
01757 
01758         noise_rng = 1; /* noise shift register */
01759         mode   = 0;    /* normal mode */
01760         STATUS_RESET(0x7f);
01761 
01762         /* reset with register write */
01763         WriteReg(0x01,0); /* wavesel disable */
01764         WriteReg(0x02,0); /* Timer1 */
01765         WriteReg(0x03,0); /* Timer2 */
01766         WriteReg(0x04,0); /* IRQ mask clear */
01767         for(int i = 0xff ; i >= 0x20 ; i-- ) WriteReg(i,0);
01768 
01769         /* reset operator parameters */
01770 //      for(OPL_CH &CH : P_CH)
01771         for(unsigned int ch = 0; ch < sizeof( P_CH )/ sizeof(P_CH[0]); ch++)
01772         {
01773                 OPL_CH &CH = P_CH[ch];
01774 //              for(OPL_SLOT &SLOT : CH.SLOT)
01775                 for(unsigned int slot = 0; slot < sizeof( CH.SLOT ) / sizeof( CH.SLOT[0]); slot++)
01776                 {
01777                     
01778                         OPL_SLOT &SLOT = CH.SLOT[slot];
01779                         /* wave table */
01780                         SLOT.wavetable = 0;
01781                         SLOT.state     = EG_OFF;
01782                         SLOT.volume    = MAX_ATT_INDEX;
01783                 }
01784         }
01785 #if BUILD_Y8950
01786         if(type&OPL_TYPE_ADPCM)
01787         {
01788                 YM_DELTAT *DELTAT = deltat;
01789 
01790                 DELTAT->freqbase = freqbase;
01791                 DELTAT->output_pointer = &output_deltat[0];
01792                 DELTAT->portshift = 5;
01793                 DELTAT->output_range = 1<<23;
01794                 DELTAT->ADPCM_Reset(0,YM_DELTAT::EMULATION_MODE_NORMAL,device);
01795         }
01796 #endif
01797 }
01798 
01799 #if 0/*not used*/
01800 void FM_OPL::postload()
01801 {
01802         for(unsigned int ch = 0; ch < sizeof( P_CH )/ sizeof(P_CH[0]); ch++)
01803         {
01804                 OPL_CH &CH = P_CH[ch];
01805                 /* Look up key scale level */
01806                 uint32_t const block_fnum = CH.block_fnum;
01807                 CH.ksl_base = static_cast<uint32_t>(ksl_tab[block_fnum >> 6]);
01808                 CH.fc       = fn_tab[block_fnum & 0x03ff] >> (7 - (block_fnum >> 10));
01809 
01810                 for(unsigned int slot = 0; slot < sizeof( CH.SLOT ) / sizeof( CH.SLOT[0]); slot++)
01811                 {
01812                         OPL_SLOT &SLOT = CH.SLOT[slot];
01813                         /* Calculate key scale rate */
01814                         SLOT.ksr = CH.kcode >> SLOT.KSR;
01815 
01816                         /* Calculate attack, decay and release rates */
01817                         if ((SLOT.ar + SLOT.ksr) < 16+62)
01818                         {
01819                                 SLOT.eg_sh_ar  = eg_rate_shift [SLOT.ar + SLOT.ksr ];
01820                                 SLOT.eg_sel_ar = eg_rate_select[SLOT.ar + SLOT.ksr ];
01821                         }
01822                         else
01823                         {
01824                                 SLOT.eg_sh_ar  = 0;
01825                                 SLOT.eg_sel_ar = 13*RATE_STEPS;
01826                         }
01827                         SLOT.eg_sh_dr  = eg_rate_shift [SLOT.dr + SLOT.ksr ];
01828                         SLOT.eg_sel_dr = eg_rate_select[SLOT.dr + SLOT.ksr ];
01829                         SLOT.eg_sh_rr  = eg_rate_shift [SLOT.rr + SLOT.ksr ];
01830                         SLOT.eg_sel_rr = eg_rate_select[SLOT.rr + SLOT.ksr ];
01831 
01832                         /* Calculate phase increment */
01833                         SLOT.Incr = CH.fc * SLOT.mul;
01834 
01835                         /* Total level */
01836                         SLOT.TLL = SLOT.TL + (CH.ksl_base >> SLOT.ksl);
01837 
01838                         /* Connect output */
01839                         SLOT.connect1 = SLOT.CON ? &output[0] : &phase_modulation;
01840                 }
01841         }
01842 #if BUILD_Y8950
01843         if ( (type & OPL_TYPE_ADPCM) && (deltat) )
01844         {
01845                 // We really should call the postlod function for the YM_DELTAT, but it's hard without registers
01846                 // (see the way the YM2610 does it)
01847                 //deltat->postload(REGS);
01848         }
01849 #endif
01850 }
01851 #endif /* end not used */
01852 
01853 } // anonymous namespace
01854 
01855 
01856 #if  0
01857 static void OPLsave_state_channel(device_t *device, OPL_CH *CH)
01858 {
01859         int slot, ch;
01860 
01861         for( ch=0 ; ch < 9 ; ch++, CH++ )
01862         {
01863                 /* channel */
01864                 device->save_item(NAME(CH->block_fnum), ch);
01865                 device->save_item(NAME(CH->kcode), ch);
01866                 /* slots */
01867                 for( slot=0 ; slot < 2 ; slot++ )
01868                 {
01869                         OPL_SLOT *SLOT = &CH->SLOT[slot];
01870 
01871                         device->save_item(NAME(SLOT->ar), ch * 2 + slot);
01872                         device->save_item(NAME(SLOT->dr), ch * 2 + slot);
01873                         device->save_item(NAME(SLOT->rr), ch * 2 + slot);
01874                         device->save_item(NAME(SLOT->KSR), ch * 2 + slot);
01875                         device->save_item(NAME(SLOT->ksl), ch * 2 + slot);
01876                         device->save_item(NAME(SLOT->mul), ch * 2 + slot);
01877 
01878                         device->save_item(NAME(SLOT->Cnt), ch * 2 + slot);
01879                         device->save_item(NAME(SLOT->FB), ch * 2 + slot);
01880                         device->save_item(NAME(SLOT->op1_out), ch * 2 + slot);
01881                         device->save_item(NAME(SLOT->CON), ch * 2 + slot);
01882 
01883                         device->save_item(NAME(SLOT->eg_type), ch * 2 + slot);
01884                         device->save_item(NAME(SLOT->state), ch * 2 + slot);
01885                         device->save_item(NAME(SLOT->TL), ch * 2 + slot);
01886                         device->save_item(NAME(SLOT->volume), ch * 2 + slot);
01887                         device->save_item(NAME(SLOT->sl), ch * 2 + slot);
01888                         device->save_item(NAME(SLOT->key), ch * 2 + slot);
01889 
01890                         device->save_item(NAME(SLOT->AMmask), ch * 2 + slot);
01891                         device->save_item(NAME(SLOT->vib), ch * 2 + slot);
01892 
01893                         device->save_item(NAME(SLOT->wavetable), ch * 2 + slot);
01894                 }
01895         }
01896 }
01897 
01898 /* Register savestate for a virtual YM3812/YM3526Y8950 */
01899 
01900 static void OPL_save_state(FM_OPL *OPL, device_t *device)
01901 {
01902         OPLsave_state_channel(device, OPL->P_CH);
01903 
01904         device->save_item(NAME(OPL->eg_cnt));
01905         device->save_item(NAME(OPL->eg_timer));
01906 
01907         device->save_item(NAME(OPL->rhythm));
01908 
01909         device->save_item(NAME(OPL->lfo_am_depth));
01910         device->save_item(NAME(OPL->lfo_pm_depth_range));
01911         device->save_item(NAME(OPL->lfo_am_cnt));
01912         device->save_item(NAME(OPL->lfo_pm_cnt));
01913 
01914         device->save_item(NAME(OPL->noise_rng));
01915         device->save_item(NAME(OPL->noise_p));
01916 
01917         if( OPL->type & OPL_TYPE_WAVESEL )
01918         {
01919                 device->save_item(NAME(OPL->wavesel));
01920         }
01921 
01922         device->save_item(NAME(OPL->T));
01923         device->save_item(NAME(OPL->st));
01924 
01925 #if BUILD_Y8950
01926         if ( (OPL->type & OPL_TYPE_ADPCM) && (OPL->deltat) )
01927         {
01928                 OPL->deltat->savestate(device);
01929         }
01930 
01931         if ( OPL->type & OPL_TYPE_IO )
01932         {
01933                 device->save_item(NAME(OPL->portDirection));
01934                 device->save_item(NAME(OPL->portLatch));
01935         }
01936 #endif
01937 
01938         device->save_item(NAME(OPL->address));
01939         device->save_item(NAME(OPL->status));
01940         device->save_item(NAME(OPL->statusmask));
01941         device->save_item(NAME(OPL->mode));
01942 
01943         device->machine().save().register_postload(save_prepost_delegate(FUNC(FM_OPL::postload), OPL));
01944 }
01945 
01946 #endif
01947 
01948 static void OPL_clock_changed(FM_OPL *OPL, uint32_t clock, uint32_t rate)
01949 {
01950         OPL->clock = clock;
01951         OPL->rate  = rate;
01952 
01953         /* init global tables */
01954         OPL->initialize();
01955 }
01956 
01957 
01958 /* Create one of virtual YM3812/YM3526/Y8950 */
01959 /* 'clock' is chip clock in Hz  */
01960 /* 'rate'  is sampling rate  */
01961 static FM_OPL *OPLCreate(device_t *device, uint32_t clock, uint32_t rate, int type)
01962 {
01963         char *ptr;
01964         FM_OPL *OPL;
01965         int state_size;
01966 
01967         if (FM_OPL::LockTable(device) == -1) return 0;
01968 
01969         /* calculate OPL state size */
01970         state_size  = sizeof(FM_OPL);
01971 
01972 #if BUILD_Y8950
01973         if (type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT);
01974 #endif
01975 
01976         /* allocate memory block */
01977         ptr = (char *)auto_alloc_array_clear(device->machine(), uint8_t, state_size);
01978 
01979         OPL  = (FM_OPL *)ptr;
01980 
01981         ptr += sizeof(FM_OPL);
01982 
01983 #if BUILD_Y8950
01984         if (type&OPL_TYPE_ADPCM)
01985         {
01986                 OPL->deltat = (YM_DELTAT *)ptr;
01987         }
01988         ptr += sizeof(YM_DELTAT);
01989 #endif
01990 
01991         OPL->device = device;
01992         OPL->type  = type;
01993         OPL_clock_changed(OPL, clock, rate);
01994 
01995         return OPL;
01996 }
01997 
01998 /* Destroy one of virtual YM3812 */
01999 static void OPLDestroy(FM_OPL *OPL)
02000 {
02001         FM_OPL::UnLockTable();
02002     free(OPL);
02003 }
02004 
02005 /* Optional handlers */
02006 
02007 static void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER timer_handler,device_t *device)
02008 {
02009         OPL->timer_handler   = timer_handler;
02010         OPL->TimerParam = device;
02011 }
02012 static void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,device_t *device)
02013 {
02014         OPL->IRQHandler     = IRQHandler;
02015         OPL->IRQParam = device;
02016 }
02017 static void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,device_t *device)
02018 {
02019         OPL->UpdateHandler = UpdateHandler;
02020         OPL->UpdateParam = device;
02021 }
02022 
02023 static int OPLWrite(FM_OPL *OPL,int a,int v)
02024 {
02025         if( !(a&1) )
02026         {   /* address port */
02027                 OPL->address = v & 0xff;
02028         }
02029         else
02030         {   /* data port */
02031                 if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
02032                 OPL->WriteReg(OPL->address,v);
02033         }
02034         return OPL->status>>7;
02035 }
02036 
02037 static unsigned char OPLRead(FM_OPL *OPL,int a)
02038 {
02039         if( !(a&1) )
02040         {
02041                 /* status port */
02042 
02043                 #if BUILD_Y8950
02044 
02045                 if(OPL->type&OPL_TYPE_ADPCM)    /* Y8950 */
02046                 {
02047                         return (OPL->status & (OPL->statusmask|0x80)) | (OPL->deltat->PCM_BSY&1);
02048                 }
02049 
02050                 #endif
02051 
02052                 /* OPL and OPL2 */
02053                 return OPL->status & (OPL->statusmask|0x80);
02054         }
02055 
02056 #if BUILD_Y8950
02057         /* data port */
02058         switch(OPL->address)
02059         {
02060         case 0x05: /* KeyBoard IN */
02061                 if(OPL->type&OPL_TYPE_KEYBOARD)
02062                 {
02063                         if(OPL->keyboardhandler_r)
02064                                 return OPL->keyboardhandler_r(OPL->keyboard_param);
02065                         else
02066                                 OPL->device->logerror("Y8950: read unmapped KEYBOARD port\n");
02067                 }
02068                 return 0;
02069 
02070         case 0x0f: /* ADPCM-DATA  */
02071                 if(OPL->type&OPL_TYPE_ADPCM)
02072                 {
02073                         uint8_t val;
02074 
02075                         val = OPL->deltat->ADPCM_Read();
02076                         /*logerror("Y8950: read ADPCM value read=%02x\n",val);*/
02077                         return val;
02078                 }
02079                 return 0;
02080 
02081         case 0x19: /* I/O DATA    */
02082                 if(OPL->type&OPL_TYPE_IO)
02083                 {
02084                         if(OPL->porthandler_r)
02085                                 return OPL->porthandler_r(OPL->port_param);
02086                         else
02087                                 OPL->device->logerror("Y8950:read unmapped I/O port\n");
02088                 }
02089                 return 0;
02090         case 0x1a: /* PCM-DATA    */
02091                 if(OPL->type&OPL_TYPE_ADPCM)
02092                 {
02093                         OPL->device->logerror("Y8950 A/D conversion is accessed but not implemented !\n");
02094                         return 0x80; /* 2's complement PCM data - result from A/D conversion */
02095                 }
02096                 return 0;
02097         }
02098 #endif
02099 
02100         return 0xff;
02101 }
02102 
02103 /* CSM Key Controll */
02104 static inline void CSMKeyControll(OPL_CH *CH)
02105 {
02106         CH->SLOT[SLOT1].KEYON(4);
02107         CH->SLOT[SLOT2].KEYON(4);
02108 
02109         /* The key off should happen exactly one sample later - not implemented correctly yet */
02110 
02111         CH->SLOT[SLOT1].KEYOFF(~4);
02112         CH->SLOT[SLOT2].KEYOFF(~4);
02113 }
02114 
02115 static int OPLTimerOver(FM_OPL *OPL,int c)
02116 {
02117         if( c )
02118         {   /* Timer B */
02119                 OPL->STATUS_SET(0x20);
02120         }
02121         else
02122         {   /* Timer A */
02123                 OPL->STATUS_SET(0x40);
02124                 /* CSM mode key,TL controll */
02125                 if( OPL->mode & 0x80 )
02126                 {   /* CSM mode total level latch and auto key on */
02127                         int ch;
02128                         if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0);
02129                         for(ch=0; ch<9; ch++)
02130                                 CSMKeyControll( &OPL->P_CH[ch] );
02131                 }
02132         }
02133         /* reload timer */
02134         //if (OPL->timer_handler) (OPL->timer_handler)(OPL->TimerParam,c,OPL->TimerBase * OPL->T[c]);
02135         return OPL->status>>7;
02136 }
02137 
02138 #define MAX_OPL_CHIPS 2
02139 
02140 
02141 #if BUILD_YM3812
02142 
02143 void ym3812_clock_changed(void *chip, uint32_t clock, uint32_t rate)
02144 {
02145         OPL_clock_changed((FM_OPL *)chip, clock, rate);
02146 }
02147 
02148 void * ym3812_init(device_t *device, uint32_t clock, uint32_t rate)
02149 {
02150         /* emulator create */
02151         FM_OPL *YM3812 = OPLCreate(device,clock,rate,OPL_TYPE_YM3812);
02152         if (YM3812)
02153         {
02154                 //OPL_save_state(YM3812, device);
02155                 ym3812_reset_chip(YM3812);
02156         }
02157         return YM3812;
02158 }
02159 
02160 void ym3812_shutdown(void *chip)
02161 {
02162         FM_OPL *YM3812 = (FM_OPL *)chip;
02163 
02164         /* emulator shutdown */
02165         OPLDestroy(YM3812);
02166 }
02167 void ym3812_reset_chip(void *chip)
02168 {
02169         FM_OPL *YM3812 = (FM_OPL *)chip;
02170         YM3812->ResetChip();
02171 }
02172 
02173 int ym3812_write(void *chip, int a, int v)
02174 {
02175         FM_OPL *YM3812 = (FM_OPL *)chip;
02176         return OPLWrite(YM3812, a, v);
02177 }
02178 
02179 unsigned char ym3812_read(void *chip, int a)
02180 {
02181         FM_OPL *YM3812 = (FM_OPL *)chip;
02182         /* YM3812 always returns bit2 and bit1 in HIGH state */
02183         return OPLRead(YM3812, a) | 0x06 ;
02184 }
02185 int ym3812_timer_over(void *chip, int c)
02186 {
02187         FM_OPL *YM3812 = (FM_OPL *)chip;
02188         return OPLTimerOver(YM3812, c);
02189 }
02190 
02191 void ym3812_set_timer_handler(void *chip, OPL_TIMERHANDLER timer_handler, device_t *device)
02192 {
02193         FM_OPL *YM3812 = (FM_OPL *)chip;
02194         OPLSetTimerHandler(YM3812, timer_handler, device);
02195 }
02196 void ym3812_set_irq_handler(void *chip,OPL_IRQHANDLER IRQHandler,device_t *device)
02197 {
02198         FM_OPL *YM3812 = (FM_OPL *)chip;
02199         OPLSetIRQHandler(YM3812, IRQHandler, device);
02200 }
02201 void ym3812_set_update_handler(void *chip,OPL_UPDATEHANDLER UpdateHandler,device_t *device)
02202 {
02203         FM_OPL *YM3812 = (FM_OPL *)chip;
02204         OPLSetUpdateHandler(YM3812, UpdateHandler, device);
02205 }
02206 
02207 /*
02208 ** Generate samples for one of the YM3812's
02209 **
02210 ** 'which' is the virtual YM3812 number
02211 ** '*buffer' is the output buffer pointer
02212 ** 'length' is the number of samples that should be generated
02213 */
02214 void ym3812_update_one(void *chip, OPLSAMPLE *buffer, int length)
02215 {
02216         FM_OPL      *OPL = (FM_OPL *)chip;
02217         uint8_t       rhythm = OPL->rhythm&0x20;
02218         OPLSAMPLE   *buf = buffer;
02219         int i;
02220 
02221         for( i=0; i < length ; i++ )
02222         {
02223                 int lt;
02224 
02225                 OPL->output[0] = 0;
02226 
02227                 OPL->advance_lfo();
02228 
02229                 /* FM part */
02230                 OPL->CALC_CH(OPL->P_CH[0]);
02231                 OPL->CALC_CH(OPL->P_CH[1]);
02232                 OPL->CALC_CH(OPL->P_CH[2]);
02233                 OPL->CALC_CH(OPL->P_CH[3]);
02234                 OPL->CALC_CH(OPL->P_CH[4]);
02235                 OPL->CALC_CH(OPL->P_CH[5]);
02236 
02237                 if(!rhythm)
02238                 {
02239                         OPL->CALC_CH(OPL->P_CH[6]);
02240                         OPL->CALC_CH(OPL->P_CH[7]);
02241                         OPL->CALC_CH(OPL->P_CH[8]);
02242                 }
02243                 else        /* Rhythm part */
02244                 {
02245                         OPL->CALC_RH();
02246                 }
02247 
02248                 lt = OPL->output[0];
02249 
02250                 lt >>= FINAL_SH;
02251 
02252                 /* limit check */
02253                 lt = limit( lt , MAXOUT, MINOUT );
02254 
02255                 #ifdef SAVE_SAMPLE
02256                 if (which==0)
02257                 {
02258                         SAVE_ALL_CHANNELS
02259                 }
02260                 #endif
02261 
02262                 /* store to sound buffer */
02263                 buf[i] = lt;
02264 
02265                 OPL->advance();
02266         }
02267 
02268 }
02269 
02270 // save state support
02271 void SaveState_Channel(OPL_CH *CH, std::ostream& stream) {
02272         int slot, ch;
02273 
02274         for( ch=0 ; ch < 9 ; ch++, CH++ ) {
02275                 /* channel */
02276                 WRITE_POD( &CH->block_fnum, CH->block_fnum );
02277             WRITE_POD( &CH->kcode, CH->kcode );
02278             /* slots */
02279                 for( slot=0 ; slot < 2 ; slot++ ) {
02280                 OPL_SLOT *SLOT = &CH->SLOT[slot];
02281 
02282                 WRITE_POD( &SLOT->ar, SLOT->ar );
02283                 WRITE_POD( &SLOT->dr, SLOT->dr );
02284                 WRITE_POD( &SLOT->rr, SLOT->rr );
02285                 WRITE_POD( &SLOT->KSR, SLOT->KSR );
02286             WRITE_POD( &SLOT->ksl, SLOT->ksl );
02287                 WRITE_POD( &SLOT->mul, SLOT->mul );
02288 
02289                 WRITE_POD( &SLOT->Cnt, SLOT->Cnt );
02290                 WRITE_POD( &SLOT->FB, SLOT->FB );
02291                 WRITE_POD( &SLOT->op1_out, SLOT->op1_out );
02292                 WRITE_POD( &SLOT->CON, SLOT->CON );
02293 
02294                 WRITE_POD( &SLOT->eg_type, SLOT->eg_type );
02295             WRITE_POD( &SLOT->state, SLOT->state );
02296             WRITE_POD( &SLOT->TL, SLOT->TL );
02297             WRITE_POD( &SLOT->volume, SLOT->volume );
02298             WRITE_POD( &SLOT->sl, SLOT->sl );
02299             WRITE_POD( &SLOT->key, SLOT->key );
02300 
02301             WRITE_POD( &SLOT->AMmask, SLOT->AMmask );
02302             WRITE_POD( &SLOT->vib, SLOT->vib );
02303 
02304             WRITE_POD( &SLOT->wavetable, SLOT->wavetable );
02305         }
02306         }
02307 }
02308 
02309 void LoadState_Channel(OPL_CH *CH, std::istream& stream) {
02310         int slot, ch;
02311 
02312         for( ch=0 ; ch < 9 ; ch++, CH++ ) {
02313                 /* channel */
02314             READ_POD( &CH->block_fnum, CH->block_fnum );
02315             READ_POD( &CH->kcode, CH->kcode );
02316             /* slots */
02317                 for( slot=0 ; slot < 2 ; slot++ ) {
02318                 OPL_SLOT *SLOT = &CH->SLOT[slot];
02319 
02320                 READ_POD( &SLOT->ar, SLOT->ar );
02321                 READ_POD( &SLOT->dr, SLOT->dr );
02322                 READ_POD( &SLOT->rr, SLOT->rr );
02323                 READ_POD( &SLOT->KSR, SLOT->KSR );
02324                 READ_POD( &SLOT->ksl, SLOT->ksl );
02325                 READ_POD( &SLOT->mul, SLOT->mul );
02326 
02327                 READ_POD( &SLOT->Cnt, SLOT->Cnt );
02328                 READ_POD( &SLOT->FB, SLOT->FB );
02329                 READ_POD( &SLOT->op1_out, SLOT->op1_out );
02330                 READ_POD( &SLOT->CON, SLOT->CON );
02331 
02332                 READ_POD( &SLOT->eg_type, SLOT->eg_type );
02333             READ_POD( &SLOT->state, SLOT->state );
02334             READ_POD( &SLOT->TL, SLOT->TL );
02335             READ_POD( &SLOT->volume, SLOT->volume );
02336             READ_POD( &SLOT->sl, SLOT->sl );
02337             READ_POD( &SLOT->key, SLOT->key );
02338 
02339             READ_POD( &SLOT->AMmask, SLOT->AMmask );
02340             READ_POD( &SLOT->vib, SLOT->vib );
02341 
02342             READ_POD( &SLOT->wavetable, SLOT->wavetable );
02343         }
02344         }
02345 }
02346 
02347 void FMOPL_SaveState(void *chip, std::ostream& stream ) {
02348     FM_OPL *OPL = (FM_OPL *)chip;
02349 
02350     SaveState_Channel(OPL->P_CH, stream);
02351 
02352     WRITE_POD(&OPL->eg_cnt, OPL->eg_cnt);
02353     WRITE_POD(&OPL->eg_timer, OPL->eg_timer);
02354     WRITE_POD(&OPL->rhythm, OPL->rhythm);
02355 
02356     WRITE_POD(&OPL->lfo_am_depth, OPL->lfo_am_depth);
02357     WRITE_POD(&OPL->lfo_pm_depth_range, OPL->lfo_pm_depth_range);
02358     WRITE_POD(&OPL->lfo_am_cnt, OPL->lfo_am_cnt);
02359     WRITE_POD(&OPL->lfo_pm_cnt, OPL->lfo_pm_cnt);
02360 
02361     WRITE_POD(&OPL->noise_rng, OPL->noise_rng);
02362     WRITE_POD(&OPL->noise_p, OPL->noise_p);
02363 
02364         if( OPL->type & OPL_TYPE_WAVESEL ) {
02365         WRITE_POD(&OPL->wavesel, OPL->wavesel);
02366         }
02367 
02368     WRITE_POD(&OPL->T, OPL->T);
02369     WRITE_POD(&OPL->st, OPL->st);
02370 
02371 #if BUILD_Y8950
02372     //ToDo - Bruenor
02373         /*if ( (OPL->type & OPL_TYPE_ADPCM) && (OPL->deltat) ) {
02374                 // OPL->deltat->savestate(device);
02375         }
02376 
02377         if ( OPL->type & OPL_TYPE_IO ) {
02378                 // device->save_item(NAME(OPL->portDirection));
02379                 // device->save_item(NAME(OPL->portLatch));
02380         }*/
02381 #endif
02382 
02383     WRITE_POD(&OPL->address, OPL->address);
02384     WRITE_POD(&OPL->status, OPL->status);
02385     WRITE_POD(&OPL->statusmask, OPL->statusmask);
02386     WRITE_POD(&OPL->mode, OPL->mode);
02387 }
02388 
02389 void FMOPL_LoadState(void *chip, std::istream& stream ) {
02390     FM_OPL *OPL = (FM_OPL *)chip;
02391 
02392     LoadState_Channel(OPL->P_CH, stream);
02393 
02394     READ_POD(&OPL->eg_cnt, OPL->eg_cnt);
02395     READ_POD(&OPL->eg_timer, OPL->eg_timer);
02396 
02397     READ_POD(&OPL->rhythm, OPL->rhythm);
02398 
02399     READ_POD(&OPL->lfo_am_depth, OPL->lfo_am_depth);
02400     READ_POD(&OPL->lfo_pm_depth_range, OPL->lfo_pm_depth_range);
02401     READ_POD(&OPL->lfo_am_cnt, OPL->lfo_am_cnt);
02402     READ_POD(&OPL->lfo_pm_cnt, OPL->lfo_pm_cnt);
02403 
02404     READ_POD(&OPL->noise_rng, OPL->noise_rng);
02405     READ_POD(&OPL->noise_p, OPL->noise_p);
02406 
02407         if( OPL->type & OPL_TYPE_WAVESEL ) {
02408         READ_POD(&OPL->wavesel, OPL->wavesel);
02409         }
02410 
02411     READ_POD(&OPL->T, OPL->T);
02412     READ_POD(&OPL->st, OPL->st);
02413 
02414 #if BUILD_Y8950
02415     //ToDo - Bruenor
02416 #endif
02417 
02418     READ_POD(&OPL->address, OPL->address);
02419     READ_POD(&OPL->status, OPL->status);
02420     READ_POD(&OPL->statusmask, OPL->statusmask);
02421     READ_POD(&OPL->mode, OPL->mode);
02422 }
02423 #endif /* BUILD_YM3812 */
02424 
02425 
02426 
02427 #if (BUILD_YM3526)
02428 
02429 void ym3526_clock_changed(void *chip, uint32_t clock, uint32_t rate)
02430 {
02431         OPL_clock_changed((FM_OPL *)chip, clock, rate);
02432 }
02433 
02434 void *ym3526_init(device_t *device, uint32_t clock, uint32_t rate)
02435 {
02436         /* emulator create */
02437         FM_OPL *YM3526 = OPLCreate(device,clock,rate,OPL_TYPE_YM3526);
02438         if (YM3526)
02439         {
02440                 //OPL_save_state(YM3526, device);
02441                 ym3526_reset_chip(YM3526);
02442         }
02443         return YM3526;
02444 }
02445 
02446 void ym3526_shutdown(void *chip)
02447 {
02448         FM_OPL *YM3526 = (FM_OPL *)chip;
02449         /* emulator shutdown */
02450         OPLDestroy(YM3526);
02451 }
02452 void ym3526_reset_chip(void *chip)
02453 {
02454         FM_OPL *YM3526 = (FM_OPL *)chip;
02455         YM3526->ResetChip();
02456 }
02457 
02458 int ym3526_write(void *chip, int a, int v)
02459 {
02460         FM_OPL *YM3526 = (FM_OPL *)chip;
02461         return OPLWrite(YM3526, a, v);
02462 }
02463 
02464 unsigned char ym3526_read(void *chip, int a)
02465 {
02466         FM_OPL *YM3526 = (FM_OPL *)chip;
02467         /* YM3526 always returns bit2 and bit1 in HIGH state */
02468         return OPLRead(YM3526, a) | 0x06 ;
02469 }
02470 int ym3526_timer_over(void *chip, int c)
02471 {
02472         FM_OPL *YM3526 = (FM_OPL *)chip;
02473         return OPLTimerOver(YM3526, c);
02474 }
02475 
02476 void ym3526_set_timer_handler(void *chip, OPL_TIMERHANDLER timer_handler, device_t *device)
02477 {
02478         FM_OPL *YM3526 = (FM_OPL *)chip;
02479         OPLSetTimerHandler(YM3526, timer_handler, device);
02480 }
02481 void ym3526_set_irq_handler(void *chip,OPL_IRQHANDLER IRQHandler,device_t *device)
02482 {
02483         FM_OPL *YM3526 = (FM_OPL *)chip;
02484         OPLSetIRQHandler(YM3526, IRQHandler, device);
02485 }
02486 void ym3526_set_update_handler(void *chip,OPL_UPDATEHANDLER UpdateHandler,device_t *device)
02487 {
02488         FM_OPL *YM3526 = (FM_OPL *)chip;
02489         OPLSetUpdateHandler(YM3526, UpdateHandler, device);
02490 }
02491 
02492 
02493 /*
02494 ** Generate samples for one of the YM3526's
02495 **
02496 ** 'which' is the virtual YM3526 number
02497 ** '*buffer' is the output buffer pointer
02498 ** 'length' is the number of samples that should be generated
02499 */
02500 void ym3526_update_one(void *chip, OPLSAMPLE *buffer, int length)
02501 {
02502         FM_OPL      *OPL = (FM_OPL *)chip;
02503         uint8_t       rhythm = OPL->rhythm&0x20;
02504         OPLSAMPLE   *buf = buffer;
02505         int i;
02506 
02507         for( i=0; i < length ; i++ )
02508         {
02509                 int lt;
02510 
02511                 OPL->output[0] = 0;
02512 
02513                 OPL->advance_lfo();
02514 
02515                 /* FM part */
02516                 OPL->CALC_CH(OPL->P_CH[0]);
02517                 OPL->CALC_CH(OPL->P_CH[1]);
02518                 OPL->CALC_CH(OPL->P_CH[2]);
02519                 OPL->CALC_CH(OPL->P_CH[3]);
02520                 OPL->CALC_CH(OPL->P_CH[4]);
02521                 OPL->CALC_CH(OPL->P_CH[5]);
02522 
02523                 if(!rhythm)
02524                 {
02525                         OPL->CALC_CH(OPL->P_CH[6]);
02526                         OPL->CALC_CH(OPL->P_CH[7]);
02527                         OPL->CALC_CH(OPL->P_CH[8]);
02528                 }
02529                 else        /* Rhythm part */
02530                 {
02531                         OPL->CALC_RH();
02532                 }
02533 
02534                 lt = OPL->output[0];
02535 
02536                 lt >>= FINAL_SH;
02537 
02538                 /* limit check */
02539                 lt = limit( lt , MAXOUT, MINOUT );
02540 
02541                 #ifdef SAVE_SAMPLE
02542                 if (which==0)
02543                 {
02544                         SAVE_ALL_CHANNELS
02545                 }
02546                 #endif
02547 
02548                 /* store to sound buffer */
02549                 buf[i] = lt;
02550 
02551                 OPL->advance();
02552         }
02553 
02554 }
02555 #endif /* BUILD_YM3526 */
02556 
02557 
02558 
02559 
02560 #if BUILD_Y8950
02561 
02562 static void Y8950_deltat_status_set(void *chip, uint8_t changebits)
02563 {
02564         FM_OPL *Y8950 = (FM_OPL *)chip;
02565         Y8950->STATUS_SET(changebits);
02566 }
02567 static void Y8950_deltat_status_reset(void *chip, uint8_t changebits)
02568 {
02569         FM_OPL *Y8950 = (FM_OPL *)chip;
02570         Y8950->STATUS_RESET(changebits);
02571 }
02572 
02573 void *y8950_init(device_t *device, uint32_t clock, uint32_t rate)
02574 {
02575         /* emulator create */
02576         FM_OPL *Y8950 = OPLCreate(device,clock,rate,OPL_TYPE_Y8950);
02577         if (Y8950)
02578         {
02579                 Y8950->deltat->status_set_handler = Y8950_deltat_status_set;
02580                 Y8950->deltat->status_reset_handler = Y8950_deltat_status_reset;
02581                 Y8950->deltat->status_change_which_chip = Y8950;
02582                 Y8950->deltat->status_change_EOS_bit = 0x10;        /* status flag: set bit4 on End Of Sample */
02583                 Y8950->deltat->status_change_BRDY_bit = 0x08;   /* status flag: set bit3 on BRDY (End Of: ADPCM analysis/synthesis, memory reading/writing) */
02584 
02585                 /*Y8950->deltat->write_time = 10.0 / clock;*/       /* a single byte write takes 10 cycles of main clock */
02586                 /*Y8950->deltat->read_time  = 8.0 / clock;*/        /* a single byte read takes 8 cycles of main clock */
02587                 /* reset */
02588                 //OPL_save_state(Y8950, device);
02589                 y8950_reset_chip(Y8950);
02590         }
02591 
02592         return Y8950;
02593 }
02594 
02595 void y8950_shutdown(void *chip)
02596 {
02597         FM_OPL *Y8950 = (FM_OPL *)chip;
02598         /* emulator shutdown */
02599         OPLDestroy(Y8950);
02600 }
02601 void y8950_reset_chip(void *chip)
02602 {
02603         FM_OPL *Y8950 = (FM_OPL *)chip;
02604         Y8950->ResetChip();
02605 }
02606 
02607 int y8950_write(void *chip, int a, int v)
02608 {
02609         FM_OPL *Y8950 = (FM_OPL *)chip;
02610         return OPLWrite(Y8950, a, v);
02611 }
02612 
02613 unsigned char y8950_read(void *chip, int a)
02614 {
02615         FM_OPL *Y8950 = (FM_OPL *)chip;
02616         return OPLRead(Y8950, a);
02617 }
02618 int y8950_timer_over(void *chip, int c)
02619 {
02620         FM_OPL *Y8950 = (FM_OPL *)chip;
02621         return OPLTimerOver(Y8950, c);
02622 }
02623 
02624 void y8950_set_timer_handler(void *chip, OPL_TIMERHANDLER timer_handler, device_t *device)
02625 {
02626         FM_OPL *Y8950 = (FM_OPL *)chip;
02627         OPLSetTimerHandler(Y8950, timer_handler, device);
02628 }
02629 void y8950_set_irq_handler(void *chip,OPL_IRQHANDLER IRQHandler,device_t *device)
02630 {
02631         FM_OPL *Y8950 = (FM_OPL *)chip;
02632         OPLSetIRQHandler(Y8950, IRQHandler, device);
02633 }
02634 void y8950_set_update_handler(void *chip,OPL_UPDATEHANDLER UpdateHandler,device_t *device)
02635 {
02636         FM_OPL *Y8950 = (FM_OPL *)chip;
02637         OPLSetUpdateHandler(Y8950, UpdateHandler, device);
02638 }
02639 
02640 void y8950_set_delta_t_memory(void *chip, void * deltat_mem_ptr, int deltat_mem_size )
02641 {
02642         FM_OPL      *OPL = (FM_OPL *)chip;
02643         OPL->deltat->memory = (uint8_t *)(deltat_mem_ptr);
02644         OPL->deltat->memory_size = deltat_mem_size;
02645 }
02646 
02647 /*
02648 ** Generate samples for one of the Y8950's
02649 **
02650 ** 'which' is the virtual Y8950 number
02651 ** '*buffer' is the output buffer pointer
02652 ** 'length' is the number of samples that should be generated
02653 */
02654 void y8950_update_one(void *chip, OPLSAMPLE *buffer, int length)
02655 {
02656         int i;
02657         FM_OPL      *OPL = (FM_OPL *)chip;
02658         uint8_t       rhythm  = OPL->rhythm&0x20;
02659         YM_DELTAT   *DELTAT = OPL->deltat;
02660         OPLSAMPLE   *buf    = buffer;
02661 
02662         for( i=0; i < length ; i++ )
02663         {
02664                 int lt;
02665 
02666                 OPL->output[0] = 0;
02667                 OPL->output_deltat[0] = 0;
02668 
02669                 OPL->advance_lfo();
02670 
02671                 /* deltaT ADPCM */
02672                 if( DELTAT->portstate&0x80 )
02673                         DELTAT->ADPCM_CALC();
02674 
02675                 /* FM part */
02676                 OPL->CALC_CH(OPL->P_CH[0]);
02677                 OPL->CALC_CH(OPL->P_CH[1]);
02678                 OPL->CALC_CH(OPL->P_CH[2]);
02679                 OPL->CALC_CH(OPL->P_CH[3]);
02680                 OPL->CALC_CH(OPL->P_CH[4]);
02681                 OPL->CALC_CH(OPL->P_CH[5]);
02682 
02683                 if(!rhythm)
02684                 {
02685                         OPL->CALC_CH(OPL->P_CH[6]);
02686                         OPL->CALC_CH(OPL->P_CH[7]);
02687                         OPL->CALC_CH(OPL->P_CH[8]);
02688                 }
02689                 else        /* Rhythm part */
02690                 {
02691                         OPL->CALC_RH();
02692                 }
02693 
02694                 lt = OPL->output[0] + (OPL->output_deltat[0]>>11);
02695 
02696                 lt >>= FINAL_SH;
02697 
02698                 /* limit check */
02699                 lt = limit( lt , MAXOUT, MINOUT );
02700 
02701                 #ifdef SAVE_SAMPLE
02702                 if (which==0)
02703                 {
02704                         SAVE_ALL_CHANNELS
02705                 }
02706                 #endif
02707 
02708                 /* store to sound buffer */
02709                 buf[i] = lt;
02710 
02711                 OPL->advance();
02712         }
02713 
02714 }
02715 
02716 void y8950_set_port_handler(void *chip,OPL_PORTHANDLER_W PortHandler_w,OPL_PORTHANDLER_R PortHandler_r,device_t *device)
02717 {
02718         FM_OPL      *OPL = (FM_OPL *)chip;
02719         OPL->porthandler_w = PortHandler_w;
02720         OPL->porthandler_r = PortHandler_r;
02721         OPL->port_param = device;
02722 }
02723 
02724 void y8950_set_keyboard_handler(void *chip,OPL_PORTHANDLER_W KeyboardHandler_w,OPL_PORTHANDLER_R KeyboardHandler_r,device_t *device)
02725 {
02726         FM_OPL      *OPL = (FM_OPL *)chip;
02727         OPL->keyboardhandler_w = KeyboardHandler_w;
02728         OPL->keyboardhandler_r = KeyboardHandler_r;
02729         OPL->keyboard_param = device;
02730 }
02731 
02732 
02733 #endif