dbopl.h 7.5 KB

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