mmc1.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * MMC1 Mapper - The peTI-NESulator Project
  3. * mmc1.h
  4. *
  5. * Created by Manoel TRAPIER.
  6. * Copyright (c) 2003-2008 986Corp. All rights reserved.
  7. *
  8. * $LastChangedDate$
  9. * $Author$
  10. * $HeadURL$
  11. * $Revision$
  12. *
  13. */
  14. unsigned char MMC1_reg0;
  15. unsigned char MMC1_reg1;
  16. unsigned char MMC1_reg2;
  17. unsigned char MMC1_reg3;
  18. unsigned char mmc1_CurrentBank;
  19. #define MMC1_R0_MIRROR 0x01
  20. #define MMC1_R0_ONESCREEN 0x02
  21. #define MMC1_R0_PRGAREA 0x04
  22. #define MMC1_R0_PRGSIZE 0x08
  23. #define MMC1_R0_VROMSW 0x10
  24. #define MMC1_R0_RESET 0x80
  25. #define MMC1_R1_VROMB1 0x0F
  26. #define MMC1_R1_256KSEL 0x10
  27. #define MMC1_R1_RESET 0x80
  28. #define MMC1_R2_VROMB2 0x0F
  29. #define MMC1_R2_256KSEL 0x10
  30. #define MMC1_R2_RESET 0x80
  31. #define MMC1_R3_VROMB2 0x0F
  32. #define MMC1_R3_SAVECE 0x10
  33. #define MMC1_R3_RESET 0x80
  34. #define MMC1_REG0_DEFAULT MMC1_R0_PRGSIZE | MMC1_R0_PRGAREA
  35. #define MMC1_REG1_DEFAULT 0
  36. #define MMC1_REG2_DEFAULT 0
  37. #define MMC1_REG3_DEFAULT 0
  38. void mmc1_MapperWriteReg0(register byte Addr, register byte Value);
  39. void mmc1_MapperWriteReg1(register byte Addr, register byte Value);
  40. void mmc1_MapperWriteReg2(register byte Addr, register byte Value);
  41. void mmc1_MapperWriteReg3(register byte Addr, register byte Value);
  42. void mmc1_MapperDump(FILE *fp)
  43. {
  44. fprintf(fp,"MMC1: r0:0x%02X r1:0x%02X r2:0x%02X r3:0x%02X\n",MMC1_reg0,MMC1_reg1,MMC1_reg2,MMC1_reg3);
  45. }
  46. int mmc1_InitMapper(NesCart * cart)
  47. {
  48. int i;
  49. MMC1_reg0 = MMC1_REG0_DEFAULT;
  50. MMC1_reg1 = MMC1_REG1_DEFAULT;
  51. MMC1_reg2 = MMC1_REG2_DEFAULT;
  52. MMC1_reg3 = MMC1_REG3_DEFAULT;
  53. set_prom_bank_16k(0x8000,0);
  54. set_prom_bank_16k(0xC000, GETLAST16KBANK(cart));
  55. mmc1_CurrentBank = 0;
  56. if (cart->VROMSize > 0)
  57. set_vrom_bank_4k(0x0000,0);
  58. /* Mapper should register itself for write hook */
  59. for (i = 0x80; i < 0xA0 ; i++)
  60. {
  61. set_page_wr_hook(i, mmc1_MapperWriteReg0);
  62. set_page_writeable(i, true);
  63. }
  64. for (i = 0xA0; i < 0xC0 ; i++)
  65. {
  66. set_page_wr_hook(i, mmc1_MapperWriteReg1);
  67. set_page_writeable(i, true);
  68. }
  69. for (i = 0xC0; i < 0xE0 ; i++)
  70. {
  71. set_page_wr_hook(i, mmc1_MapperWriteReg2);
  72. set_page_writeable(i, true);
  73. }
  74. for (i = 0xE0; i < 0x100 ; i++)
  75. {
  76. set_page_wr_hook(i, mmc1_MapperWriteReg3);
  77. set_page_writeable(i, true);
  78. }
  79. return 0;
  80. }
  81. /*
  82. Reg 0
  83. 8 : 1000
  84. 9 : 1001
  85. Reg 1
  86. A : 1010
  87. B : 1011
  88. Reg 2
  89. C : 1100
  90. D : 1101
  91. Reg 3
  92. E : 1110
  93. F : 1111
  94. ((Addr & 0x6000) >> 13) <- port number
  95. */
  96. #define MMC1_GetReg(a) ((a & 0x6000) >> 13)
  97. /* (Val & 0x01) recuperation du bit */
  98. #define MMC1_GetBit(v) (v & 0x01)
  99. /* ( ( b & (1 << Bit)) | (v << Bit) ) Ajout du bit */
  100. #define MMC1_AddBit(b,v) ( ( b & ~(1 << Bit)) | (v << Bit) )
  101. void mmc1_ApplyReg0Mod()
  102. {
  103. static unsigned char OldSwitchArea = MMC1_R0_PRGAREA;
  104. //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);
  105. switch (MMC1_reg0 & 0x03)
  106. {
  107. case 0:
  108. ppu_setScreenMode(PPU_SCMODE_SINGLE);
  109. ppu_setSingleScreen(PPU_SCREEN_000);
  110. break;
  111. case 1:
  112. ppu_setScreenMode(PPU_SCMODE_SINGLE);
  113. ppu_setSingleScreen(PPU_SCREEN_400);
  114. break;
  115. case 2:
  116. ppu_setScreenMode(PPU_SCMODE_NORMAL);
  117. ppu_setMirroring(PPU_MIRROR_VERTICAL);
  118. break;
  119. case 3:
  120. ppu_setScreenMode(PPU_SCMODE_NORMAL);
  121. ppu_setMirroring(PPU_MIRROR_HORIZTAL);
  122. break;
  123. }
  124. if ( (OldSwitchArea != (MMC1_reg0 & MMC1_R0_PRGAREA)) && ((MMC1_reg0 & MMC1_R0_PRGSIZE) != 0 ) )
  125. {
  126. if ((MMC1_reg0 & MMC1_R0_PRGAREA) != 0)
  127. { /* 0x8000 area */
  128. set_prom_bank_16k(0x8000,mmc1_CurrentBank);
  129. set_prom_bank_16k(0xC000, GETLAST16KBANK(Cart));
  130. }
  131. else
  132. { /* 0xC000 area */
  133. set_prom_bank_16k(0x8000,0);
  134. set_prom_bank_16k(0xC000,mmc1_CurrentBank);
  135. }
  136. OldSwitchArea = (MMC1_reg0 & MMC1_R0_PRGAREA);
  137. }
  138. }
  139. int VROMBankNb;
  140. unsigned char Bit = 0;
  141. unsigned char BitBuf = 0;
  142. void mmc1_MapperWriteReg0(register byte Addr, register byte Value)
  143. {
  144. if (Value & 0x80)
  145. {
  146. MMC1_reg0 = MMC1_REG0_DEFAULT;
  147. console_printf(Console_Default, "MMC1: Reg0 Reset occured !\n");
  148. mmc1_ApplyReg0Mod();
  149. }
  150. else
  151. {
  152. if (Bit < 4)
  153. { /* Pas encore ecrit les 5 bits */
  154. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  155. Bit++;
  156. }
  157. else
  158. {
  159. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  160. Bit = 0;
  161. MMC1_reg0 = BitBuf;
  162. mmc1_ApplyReg0Mod();
  163. }
  164. }
  165. }
  166. void mmc1_MapperWriteReg1(register byte Addr, register byte Value)
  167. {
  168. if (Value & 0x80)
  169. {
  170. MMC1_reg1 = MMC1_REG1_DEFAULT;
  171. console_printf(Console_Default, "MMC1: Reg1 Reset occured !\n");
  172. }
  173. else
  174. {
  175. if (Bit < 4)
  176. { /* Pas encore ecrit les 5 bits */
  177. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  178. Bit++;
  179. }
  180. else
  181. {
  182. BitBuf = MMC1_AddBit(BitBuf, MMC1_GetBit(Value));
  183. Bit = 0;
  184. MMC1_reg1 = BitBuf;
  185. VROMBankNb = (MMC1_reg1 /* & MMC1_R1_VROMB1 */ );
  186. if (Cart->VROMSize == 0)
  187. {
  188. console_printf(Console_Default, "Try to change VROM but with didn't have any VROM ! [%04X]\n", 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 byte Addr, register byte Value)
  205. {
  206. if (Value & 0x80)
  207. {
  208. MMC1_reg2 = MMC1_REG2_DEFAULT;
  209. console_printf(Console_Default, "MMC1: Reg2 Reset occured !\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 byte Addr, register byte Value)
  244. {
  245. if (Value & 0x80)
  246. {
  247. MMC1_reg3 = MMC1_REG3_DEFAULT;
  248. console_printf(Console_Default, "MMC1: Reg3 Reset occured !\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 (MMC1_reg3<<14 > Cart->PROMSize)
  263. return;
  264. if ( (MMC1_reg0 & MMC1_R0_PRGSIZE) != 0 )
  265. { /* 16K Switch */
  266. if ( (MMC1_reg0 & MMC1_R0_PRGAREA) != 0 )
  267. { /* 0x8000 switch */
  268. set_prom_bank_16k(0x8000,MMC1_reg3);
  269. //console_printf(Console_Default, "LowBank is now %d ( 0x%p )\n", MMC1_reg3, mLBank);
  270. }
  271. else
  272. { /* 0xC000 switch */
  273. set_prom_bank_16k(0xC000,MMC1_reg3);
  274. //console_printf(Console_Default, "HighBank is now %d ( 0x%p )\n", MMC1_reg3, mUBank);
  275. }
  276. }
  277. else
  278. { /* 32K Switch */
  279. set_prom_bank_32k(0x8000,MMC1_reg3>>1);
  280. }
  281. if ( ( MMC1_reg3 & MMC1_R3_SAVECE ) != 0)
  282. {
  283. unmap_sram();
  284. }
  285. else
  286. {
  287. map_sram();
  288. }
  289. }
  290. }
  291. }
  292. //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);