/gme/Gbs_Emu.cpp

http://game-music-emu.googlecode.com/ · C++ · 289 lines · 214 code · 55 blank · 20 comment · 25 complexity · 2c087170647d68494540888b215230d1 MD5 · raw file

  1. // Game_Music_Emu 0.5.5. http://www.slack.net/~ant/
  2. #include "Gbs_Emu.h"
  3. #include "blargg_endian.h"
  4. #include <string.h>
  5. /* Copyright (C) 2003-2006 Shay Green. This module is free software; you
  6. can redistribute it and/or modify it under the terms of the GNU Lesser
  7. General Public License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version. This
  9. module is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. details. You should have received a copy of the GNU Lesser General Public
  13. License along with this module; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
  15. #include "blargg_source.h"
  16. Gbs_Emu::equalizer_t const Gbs_Emu::handheld_eq = { -47.0, 2000 };
  17. Gbs_Emu::equalizer_t const Gbs_Emu::headphones_eq = { 0.0, 300 };
  18. Gbs_Emu::Gbs_Emu()
  19. {
  20. set_type( gme_gbs_type );
  21. static const char* const names [Gb_Apu::osc_count] = {
  22. "Square 1", "Square 2", "Wave", "Noise"
  23. };
  24. set_voice_names( names );
  25. static int const types [Gb_Apu::osc_count] = {
  26. wave_type | 1, wave_type | 2, wave_type | 0, mixed_type | 0
  27. };
  28. set_voice_types( types );
  29. set_silence_lookahead( 6 );
  30. set_max_initial_silence( 21 );
  31. set_gain( 1.2 );
  32. static equalizer_t const eq = { -1.0, 120 };
  33. set_equalizer( eq );
  34. }
  35. Gbs_Emu::~Gbs_Emu() { }
  36. void Gbs_Emu::unload()
  37. {
  38. rom.clear();
  39. Music_Emu::unload();
  40. }
  41. // Track info
  42. static void copy_gbs_fields( Gbs_Emu::header_t const& h, track_info_t* out )
  43. {
  44. GME_COPY_FIELD( h, out, game );
  45. GME_COPY_FIELD( h, out, author );
  46. GME_COPY_FIELD( h, out, copyright );
  47. }
  48. blargg_err_t Gbs_Emu::track_info_( track_info_t* out, int ) const
  49. {
  50. copy_gbs_fields( header_, out );
  51. return 0;
  52. }
  53. static blargg_err_t check_gbs_header( void const* header )
  54. {
  55. if ( memcmp( header, "GBS", 3 ) )
  56. return gme_wrong_file_type;
  57. return 0;
  58. }
  59. struct Gbs_File : Gme_Info_
  60. {
  61. Gbs_Emu::header_t h;
  62. Gbs_File() { set_type( gme_gbs_type ); }
  63. blargg_err_t load_( Data_Reader& in )
  64. {
  65. blargg_err_t err = in.read( &h, Gbs_Emu::header_size );
  66. if ( err )
  67. return (err == in.eof_error ? gme_wrong_file_type : err);
  68. set_track_count( h.track_count );
  69. return check_gbs_header( &h );
  70. }
  71. blargg_err_t track_info_( track_info_t* out, int ) const
  72. {
  73. copy_gbs_fields( h, out );
  74. return 0;
  75. }
  76. };
  77. static Music_Emu* new_gbs_emu () { return BLARGG_NEW Gbs_Emu ; }
  78. static Music_Emu* new_gbs_file() { return BLARGG_NEW Gbs_File; }
  79. static gme_type_t_ const gme_gbs_type_ = { "Game Boy", 0, &new_gbs_emu, &new_gbs_file, "GBS", 1 };
  80. gme_type_t const gme_gbs_type = &gme_gbs_type_;
  81. // Setup
  82. blargg_err_t Gbs_Emu::load_( Data_Reader& in )
  83. {
  84. assert( offsetof (header_t,copyright [32]) == header_size );
  85. RETURN_ERR( rom.load( in, header_size, &header_, 0 ) );
  86. set_track_count( header_.track_count );
  87. RETURN_ERR( check_gbs_header( &header_ ) );
  88. if ( header_.vers != 1 )
  89. set_warning( "Unknown file version" );
  90. if ( header_.timer_mode & 0x78 )
  91. set_warning( "Invalid timer mode" );
  92. unsigned load_addr = get_le16( header_.load_addr );
  93. if ( (header_.load_addr [1] | header_.init_addr [1] | header_.play_addr [1]) > 0x7F ||
  94. load_addr < 0x400 )
  95. set_warning( "Invalid load/init/play address" );
  96. set_voice_count( Gb_Apu::osc_count );
  97. apu.volume( gain() );
  98. return setup_buffer( 4194304 );
  99. }
  100. void Gbs_Emu::update_eq( blip_eq_t const& eq )
  101. {
  102. apu.treble_eq( eq );
  103. }
  104. void Gbs_Emu::set_voice( int i, Blip_Buffer* c, Blip_Buffer* l, Blip_Buffer* r )
  105. {
  106. apu.osc_output( i, c, l, r );
  107. }
  108. // Emulation
  109. // see gb_cpu_io.h for read/write functions
  110. void Gbs_Emu::set_bank( int n )
  111. {
  112. blargg_long addr = rom.mask_addr( n * (blargg_long) bank_size );
  113. if ( addr == 0 && rom.size() > bank_size )
  114. {
  115. // TODO: what is the correct behavior? Current Game & Watch Gallery
  116. // rip requires that this have no effect or set to bank 1.
  117. //debug_printf( "Selected ROM bank 0\n" );
  118. return;
  119. //n = 1;
  120. }
  121. cpu::map_code( bank_size, bank_size, rom.at_addr( addr ) );
  122. }
  123. void Gbs_Emu::update_timer()
  124. {
  125. if ( header_.timer_mode & 0x04 )
  126. {
  127. static byte const rates [4] = { 10, 4, 6, 8 };
  128. int shift = rates [ram [hi_page + 7] & 3] - (header_.timer_mode >> 7);
  129. play_period = (256L - ram [hi_page + 6]) << shift;
  130. }
  131. else
  132. {
  133. play_period = 70224; // 59.73 Hz
  134. }
  135. if ( tempo() != 1.0 )
  136. play_period = blip_time_t (play_period / tempo());
  137. }
  138. static BOOST::uint8_t const sound_data [Gb_Apu::register_count] = {
  139. 0x80, 0xBF, 0x00, 0x00, 0xBF, // square 1
  140. 0x00, 0x3F, 0x00, 0x00, 0xBF, // square 2
  141. 0x7F, 0xFF, 0x9F, 0x00, 0xBF, // wave
  142. 0x00, 0xFF, 0x00, 0x00, 0xBF, // noise
  143. 0x77, 0xF3, 0xF1, // vin/volume, status, power mode
  144. 0, 0, 0, 0, 0, 0, 0, 0, 0, // unused
  145. 0xAC, 0xDD, 0xDA, 0x48, 0x36, 0x02, 0xCF, 0x16, // waveform data
  146. 0x2C, 0x04, 0xE5, 0x2C, 0xAC, 0xDD, 0xDA, 0x48
  147. };
  148. void Gbs_Emu::cpu_jsr( gb_addr_t addr )
  149. {
  150. check( cpu::r.sp == get_le16( header_.stack_ptr ) );
  151. cpu::r.pc = addr;
  152. cpu_write( --cpu::r.sp, idle_addr >> 8 );
  153. cpu_write( --cpu::r.sp, idle_addr&0xFF );
  154. }
  155. void Gbs_Emu::set_tempo_( double t )
  156. {
  157. apu.set_tempo( t );
  158. update_timer();
  159. }
  160. blargg_err_t Gbs_Emu::start_track_( int track )
  161. {
  162. RETURN_ERR( Classic_Emu::start_track_( track ) );
  163. memset( ram, 0, 0x4000 );
  164. memset( ram + 0x4000, 0xFF, 0x1F80 );
  165. memset( ram + 0x5F80, 0, sizeof ram - 0x5F80 );
  166. ram [hi_page] = 0; // joypad reads back as 0
  167. apu.reset();
  168. for ( int i = 0; i < (int) sizeof sound_data; i++ )
  169. apu.write_register( 0, i + apu.start_addr, sound_data [i] );
  170. unsigned load_addr = get_le16( header_.load_addr );
  171. rom.set_addr( load_addr );
  172. cpu::rst_base = load_addr;
  173. cpu::reset( rom.unmapped() );
  174. cpu::map_code( ram_addr, 0x10000 - ram_addr, ram );
  175. cpu::map_code( 0, bank_size, rom.at_addr( 0 ) );
  176. set_bank( rom.size() > bank_size );
  177. ram [hi_page + 6] = header_.timer_modulo;
  178. ram [hi_page + 7] = header_.timer_mode;
  179. update_timer();
  180. next_play = play_period;
  181. cpu::r.a = track;
  182. cpu::r.pc = idle_addr;
  183. cpu::r.sp = get_le16( header_.stack_ptr );
  184. cpu_time = 0;
  185. cpu_jsr( get_le16( header_.init_addr ) );
  186. return 0;
  187. }
  188. blargg_err_t Gbs_Emu::run_clocks( blip_time_t& duration, int )
  189. {
  190. cpu_time = 0;
  191. while ( cpu_time < duration )
  192. {
  193. long count = duration - cpu_time;
  194. cpu_time = duration;
  195. bool result = cpu::run( count );
  196. cpu_time -= cpu::remain();
  197. if ( result )
  198. {
  199. if ( cpu::r.pc == idle_addr )
  200. {
  201. if ( next_play > duration )
  202. {
  203. cpu_time = duration;
  204. break;
  205. }
  206. if ( cpu_time < next_play )
  207. cpu_time = next_play;
  208. next_play += play_period;
  209. cpu_jsr( get_le16( header_.play_addr ) );
  210. GME_FRAME_HOOK( this );
  211. // TODO: handle timer rates different than 60 Hz
  212. }
  213. else if ( cpu::r.pc > 0xFFFF )
  214. {
  215. debug_printf( "PC wrapped around\n" );
  216. cpu::r.pc &= 0xFFFF;
  217. }
  218. else
  219. {
  220. set_warning( "Emulation error (illegal/unsupported instruction)" );
  221. debug_printf( "Bad opcode $%.2x at $%.4x\n",
  222. (int) *cpu::get_code( cpu::r.pc ), (int) cpu::r.pc );
  223. cpu::r.pc = (cpu::r.pc + 1) & 0xFFFF;
  224. cpu_time += 6;
  225. }
  226. }
  227. }
  228. duration = cpu_time;
  229. next_play -= cpu_time;
  230. if ( next_play < 0 ) // could go negative if routine is taking too long to return
  231. next_play = 0;
  232. apu.end_frame( cpu_time );
  233. return 0;
  234. }