snes.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <../base.hpp>
  2. #include <../chip/chip.hpp>
  3. #include <../cart/cart.hpp>
  4. #define SNES_CPP
  5. SNES snes;
  6. BUSCORE bus;
  7. CPUCORE cpu;
  8. SMPCORE smp;
  9. DSPCORE dsp;
  10. PPUCORE ppu;
  11. BSXBase bsxbase;
  12. BSXCart bsxcart;
  13. BSXFlash bsxflash;
  14. SRTC srtc;
  15. SDD1 sdd1;
  16. SPC7110 spc7110;
  17. Cx4 cx4;
  18. DSP1 dsp1;
  19. DSP2 dsp2;
  20. DSP3 dsp3;
  21. DSP4 dsp4;
  22. OBC1 obc1;
  23. ST010 st010;
  24. CMMIO cmmio;
  25. #include "scheduler/scheduler.cpp"
  26. #include "tracer/tracer.cpp"
  27. #include "video/video.cpp"
  28. #include "audio/audio.cpp"
  29. #include "input/input.cpp"
  30. void SNES::run() {
  31. }
  32. void SNES::runtoframe() {
  33. scheduler.enter();
  34. }
  35. void SNES::init() {
  36. bsxbase.init();
  37. bsxcart.init();
  38. bsxflash.init();
  39. srtc.init();
  40. sdd1.init();
  41. spc7110.init();
  42. cx4.init();
  43. dsp1.init();
  44. dsp2.init();
  45. dsp3.init();
  46. dsp4.init();
  47. obc1.init();
  48. st010.init();
  49. video.init();
  50. audio.init();
  51. input.init();
  52. cmmio.init();
  53. snesinterface.init();
  54. }
  55. void SNES::term() {
  56. snesinterface.term();
  57. }
  58. void SNES::power() {
  59. snes_region = max(0, min(2, snes.config.region));
  60. snes_expansion = max(0, min(1, snes.config.expansion_port));
  61. if(snes_region == Autodetect) {
  62. snes_region = (cartridge.region() == Cartridge::NTSC ? NTSC : PAL);
  63. }
  64. scheduler.init();
  65. ppu.PPUcounter::reset();
  66. cpu.power();
  67. smp.power();
  68. dsp.power();
  69. ppu.power();
  70. bus.power();
  71. if(expansion() == ExpansionBSX) bsxbase.power();
  72. if(cartridge.mode() == Cartridge::ModeBsx) bsxcart.power();
  73. if(cartridge.bsx_flash_loaded()) bsxflash.power();
  74. if(cartridge.has_srtc()) srtc.power();
  75. if(cartridge.has_sdd1()) sdd1.power();
  76. if(cartridge.has_spc7110()) spc7110.power();
  77. if(cartridge.has_cx4()) cx4.power();
  78. if(cartridge.has_dsp1()) dsp1.power();
  79. if(cartridge.has_dsp2()) dsp2.power();
  80. if(cartridge.has_dsp3()) dsp3.power();
  81. if(cartridge.has_dsp4()) dsp4.power();
  82. if(cartridge.has_obc1()) obc1.power();
  83. if(cartridge.has_st010()) st010.power();
  84. cmmio.power();
  85. for(unsigned i = 0x2100; i <= 0x213f; i++) memory::mmio.map(i, ppu);
  86. for(unsigned i = 0x2140; i <= 0x217f; i++) memory::mmio.map(i, cpu);
  87. for(unsigned i = 0x2180; i <= 0x2183; i++) memory::mmio.map(i, cpu);
  88. for(unsigned i = 0x4016; i <= 0x4017; i++) memory::mmio.map(i, cpu);
  89. for(unsigned i = 0x4200; i <= 0x421f; i++) memory::mmio.map(i, cpu);
  90. for(unsigned i = 0x4300; i <= 0x437f; i++) memory::mmio.map(i, cpu);
  91. if(expansion() == ExpansionBSX) bsxbase.enable();
  92. if(cartridge.mode() == Cartridge::ModeBsx) bsxcart.enable();
  93. if(cartridge.bsx_flash_loaded()) bsxflash.enable();
  94. if(cartridge.has_srtc()) srtc.enable();
  95. if(cartridge.has_sdd1()) sdd1.enable();
  96. if(cartridge.has_spc7110()) spc7110.enable();
  97. if(cartridge.has_cx4()) cx4.enable();
  98. if(cartridge.has_dsp1()) dsp1.enable();
  99. if(cartridge.has_dsp2()) dsp2.enable();
  100. if(cartridge.has_dsp3()) dsp3.enable();
  101. if(cartridge.has_dsp4()) dsp4.enable();
  102. if(cartridge.has_obc1()) obc1.enable();
  103. if(cartridge.has_st010()) st010.enable();
  104. cmmio.enable();
  105. input.port_set_device(0, snes.config.controller_port1);
  106. input.port_set_device(1, snes.config.controller_port2);
  107. input.update();
  108. video.update();
  109. }
  110. void SNES::reset() {
  111. scheduler.init();
  112. ppu.PPUcounter::reset();
  113. cpu.reset();
  114. smp.reset();
  115. dsp.reset();
  116. ppu.reset();
  117. bus.reset();
  118. if(expansion() == ExpansionBSX) bsxbase.reset();
  119. if(cartridge.mode() == Cartridge::ModeBsx) bsxcart.reset();
  120. if(cartridge.bsx_flash_loaded()) bsxflash.reset();
  121. if(cartridge.has_srtc()) srtc.reset();
  122. if(cartridge.has_sdd1()) sdd1.reset();
  123. if(cartridge.has_spc7110()) spc7110.reset();
  124. if(cartridge.has_cx4()) cx4.reset();
  125. if(cartridge.has_dsp1()) dsp1.reset();
  126. if(cartridge.has_dsp2()) dsp2.reset();
  127. if(cartridge.has_dsp3()) dsp3.reset();
  128. if(cartridge.has_dsp4()) dsp4.reset();
  129. if(cartridge.has_obc1()) obc1.reset();
  130. if(cartridge.has_st010()) st010.reset();
  131. input.port_set_device(0, snes.config.controller_port1);
  132. input.port_set_device(1, snes.config.controller_port2);
  133. input.update();
  134. video.update();
  135. }
  136. void SNES::scanline() {
  137. video.scanline();
  138. if(ppu.vcounter() == 241) {
  139. input.update();
  140. video.update();
  141. scheduler.exit();
  142. }
  143. }
  144. void SNES::frame() {
  145. }
  146. SNES::Region SNES::region() const {
  147. return (SNES::Region)snes_region;
  148. }
  149. SNES::ExpansionPortDevice SNES::expansion() const {
  150. return (SNES::ExpansionPortDevice)snes_expansion;
  151. }
  152. SNES::SNES() : snes_region(NTSC), snes_expansion(ExpansionNone) {
  153. config.controller_port1 = Input::DeviceJoypad;
  154. config.controller_port2 = Input::DeviceJoypad;
  155. config.expansion_port = ExpansionBSX;
  156. config.region = Autodetect;
  157. config.file.autodetect_type = false;
  158. config.file.bypass_patch_crc32 = false;
  159. config.path.base = "";
  160. config.path.user = "";
  161. config.path.current = "";
  162. config.path.rom = "";
  163. config.path.save = "";
  164. config.path.patch = "";
  165. config.path.cheat = "";
  166. config.path.data = "";
  167. config.path.bsx = "";
  168. config.path.st = "";
  169. config.cpu.version = 2;
  170. config.cpu.ntsc_clock_rate = 21477272;
  171. config.cpu.pal_clock_rate = 21281370;
  172. config.cpu.alu_mul_delay = 2;
  173. config.cpu.alu_div_delay = 2;
  174. config.cpu.wram_init_value = 0x55;
  175. config.smp.ntsc_clock_rate = 32041 * 768;
  176. config.smp.pal_clock_rate = 32041 * 768;
  177. config.ppu1.version = 1;
  178. config.ppu2.version = 3;
  179. }