Gb_Apu.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Gb_Snd_Emu 0.1.5. http://www.slack.net/~ant/
  2. #include "Gb_Apu.h"
  3. #include <string.h>
  4. /* Copyright (C) 2003-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. unsigned const vol_reg = 0xFF24;
  16. unsigned const status_reg = 0xFF26;
  17. Gb_Apu::Gb_Apu()
  18. {
  19. square1.synth = &square_synth;
  20. square2.synth = &square_synth;
  21. wave.synth = &other_synth;
  22. noise.synth = &other_synth;
  23. oscs [0] = &square1;
  24. oscs [1] = &square2;
  25. oscs [2] = &wave;
  26. oscs [3] = &noise;
  27. for ( int i = 0; i < osc_count; i++ )
  28. {
  29. Gb_Osc& osc = *oscs [i];
  30. osc.regs = &regs [i * 5];
  31. osc.output = 0;
  32. osc.outputs [0] = 0;
  33. osc.outputs [1] = 0;
  34. osc.outputs [2] = 0;
  35. osc.outputs [3] = 0;
  36. }
  37. set_tempo( 1.0 );
  38. volume( 1.0 );
  39. reset();
  40. }
  41. void Gb_Apu::treble_eq( const blip_eq_t& eq )
  42. {
  43. square_synth.treble_eq( eq );
  44. other_synth.treble_eq( eq );
  45. }
  46. void Gb_Apu::osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right )
  47. {
  48. require( (unsigned) index < osc_count );
  49. require( (center && left && right) || (!center && !left && !right) );
  50. Gb_Osc& osc = *oscs [index];
  51. osc.outputs [1] = right;
  52. osc.outputs [2] = left;
  53. osc.outputs [3] = center;
  54. osc.output = osc.outputs [osc.output_select];
  55. }
  56. void Gb_Apu::output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right )
  57. {
  58. for ( int i = 0; i < osc_count; i++ )
  59. osc_output( i, center, left, right );
  60. }
  61. void Gb_Apu::update_volume()
  62. {
  63. // TODO: doesn't handle differing left/right global volume (support would
  64. // require modification to all oscillator code)
  65. int data = regs [vol_reg - start_addr];
  66. double vol = (max( data & 7, data >> 4 & 7 ) + 1) * volume_unit;
  67. square_synth.volume( vol );
  68. other_synth.volume( vol );
  69. }
  70. static unsigned char const powerup_regs [0x20] = {
  71. 0x80,0x3F,0x00,0xFF,0xBF, // square 1
  72. 0xFF,0x3F,0x00,0xFF,0xBF, // square 2
  73. 0x7F,0xFF,0x9F,0xFF,0xBF, // wave
  74. 0xFF,0xFF,0x00,0x00,0xBF, // noise
  75. 0x00, // left/right enables
  76. 0x77, // master volume
  77. 0x80, // power
  78. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  79. };
  80. void Gb_Apu::set_tempo( double t )
  81. {
  82. frame_period = 4194304 / 256; // 256 Hz
  83. if ( t != 1.0 )
  84. frame_period = blip_time_t (frame_period / t);
  85. }
  86. void Gb_Apu::reset()
  87. {
  88. next_frame_time = 0;
  89. last_time = 0;
  90. frame_count = 0;
  91. square1.reset();
  92. square2.reset();
  93. wave.reset();
  94. noise.reset();
  95. noise.bits = 1;
  96. wave.wave_pos = 0;
  97. // avoid click at beginning
  98. regs [vol_reg - start_addr] = 0x77;
  99. update_volume();
  100. regs [status_reg - start_addr] = 0x01; // force power
  101. write_register( 0, status_reg, 0x00 );
  102. static unsigned char const initial_wave [] = {
  103. 0x84,0x40,0x43,0xAA,0x2D,0x78,0x92,0x3C, // wave table
  104. 0x60,0x59,0x59,0xB0,0x34,0xB8,0x2E,0xDA
  105. };
  106. memcpy( wave.wave, initial_wave, sizeof wave.wave );
  107. }
  108. void Gb_Apu::run_until( blip_time_t end_time )
  109. {
  110. require( end_time >= last_time ); // end_time must not be before previous time
  111. if ( end_time == last_time )
  112. return;
  113. while ( true )
  114. {
  115. blip_time_t time = next_frame_time;
  116. if ( time > end_time )
  117. time = end_time;
  118. // run oscillators
  119. for ( int i = 0; i < osc_count; ++i )
  120. {
  121. Gb_Osc& osc = *oscs [i];
  122. if ( osc.output )
  123. {
  124. osc.output->set_modified(); // TODO: misses optimization opportunities?
  125. int playing = false;
  126. if ( osc.enabled && osc.volume &&
  127. (!(osc.regs [4] & osc.len_enabled_mask) || osc.length) )
  128. playing = -1;
  129. switch ( i )
  130. {
  131. case 0: square1.run( last_time, time, playing ); break;
  132. case 1: square2.run( last_time, time, playing ); break;
  133. case 2: wave .run( last_time, time, playing ); break;
  134. case 3: noise .run( last_time, time, playing ); break;
  135. }
  136. }
  137. }
  138. last_time = time;
  139. if ( time == end_time )
  140. break;
  141. next_frame_time += frame_period;
  142. // 256 Hz actions
  143. square1.clock_length();
  144. square2.clock_length();
  145. wave.clock_length();
  146. noise.clock_length();
  147. frame_count = (frame_count + 1) & 3;
  148. if ( frame_count == 0 )
  149. {
  150. // 64 Hz actions
  151. square1.clock_envelope();
  152. square2.clock_envelope();
  153. noise.clock_envelope();
  154. }
  155. if ( frame_count & 1 )
  156. square1.clock_sweep(); // 128 Hz action
  157. }
  158. }
  159. void Gb_Apu::end_frame( blip_time_t end_time )
  160. {
  161. if ( end_time > last_time )
  162. run_until( end_time );
  163. assert( next_frame_time >= end_time );
  164. next_frame_time -= end_time;
  165. assert( last_time >= end_time );
  166. last_time -= end_time;
  167. }
  168. void Gb_Apu::write_register( blip_time_t time, unsigned addr, int data )
  169. {
  170. require( (unsigned) data < 0x100 );
  171. int reg = addr - start_addr;
  172. if ( (unsigned) reg >= register_count )
  173. return;
  174. run_until( time );
  175. int old_reg = regs [reg];
  176. regs [reg] = data;
  177. if ( addr < vol_reg )
  178. {
  179. write_osc( reg / 5, reg, data );
  180. }
  181. else if ( addr == vol_reg && data != old_reg ) // global volume
  182. {
  183. // return all oscs to 0
  184. for ( int i = 0; i < osc_count; i++ )
  185. {
  186. Gb_Osc& osc = *oscs [i];
  187. int amp = osc.last_amp;
  188. osc.last_amp = 0;
  189. if ( amp && osc.enabled && osc.output )
  190. other_synth.offset( time, -amp, osc.output );
  191. }
  192. if ( wave.outputs [3] )
  193. other_synth.offset( time, 30, wave.outputs [3] );
  194. update_volume();
  195. if ( wave.outputs [3] )
  196. other_synth.offset( time, -30, wave.outputs [3] );
  197. // oscs will update with new amplitude when next run
  198. }
  199. else if ( addr == 0xFF25 || addr == status_reg )
  200. {
  201. int mask = (regs [status_reg - start_addr] & 0x80) ? ~0 : 0;
  202. int flags = regs [0xFF25 - start_addr] & mask;
  203. // left/right assignments
  204. for ( int i = 0; i < osc_count; i++ )
  205. {
  206. Gb_Osc& osc = *oscs [i];
  207. osc.enabled &= mask;
  208. int bits = flags >> i;
  209. Blip_Buffer* old_output = osc.output;
  210. osc.output_select = (bits >> 3 & 2) | (bits & 1);
  211. osc.output = osc.outputs [osc.output_select];
  212. if ( osc.output != old_output )
  213. {
  214. int amp = osc.last_amp;
  215. osc.last_amp = 0;
  216. if ( amp && old_output )
  217. other_synth.offset( time, -amp, old_output );
  218. }
  219. }
  220. if ( addr == status_reg && data != old_reg )
  221. {
  222. if ( !(data & 0x80) )
  223. {
  224. for ( unsigned i = 0; i < sizeof powerup_regs; i++ )
  225. {
  226. if ( i != status_reg - start_addr )
  227. write_register( time, i + start_addr, powerup_regs [i] );
  228. }
  229. }
  230. else
  231. {
  232. //debug_printf( "APU powered on\n" );
  233. }
  234. }
  235. }
  236. else if ( addr >= 0xFF30 )
  237. {
  238. int index = (addr & 0x0F) * 2;
  239. wave.wave [index] = data >> 4;
  240. wave.wave [index + 1] = data & 0x0F;
  241. }
  242. }
  243. int Gb_Apu::read_register( blip_time_t time, unsigned addr )
  244. {
  245. run_until( time );
  246. int index = addr - start_addr;
  247. require( (unsigned) index < register_count );
  248. int data = regs [index];
  249. if ( addr == status_reg )
  250. {
  251. data = (data & 0x80) | 0x70;
  252. for ( int i = 0; i < osc_count; i++ )
  253. {
  254. const Gb_Osc& osc = *oscs [i];
  255. if ( osc.enabled && (osc.length || !(osc.regs [4] & osc.len_enabled_mask)) )
  256. data |= 1 << i;
  257. }
  258. }
  259. return data;
  260. }