release r2, update credits
[fceu.git] / mappers / 69.c
CommitLineData
c62d2810 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
d97315ac 4 * Copyright (C) 2002 Xodnizel
c62d2810 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
43725da7 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c62d2810 19 */
20
21#include "mapinc.h"
d97315ac 22
c62d2810 23static void AYSound(int Count);
386f5371 24static void AYSoundHQ(void);
c62d2810 25static void DoAYSQ(int x);
386f5371 26static void DoAYSQHQ(int x);
c62d2810 27
28#define sunselect mapbyte1[0]
29#define sungah mapbyte1[1]
d97315ac 30static uint8 sunindex;
c62d2810 31
d97315ac 32static DECLFW(SUN5BWRAM)
c62d2810 33{
34 if((sungah&0xC0)==0xC0)
35 (WRAM-0x6000)[A]=V;
36}
37
d97315ac 38static DECLFR(SUN5AWRAM)
c62d2810 39{
40 if((sungah&0xC0)==0x40)
c0bf6f9f 41 return X.DB;
386f5371 42 return CartBROB(A);
c62d2810 43}
44
d97315ac 45static DECLFW(Mapper69_SWL)
c62d2810 46{
47 sunindex=V%14;
48}
d97315ac 49
50static DECLFW(Mapper69_SWH)
c62d2810 51{
d97315ac 52 int x;
c62d2810 53 GameExpSound.Fill=AYSound;
386f5371 54 GameExpSound.HiFill=AYSoundHQ;
43725da7 55 if(FSettings.SndRate);
c62d2810 56 switch(sunindex)
57 {
58 case 0:
59 case 1:
386f5371 60 case 8:if(FSettings.soundq>=1) DoAYSQHQ(0); else DoAYSQ(0);break;
c62d2810 61 case 2:
62 case 3:
386f5371 63 case 9:if(FSettings.soundq>=1) DoAYSQHQ(1); else DoAYSQ(1);break;
c62d2810 64 case 4:
65 case 5:
386f5371 66 case 10:if(FSettings.soundq>=1) DoAYSQHQ(2); else DoAYSQ(2);break;
d97315ac 67 case 7:
68 for(x=0;x<2;x++)
386f5371 69 if(FSettings.soundq>=1) DoAYSQHQ(x); else DoAYSQ(x);
d97315ac 70 break;
c62d2810 71 }
c0bf6f9f 72 MapperExRAM[sunindex]=V;
c62d2810 73}
74
d97315ac 75static DECLFW(Mapper69_write)
c62d2810 76{
77 switch(A&0xE000)
78 {
79 case 0x8000:sunselect=V;break;
80 case 0xa000:
81 sunselect&=0xF;
82 if(sunselect<=7)
83 VROM_BANK1(sunselect<<10,V);
84 else
85 switch(sunselect&0x0f)
86 {
87 case 8:
88 sungah=V;
89 if(V&0x40)
90 {
91 if(V&0x80) // Select WRAM
92 setprg8r(0x10,0x6000,0);
93 }
94 else
95 setprg8(0x6000,V);
96 break;
d97315ac 97 case 9:ROM_BANK8(0x8000,V);break;
98 case 0xa:ROM_BANK8(0xa000,V);break;
99 case 0xb:ROM_BANK8(0xc000,V);break;
c62d2810 100 case 0xc:
101 switch(V&3)
102 {
103 case 0:MIRROR_SET2(1);break;
104 case 1:MIRROR_SET2(0);break;
105 case 2:onemir(0);break;
106 case 3:onemir(1);break;
107 }
108 break;
d97315ac 109 case 0xd:IRQa=V;X6502_IRQEnd(FCEU_IQEXT);break;
110 case 0xe:IRQCount&=0xFF00;IRQCount|=V;X6502_IRQEnd(FCEU_IQEXT);break;
111 case 0xf:IRQCount&=0x00FF;IRQCount|=V<<8;X6502_IRQEnd(FCEU_IQEXT);break;
c62d2810 112 }
113 break;
114 }
115}
116
d97315ac 117static int32 vcount[3];
118static int32 dcount[3];
119static int CAYBC[3];
120
c62d2810 121static void DoAYSQ(int x)
122{
d97315ac 123 int32 freq=((MapperExRAM[x<<1]|((MapperExRAM[(x<<1)+1]&15)<<8))+1)<<(4+17);
124 int32 amp=(MapperExRAM[0x8+x]&15)<<2;
c62d2810 125 int32 start,end;
d97315ac 126 int V;
127
128 amp+=amp>>1;
c62d2810 129
c0bf6f9f 130 start=CAYBC[x];
4fdfab07 131 end=(SOUNDTS<<16)/soundtsinc;
c62d2810 132 if(end<=start) return;
133 CAYBC[x]=end;
134
d97315ac 135 if(amp)
d97315ac 136 for(V=start;V<end;V++)
c62d2810 137 {
d97315ac 138 if(dcount[x])
139 Wave[V>>4]+=amp;
140 vcount[x]-=nesincsize;
141 while(vcount[x]<=0)
142 {
143 dcount[x]^=1;
144 vcount[x]+=freq;
c62d2810 145 }
d97315ac 146 }
c62d2810 147}
c62d2810 148
d97315ac 149static void DoAYSQHQ(int x)
150{
43725da7 151 int32 V;
d97315ac 152 int32 freq=((MapperExRAM[x<<1]|((MapperExRAM[(x<<1)+1]&15)<<8))+1)<<4;
153 int32 amp=(MapperExRAM[0x8+x]&15)<<6;
c62d2810 154
d97315ac 155 amp+=amp>>1;
c62d2810 156
d97315ac 157 if(!(MapperExRAM[0x7]&(1<<x)))
158 {
159 for(V=CAYBC[x];V<SOUNDTS;V++)
160 {
161 if(dcount[x])
162 WaveHi[V]+=amp;
163 vcount[x]--;
164 if(vcount[x]<=0)
165 {
166 dcount[x]^=1;
167 vcount[x]=freq;
168 }
169 }
170 }
171 CAYBC[x]=SOUNDTS;
c62d2810 172}
d97315ac 173
c62d2810 174static void AYSound(int Count)
175{
176 int x;
177 DoAYSQ(0);
178 DoAYSQ(1);
179 DoAYSQ(2);
d97315ac 180 for(x=0;x<3;x++)
c62d2810 181 CAYBC[x]=Count;
182}
183
d97315ac 184static void AYSoundHQ(void)
185{
186 DoAYSQHQ(0);
187 DoAYSQHQ(1);
188 DoAYSQHQ(2);
189}
d97315ac 190
191static void AYHiSync(int32 ts)
192{
193 int x;
194
195 for(x=0;x<3;x++)
196 CAYBC[x]=ts;
197}
198
43725da7 199static void FP_FASTAPASS(1) SunIRQHook(int a)
c62d2810 200{
201 if(IRQa)
202 {
203 IRQCount-=a;
204 if(IRQCount<=0)
d97315ac 205 {X6502_IRQBegin(FCEU_IQEXT);IRQa=0;IRQCount=0xFFFF;}
c62d2810 206 }
207}
208
209void Mapper69_StateRestore(int version)
210{
d97315ac 211 if(mapbyte1[1]&0x40)
c62d2810 212 {
d97315ac 213 if(mapbyte1[1]&0x80) // Select WRAM
214 setprg8r(0x10,0x6000,0);
c62d2810 215 }
216 else
d97315ac 217 setprg8(0x6000,mapbyte1[1]);
c62d2810 218}
219
d97315ac 220void Mapper69_ESI(void)
c62d2810 221{
d97315ac 222 GameExpSound.RChange=Mapper69_ESI;
223 GameExpSound.HiSync=AYHiSync;
224 memset(dcount,0,sizeof(dcount));
225 memset(vcount,0,sizeof(vcount));
226 memset(CAYBC,0,sizeof(CAYBC));
c62d2810 227}
228
d97315ac 229void NSFAY_Init(void)
c62d2810 230{
d97315ac 231 sunindex=0;
232 SetWriteHandler(0xc000,0xdfff,Mapper69_SWL);
233 SetWriteHandler(0xe000,0xffff,Mapper69_SWH);
234 Mapper69_ESI();
c62d2810 235}
236
237void Mapper69_init(void)
238{
d97315ac 239 sunindex=0;
240
c62d2810 241 SetupCartPRGMapping(0x10,WRAM,8192,1);
242
243 SetWriteHandler(0x8000,0xbfff,Mapper69_write);
d97315ac 244 SetWriteHandler(0xc000,0xdfff,Mapper69_SWL);
245 SetWriteHandler(0xe000,0xffff,Mapper69_SWH);
c62d2810 246 SetWriteHandler(0x6000,0x7fff,SUN5BWRAM);
247 SetReadHandler(0x6000,0x7fff,SUN5AWRAM);
248 Mapper69_ESI();
249 MapIRQHook=SunIRQHook;
250 MapStateRestore=Mapper69_StateRestore;
c62d2810 251}
252