dbopl.h 7.6 KB

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