mapper fixes for ncpu, debug is broken atm
[fceu.git] / mappers / 85.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
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
21 #include "mapinc.h"
22
23 #define vrctemp mapbyte1[0]
24 #define indox   mapbyte1[1]
25
26 static int acount=0;
27
28 void KillOPL(void);
29 void UpdateOPL(int Count);
30 void vrc7translate(uint8 A, uint8 V);
31 void LoadOPL(void);
32 extern uint8 VRC7Instrument[16][8];
33 extern uint8 VRC7Chan[3][6];
34
35 static INLINE void DaMirror(int V)
36 {
37  int salpo[4]={MI_V,MI_H,MI_0,MI_1};
38  setmirror(salpo[V&3]);
39 }
40
41 DECLFW(Mapper85_write)
42 {
43         A|=(A&8)<<1;
44
45         if(A>=0xa000 && A<=0xDFFF)
46         {
47          A&=0xF010;
48          {
49           int x=((A>>4)&1)|((A-0xA000)>>11);
50           mapbyte3[x]=V;
51           setchr1(x<<10,V);
52          }
53         }
54         else if(A==0x9030)
55         {
56          if(FSettings.SndRate)
57           vrc7translate(indox,V);
58          GameExpSound.Fill=UpdateOPL;
59         }
60         else switch(A&0xF010)
61         {
62          case 0x8000:mapbyte2[0]=V;setprg8(0x8000,V);
63                      X6502_Rebase();break;
64          case 0x8010:mapbyte2[1]=V;setprg8(0xa000,V);
65                      X6502_Rebase();break;
66          case 0x9000:mapbyte2[2]=V;setprg8(0xc000,V);
67                      X6502_Rebase();break;
68          case 0x9010:indox=V;break;
69          case 0xe000:mapbyte2[3]=V;DaMirror(V);break;
70          case 0xE010:IRQLatch=V;
71                      break;
72          case 0xF000:IRQa=V&2;
73                      vrctemp=V&1;
74                      if(V&2) {IRQCount=IRQLatch;acount=0;}
75                      break;
76          case 0xf010:if(vrctemp) IRQa=1;
77                      else IRQa=0;
78                      break;
79         }
80 }
81
82 static void FP_FASTAPASS(1) KonamiIRQHook(int a)
83 {
84   if(IRQa)
85    {
86     acount+=(a<<1)+a;
87     if(acount>=339)
88     {
89      doagainbub:acount-=339;IRQCount++;
90      if(IRQCount&0x100) {TriggerIRQ();IRQCount=IRQLatch;}
91      if(acount>=339) goto doagainbub;
92     }
93  }
94 }
95
96 void Mapper85_StateRestore(int version)
97 {
98  int x;
99
100  if(version<72)
101  {
102   for(x=0;x<8;x++)
103    mapbyte3[x]=CHRBankList[x];
104   for(x=0;x<3;x++)
105    mapbyte2[x]=PRGBankList[x];
106   mapbyte2[3]=(Mirroring<0x10)?Mirroring:Mirroring-0xE;
107  }
108
109  for(x=0;x<8;x++)
110   setchr1(x*0x400,mapbyte3[x]);
111  for(x=0;x<3;x++)
112   setprg8(0x8000+x*8192,mapbyte2[x]);
113  DaMirror(mapbyte2[3]);
114  LoadOPL();
115 }
116
117 static void M85SC(void)
118 {
119   KillOPL();
120 }
121
122 void VRC7_ESI(void)
123 {
124    if(FSettings.SndRate)
125    {
126     SetWriteHandler(0x9010,0x901F,Mapper85_write);
127     SetWriteHandler(0x9030,0x903F,Mapper85_write);
128    }
129    GameExpSound.RChange=M85SC;
130 }
131
132 void Mapper85_init(void)
133 {
134   MapIRQHook=KonamiIRQHook;
135   SetWriteHandler(0x8000,0xffff,Mapper85_write);
136   GameStateRestore=Mapper85_StateRestore;
137   VRC7_ESI();
138   if(!VROM_size)
139    SetupCartCHRMapping(0, CHRRAM, 8192, 1);
140   AddExState(VRC7Instrument, 16, 0, "VC7I");
141   AddExState(VRC7Chan, sizeof(VRC7Chan), 0, "V7CH");
142
143 }