updated bords/mappers/stuff to 0.98.15, lots of them got broken, asmcore support...
[fceu.git] / mappers / 69.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "mapinc.h"
22
23 static void AYSound(int Count);
24 //static void AYSoundHQ(void);
25 static void DoAYSQ(int x);
26 //static void DoAYSQHQ(int x);
27
28 #define sunselect mapbyte1[0]
29 #define sungah    mapbyte1[1]
30 static uint8 sunindex;
31
32 static DECLFW(SUN5BWRAM)
33 {
34  if((sungah&0xC0)==0xC0)
35   (WRAM-0x6000)[A]=V;
36 }
37
38 static DECLFR(SUN5AWRAM)
39 {
40  if((sungah&0xC0)==0x40)
41   return X.DB;
42  return CartBR(A);
43 }
44
45 static DECLFW(Mapper69_SWL)
46 {
47   sunindex=V%14;
48 }
49
50 static DECLFW(Mapper69_SWH)
51 {
52              int x;
53              GameExpSound.Fill=AYSound;
54              GameExpSound.HiFill=0;//AYSoundHQ;
55              if(FSettings.SndRate);
56              switch(sunindex)
57              {
58               case 0:
59               case 1:
60               case 8:/*if(FSettings.soundq>=1) DoAYSQHQ(0); else*/ DoAYSQ(0);break;
61               case 2:
62               case 3:
63               case 9:/*if(FSettings.soundq>=1) DoAYSQHQ(1); else*/ DoAYSQ(1);break;
64               case 4:
65               case 5:
66               case 10:/*if(FSettings.soundq>=1) DoAYSQHQ(2); else*/ DoAYSQ(2);break;
67               case 7:
68                      for(x=0;x<2;x++)
69                         /*if(FSettings.soundq>=1) DoAYSQHQ(x); else*/ DoAYSQ(x);
70                      break;
71              }
72              MapperExRAM[sunindex]=V;
73 }
74
75 static DECLFW(Mapper69_write)
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;
97                 case 9:ROM_BANK8(0x8000,V);break;
98                 case 0xa:ROM_BANK8(0xa000,V);break;
99                 case 0xb:ROM_BANK8(0xc000,V);break;
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;
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;
112              }
113              break;
114  }
115 }
116
117 static int32 vcount[3];
118 static int32 dcount[3];
119 static int CAYBC[3];
120
121 static void DoAYSQ(int x)
122 {
123     int32 freq=((MapperExRAM[x<<1]|((MapperExRAM[(x<<1)+1]&15)<<8))+1)<<(4+17);
124     int32 amp=(MapperExRAM[0x8+x]&15)<<2;
125     int32 start,end;
126     int V;
127
128     amp+=amp>>1;
129
130     start=CAYBC[x];
131     end=(SOUNDTS<<16)/soundtsinc;
132     if(end<=start) return;
133     CAYBC[x]=end;
134
135     if(amp)
136         if(!(MapperExRAM[0x7]&(1<<x)))
137     for(V=start;V<end;V++)
138     {
139      if(dcount[x])
140       Wave[V>>4]+=amp;
141      vcount[x]-=nesincsize;
142      while(vcount[x]<=0)
143      {
144       dcount[x]^=1;
145       vcount[x]+=freq;
146      }
147     }
148 }
149
150 #if 0
151 static void DoAYSQHQ(int x)
152 {
153  int32 V;
154  int32 freq=((MapperExRAM[x<<1]|((MapperExRAM[(x<<1)+1]&15)<<8))+1)<<4;
155  int32 amp=(MapperExRAM[0x8+x]&15)<<6;
156
157  amp+=amp>>1;
158
159  if(!(MapperExRAM[0x7]&(1<<x)))
160  {
161   for(V=CAYBC[x];V<SOUNDTS;V++)
162   {
163    if(dcount[x])
164     WaveHi[V]+=amp;
165    vcount[x]--;
166    if(vcount[x]<=0)
167    {
168     dcount[x]^=1;
169     vcount[x]=freq;
170    }
171   }
172  }
173  CAYBC[x]=SOUNDTS;
174 }
175 #endif
176
177 static void AYSound(int Count)
178 {
179     int x;
180     DoAYSQ(0);
181     DoAYSQ(1);
182     DoAYSQ(2);
183     for(x=0;x<3;x++)
184      CAYBC[x]=Count;
185 }
186
187 #if 0
188 static void AYSoundHQ(void)
189 {
190     DoAYSQHQ(0);
191     DoAYSQHQ(1);
192     DoAYSQHQ(2);
193 }
194 #endif
195
196 static void AYHiSync(int32 ts)
197 {
198  int x;
199
200  for(x=0;x<3;x++)
201   CAYBC[x]=ts;
202 }
203
204 static void FP_FASTAPASS(1) SunIRQHook(int a)
205 {
206   if(IRQa)
207   {
208    IRQCount-=a;
209    if(IRQCount<=0)
210    {X6502_IRQBegin(FCEU_IQEXT);IRQa=0;IRQCount=0xFFFF;}
211   }
212 }
213
214 void Mapper69_StateRestore(int version)
215 {
216    if(mapbyte1[1]&0x40)
217    {
218     if(mapbyte1[1]&0x80) // Select WRAM
219      setprg8r(0x10,0x6000,0);
220    }
221    else
222     setprg8(0x6000,mapbyte1[1]);
223 }
224
225 void Mapper69_ESI(void)
226 {
227  GameExpSound.RChange=Mapper69_ESI;
228  GameExpSound.HiSync=AYHiSync;
229  memset(dcount,0,sizeof(dcount));
230  memset(vcount,0,sizeof(vcount));
231  memset(CAYBC,0,sizeof(CAYBC));
232 }
233
234 void NSFAY_Init(void)
235 {
236  sunindex=0;
237  SetWriteHandler(0xc000,0xdfff,Mapper69_SWL);
238  SetWriteHandler(0xe000,0xffff,Mapper69_SWH);
239  Mapper69_ESI();
240 }
241
242 void Mapper69_init(void)
243 {
244  sunindex=0;
245
246  SetupCartPRGMapping(0x10,WRAM,8192,1);
247
248  SetWriteHandler(0x8000,0xbfff,Mapper69_write);
249  SetWriteHandler(0xc000,0xdfff,Mapper69_SWL);
250  SetWriteHandler(0xe000,0xffff,Mapper69_SWH);
251  SetWriteHandler(0x6000,0x7fff,SUN5BWRAM);
252  SetReadHandler(0x6000,0x7fff,SUN5AWRAM);
253  Mapper69_ESI();
254  MapIRQHook=SunIRQHook;
255  MapStateRestore=Mapper69_StateRestore;
256 }
257