merge mappers from FCEU-mm
[fceu.git] / boards / KS7017.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2007 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  * FDS Conversion\r
21  *\r
22  */\r
23 \r
24 #include "mapinc.h"\r
25 \r
26 static uint8 reg, mirr;\r
27 static int32 IRQa, IRQCount, IRQLatch;\r
28 static uint8 *WRAM=NULL;\r
29 static uint32 WRAMSIZE;\r
30 \r
31 static SFORMAT StateRegs[]=\r
32 {\r
33   {&mirr, 1, "MIRR"},\r
34   {&reg, 1, "REGS"},\r
35   {&IRQa, 4, "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   setprg16(0x8000,reg);\r
44   setprg16(0xC000,2);\r
45   setmirror(mirr);\r
46 }\r
47 \r
48 static DECLFW(UNLKS7017Write)\r
49 {\r
50 //  FCEU_printf("bs %04x %02x\n",A,V);\r
51     if((A & 0xFF00) == 0x4A00)\r
52     {\r
53       reg = ((A >> 2) & 3)|((A >> 4) & 4);\r
54     }\r
55     else if ((A & 0xFF00) == 0x5100)\r
56     {\r
57       Sync();\r
58     }\r
59     else if (A == 0x4020)\r
60     {\r
61       X6502_IRQEnd(FCEU_IQEXT);\r
62       IRQCount&=0xFF00;\r
63       IRQCount|=V;\r
64     }\r
65     else if (A == 0x4021)\r
66     {\r
67       X6502_IRQEnd(FCEU_IQEXT);\r
68       IRQCount&=0xFF;\r
69       IRQCount|=V<<8;\r
70       IRQa = 1;\r
71     }\r
72     else if (A == 0x4025)\r
73     {\r
74       mirr = ((V & 8) >> 3) ^ 1;\r
75     }\r
76 }\r
77 \r
78 static DECLFR(FDSRead4030)\r
79 {\r
80   X6502_IRQEnd(FCEU_IQEXT);\r
81   return X.IRQlow&FCEU_IQEXT?1:0;\r
82 }\r
83 \r
84 static void FP_FASTAPASS(1) UNL7017IRQ(int a)\r
85 {\r
86  if(IRQa)\r
87  {\r
88   IRQCount-=a;\r
89   if(IRQCount<=0)\r
90   {\r
91     IRQa=0;\r
92     X6502_IRQBegin(FCEU_IQEXT);\r
93   }\r
94  }\r
95 }\r
96 \r
97 static void UNLKS7017Power(void)\r
98 {\r
99   Sync();\r
100   setchr8(0);\r
101   setprg8r(0x10,0x6000,0);\r
102   SetReadHandler(0x6000,0x7FFF,CartBR);\r
103   SetWriteHandler(0x6000,0x7FFF,CartBW);\r
104   SetReadHandler(0x8000,0xFFFF,CartBR);\r
105   SetReadHandler(0x4030,0x4030,FDSRead4030);\r
106   SetWriteHandler(0x4020,0x5FFF,UNLKS7017Write);\r
107 }\r
108 \r
109 static void UNLKS7017Close(void)\r
110 {\r
111   if(WRAM)\r
112     FCEU_gfree(WRAM);\r
113   WRAM=NULL;\r
114 }\r
115 \r
116 static void StateRestore(int version)\r
117 {\r
118   Sync();\r
119 }\r
120 \r
121 void UNLKS7017_Init(CartInfo *info)\r
122 {\r
123   info->Power=UNLKS7017Power;\r
124   info->Close=UNLKS7017Close;\r
125   MapIRQHook=UNL7017IRQ;\r
126 \r
127   WRAMSIZE=8192;\r
128   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
129   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
130   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
131 \r
132   GameStateRestore=StateRestore;\r
133   AddExState(&StateRegs, ~0, 0, 0);\r
134 }\r