Debian Codegraph @

src/hardware/dbopl.h

raw symbols SCIP
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 #include "adlib.h"
20 #include "dosbox.h"
21
22 //Use 8 handlers based on a small logatirmic wavetabe and an exponential table for volume
23 #define WAVE_HANDLER 10
24 //Use a logarithmic wavetable with an exponential table for volume
25 #define WAVE_TABLELOG 11
26 //Use a linear wavetable with a multiply table for volume
27 #define WAVE_TABLEMUL 12
28
29 //Select the type of wave generator routine
30 #define DBOPL_WAVE WAVE_TABLEMUL
31
32 namespace DBOPL {
33
34 struct Chip;
35 struct Operator;
36 struct Channel;
37
38 #if (DBOPL_WAVE == WAVE_HANDLER)
39 typedef Bits ( DB_FASTCALL *WaveHandler) ( Bitu i, Bitu volume );
40 #endif
41
42 typedef Bits ( DBOPL::Operator::*VolumeHandler) ( );
43 typedef Channel* ( DBOPL::Channel::*SynthHandler) ( Chip* chip, Bit32u samples, Bit32s* output );
44
45 //Different synth modes that can generate blocks of data
46 typedef enum {
47 sm2AM,
48 sm2FM,
49 sm3AM,
50 sm3FM,
51 sm4Start,
52 sm3FMFM,
53 sm3AMFM,
54 sm3FMAM,
55 sm3AMAM,
56 sm6Start,
57 sm2Percussion,
58 sm3Percussion,
59 } SynthMode;
60
61 //Shifts for the values contained in chandata variable
62 enum {
63 SHIFT_KSLBASE = 16,
64 SHIFT_KEYCODE = 24,
65 };
66
67 struct Operator {
68 public:
69 //Masks for operator 20 values
70 enum {
71 MASK_KSR = 0x10,
72 MASK_SUSTAIN = 0x20,
73 MASK_VIBRATO = 0x40,
74 MASK_TREMOLO = 0x80,
75 };
76
77 typedef enum {
78 OFF,
79 RELEASE,
80 SUSTAIN,
81 DECAY,
82 ATTACK,
83 } State;
84
85 VolumeHandler volHandler;
86
87 #if (DBOPL_WAVE == WAVE_HANDLER)
88 WaveHandler waveHandler; //Routine that generate a wave
89 #else
90 Bit16s* waveBase;
91 Bit32u waveMask;
92 Bit32u waveStart;
93 #endif
94 Bit32u waveIndex; //WAVE_BITS shifted counter of the frequency index
95 Bit32u waveAdd; //The base frequency without vibrato
96 Bit32u waveCurrent; //waveAdd + vibratao
97
98 Bit32u chanData; //Frequency/octave and derived data coming from whatever channel controls this
99 Bit32u freqMul; //Scale channel frequency with this, TODO maybe remove?
100 Bit32u vibrato; //Scaled up vibrato strength
101 Bit32s sustainLevel; //When stopping at sustain level stop here
102 Bit32s totalLevel; //totalLevel is added to every generated volume
103 Bit32u currentLevel; //totalLevel + tremolo
104 Bit32s volume; //The currently active volume
105
106 Bit32u attackAdd; //Timers for the different states of the envelope
107 Bit32u decayAdd;
108 Bit32u releaseAdd;
109 Bit32u rateIndex; //Current position of the evenlope
110
111 Bit8u rateZero; //Bits for the different states of the envelope having no changes
112 Bit8u keyOn; //Bitmask of different values that can generate keyon
113 //Registers, also used to check for changes
114 Bit8u reg20, reg40, reg60, reg80, regE0;
115 //Active part of the envelope we're in
116 Bit8u state;
117 //0xff when tremolo is enabled
118 Bit8u tremoloMask;
119 //Strength of the vibrato
120 Bit8u vibStrength;
121 //Keep track of the calculated KSR so we can check for changes
122 Bit8u ksr;
123 private:
124 void SetState( Bit8u s );
125 void UpdateAttack( const Chip* chip );
126 void UpdateRelease( const Chip* chip );
127 void UpdateDecay( const Chip* chip );
128 public:
129 void UpdateAttenuation();
130 void UpdateRates( const Chip* chip );
131 void UpdateFrequency( );
132
133 void Write20( const Chip* chip, Bit8u val );
134 void Write40( const Chip* chip, Bit8u val );
135 void Write60( const Chip* chip, Bit8u val );
136 void Write80( const Chip* chip, Bit8u val );
137 void WriteE0( const Chip* chip, Bit8u val );
138
139 bool Silent() const;
140 void Prepare( const Chip* chip );
141
142 void KeyOn( Bit8u mask);
143 void KeyOff( Bit8u mask);
144
145 template< State state>
146 Bits TemplateVolume( );
147
148 Bit32s RateForward( Bit32u add );
149 Bitu ForwardWave();
150 Bitu ForwardVolume();
151
152 Bits GetSample( Bits modulation );
153 Bits GetWave( Bitu index, Bitu vol );
154 public:
155 Operator();
156 };
157
158 struct Channel {
159 Operator op[2];
160 inline Operator* Op( Bitu index ) {
161 return &( ( this + (index >> 1) )->op[ index & 1 ]);
162 }
163 SynthHandler synthHandler;
164 Bit32u chanData; //Frequency/octave and derived values
165 Bit32s old[2]; //Old data for feedback
166
167 Bit8u feedback; //Feedback shift
168 Bit8u regB0; //Register values to check for changes
169 Bit8u regC0;
170 //This should correspond with reg104, bit 6 indicates a Percussion channel, bit 7 indicates a silent channel
171 Bit8u fourMask;
172 Bit8s maskLeft; //Sign extended values for both channel's panning
173 Bit8s maskRight;
174
175 //Forward the channel data to the operators of the channel
176 void SetChanData( const Chip* chip, Bit32u data );
177 //Change in the chandata, check for new values and if we have to forward to operators
178 void UpdateFrequency( const Chip* chip, Bit8u fourOp );
179 void WriteA0( const Chip* chip, Bit8u val );
180 void WriteB0( const Chip* chip, Bit8u val );
181 void WriteC0( const Chip* chip, Bit8u val );
182 void ResetC0( const Chip* chip );
183
184 //call this for the first channel
185 template< bool opl3Mode >
186 void GeneratePercussion( Chip* chip, Bit32s* output );
187
188 //Generate blocks of data in specific modes
189 template<SynthMode mode>
190 Channel* BlockTemplate( Chip* chip, Bit32u samples, Bit32s* output );
191 Channel();
192 };
193
194 struct Chip {
195 //This is used as the base counter for vibrato and tremolo
196 Bit32u lfoCounter;
197 Bit32u lfoAdd;
198
199
200 Bit32u noiseCounter;
201 Bit32u noiseAdd;
202 Bit32u noiseValue;
203
204 //Frequency scales for the different multiplications
205 Bit32u freqMul[16];
206 //Rates for decay and release for rate of this chip
207 Bit32u linearRates[76];
208 //Best match attack rates for the rate of this chip
209 Bit32u attackRates[76];
210
211 //18 channels with 2 operators each
212 Channel chan[18];
213
214 Bit8u reg104;
215 Bit8u reg08;
216 Bit8u reg04;
217 Bit8u regBD;
218 Bit8u vibratoIndex;
219 Bit8u tremoloIndex;
220 Bit8s vibratoSign;
221 Bit8u vibratoShift;
222 Bit8u tremoloValue;
223 Bit8u vibratoStrength;
224 Bit8u tremoloStrength;
225 //Mask for allowed wave forms
226 Bit8u waveFormMask;
227 //0 or -1 when enabled
228 Bit8s opl3Active;
229
230 //Return the maximum amount of samples before and LFO change
231 Bit32u ForwardLFO( Bit32u samples );
232 Bit32u ForwardNoise();
233
234 void WriteBD( Bit8u val );
235 void WriteReg(Bit32u reg, Bit8u val );
236
237 Bit32u WriteAddr( Bit32u port, Bit8u val );
238
239 void GenerateBlock2( Bitu samples, Bit32s* output );
240 void GenerateBlock3( Bitu samples, Bit32s* output );
241
242 void Generate( Bit32u samples );
243 void Setup( Bit32u r );
244
245 Chip();
246 };
247
248 struct Handler : public Adlib::Handler {
249 DBOPL::Chip chip;
250 virtual Bit32u WriteAddr( Bit32u port, Bit8u val );
251 virtual void WriteReg( Bit32u addr, Bit8u val );
252 virtual void Generate( MixerChannel* chan, Bitu samples );
253 virtual void Init( Bitu rate );
254 };
255
256
257 }; //Namespace