snes.cpp 5.5 KB

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