-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbopl.cpp
1562 lines (1443 loc) · 41.8 KB
/
dbopl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef __MINGW32__
typedef struct vswprintf {} swprintf;
#endif
/*
* Copyright (C) 2002-2010 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
DOSBox implementation of a combined Yamaha YMF262 and Yamaha YM3812 emulator.
Enabling the opl3 bit will switch the emulator to stereo opl3 output instead of regular mono opl2
Except for the table generation it's all integer math
Can choose different types of generators, using muls and bigger tables, try different ones for slower platforms
The generation was based on the MAME implementation but tried to have it use less memory and be faster in general
MAME uses much bigger envelope tables and this will be the biggest cause of it sounding different at times
//TODO Don't delay first operator 1 sample in opl3 mode
//TODO Maybe not use class method pointers but a regular function pointers with operator as first parameter
//TODO Fix panning for the Percussion channels, would any opl3 player use it and actually really change it though?
//TODO Check if having the same accuracy in all frequency multipliers sounds better or not
//DUNNO Keyon in 4op, switch to 2op without keyoff.
*/
/* $Id: dbopl.cpp,v 1.10 2009-06-10 19:54:51 harekiet Exp $ */
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "dbopl.h"
#ifndef PI
#define PI 3.14159265358979323846
#endif
namespace DBOPL {
#define OPLRATE ((double)(14318180.0 / 288.0))
#define TREMOLO_TABLE 52
//Try to use most precision for frequencies
//Else try to keep different waves in synch
//#define WAVE_PRECISION 1
#ifndef WAVE_PRECISION
//Wave bits available in the top of the 32bit range
//Original adlib uses 10.10, we use 10.22
#define WAVE_BITS 10
#else
//Need some extra bits at the top to have room for octaves and frequency multiplier
//We support to 8 times lower rate
//128 * 15 * 8 = 15350, 2^13.9, so need 14 bits
#define WAVE_BITS 14
#endif
#define WAVE_SH ( 32 - WAVE_BITS )
#define WAVE_MASK ( ( 1 << WAVE_SH ) - 1 )
//Use the same accuracy as the waves
#define LFO_SH ( WAVE_SH - 10 )
//LFO is controlled by our tremolo 256 sample limit
#define LFO_MAX ( 256 << ( LFO_SH ) )
//Maximum amount of attenuation bits
//Envelope goes to 511, 9 bits
#if (DBOPL_WAVE == WAVE_TABLEMUL )
//Uses the value directly
#define ENV_BITS ( 9 )
#else
//Add 3 bits here for more accuracy and would have to be shifted up either way
#define ENV_BITS ( 9 )
#endif
//Limits of the envelope with those bits and when the envelope goes silent
#define ENV_MIN 0
#define ENV_EXTRA ( ENV_BITS - 9 )
#define ENV_MAX ( 511 << ENV_EXTRA )
#define ENV_LIMIT ( ( 12 * 256) >> ( 3 - ENV_EXTRA ) )
#define ENV_SILENT( _X_ ) ( (_X_) >= ENV_LIMIT )
//Attack/decay/release rate counter shift
#define RATE_SH 24
#define RATE_MASK ( ( 1 << RATE_SH ) - 1 )
//Has to fit within 16bit lookuptable
#define MUL_SH 16
//Check some ranges
#if ENV_EXTRA > 3
#error Too many envelope bits
#endif
//How much to substract from the base value for the final attenuation
static const Bit8u KslCreateTable[16] = {
//0 will always be be lower than 7 * 8
64, 32, 24, 19,
16, 12, 11, 10,
8, 6, 5, 4,
3, 2, 1, 0,
};
#define M(_X_) ((Bit8u)( (_X_) * 2))
static const Bit8u FreqCreateTable[16] = {
M(0.5), M(1 ), M(2 ), M(3 ), M(4 ), M(5 ), M(6 ), M(7 ),
M(8 ), M(9 ), M(10), M(10), M(12), M(12), M(15), M(15)
};
#undef M
//We're not including the highest attack rate, that gets a special value
static const Bit8u AttackSamplesTable[13] = {
69, 55, 46, 40,
35, 29, 23, 20,
19, 15, 11, 10,
9
};
//On a real opl these values take 8 samples to reach and are based upon larger tables
static const Bit8u EnvelopeIncreaseTable[13] = {
4, 5, 6, 7,
8, 10, 12, 14,
16, 20, 24, 28,
32,
};
#if ( DBOPL_WAVE == WAVE_HANDLER ) || ( DBOPL_WAVE == WAVE_TABLELOG )
static Bit16u ExpTable[ 256 ];
#endif
#if ( DBOPL_WAVE == WAVE_HANDLER )
//PI table used by WAVEHANDLER
static Bit16u SinTable[ 512 ];
#endif
#if ( DBOPL_WAVE > WAVE_HANDLER )
//Layout of the waveform table in 512 entry intervals
//With overlapping waves we reduce the table to half it's size
// | |//\\|____|WAV7|//__|/\ |____|/\/\|
// |\\//| | |WAV7| | \/| | |
// |06 |0126|17 |7 |3 |4 |4 5 |5 |
//6 is just 0 shifted and masked
static Bit16s WaveTable[ 8 * 512 ];
//Distance into WaveTable the wave starts
static const Bit16u WaveBaseTable[8] = {
0x000, 0x200, 0x200, 0x800,
0xa00, 0xc00, 0x100, 0x400,
};
//Mask the counter with this
static const Bit16u WaveMaskTable[8] = {
1023, 1023, 511, 511,
1023, 1023, 512, 1023,
};
//Where to start the counter on at keyon
static const Bit16u WaveStartTable[8] = {
512, 0, 0, 0,
0, 512, 512, 256,
};
#endif
#if ( DBOPL_WAVE == WAVE_TABLEMUL )
static Bit16u MulTable[ 384 ];
#endif
static Bit8u KslTable[ 8 * 16 ];
static Bit8u TremoloTable[ TREMOLO_TABLE ];
//Start of a channel behind the chip struct start
static Bit16u ChanOffsetTable[32];
//Start of an operator behind the chip struct start
static Bit16u OpOffsetTable[64];
//The lower bits are the shift of the operator vibrato value
//The highest bit is right shifted to generate -1 or 0 for negation
//So taking the highest input value of 7 this gives 3, 7, 3, 0, -3, -7, -3, 0
static const Bit8s VibratoTable[ 8 ] = {
1 - 0x00, 0 - 0x00, 1 - 0x00, 30 - 0x00,
1 - 0x80, 0 - 0x80, 1 - 0x80, 30 - 0x80
};
//Shift strength for the ksl value determined by ksl strength
static const Bit8u KslShiftTable[4] = {
31,1,2,0
};
//Generate a table index and table shift value using input value from a selected rate
static void EnvelopeSelect( Bit8u val, Bit8u& index, Bit8u& shift ) {
if ( val < 13 * 4 ) { //Rate 0 - 12
shift = 12 - ( val >> 2 );
index = val & 3;
} else if ( val < 15 * 4 ) { //rate 13 - 14
shift = 0;
index = val - 12 * 4;
} else { //rate 15 and up
shift = 0;
index = 12;
}
}
#if ( DBOPL_WAVE == WAVE_HANDLER )
/*
Generate the different waveforms out of the sine/exponetial table using handlers
*/
static inline Bits MakeVolume( Bitu wave, Bitu volume ) {
Bitu total = wave + volume;
Bitu index = total & 0xff;
Bitu sig = ExpTable[ index ];
Bitu exp = total >> 8;
#if 0
//Check if we overflow the 31 shift limit
if ( exp >= 32 ) {
LOG_MSG( "WTF %d %d", total, exp );
}
#endif
return (sig >> exp);
};
static Bits DB_FASTCALL WaveForm0( Bitu i, Bitu volume ) {
Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0
Bitu wave = SinTable[i & 511];
return (MakeVolume( wave, volume ) ^ neg) - neg;
}
static Bits DB_FASTCALL WaveForm1( Bitu i, Bitu volume ) {
Bit32u wave = SinTable[i & 511];
wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 );
return MakeVolume( wave, volume );
}
static Bits DB_FASTCALL WaveForm2( Bitu i, Bitu volume ) {
Bitu wave = SinTable[i & 511];
return MakeVolume( wave, volume );
}
static Bits DB_FASTCALL WaveForm3( Bitu i, Bitu volume ) {
Bitu wave = SinTable[i & 255];
wave |= ( ( (i ^ 256 ) & 256) - 1) >> ( 32 - 12 );
return MakeVolume( wave, volume );
}
static Bits DB_FASTCALL WaveForm4( Bitu i, Bitu volume ) {
//Twice as fast
i <<= 1;
Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0
Bitu wave = SinTable[i & 511];
wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 );
return (MakeVolume( wave, volume ) ^ neg) - neg;
}
static Bits DB_FASTCALL WaveForm5( Bitu i, Bitu volume ) {
//Twice as fast
i <<= 1;
Bitu wave = SinTable[i & 511];
wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 );
return MakeVolume( wave, volume );
}
static Bits DB_FASTCALL WaveForm6( Bitu i, Bitu volume ) {
Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0
return (MakeVolume( 0, volume ) ^ neg) - neg;
}
static Bits DB_FASTCALL WaveForm7( Bitu i, Bitu volume ) {
//Negative is reversed here
Bits neg = (( i >> 9) & 1) - 1;
Bitu wave = (i << 3);
//When negative the volume also runs backwards
wave = ((wave ^ neg) - neg) & 4095;
return (MakeVolume( wave, volume ) ^ neg) - neg;
}
static const WaveHandler WaveHandlerTable[8] = {
WaveForm0, WaveForm1, WaveForm2, WaveForm3,
WaveForm4, WaveForm5, WaveForm6, WaveForm7
};
#endif
/*
Operator
*/
//We zero out when rate == 0
inline void Operator::UpdateAttack( const Chip* chip ) {
Bit8u rate = reg60 >> 4;
if ( rate ) {
Bit8u val = (rate << 2) + ksr;
attackAdd = chip->attackRates[ val ];
rateZero &= ~(1 << ATTACK);
} else {
attackAdd = 0;
rateZero |= (1 << ATTACK);
}
}
inline void Operator::UpdateDecay( const Chip* chip ) {
Bit8u rate = reg60 & 0xf;
if ( rate ) {
Bit8u val = (rate << 2) + ksr;
decayAdd = chip->linearRates[ val ];
rateZero &= ~(1 << DECAY);
} else {
decayAdd = 0;
rateZero |= (1 << DECAY);
}
}
inline void Operator::UpdateRelease( const Chip* chip ) {
Bit8u rate = reg80 & 0xf;
if ( rate ) {
Bit8u val = (rate << 2) + ksr;
releaseAdd = chip->linearRates[ val ];
rateZero &= ~(1 << RELEASE);
if ( !(reg20 & MASK_SUSTAIN ) ) {
rateZero &= ~( 1 << SUSTAIN );
}
} else {
rateZero |= (1 << RELEASE);
releaseAdd = 0;
if ( !(reg20 & MASK_SUSTAIN ) ) {
rateZero |= ( 1 << SUSTAIN );
}
}
}
inline void Operator::UpdateAttenuation( ) {
Bit8u kslBase = (Bit8u)((chanData >> SHIFT_KSLBASE) & 0xff);
Bit32u tl = reg40 & 0x3f;
Bit8u kslShift = KslShiftTable[ reg40 >> 6 ];
//Make sure the attenuation goes to the right bits
totalLevel = tl << ( ENV_BITS - 7 ); //Total level goes 2 bits below max
totalLevel += ( kslBase << ENV_EXTRA ) >> kslShift;
}
void Operator::UpdateFrequency( ) {
Bit32u freq = chanData & (( 1 << 10 ) - 1);
Bit32u block = (chanData >> 10) & 0xff;
#ifdef WAVE_PRECISION
block = 7 - block;
waveAdd = ( freq * freqMul ) >> block;
#else
waveAdd = ( freq << block ) * freqMul;
#endif
if ( reg20 & MASK_VIBRATO ) {
vibStrength = (Bit8u)(freq >> 7);
#ifdef WAVE_PRECISION
vibrato = ( vibStrength * freqMul ) >> block;
#else
vibrato = ( vibStrength << block ) * freqMul;
#endif
} else {
vibStrength = 0;
vibrato = 0;
}
}
void Operator::UpdateRates( const Chip* chip ) {
//Mame seems to reverse this where enabling ksr actually lowers
//the rate, but pdf manuals says otherwise?
Bit8u newKsr = (Bit8u)((chanData >> SHIFT_KEYCODE) & 0xff);
if ( !( reg20 & MASK_KSR ) ) {
newKsr >>= 2;
}
if ( ksr == newKsr )
return;
ksr = newKsr;
UpdateAttack( chip );
UpdateDecay( chip );
UpdateRelease( chip );
}
INLINE Bit32s Operator::RateForward( Bit32u add ) {
rateIndex += add;
Bit32s ret = rateIndex >> RATE_SH;
rateIndex = rateIndex & RATE_MASK;
return ret;
}
template< Operator::State yes>
Bits Operator::TemplateVolume( ) {
Bit32s vol = volume;
Bit32s change;
switch ( yes ) {
case OFF:
return ENV_MAX;
case ATTACK:
change = RateForward( attackAdd );
if ( !change )
return vol;
vol += ( (~vol) * change ) >> 3;
if ( vol < ENV_MIN ) {
volume = ENV_MIN;
rateIndex = 0;
SetState( DECAY );
return ENV_MIN;
}
break;
case DECAY:
vol += RateForward( decayAdd );
if ( GCC_UNLIKELY(vol >= sustainLevel) ) {
//Check if we didn't overshoot max attenuation, then just go off
if ( GCC_UNLIKELY(vol >= ENV_MAX) ) {
volume = ENV_MAX;
SetState( OFF );
return ENV_MAX;
}
//Continue as sustain
rateIndex = 0;
SetState( SUSTAIN );
}
break;
case SUSTAIN:
if ( reg20 & MASK_SUSTAIN ) {
return vol;
}
//In sustain phase, but not sustaining, do regular release
case RELEASE:
vol += RateForward( releaseAdd );;
if ( GCC_UNLIKELY(vol >= ENV_MAX) ) {
volume = ENV_MAX;
SetState( OFF );
return ENV_MAX;
}
break;
}
volume = vol;
return vol;
}
static const VolumeHandler VolumeHandlerTable[5] = {
&Operator::TemplateVolume< Operator::OFF >,
&Operator::TemplateVolume< Operator::RELEASE >,
&Operator::TemplateVolume< Operator::SUSTAIN >,
&Operator::TemplateVolume< Operator::DECAY >,
&Operator::TemplateVolume< Operator::ATTACK >
};
INLINE Bitu Operator::ForwardVolume() {
return currentLevel + (this->*volHandler)();
}
INLINE Bitu Operator::ForwardWave() {
waveIndex += waveCurrent;
return waveIndex >> WAVE_SH;
}
void Operator::Write20( const Chip* chip, Bit8u val ) {
Bit8u change = (reg20 ^ val );
if ( !change )
return;
reg20 = val;
//Shift the tremolo bit over the entire register, saved a branch, YES!
tremoloMask = (Bit8s)(val) >> 7;
tremoloMask &= ~(( 1 << ENV_EXTRA ) -1);
//Update specific features based on changes
if ( change & MASK_KSR ) {
UpdateRates( chip );
}
//With sustain enable the volume doesn't change
if ( reg20 & MASK_SUSTAIN || ( !releaseAdd ) ) {
rateZero |= ( 1 << SUSTAIN );
} else {
rateZero &= ~( 1 << SUSTAIN );
}
//Frequency multiplier or vibrato changed
if ( change & (0xf | MASK_VIBRATO) ) {
freqMul = chip->freqMul[ val & 0xf ];
UpdateFrequency();
}
}
void Operator::Write40( const Chip* /*chip*/, Bit8u val ) {
if (!(reg40 ^ val ))
return;
reg40 = val;
UpdateAttenuation( );
}
void Operator::Write60( const Chip* chip, Bit8u val ) {
Bit8u change = reg60 ^ val;
reg60 = val;
if ( change & 0x0f ) {
UpdateDecay( chip );
}
if ( change & 0xf0 ) {
UpdateAttack( chip );
}
}
void Operator::Write80( const Chip* chip, Bit8u val ) {
Bit8u change = (reg80 ^ val );
if ( !change )
return;
reg80 = val;
Bit8u sustain = val >> 4;
//Turn 0xf into 0x1f
sustain |= ( sustain + 1) & 0x10;
sustainLevel = sustain << ( ENV_BITS - 5 );
if ( change & 0x0f ) {
UpdateRelease( chip );
}
}
void Operator::WriteE0( const Chip* chip, Bit8u val ) {
if ( !(regE0 ^ val) )
return;
//in opl3 mode you can always selet 7 waveforms regardless of waveformselect
Bit8u waveForm = val & ( ( 0x3 & chip->waveFormMask ) | (0x7 & chip->opl3Active ) );
regE0 = val;
#if ( DBOPL_WAVE == WAVE_HANDLER )
waveHandler = WaveHandlerTable[ waveForm ];
#else
waveBase = WaveTable + WaveBaseTable[ waveForm ];
waveStart = WaveStartTable[ waveForm ] << WAVE_SH;
waveMask = WaveMaskTable[ waveForm ];
#endif
}
INLINE void Operator::SetState( Bit8u s ) {
state = s;
volHandler = VolumeHandlerTable[ s ];
}
INLINE bool Operator::Silent() const {
if ( !ENV_SILENT( totalLevel + volume ) )
return false;
if ( !(rateZero & ( 1 << state ) ) )
return false;
return true;
}
INLINE void Operator::Prepare( const Chip* chip ) {
currentLevel = totalLevel + (chip->tremoloValue & tremoloMask);
waveCurrent = waveAdd;
if ( vibStrength >> chip->vibratoShift ) {
Bit32s add = vibrato >> chip->vibratoShift;
//Sign extend over the shift value
Bit32s neg = chip->vibratoSign;
//Negate the add with -1 or 0
add = ( add ^ neg ) - neg;
waveCurrent += add;
}
}
void Operator::KeyOn( Bit8u mask ) {
if ( !keyOn ) {
//Restart the frequency generator
#if ( DBOPL_WAVE > WAVE_HANDLER )
waveIndex = waveStart;
#else
waveIndex = 0;
#endif
rateIndex = 0;
SetState( ATTACK );
}
keyOn |= mask;
}
void Operator::KeyOff( Bit8u mask ) {
keyOn &= ~mask;
if ( !keyOn ) {
if ( state != OFF ) {
SetState( RELEASE );
}
}
}
INLINE Bits Operator::GetWave( Bitu index, Bitu vol ) {
#if ( DBOPL_WAVE == WAVE_HANDLER )
return waveHandler( index, vol << ( 3 - ENV_EXTRA ) );
#elif ( DBOPL_WAVE == WAVE_TABLEMUL )
return (waveBase[ index & waveMask ] * MulTable[ vol >> ENV_EXTRA ]) >> MUL_SH;
#elif ( DBOPL_WAVE == WAVE_TABLELOG )
Bit32s wave = waveBase[ index & waveMask ];
Bit32u total = ( wave & 0x7fff ) + vol << ( 3 - ENV_EXTRA );
Bit32s sig = ExpTable[ total & 0xff ];
Bit32u exp = total >> 8;
Bit32s neg = wave >> 16;
return ((sig ^ neg) - neg) >> exp;
#else
#error "No valid wave routine"
#endif
}
Bits INLINE Operator::GetSample( Bits modulation ) {
Bitu vol = ForwardVolume();
if ( ENV_SILENT( vol ) ) {
//Simply forward the wave
waveIndex += waveCurrent;
return 0;
} else {
Bitu index = ForwardWave();
index += modulation;
return GetWave( index, vol );
}
}
Operator::Operator() {
chanData = 0;
freqMul = 0;
waveIndex = 0;
waveAdd = 0;
waveCurrent = 0;
keyOn = 0;
ksr = 0;
reg20 = 0;
reg40 = 0;
reg60 = 0;
reg80 = 0;
regE0 = 0;
SetState( OFF );
rateZero = (1 << OFF);
sustainLevel = ENV_MAX;
currentLevel = ENV_MAX;
totalLevel = ENV_MAX;
volume = ENV_MAX;
releaseAdd = 0;
}
/*
Channel
*/
Channel::Channel() {
old[0] = old[1] = 0;
chanData = 0;
regB0 = 0;
regC0 = 0;
maskLeft = -1;
maskRight = -1;
feedback = 31;
fourMask = 0;
synthHandler = &Channel::BlockTemplate< sm2FM >;
};
void Channel::SetChanData( const Chip* chip, Bit32u data ) {
Bit32u change = chanData ^ data;
chanData = data;
Op( 0 )->chanData = data;
Op( 1 )->chanData = data;
//Since a frequency update triggered this, always update frequency
Op( 0 )->UpdateFrequency();
Op( 1 )->UpdateFrequency();
if ( change & ( 0xff << SHIFT_KSLBASE ) ) {
Op( 0 )->UpdateAttenuation();
Op( 1 )->UpdateAttenuation();
}
if ( change & ( 0xff << SHIFT_KEYCODE ) ) {
Op( 0 )->UpdateRates( chip );
Op( 1 )->UpdateRates( chip );
}
}
void Channel::UpdateFrequency( const Chip* chip, Bit8u fourOp ) {
//Extrace the frequency bits
Bit32u data = chanData & 0xffff;
Bit32u kslBase = KslTable[ data >> 6 ];
Bit32u keyCode = ( data & 0x1c00) >> 9;
if ( chip->reg08 & 0x40 ) {
keyCode |= ( data & 0x100)>>8; /* notesel == 1 */
} else {
keyCode |= ( data & 0x200)>>9; /* notesel == 0 */
}
//Add the keycode and ksl into the highest bits of chanData
data |= (keyCode << SHIFT_KEYCODE) | ( kslBase << SHIFT_KSLBASE );
( this + 0 )->SetChanData( chip, data );
if ( fourOp & 0x3f ) {
( this + 1 )->SetChanData( chip, data );
}
}
void Channel::WriteA0( const Chip* chip, Bit8u val ) {
Bit8u fourOp = chip->reg104 & chip->opl3Active & fourMask;
//Don't handle writes to silent fourop channels
if ( fourOp > 0x80 )
return;
Bit32u change = (chanData ^ val ) & 0xff;
if ( change ) {
chanData ^= change;
UpdateFrequency( chip, fourOp );
}
}
void Channel::WriteB0( const Chip* chip, Bit8u val ) {
Bit8u fourOp = chip->reg104 & chip->opl3Active & fourMask;
//Don't handle writes to silent fourop channels
if ( fourOp > 0x80 )
return;
Bitu change = (chanData ^ ( val << 8 ) ) & 0x1f00;
if ( change ) {
chanData ^= change;
UpdateFrequency( chip, fourOp );
}
//Check for a change in the keyon/off state
if ( !(( val ^ regB0) & 0x20))
return;
regB0 = val;
if ( val & 0x20 ) {
Op(0)->KeyOn( 0x1 );
Op(1)->KeyOn( 0x1 );
if ( fourOp & 0x3f ) {
( this + 1 )->Op(0)->KeyOn( 1 );
( this + 1 )->Op(1)->KeyOn( 1 );
}
} else {
Op(0)->KeyOff( 0x1 );
Op(1)->KeyOff( 0x1 );
if ( fourOp & 0x3f ) {
( this + 1 )->Op(0)->KeyOff( 1 );
( this + 1 )->Op(1)->KeyOff( 1 );
}
}
}
void Channel::WriteC0( const Chip* chip, Bit8u val ) {
Bit8u change = val ^ regC0;
if ( !change )
return;
regC0 = val;
feedback = ( val >> 1 ) & 7;
if ( feedback ) {
//We shift the input to the right 10 bit wave index value
feedback = 9 - feedback;
} else {
feedback = 31;
}
//Select the new synth mode
if ( chip->opl3Active ) {
//4-op mode enabled for this channel
if ( (chip->reg104 & fourMask) & 0x3f ) {
Channel* chan0, *chan1;
//Check if it's the 2nd channel in a 4-op
if ( !(fourMask & 0x80 ) ) {
chan0 = this;
chan1 = this + 1;
} else {
chan0 = this - 1;
chan1 = this;
}
Bit8u synth = ( (chan0->regC0 & 1) << 0 )| (( chan1->regC0 & 1) << 1 );
switch ( synth ) {
case 0:
chan0->synthHandler = &Channel::BlockTemplate< sm3FMFM >;
break;
case 1:
chan0->synthHandler = &Channel::BlockTemplate< sm3AMFM >;
break;
case 2:
chan0->synthHandler = &Channel::BlockTemplate< sm3FMAM >;
break;
case 3:
chan0->synthHandler = &Channel::BlockTemplate< sm3AMAM >;
break;
}
//Disable updating percussion channels
} else if ((fourMask & 0x40) && ( chip->regBD & 0x20) ) {
//Regular dual op, am or fm
} else if ( val & 1 ) {
synthHandler = &Channel::BlockTemplate< sm3AM >;
} else {
synthHandler = &Channel::BlockTemplate< sm3FM >;
}
maskLeft = ( val & 0x10 ) ? -1 : 0;
maskRight = ( val & 0x20 ) ? -1 : 0;
//opl2 active
} else {
//Disable updating percussion channels
if ( (fourMask & 0x40) && ( chip->regBD & 0x20 ) ) {
//Regular dual op, am or fm
} else if ( val & 1 ) {
synthHandler = &Channel::BlockTemplate< sm2AM >;
} else {
synthHandler = &Channel::BlockTemplate< sm2FM >;
}
}
}
void Channel::ResetC0( const Chip* chip ) {
Bit8u val = regC0;
regC0 ^= 0xff;
WriteC0( chip, val );
};
template< bool opl3Mode>
INLINE void Channel::GeneratePercussion( Chip* chip, Bit32s* output ) {
Channel* chan = this;
//BassDrum
Bit32s mod = (Bit32u)((old[0] + old[1])) >> feedback;
old[0] = old[1];
old[1] = Op(0)->GetSample( mod );
//When bassdrum is in AM mode first operator is ignoed
if ( chan->regC0 & 1 ) {
mod = 0;
} else {
mod = old[0];
}
Bit32s sample = Op(1)->GetSample( mod );
//Precalculate stuff used by other outputs
Bit32u noiseBit = chip->ForwardNoise() & 0x1;
Bit32u c2 = Op(2)->ForwardWave();
Bit32u c5 = Op(5)->ForwardWave();
Bit32u phaseBit = (((c2 & 0x88) ^ ((c2<<5) & 0x80)) | ((c5 ^ (c5<<2)) & 0x20)) ? 0x02 : 0x00;
//Hi-Hat
Bit32u hhVol = Op(2)->ForwardVolume();
if ( !ENV_SILENT( hhVol ) ) {
Bit32u hhIndex = (phaseBit<<8) | (0x34 << ( phaseBit ^ (noiseBit << 1 )));
sample += Op(2)->GetWave( hhIndex, hhVol );
}
//Snare Drum
Bit32u sdVol = Op(3)->ForwardVolume();
if ( !ENV_SILENT( sdVol ) ) {
Bit32u sdIndex = ( 0x100 + (c2 & 0x100) ) ^ ( noiseBit << 8 );
sample += Op(3)->GetWave( sdIndex, sdVol );
}
//Tom-tom
sample += Op(4)->GetSample( 0 );
//Top-Cymbal
Bit32u tcVol = Op(5)->ForwardVolume();
if ( !ENV_SILENT( tcVol ) ) {
Bit32u tcIndex = (1 + phaseBit) << 8;
sample += Op(5)->GetWave( tcIndex, tcVol );
}
sample <<= 1;
if ( opl3Mode ) {
output[0] += sample;
output[1] += sample;
} else {
output[0] += sample;
}
}
template<SynthMode mode>
Channel* Channel::BlockTemplate( Chip* chip, Bit32u samples, Bit32s* output ) {
switch( mode ) {
case sm2AM:
case sm3AM:
if ( Op(0)->Silent() && Op(1)->Silent() ) {
old[0] = old[1] = 0;
return (this + 1);
}
break;
case sm2FM:
case sm3FM:
if ( Op(1)->Silent() ) {
old[0] = old[1] = 0;
return (this + 1);
}
break;
case sm3FMFM:
if ( Op(3)->Silent() ) {
old[0] = old[1] = 0;
return (this + 2);
}
break;
case sm3AMFM:
if ( Op(0)->Silent() && Op(3)->Silent() ) {
old[0] = old[1] = 0;
return (this + 2);
}
break;
case sm3FMAM:
if ( Op(1)->Silent() && Op(3)->Silent() ) {
old[0] = old[1] = 0;
return (this + 2);
}
break;
case sm3AMAM:
if ( Op(0)->Silent() && Op(2)->Silent() && Op(3)->Silent() ) {
old[0] = old[1] = 0;
return (this + 2);
}
break;
}
//Init the operators with the the current vibrato and tremolo values
Op( 0 )->Prepare( chip );
Op( 1 )->Prepare( chip );
if ( mode > sm4Start ) {
Op( 2 )->Prepare( chip );
Op( 3 )->Prepare( chip );
}
if ( mode > sm6Start ) {
Op( 4 )->Prepare( chip );
Op( 5 )->Prepare( chip );
}
for ( Bitu i = 0; i < samples; i++ ) {
//Early out for percussion handlers
if ( mode == sm2Percussion ) {
GeneratePercussion<false>( chip, output + i );
continue; //Prevent some unitialized value bitching
} else if ( mode == sm3Percussion ) {
GeneratePercussion<true>( chip, output + i * 2 );
continue; //Prevent some unitialized value bitching
}
//Do unsigned shift so we can shift out all bits but still stay in 10 bit range otherwise
Bit32s mod = (Bit32u)((old[0] + old[1])) >> feedback;
old[0] = old[1];
old[1] = Op(0)->GetSample( mod );
Bit32s sample;
Bit32s out0 = old[0];
if ( mode == sm2AM || mode == sm3AM ) {
sample = out0 + Op(1)->GetSample( 0 );
} else if ( mode == sm2FM || mode == sm3FM ) {
sample = Op(1)->GetSample( out0 );
} else if ( mode == sm3FMFM ) {
Bits next = Op(1)->GetSample( out0 );
next = Op(2)->GetSample( next );
sample = Op(3)->GetSample( next );
} else if ( mode == sm3AMFM ) {
sample = out0;
Bits next = Op(1)->GetSample( 0 );
next = Op(2)->GetSample( next );
sample += Op(3)->GetSample( next );
} else if ( mode == sm3FMAM ) {
sample = Op(1)->GetSample( out0 );
Bits next = Op(2)->GetSample( 0 );
sample += Op(3)->GetSample( next );
} else if ( mode == sm3AMAM ) {
sample = out0;
Bits next = Op(1)->GetSample( 0 );
sample += Op(2)->GetSample( next );
sample += Op(3)->GetSample( 0 );
}
switch( mode ) {
case sm2AM:
case sm2FM:
output[ i ] += sample;
break;
case sm3AM:
case sm3FM:
case sm3FMFM:
case sm3AMFM:
case sm3FMAM:
case sm3AMAM:
output[ i * 2 + 0 ] += sample & maskLeft;
output[ i * 2 + 1 ] += sample & maskRight;
break;
}
}
switch( mode ) {
case sm2AM:
case sm2FM:
case sm3AM:
case sm3FM:
return ( this + 1 );
case sm3FMFM:
case sm3AMFM:
case sm3FMAM:
case sm3AMAM:
return( this + 2 );
case sm2Percussion:
case sm3Percussion:
return( this + 3 );
}
return 0;
}
/*
Chip
*/
Chip::Chip() {
reg08 = 0;
reg04 = 0;
regBD = 0;
reg104 = 0;
opl3Active = 0;
}
INLINE Bit32u Chip::ForwardNoise() {
noiseCounter += noiseAdd;
Bitu count = noiseCounter >> LFO_SH;
noiseCounter &= WAVE_MASK;
for ( ; count > 0; --count ) {
//Noise calculation from mame
noiseValue ^= ( 0x800302 ) & ( 0 - (noiseValue & 1 ) );
noiseValue >>= 1;
}
return noiseValue;
}
INLINE Bit32u Chip::ForwardLFO( Bit32u samples ) {
//Current vibrato value, runs 4x slower than tremolo
vibratoSign = ( VibratoTable[ vibratoIndex >> 2] ) >> 7;