smb3 and addams family hacks
[fceu.git] / boards / 183.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2005 CaH4e3
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Gimmick Bootleg
21  */
22
23 #include "mapinc.h"
24
25 static uint8 prg[4];
26 static uint8 chr[8];
27 static uint8 IRQCount;
28 static uint8 IRQPre;
29 static uint8 IRQa;
30
31 static SFORMAT StateRegs[]=
32 {
33   {prg, 4, "PRG"},
34   {chr, 8, "CHR"},
35   {&IRQCount, 1, "IRQCOUNT"},
36   {&IRQPre, 1, "IRQPRE"},
37   {&IRQa, 1, "IRQA"},
38   {0}
39 };
40
41 static void SyncPrg(void)
42 {
43   setprg8(0x6000,0);
44   setprg8(0x8000,prg[0]);
45   setprg8(0xA000,prg[1]);
46   setprg8(0xC000,prg[2]);
47   setprg8(0xE000,~0);
48 }
49
50 static void SyncChr(void)
51 {
52   int i;
53   for(i=0; i<8; i++)
54      setchr1(i<<10,chr[i]);
55 }
56
57 static void StateRestore(int version)
58 {
59   SyncPrg();
60   SyncChr();
61 }
62
63 static DECLFW(M183Write)
64 {
65   if(((A&0xF80C)>=0xB000)&&((A&0xF80C)<=0xE00C))
66   {
67     uint8 index=(((A&0x7000)>>11)-6)|((A&8)>>3);
68     chr[index]=(chr[index]&(0xF0>>(A&4)))|((V&0x0F)<<(A&4));
69     SyncChr();
70   }
71   else switch (A&0xF80C)
72   {
73     case 0x8800: prg[0]=V; SyncPrg(); break;
74     case 0xA800: prg[1]=V; SyncPrg(); break;
75     case 0xA000: prg[2]=V; SyncPrg(); break;
76     case 0x9800: switch (V&3)
77                  {
78                    case 0: setmirror(MI_V); break;
79                    case 1: setmirror(MI_H); break;
80                    case 2: setmirror(MI_0); break;
81                    case 3: setmirror(MI_1); break;
82                  }
83                  break;
84     case 0xF000: IRQCount=((IRQCount&0xF0)|(V&0xF)); break;
85     case 0xF004: IRQCount=((IRQCount&0x0F)|((V&0xF)<<4)); break;
86     case 0xF008: IRQa=V; if(!V)IRQPre=0; X6502_IRQEnd(FCEU_IQEXT); break;
87     case 0xF00C: IRQPre=16; break;
88   }
89 }
90
91 static void M183IRQCounter(void)
92 {
93   if(IRQa)
94   {
95     IRQCount++;
96     if((IRQCount-IRQPre)==238)
97       X6502_IRQBegin(FCEU_IQEXT);
98   }
99 }
100
101 static void M183Power(void)
102 {
103   IRQPre=IRQCount=IRQa=0;
104   SetReadHandler(0x8000,0xFFFF,CartBR);
105   SetWriteHandler(0x8000,0xFFFF,M183Write);
106   SetReadHandler(0x6000,0x7FFF,CartBR);
107   SyncPrg();
108   SyncChr();
109 }
110
111 void Mapper183_Init(CartInfo *info)
112 {
113   info->Power=M183Power;
114   GameHBIRQHook=M183IRQCounter;
115   GameStateRestore=StateRestore;
116   AddExState(&StateRegs, ~0, 0, 0);
117 }