mapper fixes for ncpu, debug is broken atm
[fceu.git] / mappers / 105.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 1998 BERO
5  *  Copyright (C) 2002 Ben Parnell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "mapinc.h"
23
24 #define MMC1_reg mapbyte1
25 #define MMC1_buf mapbyte2[0]
26 #define MMC1_sft mapbyte3[0]
27 #define lastn    mapbyte2[1]
28
29 #define NWCDIP 0xE
30
31 static void FP_FASTAPASS(1) NWCIRQHook(int a)
32 {
33         if(!(MMC1_reg[1]&0x10))
34         {
35          IRQCount+=a;
36          if((IRQCount|(NWCDIP<<25))>=0x3e000000)
37           {
38            IRQCount=0;
39            X6502_IRQBegin(FCEU_IQEXT);
40           }
41         }
42 }
43
44
45 static void MMC1PRG(void)
46 {
47         if(MMC1_reg[1]&8)
48         {
49         switch(MMC1_reg[0]&0xC)
50          {
51            case 0xC: ROM_BANK16(0x8000,8+(MMC1_reg[3]&7));
52                      ROM_BANK16(0xC000,15);
53                      break;
54            case 0x8: ROM_BANK16(0xC000,8+((MMC1_reg[3])&7));
55                      ROM_BANK16(0x8000,8);
56                      break;
57            case 0x0:
58            case 0x4:
59                      ROM_BANK16(0x8000,8+(MMC1_reg[3]&6));
60                      ROM_BANK16(0xc000,8+((MMC1_reg[3]&6)+1));
61                      break;
62          }
63         }
64         else
65         {
66          ROM_BANK32((MMC1_reg[1]>>1)&3);
67         }
68         X6502_Rebase();
69 }
70
71 DECLFW(Mapper105_write)
72 {
73         int n=(A>>13)-4;
74
75         if (V&0x80)
76         {
77          MMC1_sft=MMC1_buf=0;
78          return;
79         }
80
81         if(lastn!=n)
82         {
83          MMC1_sft=MMC1_buf=0;
84         }
85         lastn=n;
86
87         //MMC1_reg[n]&=~((1)<<(MMC1_sft));
88         MMC1_buf|=(V&1)<<(MMC1_sft++);
89
90   if (MMC1_sft==5)
91         {
92         if(n==3) V&=0xF;
93         else     V&=0x1F;
94
95         MMC1_reg[n]=V=MMC1_buf;
96         MMC1_sft = MMC1_buf=0;
97
98         switch(n){
99         case 0:
100                 switch(MMC1_reg[0]&3)
101                 {
102                  case 2: MIRROR_SET(0);break;
103                  case 3: MIRROR_SET(1);break;
104                  case 0: onemir(0);break;
105                  case 1: onemir(1);break;
106                 }
107                 MMC1PRG();
108                 break;
109         case 1:
110                 if(MMC1_reg[1]&0x10)
111                  {IRQCount=0;X6502_IRQEnd(FCEU_IQEXT);}
112                 MMC1PRG();
113                 break;
114         case 3:
115                 MMC1PRG();
116                 break;
117         }
118   }
119 }
120
121
122 void Mapper105_init(void)
123 {
124         int i;
125         for(i=0;i<4;i++) MMC1_reg[i]=0;
126         MMC1_sft = MMC1_buf =0;
127         MMC1_reg[0]=0xC;
128         ROM_BANK32(0);
129         SetWriteHandler(0x8000,0xFFFF,Mapper105_write);
130         MapIRQHook=NWCIRQHook;
131 }
132