/gme/Sap_Apu.cpp

http://game-music-emu.googlecode.com/ · C++ · 334 lines · 261 code · 36 blank · 37 comment · 50 complexity · 61307410c727187b449840d47655ce67 MD5 · raw file

  1. // Game_Music_Emu 0.5.5. http://www.slack.net/~ant/
  2. #include "Sap_Apu.h"
  3. #include <string.h>
  4. /* Copyright (C) 2006 Shay Green. This module is free software; you
  5. can redistribute it and/or modify it under the terms of the GNU Lesser
  6. General Public License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version. This
  8. module is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  11. details. You should have received a copy of the GNU Lesser General Public
  12. License along with this module; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
  14. #include "blargg_source.h"
  15. int const max_frequency = 12000; // pure waves above this frequency are silenced
  16. static void gen_poly( blargg_ulong mask, int count, byte* out )
  17. {
  18. blargg_ulong n = 1;
  19. do
  20. {
  21. int bits = 0;
  22. int b = 0;
  23. do
  24. {
  25. // implemented using "Galios configuration"
  26. bits |= (n & 1) << b;
  27. n = (n >> 1) ^ (mask & -(n & 1));
  28. }
  29. while ( b++ < 7 );
  30. *out++ = bits;
  31. }
  32. while ( --count );
  33. }
  34. // poly5
  35. int const poly5_len = (1 << 5) - 1;
  36. blargg_ulong const poly5_mask = (1UL << poly5_len) - 1;
  37. blargg_ulong const poly5 = 0x167C6EA1;
  38. inline blargg_ulong run_poly5( blargg_ulong in, int shift )
  39. {
  40. return (in << shift & poly5_mask) | (in >> (poly5_len - shift));
  41. }
  42. #define POLY_MASK( width, tap1, tap2 ) \
  43. ((1UL << (width - 1 - tap1)) | (1UL << (width - 1 - tap2)))
  44. Sap_Apu_Impl::Sap_Apu_Impl()
  45. {
  46. gen_poly( POLY_MASK( 4, 1, 0 ), sizeof poly4, poly4 );
  47. gen_poly( POLY_MASK( 9, 5, 0 ), sizeof poly9, poly9 );
  48. gen_poly( POLY_MASK( 17, 5, 0 ), sizeof poly17, poly17 );
  49. if ( 0 ) // comment out to recauculate poly5 constant
  50. {
  51. byte poly5 [4];
  52. gen_poly( POLY_MASK( 5, 2, 0 ), sizeof poly5, poly5 );
  53. blargg_ulong n = poly5 [3] * 0x1000000L + poly5 [2] * 0x10000L +
  54. poly5 [1] * 0x100L + poly5 [0];
  55. blargg_ulong rev = n & 1;
  56. for ( int i = 1; i < poly5_len; i++ )
  57. rev |= (n >> i & 1) << (poly5_len - i);
  58. debug_printf( "poly5: 0x%08lX\n", rev );
  59. }
  60. }
  61. Sap_Apu::Sap_Apu()
  62. {
  63. impl = 0;
  64. for ( int i = 0; i < osc_count; i++ )
  65. osc_output( i, 0 );
  66. }
  67. void Sap_Apu::reset( Sap_Apu_Impl* new_impl )
  68. {
  69. impl = new_impl;
  70. last_time = 0;
  71. poly5_pos = 0;
  72. poly4_pos = 0;
  73. polym_pos = 0;
  74. control = 0;
  75. for ( int i = 0; i < osc_count; i++ )
  76. memset( &oscs [i], 0, offsetof (osc_t,output) );
  77. }
  78. inline void Sap_Apu::calc_periods()
  79. {
  80. // 15/64 kHz clock
  81. int divider = 28;
  82. if ( this->control & 1 )
  83. divider = 114;
  84. for ( int i = 0; i < osc_count; i++ )
  85. {
  86. osc_t* const osc = &oscs [i];
  87. int const osc_reload = osc->regs [0]; // cache
  88. blargg_long period = (osc_reload + 1) * divider;
  89. static byte const fast_bits [osc_count] = { 1 << 6, 1 << 4, 1 << 5, 1 << 3 };
  90. if ( this->control & fast_bits [i] )
  91. {
  92. period = osc_reload + 4;
  93. if ( i & 1 )
  94. {
  95. period = osc_reload * 0x100L + osc [-1].regs [0] + 7;
  96. if ( !(this->control & fast_bits [i - 1]) )
  97. period = (period - 6) * divider;
  98. if ( (osc [-1].regs [1] & 0x1F) > 0x10 )
  99. debug_printf( "Use of slave channel in 16-bit mode not supported\n" );
  100. }
  101. }
  102. osc->period = period;
  103. }
  104. }
  105. void Sap_Apu::run_until( blip_time_t end_time )
  106. {
  107. calc_periods();
  108. Sap_Apu_Impl* const impl = this->impl; // cache
  109. // 17/9-bit poly selection
  110. byte const* polym = impl->poly17;
  111. int polym_len = poly17_len;
  112. if ( this->control & 0x80 )
  113. {
  114. polym_len = poly9_len;
  115. polym = impl->poly9;
  116. }
  117. polym_pos %= polym_len;
  118. for ( int i = 0; i < osc_count; i++ )
  119. {
  120. osc_t* const osc = &oscs [i];
  121. blip_time_t time = last_time + osc->delay;
  122. blip_time_t const period = osc->period;
  123. // output
  124. Blip_Buffer* output = osc->output;
  125. if ( output )
  126. {
  127. output->set_modified();
  128. int const osc_control = osc->regs [1]; // cache
  129. int volume = (osc_control & 0x0F) * 2;
  130. if ( !volume || osc_control & 0x10 || // silent, DAC mode, or inaudible frequency
  131. ((osc_control & 0xA0) == 0xA0 && period < 1789773 / 2 / max_frequency) )
  132. {
  133. if ( !(osc_control & 0x10) )
  134. volume >>= 1; // inaudible frequency = half volume
  135. int delta = volume - osc->last_amp;
  136. if ( delta )
  137. {
  138. osc->last_amp = volume;
  139. impl->synth.offset( last_time, delta, output );
  140. }
  141. // TODO: doesn't maintain high pass flip-flop (very minor issue)
  142. }
  143. else
  144. {
  145. // high pass
  146. static byte const hipass_bits [osc_count] = { 1 << 2, 1 << 1, 0, 0 };
  147. blip_time_t period2 = 0; // unused if no high pass
  148. blip_time_t time2 = end_time;
  149. if ( this->control & hipass_bits [i] )
  150. {
  151. period2 = osc [2].period;
  152. time2 = last_time + osc [2].delay;
  153. if ( osc->invert )
  154. {
  155. // trick inner wave loop into inverting output
  156. osc->last_amp -= volume;
  157. volume = -volume;
  158. }
  159. }
  160. if ( time < end_time || time2 < end_time )
  161. {
  162. // poly source
  163. static byte const poly1 [] = { 0x55, 0x55 }; // square wave
  164. byte const* poly = poly1;
  165. int poly_len = 8 * sizeof poly1; // can be just 2 bits, but this is faster
  166. int poly_pos = osc->phase & 1;
  167. int poly_inc = 1;
  168. if ( !(osc_control & 0x20) )
  169. {
  170. poly = polym;
  171. poly_len = polym_len;
  172. poly_pos = polym_pos;
  173. if ( osc_control & 0x40 )
  174. {
  175. poly = impl->poly4;
  176. poly_len = poly4_len;
  177. poly_pos = poly4_pos;
  178. }
  179. poly_inc = period % poly_len;
  180. poly_pos = (poly_pos + osc->delay) % poly_len;
  181. }
  182. poly_inc -= poly_len; // allows more optimized inner loop below
  183. // square/poly5 wave
  184. blargg_ulong wave = poly5;
  185. check( poly5 & 1 ); // low bit is set for pure wave
  186. int poly5_inc = 0;
  187. if ( !(osc_control & 0x80) )
  188. {
  189. wave = run_poly5( wave, (osc->delay + poly5_pos) % poly5_len );
  190. poly5_inc = period % poly5_len;
  191. }
  192. // Run wave and high pass interleved with each catching up to the other.
  193. // Disabled high pass has no performance effect since inner wave loop
  194. // makes no compromise for high pass, and only runs once in that case.
  195. int osc_last_amp = osc->last_amp;
  196. do
  197. {
  198. // run high pass
  199. if ( time2 < time )
  200. {
  201. int delta = -osc_last_amp;
  202. if ( volume < 0 )
  203. delta += volume;
  204. if ( delta )
  205. {
  206. osc_last_amp += delta - volume;
  207. volume = -volume;
  208. impl->synth.offset( time2, delta, output );
  209. }
  210. }
  211. while ( time2 <= time ) // must advance *past* time to avoid hang
  212. time2 += period2;
  213. // run wave
  214. blip_time_t end = end_time;
  215. if ( end > time2 )
  216. end = time2;
  217. while ( time < end )
  218. {
  219. if ( wave & 1 )
  220. {
  221. int amp = volume & -(poly [poly_pos >> 3] >> (poly_pos & 7) & 1);
  222. if ( (poly_pos += poly_inc) < 0 )
  223. poly_pos += poly_len;
  224. int delta = amp - osc_last_amp;
  225. if ( delta )
  226. {
  227. osc_last_amp = amp;
  228. impl->synth.offset( time, delta, output );
  229. }
  230. }
  231. wave = run_poly5( wave, poly5_inc );
  232. time += period;
  233. }
  234. }
  235. while ( time < end_time || time2 < end_time );
  236. osc->phase = poly_pos;
  237. osc->last_amp = osc_last_amp;
  238. }
  239. osc->invert = 0;
  240. if ( volume < 0 )
  241. {
  242. // undo inversion trickery
  243. osc->last_amp -= volume;
  244. osc->invert = 1;
  245. }
  246. }
  247. }
  248. // maintain divider
  249. blip_time_t remain = end_time - time;
  250. if ( remain > 0 )
  251. {
  252. blargg_long count = (remain + period - 1) / period;
  253. osc->phase ^= count;
  254. time += count * period;
  255. }
  256. osc->delay = time - end_time;
  257. }
  258. // advance polies
  259. blip_time_t duration = end_time - last_time;
  260. last_time = end_time;
  261. poly4_pos = (poly4_pos + duration) % poly4_len;
  262. poly5_pos = (poly5_pos + duration) % poly5_len;
  263. polym_pos += duration; // will get %'d on next call
  264. }
  265. void Sap_Apu::write_data( blip_time_t time, unsigned addr, int data )
  266. {
  267. run_until( time );
  268. int i = (addr ^ 0xD200) >> 1;
  269. if ( i < osc_count )
  270. {
  271. oscs [i].regs [addr & 1] = data;
  272. }
  273. else if ( addr == 0xD208 )
  274. {
  275. control = data;
  276. }
  277. else if ( addr == 0xD209 )
  278. {
  279. oscs [0].delay = 0;
  280. oscs [1].delay = 0;
  281. oscs [2].delay = 0;
  282. oscs [3].delay = 0;
  283. }
  284. /*
  285. // TODO: are polynomials reset in this case?
  286. else if ( addr == 0xD20F )
  287. {
  288. if ( (data & 3) == 0 )
  289. polym_pos = 0;
  290. }
  291. */
  292. }
  293. void Sap_Apu::end_frame( blip_time_t end_time )
  294. {
  295. if ( end_time > last_time )
  296. run_until( end_time );
  297. last_time -= end_time;
  298. }