non-asm compatibility re-fixed
[fceu.git] / boards / n106.c
CommitLineData
d97315ac 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
23static uint16 IRQCount;
24static uint8 IRQa;
25
26static uint8 WRAM[8192];
27static uint8 IRAM[128];
28
29static DECLFR(AWRAM)
30{
31 return(WRAM[A-0x6000]);
32}
33
34static DECLFW(BWRAM)
35{
36 WRAM[A-0x6000]=V;
37}
38
39void Mapper19_ESI(void);
40
41static uint8 NTAPage[4];
42
43static uint8 dopol;
44static uint8 gorfus;
45static uint8 gorko;
46
47static void NamcoSound(int Count);
48static void NamcoSoundHack(void);
49static void DoNamcoSound(int32 *Wave, int Count);
50//static void DoNamcoSoundHQ(void);
51static void SyncHQ(int32 ts);
52
53static int is210; /* Lesser mapper. */
54
55static uint8 PRG[3];
56static uint8 CHR[8];
57
58static SFORMAT N106_StateRegs[]={
59 {PRG,3,"PRG"},
60 {CHR,8,"CHR"},
61 {NTAPage,4,"NTA"},
62 {0}
63};
64
65static void SyncPRG(void)
66{
67 setprg8(0x8000,PRG[0]);
68 setprg8(0xa000,PRG[1]);
69 setprg8(0xc000,PRG[2]);
70 setprg8(0xe000,0x3F);
71}
72
73static void FP_FASTAPASS(1) NamcoIRQHook(int a)
74{
75 if(IRQa)
76 {
77 IRQCount+=a;
78 if(IRQCount>=0x7FFF)
79 {
80 X6502_IRQBegin(FCEU_IQEXT);
81 IRQa=0;
82 IRQCount=0x7FFF; //7FFF;
83 }
84 }
85}
86
87static DECLFR(Namco_Read4800)
88{
89 uint8 ret=IRAM[dopol&0x7f];
90 /* Maybe I should call NamcoSoundHack() here? */
91 if(!fceuindbg)
92 if(dopol&0x80)
93 dopol=(dopol&0x80)|((dopol+1)&0x7f);
94 return ret;
95}
96
97static DECLFR(Namco_Read5000)
98{
99 return(IRQCount);
100}
101
102static DECLFR(Namco_Read5800)
103{
104 return(IRQCount>>8);
105}
106
107static void FASTAPASS(2) DoNTARAMROM(int w, uint8 V)
108{
109 NTAPage[w]=V;
110 if(V>=0xE0)
111 setntamem(NTARAM+((V&1)<<10), 1, w);
112 else
113 {
114 V&=CHRmask1[0];
115 setntamem(CHRptr[0]+(V<<10), 0, w);
116 }
117}
118
119static void FixNTAR(void)
120{
121 int x;
122 for(x=0;x<4;x++)
123 DoNTARAMROM(x,NTAPage[x]);
124}
125
126static void FASTAPASS(2) DoCHRRAMROM(int x, uint8 V)
127{
128 CHR[x]=V;
129 if(!is210 && !((gorfus>>((x>>2)+6))&1) && (V>=0xE0))
130 {
131 // printf("BLAHAHA: %d, %02x\n",x,V);
132 //setchr1r(0x10,x<<10,V&7);
133 }
134 else
135 setchr1(x<<10,V);
136}
137
138static void FixCRR(void)
139{
140 int x;
141 for(x=0;x<8;x++)
142 DoCHRRAMROM(x,CHR[x]);
143}
144
145static DECLFW(Mapper19C0D8_write)
146{
147 DoNTARAMROM((A-0xC000)>>11,V);
148}
149
150static uint32 FreqCache[8];
151static uint32 EnvCache[8];
152static uint32 LengthCache[8];
153
154static void FixCache(int a,int V)
155{
156 int w=(a>>3)&0x7;
157 switch(a&0x07)
158 {
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;
163 break;
164 case 0x07:EnvCache[w]=(double)(V&0xF)*576716;break;
165 }
166}
167
168static DECLFW(Mapper19_write)
169{
170 A&=0xF800;
171 if(A>=0x8000 && A<=0xb800)
172 DoCHRRAMROM((A-0x8000)>>11,V);
173 else switch(A)
174 {
175 case 0x4800:
176 if(dopol&0x40)
177 {
178 if(FSettings.SndRate)
179 {
180 NamcoSoundHack();
181 GameExpSound.Fill=NamcoSound;
182 GameExpSound.HiFill=0;//DoNamcoSoundHQ;
183 GameExpSound.HiSync=SyncHQ;
184 }
185 FixCache(dopol,V);
186 }
187 IRAM[dopol&0x7f]=V;
188 if(dopol&0x80)
189 dopol=(dopol&0x80)|((dopol+1)&0x7f);
190 break;
191 case 0xf800:
192 dopol=V;break;
193 case 0x5000:
194 IRQCount&=0xFF00;IRQCount|=V;X6502_IRQEnd(FCEU_IQEXT);break;
195 case 0x5800:
196 IRQCount&=0x00ff;IRQCount|=(V&0x7F)<<8;
197 IRQa=V&0x80;
198 X6502_IRQEnd(FCEU_IQEXT);
199 break;
200 case 0xE000:
201 gorko=V&0xC0;
202 PRG[0]=V&0x3F;
203 SyncPRG();
204 break;
205 case 0xE800:
206 gorfus=V&0xC0;
207 FixCRR();
208 PRG[1]=V&0x3F;
209 SyncPRG();
210 break;
211 case 0xF000:
212 PRG[2]=V&0x3F;
213 SyncPRG();
214 break;
215 }
216}
217
218static int dwave=0;
219
220static void NamcoSoundHack(void)
221{
222 int32 z,a;
223#if 0
224 if(FSettings.soundq>=1)
225 {
226 DoNamcoSoundHQ();
227 return;
228 }
229#endif
230 z=((SOUNDTS<<16)/soundtsinc)>>4;
231 a=z-dwave;
232 if(a) DoNamcoSound(&Wave[dwave], a);
233 dwave+=a;
234}
235
236static void NamcoSound(int Count)
237{
238 int32 z,a;
239 z=((SOUNDTS<<16)/soundtsinc)>>4;
240 a=z-dwave;
241 if(a) DoNamcoSound(&Wave[dwave], a);
242 dwave=0;
243}
244
245static uint32 PlayIndex[8];
246static int32 vcount[8];
247static int32 CVBC;
248
249#define TOINDEX (16+1)
250
251// 16:15
252static void SyncHQ(int32 ts)
253{
254 CVBC=ts;
255}
256
257
258/* Things to do:
259 1 Read freq low
260 2 Read freq mid
261 3 Read freq high
262 4 Read envelope
263 ...?
264*/
265
266static INLINE uint32 FetchDuff(uint32 P, uint32 envelope)
267{
268 uint32 duff;
269 duff=IRAM[((IRAM[0x46+(P<<3)]+(PlayIndex[P]>>TOINDEX))&0xFF)>>1];
270 if((IRAM[0x46+(P<<3)]+(PlayIndex[P]>>TOINDEX))&1)
271 duff>>=4;
272 duff&=0xF;
273 duff=(duff*envelope)>>16;
274 return(duff);
275}
276
277#if 0
278static void DoNamcoSoundHQ(void)
279{
280 int32 P,V;
281 int32 cyclesuck=(((IRAM[0x7F]>>4)&7)+1)*15;
282
283 for(P=7;P>=(7-((IRAM[0x7F]>>4)&7));P--)
284 {
285 if((IRAM[0x44+(P<<3)]&0xE0) && (IRAM[0x47+(P<<3)]&0xF))
286 {
287 uint32 freq;
288 int32 vco;
289 uint32 duff2,lengo,envelope;
290
291 vco=vcount[P];
292 freq=FreqCache[P];
293 envelope=EnvCache[P];
294 lengo=LengthCache[P];
295
296 duff2=FetchDuff(P,envelope);
297 for(V=CVBC<<1;V<SOUNDTS<<1;V++)
298 {
299 WaveHi[V>>1]+=duff2;
300 if(!vco)
301 {
302 PlayIndex[P]+=freq;
303 while((PlayIndex[P]>>TOINDEX)>=lengo) PlayIndex[P]-=lengo<<TOINDEX;
304 duff2=FetchDuff(P,envelope);
305 vco=cyclesuck;
306 }
307 vco--;
308 }
309 vcount[P]=vco;
310 }
311 }
312 CVBC=SOUNDTS;
313}
314#endif
315
316static void DoNamcoSound(int32 *Wave, int Count)
317{
318 int P,V;
319 for(P=7;P>=7-((IRAM[0x7F]>>4)&7);P--)
320 {
321 if((IRAM[0x44+(P<<3)]&0xE0) && (IRAM[0x47+(P<<3)]&0xF))
322 {
323 int32 inc;
324 uint32 freq;
325 int32 vco;
326 uint32 duff,duff2,lengo,envelope;
327
328 vco=vcount[P];
329 freq=FreqCache[P];
330 envelope=EnvCache[P];
331 lengo=LengthCache[P];
332
333 if(!freq) {/*printf("Ack");*/ continue;}
334
335 {
336 int c=((IRAM[0x7F]>>4)&7)+1;
337 inc=(long double)(FSettings.SndRate<<15)/((long double)freq*21477272/((long double)0x400000*c*45));
338 }
339
340 duff=IRAM[(((IRAM[0x46+(P<<3)]+PlayIndex[P])&0xFF)>>1)];
341 if((IRAM[0x46+(P<<3)]+PlayIndex[P])&1)
342 duff>>=4;
343 duff&=0xF;
344 duff2=(duff*envelope)>>19;
345 for(V=0;V<Count*16;V++)
346 {
347 if(vco>=inc)
348 {
349 PlayIndex[P]++;
350 if(PlayIndex[P]>=lengo)
351 PlayIndex[P]=0;
352 vco-=inc;
353 duff=IRAM[(((IRAM[0x46+(P<<3)]+PlayIndex[P])&0xFF)>>1)];
354 if((IRAM[0x46+(P<<3)]+PlayIndex[P])&1)
355 duff>>=4;
356 duff&=0xF;
357 duff2=(duff*envelope)>>19;
358 }
359 Wave[V>>4]+=duff2;
360 vco+=0x8000;
361 }
362 vcount[P]=vco;
363 }
364 }
365}
366
367static void Mapper19_StateRestore(int version)
368{
369 SyncPRG();
370 FixNTAR();
371 FixCRR();
372 int x;
373 for(x=0x40;x<0x80;x++)
374 FixCache(x,IRAM[x]);
375}
376
377static void M19SC(void)
378{
379 if(FSettings.SndRate)
380 Mapper19_ESI();
381}
382
383void Mapper19_ESI(void)
384{
385 GameExpSound.RChange=M19SC;
386 memset(vcount,0,sizeof(vcount));
387 memset(PlayIndex,0,sizeof(PlayIndex));
388 CVBC=0;
389}
390
391void NSFN106_Init(void)
392{
393 SetWriteHandler(0xf800,0xffff,Mapper19_write);
394 SetWriteHandler(0x4800,0x4fff,Mapper19_write);
395 SetReadHandler(0x4800,0x4fff,Namco_Read4800);
396 Mapper19_ESI();
397}
398
399static int battery=0;
400
401static void N106_Power(void)
402{
403 int x;
404 SetReadHandler(0x8000,0xFFFF,CartBR);
405 SetWriteHandler(0x8000,0xffff,Mapper19_write);
406 SetWriteHandler(0x4020,0x5fff,Mapper19_write);
407 if(!is210)
408 {
409 SetWriteHandler(0xc000,0xdfff,Mapper19C0D8_write);
410 SetReadHandler(0x4800,0x4fff,Namco_Read4800);
411 SetReadHandler(0x5000,0x57ff,Namco_Read5000);
412 SetReadHandler(0x5800,0x5fff,Namco_Read5800);
413 NTAPage[0]=NTAPage[1]=NTAPage[2]=NTAPage[3]=0xFF;
414 FixNTAR();
415 }
416
417 SetReadHandler(0x6000,0x7FFF,AWRAM);
418 SetWriteHandler(0x6000,0x7FFF,BWRAM);
419 FCEU_CheatAddRAM(8,0x6000,WRAM);
420
421 gorfus=0xFF;
422 SyncPRG();
423 FixCRR();
424
425 if(!battery)
426 {
427 FCEU_dwmemset(WRAM,0,8192);
428 FCEU_dwmemset(IRAM,0,128);
429 }
430 for(x=0x40;x<0x80;x++)
431 FixCache(x,IRAM[x]);
432}
433
434void Mapper19_Init(CartInfo *info)
435{
436 is210=0;
437 battery=info->battery;
438 info->Power=N106_Power;
439
440 MapIRQHook=NamcoIRQHook;
441 GameStateRestore=Mapper19_StateRestore;
442 GameExpSound.RChange=M19SC;
443
444 if(FSettings.SndRate)
445 Mapper19_ESI();
446
447 AddExState(WRAM, 8192, 0, "WRAM");
448 AddExState(IRAM, 128, 0, "WRAM");
449 AddExState(N106_StateRegs, ~0, 0, 0);
450
451 if(info->battery)
452 {
453 info->SaveGame[0]=WRAM;
454 info->SaveGameLen[0]=8192;
455 info->SaveGame[1]=IRAM;
456 info->SaveGameLen[1]=128;
457 }
458}
459
460static void Mapper210_StateRestore(int version)
461{
462 SyncPRG();
463 FixCRR();
464}
465
466void Mapper210_Init(CartInfo *info)
467{
468 is210=1;
469 GameStateRestore=Mapper210_StateRestore;
470 info->Power=N106_Power;
471 AddExState(WRAM, 8192, 0, "WRAM");
472}