dbopl.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. /*
  19. DOSBox implementation of a combined Yamaha YMF262 and Yamaha YM3812 emulator.
  20. Enabling the opl3 bit will switch the emulator to stereo opl3 output instead of regular mono opl2
  21. Except for the table generation it's all integer math
  22. Can choose different types of generators, using muls and bigger tables, try different ones for slower platforms
  23. The generation was based on the MAME implementation but tried to have it use less memory and be faster in general
  24. MAME uses much bigger envelope tables and this will be the biggest cause of it sounding different at times
  25. //TODO Don't delay first operator 1 sample in opl3 mode
  26. //TODO Maybe not use class method pointers but a regular function pointers with operator as first parameter
  27. //TODO Fix panning for the Percussion channels, would any opl3 player use it and actually really change it though?
  28. //TODO Check if having the same accuracy in all frequency multipliers sounds better or not
  29. //DUNNO Keyon in 4op, switch to 2op without keyoff.
  30. */
  31. #include <math.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. //#include "dosbox.h"
  35. #include "dbopl.h"
  36. #ifndef PI
  37. #define PI 3.14159265358979323846
  38. #endif
  39. namespace DBOPL {
  40. #define OPLRATE ((double)(14318180.0 / 288.0))
  41. #define TREMOLO_TABLE 52
  42. //Try to use most precision for frequencies
  43. //Else try to keep different waves in synch
  44. //#define WAVE_PRECISION 1
  45. #ifndef WAVE_PRECISION
  46. //Wave bits available in the top of the 32bit range
  47. //Original adlib uses 10.10, we use 10.22
  48. #define WAVE_BITS 10
  49. #else
  50. //Need some extra bits at the top to have room for octaves and frequency multiplier
  51. //We support to 8 times lower rate
  52. //128 * 15 * 8 = 15350, 2^13.9, so need 14 bits
  53. #define WAVE_BITS 14
  54. #endif
  55. #define WAVE_SH ( 32 - WAVE_BITS )
  56. #define WAVE_MASK ( ( 1 << WAVE_SH ) - 1 )
  57. //Use the same accuracy as the waves
  58. #define LFO_SH ( WAVE_SH - 10 )
  59. //LFO is controlled by our tremolo 256 sample limit
  60. #define LFO_MAX ( 256 << ( LFO_SH ) )
  61. //Maximum amount of attenuation bits
  62. //Envelope goes to 511, 9 bits
  63. #if (DBOPL_WAVE == WAVE_TABLEMUL )
  64. //Uses the value directly
  65. #define ENV_BITS ( 9 )
  66. #else
  67. //Add 3 bits here for more accuracy and would have to be shifted up either way
  68. #define ENV_BITS ( 9 )
  69. #endif
  70. //Limits of the envelope with those bits and when the envelope goes silent
  71. #define ENV_MIN 0
  72. #define ENV_EXTRA ( ENV_BITS - 9 )
  73. #define ENV_MAX ( 511 << ENV_EXTRA )
  74. #define ENV_LIMIT ( ( 12 * 256) >> ( 3 - ENV_EXTRA ) )
  75. #define ENV_SILENT( _X_ ) ( (_X_) >= ENV_LIMIT )
  76. //Attack/decay/release rate counter shift
  77. #define RATE_SH 24
  78. #define RATE_MASK ( ( 1 << RATE_SH ) - 1 )
  79. //Has to fit within 16bit lookuptable
  80. #define MUL_SH 16
  81. //Check some ranges
  82. #if ENV_EXTRA > 3
  83. #error Too many envelope bits
  84. #endif
  85. //How much to substract from the base value for the final attenuation
  86. static const Bit8u KslCreateTable[16] = {
  87. //0 will always be be lower than 7 * 8
  88. 64, 32, 24, 19,
  89. 16, 12, 11, 10,
  90. 8, 6, 5, 4,
  91. 3, 2, 1, 0,
  92. };
  93. #define M(_X_) ((Bit8u)( (_X_) * 2))
  94. static const Bit8u FreqCreateTable[16] = {
  95. M(0.5), M(1 ), M(2 ), M(3 ), M(4 ), M(5 ), M(6 ), M(7 ),
  96. M(8 ), M(9 ), M(10), M(10), M(12), M(12), M(15), M(15)
  97. };
  98. #undef M
  99. //We're not including the highest attack rate, that gets a special value
  100. static const Bit8u AttackSamplesTable[13] = {
  101. 69, 55, 46, 40,
  102. 35, 29, 23, 20,
  103. 19, 15, 11, 10,
  104. 9
  105. };
  106. //On a real opl these values take 8 samples to reach and are based upon larger tables
  107. static const Bit8u EnvelopeIncreaseTable[13] = {
  108. 4, 5, 6, 7,
  109. 8, 10, 12, 14,
  110. 16, 20, 24, 28,
  111. 32,
  112. };
  113. #if ( DBOPL_WAVE == WAVE_HANDLER ) || ( DBOPL_WAVE == WAVE_TABLELOG )
  114. static Bit16u ExpTable[ 256 ];
  115. #endif
  116. #if ( DBOPL_WAVE == WAVE_HANDLER )
  117. //PI table used by WAVEHANDLER
  118. static Bit16u SinTable[ 512 ];
  119. #endif
  120. #if ( DBOPL_WAVE > WAVE_HANDLER )
  121. //Layout of the waveform table in 512 entry intervals
  122. //With overlapping waves we reduce the table to half it's size
  123. // | |//\\|____|WAV7|//__|/\ |____|/\/\|
  124. // |\\//| | |WAV7| | \/| | |
  125. // |06 |0126|17 |7 |3 |4 |4 5 |5 |
  126. //6 is just 0 shifted and masked
  127. static Bit16s WaveTable[ 8 * 512 ];
  128. //Distance into WaveTable the wave starts
  129. static const Bit16u WaveBaseTable[8] = {
  130. 0x000, 0x200, 0x200, 0x800,
  131. 0xa00, 0xc00, 0x100, 0x400,
  132. };
  133. //Mask the counter with this
  134. static const Bit16u WaveMaskTable[8] = {
  135. 1023, 1023, 511, 511,
  136. 1023, 1023, 512, 1023,
  137. };
  138. //Where to start the counter on at keyon
  139. static const Bit16u WaveStartTable[8] = {
  140. 512, 0, 0, 0,
  141. 0, 512, 512, 256,
  142. };
  143. #endif
  144. #if ( DBOPL_WAVE == WAVE_TABLEMUL )
  145. static Bit16u MulTable[ 384 ];
  146. #endif
  147. static Bit8u KslTable[ 8 * 16 ];
  148. static Bit8u TremoloTable[ TREMOLO_TABLE ];
  149. //Start of a channel behind the chip struct start
  150. static Bit16u ChanOffsetTable[32];
  151. //Start of an operator behind the chip struct start
  152. static Bit16u OpOffsetTable[64];
  153. //The lower bits are the shift of the operator vibrato value
  154. //The highest bit is right shifted to generate -1 or 0 for negation
  155. //So taking the highest input value of 7 this gives 3, 7, 3, 0, -3, -7, -3, 0
  156. static const Bit8s VibratoTable[ 8 ] = {
  157. 1 - 0x00, 0 - 0x00, 1 - 0x00, 30 - 0x00,
  158. 1 - 0x80, 0 - 0x80, 1 - 0x80, 30 - 0x80
  159. };
  160. //Shift strength for the ksl value determined by ksl strength
  161. static const Bit8u KslShiftTable[4] = {
  162. 31,1,2,0
  163. };
  164. //Generate a table index and table shift value using input value from a selected rate
  165. static void EnvelopeSelect( Bit8u val, Bit8u& index, Bit8u& shift ) {
  166. if ( val < 13 * 4 ) { //Rate 0 - 12
  167. shift = 12 - ( val >> 2 );
  168. index = val & 3;
  169. } else if ( val < 15 * 4 ) { //rate 13 - 14
  170. shift = 0;
  171. index = val - 12 * 4;
  172. } else { //rate 15 and up
  173. shift = 0;
  174. index = 12;
  175. }
  176. }
  177. #if ( DBOPL_WAVE == WAVE_HANDLER )
  178. /*
  179. Generate the different waveforms out of the sine/exponetial table using handlers
  180. */
  181. static inline Bits MakeVolume( Bitu wave, Bitu volume ) {
  182. Bitu total = wave + volume;
  183. Bitu index = total & 0xff;
  184. Bitu sig = ExpTable[ index ];
  185. Bitu exp = total >> 8;
  186. #if 0
  187. //Check if we overflow the 31 shift limit
  188. if ( exp >= 32 ) {
  189. LOG_MSG( "WTF %d %d", total, exp );
  190. }
  191. #endif
  192. return (sig >> exp);
  193. };
  194. static Bits DB_FASTCALL WaveForm0( Bitu i, Bitu volume ) {
  195. Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0
  196. Bitu wave = SinTable[i & 511];
  197. return (MakeVolume( wave, volume ) ^ neg) - neg;
  198. }
  199. static Bits DB_FASTCALL WaveForm1( Bitu i, Bitu volume ) {
  200. Bit32u wave = SinTable[i & 511];
  201. wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 );
  202. return MakeVolume( wave, volume );
  203. }
  204. static Bits DB_FASTCALL WaveForm2( Bitu i, Bitu volume ) {
  205. Bitu wave = SinTable[i & 511];
  206. return MakeVolume( wave, volume );
  207. }
  208. static Bits DB_FASTCALL WaveForm3( Bitu i, Bitu volume ) {
  209. Bitu wave = SinTable[i & 255];
  210. wave |= ( ( (i ^ 256 ) & 256) - 1) >> ( 32 - 12 );
  211. return MakeVolume( wave, volume );
  212. }
  213. static Bits DB_FASTCALL WaveForm4( Bitu i, Bitu volume ) {
  214. //Twice as fast
  215. i <<= 1;
  216. Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0
  217. Bitu wave = SinTable[i & 511];
  218. wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 );
  219. return (MakeVolume( wave, volume ) ^ neg) - neg;
  220. }
  221. static Bits DB_FASTCALL WaveForm5( Bitu i, Bitu volume ) {
  222. //Twice as fast
  223. i <<= 1;
  224. Bitu wave = SinTable[i & 511];
  225. wave |= ( ( (i ^ 512 ) & 512) - 1) >> ( 32 - 12 );
  226. return MakeVolume( wave, volume );
  227. }
  228. static Bits DB_FASTCALL WaveForm6( Bitu i, Bitu volume ) {
  229. Bits neg = 0 - (( i >> 9) & 1);//Create ~0 or 0
  230. return (MakeVolume( 0, volume ) ^ neg) - neg;
  231. }
  232. static Bits DB_FASTCALL WaveForm7( Bitu i, Bitu volume ) {
  233. //Negative is reversed here
  234. Bits neg = (( i >> 9) & 1) - 1;
  235. Bitu wave = (i << 3);
  236. //When negative the volume also runs backwards
  237. wave = ((wave ^ neg) - neg) & 4095;
  238. return (MakeVolume( wave, volume ) ^ neg) - neg;
  239. }
  240. static const WaveHandler WaveHandlerTable[8] = {
  241. WaveForm0, WaveForm1, WaveForm2, WaveForm3,
  242. WaveForm4, WaveForm5, WaveForm6, WaveForm7
  243. };
  244. #endif
  245. /*
  246. Operator
  247. */
  248. //We zero out when rate == 0
  249. inline void Operator::UpdateAttack( const Chip* chip ) {
  250. Bit8u rate = reg60 >> 4;
  251. if ( rate ) {
  252. Bit8u val = (rate << 2) + ksr;
  253. attackAdd = chip->attackRates[ val ];
  254. rateZero &= ~(1 << ATTACK);
  255. } else {
  256. attackAdd = 0;
  257. rateZero |= (1 << ATTACK);
  258. }
  259. }
  260. inline void Operator::UpdateDecay( const Chip* chip ) {
  261. Bit8u rate = reg60 & 0xf;
  262. if ( rate ) {
  263. Bit8u val = (rate << 2) + ksr;
  264. decayAdd = chip->linearRates[ val ];
  265. rateZero &= ~(1 << DECAY);
  266. } else {
  267. decayAdd = 0;
  268. rateZero |= (1 << DECAY);
  269. }
  270. }
  271. inline void Operator::UpdateRelease( const Chip* chip ) {
  272. Bit8u rate = reg80 & 0xf;
  273. if ( rate ) {
  274. Bit8u val = (rate << 2) + ksr;
  275. releaseAdd = chip->linearRates[ val ];
  276. rateZero &= ~(1 << RELEASE);
  277. if ( !(reg20 & MASK_SUSTAIN ) ) {
  278. rateZero &= ~( 1 << SUSTAIN );
  279. }
  280. } else {
  281. rateZero |= (1 << RELEASE);
  282. releaseAdd = 0;
  283. if ( !(reg20 & MASK_SUSTAIN ) ) {
  284. rateZero |= ( 1 << SUSTAIN );
  285. }
  286. }
  287. }
  288. inline void Operator::UpdateAttenuation( ) {
  289. Bit8u kslBase = (Bit8u)((chanData >> SHIFT_KSLBASE) & 0xff);
  290. Bit32u tl = reg40 & 0x3f;
  291. Bit8u kslShift = KslShiftTable[ reg40 >> 6 ];
  292. //Make sure the attenuation goes to the right bits
  293. totalLevel = tl << ( ENV_BITS - 7 ); //Total level goes 2 bits below max
  294. totalLevel += ( kslBase << ENV_EXTRA ) >> kslShift;
  295. }
  296. void Operator::UpdateFrequency( ) {
  297. Bit32u freq = chanData & (( 1 << 10 ) - 1);
  298. Bit32u block = (chanData >> 10) & 0xff;
  299. #ifdef WAVE_PRECISION
  300. block = 7 - block;
  301. waveAdd = ( freq * freqMul ) >> block;
  302. #else
  303. waveAdd = ( freq << block ) * freqMul;
  304. #endif
  305. if ( reg20 & MASK_VIBRATO ) {
  306. vibStrength = (Bit8u)(freq >> 7);
  307. #ifdef WAVE_PRECISION
  308. vibrato = ( vibStrength * freqMul ) >> block;
  309. #else
  310. vibrato = ( vibStrength << block ) * freqMul;
  311. #endif
  312. } else {
  313. vibStrength = 0;
  314. vibrato = 0;
  315. }
  316. }
  317. void Operator::UpdateRates( const Chip* chip ) {
  318. //Mame seems to reverse this where enabling ksr actually lowers
  319. //the rate, but pdf manuals says otherwise?
  320. Bit8u newKsr = (Bit8u)((chanData >> SHIFT_KEYCODE) & 0xff);
  321. if ( !( reg20 & MASK_KSR ) ) {
  322. newKsr >>= 2;
  323. }
  324. if ( ksr == newKsr )
  325. return;
  326. ksr = newKsr;
  327. UpdateAttack( chip );
  328. UpdateDecay( chip );
  329. UpdateRelease( chip );
  330. }
  331. OPL_INLINE Bit32s Operator::RateForward( Bit32u add ) {
  332. rateIndex += add;
  333. Bit32s ret = rateIndex >> RATE_SH;
  334. rateIndex = rateIndex & RATE_MASK;
  335. return ret;
  336. }
  337. template< Operator::State yes>
  338. Bits Operator::TemplateVolume( ) {
  339. Bit32s vol = volume;
  340. Bit32s change;
  341. switch ( yes ) {
  342. case OFF:
  343. return ENV_MAX;
  344. case ATTACK:
  345. change = RateForward( attackAdd );
  346. if ( !change )
  347. return vol;
  348. vol += ( (~vol) * change ) >> 3;
  349. if ( vol < ENV_MIN ) {
  350. volume = ENV_MIN;
  351. rateIndex = 0;
  352. SetState( DECAY );
  353. return ENV_MIN;
  354. }
  355. break;
  356. case DECAY:
  357. vol += RateForward( decayAdd );
  358. if ( GCC_UNLIKELY(vol >= sustainLevel) ) {
  359. //Check if we didn't overshoot max attenuation, then just go off
  360. if ( GCC_UNLIKELY(vol >= ENV_MAX) ) {
  361. volume = ENV_MAX;
  362. SetState( OFF );
  363. return ENV_MAX;
  364. }
  365. //Continue as sustain
  366. rateIndex = 0;
  367. SetState( SUSTAIN );
  368. }
  369. break;
  370. case SUSTAIN:
  371. if ( reg20 & MASK_SUSTAIN ) {
  372. return vol;
  373. }
  374. //In sustain phase, but not sustaining, do regular release
  375. case RELEASE:
  376. vol += RateForward( releaseAdd );;
  377. if ( GCC_UNLIKELY(vol >= ENV_MAX) ) {
  378. volume = ENV_MAX;
  379. SetState( OFF );
  380. return ENV_MAX;
  381. }
  382. break;
  383. }
  384. volume = vol;
  385. return vol;
  386. }
  387. static const VolumeHandler VolumeHandlerTable[5] = {
  388. &Operator::TemplateVolume< Operator::OFF >,
  389. &Operator::TemplateVolume< Operator::RELEASE >,
  390. &Operator::TemplateVolume< Operator::SUSTAIN >,
  391. &Operator::TemplateVolume< Operator::DECAY >,
  392. &Operator::TemplateVolume< Operator::ATTACK >
  393. };
  394. OPL_INLINE Bitu Operator::ForwardVolume() {
  395. return currentLevel + (this->*volHandler)();
  396. }
  397. OPL_INLINE Bitu Operator::ForwardWave() {
  398. waveIndex += waveCurrent;
  399. return waveIndex >> WAVE_SH;
  400. }
  401. void Operator::Write20( const Chip* chip, Bit8u val ) {
  402. Bit8u change = (reg20 ^ val );
  403. if ( !change )
  404. return;
  405. reg20 = val;
  406. //Shift the tremolo bit over the entire register, saved a branch, YES!
  407. tremoloMask = (Bit8s)(val) >> 7;
  408. tremoloMask &= ~(( 1 << ENV_EXTRA ) -1);
  409. //Update specific features based on changes
  410. if ( change & MASK_KSR ) {
  411. UpdateRates( chip );
  412. }
  413. //With sustain enable the volume doesn't change
  414. if ( reg20 & MASK_SUSTAIN || ( !releaseAdd ) ) {
  415. rateZero |= ( 1 << SUSTAIN );
  416. } else {
  417. rateZero &= ~( 1 << SUSTAIN );
  418. }
  419. //Frequency multiplier or vibrato changed
  420. if ( change & (0xf | MASK_VIBRATO) ) {
  421. freqMul = chip->freqMul[ val & 0xf ];
  422. UpdateFrequency();
  423. }
  424. }
  425. void Operator::Write40( const Chip* /*chip*/, Bit8u val ) {
  426. if (!(reg40 ^ val ))
  427. return;
  428. reg40 = val;
  429. UpdateAttenuation( );
  430. }
  431. void Operator::Write60( const Chip* chip, Bit8u val ) {
  432. Bit8u change = reg60 ^ val;
  433. reg60 = val;
  434. if ( change & 0x0f ) {
  435. UpdateDecay( chip );
  436. }
  437. if ( change & 0xf0 ) {
  438. UpdateAttack( chip );
  439. }
  440. }
  441. void Operator::Write80( const Chip* chip, Bit8u val ) {
  442. Bit8u change = (reg80 ^ val );
  443. if ( !change )
  444. return;
  445. reg80 = val;
  446. Bit8u sustain = val >> 4;
  447. //Turn 0xf into 0x1f
  448. sustain |= ( sustain + 1) & 0x10;
  449. sustainLevel = sustain << ( ENV_BITS - 5 );
  450. if ( change & 0x0f ) {
  451. UpdateRelease( chip );
  452. }
  453. }
  454. void Operator::WriteE0( const Chip* chip, Bit8u val ) {
  455. if ( !(regE0 ^ val) )
  456. return;
  457. //in opl3 mode you can always selet 7 waveforms regardless of waveformselect
  458. Bit8u waveForm = val & ( ( 0x3 & chip->waveFormMask ) | (0x7 & chip->opl3Active ) );
  459. regE0 = val;
  460. #if ( DBOPL_WAVE == WAVE_HANDLER )
  461. waveHandler = WaveHandlerTable[ waveForm ];
  462. #else
  463. waveBase = WaveTable + WaveBaseTable[ waveForm ];
  464. waveStart = WaveStartTable[ waveForm ] << WAVE_SH;
  465. waveMask = WaveMaskTable[ waveForm ];
  466. #endif
  467. }
  468. OPL_INLINE void Operator::SetState( Bit8u s ) {
  469. state = s;
  470. volHandler = VolumeHandlerTable[ s ];
  471. }
  472. OPL_INLINE bool Operator::Silent() const {
  473. if ( !ENV_SILENT( totalLevel + volume ) )
  474. return false;
  475. if ( !(rateZero & ( 1 << state ) ) )
  476. return false;
  477. return true;
  478. }
  479. OPL_INLINE void Operator::Prepare( const Chip* chip ) {
  480. currentLevel = totalLevel + (chip->tremoloValue & tremoloMask);
  481. waveCurrent = waveAdd;
  482. if ( vibStrength >> chip->vibratoShift ) {
  483. Bit32s add = vibrato >> chip->vibratoShift;
  484. //Sign extend over the shift value
  485. Bit32s neg = chip->vibratoSign;
  486. //Negate the add with -1 or 0
  487. add = ( add ^ neg ) - neg;
  488. waveCurrent += add;
  489. }
  490. }
  491. void Operator::KeyOn( Bit8u mask ) {
  492. if ( !keyOn ) {
  493. //Restart the frequency generator
  494. #if ( DBOPL_WAVE > WAVE_HANDLER )
  495. waveIndex = waveStart;
  496. #else
  497. waveIndex = 0;
  498. #endif
  499. rateIndex = 0;
  500. SetState( ATTACK );
  501. }
  502. keyOn |= mask;
  503. }
  504. void Operator::KeyOff( Bit8u mask ) {
  505. keyOn &= ~mask;
  506. if ( !keyOn ) {
  507. if ( state != OFF ) {
  508. SetState( RELEASE );
  509. }
  510. }
  511. }
  512. OPL_INLINE Bits Operator::GetWave( Bitu index, Bitu vol ) {
  513. #if ( DBOPL_WAVE == WAVE_HANDLER )
  514. return waveHandler( index, vol << ( 3 - ENV_EXTRA ) );
  515. #elif ( DBOPL_WAVE == WAVE_TABLEMUL )
  516. return (waveBase[ index & waveMask ] * MulTable[ vol >> ENV_EXTRA ]) >> MUL_SH;
  517. #elif ( DBOPL_WAVE == WAVE_TABLELOG )
  518. Bit32s wave = waveBase[ index & waveMask ];
  519. Bit32u total = ( wave & 0x7fff ) + vol << ( 3 - ENV_EXTRA );
  520. Bit32s sig = ExpTable[ total & 0xff ];
  521. Bit32u exp = total >> 8;
  522. Bit32s neg = wave >> 16;
  523. return ((sig ^ neg) - neg) >> exp;
  524. #else
  525. #error "No valid wave routine"
  526. #endif
  527. }
  528. Bits OPL_INLINE Operator::GetSample( Bits modulation ) {
  529. Bitu vol = ForwardVolume();
  530. if ( ENV_SILENT( vol ) ) {
  531. //Simply forward the wave
  532. waveIndex += waveCurrent;
  533. return 0;
  534. } else {
  535. Bitu index = ForwardWave();
  536. index += modulation;
  537. return GetWave( index, vol );
  538. }
  539. }
  540. Operator::Operator() {
  541. chanData = 0;
  542. freqMul = 0;
  543. waveIndex = 0;
  544. waveAdd = 0;
  545. waveCurrent = 0;
  546. keyOn = 0;
  547. ksr = 0;
  548. reg20 = 0;
  549. reg40 = 0;
  550. reg60 = 0;
  551. reg80 = 0;
  552. regE0 = 0;
  553. SetState( OFF );
  554. rateZero = (1 << OFF);
  555. sustainLevel = ENV_MAX;
  556. currentLevel = ENV_MAX;
  557. totalLevel = ENV_MAX;
  558. volume = ENV_MAX;
  559. releaseAdd = 0;
  560. }
  561. /*
  562. Channel
  563. */
  564. Channel::Channel() {
  565. old[0] = old[1] = 0;
  566. chanData = 0;
  567. regB0 = 0;
  568. regC0 = 0;
  569. maskLeft = -1;
  570. maskRight = -1;
  571. feedback = 31;
  572. fourMask = 0;
  573. synthHandler = &Channel::BlockTemplate< sm2FM >;
  574. };
  575. void Channel::SetChanData( const Chip* chip, Bit32u data ) {
  576. Bit32u change = chanData ^ data;
  577. chanData = data;
  578. Op( 0 )->chanData = data;
  579. Op( 1 )->chanData = data;
  580. //Since a frequency update triggered this, always update frequency
  581. Op( 0 )->UpdateFrequency();
  582. Op( 1 )->UpdateFrequency();
  583. if ( change & ( 0xff << SHIFT_KSLBASE ) ) {
  584. Op( 0 )->UpdateAttenuation();
  585. Op( 1 )->UpdateAttenuation();
  586. }
  587. if ( change & ( 0xff << SHIFT_KEYCODE ) ) {
  588. Op( 0 )->UpdateRates( chip );
  589. Op( 1 )->UpdateRates( chip );
  590. }
  591. }
  592. void Channel::UpdateFrequency( const Chip* chip, Bit8u fourOp ) {
  593. //Extrace the frequency bits
  594. Bit32u data = chanData & 0xffff;
  595. Bit32u kslBase = KslTable[ data >> 6 ];
  596. Bit32u keyCode = ( data & 0x1c00) >> 9;
  597. if ( chip->reg08 & 0x40 ) {
  598. keyCode |= ( data & 0x100)>>8; /* notesel == 1 */
  599. } else {
  600. keyCode |= ( data & 0x200)>>9; /* notesel == 0 */
  601. }
  602. //Add the keycode and ksl into the highest bits of chanData
  603. data |= (keyCode << SHIFT_KEYCODE) | ( kslBase << SHIFT_KSLBASE );
  604. ( this + 0 )->SetChanData( chip, data );
  605. if ( fourOp & 0x3f ) {
  606. ( this + 1 )->SetChanData( chip, data );
  607. }
  608. }
  609. void Channel::WriteA0( const Chip* chip, Bit8u val ) {
  610. Bit8u fourOp = chip->reg104 & chip->opl3Active & fourMask;
  611. //Don't handle writes to silent fourop channels
  612. if ( fourOp > 0x80 )
  613. return;
  614. Bit32u change = (chanData ^ val ) & 0xff;
  615. if ( change ) {
  616. chanData ^= change;
  617. UpdateFrequency( chip, fourOp );
  618. }
  619. }
  620. void Channel::WriteB0( const Chip* chip, Bit8u val ) {
  621. Bit8u fourOp = chip->reg104 & chip->opl3Active & fourMask;
  622. //Don't handle writes to silent fourop channels
  623. if ( fourOp > 0x80 )
  624. return;
  625. Bitu change = (chanData ^ ( val << 8 ) ) & 0x1f00;
  626. if ( change ) {
  627. chanData ^= change;
  628. UpdateFrequency( chip, fourOp );
  629. }
  630. //Check for a change in the keyon/off state
  631. if ( !(( val ^ regB0) & 0x20))
  632. return;
  633. regB0 = val;
  634. if ( val & 0x20 ) {
  635. Op(0)->KeyOn( 0x1 );
  636. Op(1)->KeyOn( 0x1 );
  637. if ( fourOp & 0x3f ) {
  638. ( this + 1 )->Op(0)->KeyOn( 1 );
  639. ( this + 1 )->Op(1)->KeyOn( 1 );
  640. }
  641. } else {
  642. Op(0)->KeyOff( 0x1 );
  643. Op(1)->KeyOff( 0x1 );
  644. if ( fourOp & 0x3f ) {
  645. ( this + 1 )->Op(0)->KeyOff( 1 );
  646. ( this + 1 )->Op(1)->KeyOff( 1 );
  647. }
  648. }
  649. }
  650. void Channel::WriteC0( const Chip* chip, Bit8u val ) {
  651. Bit8u change = val ^ regC0;
  652. if ( !change )
  653. return;
  654. regC0 = val;
  655. feedback = ( val >> 1 ) & 7;
  656. if ( feedback ) {
  657. //We shift the input to the right 10 bit wave index value
  658. feedback = 9 - feedback;
  659. } else {
  660. feedback = 31;
  661. }
  662. //Select the new synth mode
  663. if ( chip->opl3Active ) {
  664. //4-op mode enabled for this channel
  665. if ( (chip->reg104 & fourMask) & 0x3f ) {
  666. Channel* chan0, *chan1;
  667. //Check if it's the 2nd channel in a 4-op
  668. if ( !(fourMask & 0x80 ) ) {
  669. chan0 = this;
  670. chan1 = this + 1;
  671. } else {
  672. chan0 = this - 1;
  673. chan1 = this;
  674. }
  675. Bit8u synth = ( (chan0->regC0 & 1) << 0 )| (( chan1->regC0 & 1) << 1 );
  676. switch ( synth ) {
  677. case 0:
  678. chan0->synthHandler = &Channel::BlockTemplate< sm3FMFM >;
  679. break;
  680. case 1:
  681. chan0->synthHandler = &Channel::BlockTemplate< sm3AMFM >;
  682. break;
  683. case 2:
  684. chan0->synthHandler = &Channel::BlockTemplate< sm3FMAM >;
  685. break;
  686. case 3:
  687. chan0->synthHandler = &Channel::BlockTemplate< sm3AMAM >;
  688. break;
  689. }
  690. //Disable updating percussion channels
  691. } else if ((fourMask & 0x40) && ( chip->regBD & 0x20) ) {
  692. //Regular dual op, am or fm
  693. } else if ( val & 1 ) {
  694. synthHandler = &Channel::BlockTemplate< sm3AM >;
  695. } else {
  696. synthHandler = &Channel::BlockTemplate< sm3FM >;
  697. }
  698. maskLeft = ( val & 0x10 ) ? -1 : 0;
  699. maskRight = ( val & 0x20 ) ? -1 : 0;
  700. //opl2 active
  701. } else {
  702. //Disable updating percussion channels
  703. if ( (fourMask & 0x40) && ( chip->regBD & 0x20 ) ) {
  704. //Regular dual op, am or fm
  705. } else if ( val & 1 ) {
  706. synthHandler = &Channel::BlockTemplate< sm2AM >;
  707. } else {
  708. synthHandler = &Channel::BlockTemplate< sm2FM >;
  709. }
  710. }
  711. }
  712. void Channel::ResetC0( const Chip* chip ) {
  713. Bit8u val = regC0;
  714. regC0 ^= 0xff;
  715. WriteC0( chip, val );
  716. };
  717. template< bool opl3Mode>
  718. OPL_INLINE void Channel::GeneratePercussion( Chip* chip, Bit32s* output ) {
  719. Channel* chan = this;
  720. //BassDrum
  721. Bit32s mod = (Bit32u)((old[0] + old[1])) >> feedback;
  722. old[0] = old[1];
  723. old[1] = Op(0)->GetSample( mod );
  724. //When bassdrum is in AM mode first operator is ignoed
  725. if ( chan->regC0 & 1 ) {
  726. mod = 0;
  727. } else {
  728. mod = old[0];
  729. }
  730. Bit32s sample = Op(1)->GetSample( mod );
  731. //Precalculate stuff used by other outputs
  732. Bit32u noiseBit = chip->ForwardNoise() & 0x1;
  733. Bit32u c2 = Op(2)->ForwardWave();
  734. Bit32u c5 = Op(5)->ForwardWave();
  735. Bit32u phaseBit = (((c2 & 0x88) ^ ((c2<<5) & 0x80)) | ((c5 ^ (c5<<2)) & 0x20)) ? 0x02 : 0x00;
  736. //Hi-Hat
  737. Bit32u hhVol = Op(2)->ForwardVolume();
  738. if ( !ENV_SILENT( hhVol ) ) {
  739. Bit32u hhIndex = (phaseBit<<8) | (0x34 << ( phaseBit ^ (noiseBit << 1 )));
  740. sample += Op(2)->GetWave( hhIndex, hhVol );
  741. }
  742. //Snare Drum
  743. Bit32u sdVol = Op(3)->ForwardVolume();
  744. if ( !ENV_SILENT( sdVol ) ) {
  745. Bit32u sdIndex = ( 0x100 + (c2 & 0x100) ) ^ ( noiseBit << 8 );
  746. sample += Op(3)->GetWave( sdIndex, sdVol );
  747. }
  748. //Tom-tom
  749. sample += Op(4)->GetSample( 0 );
  750. //Top-Cymbal
  751. Bit32u tcVol = Op(5)->ForwardVolume();
  752. if ( !ENV_SILENT( tcVol ) ) {
  753. Bit32u tcIndex = (1 + phaseBit) << 8;
  754. sample += Op(5)->GetWave( tcIndex, tcVol );
  755. }
  756. sample <<= 1;
  757. if ( opl3Mode ) {
  758. output[0] += sample;
  759. output[1] += sample;
  760. } else {
  761. output[0] += sample;
  762. }
  763. }
  764. template<SynthMode mode>
  765. Channel* Channel::BlockTemplate( Chip* chip, Bit32u samples, Bit32s* output ) {
  766. switch( mode ) {
  767. case sm2AM:
  768. case sm3AM:
  769. if ( Op(0)->Silent() && Op(1)->Silent() ) {
  770. old[0] = old[1] = 0;
  771. return (this + 1);
  772. }
  773. break;
  774. case sm2FM:
  775. case sm3FM:
  776. if ( Op(1)->Silent() ) {
  777. old[0] = old[1] = 0;
  778. return (this + 1);
  779. }
  780. break;
  781. case sm3FMFM:
  782. if ( Op(3)->Silent() ) {
  783. old[0] = old[1] = 0;
  784. return (this + 2);
  785. }
  786. break;
  787. case sm3AMFM:
  788. if ( Op(0)->Silent() && Op(3)->Silent() ) {
  789. old[0] = old[1] = 0;
  790. return (this + 2);
  791. }
  792. break;
  793. case sm3FMAM:
  794. if ( Op(1)->Silent() && Op(3)->Silent() ) {
  795. old[0] = old[1] = 0;
  796. return (this + 2);
  797. }
  798. break;
  799. case sm3AMAM:
  800. if ( Op(0)->Silent() && Op(2)->Silent() && Op(3)->Silent() ) {
  801. old[0] = old[1] = 0;
  802. return (this + 2);
  803. }
  804. break;
  805. }
  806. //Init the operators with the the current vibrato and tremolo values
  807. Op( 0 )->Prepare( chip );
  808. Op( 1 )->Prepare( chip );
  809. if ( mode > sm4Start ) {
  810. Op( 2 )->Prepare( chip );
  811. Op( 3 )->Prepare( chip );
  812. }
  813. if ( mode > sm6Start ) {
  814. Op( 4 )->Prepare( chip );
  815. Op( 5 )->Prepare( chip );
  816. }
  817. for ( Bitu i = 0; i < samples; i++ ) {
  818. //Early out for percussion handlers
  819. if ( mode == sm2Percussion ) {
  820. GeneratePercussion<false>( chip, output + i );
  821. continue; //Prevent some unitialized value bitching
  822. } else if ( mode == sm3Percussion ) {
  823. GeneratePercussion<true>( chip, output + i * 2 );
  824. continue; //Prevent some unitialized value bitching
  825. }
  826. //Do unsigned shift so we can shift out all bits but still stay in 10 bit range otherwise
  827. Bit32s mod = (Bit32u)((old[0] + old[1])) >> feedback;
  828. old[0] = old[1];
  829. old[1] = Op(0)->GetSample( mod );
  830. Bit32s sample;
  831. Bit32s out0 = old[0];
  832. if ( mode == sm2AM || mode == sm3AM ) {
  833. sample = out0 + Op(1)->GetSample( 0 );
  834. } else if ( mode == sm2FM || mode == sm3FM ) {
  835. sample = Op(1)->GetSample( out0 );
  836. } else if ( mode == sm3FMFM ) {
  837. Bits next = Op(1)->GetSample( out0 );
  838. next = Op(2)->GetSample( next );
  839. sample = Op(3)->GetSample( next );
  840. } else if ( mode == sm3AMFM ) {
  841. sample = out0;
  842. Bits next = Op(1)->GetSample( 0 );
  843. next = Op(2)->GetSample( next );
  844. sample += Op(3)->GetSample( next );
  845. } else if ( mode == sm3FMAM ) {
  846. sample = Op(1)->GetSample( out0 );
  847. Bits next = Op(2)->GetSample( 0 );
  848. sample += Op(3)->GetSample( next );
  849. } else if ( mode == sm3AMAM ) {
  850. sample = out0;
  851. Bits next = Op(1)->GetSample( 0 );
  852. sample += Op(2)->GetSample( next );
  853. sample += Op(3)->GetSample( 0 );
  854. }
  855. switch( mode ) {
  856. case sm2AM:
  857. case sm2FM:
  858. output[ i ] += sample;
  859. break;
  860. case sm3AM:
  861. case sm3FM:
  862. case sm3FMFM:
  863. case sm3AMFM:
  864. case sm3FMAM:
  865. case sm3AMAM:
  866. output[ i * 2 + 0 ] += sample & maskLeft;
  867. output[ i * 2 + 1 ] += sample & maskRight;
  868. break;
  869. }
  870. }
  871. switch( mode ) {
  872. case sm2AM:
  873. case sm2FM:
  874. case sm3AM:
  875. case sm3FM:
  876. return ( this + 1 );
  877. case sm3FMFM:
  878. case sm3AMFM:
  879. case sm3FMAM:
  880. case sm3AMAM:
  881. return( this + 2 );
  882. case sm2Percussion:
  883. case sm3Percussion:
  884. return( this + 3 );
  885. }
  886. return 0;
  887. }
  888. /*
  889. Chip
  890. */
  891. Chip::Chip() {
  892. reg08 = 0;
  893. reg04 = 0;
  894. regBD = 0;
  895. reg104 = 0;
  896. opl3Active = 0;
  897. }
  898. OPL_INLINE Bit32u Chip::ForwardNoise() {
  899. noiseCounter += noiseAdd;
  900. Bitu count = noiseCounter >> LFO_SH;
  901. noiseCounter &= WAVE_MASK;
  902. for ( ; count > 0; --count ) {
  903. //Noise calculation from mame
  904. noiseValue ^= ( 0x800302 ) & ( 0 - (noiseValue & 1 ) );
  905. noiseValue >>= 1;
  906. }
  907. return noiseValue;
  908. }
  909. OPL_INLINE Bit32u Chip::ForwardLFO( Bit32u samples ) {
  910. //Current vibrato value, runs 4x slower than tremolo
  911. vibratoSign = ( VibratoTable[ vibratoIndex >> 2] ) >> 7;
  912. vibratoShift = ( VibratoTable[ vibratoIndex >> 2] & 7) + vibratoStrength;
  913. tremoloValue = TremoloTable[ tremoloIndex ] >> tremoloStrength;
  914. //Check hom many samples there can be done before the value changes
  915. Bit32u todo = LFO_MAX - lfoCounter;
  916. Bit32u count = (todo + lfoAdd - 1) / lfoAdd;
  917. if ( count > samples ) {
  918. count = samples;
  919. lfoCounter += count * lfoAdd;
  920. } else {
  921. lfoCounter += count * lfoAdd;
  922. lfoCounter &= (LFO_MAX - 1);
  923. //Maximum of 7 vibrato value * 4
  924. vibratoIndex = ( vibratoIndex + 1 ) & 31;
  925. //Clip tremolo to the the table size
  926. if ( tremoloIndex + 1 < TREMOLO_TABLE )
  927. ++tremoloIndex;
  928. else
  929. tremoloIndex = 0;
  930. }
  931. return count;
  932. }
  933. void Chip::WriteBD( Bit8u val ) {
  934. Bit8u change = regBD ^ val;
  935. if ( !change )
  936. return;
  937. regBD = val;
  938. //TODO could do this with shift and xor?
  939. vibratoStrength = (val & 0x40) ? 0x00 : 0x01;
  940. tremoloStrength = (val & 0x80) ? 0x00 : 0x02;
  941. if ( val & 0x20 ) {
  942. //Drum was just enabled, make sure channel 6 has the right synth
  943. if ( change & 0x20 ) {
  944. if ( opl3Active ) {
  945. chan[6].synthHandler = &Channel::BlockTemplate< sm3Percussion >;
  946. } else {
  947. chan[6].synthHandler = &Channel::BlockTemplate< sm2Percussion >;
  948. }
  949. }
  950. //Bass Drum
  951. if ( val & 0x10 ) {
  952. chan[6].op[0].KeyOn( 0x2 );
  953. chan[6].op[1].KeyOn( 0x2 );
  954. } else {
  955. chan[6].op[0].KeyOff( 0x2 );
  956. chan[6].op[1].KeyOff( 0x2 );
  957. }
  958. //Hi-Hat
  959. if ( val & 0x1 ) {
  960. chan[7].op[0].KeyOn( 0x2 );
  961. } else {
  962. chan[7].op[0].KeyOff( 0x2 );
  963. }
  964. //Snare
  965. if ( val & 0x8 ) {
  966. chan[7].op[1].KeyOn( 0x2 );
  967. } else {
  968. chan[7].op[1].KeyOff( 0x2 );
  969. }
  970. //Tom-Tom
  971. if ( val & 0x4 ) {
  972. chan[8].op[0].KeyOn( 0x2 );
  973. } else {
  974. chan[8].op[0].KeyOff( 0x2 );
  975. }
  976. //Top Cymbal
  977. if ( val & 0x2 ) {
  978. chan[8].op[1].KeyOn( 0x2 );
  979. } else {
  980. chan[8].op[1].KeyOff( 0x2 );
  981. }
  982. //Toggle keyoffs when we turn off the percussion
  983. } else if ( change & 0x20 ) {
  984. //Trigger a reset to setup the original synth handler
  985. chan[6].ResetC0( this );
  986. chan[6].op[0].KeyOff( 0x2 );
  987. chan[6].op[1].KeyOff( 0x2 );
  988. chan[7].op[0].KeyOff( 0x2 );
  989. chan[7].op[1].KeyOff( 0x2 );
  990. chan[8].op[0].KeyOff( 0x2 );
  991. chan[8].op[1].KeyOff( 0x2 );
  992. }
  993. }
  994. #define REGOP( _FUNC_ ) \
  995. index = ( ( reg >> 3) & 0x20 ) | ( reg & 0x1f ); \
  996. if ( OpOffsetTable[ index ] ) { \
  997. Operator* regOp = (Operator*)( ((char *)this ) + OpOffsetTable[ index ] ); \
  998. regOp->_FUNC_( this, val ); \
  999. }
  1000. #define REGCHAN( _FUNC_ ) \
  1001. index = ( ( reg >> 4) & 0x10 ) | ( reg & 0xf ); \
  1002. if ( ChanOffsetTable[ index ] ) { \
  1003. Channel* regChan = (Channel*)( ((char *)this ) + ChanOffsetTable[ index ] ); \
  1004. regChan->_FUNC_( this, val ); \
  1005. }
  1006. void Chip::WriteReg( Bit32u reg, Bit8u val ) {
  1007. Bitu index;
  1008. switch ( (reg & 0xf0) >> 4 ) {
  1009. case 0x00 >> 4:
  1010. if ( reg == 0x01 ) {
  1011. waveFormMask = ( val & 0x20 ) ? 0x7 : 0x0;
  1012. } else if ( reg == 0x104 ) {
  1013. //Only detect changes in lowest 6 bits
  1014. if ( !((reg104 ^ val) & 0x3f) )
  1015. return;
  1016. //Always keep the highest bit enabled, for checking > 0x80
  1017. reg104 = 0x80 | ( val & 0x3f );
  1018. } else if ( reg == 0x105 ) {
  1019. //MAME says the real opl3 doesn't reset anything on opl3 disable/enable till the next write in another register
  1020. if ( !((opl3Active ^ val) & 1 ) )
  1021. return;
  1022. opl3Active = ( val & 1 ) ? 0xff : 0;
  1023. //Update the 0xc0 register for all channels to signal the switch to mono/stereo handlers
  1024. for ( int i = 0; i < 18;i++ ) {
  1025. chan[i].ResetC0( this );
  1026. }
  1027. } else if ( reg == 0x08 ) {
  1028. reg08 = val;
  1029. }
  1030. case 0x10 >> 4:
  1031. break;
  1032. case 0x20 >> 4:
  1033. case 0x30 >> 4:
  1034. REGOP( Write20 );
  1035. break;
  1036. case 0x40 >> 4:
  1037. case 0x50 >> 4:
  1038. REGOP( Write40 );
  1039. break;
  1040. case 0x60 >> 4:
  1041. case 0x70 >> 4:
  1042. REGOP( Write60 );
  1043. break;
  1044. case 0x80 >> 4:
  1045. case 0x90 >> 4:
  1046. REGOP( Write80 );
  1047. break;
  1048. case 0xa0 >> 4:
  1049. REGCHAN( WriteA0 );
  1050. break;
  1051. case 0xb0 >> 4:
  1052. if ( reg == 0xbd ) {
  1053. WriteBD( val );
  1054. } else {
  1055. REGCHAN( WriteB0 );
  1056. }
  1057. break;
  1058. case 0xc0 >> 4:
  1059. REGCHAN( WriteC0 );
  1060. case 0xd0 >> 4:
  1061. break;
  1062. case 0xe0 >> 4:
  1063. case 0xf0 >> 4:
  1064. REGOP( WriteE0 );
  1065. break;
  1066. }
  1067. }
  1068. Bit32u Chip::WriteAddr( Bit32u port, Bit8u val ) {
  1069. switch ( port & 3 ) {
  1070. case 0:
  1071. return val;
  1072. case 2:
  1073. if ( opl3Active || (val == 0x05) )
  1074. return 0x100 | val;
  1075. else
  1076. return val;
  1077. }
  1078. return 0;
  1079. }
  1080. void Chip::GenerateBlock2( Bitu total, Bit32s* output ) {
  1081. while ( total > 0 ) {
  1082. Bit32u samples = ForwardLFO( total );
  1083. memset(output, 0, sizeof(Bit32s) * samples);
  1084. int count = 0;
  1085. for( Channel* ch = chan; ch < chan + 9; ) {
  1086. count++;
  1087. ch = (ch->*(ch->synthHandler))( this, samples, output );
  1088. }
  1089. total -= samples;
  1090. output += samples;
  1091. }
  1092. }
  1093. void Chip::GenerateBlock3( Bitu total, Bit32s* output ) {
  1094. while ( total > 0 ) {
  1095. Bit32u samples = ForwardLFO( total );
  1096. memset(output, 0, sizeof(Bit32s) * samples *2);
  1097. int count = 0;
  1098. for( Channel* ch = chan; ch < chan + 18; ) {
  1099. count++;
  1100. ch = (ch->*(ch->synthHandler))( this, samples, output );
  1101. }
  1102. total -= samples;
  1103. output += samples * 2;
  1104. }
  1105. }
  1106. void Chip::Setup( Bit32u rate ) {
  1107. double original = OPLRATE;
  1108. // double original = rate;
  1109. double scale = original / (double)rate;
  1110. //Noise counter is run at the same precision as general waves
  1111. noiseAdd = (Bit32u)( 0.5 + scale * ( 1 << LFO_SH ) );
  1112. noiseCounter = 0;
  1113. noiseValue = 1; //Make sure it triggers the noise xor the first time
  1114. //The low frequency oscillation counter
  1115. //Every time his overflows vibrato and tremoloindex are increased
  1116. lfoAdd = (Bit32u)( 0.5 + scale * ( 1 << LFO_SH ) );
  1117. lfoCounter = 0;
  1118. vibratoIndex = 0;
  1119. tremoloIndex = 0;
  1120. //With higher octave this gets shifted up
  1121. //-1 since the freqCreateTable = *2
  1122. #ifdef WAVE_PRECISION
  1123. double freqScale = ( 1 << 7 ) * scale * ( 1 << ( WAVE_SH - 1 - 10));
  1124. for ( int i = 0; i < 16; i++ ) {
  1125. freqMul[i] = (Bit32u)( 0.5 + freqScale * FreqCreateTable[ i ] );
  1126. }
  1127. #else
  1128. Bit32u freqScale = (Bit32u)( 0.5 + scale * ( 1 << ( WAVE_SH - 1 - 10)));
  1129. for ( int i = 0; i < 16; i++ ) {
  1130. freqMul[i] = freqScale * FreqCreateTable[ i ];
  1131. }
  1132. #endif
  1133. //-3 since the real envelope takes 8 steps to reach the single value we supply
  1134. for ( Bit8u i = 0; i < 76; i++ ) {
  1135. Bit8u index, shift;
  1136. EnvelopeSelect( i, index, shift );
  1137. linearRates[i] = (Bit32u)( scale * (EnvelopeIncreaseTable[ index ] << ( RATE_SH + ENV_EXTRA - shift - 3 )));
  1138. }
  1139. //Generate the best matching attack rate
  1140. for ( Bit8u i = 0; i < 62; i++ ) {
  1141. Bit8u index, shift;
  1142. EnvelopeSelect( i, index, shift );
  1143. //Original amount of samples the attack would take
  1144. Bit32s original = (Bit32u)( (AttackSamplesTable[ index ] << shift) / scale);
  1145. Bit32s guessAdd = (Bit32u)( scale * (EnvelopeIncreaseTable[ index ] << ( RATE_SH - shift - 3 )));
  1146. Bit32s bestAdd = guessAdd;
  1147. Bit32u bestDiff = 1 << 30;
  1148. for( Bit32u passes = 0; passes < 16; passes ++ ) {
  1149. Bit32s volume = ENV_MAX;
  1150. Bit32s samples = 0;
  1151. Bit32u count = 0;
  1152. while ( volume > 0 && samples < original * 2 ) {
  1153. count += guessAdd;
  1154. Bit32s change = count >> RATE_SH;
  1155. count &= RATE_MASK;
  1156. if ( GCC_UNLIKELY(change) ) { // less than 1 %
  1157. volume += ( ~volume * change ) >> 3;
  1158. }
  1159. samples++;
  1160. }
  1161. Bit32s diff = original - samples;
  1162. Bit32u lDiff = labs( diff );
  1163. //Init last on first pass
  1164. if ( lDiff < bestDiff ) {
  1165. bestDiff = lDiff;
  1166. bestAdd = guessAdd;
  1167. if ( !bestDiff )
  1168. break;
  1169. }
  1170. //Below our target
  1171. if ( diff < 0 ) {
  1172. //Better than the last time
  1173. Bit32s mul = ((original - diff) << 12) / original;
  1174. guessAdd = ((guessAdd * mul) >> 12);
  1175. guessAdd++;
  1176. } else if ( diff > 0 ) {
  1177. Bit32s mul = ((original - diff) << 12) / original;
  1178. guessAdd = (guessAdd * mul) >> 12;
  1179. guessAdd--;
  1180. }
  1181. }
  1182. attackRates[i] = bestAdd;
  1183. }
  1184. for ( Bit8u i = 62; i < 76; i++ ) {
  1185. //This should provide instant volume maximizing
  1186. attackRates[i] = 8 << RATE_SH;
  1187. }
  1188. //Setup the channels with the correct four op flags
  1189. //Channels are accessed through a table so they appear linear here
  1190. chan[ 0].fourMask = 0x00 | ( 1 << 0 );
  1191. chan[ 1].fourMask = 0x80 | ( 1 << 0 );
  1192. chan[ 2].fourMask = 0x00 | ( 1 << 1 );
  1193. chan[ 3].fourMask = 0x80 | ( 1 << 1 );
  1194. chan[ 4].fourMask = 0x00 | ( 1 << 2 );
  1195. chan[ 5].fourMask = 0x80 | ( 1 << 2 );
  1196. chan[ 9].fourMask = 0x00 | ( 1 << 3 );
  1197. chan[10].fourMask = 0x80 | ( 1 << 3 );
  1198. chan[11].fourMask = 0x00 | ( 1 << 4 );
  1199. chan[12].fourMask = 0x80 | ( 1 << 4 );
  1200. chan[13].fourMask = 0x00 | ( 1 << 5 );
  1201. chan[14].fourMask = 0x80 | ( 1 << 5 );
  1202. //mark the percussion channels
  1203. chan[ 6].fourMask = 0x40;
  1204. chan[ 7].fourMask = 0x40;
  1205. chan[ 8].fourMask = 0x40;
  1206. //Clear Everything in opl3 mode
  1207. WriteReg( 0x105, 0x1 );
  1208. for ( int i = 0; i < 512; i++ ) {
  1209. if ( i == 0x105 )
  1210. continue;
  1211. WriteReg( i, 0xff );
  1212. WriteReg( i, 0x0 );
  1213. }
  1214. WriteReg( 0x105, 0x0 );
  1215. //Clear everything in opl2 mode
  1216. for ( int i = 0; i < 255; i++ ) {
  1217. WriteReg( i, 0xff );
  1218. WriteReg( i, 0x0 );
  1219. }
  1220. }
  1221. static bool doneTables = false;
  1222. bool InitTables( void ) {
  1223. if ( doneTables )
  1224. return true;
  1225. doneTables = true;
  1226. #if ( DBOPL_WAVE == WAVE_HANDLER ) || ( DBOPL_WAVE == WAVE_TABLELOG )
  1227. //Exponential volume table, same as the real adlib
  1228. for ( int i = 0; i < 256; i++ ) {
  1229. //Save them in reverse
  1230. ExpTable[i] = (int)( 0.5 + ( pow(2.0, ( 255 - i) * ( 1.0 /256 ) )-1) * 1024 );
  1231. ExpTable[i] += 1024; //or remove the -1 oh well :)
  1232. //Preshift to the left once so the final volume can shift to the right
  1233. ExpTable[i] *= 2;
  1234. }
  1235. #endif
  1236. #if ( DBOPL_WAVE == WAVE_HANDLER )
  1237. //Add 0.5 for the trunc rounding of the integer cast
  1238. //Do a PI sinetable instead of the original 0.5 PI
  1239. for ( int i = 0; i < 512; i++ ) {
  1240. SinTable[i] = (Bit16s)( 0.5 - log10( sin( (i + 0.5) * (PI / 512.0) ) ) / log10(2.0)*256 );
  1241. }
  1242. #endif
  1243. #if ( DBOPL_WAVE == WAVE_TABLEMUL )
  1244. //Multiplication based tables
  1245. for ( int i = 0; i < 384; i++ ) {
  1246. int s = i * 8;
  1247. //TODO maybe keep some of the precision errors of the original table?
  1248. double val = ( 0.5 + ( pow(2.0, -1.0 + ( 255 - s) * ( 1.0 /256 ) )) * ( 1 << MUL_SH ));
  1249. MulTable[i] = (Bit16u)(val);
  1250. }
  1251. //Sine Wave Base
  1252. for ( int i = 0; i < 512; i++ ) {
  1253. WaveTable[ 0x0200 + i ] = (Bit16s)(sin( (i + 0.5) * (PI / 512.0) ) * 4084);
  1254. WaveTable[ 0x0000 + i ] = -WaveTable[ 0x200 + i ];
  1255. }
  1256. //Exponential wave
  1257. for ( int i = 0; i < 256; i++ ) {
  1258. WaveTable[ 0x700 + i ] = (Bit16s)( 0.5 + ( pow(2.0, -1.0 + ( 255 - i * 8) * ( 1.0 /256 ) ) ) * 4085 );
  1259. WaveTable[ 0x6ff - i ] = -WaveTable[ 0x700 + i ];
  1260. }
  1261. #endif
  1262. #if ( DBOPL_WAVE == WAVE_TABLELOG )
  1263. //Sine Wave Base
  1264. for ( int i = 0; i < 512; i++ ) {
  1265. WaveTable[ 0x0200 + i ] = (Bit16s)( 0.5 - log10( sin( (i + 0.5) * (PI / 512.0) ) ) / log10(2.0)*256 );
  1266. WaveTable[ 0x0000 + i ] = ((Bit16s)0x8000) | WaveTable[ 0x200 + i];
  1267. }
  1268. //Exponential wave
  1269. for ( int i = 0; i < 256; i++ ) {
  1270. WaveTable[ 0x700 + i ] = i * 8;
  1271. WaveTable[ 0x6ff - i ] = ((Bit16s)0x8000) | i * 8;
  1272. }
  1273. #endif
  1274. // | |//\\|____|WAV7|//__|/\ |____|/\/\|
  1275. // |\\//| | |WAV7| | \/| | |
  1276. // |06 |0126|27 |7 |3 |4 |4 5 |5 |
  1277. #if (( DBOPL_WAVE == WAVE_TABLELOG ) || ( DBOPL_WAVE == WAVE_TABLEMUL ))
  1278. for ( int i = 0; i < 256; i++ ) {
  1279. //Fill silence gaps
  1280. WaveTable[ 0x400 + i ] = WaveTable[0];
  1281. WaveTable[ 0x500 + i ] = WaveTable[0];
  1282. WaveTable[ 0x900 + i ] = WaveTable[0];
  1283. WaveTable[ 0xc00 + i ] = WaveTable[0];
  1284. WaveTable[ 0xd00 + i ] = WaveTable[0];
  1285. //Replicate sines in other pieces
  1286. WaveTable[ 0x800 + i ] = WaveTable[ 0x200 + i ];
  1287. //double speed sines
  1288. WaveTable[ 0xa00 + i ] = WaveTable[ 0x200 + i * 2 ];
  1289. WaveTable[ 0xb00 + i ] = WaveTable[ 0x000 + i * 2 ];
  1290. WaveTable[ 0xe00 + i ] = WaveTable[ 0x200 + i * 2 ];
  1291. WaveTable[ 0xf00 + i ] = WaveTable[ 0x200 + i * 2 ];
  1292. }
  1293. #endif
  1294. //Create the ksl table
  1295. for ( int oct = 0; oct < 8; oct++ ) {
  1296. int base = oct * 8;
  1297. for ( int i = 0; i < 16; i++ ) {
  1298. int val = base - KslCreateTable[i];
  1299. if ( val < 0 )
  1300. val = 0;
  1301. //*4 for the final range to match attenuation range
  1302. KslTable[ oct * 16 + i ] = val * 4;
  1303. }
  1304. }
  1305. //Create the Tremolo table, just increase and decrease a triangle wave
  1306. for ( Bit8u i = 0; i < TREMOLO_TABLE / 2; i++ ) {
  1307. Bit8u val = i << ENV_EXTRA;
  1308. TremoloTable[i] = val;
  1309. TremoloTable[TREMOLO_TABLE - 1 - i] = val;
  1310. }
  1311. //Create a table with offsets of the channels from the start of the chip
  1312. DBOPL::Chip* chip = 0;
  1313. for ( Bitu i = 0; i < 32; i++ ) {
  1314. Bitu index = i & 0xf;
  1315. if ( index >= 9 ) {
  1316. ChanOffsetTable[i] = 0;
  1317. continue;
  1318. }
  1319. //Make sure the four op channels follow eachother
  1320. if ( index < 6 ) {
  1321. index = (index % 3) * 2 + ( index / 3 );
  1322. }
  1323. //Add back the bits for highest ones
  1324. if ( i >= 16 )
  1325. index += 9;
  1326. Bitu blah = reinterpret_cast<Bitu>( &(chip->chan[ index ]) );
  1327. ChanOffsetTable[i] = blah;
  1328. }
  1329. //Same for operators
  1330. for ( Bitu i = 0; i < 64; i++ ) {
  1331. if ( i % 8 >= 6 || ( (i / 8) % 4 == 3 ) ) {
  1332. OpOffsetTable[i] = 0;
  1333. continue;
  1334. }
  1335. Bitu chNum = (i / 8) * 3 + (i % 8) % 3;
  1336. //Make sure we use 16 and up for the 2nd range to match the chanoffset gap
  1337. if ( chNum >= 12 )
  1338. chNum += 16 - 12;
  1339. Bitu opNum = ( i % 8 ) / 3;
  1340. DBOPL::Channel* chan = 0;
  1341. Bitu blah = reinterpret_cast<Bitu>( &(chan->op[opNum]) );
  1342. OpOffsetTable[i] = ChanOffsetTable[ chNum ] + blah;
  1343. }
  1344. #if 0
  1345. //Stupid checks if table's are correct
  1346. for ( Bitu i = 0; i < 18; i++ ) {
  1347. Bit32u find = (Bit16u)( &(chip->chan[ i ]) );
  1348. for ( Bitu c = 0; c < 32; c++ ) {
  1349. if ( ChanOffsetTable[c] == find ) {
  1350. find = 0;
  1351. break;
  1352. }
  1353. }
  1354. if ( find ) {
  1355. find = find;
  1356. }
  1357. }
  1358. for ( Bitu i = 0; i < 36; i++ ) {
  1359. Bit32u find = (Bit16u)( &(chip->chan[ i / 2 ].op[i % 2]) );
  1360. for ( Bitu c = 0; c < 64; c++ ) {
  1361. if ( OpOffsetTable[c] == find ) {
  1362. find = 0;
  1363. break;
  1364. }
  1365. }
  1366. if ( find ) {
  1367. find = find;
  1368. }
  1369. }
  1370. #endif
  1371. return true;
  1372. }
  1373. }; //Namespace DBOPL