mmc3.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * MMC3 Mapper - The peTI-NESulator Project
  3. * mmc3.h
  4. *
  5. * Created by Manoël Trapier.
  6. * Copyright (c) 2002-2019 986-Studio.
  7. *
  8. */
  9. #include "mmc3.h"
  10. extern uint16_t ScanLine;
  11. uint16_t mmc3_command;
  12. uint8_t mmc3_irq_counter;
  13. uint8_t mmc3_irq_counter_reload;
  14. uint8_t mmc3_irq_enable;
  15. uint16_t mmc3_first_prom_page;
  16. uint16_t mmc3_second_prom_page;
  17. uint8_t mmc3_use_xor;
  18. uint8_t mmc3_last_vrom[6];
  19. uint8_t mmc3_last_prom[2];
  20. uint8_t mmc3_last_prom_switch;
  21. uint16_t dummy;
  22. void mmc3_MapperWrite80Hook(uint8_t addr, uint8_t Value);
  23. void mmc3_MapperWriteA0Hook(uint8_t addr, uint8_t Value);
  24. void mmc3_MapperWriteC0Hook(uint8_t addr, uint8_t Value);
  25. void mmc3_MapperWriteE0Hook(uint8_t addr, uint8_t Value);
  26. void mmc3_MapperDump(FILE *fp)
  27. {
  28. fprintf(fp, "MMC3: CMD:%d IC:%d IR:%d IE:%d FPP:0x%04X SPP:0x%04X UX:%d\n", mmc3_command, mmc3_irq_counter,
  29. mmc3_irq_counter_reload, mmc3_irq_enable, mmc3_first_prom_page, mmc3_second_prom_page, mmc3_use_xor);
  30. fprintf(fp, "MMC3: LV0:%d LV1:%d LV2:%d LV3:%d LV4:%d LV5:%d\n", mmc3_last_vrom[0], mmc3_last_vrom[1],
  31. mmc3_last_vrom[2], mmc3_last_vrom[3], mmc3_last_vrom[4], mmc3_last_vrom[5]);
  32. fprintf(fp, "MMC3: LP0:%d LP1:%d LPS:%d\n", mmc3_last_prom[0], mmc3_last_prom[1], mmc3_last_prom_switch);
  33. }
  34. int mmc3_InitMapper(NesCart *cart)
  35. {
  36. set_prom_bank_16k(0x8000, 0);
  37. set_prom_bank_16k(0xC000, GETLAST16KBANK(cart));
  38. if (Cart->VROMSize > 0)
  39. {
  40. set_vrom_bank_8k(0, 0x0000);
  41. }
  42. mmc3_command = -1;
  43. mmc3_irq_counter = -1;
  44. mmc3_irq_enable = 0;
  45. mmc3_irq_counter_reload = 0;
  46. mmc3_use_xor = 0x42;
  47. mmc3_last_prom_switch = 0x42;
  48. mmc3_last_prom[0] = 0;
  49. mmc3_last_prom[1] = 1;
  50. mmc3_last_vrom[0] = 0;
  51. mmc3_last_vrom[1] = 2;
  52. mmc3_last_vrom[2] = 3;
  53. mmc3_last_vrom[3] = 4;
  54. mmc3_last_vrom[4] = 5;
  55. mmc3_last_vrom[5] = 6;
  56. mmc3_first_prom_page = 0x8000;
  57. mmc3_second_prom_page = 0xA000;
  58. /* Register mapper write hook */
  59. set_page_wr_hook(0x80, mmc3_MapperWrite80Hook);
  60. set_page_writeable(0x80, true);
  61. set_page_wr_hook(0xA0, mmc3_MapperWriteA0Hook);
  62. set_page_writeable(0xA0, true);
  63. set_page_wr_hook(0xC0, mmc3_MapperWriteC0Hook);
  64. set_page_writeable(0xC0, true);
  65. set_page_wr_hook(0xE0, mmc3_MapperWriteE0Hook);
  66. set_page_writeable(0xE0, true);
  67. return 0;
  68. }
  69. void mmc3_MapperWrite80Hook(uint8_t addr, uint8_t Value)
  70. {
  71. //console_printf(Console_Default, "%s(0x%02X, 0x%02X)\n", __func__, addr, Value);
  72. if (addr > 0x01)
  73. {
  74. return;
  75. }
  76. if (!addr)
  77. {
  78. if ((Cart->VROMSize > 0) && (mmc3_use_xor != (Value & 0x80)))
  79. {
  80. if (Value & 0x80)
  81. {
  82. set_vrom_bank_1k(0x0000, mmc3_last_vrom[2]);
  83. set_vrom_bank_1k(0x0400, mmc3_last_vrom[3]);
  84. set_vrom_bank_1k(0x0800, mmc3_last_vrom[4]);
  85. set_vrom_bank_1k(0x0C00, mmc3_last_vrom[5]);
  86. set_vrom_bank_2k(0x1000, mmc3_last_vrom[0] >> 1);
  87. set_vrom_bank_2k(0x1800, mmc3_last_vrom[1] >> 1);
  88. //chr4,chr5,chr6,chr7,chr01,chr01+1,chr23,chr23+1
  89. }
  90. else
  91. {
  92. set_vrom_bank_2k(0x0000, mmc3_last_vrom[0] >> 1);
  93. set_vrom_bank_2k(0x0800, mmc3_last_vrom[1] >> 1);
  94. set_vrom_bank_1k(0x1000, mmc3_last_vrom[2]);
  95. set_vrom_bank_1k(0x1400, mmc3_last_vrom[3]);
  96. set_vrom_bank_1k(0x1800, mmc3_last_vrom[4]);
  97. set_vrom_bank_1k(0x1C00, mmc3_last_vrom[5]);
  98. //chr01,chr01+1,chr23,chr23+1,chr4,chr5,chr6,chr7
  99. }
  100. mmc3_use_xor = (Value & 0x80);
  101. }
  102. if (mmc3_last_prom_switch != (Value & 0x40))
  103. {
  104. if (!(Value & 0x40))
  105. {
  106. console_printf(Console_Default, "MMC3: Switch -> 8/A\n");
  107. mmc3_first_prom_page = 0x8000;
  108. mmc3_second_prom_page = 0xA000;
  109. set_prom_bank_8k(mmc3_first_prom_page, mmc3_last_prom[0]);
  110. set_prom_bank_8k(mmc3_second_prom_page, mmc3_last_prom[1]);
  111. set_prom_bank_8k(0xC000, GETLAST08KBANK(Cart) - 1);
  112. //set_prom_bank_8k(0xE000, GETLAST08KBANK(cart));
  113. //prg_bank(prg0,prg1,max_prg-1,max_prg);
  114. }
  115. else
  116. {
  117. console_printf(Console_Default, "MMC3: Switch -> C/A\n");
  118. mmc3_first_prom_page = 0xC000;
  119. mmc3_second_prom_page = 0xA000;
  120. set_prom_bank_8k(mmc3_first_prom_page, mmc3_last_prom[0]);
  121. set_prom_bank_8k(mmc3_second_prom_page, mmc3_last_prom[1]);
  122. set_prom_bank_8k(0x8000, GETLAST08KBANK(Cart) - 1);
  123. //prg_bank(max_prg-1,prg1,prg0,max_prg);
  124. }
  125. mmc3_last_prom_switch = (Value & 0x40);
  126. }
  127. mmc3_command = Value & 0x07;
  128. }
  129. else
  130. { /* 8001 */
  131. switch (mmc3_command)
  132. {
  133. default:
  134. case 0:
  135. case 1:
  136. case 2:
  137. case 3:
  138. case 4:
  139. case 5:
  140. if (Cart->VROMSize == 0)
  141. {
  142. return;
  143. }
  144. mmc3_last_vrom[mmc3_command] = Value;
  145. if (mmc3_use_xor)
  146. {
  147. set_vrom_bank_1k(0x0000, mmc3_last_vrom[2]);
  148. set_vrom_bank_1k(0x0400, mmc3_last_vrom[3]);
  149. set_vrom_bank_1k(0x0800, mmc3_last_vrom[4]);
  150. set_vrom_bank_1k(0x0C00, mmc3_last_vrom[5]);
  151. set_vrom_bank_2k(0x1000, mmc3_last_vrom[0] >> 1);
  152. set_vrom_bank_2k(0x1800, mmc3_last_vrom[1] >> 1);
  153. //chr4,chr5,chr6,chr7,chr01,chr01+1,chr23,chr23+1
  154. }
  155. else
  156. {
  157. set_vrom_bank_2k(0x0000, mmc3_last_vrom[0] >> 1);
  158. set_vrom_bank_2k(0x0800, mmc3_last_vrom[1] >> 1);
  159. set_vrom_bank_1k(0x1000, mmc3_last_vrom[2]);
  160. set_vrom_bank_1k(0x1400, mmc3_last_vrom[3]);
  161. set_vrom_bank_1k(0x1800, mmc3_last_vrom[4]);
  162. set_vrom_bank_1k(0x1C00, mmc3_last_vrom[5]);
  163. //chr01,chr01+1,chr23,chr23+1,chr4,chr5,chr6,chr7
  164. }
  165. break;
  166. case 6:
  167. mmc3_last_prom[0] = Value;
  168. set_prom_bank_8k(mmc3_first_prom_page, mmc3_last_prom[0]);
  169. break;
  170. case 7:
  171. mmc3_last_prom[1] = Value;
  172. set_prom_bank_8k(mmc3_second_prom_page, mmc3_last_prom[1]);
  173. break;
  174. }
  175. /*if(mmc3_use_xor)
  176. chr_bank(chr4,chr5,chr6,chr7,chr01,chr01+1,chr23,chr23+1);
  177. else
  178. chr_bank(chr01,chr01+1,chr23,chr23+1,chr4,chr5,chr6,chr7);*/
  179. }
  180. }
  181. void mmc3_MapperWriteA0Hook(uint8_t addr, uint8_t Value)
  182. {
  183. //console_printf(Console_Default, "%s(0x%02X, 0x%02X)\n", __func__, addr, Value);
  184. if (addr > 0x01)
  185. {
  186. return;
  187. }
  188. if (!addr)
  189. {
  190. //console_printf(Console_Default, "MMC3: Select mirroring (0xA000) : 0x%X\n",Value);
  191. if (Value & 0x1)
  192. {
  193. ppu_setMirroring(PPU_MIRROR_HORIZTAL);
  194. }
  195. else
  196. {
  197. ppu_setMirroring(PPU_MIRROR_VERTICAL);
  198. }
  199. }
  200. else
  201. {
  202. //console_printf(Console_Default, "MMC3: SaveRAM Toggle (0xA001) : 0x%X\n",Value);
  203. if (Value)
  204. {
  205. map_sram();
  206. }
  207. else
  208. {
  209. unmap_sram();
  210. }
  211. }
  212. }
  213. void mmc3_MapperWriteC0Hook(uint8_t addr, uint8_t Value)
  214. {
  215. //console_printf(Console_Default, "%s(0x%02X, 0x%02X)\n", __func__, addr, Value);
  216. if (addr > 0x01)
  217. {
  218. return;
  219. }
  220. if (!addr)
  221. {
  222. mmc3_irq_counter_reload = Value;
  223. mmc3_irq_counter = Value;
  224. //console_printf(Console_Default, "MMC3 IRQ[%d]: SetIRQ reload to %d\n", ScanLine, Value);
  225. }
  226. else
  227. { /* C001 */
  228. //console_printf(Console_Default, "MMC3: New tmp IRQ value (0xC001) : 0x%X\n",Value);
  229. //console_printf(Console_Default, "MMC3 IRQ[%d]: Reset IRQ counter to val %d [Value = %d]\n", ScanLine, mmc3_irq_counter_reload, Value);
  230. mmc3_irq_counter = Value;
  231. }
  232. }
  233. void mmc3_MapperWriteE0Hook(uint8_t addr, uint8_t Value)
  234. {
  235. //console_printf(Console_Default, "%s(0x%02X, 0x%02X)\n", __func__, addr, Value);
  236. if (addr > 0x01)
  237. {
  238. return;
  239. }
  240. if (!addr)
  241. {
  242. //console_printf(Console_Default, "MMC3: Writing to 0xE001 : 0x%X\n",Value);
  243. //console_printf(Console_Default, "MMC3 IRQ[%d]: IRQ disabled\n", ScanLine);
  244. mmc3_irq_enable = 0;
  245. //MapperWantIRQ = 1;
  246. // Add a way to raise an IRQ
  247. }
  248. else
  249. { /* E001 */
  250. //console_printf(Console_Default, "MMC3: Writing to 0xE001 : 0x%X\n",Value);
  251. //console_printf(Console_Default, "MMC3: IRQ Enabled (value : %d)\n",mmc3_irq_counter);
  252. //console_printf(Console_Default, "MMC3 IRQ[%d]: Enable IRQ\nr", ScanLine);
  253. mmc3_irq_enable = 1;
  254. }
  255. }
  256. int mmc3_MapperIRQ(int cycledone)
  257. {
  258. if (((cycledone > 0) && (cycledone < 241)) /*&&
  259. (ppu.ControlRegister2.b & (PPU_CR2_BGVISIBILITY | PPU_CR2_SPRTVISIBILITY)) == (PPU_CR2_BGVISIBILITY | PPU_CR2_SPRTVISIBILITY)*/)
  260. {
  261. if ((mmc3_irq_counter--) > 0)
  262. {
  263. return 0;
  264. }
  265. /* Load next counter position */
  266. mmc3_irq_counter = mmc3_irq_counter_reload;
  267. if (mmc3_irq_enable == 0)
  268. {
  269. return 0;
  270. }
  271. mmc3_irq_enable = 0;
  272. //console_printf(Console_Default, "MMC3 IRQ[%d]: Tick next at %d\n", ScanLine, mmc3_irq_counter_reload);
  273. return 1;
  274. }
  275. return 0;
  276. }