mmc1.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * MMC1 Mapper - The peTI-NESulator Project
  3. * mmc1.h
  4. *
  5. * Created by Manoël Trapier.
  6. * Copyright (c) 2002-2019 986-Studio.
  7. *
  8. */
  9. #include "mmc1.h"
  10. uint8_t MMC1_reg0;
  11. uint8_t MMC1_reg1;
  12. uint8_t MMC1_reg2;
  13. uint8_t MMC1_reg3;
  14. uint8_t mmc1_CurrentBank;
  15. #define MMC1_R0_MIRROR 0x01
  16. #define MMC1_R0_ONESCREEN 0x02
  17. #define MMC1_R0_PRGAREA 0x04
  18. #define MMC1_R0_PRGSIZE 0x08
  19. #define MMC1_R0_VROMSW 0x10
  20. #define MMC1_R0_RESET 0x80
  21. #define MMC1_R1_VROMB1 0x0F
  22. #define MMC1_R1_256KSEL 0x10
  23. #define MMC1_R1_RESET 0x80
  24. #define MMC1_R2_VROMB2 0x0F
  25. #define MMC1_R2_256KSEL 0x10
  26. #define MMC1_R2_RESET 0x80
  27. #define MMC1_R3_VROMB2 0x0F
  28. #define MMC1_R3_SAVECE 0x10
  29. #define MMC1_R3_RESET 0x80
  30. #define MMC1_REG0_DEFAULT (MMC1_R0_PRGSIZE | MMC1_R0_PRGAREA)
  31. #define MMC1_REG1_DEFAULT (0)
  32. #define MMC1_REG2_DEFAULT (0)
  33. #define MMC1_REG3_DEFAULT (0)
  34. void mmc1_MapperWriteReg0(register uint8_t Addr, register uint8_t Value);
  35. void mmc1_MapperWriteReg1(register uint8_t Addr, register uint8_t Value);
  36. void mmc1_MapperWriteReg2(register uint8_t Addr, register uint8_t Value);
  37. void mmc1_MapperWriteReg3(register uint8_t Addr, register uint8_t Value);
  38. void mmc1_MapperDump(FILE *fp)
  39. {
  40. fprintf(fp, "MMC1: r0:0x%02X r1:0x%02X r2:0x%02X r3:0x%02X\n", MMC1_reg0, MMC1_reg1, MMC1_reg2, MMC1_reg3);
  41. }
  42. int mmc1_InitMapper(NesCart *cart)
  43. {
  44. int i;
  45. MMC1_reg0 = MMC1_REG0_DEFAULT;
  46. MMC1_reg1 = MMC1_REG1_DEFAULT;
  47. MMC1_reg2 = MMC1_REG2_DEFAULT;
  48. MMC1_reg3 = MMC1_REG3_DEFAULT;
  49. set_prom_bank_16k(0x8000, 0);
  50. set_prom_bank_16k(0xC000, GETLAST16KBANK(cart));
  51. mmc1_CurrentBank = 0;
  52. if (cart->VROMSize > 0)
  53. {
  54. set_vrom_bank_4k(0x0000, 0);
  55. }
  56. /* Mapper should register itself for write hook */
  57. for (i = 0x80 ; i < 0xA0 ; i++)
  58. {
  59. set_page_wr_hook(i, mmc1_MapperWriteReg0);
  60. set_page_writeable(i, true);
  61. }
  62. for (i = 0xA0 ; i < 0xC0 ; i++)
  63. {
  64. set_page_wr_hook(i, mmc1_MapperWriteReg1);
  65. set_page_writeable(i, true);
  66. }
  67. for (i = 0xC0 ; i < 0xE0 ; i++)
  68. {
  69. set_page_wr_hook(i, mmc1_MapperWriteReg2);
  70. set_page_writeable(i, true);
  71. }
  72. for (i = 0xE0 ; i < 0x100 ; i++)
  73. {
  74. set_page_wr_hook(i, mmc1_MapperWriteReg3);
  75. set_page_writeable(i, true);
  76. }
  77. return 0;
  78. }
  79. /*
  80. Reg 0
  81. 8 : 1000
  82. 9 : 1001
  83. Reg 1
  84. A : 1010
  85. B : 1011
  86. Reg 2
  87. C : 1100
  88. D : 1101
  89. Reg 3
  90. E : 1110
  91. F : 1111
  92. ((Addr & 0x6000) >> 13) <- port number
  93. */
  94. #define MMC1_GetReg(_a) (((_a) & 0x6000) >> 13)
  95. /* (Val & 0x01) recuperation du bit */
  96. #define MMC1_GetBit(_v) ((_v) & 0x01)
  97. /* ( ( b & (1 << Bit)) | (v << Bit) ) Ajout du bit */
  98. #define MMC1_AddBit(_b, _v) ( ( (_b) & ~(1 << Bit)) | ( (_v) << Bit) )
  99. void mmc1_ApplyReg0Mod()
  100. {
  101. static uint8_t OldSwitchArea = MMC1_R0_PRGAREA;
  102. //console_printf(Console_Default, "Change to reg0 done ! (0x%x)\n\tMiror flag : %d\n\tOneScreen Flag : %d\n\tPRG Size : %d\n\tPRG Area : %d\n\tVROM Switch size : %d\n", MMC1_reg0, MMC1_reg0 & MMC1_R0_MIRROR, MMC1_reg0 & MMC1_R0_ONESCREEN, MMC1_reg0 & MMC1_R0_PRGAREA, MMC1_reg0 & MMC1_R0_PRGSIZE, MMC1_reg0 & MMC1_R0_VROMSW);
  103. switch (MMC1_reg0 & 0x03)
  104. {
  105. default:
  106. case 0:
  107. ppu_setScreenMode(PPU_SCMODE_SINGLE);
  108. ppu_setSingleScreen(PPU_SCREEN_000);
  109. break;
  110. case 1:
  111. ppu_setScreenMode(PPU_SCMODE_SINGLE);
  112. ppu_setSingleScreen(PPU_SCREEN_400);
  113. break;
  114. case 2:
  115. ppu_setScreenMode(PPU_SCMODE_NORMAL);
  116. ppu_setMirroring(PPU_MIRROR_VERTICAL);
  117. break;
  118. case 3:
  119. ppu_setScreenMode(PPU_SCMODE_NORMAL);
  120. ppu_setMirroring(PPU_MIRROR_HORIZTAL);
  121. break;
  122. }
  123. if ((OldSwitchArea != (MMC1_reg0 & MMC1_R0_PRGAREA)) && ((MMC1_reg0 & MMC1_R0_PRGSIZE) != 0))
  124. {
  125. if ((MMC1_reg0 & MMC1_R0_PRGAREA) != 0)
  126. { /* 0x8000 area */
  127. set_prom_bank_16k(0x8000, mmc1_CurrentBank);
  128. set_prom_bank_16k(0xC000, GETLAST16KBANK(Cart));
  129. }
  130. else
  131. { /* 0xC000 area */
  132. set_prom_bank_16k(0x8000, 0);
  133. set_prom_bank_16k(0xC000, mmc1_CurrentBank);
  134. }
  135. OldSwitchArea = (MMC1_reg0 & MMC1_R0_PRGAREA);
  136. }
  137. }
  138. uint32_t VROMBankNb;
  139. uint8_t Bit = 0;
  140. uint8_t BitBuf = 0;
  141. void mmc1_MapperWriteReg0(register uint8_t Addr, register uint8_t Value)
  142. {
  143. if (Value & 0x80)
  144. {
  145. MMC1_reg0 = MMC1_REG0_DEFAULT;
  146. console_printf(Console_Default, "MMC1: Reg0 Reset occurred !\n");
  147. mmc1_ApplyReg0Mod();
  148. }
  149. else
  150. {
  151. if (Bit < 4)
  152. { /* Pas encore ecrit les 5 bits */
  153. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  154. Bit++;
  155. }
  156. else
  157. {
  158. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  159. Bit = 0;
  160. MMC1_reg0 = BitBuf;
  161. mmc1_ApplyReg0Mod();
  162. }
  163. }
  164. }
  165. void mmc1_MapperWriteReg1(register uint8_t Addr, register uint8_t Value)
  166. {
  167. if (Value & 0x80)
  168. {
  169. MMC1_reg1 = MMC1_REG1_DEFAULT;
  170. console_printf(Console_Default, "MMC1: Reg1 Reset occurred !\n");
  171. }
  172. else
  173. {
  174. if (Bit < 4)
  175. { /* Pas encore ecrit les 5 bits */
  176. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  177. Bit++;
  178. }
  179. else
  180. {
  181. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  182. Bit = 0;
  183. MMC1_reg1 = BitBuf;
  184. VROMBankNb = (MMC1_reg1 /* & MMC1_R1_VROMB1 */ );
  185. if (Cart->VROMSize == 0)
  186. {
  187. console_printf(Console_Default, "Try to change VROM but with didn't have any VROM ! [%04X]\n",
  188. VROMBankNb);
  189. return;
  190. }
  191. if ((MMC1_reg0 & MMC1_R0_VROMSW) != 0)
  192. { /* 4K vram */
  193. //console_printf(Console_Default, "Switching VROM at 0x0000 to 4k bank %d\n", VROMBankNb);
  194. set_vrom_bank_4k(0x0000, VROMBankNb);
  195. }
  196. else
  197. { /* 8K vram */
  198. //console_printf(Console_Default, "Switching VROM at 0x0000 to 8k bank %d\n", VROMBankNb>>1);
  199. set_vrom_bank_8k(0x0000, VROMBankNb >> 1);
  200. }
  201. }
  202. }
  203. }
  204. void mmc1_MapperWriteReg2(register uint8_t Addr, register uint8_t Value)
  205. {
  206. if (Value & 0x80)
  207. {
  208. MMC1_reg2 = MMC1_REG2_DEFAULT;
  209. console_printf(Console_Default, "MMC1: Reg2 Reset occurred !\n");
  210. }
  211. else
  212. {
  213. if (Bit < 4)
  214. { /* Pas encore ecrit les 5 bits */
  215. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  216. Bit++;
  217. }
  218. else
  219. {
  220. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  221. Bit = 0;
  222. MMC1_reg2 = BitBuf;
  223. VROMBankNb = (MMC1_reg2 /* & MMC1_R2_VROMB2 */ );
  224. //console_printf(Console_Default, "Want to switch VROM at 0x1000 to 4k bank %d\n", VROMBankNb);
  225. if (Cart->VROMSize == 0)
  226. {
  227. //console_printf(Console_Default, ": No\n");
  228. return;
  229. }
  230. if ((MMC1_reg0 & MMC1_R0_VROMSW) != 0)
  231. { /* 4K vram */
  232. //console_printf(Console_Default, "Switching VROM at 0x1000 to 4k bank %d\n", VROMBankNb);
  233. set_vrom_bank_4k(0x1000, VROMBankNb);
  234. }
  235. else
  236. { /* 8K vram */
  237. // console_printf(Console_Default, "Switching VROM at 0x1000 to 8k bank %d\n", VROMBankNb>>1);
  238. // set_vrom_bank_8k(0x1000,VROMBankNb>>1);
  239. }
  240. }
  241. }
  242. }
  243. void mmc1_MapperWriteReg3(register uint8_t Addr, register uint8_t Value)
  244. {
  245. if (Value & 0x80)
  246. {
  247. MMC1_reg3 = MMC1_REG3_DEFAULT;
  248. console_printf(Console_Default, "MMC1: Reg3 Reset occurred !\n");
  249. }
  250. else
  251. {
  252. if (Bit < 4)
  253. { /* Pas encore ecrit les 5 bits */
  254. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  255. Bit++;
  256. }
  257. else
  258. {
  259. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  260. Bit = 0;
  261. MMC1_reg3 = BitBuf;
  262. if (((uint32_t)MMC1_reg3 << 14) > Cart->PROMSize)
  263. {
  264. return;
  265. }
  266. if ((MMC1_reg0 & MMC1_R0_PRGSIZE) != 0)
  267. { /* 16K Switch */
  268. if ((MMC1_reg0 & MMC1_R0_PRGAREA) != 0)
  269. { /* 0x8000 switch */
  270. set_prom_bank_16k(0x8000, MMC1_reg3);
  271. //console_printf(Console_Default, "LowBank is now %d ( 0x%p )\n", MMC1_reg3, mLBank);
  272. }
  273. else
  274. { /* 0xC000 switch */
  275. set_prom_bank_16k(0xC000, MMC1_reg3);
  276. //console_printf(Console_Default, "HighBank is now %d ( 0x%p )\n", MMC1_reg3, mUBank);
  277. }
  278. }
  279. else
  280. { /* 32K Switch */
  281. set_prom_bank_32k(0x8000, MMC1_reg3 >> 1);
  282. }
  283. if ((MMC1_reg3 & MMC1_R3_SAVECE) != 0)
  284. {
  285. unmap_sram();
  286. }
  287. else
  288. {
  289. map_sram();
  290. }
  291. }
  292. }
  293. }
  294. //console_printf(Console_Default, "MMC1: Debug (Reg:%d,Val:0x%02X,reg0:0x%02X,reg1:0x%02X,reg2:0x%02X,reg3:0x%02X)\n", MMC1_GetReg(Addr), Value, MMC1_reg0, MMC1_reg1, MMC1_reg2, MMC1_reg3);