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 #ifndef __DBOPL_H__ 20 #define __DBOPL_H__ 21 22 #include <stdint.h> 23 24 #ifdef _MSC_VER 25 #if _MSC_VER < 1300 26 #include <BaseTsd.h> 27 typedef INT_PTR intptr_t; 28 #endif 29 #endif 30 31 //Use 8 handlers based on a small logatirmic wavetabe and an exponential table for volume 32 #define WAVE_HANDLER 10 33 //Use a logarithmic wavetable with an exponential table for volume 34 #define WAVE_TABLELOG 11 35 //Use a linear wavetable with a multiply table for volume 36 #define WAVE_TABLEMUL 12 37 38 //Select the type of wave generator routine 39 #define DBOPL_WAVE WAVE_TABLEMUL 40 41 typedef struct _Chip Chip; 42 typedef struct _Operator Operator; 43 typedef struct _Channel Channel; 44 45 typedef uintptr_t Bitu; 46 typedef intptr_t Bits; 47 typedef uint32_t Bit32u; 48 typedef int32_t Bit32s; 49 typedef uint16_t Bit16u; 50 typedef int16_t Bit16s; 51 typedef uint8_t Bit8u; 52 typedef int8_t Bit8s; 53 54 #if (DBOPL_WAVE == WAVE_HANDLER) 55 typedef Bits ( DB_FASTCALL *WaveHandler) ( Bitu i, Bitu volume ); 56 #endif 57 58 #define DB_FASTCALL 59 60 typedef Bits (*VolumeHandler)(Operator *self); 61 typedef Channel* (*SynthHandler)(Channel *self, Chip* chip, Bit32u samples, Bit32s* output ); 62 63 //Different synth modes that can generate blocks of data 64 typedef enum { 65 sm2AM, 66 sm2FM, 67 sm3AM, 68 sm3FM, 69 sm4Start, 70 sm3FMFM, 71 sm3AMFM, 72 sm3FMAM, 73 sm3AMAM, 74 sm6Start, 75 sm2Percussion, 76 sm3Percussion, 77 } SynthMode; 78 79 //Shifts for the values contained in chandata variable 80 enum { 81 SHIFT_KSLBASE = 16, 82 SHIFT_KEYCODE = 24, 83 }; 84 85 //Masks for operator 20 values 86 enum { 87 MASK_KSR = 0x10, 88 MASK_SUSTAIN = 0x20, 89 MASK_VIBRATO = 0x40, 90 MASK_TREMOLO = 0x80, 91 }; 92 93 typedef enum { 94 OFF, 95 RELEASE, 96 SUSTAIN, 97 DECAY, 98 ATTACK, 99 } OperatorState; 100 101 struct _Operator { 102 VolumeHandler volHandler; 103 104 #if (DBOPL_WAVE == WAVE_HANDLER) 105 WaveHandler waveHandler; //Routine that generate a wave 106 #else 107 Bit16s* waveBase; 108 Bit32u waveMask; 109 Bit32u waveStart; 110 #endif 111 Bit32u waveIndex; //WAVE_BITS shifted counter of the frequency index 112 Bit32u waveAdd; //The base frequency without vibrato 113 Bit32u waveCurrent; //waveAdd + vibratao 114 115 Bit32u chanData; //Frequency/octave and derived data coming from whatever channel controls this 116 Bit32u freqMul; //Scale channel frequency with this, TODO maybe remove? 117 Bit32u vibrato; //Scaled up vibrato strength 118 Bit32s sustainLevel; //When stopping at sustain level stop here 119 Bit32s totalLevel; //totalLevel is added to every generated volume 120 Bit32u currentLevel; //totalLevel + tremolo 121 Bit32s volume; //The currently active volume 122 123 Bit32u attackAdd; //Timers for the different states of the envelope 124 Bit32u decayAdd; 125 Bit32u releaseAdd; 126 Bit32u rateIndex; //Current position of the evenlope 127 128 Bit8u rateZero; //Bits for the different states of the envelope having no changes 129 Bit8u keyOn; //Bitmask of different values that can generate keyon 130 //Registers, also used to check for changes 131 Bit8u reg20, reg40, reg60, reg80, regE0; 132 //Active part of the envelope we're in 133 Bit8u state; 134 //0xff when tremolo is enabled 135 Bit8u tremoloMask; 136 //Strength of the vibrato 137 Bit8u vibStrength; 138 //Keep track of the calculated KSR so we can check for changes 139 Bit8u ksr; 140 }; 141 142 struct _Channel { 143 Operator op[2]; 144 SynthHandler synthHandler; 145 Bit32u chanData; //Frequency/octave and derived values 146 Bit32s old[2]; //Old data for feedback 147 148 Bit8u feedback; //Feedback shift 149 Bit8u regB0; //Register values to check for changes 150 Bit8u regC0; 151 //This should correspond with reg104, bit 6 indicates a Percussion channel, bit 7 indicates a silent channel 152 Bit8u fourMask; 153 Bit8s maskLeft; //Sign extended values for both channel's panning 154 Bit8s maskRight; 155 156 }; 157 158 struct _Chip { 159 //This is used as the base counter for vibrato and tremolo 160 Bit32u lfoCounter; 161 Bit32u lfoAdd; 162 163 164 Bit32u noiseCounter; 165 Bit32u noiseAdd; 166 Bit32u noiseValue; 167 168 //Frequency scales for the different multiplications 169 Bit32u freqMul[16]; 170 //Rates for decay and release for rate of this chip 171 Bit32u linearRates[76]; 172 //Best match attack rates for the rate of this chip 173 Bit32u attackRates[76]; 174 175 //18 channels with 2 operators each 176 Channel chan[18]; 177 178 Bit8u reg104; 179 Bit8u reg08; 180 Bit8u reg04; 181 Bit8u regBD; 182 Bit8u vibratoIndex; 183 Bit8u tremoloIndex; 184 Bit8s vibratoSign; 185 Bit8u vibratoShift; 186 Bit8u tremoloValue; 187 Bit8u vibratoStrength; 188 Bit8u tremoloStrength; 189 //Mask for allowed wave forms 190 Bit8u waveFormMask; 191 //0 or -1 when enabled 192 Bit8s opl3Active; 193 194 }; 195 196 /* 197 struct Handler : public Adlib::Handler { 198 DBOPL::Chip chip; 199 virtual Bit32u WriteAddr( Bit32u port, Bit8u val ); 200 virtual void WriteReg( Bit32u addr, Bit8u val ); 201 virtual void Generate( MixerChannel* chan, Bitu samples ); 202 virtual void Init( Bitu rate ); 203 }; 204 */ 205 206 #ifdef __cplusplus 207 extern "C" void Chip__Setup(Chip *self, Bit32u rate ); 208 extern "C" void DBOPL_InitTables( void ); 209 extern "C" void Chip__Chip(Chip *self); 210 extern "C" void Chip__WriteReg(Chip *self, Bit32u reg, Bit8u val ); 211 extern "C" void Chip__GenerateBlock2(Chip *self, Bitu total, Bit32s* output ); 212 #else 213 void Chip__Setup(Chip *self, Bit32u rate ); 214 void DBOPL_InitTables( void ); 215 void Chip__Chip(Chip *self); 216 void Chip__WriteReg(Chip *self, Bit32u reg, Bit8u val ); 217 void Chip__GenerateBlock2(Chip *self, Bitu total, Bit32s* output ); 218 #endif 219 220 #endif 221