Gb_Oscs.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // Gb_Osc
  16. void Gb_Osc::reset()
  17. {
  18. delay = 0;
  19. last_amp = 0;
  20. length = 0;
  21. output_select = 3;
  22. output = outputs [output_select];
  23. }
  24. void Gb_Osc::clock_length()
  25. {
  26. if ( (regs [4] & len_enabled_mask) && length )
  27. length--;
  28. }
  29. // Gb_Env
  30. void Gb_Env::clock_envelope()
  31. {
  32. if ( env_delay && !--env_delay )
  33. {
  34. env_delay = regs [2] & 7;
  35. int v = volume - 1 + (regs [2] >> 2 & 2);
  36. if ( (unsigned) v < 15 )
  37. volume = v;
  38. }
  39. }
  40. bool Gb_Env::write_register( int reg, int data )
  41. {
  42. switch ( reg )
  43. {
  44. case 1:
  45. length = 64 - (regs [1] & 0x3F);
  46. break;
  47. case 2:
  48. if ( !(data >> 4) )
  49. enabled = false;
  50. break;
  51. case 4:
  52. if ( data & trigger )
  53. {
  54. env_delay = regs [2] & 7;
  55. volume = regs [2] >> 4;
  56. enabled = true;
  57. if ( length == 0 )
  58. length = 64;
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. // Gb_Square
  65. void Gb_Square::reset()
  66. {
  67. phase = 0;
  68. sweep_freq = 0;
  69. sweep_delay = 0;
  70. Gb_Env::reset();
  71. }
  72. void Gb_Square::clock_sweep()
  73. {
  74. int sweep_period = (regs [0] & period_mask) >> 4;
  75. if ( sweep_period && sweep_delay && !--sweep_delay )
  76. {
  77. sweep_delay = sweep_period;
  78. regs [3] = sweep_freq & 0xFF;
  79. regs [4] = (regs [4] & ~0x07) | (sweep_freq >> 8 & 0x07);
  80. int offset = sweep_freq >> (regs [0] & shift_mask);
  81. if ( regs [0] & 0x08 )
  82. offset = -offset;
  83. sweep_freq += offset;
  84. if ( sweep_freq < 0 )
  85. {
  86. sweep_freq = 0;
  87. }
  88. else if ( sweep_freq >= 2048 )
  89. {
  90. sweep_delay = 0; // don't modify channel frequency any further
  91. sweep_freq = 2048; // silence sound immediately
  92. }
  93. }
  94. }
  95. void Gb_Square::run( blip_time_t time, blip_time_t end_time, int playing )
  96. {
  97. if ( sweep_freq == 2048 )
  98. playing = false;
  99. static unsigned char const table [4] = { 1, 2, 4, 6 };
  100. int const duty = table [regs [1] >> 6];
  101. int amp = volume & playing;
  102. if ( phase >= duty )
  103. amp = -amp;
  104. int frequency = this->frequency();
  105. if ( unsigned (frequency - 1) > 2040 ) // frequency < 1 || frequency > 2041
  106. {
  107. // really high frequency results in DC at half volume
  108. amp = volume >> 1;
  109. playing = false;
  110. }
  111. {
  112. int delta = amp - last_amp;
  113. if ( delta )
  114. {
  115. last_amp = amp;
  116. synth->offset( time, delta, output );
  117. }
  118. }
  119. time += delay;
  120. if ( !playing )
  121. time = end_time;
  122. if ( time < end_time )
  123. {
  124. int const period = (2048 - frequency) * 4;
  125. Blip_Buffer* const output = this->output;
  126. int phase = this->phase;
  127. int delta = amp * 2;
  128. do
  129. {
  130. phase = (phase + 1) & 7;
  131. if ( phase == 0 || phase == duty )
  132. {
  133. delta = -delta;
  134. synth->offset_inline( time, delta, output );
  135. }
  136. time += period;
  137. }
  138. while ( time < end_time );
  139. this->phase = phase;
  140. last_amp = delta >> 1;
  141. }
  142. delay = time - end_time;
  143. }
  144. // Gb_Noise
  145. void Gb_Noise::run( blip_time_t time, blip_time_t end_time, int playing )
  146. {
  147. int amp = volume & playing;
  148. int tap = 13 - (regs [3] & 8);
  149. if ( bits >> tap & 2 )
  150. amp = -amp;
  151. {
  152. int delta = amp - last_amp;
  153. if ( delta )
  154. {
  155. last_amp = amp;
  156. synth->offset( time, delta, output );
  157. }
  158. }
  159. time += delay;
  160. if ( !playing )
  161. time = end_time;
  162. if ( time < end_time )
  163. {
  164. static unsigned char const table [8] = { 8, 16, 32, 48, 64, 80, 96, 112 };
  165. int period = table [regs [3] & 7] << (regs [3] >> 4);
  166. // keep parallel resampled time to eliminate time conversion in the loop
  167. Blip_Buffer* const output = this->output;
  168. const blip_resampled_time_t resampled_period =
  169. output->resampled_duration( period );
  170. blip_resampled_time_t resampled_time = output->resampled_time( time );
  171. unsigned bits = this->bits;
  172. int delta = amp * 2;
  173. do
  174. {
  175. unsigned changed = (bits >> tap) + 1;
  176. time += period;
  177. bits <<= 1;
  178. if ( changed & 2 )
  179. {
  180. delta = -delta;
  181. bits |= 1;
  182. synth->offset_resampled( resampled_time, delta, output );
  183. }
  184. resampled_time += resampled_period;
  185. }
  186. while ( time < end_time );
  187. this->bits = bits;
  188. last_amp = delta >> 1;
  189. }
  190. delay = time - end_time;
  191. }
  192. // Gb_Wave
  193. inline void Gb_Wave::write_register( int reg, int data )
  194. {
  195. switch ( reg )
  196. {
  197. case 0:
  198. if ( !(data & 0x80) )
  199. enabled = false;
  200. break;
  201. case 1:
  202. length = 256 - regs [1];
  203. break;
  204. case 2:
  205. volume = data >> 5 & 3;
  206. break;
  207. case 4:
  208. if ( data & trigger & regs [0] )
  209. {
  210. wave_pos = 0;
  211. enabled = true;
  212. if ( length == 0 )
  213. length = 256;
  214. }
  215. }
  216. }
  217. void Gb_Wave::run( blip_time_t time, blip_time_t end_time, int playing )
  218. {
  219. int volume_shift = (volume - 1) & 7; // volume = 0 causes shift = 7
  220. int frequency;
  221. {
  222. int amp = (wave [wave_pos] >> volume_shift & playing) * 2;
  223. frequency = this->frequency();
  224. if ( unsigned (frequency - 1) > 2044 ) // frequency < 1 || frequency > 2045
  225. {
  226. amp = 30 >> volume_shift & playing;
  227. playing = false;
  228. }
  229. int delta = amp - last_amp;
  230. if ( delta )
  231. {
  232. last_amp = amp;
  233. synth->offset( time, delta, output );
  234. }
  235. }
  236. time += delay;
  237. if ( !playing )
  238. time = end_time;
  239. if ( time < end_time )
  240. {
  241. Blip_Buffer* const output = this->output;
  242. int const period = (2048 - frequency) * 2;
  243. int wave_pos = (this->wave_pos + 1) & (wave_size - 1);
  244. do
  245. {
  246. int amp = (wave [wave_pos] >> volume_shift) * 2;
  247. wave_pos = (wave_pos + 1) & (wave_size - 1);
  248. int delta = amp - last_amp;
  249. if ( delta )
  250. {
  251. last_amp = amp;
  252. synth->offset_inline( time, delta, output );
  253. }
  254. time += period;
  255. }
  256. while ( time < end_time );
  257. this->wave_pos = (wave_pos - 1) & (wave_size - 1);
  258. }
  259. delay = time - end_time;
  260. }
  261. // Gb_Apu::write_osc
  262. void Gb_Apu::write_osc( int index, int reg, int data )
  263. {
  264. reg -= index * 5;
  265. Gb_Square* sq = &square2;
  266. switch ( index )
  267. {
  268. case 0:
  269. sq = &square1;
  270. case 1:
  271. if ( sq->write_register( reg, data ) && index == 0 )
  272. {
  273. square1.sweep_freq = square1.frequency();
  274. if ( (regs [0] & sq->period_mask) && (regs [0] & sq->shift_mask) )
  275. {
  276. square1.sweep_delay = 1; // cause sweep to recalculate now
  277. square1.clock_sweep();
  278. }
  279. }
  280. break;
  281. case 2:
  282. wave.write_register( reg, data );
  283. break;
  284. case 3:
  285. if ( noise.write_register( reg, data ) )
  286. noise.bits = 0x7FFF;
  287. }
  288. }