merge mapper code from FCEUX
[fceu.git] / boards / 117.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Xodnizel
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "mapinc.h"
22
23 static uint8 prgreg[4], chrreg[8], mirror;
24 static uint8 IRQa, IRQCount, IRQLatch;
25
26 static SFORMAT StateRegs[]=
27 {
28   {&IRQa, 1, "IRQA"},
29   {&IRQCount, 1, "IRQC"},
30   {&IRQLatch, 1, "IRQL"},
31   {prgreg, 4, "PREGS"},
32   {chrreg, 8, "CREGS"},
33   {&mirror, 1, "MREG"},
34   {0}
35 };
36
37 static void Sync(void)
38 {
39   int i;
40   setprg8(0x8000,prgreg[0]);     
41   setprg8(0xa000,prgreg[1]);     
42   setprg8(0xc000,prgreg[2]);     
43   setprg8(0xe000,prgreg[3]);
44   for(i=0; i<8; i++)
45      setchr1(i<<10,chrreg[i]);     
46   setmirror(mirror^1);   
47 }
48
49 static DECLFW(M117Write)
50 {
51   if(A<0x8004)
52   {
53     prgreg[A&3]=V;
54     Sync();
55   }  
56   else if((A>=0xA000)&&(A<=0xA007))
57   {
58     chrreg[A&7]=V;
59     Sync();
60   }
61   else switch(A)
62        {
63          case 0xc001: IRQLatch=V; break;
64          case 0xc003: IRQCount=IRQLatch; IRQa|=2; break;
65          case 0xe000: IRQa&=~1; IRQa|=V&1; X6502_IRQEnd(FCEU_IQEXT); break;
66          case 0xc002: X6502_IRQEnd(FCEU_IQEXT); break;
67          case 0xd000: mirror=V&1;
68        }
69 }
70
71 static void M117Power(void)
72 {
73   prgreg[0]=~3; prgreg[1]=~2; prgreg[2]=~1; prgreg[3]=~0;
74   Sync();
75   SetReadHandler(0x8000,0xFFFF,CartBR);
76   SetWriteHandler(0x8000,0xFFFF,M117Write);
77 }
78
79 static void M117IRQHook(void)
80 {
81   if(IRQa==3&&IRQCount)
82   {
83     IRQCount--;
84     if(!IRQCount)
85     {
86       IRQa&=1;
87       X6502_IRQBegin(FCEU_IQEXT);
88     }
89   }
90 }
91
92 static void StateRestore(int version)
93 {
94   Sync();
95 }
96
97 void Mapper117_Init(CartInfo *info)
98 {
99   info->Power=M117Power;
100   GameHBIRQHook=M117IRQHook;
101   GameStateRestore=StateRestore;
102   AddExState(&StateRegs, ~0, 0, 0);
103 }
104