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