| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | DOSBox implementation of a combined Yamaha YMF262 and Yamaha YM3812 emulator. |
| 21 | Enabling the opl3 bit will switch the emulator to stereo opl3 output instead of regular mono opl2 |
| 22 | Except for the table generation it's all integer math |
| 23 | Can choose different types of generators, using muls and bigger tables, try different ones for slower platforms |
| 24 | The generation was based on the MAME implementation but tried to have it use less memory and be faster in general |
| 25 | MAME uses much bigger envelope tables and this will be the biggest cause of it sounding different at times |
| 26 | |
| 27 | //TODO Don't delay first operator 1 sample in opl3 mode |
| 28 | //TODO Maybe not use class method pointers but a regular function pointers with operator as first parameter |
| 29 | //TODO Fix panning for the Percussion channels, would any opl3 player use it and actually really change it though? |
| 30 | //TODO Check if having the same accuracy in all frequency multipliers sounds better or not |
| 31 | |
| 32 | //DUNNO Keyon in 4op, switch to 2op without keyoff. |
| 33 | */ |
| 34 | |
| 35 | /* $Id: dbopl.cpp,v 1.10 2009-06-10 19:54:51 harekiet Exp $ */ |
| 36 | |
| 37 | |
| 38 | #include <math.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include "dosbox.h" |
| 42 | #include "dbopl.h" |
| 43 | |
| 44 | |
| 45 | #ifndef PI |
| 46 | #define PI 3.14159265358979323846 |
| 47 | #endif |
| 48 | |
| 49 | namespace DBOPL { |
| 50 | |
| 51 | #define OPLRATE ((double)(14318180.0 / 288.0)) |
| 52 | #define TREMOLO_TABLE 52 |
| 53 | |
| 54 | //Try to use most precision for frequencies |
| 55 | //Else try to keep different waves in synch |
| 56 | //#define WAVE_PRECISION 1 |
| 57 | #ifndef WAVE_PRECISION |
| 58 | //Wave bits available in the top of the 32bit range |
| 59 | //Original adlib uses 10.10, we use 10.22 |
| 60 | #define WAVE_BITS 10 |
| 61 | #else |
| 62 | //Need some extra bits at the top to have room for octaves and frequency multiplier |
| 63 | //We support to 8 times lower rate |
| 64 | //128 * 15 * 8 = 15350, 2^13.9, so need 14 bits |
| 65 | #define WAVE_BITS 14 |
| 66 | #endif |
| 67 | #define WAVE_SH ( 32 - WAVE_BITS ) |
| 68 | #define WAVE_MASK ( ( 1 << WAVE_SH ) - 1 ) |
| 69 | |
| 70 | //Use the same accuracy as the waves |
| 71 | #define LFO_SH ( WAVE_SH - 10 ) |
| 72 | //LFO is controlled by our tremolo 256 sample limit |
| 73 | #define LFO_MAX ( 256 << ( LFO_SH ) ) |
| 74 | |
| 75 | |
| 76 | //Maximum amount of attenuation bits |
| 77 | //Envelope goes to 511, 9 bits |
| 78 | #if (DBOPL_WAVE == WAVE_TABLEMUL ) |
| 79 | //Uses the value directly |
| 80 | #define ENV_BITS ( 9 ) |
| 81 | #else |
| 82 | //Add 3 bits here for more accuracy and would have to be shifted up either way |
| 83 | #define ENV_BITS ( 9 ) |
| 84 | #endif |
| 85 | //Limits of the envelope with those bits and when the envelope goes silent |
| 86 | #define ENV_MIN 0 |
| 87 | #define ENV_EXTRA ( ENV_BITS - 9 ) |
| 88 | #define ENV_MAX ( 511 << ENV_EXTRA ) |
| 89 | #define ENV_LIMIT ( ( 12 * 256) >> ( 3 - ENV_EXTRA ) ) |
| 90 | #define ENV_SILENT( _X_ ) ( (_X_) >= ENV_LIMIT ) |
| 91 | |
| 92 | //Attack/decay/release rate counter shift |
| 93 | #define RATE_SH 24 |
| 94 | #define RATE_MASK ( ( 1 << RATE_SH ) - 1 ) |
| 95 | //Has to fit within 16bit lookuptable |
| 96 | #define MUL_SH 16 |
| 97 | |
| 98 | //Check some ranges |
| 99 | #if ENV_EXTRA > 3 |
| 100 | #error Too many envelope bits |
| 101 | #endif |
| 102 | |
| 103 | |
| 104 | //How much to substract from the base value for the final attenuation |
| 105 | static const Bit8u KslCreateTable[16] = { |
| 106 | //0 will always be be lower than 7 * 8 |
| 107 | 64, 32, 24, 19, |
| 108 | 16, 12, 11, 10, |
| 109 | 8, 6, 5, 4, |
| 110 | 3, 2, 1, 0, |
| 111 | }; |
| 112 | |
| 113 | #define M(_X_) ((Bit8u)( (_X_) * 2)) |
| 114 | static const Bit8u FreqCreateTable[16] = { |
| 115 | M(0.5), M(1 ), M(2 ), M(3 ), M(4 ), M(5 ), M(6 ), M(7 ), |
| 116 | M(8 ), M(9 ), M(10), M(10), M(12), M(12), M(15), M(15) |
| 117 | }; |
| 118 | #undef M |
| 119 | |
| 120 | //We're not including the highest attack rate, that gets a special value |
| 121 | static const Bit8u AttackSamplesTable[13] = { |
| 122 | 69, 55, 46, 40, |
| 123 | 35, 29, 23, 20, |
| 124 | 19, 15, 11, 10, |
| 125 | 9 |
| 126 | }; |
| 127 | //On a real opl these values take 8 samples to reach and are based upon larger tables |
| 128 | static const Bit8u EnvelopeIncreaseTable[13] = { |
| 129 | 4, 5, 6, 7, |
| 130 | 8, 10, 12, 14, |
| 131 | 16, 20, 24, 28, |
| 132 | 32, |
| 133 | }; |
| 134 | |
| 135 | #if ( DBOPL_WAVE == WAVE_HANDLER ) || ( DBOPL_WAVE == WAVE_TABLELOG ) |
| 136 | static Bit16u ExpTable[ 256 ]; |
| 137 | #endif |
| 138 | |
| 139 | #if ( DBOPL_WAVE == WAVE_HANDLER ) |
| 140 | //PI table used by WAVEHANDLER |
| 141 | static Bit16u SinTable[ 512 ]; |
| 142 | #endif |
| 143 | |
| 144 | #if ( DBOPL_WAVE > WAVE_HANDLER ) |
| 145 | //Layout of the waveform table in 512 entry intervals |
| 146 | //With overlapping waves we reduce the table to half it's size |
| 147 | |
| 148 | // | |//\\|____|WAV7|//__|/\ |____|/\/\| |
| 149 | // |\\//| | |WAV7| | \/| | | |
| 150 | // |06 |0126|17 |7 |3 |4 |4 5 |5 | |
| 151 | |
| 152 | //6 is just 0 shifted and masked |
| 153 | |
| 154 | static Bit16s WaveTable[ 8 * 512 ]; |
| 155 | //Distance into WaveTable the wave starts |
| 156 | static const Bit16u WaveBaseTable[8] = { |
| 157 | 0x000, 0x200, 0x200, 0x800, |
| 158 | 0xa00, 0xc00, 0x100, 0x400, |
| 159 | |
| 160 | }; |
| 161 | //Mask the counter with this |
| 162 | static const Bit16u WaveMaskTable[8] = { |
| 163 | 1023, 1023, 511, 511, |
| 164 | 1023, 1023, 512, 1023, |
| 165 | }; |
| 166 | |
| 167 | //Where to start the counter on at keyon |
| 168 | static const Bit16u WaveStartTable[8] = { |
| 169 | 512, 0, 0, 0, |
| 170 | 0, 512, 512, 256, |
| 171 | }; |
| 172 | #endif |
| 173 | |
| 174 | #if ( DBOPL_WAVE == WAVE_TABLEMUL ) |
| 175 | static Bit16u MulTable[ 384 ]; |
| 176 | #endif |
| 177 | |
| 178 | static Bit8u KslTable[ 8 * 16 ]; |
| 179 | static Bit8u TremoloTable[ TREMOLO_TABLE ]; |
| 180 | //Start of a channel behind the chip struct start |
| 181 | static Bit16u ChanOffsetTable[32]; |
| 182 | //Start of an operator behind the chip struct start |
| 183 | static Bit16u OpOffsetTable[64]; |
| 184 | |
| 185 | //The lower bits are the shift of the operator vibrato value |
| 186 | //The highest bit is right shifted to generate -1 or 0 for negation |
| 187 | //So taking the highest input value of 7 this gives 3, 7, 3, 0, -3, -7, -3, 0 |
| 188 | static const Bit8s VibratoTable[ 8 ] = { |
| 189 | 1 - 0x00, 0 - 0x00, 1 - 0x00, 30 - 0x00, |
| 190 | 1 - 0x80, 0 - 0x80, 1 - 0x80, 30 - 0x80 |
| 191 | }; |
| 192 | |
| 193 | //Shift strength for the ksl value determined by ksl strength |
| 194 | static const Bit8u KslShiftTable[4] = { |
| 195 | 31,1,2,0 |
| 196 | }; |
| 197 | |
| 198 | //Generate a table index and table shift value using input value from a selected rate |
| 199 | static void EnvelopeSelect( Bit8u val, Bit8u& index, Bit8u& shift ) { |
| 200 | if ( val < 13 * 4 ) { //Rate 0 - 12 |
| 201 | shift = 12 - ( val >> 2 ); |
| 202 | index = val & 3; |
| 203 | } else if ( val < 15 * 4 ) { //rate 13 - 14 |
| 204 | shift = 0; |
| 205 | index = val - 12 * 4; |
| 206 | } else { //rate 15 and up |
| 207 | shift = 0; |
| 208 | index = 12; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #if ( DBOPL_WAVE == WAVE_HANDLER ) |
| 213 | /* |
| 214 | Generate the different waveforms out of the sine/exponetial table using handlers |
| 215 | */ |
| 216 | static inline Bits MakeVolume( Bitu wave, Bitu volume ) { |
| 217 | Bitu total = wave + volume; |
| 218 | Bitu index = total & 0xff; |
| 219 | Bitu sig = ExpTable[ index ]; |
| 220 | Bitu exp = total >> 8; |
| 221 | #if 0 |
| 222 | //Check if we overflow the 31 shift limit |
| 223 | if ( exp >= 32 ) { |
| 224 | LOG_MSG( "WTF %d %d", total, exp ); |
| 225 | } |
| 226 | #endif |
| 227 | return (sig >> exp); |
| 228 | }; |
| 229 | |
| 230 | static Bits DB_FASTCALL WaveForm0( Bitu i, Bitu volume ) { |
| 231 | Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0 |
| 232 | Bitu wave = SinTable[i & 511]; |
| 233 | return (MakeVolume( wave, volume ) ^ neg) - neg; |
| 234 | } |
| 235 | static Bits DB_FASTCALL WaveForm1( Bitu i, Bitu volume ) { |
| 236 | Bit32u wave = SinTable[i & 511]; |
| 237 | wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 ); |
| 238 | return MakeVolume( wave, volume ); |
| 239 | } |
| 240 | static Bits DB_FASTCALL WaveForm2( Bitu i, Bitu volume ) { |
| 241 | Bitu wave = SinTable[i & 511]; |
| 242 | return MakeVolume( wave, volume ); |
| 243 | } |
| 244 | static Bits DB_FASTCALL WaveForm3( Bitu i, Bitu volume ) { |
| 245 | Bitu wave = SinTable[i & 255]; |
| 246 | wave |= ( ( (i ^ 256 ) & 256) - 1) >> ( 32 - 12 ); |
| 247 | return MakeVolume( wave, volume ); |
| 248 | } |
| 249 | static Bits DB_FASTCALL WaveForm4( Bitu i, Bitu volume ) { |
| 250 | //Twice as fast |
| 251 | i <<= 1; |
| 252 | Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0 |
| 253 | Bitu wave = SinTable[i & 511]; |
| 254 | wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 ); |
| 255 | return (MakeVolume( wave, volume ) ^ neg) - neg; |
| 256 | } |
| 257 | static Bits DB_FASTCALL WaveForm5( Bitu i, Bitu volume ) { |
| 258 | //Twice as fast |
| 259 | i <<= 1; |
| 260 | Bitu wave = SinTable[i & 511]; |
| 261 | wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 ); |
| 262 | return MakeVolume( wave, volume ); |
| 263 | } |
| 264 | static Bits DB_FASTCALL WaveForm6( Bitu i, Bitu volume ) { |
| 265 | Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0 |
| 266 | return (MakeVolume( 0, volume ) ^ neg) - neg; |
| 267 | } |
| 268 | static Bits DB_FASTCALL WaveForm7( Bitu i, Bitu volume ) { |
| 269 | //Negative is reversed here |
| 270 | Bits neg = (( i >> 9) & 1) - 1; |
| 271 | Bitu wave = (i << 3); |
| 272 | //When negative the volume also runs backwards |
| 273 | wave = ((wave ^ neg) - neg) & 4095; |
| 274 | return (MakeVolume( wave, volume ) ^ neg) - neg; |
| 275 | } |
| 276 | |
| 277 | static const WaveHandler WaveHandlerTable[8] = { |
| 278 | WaveForm0, WaveForm1, WaveForm2, WaveForm3, |
| 279 | WaveForm4, WaveForm5, WaveForm6, WaveForm7 |
| 280 | }; |
| 281 | |
| 282 | #endif |
| 283 | |
| 284 | /* |
| 285 | Operator |
| 286 | */ |
| 287 | |
| 288 | //We zero out when rate == 0 |
| 289 | inline void Operator::UpdateAttack( const Chip* chip ) { |
| 290 | Bit8u rate = reg60 >> 4; |
| 291 | if ( rate ) { |
| 292 | Bit8u val = (rate << 2) + ksr; |
| 293 | attackAdd = chip->attackRates[ val ]; |
| 294 | rateZero &= ~(1 << ATTACK); |
| 295 | } else { |
| 296 | attackAdd = 0; |
| 297 | rateZero |= (1 << ATTACK); |
| 298 | } |
| 299 | } |
| 300 | inline void Operator::UpdateDecay( const Chip* chip ) { |
| 301 | Bit8u rate = reg60 & 0xf; |
| 302 | if ( rate ) { |
| 303 | Bit8u val = (rate << 2) + ksr; |
| 304 | decayAdd = chip->linearRates[ val ]; |
| 305 | rateZero &= ~(1 << DECAY); |
| 306 | } else { |
| 307 | decayAdd = 0; |
| 308 | rateZero |= (1 << DECAY); |
| 309 | } |
| 310 | } |
| 311 | inline void Operator::UpdateRelease( const Chip* chip ) { |
| 312 | Bit8u rate = reg80 & 0xf; |
| 313 | if ( rate ) { |
| 314 | Bit8u val = (rate << 2) + ksr; |
| 315 | releaseAdd = chip->linearRates[ val ]; |
| 316 | rateZero &= ~(1 << RELEASE); |
| 317 | if ( !(reg20 & MASK_SUSTAIN ) ) { |
| 318 | rateZero &= ~( 1 << SUSTAIN ); |
| 319 | } |
| 320 | } else { |
| 321 | rateZero |= (1 << RELEASE); |
| 322 | releaseAdd = 0; |
| 323 | if ( !(reg20 & MASK_SUSTAIN ) ) { |
| 324 | rateZero |= ( 1 << SUSTAIN ); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | inline void Operator::UpdateAttenuation( ) { |
| 330 | Bit8u kslBase = (Bit8u)((chanData >> SHIFT_KSLBASE) & 0xff); |
| 331 | Bit32u tl = reg40 & 0x3f; |
| 332 | Bit8u kslShift = KslShiftTable[ reg40 >> 6 ]; |
| 333 | //Make sure the attenuation goes to the right bits |
| 334 | totalLevel = tl << ( ENV_BITS - 7 ); //Total level goes 2 bits below max |
| 335 | totalLevel += ( kslBase << ENV_EXTRA ) >> kslShift; |
| 336 | } |
| 337 | |
| 338 | void Operator::UpdateFrequency( ) { |
| 339 | Bit32u freq = chanData & (( 1 << 10 ) - 1); |
| 340 | Bit32u block = (chanData >> 10) & 0xff; |
| 341 | #ifdef WAVE_PRECISION |
| 342 | block = 7 - block; |
| 343 | waveAdd = ( freq * freqMul ) >> block; |
| 344 | #else |
| 345 | waveAdd = ( freq << block ) * freqMul; |
| 346 | #endif |
| 347 | if ( reg20 & MASK_VIBRATO ) { |
| 348 | vibStrength = (Bit8u)(freq >> 7); |
| 349 | |
| 350 | #ifdef WAVE_PRECISION |
| 351 | vibrato = ( vibStrength * freqMul ) >> block; |
| 352 | #else |
| 353 | vibrato = ( vibStrength << block ) * freqMul; |
| 354 | #endif |
| 355 | } else { |
| 356 | vibStrength = 0; |
| 357 | vibrato = 0; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void Operator::UpdateRates( const Chip* chip ) { |
| 362 | //Mame seems to reverse this where enabling ksr actually lowers |
| 363 | //the rate, but pdf manuals says otherwise? |
| 364 | Bit8u newKsr = (Bit8u)((chanData >> SHIFT_KEYCODE) & 0xff); |
| 365 | if ( !( reg20 & MASK_KSR ) ) { |
| 366 | newKsr >>= 2; |
| 367 | } |
| 368 | if ( ksr == newKsr ) |
| 369 | return; |
| 370 | ksr = newKsr; |
| 371 | UpdateAttack( chip ); |
| 372 | UpdateDecay( chip ); |
| 373 | UpdateRelease( chip ); |
| 374 | } |
| 375 | |
| 376 | INLINE Bit32s Operator::RateForward( Bit32u add ) { |
| 377 | rateIndex += add; |
| 378 | Bit32s ret = rateIndex >> RATE_SH; |
| 379 | rateIndex = rateIndex & RATE_MASK; |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | template< Operator::State yes> |
| 384 | Bits Operator::TemplateVolume( ) { |
| 385 | Bit32s vol = volume; |
| 386 | Bit32s change; |
| 387 | switch ( yes ) { |
| 388 | case OFF: |
| 389 | return ENV_MAX; |
| 390 | case ATTACK: |
| 391 | change = RateForward( attackAdd ); |
| 392 | if ( !change ) |
| 393 | return vol; |
| 394 | vol += ( (~vol) * change ) >> 3; |
| 395 | if ( vol < ENV_MIN ) { |
| 396 | volume = ENV_MIN; |
| 397 | rateIndex = 0; |
| 398 | SetState( DECAY ); |
| 399 | return ENV_MIN; |
| 400 | } |
| 401 | break; |
| 402 | case DECAY: |
| 403 | vol += RateForward( decayAdd ); |
| 404 | if ( GCC_UNLIKELY(vol >= sustainLevel) ) { |
| 405 | //Check if we didn't overshoot max attenuation, then just go off |
| 406 | if ( GCC_UNLIKELY(vol >= ENV_MAX) ) { |
| 407 | volume = ENV_MAX; |
| 408 | SetState( OFF ); |
| 409 | return ENV_MAX; |
| 410 | } |
| 411 | //Continue as sustain |
| 412 | rateIndex = 0; |
| 413 | SetState( SUSTAIN ); |
| 414 | } |
| 415 | break; |
| 416 | case SUSTAIN: |
| 417 | if ( reg20 & MASK_SUSTAIN ) { |
| 418 | return vol; |
| 419 | } |
| 420 | //In sustain phase, but not sustaining, do regular release |
| 421 | case RELEASE: |
| 422 | vol += RateForward( releaseAdd );; |
| 423 | if ( GCC_UNLIKELY(vol >= ENV_MAX) ) { |
| 424 | volume = ENV_MAX; |
| 425 | SetState( OFF ); |
| 426 | return ENV_MAX; |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | volume = vol; |
| 431 | return vol; |
| 432 | } |
| 433 | |
| 434 | static const VolumeHandler VolumeHandlerTable[5] = { |
| 435 | &Operator::TemplateVolume< Operator::OFF >, |
| 436 | &Operator::TemplateVolume< Operator::RELEASE >, |
| 437 | &Operator::TemplateVolume< Operator::SUSTAIN >, |
| 438 | &Operator::TemplateVolume< Operator::DECAY >, |
| 439 | &Operator::TemplateVolume< Operator::ATTACK > |
| 440 | }; |
| 441 | |
| 442 | INLINE Bitu Operator::ForwardVolume() { |
| 443 | return currentLevel + (this->*volHandler)(); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | INLINE Bitu Operator::ForwardWave() { |
| 448 | waveIndex += waveCurrent; |
| 449 | return waveIndex >> WAVE_SH; |
| 450 | } |
| 451 | |
| 452 | void Operator::Write20( const Chip* chip, Bit8u val ) { |
| 453 | Bit8u change = (reg20 ^ val ); |
| 454 | if ( !change ) |
| 455 | return; |
| 456 | reg20 = val; |
| 457 | //Shift the tremolo bit over the entire register, saved a branch, YES! |
| 458 | tremoloMask = (Bit8s)(val) >> 7; |
| 459 | tremoloMask &= ~(( 1 << ENV_EXTRA ) -1); |
| 460 | //Update specific features based on changes |
| 461 | if ( change & MASK_KSR ) { |
| 462 | UpdateRates( chip ); |
| 463 | } |
| 464 | //With sustain enable the volume doesn't change |
| 465 | if ( reg20 & MASK_SUSTAIN || ( !releaseAdd ) ) { |
| 466 | rateZero |= ( 1 << SUSTAIN ); |
| 467 | } else { |
| 468 | rateZero &= ~( 1 << SUSTAIN ); |
| 469 | } |
| 470 | //Frequency multiplier or vibrato changed |
| 471 | if ( change & (0xf | MASK_VIBRATO) ) { |
| 472 | freqMul = chip->freqMul[ val & 0xf ]; |
| 473 | UpdateFrequency(); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void Operator::Write40( const Chip* /*chip*/, Bit8u val ) { |
| 478 | if (!(reg40 ^ val )) |
| 479 | return; |
| 480 | reg40 = val; |
| 481 | UpdateAttenuation( ); |
| 482 | } |
| 483 | |
| 484 | void Operator::Write60( const Chip* chip, Bit8u val ) { |
| 485 | Bit8u change = reg60 ^ val; |
| 486 | reg60 = val; |
| 487 | if ( change & 0x0f ) { |
| 488 | UpdateDecay( chip ); |
| 489 | } |
| 490 | if ( change & 0xf0 ) { |
| 491 | UpdateAttack( chip ); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void Operator::Write80( const Chip* chip, Bit8u val ) { |
| 496 | Bit8u change = (reg80 ^ val ); |
| 497 | if ( !change ) |
| 498 | return; |
| 499 | reg80 = val; |
| 500 | Bit8u sustain = val >> 4; |
| 501 | //Turn 0xf into 0x1f |
| 502 | sustain |= ( sustain + 1) & 0x10; |
| 503 | sustainLevel = sustain << ( ENV_BITS - 5 ); |
| 504 | if ( change & 0x0f ) { |
| 505 | UpdateRelease( chip ); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void Operator::WriteE0( const Chip* chip, Bit8u val ) { |
| 510 | if ( !(regE0 ^ val) ) |
| 511 | return; |
| 512 | //in opl3 mode you can always selet 7 waveforms regardless of waveformselect |
| 513 | Bit8u waveForm = val & ( ( 0x3 & chip->waveFormMask ) | (0x7 & chip->opl3Active ) ); |
| 514 | regE0 = val; |
| 515 | #if ( DBOPL_WAVE == WAVE_HANDLER ) |
| 516 | waveHandler = WaveHandlerTable[ waveForm ]; |
| 517 | #else |
| 518 | waveBase = WaveTable + WaveBaseTable[ waveForm ]; |
| 519 | waveStart = WaveStartTable[ waveForm ] << WAVE_SH; |
| 520 | waveMask = WaveMaskTable[ waveForm ]; |
| 521 | #endif |
| 522 | } |
| 523 | |
| 524 | INLINE void Operator::SetState( Bit8u s ) { |
| 525 | state = s; |
| 526 | volHandler = VolumeHandlerTable[ s ]; |
| 527 | } |
| 528 | |
| 529 | INLINE bool Operator::Silent() const { |
| 530 | if ( !ENV_SILENT( totalLevel + volume ) ) |
| 531 | return false; |
| 532 | if ( !(rateZero & ( 1 << state ) ) ) |
| 533 | return false; |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | INLINE void Operator::Prepare( const Chip* chip ) { |
| 538 | currentLevel = totalLevel + (chip->tremoloValue & tremoloMask); |
| 539 | waveCurrent = waveAdd; |
| 540 | if ( vibStrength >> chip->vibratoShift ) { |
| 541 | Bit32s add = vibrato >> chip->vibratoShift; |
| 542 | //Sign extend over the shift value |
| 543 | Bit32s neg = chip->vibratoSign; |
| 544 | //Negate the add with -1 or 0 |
| 545 | add = ( add ^ neg ) - neg; |
| 546 | waveCurrent += add; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void Operator::KeyOn( Bit8u mask ) { |
| 551 | if ( !keyOn ) { |
| 552 | //Restart the frequency generator |
| 553 | #if ( DBOPL_WAVE > WAVE_HANDLER ) |
| 554 | waveIndex = waveStart; |
| 555 | #else |
| 556 | waveIndex = 0; |
| 557 | #endif |
| 558 | rateIndex = 0; |
| 559 | SetState( ATTACK ); |
| 560 | } |
| 561 | keyOn |= mask; |
| 562 | } |
| 563 | |
| 564 | void Operator::KeyOff( Bit8u mask ) { |
| 565 | keyOn &= ~mask; |
| 566 | if ( !keyOn ) { |
| 567 | if ( state != OFF ) { |
| 568 | SetState( RELEASE ); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | INLINE Bits Operator::GetWave( Bitu index, Bitu vol ) { |
| 574 | #if ( DBOPL_WAVE == WAVE_HANDLER ) |
| 575 | return waveHandler( index, vol << ( 3 - ENV_EXTRA ) ); |
| 576 | #elif ( DBOPL_WAVE == WAVE_TABLEMUL ) |
| 577 | return (waveBase[ index & waveMask ] * MulTable[ vol >> ENV_EXTRA ]) >> MUL_SH; |
| 578 | #elif ( DBOPL_WAVE == WAVE_TABLELOG ) |
| 579 | Bit32s wave = waveBase[ index & waveMask ]; |
| 580 | Bit32u total = ( wave & 0x7fff ) + vol << ( 3 - ENV_EXTRA ); |
| 581 | Bit32s sig = ExpTable[ total & 0xff ]; |
| 582 | Bit32u exp = total >> 8; |
| 583 | Bit32s neg = wave >> 16; |
| 584 | return ((sig ^ neg) - neg) >> exp; |
| 585 | #else |
| 586 | #error "No valid wave routine" |
| 587 | #endif |
| 588 | } |
| 589 | |
| 590 | Bits INLINE Operator::GetSample( Bits modulation ) { |
| 591 | Bitu vol = ForwardVolume(); |
| 592 | if ( ENV_SILENT( vol ) ) { |
| 593 | //Simply forward the wave |
| 594 | waveIndex += waveCurrent; |
| 595 | return 0; |
| 596 | } else { |
| 597 | Bitu index = ForwardWave(); |
| 598 | index += modulation; |
| 599 | return GetWave( index, vol ); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | Operator::Operator() { |
| 604 | chanData = 0; |
| 605 | freqMul = 0; |
| 606 | waveIndex = 0; |
| 607 | waveAdd = 0; |
| 608 | waveCurrent = 0; |
| 609 | keyOn = 0; |
| 610 | ksr = 0; |
| 611 | reg20 = 0; |
| 612 | reg40 = 0; |
| 613 | reg60 = 0; |
| 614 | reg80 = 0; |
| 615 | regE0 = 0; |
| 616 | SetState( OFF ); |
| 617 | rateZero = (1 << OFF); |
| 618 | sustainLevel = ENV_MAX; |
| 619 | currentLevel = ENV_MAX; |
| 620 | totalLevel = ENV_MAX; |
| 621 | volume = ENV_MAX; |
| 622 | releaseAdd = 0; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | Channel |
| 627 | */ |
| 628 | |
| 629 | Channel::Channel() { |
| 630 | old[0] = old[1] = 0; |
| 631 | chanData = 0; |
| 632 | regB0 = 0; |
| 633 | regC0 = 0; |
| 634 | maskLeft = -1; |
| 635 | maskRight = -1; |
| 636 | feedback = 31; |
| 637 | fourMask = 0; |
| 638 | synthHandler = &Channel::BlockTemplate< sm2FM >; |
| 639 | }; |
| 640 | |
| 641 | void Channel::SetChanData( const Chip* chip, Bit32u data ) { |
| 642 | Bit32u change = chanData ^ data; |
| 643 | chanData = data; |
| 644 | Op( 0 )->chanData = data; |
| 645 | Op( 1 )->chanData = data; |
| 646 | //Since a frequency update triggered this, always update frequency |
| 647 | Op( 0 )->UpdateFrequency(); |
| 648 | Op( 1 )->UpdateFrequency(); |
| 649 | if ( change & ( 0xff << SHIFT_KSLBASE ) ) { |
| 650 | Op( 0 )->UpdateAttenuation(); |
| 651 | Op( 1 )->UpdateAttenuation(); |
| 652 | } |
| 653 | if ( change & ( 0xff << SHIFT_KEYCODE ) ) { |
| 654 | Op( 0 )->UpdateRates( chip ); |
| 655 | Op( 1 )->UpdateRates( chip ); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | void Channel::UpdateFrequency( const Chip* chip, Bit8u fourOp ) { |
| 660 | //Extrace the frequency bits |
| 661 | Bit32u data = chanData & 0xffff; |
| 662 | Bit32u kslBase = KslTable[ data >> 6 ]; |
| 663 | Bit32u keyCode = ( data & 0x1c00) >> 9; |
| 664 | if ( chip->reg08 & 0x40 ) { |
| 665 | keyCode |= ( data & 0x100)>>8; /* notesel == 1 */ |
| 666 | } else { |
| 667 | keyCode |= ( data & 0x200)>>9; /* notesel == 0 */ |
| 668 | } |
| 669 | //Add the keycode and ksl into the highest bits of chanData |
| 670 | data |= (keyCode << SHIFT_KEYCODE) | ( kslBase << SHIFT_KSLBASE ); |
| 671 | ( this + 0 )->SetChanData( chip, data ); |
| 672 | if ( fourOp & 0x3f ) { |
| 673 | ( this + 1 )->SetChanData( chip, data ); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | void Channel::WriteA0( const Chip* chip, Bit8u val ) { |
| 678 | Bit8u fourOp = chip->reg104 & chip->opl3Active & fourMask; |
| 679 | //Don't handle writes to silent fourop channels |
| 680 | if ( fourOp > 0x80 ) |
| 681 | return; |
| 682 | Bit32u change = (chanData ^ val ) & 0xff; |
| 683 | if ( change ) { |
| 684 | chanData ^= change; |
| 685 | UpdateFrequency( chip, fourOp ); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | void Channel::WriteB0( const Chip* chip, Bit8u val ) { |
| 690 | Bit8u fourOp = chip->reg104 & chip->opl3Active & fourMask; |
| 691 | //Don't handle writes to silent fourop channels |
| 692 | if ( fourOp > 0x80 ) |
| 693 | return; |
| 694 | Bitu change = (chanData ^ ( val << 8 ) ) & 0x1f00; |
| 695 | if ( change ) { |
| 696 | chanData ^= change; |
| 697 | UpdateFrequency( chip, fourOp ); |
| 698 | } |
| 699 | //Check for a change in the keyon/off state |
| 700 | if ( !(( val ^ regB0) & 0x20)) |
| 701 | return; |
| 702 | regB0 = val; |
| 703 | if ( val & 0x20 ) { |
| 704 | Op(0)->KeyOn( 0x1 ); |
| 705 | Op(1)->KeyOn( 0x1 ); |
| 706 | if ( fourOp & 0x3f ) { |
| 707 | ( this + 1 )->Op(0)->KeyOn( 1 ); |
| 708 | ( this + 1 )->Op(1)->KeyOn( 1 ); |
| 709 | } |
| 710 | } else { |
| 711 | Op(0)->KeyOff( 0x1 ); |
| 712 | Op(1)->KeyOff( 0x1 ); |
| 713 | if ( fourOp & 0x3f ) { |
| 714 | ( this + 1 )->Op(0)->KeyOff( 1 ); |
| 715 | ( this + 1 )->Op(1)->KeyOff( 1 ); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | void Channel::WriteC0( const Chip* chip, Bit8u val ) { |
| 721 | Bit8u change = val ^ regC0; |
| 722 | if ( !change ) |
| 723 | return; |
| 724 | regC0 = val; |
| 725 | feedback = ( val >> 1 ) & 7; |
| 726 | if ( feedback ) { |
| 727 | //We shift the input to the right 10 bit wave index value |
| 728 | feedback = 9 - feedback; |
| 729 | } else { |
| 730 | feedback = 31; |
| 731 | } |
| 732 | //Select the new synth mode |
| 733 | if ( chip->opl3Active ) { |
| 734 | //4-op mode enabled for this channel |
| 735 | if ( (chip->reg104 & fourMask) & 0x3f ) { |
| 736 | Channel* chan0, *chan1; |
| 737 | //Check if it's the 2nd channel in a 4-op |
| 738 | if ( !(fourMask & 0x80 ) ) { |
| 739 | chan0 = this; |
| 740 | chan1 = this + 1; |
| 741 | } else { |
| 742 | chan0 = this - 1; |
| 743 | chan1 = this; |
| 744 | } |
| 745 | |
| 746 | Bit8u synth = ( (chan0->regC0 & 1) << 0 )| (( chan1->regC0 & 1) << 1 ); |
| 747 | switch ( synth ) { |
| 748 | case 0: |
| 749 | chan0->synthHandler = &Channel::BlockTemplate< sm3FMFM >; |
| 750 | break; |
| 751 | case 1: |
| 752 | chan0->synthHandler = &Channel::BlockTemplate< sm3AMFM >; |
| 753 | break; |
| 754 | case 2: |
| 755 | chan0->synthHandler = &Channel::BlockTemplate< sm3FMAM >; |
| 756 | break; |
| 757 | case 3: |
| 758 | chan0->synthHandler = &Channel::BlockTemplate< sm3AMAM >; |
| 759 | break; |
| 760 | } |
| 761 | //Disable updating percussion channels |
| 762 | } else if ((fourMask & 0x40) && ( chip->regBD & 0x20) ) { |
| 763 | |
| 764 | //Regular dual op, am or fm |
| 765 | } else if ( val & 1 ) { |
| 766 | synthHandler = &Channel::BlockTemplate< sm3AM >; |
| 767 | } else { |
| 768 | synthHandler = &Channel::BlockTemplate< sm3FM >; |
| 769 | } |
| 770 | maskLeft = ( val & 0x10 ) ? -1 : 0; |
| 771 | maskRight = ( val & 0x20 ) ? -1 : 0; |
| 772 | //opl2 active |
| 773 | } else { |
| 774 | //Disable updating percussion channels |
| 775 | if ( (fourMask & 0x40) && ( chip->regBD & 0x20 ) ) { |
| 776 | |
| 777 | //Regular dual op, am or fm |
| 778 | } else if ( val & 1 ) { |
| 779 | synthHandler = &Channel::BlockTemplate< sm2AM >; |
| 780 | } else { |
| 781 | synthHandler = &Channel::BlockTemplate< sm2FM >; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | void Channel::ResetC0( const Chip* chip ) { |
| 787 | Bit8u val = regC0; |
| 788 | regC0 ^= 0xff; |
| 789 | WriteC0( chip, val ); |
| 790 | }; |
| 791 | |
| 792 | template< bool opl3Mode> |
| 793 | INLINE void Channel::GeneratePercussion( Chip* chip, Bit32s* output ) { |
| 794 | Channel* chan = this; |
| 795 | |
| 796 | //BassDrum |
| 797 | Bit32s mod = (Bit32u)((old[0] + old[1])) >> feedback; |
| 798 | old[0] = old[1]; |
| 799 | old[1] = Op(0)->GetSample( mod ); |
| 800 | |
| 801 | //When bassdrum is in AM mode first operator is ignoed |
| 802 | if ( chan->regC0 & 1 ) { |
| 803 | mod = 0; |
| 804 | } else { |
| 805 | mod = old[0]; |
| 806 | } |
| 807 | Bit32s sample = Op(1)->GetSample( mod ); |
| 808 | |
| 809 | |
| 810 | //Precalculate stuff used by other outputs |
| 811 | Bit32u noiseBit = chip->ForwardNoise() & 0x1; |
| 812 | Bit32u c2 = Op(2)->ForwardWave(); |
| 813 | Bit32u c5 = Op(5)->ForwardWave(); |
| 814 | Bit32u phaseBit = (((c2 & 0x88) ^ ((c2<<5) & 0x80)) | ((c5 ^ (c5<<2)) & 0x20)) ? 0x02 : 0x00; |
| 815 | |
| 816 | //Hi-Hat |
| 817 | Bit32u hhVol = Op(2)->ForwardVolume(); |
| 818 | if ( !ENV_SILENT( hhVol ) ) { |
| 819 | Bit32u hhIndex = (phaseBit<<8) | (0x34 << ( phaseBit ^ (noiseBit << 1 ))); |
| 820 | sample += Op(2)->GetWave( hhIndex, hhVol ); |
| 821 | } |
| 822 | //Snare Drum |
| 823 | Bit32u sdVol = Op(3)->ForwardVolume(); |
| 824 | if ( !ENV_SILENT( sdVol ) ) { |
| 825 | Bit32u sdIndex = ( 0x100 + (c2 & 0x100) ) ^ ( noiseBit << 8 ); |
| 826 | sample += Op(3)->GetWave( sdIndex, sdVol ); |
| 827 | } |
| 828 | //Tom-tom |
| 829 | sample += Op(4)->GetSample( 0 ); |
| 830 | |
| 831 | //Top-Cymbal |
| 832 | Bit32u tcVol = Op(5)->ForwardVolume(); |
| 833 | if ( !ENV_SILENT( tcVol ) ) { |
| 834 | Bit32u tcIndex = (1 + phaseBit) << 8; |
| 835 | sample += Op(5)->GetWave( tcIndex, tcVol ); |
| 836 | } |
| 837 | sample <<= 1; |
| 838 | if ( opl3Mode ) { |
| 839 | output[0] += sample; |
| 840 | output[1] += sample; |
| 841 | } else { |
| 842 | output[0] += sample; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | template<SynthMode mode> |
| 847 | Channel* Channel::BlockTemplate( Chip* chip, Bit32u samples, Bit32s* output ) { |
| 848 | switch( mode ) { |
| 849 | case sm2AM: |
| 850 | case sm3AM: |
| 851 | if ( Op(0)->Silent() && Op(1)->Silent() ) { |
| 852 | old[0] = old[1] = 0; |
| 853 | return (this + 1); |
| 854 | } |
| 855 | break; |
| 856 | case sm2FM: |
| 857 | case sm3FM: |
| 858 | if ( Op(1)->Silent() ) { |
| 859 | old[0] = old[1] = 0; |
| 860 | return (this + 1); |
| 861 | } |
| 862 | break; |
| 863 | case sm3FMFM: |
| 864 | if ( Op(3)->Silent() ) { |
| 865 | old[0] = old[1] = 0; |
| 866 | return (this + 2); |
| 867 | } |
| 868 | break; |
| 869 | case sm3AMFM: |
| 870 | if ( Op(0)->Silent() && Op(3)->Silent() ) { |
| 871 | old[0] = old[1] = 0; |
| 872 | return (this + 2); |
| 873 | } |
| 874 | break; |
| 875 | case sm3FMAM: |
| 876 | if ( Op(1)->Silent() && Op(3)->Silent() ) { |
| 877 | old[0] = old[1] = 0; |
| 878 | return (this + 2); |
| 879 | } |
| 880 | break; |
| 881 | case sm3AMAM: |
| 882 | if ( Op(0)->Silent() && Op(2)->Silent() && Op(3)->Silent() ) { |
| 883 | old[0] = old[1] = 0; |
| 884 | return (this + 2); |
| 885 | } |
| 886 | break; |
| 887 | } |
| 888 | //Init the operators with the the current vibrato and tremolo values |
| 889 | Op( 0 )->Prepare( chip ); |
| 890 | Op( 1 )->Prepare( chip ); |
| 891 | if ( mode > sm4Start ) { |
| 892 | Op( 2 )->Prepare( chip ); |
| 893 | Op( 3 )->Prepare( chip ); |
| 894 | } |
| 895 | if ( mode > sm6Start ) { |
| 896 | Op( 4 )->Prepare( chip ); |
| 897 | Op( 5 )->Prepare( chip ); |
| 898 | } |
| 899 | for ( Bitu i = 0; i < samples; i++ ) { |
| 900 | //Early out for percussion handlers |
| 901 | if ( mode == sm2Percussion ) { |
| 902 | GeneratePercussion<false>( chip, output + i ); |
| 903 | continue; //Prevent some unitialized value bitching |
| 904 | } else if ( mode == sm3Percussion ) { |
| 905 | GeneratePercussion<true>( chip, output + i * 2 ); |
| 906 | continue; //Prevent some unitialized value bitching |
| 907 | } |
| 908 | |
| 909 | //Do unsigned shift so we can shift out all bits but still stay in 10 bit range otherwise |
| 910 | Bit32s mod = (Bit32u)((old[0] + old[1])) >> feedback; |
| 911 | old[0] = old[1]; |
| 912 | old[1] = Op(0)->GetSample( mod ); |
| 913 | Bit32s sample; |
| 914 | Bit32s out0 = old[0]; |
| 915 | if ( mode == sm2AM || mode == sm3AM ) { |
| 916 | sample = out0 + Op(1)->GetSample( 0 ); |
| 917 | } else if ( mode == sm2FM || mode == sm3FM ) { |
| 918 | sample = Op(1)->GetSample( out0 ); |
| 919 | } else if ( mode == sm3FMFM ) { |
| 920 | Bits next = Op(1)->GetSample( out0 ); |
| 921 | next = Op(2)->GetSample( next ); |
| 922 | sample = Op(3)->GetSample( next ); |
| 923 | } else if ( mode == sm3AMFM ) { |
| 924 | sample = out0; |
| 925 | Bits next = Op(1)->GetSample( 0 ); |
| 926 | next = Op(2)->GetSample( next ); |
| 927 | sample += Op(3)->GetSample( next ); |
| 928 | } else if ( mode == sm3FMAM ) { |
| 929 | sample = Op(1)->GetSample( out0 ); |
| 930 | Bits next = Op(2)->GetSample( 0 ); |
| 931 | sample += Op(3)->GetSample( next ); |
| 932 | } else if ( mode == sm3AMAM ) { |
| 933 | sample = out0; |
| 934 | Bits next = Op(1)->GetSample( 0 ); |
| 935 | sample += Op(2)->GetSample( next ); |
| 936 | sample += Op(3)->GetSample( 0 ); |
| 937 | } |
| 938 | switch( mode ) { |
| 939 | case sm2AM: |
| 940 | case sm2FM: |
| 941 | output[ i ] += sample; |
| 942 | break; |
| 943 | case sm3AM: |
| 944 | case sm3FM: |
| 945 | case sm3FMFM: |
| 946 | case sm3AMFM: |
| 947 | case sm3FMAM: |
| 948 | case sm3AMAM: |
| 949 | output[ i * 2 + 0 ] += sample & maskLeft; |
| 950 | output[ i * 2 + 1 ] += sample & maskRight; |
| 951 | break; |
| 952 | } |
| 953 | } |
| 954 | switch( mode ) { |
| 955 | case sm2AM: |
| 956 | case sm2FM: |
| 957 | case sm3AM: |
| 958 | case sm3FM: |
| 959 | return ( this + 1 ); |
| 960 | case sm3FMFM: |
| 961 | case sm3AMFM: |
| 962 | case sm3FMAM: |
| 963 | case sm3AMAM: |
| 964 | return( this + 2 ); |
| 965 | case sm2Percussion: |
| 966 | case sm3Percussion: |
| 967 | return( this + 3 ); |
| 968 | } |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | Chip |
| 974 | */ |
| 975 | |
| 976 | Chip::Chip() { |
| 977 | reg08 = 0; |
| 978 | reg04 = 0; |
| 979 | regBD = 0; |
| 980 | reg104 = 0; |
| 981 | opl3Active = 0; |
| 982 | } |
| 983 | |
| 984 | INLINE Bit32u Chip::ForwardNoise() { |
| 985 | noiseCounter += noiseAdd; |
| 986 | Bitu count = noiseCounter >> LFO_SH; |
| 987 | noiseCounter &= WAVE_MASK; |
| 988 | for ( ; count > 0; --count ) { |
| 989 | //Noise calculation from mame |
| 990 | noiseValue ^= ( 0x800302 ) & ( 0 - (noiseValue & 1 ) ); |
| 991 | noiseValue >>= 1; |
| 992 | } |
| 993 | return noiseValue; |
| 994 | } |
| 995 | |
| 996 | INLINE Bit32u Chip::ForwardLFO( Bit32u samples ) { |
| 997 | //Current vibrato value, runs 4x slower than tremolo |
| 998 | vibratoSign = ( VibratoTable[ vibratoIndex >> 2] ) >> 7; |
| 999 | vibratoShift = ( VibratoTable[ vibratoIndex >> 2] & 7) + vibratoStrength; |
| 1000 | tremoloValue = TremoloTable[ tremoloIndex ] >> tremoloStrength; |
| 1001 | |
| 1002 | //Check hom many samples there can be done before the value changes |
| 1003 | Bit32u todo = LFO_MAX - lfoCounter; |
| 1004 | Bit32u count = (todo + lfoAdd - 1) / lfoAdd; |
| 1005 | if ( count > samples ) { |
| 1006 | count = samples; |
| 1007 | lfoCounter += count * lfoAdd; |
| 1008 | } else { |
| 1009 | lfoCounter += count * lfoAdd; |
| 1010 | lfoCounter &= (LFO_MAX - 1); |
| 1011 | //Maximum of 7 vibrato value * 4 |
| 1012 | vibratoIndex = ( vibratoIndex + 1 ) & 31; |
| 1013 | //Clip tremolo to the the table size |
| 1014 | if ( tremoloIndex + 1 < TREMOLO_TABLE ) |
| 1015 | ++tremoloIndex; |
| 1016 | else |
| 1017 | tremoloIndex = 0; |
| 1018 | } |
| 1019 | return count; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | void Chip::WriteBD( Bit8u val ) { |
| 1024 | Bit8u change = regBD ^ val; |
| 1025 | if ( !change ) |
| 1026 | return; |
| 1027 | regBD = val; |
| 1028 | //TODO could do this with shift and xor? |
| 1029 | vibratoStrength = (val & 0x40) ? 0x00 : 0x01; |
| 1030 | tremoloStrength = (val & 0x80) ? 0x00 : 0x02; |
| 1031 | if ( val & 0x20 ) { |
| 1032 | //Drum was just enabled, make sure channel 6 has the right synth |
| 1033 | if ( change & 0x20 ) { |
| 1034 | if ( opl3Active ) { |
| 1035 | chan[6].synthHandler = &Channel::BlockTemplate< sm3Percussion >; |
| 1036 | } else { |
| 1037 | chan[6].synthHandler = &Channel::BlockTemplate< sm2Percussion >; |
| 1038 | } |
| 1039 | } |
| 1040 | //Bass Drum |
| 1041 | if ( val & 0x10 ) { |
| 1042 | chan[6].op[0].KeyOn( 0x2 ); |
| 1043 | chan[6].op[1].KeyOn( 0x2 ); |
| 1044 | } else { |
| 1045 | chan[6].op[0].KeyOff( 0x2 ); |
| 1046 | chan[6].op[1].KeyOff( 0x2 ); |
| 1047 | } |
| 1048 | //Hi-Hat |
| 1049 | if ( val & 0x1 ) { |
| 1050 | chan[7].op[0].KeyOn( 0x2 ); |
| 1051 | } else { |
| 1052 | chan[7].op[0].KeyOff( 0x2 ); |
| 1053 | } |
| 1054 | //Snare |
| 1055 | if ( val & 0x8 ) { |
| 1056 | chan[7].op[1].KeyOn( 0x2 ); |
| 1057 | } else { |
| 1058 | chan[7].op[1].KeyOff( 0x2 ); |
| 1059 | } |
| 1060 | //Tom-Tom |
| 1061 | if ( val & 0x4 ) { |
| 1062 | chan[8].op[0].KeyOn( 0x2 ); |
| 1063 | } else { |
| 1064 | chan[8].op[0].KeyOff( 0x2 ); |
| 1065 | } |
| 1066 | //Top Cymbal |
| 1067 | if ( val & 0x2 ) { |
| 1068 | chan[8].op[1].KeyOn( 0x2 ); |
| 1069 | } else { |
| 1070 | chan[8].op[1].KeyOff( 0x2 ); |
| 1071 | } |
| 1072 | //Toggle keyoffs when we turn off the percussion |
| 1073 | } else if ( change & 0x20 ) { |
| 1074 | //Trigger a reset to setup the original synth handler |
| 1075 | chan[6].ResetC0( this ); |
| 1076 | chan[6].op[0].KeyOff( 0x2 ); |
| 1077 | chan[6].op[1].KeyOff( 0x2 ); |
| 1078 | chan[7].op[0].KeyOff( 0x2 ); |
| 1079 | chan[7].op[1].KeyOff( 0x2 ); |
| 1080 | chan[8].op[0].KeyOff( 0x2 ); |
| 1081 | chan[8].op[1].KeyOff( 0x2 ); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | #define REGOP( _FUNC_ ) \ |
| 1087 | index = ( ( reg >> 3) & 0x20 ) | ( reg & 0x1f ); \ |
| 1088 | if ( OpOffsetTable[ index ] ) { \ |
| 1089 | Operator* regOp = (Operator*)( ((char *)this ) + OpOffsetTable[ index ] ); \ |
| 1090 | regOp->_FUNC_( this, val ); \ |
| 1091 | } |
| 1092 | |
| 1093 | #define REGCHAN( _FUNC_ ) \ |
| 1094 | index = ( ( reg >> 4) & 0x10 ) | ( reg & 0xf ); \ |
| 1095 | if ( ChanOffsetTable[ index ] ) { \ |
| 1096 | Channel* regChan = (Channel*)( ((char *)this ) + ChanOffsetTable[ index ] ); \ |
| 1097 | regChan->_FUNC_( this, val ); \ |
| 1098 | } |
| 1099 | |
| 1100 | void Chip::WriteReg( Bit32u reg, Bit8u val ) { |
| 1101 | Bitu index; |
| 1102 | switch ( (reg & 0xf0) >> 4 ) { |
| 1103 | case 0x00 >> 4: |
| 1104 | if ( reg == 0x01 ) { |
| 1105 | waveFormMask = ( val & 0x20 ) ? 0x7 : 0x0; |
| 1106 | } else if ( reg == 0x104 ) { |
| 1107 | //Only detect changes in lowest 6 bits |
| 1108 | if ( !((reg104 ^ val) & 0x3f) ) |
| 1109 | return; |
| 1110 | //Always keep the highest bit enabled, for checking > 0x80 |
| 1111 | reg104 = 0x80 | ( val & 0x3f ); |
| 1112 | } else if ( reg == 0x105 ) { |
| 1113 | //MAME says the real opl3 doesn't reset anything on opl3 disable/enable till the next write in another register |
| 1114 | if ( !((opl3Active ^ val) & 1 ) ) |
| 1115 | return; |
| 1116 | opl3Active = ( val & 1 ) ? 0xff : 0; |
| 1117 | //Update the 0xc0 register for all channels to signal the switch to mono/stereo handlers |
| 1118 | for ( int i = 0; i < 18;i++ ) { |
| 1119 | chan[i].ResetC0( this ); |
| 1120 | } |
| 1121 | } else if ( reg == 0x08 ) { |
| 1122 | reg08 = val; |
| 1123 | } |
| 1124 | case 0x10 >> 4: |
| 1125 | break; |
| 1126 | case 0x20 >> 4: |
| 1127 | case 0x30 >> 4: |
| 1128 | REGOP( Write20 ); |
| 1129 | break; |
| 1130 | case 0x40 >> 4: |
| 1131 | case 0x50 >> 4: |
| 1132 | REGOP( Write40 ); |
| 1133 | break; |
| 1134 | case 0x60 >> 4: |
| 1135 | case 0x70 >> 4: |
| 1136 | REGOP( Write60 ); |
| 1137 | break; |
| 1138 | case 0x80 >> 4: |
| 1139 | case 0x90 >> 4: |
| 1140 | REGOP( Write80 ); |
| 1141 | break; |
| 1142 | case 0xa0 >> 4: |
| 1143 | REGCHAN( WriteA0 ); |
| 1144 | break; |
| 1145 | case 0xb0 >> 4: |
| 1146 | if ( reg == 0xbd ) { |
| 1147 | WriteBD( val ); |
| 1148 | } else { |
| 1149 | REGCHAN( WriteB0 ); |
| 1150 | } |
| 1151 | break; |
| 1152 | case 0xc0 >> 4: |
| 1153 | REGCHAN( WriteC0 ); |
| 1154 | case 0xd0 >> 4: |
| 1155 | break; |
| 1156 | case 0xe0 >> 4: |
| 1157 | case 0xf0 >> 4: |
| 1158 | REGOP( WriteE0 ); |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | Bit32u Chip::WriteAddr( Bit32u port, Bit8u val ) { |
| 1165 | switch ( port & 3 ) { |
| 1166 | case 0: |
| 1167 | return val; |
| 1168 | case 2: |
| 1169 | if ( opl3Active || (val == 0x05) ) |
| 1170 | return 0x100 | val; |
| 1171 | else |
| 1172 | return val; |
| 1173 | } |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | void Chip::GenerateBlock2( Bitu total, Bit32s* output ) { |
| 1178 | while ( total > 0 ) { |
| 1179 | Bit32u samples = ForwardLFO( total ); |
| 1180 | memset(output, 0, sizeof(Bit32s) * samples); |
| 1181 | int count = 0; |
| 1182 | for( Channel* ch = chan; ch < chan + 9; ) { |
| 1183 | count++; |
| 1184 | ch = (ch->*(ch->synthHandler))( this, samples, output ); |
| 1185 | } |
| 1186 | total -= samples; |
| 1187 | output += samples; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | void Chip::GenerateBlock3( Bitu total, Bit32s* output ) { |
| 1192 | while ( total > 0 ) { |
| 1193 | Bit32u samples = ForwardLFO( total ); |
| 1194 | memset(output, 0, sizeof(Bit32s) * samples *2); |
| 1195 | int count = 0; |
| 1196 | for( Channel* ch = chan; ch < chan + 18; ) { |
| 1197 | count++; |
| 1198 | ch = (ch->*(ch->synthHandler))( this, samples, output ); |
| 1199 | } |
| 1200 | total -= samples; |
| 1201 | output += samples * 2; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | void Chip::Setup( Bit32u rate ) { |
| 1206 | double original = OPLRATE; |
| 1207 | // double original = rate; |
| 1208 | double scale = original / (double)rate; |
| 1209 | |
| 1210 | //Noise counter is run at the same precision as general waves |
| 1211 | noiseAdd = (Bit32u)( 0.5 + scale * ( 1 << LFO_SH ) ); |
| 1212 | noiseCounter = 0; |
| 1213 | noiseValue = 1; //Make sure it triggers the noise xor the first time |
| 1214 | //The low frequency oscillation counter |
| 1215 | //Every time his overflows vibrato and tremoloindex are increased |
| 1216 | lfoAdd = (Bit32u)( 0.5 + scale * ( 1 << LFO_SH ) ); |
| 1217 | lfoCounter = 0; |
| 1218 | vibratoIndex = 0; |
| 1219 | tremoloIndex = 0; |
| 1220 | |
| 1221 | //With higher octave this gets shifted up |
| 1222 | //-1 since the freqCreateTable = *2 |
| 1223 | #ifdef WAVE_PRECISION |
| 1224 | double freqScale = ( 1 << 7 ) * scale * ( 1 << ( WAVE_SH - 1 - 10)); |
| 1225 | for ( int i = 0; i < 16; i++ ) { |
| 1226 | freqMul[i] = (Bit32u)( 0.5 + freqScale * FreqCreateTable[ i ] ); |
| 1227 | } |
| 1228 | #else |
| 1229 | Bit32u freqScale = (Bit32u)( 0.5 + scale * ( 1 << ( WAVE_SH - 1 - 10))); |
| 1230 | for ( int i = 0; i < 16; i++ ) { |
| 1231 | freqMul[i] = freqScale * FreqCreateTable[ i ]; |
| 1232 | } |
| 1233 | #endif |
| 1234 | |
| 1235 | //-3 since the real envelope takes 8 steps to reach the single value we supply |
| 1236 | for ( Bit8u i = 0; i < 76; i++ ) { |
| 1237 | Bit8u index, shift; |
| 1238 | EnvelopeSelect( i, index, shift ); |
| 1239 | linearRates[i] = (Bit32u)( scale * (EnvelopeIncreaseTable[ index ] << ( RATE_SH + ENV_EXTRA - shift - 3 ))); |
| 1240 | } |
| 1241 | //Generate the best matching attack rate |
| 1242 | for ( Bit8u i = 0; i < 62; i++ ) { |
| 1243 | Bit8u index, shift; |
| 1244 | EnvelopeSelect( i, index, shift ); |
| 1245 | //Original amount of samples the attack would take |
| 1246 | Bit32s original = (Bit32u)( (AttackSamplesTable[ index ] << shift) / scale); |
| 1247 | |
| 1248 | Bit32s guessAdd = (Bit32u)( scale * (EnvelopeIncreaseTable[ index ] << ( RATE_SH - shift - 3 ))); |
| 1249 | Bit32s bestAdd = guessAdd; |
| 1250 | Bit32u bestDiff = 1 << 30; |
| 1251 | for( Bit32u passes = 0; passes < 16; passes ++ ) { |
| 1252 | Bit32s volume = ENV_MAX; |
| 1253 | Bit32s samples = 0; |
| 1254 | Bit32u count = 0; |
| 1255 | while ( volume > 0 && samples < original * 2 ) { |
| 1256 | count += guessAdd; |
| 1257 | Bit32s change = count >> RATE_SH; |
| 1258 | count &= RATE_MASK; |
| 1259 | if ( GCC_UNLIKELY(change) ) { // less than 1 % |
| 1260 | volume += ( ~volume * change ) >> 3; |
| 1261 | } |
| 1262 | samples++; |
| 1263 | |
| 1264 | } |
| 1265 | Bit32s diff = original - samples; |
| 1266 | Bit32u lDiff = labs( diff ); |
| 1267 | //Init last on first pass |
| 1268 | if ( lDiff < bestDiff ) { |
| 1269 | bestDiff = lDiff; |
| 1270 | bestAdd = guessAdd; |
| 1271 | if ( !bestDiff ) |
| 1272 | break; |
| 1273 | } |
| 1274 | //Below our target |
| 1275 | if ( diff < 0 ) { |
| 1276 | //Better than the last time |
| 1277 | Bit32s mul = ((original - diff) << 12) / original; |
| 1278 | guessAdd = ((guessAdd * mul) >> 12); |
| 1279 | guessAdd++; |
| 1280 | } else if ( diff > 0 ) { |
| 1281 | Bit32s mul = ((original - diff) << 12) / original; |
| 1282 | guessAdd = (guessAdd * mul) >> 12; |
| 1283 | guessAdd--; |
| 1284 | } |
| 1285 | } |
| 1286 | attackRates[i] = bestAdd; |
| 1287 | } |
| 1288 | for ( Bit8u i = 62; i < 76; i++ ) { |
| 1289 | //This should provide instant volume maximizing |
| 1290 | attackRates[i] = 8 << RATE_SH; |
| 1291 | } |
| 1292 | //Setup the channels with the correct four op flags |
| 1293 | //Channels are accessed through a table so they appear linear here |
| 1294 | chan[ 0].fourMask = 0x00 | ( 1 << 0 ); |
| 1295 | chan[ 1].fourMask = 0x80 | ( 1 << 0 ); |
| 1296 | chan[ 2].fourMask = 0x00 | ( 1 << 1 ); |
| 1297 | chan[ 3].fourMask = 0x80 | ( 1 << 1 ); |
| 1298 | chan[ 4].fourMask = 0x00 | ( 1 << 2 ); |
| 1299 | chan[ 5].fourMask = 0x80 | ( 1 << 2 ); |
| 1300 | |
| 1301 | chan[ 9].fourMask = 0x00 | ( 1 << 3 ); |
| 1302 | chan[10].fourMask = 0x80 | ( 1 << 3 ); |
| 1303 | chan[11].fourMask = 0x00 | ( 1 << 4 ); |
| 1304 | chan[12].fourMask = 0x80 | ( 1 << 4 ); |
| 1305 | chan[13].fourMask = 0x00 | ( 1 << 5 ); |
| 1306 | chan[14].fourMask = 0x80 | ( 1 << 5 ); |
| 1307 | |
| 1308 | //mark the percussion channels |
| 1309 | chan[ 6].fourMask = 0x40; |
| 1310 | chan[ 7].fourMask = 0x40; |
| 1311 | chan[ 8].fourMask = 0x40; |
| 1312 | |
| 1313 | //Clear Everything in opl3 mode |
| 1314 | WriteReg( 0x105, 0x1 ); |
| 1315 | for ( int i = 0; i < 512; i++ ) { |
| 1316 | if ( i == 0x105 ) |
| 1317 | continue; |
| 1318 | WriteReg( i, 0xff ); |
| 1319 | WriteReg( i, 0x0 ); |
| 1320 | } |
| 1321 | WriteReg( 0x105, 0x0 ); |
| 1322 | //Clear everything in opl2 mode |
| 1323 | for ( int i = 0; i < 255; i++ ) { |
| 1324 | WriteReg( i, 0xff ); |
| 1325 | WriteReg( i, 0x0 ); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | static bool doneTables = false; |
| 1330 | void InitTables( void ) { |
| 1331 | if ( doneTables ) |
| 1332 | return; |
| 1333 | doneTables = true; |
| 1334 | #if ( DBOPL_WAVE == WAVE_HANDLER ) || ( DBOPL_WAVE == WAVE_TABLELOG ) |
| 1335 | //Exponential volume table, same as the real adlib |
| 1336 | for ( int i = 0; i < 256; i++ ) { |
| 1337 | //Save them in reverse |
| 1338 | ExpTable[i] = (int)( 0.5 + ( pow(2.0, ( 255 - i) * ( 1.0 /256 ) )-1) * 1024 ); |
| 1339 | ExpTable[i] += 1024; //or remove the -1 oh well :) |
| 1340 | //Preshift to the left once so the final volume can shift to the right |
| 1341 | ExpTable[i] *= 2; |
| 1342 | } |
| 1343 | #endif |
| 1344 | #if ( DBOPL_WAVE == WAVE_HANDLER ) |
| 1345 | //Add 0.5 for the trunc rounding of the integer cast |
| 1346 | //Do a PI sinetable instead of the original 0.5 PI |
| 1347 | for ( int i = 0; i < 512; i++ ) { |
| 1348 | SinTable[i] = (Bit16s)( 0.5 - log10( sin( (i + 0.5) * (PI / 512.0) ) ) / log10(2.0)*256 ); |
| 1349 | } |
| 1350 | #endif |
| 1351 | #if ( DBOPL_WAVE == WAVE_TABLEMUL ) |
| 1352 | //Multiplication based tables |
| 1353 | for ( int i = 0; i < 384; i++ ) { |
| 1354 | int s = i * 8; |
| 1355 | //TODO maybe keep some of the precision errors of the original table? |
| 1356 | double val = ( 0.5 + ( pow(2.0, -1.0 + ( 255 - s) * ( 1.0 /256 ) )) * ( 1 << MUL_SH )); |
| 1357 | MulTable[i] = (Bit16u)(val); |
| 1358 | } |
| 1359 | |
| 1360 | //Sine Wave Base |
| 1361 | for ( int i = 0; i < 512; i++ ) { |
| 1362 | WaveTable[ 0x0200 + i ] = (Bit16s)(sin( (i + 0.5) * (PI / 512.0) ) * 4084); |
| 1363 | WaveTable[ 0x0000 + i ] = -WaveTable[ 0x200 + i ]; |
| 1364 | } |
| 1365 | //Exponential wave |
| 1366 | for ( int i = 0; i < 256; i++ ) { |
| 1367 | WaveTable[ 0x700 + i ] = (Bit16s)( 0.5 + ( pow(2.0, -1.0 + ( 255 - i * 8) * ( 1.0 /256 ) ) ) * 4085 ); |
| 1368 | WaveTable[ 0x6ff - i ] = -WaveTable[ 0x700 + i ]; |
| 1369 | } |
| 1370 | #endif |
| 1371 | #if ( DBOPL_WAVE == WAVE_TABLELOG ) |
| 1372 | //Sine Wave Base |
| 1373 | for ( int i = 0; i < 512; i++ ) { |
| 1374 | WaveTable[ 0x0200 + i ] = (Bit16s)( 0.5 - log10( sin( (i + 0.5) * (PI / 512.0) ) ) / log10(2.0)*256 ); |
| 1375 | WaveTable[ 0x0000 + i ] = ((Bit16s)0x8000) | WaveTable[ 0x200 + i]; |
| 1376 | } |
| 1377 | //Exponential wave |
| 1378 | for ( int i = 0; i < 256; i++ ) { |
| 1379 | WaveTable[ 0x700 + i ] = i * 8; |
| 1380 | WaveTable[ 0x6ff - i ] = ((Bit16s)0x8000) | i * 8; |
| 1381 | } |
| 1382 | #endif |
| 1383 | |
| 1384 | // | |//\\|____|WAV7|//__|/\ |____|/\/\| |
| 1385 | // |\\//| | |WAV7| | \/| | | |
| 1386 | // |06 |0126|27 |7 |3 |4 |4 5 |5 | |
| 1387 | |
| 1388 | #if (( DBOPL_WAVE == WAVE_TABLELOG ) || ( DBOPL_WAVE == WAVE_TABLEMUL )) |
| 1389 | for ( int i = 0; i < 256; i++ ) { |
| 1390 | //Fill silence gaps |
| 1391 | WaveTable[ 0x400 + i ] = WaveTable[0]; |
| 1392 | WaveTable[ 0x500 + i ] = WaveTable[0]; |
| 1393 | WaveTable[ 0x900 + i ] = WaveTable[0]; |
| 1394 | WaveTable[ 0xc00 + i ] = WaveTable[0]; |
| 1395 | WaveTable[ 0xd00 + i ] = WaveTable[0]; |
| 1396 | //Replicate sines in other pieces |
| 1397 | WaveTable[ 0x800 + i ] = WaveTable[ 0x200 + i ]; |
| 1398 | //double speed sines |
| 1399 | WaveTable[ 0xa00 + i ] = WaveTable[ 0x200 + i * 2 ]; |
| 1400 | WaveTable[ 0xb00 + i ] = WaveTable[ 0x000 + i * 2 ]; |
| 1401 | WaveTable[ 0xe00 + i ] = WaveTable[ 0x200 + i * 2 ]; |
| 1402 | WaveTable[ 0xf00 + i ] = WaveTable[ 0x200 + i * 2 ]; |
| 1403 | } |
| 1404 | #endif |
| 1405 | |
| 1406 | //Create the ksl table |
| 1407 | for ( int oct = 0; oct < 8; oct++ ) { |
| 1408 | int base = oct * 8; |
| 1409 | for ( int i = 0; i < 16; i++ ) { |
| 1410 | int val = base - KslCreateTable[i]; |
| 1411 | if ( val < 0 ) |
| 1412 | val = 0; |
| 1413 | //*4 for the final range to match attenuation range |
| 1414 | KslTable[ oct * 16 + i ] = val * 4; |
| 1415 | } |
| 1416 | } |
| 1417 | //Create the Tremolo table, just increase and decrease a triangle wave |
| 1418 | for ( Bit8u i = 0; i < TREMOLO_TABLE / 2; i++ ) { |
| 1419 | Bit8u val = i << ENV_EXTRA; |
| 1420 | TremoloTable[i] = val; |
| 1421 | TremoloTable[TREMOLO_TABLE - 1 - i] = val; |
| 1422 | } |
| 1423 | //Create a table with offsets of the channels from the start of the chip |
| 1424 | DBOPL::Chip* chip = 0; |
| 1425 | for ( Bitu i = 0; i < 32; i++ ) { |
| 1426 | Bitu index = i & 0xf; |
| 1427 | if ( index >= 9 ) { |
| 1428 | ChanOffsetTable[i] = 0; |
| 1429 | continue; |
| 1430 | } |
| 1431 | //Make sure the four op channels follow eachother |
| 1432 | if ( index < 6 ) { |
| 1433 | index = (index % 3) * 2 + ( index / 3 ); |
| 1434 | } |
| 1435 | //Add back the bits for highest ones |
| 1436 | if ( i >= 16 ) |
| 1437 | index += 9; |
| 1438 | Bitu blah = reinterpret_cast<Bitu>( &(chip->chan[ index ]) ); |
| 1439 | ChanOffsetTable[i] = blah; |
| 1440 | } |
| 1441 | //Same for operators |
| 1442 | for ( Bitu i = 0; i < 64; i++ ) { |
| 1443 | if ( i % 8 >= 6 || ( (i / 8) % 4 == 3 ) ) { |
| 1444 | OpOffsetTable[i] = 0; |
| 1445 | continue; |
| 1446 | } |
| 1447 | Bitu chNum = (i / 8) * 3 + (i % 8) % 3; |
| 1448 | //Make sure we use 16 and up for the 2nd range to match the chanoffset gap |
| 1449 | if ( chNum >= 12 ) |
| 1450 | chNum += 16 - 12; |
| 1451 | Bitu opNum = ( i % 8 ) / 3; |
| 1452 | DBOPL::Channel* chan = 0; |
| 1453 | Bitu blah = reinterpret_cast<Bitu>( &(chan->op[opNum]) ); |
| 1454 | OpOffsetTable[i] = ChanOffsetTable[ chNum ] + blah; |
| 1455 | } |
| 1456 | #if 0 |
| 1457 | //Stupid checks if table's are correct |
| 1458 | for ( Bitu i = 0; i < 18; i++ ) { |
| 1459 | Bit32u find = (Bit16u)( &(chip->chan[ i ]) ); |
| 1460 | for ( Bitu c = 0; c < 32; c++ ) { |
| 1461 | if ( ChanOffsetTable[c] == find ) { |
| 1462 | find = 0; |
| 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | if ( find ) { |
| 1467 | find = find; |
| 1468 | } |
| 1469 | } |
| 1470 | for ( Bitu i = 0; i < 36; i++ ) { |
| 1471 | Bit32u find = (Bit16u)( &(chip->chan[ i / 2 ].op[i % 2]) ); |
| 1472 | for ( Bitu c = 0; c < 64; c++ ) { |
| 1473 | if ( OpOffsetTable[c] == find ) { |
| 1474 | find = 0; |
| 1475 | break; |
| 1476 | } |
| 1477 | } |
| 1478 | if ( find ) { |
| 1479 | find = find; |
| 1480 | } |
| 1481 | } |
| 1482 | #endif |
| 1483 | } |
| 1484 | |
| 1485 | Bit32u Handler::WriteAddr( Bit32u port, Bit8u val ) { |
| 1486 | return chip.WriteAddr( port, val ); |
| 1487 | |
| 1488 | } |
| 1489 | void Handler::WriteReg( Bit32u addr, Bit8u val ) { |
| 1490 | chip.WriteReg( addr, val ); |
| 1491 | } |
| 1492 | |
| 1493 | void Handler::Generate( MixerChannel* chan, Bitu samples ) { |
| 1494 | Bit32s buffer[ 512 * 2 ]; |
| 1495 | if ( GCC_UNLIKELY(samples > 512) ) |
| 1496 | samples = 512; |
| 1497 | if ( !chip.opl3Active ) { |
| 1498 | chip.GenerateBlock2( samples, buffer ); |
| 1499 | chan->AddSamples_m32( samples, buffer ); |
| 1500 | } else { |
| 1501 | chip.GenerateBlock3( samples, buffer ); |
| 1502 | chan->AddSamples_s32( samples, buffer ); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | void Handler::Init( Bitu rate ) { |
| 1507 | InitTables(); |
| 1508 | chip.Setup( rate ); |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | }; //Namespace DBOPL |