1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 /* TODO: Add code to put a delay in between the time a disk is inserted
39 and the when it can be successfully read/written to. This should
40 prevent writes to wrong places OR add code to prevent disk ejects
41 when the virtual motor is on(mmm...virtual motor).
44 static DECLFR(FDSRead4030);
45 static DECLFR(FDSRead4031);
46 static DECLFR(FDSRead4032);
47 static DECLFR(FDSRead4033);
48 static DECLFW(FDSWrite4020);
49 static DECLFW(FDSWrite4021);
50 static DECLFW(FDSWrite4022);
51 static DECLFW(FDSWrite4023);
52 static DECLFW(FDSWrite4024);
53 static DECLFW(FDSWrite4025);
54 static DECLFW(FDSWaveWrite);
55 static DECLFR(FDSWaveRead);
57 static DECLFR(FDSSRead);
58 static DECLFW(FDSSWrite);
59 static DECLFR(FDSBIOSRead);
60 static DECLFR(FDSRAMRead);
61 static DECLFW(FDSRAMWrite);
62 static void FDSInit(void);
63 static void FDSReset(void);
64 static void FP_FASTAPASS(1) FDSFix(int a);
66 #define FDSRAM GameMemBlock
67 #define mapbyte1 (GameMemBlock+32768)
68 #define mapbyte2 (GameMemBlock+32768+8)
69 #define mapbyte3 (GameMemBlock+32768+16)
70 #define mapbyte4 (GameMemBlock+32768+24) // 8 bytes
71 #define CHRRAM (GameMemBlock+32768+32+64)
73 #define IRQLatch (*(int32*)(CHRRAM+8192))
74 #define IRQCount (*(int32*)(CHRRAM+8192+4))
75 #define IRQa (*(CHRRAM+8192+8))
77 static void FDSClose(void);
78 static uint8 header[16];
79 #define writeskip mapbyte2[0]
81 static char FDSSaveName[2048];
84 uint8 *diskdata[4]={0,0,0,0};
86 #define SideWrite mapbyte2[1]
87 #define DiskPtr (*(uint32*)(mapbyte2+4))
88 #define dsr0 mapbyte2[2]
89 #define dsr1 mapbyte2[3]
93 #define DiskSeekIRQ (*(int32*)(mapbyte3+4))
94 #define SelectDisk mapbyte3[0]
95 #define InDisk mapbyte3[1]
97 static void FDSReset(void)
100 memset(mapbyte2,0,8);
101 memset(mapbyte3+4,0,4);
102 memset(mapbyte4,0,8);
109 case GI_CLOSE: FDSClose();break;
110 case GI_POWER: FDSReset();FDSInit();break;
114 static void FDSStateRestore(int version)
116 setmirror(((mapbyte1[5]&8)>>3)^1);
120 void FDSSoundReset(void);
121 void FDSSoundStateAdd(void);
122 static void RenderSound(void);
124 static void FDSInit(void)
132 setprg8r(0,0xe000,0); // BIOS
133 setprg32r(1,0x6000,0); // 32KB RAM
134 setchr8(0); // 8KB CHR RAM
137 GameStateRestore=FDSStateRestore;
139 SetReadHandler(0x4030,0x4030,FDSRead4030);
140 SetReadHandler(0x4031,0x4031,FDSRead4031);
141 SetReadHandler(0x4032,0x4032,FDSRead4032);
142 SetReadHandler(0x4033,0x4033,FDSRead4033);
144 SetWriteHandler(0x4020,0x4020,FDSWrite4020);
145 SetWriteHandler(0x4021,0x4021,FDSWrite4021);
146 SetWriteHandler(0x4022,0x4022,FDSWrite4022);
147 SetWriteHandler(0x4023,0x4023,FDSWrite4023);
148 SetWriteHandler(0x4024,0x4024,FDSWrite4024);
149 SetWriteHandler(0x4025,0x4025,FDSWrite4025);
151 SetWriteHandler(0x6000,0xdfff,FDSRAMWrite);
152 SetReadHandler(0x6000,0xdfff,FDSRAMRead);
153 SetReadHandler(0xE000,0xFFFF,FDSBIOSRead);
155 IRQCount=IRQLatch=IRQa=0;
160 void FDSControl(int what)
164 case FDS_IDISK:dsr1&=0xFE;
167 FCEU_DispMessage("Disk %d Side %s Inserted",
168 SelectDisk>>1,(SelectDisk&1)?"B":"A");
172 FCEU_DispMessage("Jamming disks is a BAD IDEA");
176 FCEU_DispMessage("Disk Ejected");
178 FCEU_DispMessage("Cannot Eject Air");
185 FCEU_DispMessage("Eject disk before selecting.");
188 SelectDisk=((SelectDisk+1)%header[4])&3;
189 FCEU_DispMessage("Disk %d Side %s Selected",
190 SelectDisk>>1,(SelectDisk&1)?"B":"A");
195 static void FP_FASTAPASS(1) FDSFix(int a)
206 X6502_IRQBegin(FCEU_IQEXT);
224 void DiskControl(int which)
231 if(DiskPtr<64999) DiskPtr++;
232 //DiskSeekIRQ=160+100;
240 static DECLFR(FDSRead4030)
242 X6502_IRQEnd(FCEU_IQEXT);
246 static DECLFR(FDSRead4031)
251 z=diskdata[InDisk][DiskPtr];
257 static DECLFR(FDSRead4032)
262 static DECLFR(FDSRead4033)
264 return 0x80; // battery
267 static DECLFW(FDSRAMWrite)
269 (FDSRAM-0x6000)[A]=V;
272 static DECLFR(FDSBIOSRead)
274 return (FDSBIOS-0xE000)[A];
277 static DECLFR(FDSRAMRead)
279 return (FDSRAM-0x6000)[A];
282 /* Begin FDS sound */
284 #define FDSClock (1789772.7272727272727272/8)
287 int64 cycles; // Cycles per PCM sample
288 int64 count; // Cycle counter
289 int64 envcount; // Envelope cycle counter
290 uint32 b19shiftreg60;
294 int32 clockcount; // Counter to divide frequency by 8.
295 uint8 b8shiftreg88; // Modulation register.
296 uint8 amplitude[2]; // Current amplitudes.
297 uint8 mwave[0x20]; // Modulation waveform
298 uint8 cwave[0x40]; // Game-defined waveform(carrier)
302 static FDSSOUND fdso;
304 #define SPSG fdso.SPSG
305 #define b19shiftreg60 fdso.b19shiftreg60
306 #define b24adder66 fdso.b24adder66
307 #define b24latch68 fdso.b24latch68
308 #define b17latch76 fdso.b17latch76
309 #define b8shiftreg88 fdso.b8shiftreg88
310 #define clockcount fdso.clockcount
311 #define amplitude fdso.amplitude
313 void FDSSoundStateAdd(void)
315 AddExState(fdso.cwave,64,0,"WAVE");
316 AddExState(fdso.mwave,32,0,"MWAV");
317 AddExState(amplitude,2,0,"AMPL");
318 AddExState(SPSG,0xB,0,"SPSG");
320 AddExState(&b8shiftreg88,1,0,"B88");
322 AddExState(&clockcount, 4, 1, "CLOC");
323 AddExState(&b19shiftreg60,4,1,"B60");
324 AddExState(&b24adder66,4,1,"B66");
325 AddExState(&b24latch68,4,1,"B68");
326 AddExState(&b17latch76,4,1,"B76");
330 static DECLFR(FDSSRead)
334 case 0x0:return(amplitude[0]|(X.DB&0xC0));
335 case 0x2:return(amplitude[1]|(X.DB&0xC0));
340 static DECLFW(FDSSWrite)
342 if(FSettings.SndRate)
351 // if(V&0x40) amplitude[(A&0xF)>>2]=0;
352 // else amplitude[(A&0xF)>>2]=0x3F;
355 amplitude[(A&0xF)>>2]=V&0x3F;
357 case 0x7: b17latch76=0;SPSG[0x5]=0;break;
359 //printf("%d:$%02x\n",SPSG[0x5],V);
360 fdso.mwave[SPSG[0x5]&0x1F]=V&0x7;
361 SPSG[0x5]=(SPSG[0x5]+1)&0x1F;
364 //if(A>=0x7 && A!=0x8 && A<=0xF)
365 //if(A==0xA || A==0x9) printf("$%04x:$%02x\n",A,V);
369 // $4080 - Fundamental wave amplitude data register 92
370 // $4082 - Fundamental wave frequency data register 58
371 // $4083 - Same as $4082($4083 is the upper 4 bits).
373 // $4084 - Modulation amplitude data register 78
374 // $4086 - Modulation frequency data register 72
375 // $4087 - Same as $4086($4087 is the upper 4 bits)
383 if(!(SPSG[x<<2]&0x80) && !(SPSG[0x9+x]&0x80))
385 static int counto[2]={0,0};
391 if(amplitude[x]<0x3F)
399 counto[x]=(SPSG[x<<2]&0x3F);
406 static DECLFR(FDSWaveRead)
408 return(fdso.cwave[A&0x3f]|(X.DB&0xC0));
411 static DECLFW(FDSWaveWrite)
414 fdso.cwave[A&0x3f]=V&0x3F;
417 static INLINE void ClockRise(void)
421 b19shiftreg60=(SPSG[0x2]|((SPSG[0x3]&0xF)<<8));
422 b17latch76=(SPSG[0x6]|((SPSG[0x07]&0x3)<<8))+b17latch76;
424 if(!(SPSG[0x7]&0x80))
426 b8shiftreg88=(amplitude[1]*((fdso.mwave[(b17latch76>>11)&0x1F]&7)));
427 //b8shiftreg88=((fdso.mwave[(b17latch76>>11)&0x1F]&7))|(amplitude[1]<<3);
437 b24adder66=(b24latch68+b19shiftreg60)&0xFFFFFF;
440 static INLINE void ClockFall(void)
442 // if(!(SPSG[0x7]&0x80))
444 if(!(b8shiftreg88&1))
445 b24latch68=b24adder66;
447 clockcount=(clockcount+1)&7;
450 static INLINE int32 FDSDoSound(void)
452 fdso.count+=fdso.cycles;
453 if(fdso.count>=((int64)1<<40))
456 fdso.count-=(int64)1<<40;
460 if(fdso.count>=32768) goto dogk;
462 fdso.envcount-=fdso.cycles;
466 fdso.envcount+=((int64)1<<40)*FDSClock/1024;
470 // Might need to emulate applying the amplitude to the waveform a bit better...
471 return (fdso.cwave[b24latch68>>18]*amplitude[0]);
476 static void RenderSound(void)
482 end=(SOUNDTS<<16)/soundtsinc;
487 if(!(SPSG[0x9]&0x80))
488 for(x=start;x<end;x++)
490 uint32 t=FDSDoSound();
501 static void FDS_ESI(void)
503 if(FSettings.SndRate)
505 fdso.cycles=((int64)1<<40)*FDSClock;
506 fdso.cycles/=FSettings.SndRate OVERSAMPLE;
508 // fdso.cycles=(int64)32768*FDSClock/(FSettings.SndRate OVERSAMPLE);
509 SetReadHandler(0x4040,0x407f,FDSWaveRead);
510 SetWriteHandler(0x4040,0x407f,FDSWaveWrite);
511 SetWriteHandler(0x4080,0x408A,FDSSWrite);
512 SetReadHandler(0x4090,0x4092,FDSSRead);
515 void FDSSoundReset(void)
517 memset(&fdso,0,sizeof(fdso));
519 GameExpSound.Fill=FDSSound;
520 GameExpSound.RChange=FDS_ESI;
524 static DECLFW(FDSWrite4020)
526 X6502_IRQEnd(FCEU_IQEXT);
531 static DECLFW(FDSWrite4021)
533 X6502_IRQEnd(FCEU_IQEXT);
538 static DECLFW(FDSWrite4022)
540 X6502_IRQEnd(FCEU_IQEXT);
545 static DECLFW(FDSWrite4023)
549 static DECLFW(FDSWrite4024)
551 if(InDisk!=255 && !(mapbyte1[5]&0x4) && mapbyte1[3]&0x1)
553 if(DiskPtr>=0 && DiskPtr<65000)
555 if(writeskip) writeskip--;
558 SideWrite|=1<<InDisk;
559 diskdata[InDisk][DiskPtr-2]=V;
564 static DECLFW(FDSWrite4025)
570 if(mapbyte1[5]&0x40 && !(V&0x10))
575 if(DiskPtr<0) DiskPtr=0;
577 if(!(V&0x4)) writeskip=2;
578 if(V&2) {DiskPtr=0;DiskSeekIRQ=200;}
579 if(V&0x40) DiskSeekIRQ=200;
582 setmirror(((V>>3)&1)^1);
584 static void FreeFDSMemory(void)
588 for(x=0;x<header[4];x++)
596 int FDSLoad(char *name, int fp)
601 FCEU_fseek(fp,0,SEEK_SET);
602 FCEU_fread(header,16,1,fp);
604 if(memcmp(header,"FDS\x1a",4))
606 if(!(memcmp(header+1,"*NINTENDO-HVC*",14)))
614 FCEU_fseek(fp,0,SEEK_SET);
620 if(header[4]>4) header[4]=4;
621 if(!header[4]) header[4]|=1;
622 for(x=0;x<header[4];x++)
624 diskdata[x]=FCEU_malloc(65500);
628 for(zol=0;zol<x;zol++)
632 FCEU_fread(diskdata[x],1,65500,fp);
635 if(!(zp=fopen(FCEU_MakeFName(FCEUMKF_FDSROM,0,0),"rb")))
637 FCEU_PrintError("FDS BIOS ROM image missing!");
642 if(fread(FDSBIOS,1,8192,zp)!=8192)
646 FCEU_PrintError("Error reading FDS BIOS ROM image");
652 FCEUGameInfo.type=GIT_FDS;
653 strcpy(FDSSaveName,name);
662 for(x=0;x<header[4];x++)
665 sprintf(temp,"DDT%d",x);
666 AddExState(diskdata[x],65500,0,temp);
669 AddExState(FDSRAM,32768,0,"FDSR");
670 AddExState(mapbyte1,32,0,"MPBY");
671 AddExState(CHRRAM,8192,0,"CHRR");
672 AddExState(&IRQCount, 4, 1, "IRQC");
673 AddExState(&IRQLatch, 4, 1, "IQL1");
674 AddExState(&IRQa, 1, 0, "IRQA");
679 SetupCartCHRMapping(0,CHRRAM,8192,1);
680 SetupCartMirroring(0,0,0);
690 fp=FCEU_fopen(FDSSaveName,"wb");
693 if(header[0]) // Does it have a 16-byte FWNES-style header?
694 if(FCEU_fwrite(header,1,16,fp)!=16) // Write it out. Should be nicer than fseek()'ing if the file is compressed. Hopefully...
697 for(x=0;x<header[4];x++)
699 if(FCEU_fwrite(diskdata[x],1,65500,fp)!=65500)
702 FCEU_PrintError("Error writing FDS image \"%s\"!",FDSSaveName);