1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Xodnizel
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 static uint16 IRQCount;
26 static uint8 WRAM[8192];
27 static uint8 IRAM[128];
31 return(WRAM[A-0x6000]);
39 void Mapper19_ESI(void);
41 static uint8 NTAPage[4];
47 static void NamcoSound(int Count);
48 static void NamcoSoundHack(void);
49 static void DoNamcoSound(int32 *Wave, int Count);
50 static void DoNamcoSoundHQ(void);
51 static void SyncHQ(int32 ts);
53 static int is210; /* Lesser mapper. */
58 static SFORMAT N106_StateRegs[]={
65 static void SyncPRG(void)
67 setprg8(0x8000,PRG[0]);
68 setprg8(0xa000,PRG[1]);
69 setprg8(0xc000,PRG[2]);
73 static void NamcoIRQHook(int a)
80 X6502_IRQBegin(FCEU_IQEXT);
82 IRQCount=0x7FFF; //7FFF;
87 static DECLFR(Namco_Read4800)
89 uint8 ret=IRAM[dopol&0x7f];
90 /* Maybe I should call NamcoSoundHack() here? */
93 dopol=(dopol&0x80)|((dopol+1)&0x7f);
97 static DECLFR(Namco_Read5000)
102 static DECLFR(Namco_Read5800)
107 static void DoNTARAMROM(int w, uint8 V)
111 setntamem(NTARAM+((V&1)<<10), 1, w);
115 setntamem(CHRptr[0]+(V<<10), 0, w);
119 static void FixNTAR(void)
123 DoNTARAMROM(x,NTAPage[x]);
126 static void DoCHRRAMROM(int x, uint8 V)
129 if(!is210 && !((gorfus>>((x>>2)+6))&1) && (V>=0xE0))
131 // printf("BLAHAHA: %d, %02x\n",x,V);
132 //setchr1r(0x10,x<<10,V&7);
138 static void FixCRR(void)
142 DoCHRRAMROM(x,CHR[x]);
145 static DECLFW(Mapper19C0D8_write)
147 DoNTARAMROM((A-0xC000)>>11,V);
150 static uint32 FreqCache[8];
151 static uint32 EnvCache[8];
152 static uint32 LengthCache[8];
154 static void FixCache(int a,int V)
159 case 0x00:FreqCache[w]&=~0x000000FF;FreqCache[w]|=V;break;
160 case 0x02:FreqCache[w]&=~0x0000FF00;FreqCache[w]|=V<<8;break;
161 case 0x04:FreqCache[w]&=~0x00030000;FreqCache[w]|=(V&3)<<16;
162 LengthCache[w]=(8-((V>>2)&7))<<2;
164 case 0x07:EnvCache[w]=(double)(V&0xF)*576716;break;
168 static DECLFW(Mapper19_write)
171 if(A>=0x8000 && A<=0xb800)
172 DoCHRRAMROM((A-0x8000)>>11,V);
178 if(FSettings.SndRate)
181 GameExpSound.Fill=NamcoSound;
182 GameExpSound.HiFill=DoNamcoSoundHQ;
183 GameExpSound.HiSync=SyncHQ;
189 dopol=(dopol&0x80)|((dopol+1)&0x7f);
194 IRQCount&=0xFF00;IRQCount|=V;X6502_IRQEnd(FCEU_IQEXT);break;
196 IRQCount&=0x00ff;IRQCount|=(V&0x7F)<<8;
198 X6502_IRQEnd(FCEU_IQEXT);
220 static void NamcoSoundHack(void)
223 if(FSettings.soundq>=1)
228 z=((SOUNDTS<<16)/soundtsinc)>>4;
230 if(a) DoNamcoSound(&Wave[dwave], a);
234 static void NamcoSound(int Count)
237 z=((SOUNDTS<<16)/soundtsinc)>>4;
239 if(a) DoNamcoSound(&Wave[dwave], a);
243 static uint32 PlayIndex[8];
244 static int32 vcount[8];
247 #define TOINDEX (16+1)
250 static void SyncHQ(int32 ts)
264 static INLINE uint32 FetchDuff(uint32 P, uint32 envelope)
267 duff=IRAM[((IRAM[0x46+(P<<3)]+(PlayIndex[P]>>TOINDEX))&0xFF)>>1];
268 if((IRAM[0x46+(P<<3)]+(PlayIndex[P]>>TOINDEX))&1)
271 duff=(duff*envelope)>>16;
275 static void DoNamcoSoundHQ(void)
277 uint32 V; //mbg merge 7/17/06 made uint32
279 int32 cyclesuck=(((IRAM[0x7F]>>4)&7)+1)*15;
281 for(P=7;P>=(7-((IRAM[0x7F]>>4)&7));P--)
283 if((IRAM[0x44+(P<<3)]&0xE0) && (IRAM[0x47+(P<<3)]&0xF))
287 uint32 duff2,lengo,envelope;
291 envelope=EnvCache[P];
292 lengo=LengthCache[P];
294 duff2=FetchDuff(P,envelope);
295 for(V=CVBC<<1;V<SOUNDTS<<1;V++)
301 while((PlayIndex[P]>>TOINDEX)>=lengo) PlayIndex[P]-=lengo<<TOINDEX;
302 duff2=FetchDuff(P,envelope);
314 static void DoNamcoSound(int32 *Wave, int Count)
317 for(P=7;P>=7-((IRAM[0x7F]>>4)&7);P--)
319 if((IRAM[0x44+(P<<3)]&0xE0) && (IRAM[0x47+(P<<3)]&0xF))
324 uint32 duff,duff2,lengo,envelope;
328 envelope=EnvCache[P];
329 lengo=LengthCache[P];
331 if(!freq) {/*printf("Ack");*/ continue;}
334 int c=((IRAM[0x7F]>>4)&7)+1;
335 inc=(long double)(FSettings.SndRate<<15)/((long double)freq*21477272/((long double)0x400000*c*45));
338 duff=IRAM[(((IRAM[0x46+(P<<3)]+PlayIndex[P])&0xFF)>>1)];
339 if((IRAM[0x46+(P<<3)]+PlayIndex[P])&1)
342 duff2=(duff*envelope)>>19;
343 for(V=0;V<Count*16;V++)
348 if(PlayIndex[P]>=lengo)
351 duff=IRAM[(((IRAM[0x46+(P<<3)]+PlayIndex[P])&0xFF)>>1)];
352 if((IRAM[0x46+(P<<3)]+PlayIndex[P])&1)
355 duff2=(duff*envelope)>>19;
365 static void Mapper19_StateRestore(int version)
371 for(x=0x40;x<0x80;x++)
375 static void M19SC(void)
377 if(FSettings.SndRate)
381 void Mapper19_ESI(void)
383 GameExpSound.RChange=M19SC;
384 memset(vcount,0,sizeof(vcount));
385 memset(PlayIndex,0,sizeof(PlayIndex));
389 void NSFN106_Init(void)
391 SetWriteHandler(0xf800,0xffff,Mapper19_write);
392 SetWriteHandler(0x4800,0x4fff,Mapper19_write);
393 SetReadHandler(0x4800,0x4fff,Namco_Read4800);
397 static int battery=0;
399 static void N106_Power(void)
402 SetReadHandler(0x8000,0xFFFF,CartBR);
403 SetWriteHandler(0x8000,0xffff,Mapper19_write);
404 SetWriteHandler(0x4020,0x5fff,Mapper19_write);
407 SetWriteHandler(0xc000,0xdfff,Mapper19C0D8_write);
408 SetReadHandler(0x4800,0x4fff,Namco_Read4800);
409 SetReadHandler(0x5000,0x57ff,Namco_Read5000);
410 SetReadHandler(0x5800,0x5fff,Namco_Read5800);
411 NTAPage[0]=NTAPage[1]=NTAPage[2]=NTAPage[3]=0xFF;
415 SetReadHandler(0x6000,0x7FFF,AWRAM);
416 SetWriteHandler(0x6000,0x7FFF,BWRAM);
417 FCEU_CheatAddRAM(8,0x6000,WRAM);
425 FCEU_dwmemset(WRAM,0,8192);
426 FCEU_dwmemset(IRAM,0,128);
428 for(x=0x40;x<0x80;x++)
432 void Mapper19_Init(CartInfo *info)
435 battery=info->battery;
436 info->Power=N106_Power;
438 MapIRQHook=NamcoIRQHook;
439 GameStateRestore=Mapper19_StateRestore;
440 GameExpSound.RChange=M19SC;
442 if(FSettings.SndRate)
445 AddExState(WRAM, 8192, 0, "WRAM");
446 AddExState(IRAM, 128, 0, "IRAM");
447 AddExState(N106_StateRegs, ~0, 0, 0);
451 info->SaveGame[0]=WRAM;
452 info->SaveGameLen[0]=8192;
453 info->SaveGame[1]=IRAM;
454 info->SaveGameLen[1]=128;
458 static void Mapper210_StateRestore(int version)
464 void Mapper210_Init(CartInfo *info)
467 GameStateRestore=Mapper210_StateRestore;
468 info->Power=N106_Power;
469 AddExState(WRAM, 8192, 0, "WRAM");
470 AddExState(N106_StateRegs, ~0, 0, 0);