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