a08caaae776b68deff086a1529ab362938a8f8fb
[fceu.git] / boards / 17.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2012 CaH4e3\r
5  *\r
6  * This program is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * This program is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * FFE Copier Mapper\r
21  *\r
22  */\r
23 \r
24 #include "mapinc.h"\r
25 \r
26 static uint8 preg[4], creg[8];\r
27 static uint8 IRQa, mirr;\r
28 static int32 IRQCount, IRQLatch;\r
29 \r
30 static SFORMAT StateRegs[]=\r
31 {\r
32   {preg, 4, "PREG"},\r
33   {creg, 8, "CREG"},\r
34   {&mirr, 1, "MIRR"},\r
35   {&IRQa, 1, "IRQA"},\r
36   {&IRQCount, 4, "IRQC"},\r
37   {&IRQLatch, 4, "IRQL"},\r
38   {0}\r
39 };\r
40 \r
41 static void Sync(void)\r
42 {\r
43   int i;\r
44   for(i=0; i<8; i++) setchr1(i<<10,creg[i]);\r
45   setprg8(0x8000,preg[0]);\r
46   setprg8(0xA000,preg[1]);\r
47   setprg8(0xC000,preg[2]);\r
48   setprg8(0xE000,preg[3]);\r
49   switch(mirr) {\r
50     case 0: setmirror(MI_0); break;\r
51     case 1: setmirror(MI_1); break;\r
52     case 2: setmirror(MI_H); break;\r
53     case 3: setmirror(MI_V); break;\r
54   }\r
55 }\r
56 \r
57 static DECLFW(M17WriteMirr)\r
58 {\r
59   mirr = ((A << 1) & 2)|((V >> 4) & 1);\r
60   Sync();\r
61 }\r
62 \r
63 static DECLFW(M17WriteIRQ)\r
64 {\r
65   switch(A) {\r
66     case 0x4501: IRQa=0; X6502_IRQEnd(FCEU_IQEXT); break;\r
67     case 0x4502: IRQCount&=0xFF00; IRQCount|=V; break;\r
68     case 0x4503: IRQCount&=0x00FF; IRQCount|=V<<8; IRQa=1; break;\r
69   }\r
70 }\r
71 \r
72 static DECLFW(M17WritePrg)\r
73 {\r
74   preg[A & 3] = V;\r
75   Sync();\r
76 }\r
77 \r
78 static DECLFW(M17WriteChr)\r
79 {\r
80   creg[A & 7] = V;\r
81   Sync();\r
82 }\r
83 \r
84 static void M17Power(void)\r
85 {\r
86   preg[3] = ~0;\r
87   Sync();\r
88   SetReadHandler(0x8000,0xFFFF,CartBR);\r
89   SetWriteHandler(0x42FE,0x42FF,M17WriteMirr);\r
90   SetWriteHandler(0x4500,0x4503,M17WriteIRQ);\r
91   SetWriteHandler(0x4504,0x4507,M17WritePrg);\r
92   SetWriteHandler(0x4510,0x4517,M17WriteChr);\r
93 }\r
94 \r
95 static void FP_FASTAPASS(1) M17IRQHook(int a)\r
96 {\r
97   if(IRQa)\r
98   {\r
99     IRQCount+=a;\r
100     if(IRQCount>=0x10000)\r
101     {\r
102       X6502_IRQBegin(FCEU_IQEXT);\r
103       IRQa=0;\r
104       IRQCount=0;\r
105     }\r
106   }\r
107 }\r
108 \r
109 static void StateRestore(int version)\r
110 {\r
111   Sync();\r
112 }\r
113 \r
114 void Mapper17_Init(CartInfo *info)\r
115 {\r
116   info->Power=M17Power;\r
117   GameHBIRQHook=M17IRQHook;\r
118   GameStateRestore=StateRestore;\r
119 \r
120   AddExState(&StateRegs, ~0, 0, 0);\r
121 }\r
122 \r