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