bugfixes, r171 release
[fceu.git] / fce.c
diff --git a/fce.c b/fce.c
index 814c80e..a21e5f5 100644 (file)
--- a/fce.c
+++ b/fce.c
@@ -26,8 +26,9 @@
 #include       "types.h"
 #include       "x6502.h"
 #include       "fce.h"
+#include       "fceu098.h"
 #include       "sound.h"
-#include        "svga.h"
+#include       "svga.h"
 #include       "netplay.h"
 #include       "general.h"
 #include       "endian.h"
@@ -39,6 +40,7 @@
 #include       "fds.h"
 #include       "ines.h"
 #include       "unif.h"
+#include       "vsuni.h"
 #include        "cheat.h"
 
 #include       "state.h"
@@ -47,6 +49,7 @@
 #include       "file.h"
 #include       "crc32.h"
 #include        "ppu.h"
+#include        "ppu098.h"
 
 #include        "palette.h"
 #include        "movie.h"
@@ -68,8 +71,8 @@ static void PowerPPU(void);
 
 uint64 timestampbase=0;
 
-static int ppudead=1;
-static int kook=0;
+int ppudead=1;
+int kook=0;
 
 int MMC5Hack;
 uint32 MMC5HackVROMMask;
@@ -95,7 +98,7 @@ static uint8 deemp=0;
 static int deempcnt[8];
 
 FCEUGI FCEUGameInfo;
-void (*GameInterface)(int h);
+void (*GameInterface)(int h, void *param);
 
 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
 
@@ -109,6 +112,7 @@ static writefunc *BWriteG;
 static int RWWrap=0;
 
 #ifdef ASM_6502
+#ifndef DEBUG_ASM_6502
 static void asmcpu_update(int32 cycles)
 {
  // some code from x6502.c
@@ -134,6 +138,7 @@ static void asmcpu_update(int32 cycles)
   }
  }
 }
+#endif
 
 void asmcpu_unpack(void)
 {
@@ -145,6 +150,8 @@ void asmcpu_unpack(void)
        X6502_Rebase_a();
        nes_registers[4] = X.S << 24;
        nes_registers[4]|= X.IRQlow << 8;
+       if (MapIRQHook)
+               nes_registers[4] |= 1<<16; // MapIRQHook set bit
        nes_registers[7] = (uint32)X.count << 16;
 
        // NVUB DIZC
@@ -604,23 +611,31 @@ void FCEUI_FrameSkip(int x)
 static void LineUpdate(uint8 *target)
 {
        uint32 tem;
+       int y;
 
+       /* PRefreshLine() will not get called on skipped frames.  This
+        * could cause a problem, but the solution would be rather complex,
+        * due to the current sprite 0 hit code.
+        */
        if(FSkip)
+       {
+        y=(int)SPRAM[0] + 1;
+        if(scanline==y && SpriteON) PPU_status|=0x40; // hack
+        return;
+       }
+
+       if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
        {
           if(PPU_hook)
            PRefreshLine();
+          y=(int)SPRAM[0] + 1;
+          if(scanline==y && SpriteON) PPU_status|=0x40;
        }
        else
        {
         if(ScreenON)
         {
-          if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
-           BGRender(target);
-          else
-          {
-           if(PPU_hook)
-            PRefreshLine();
-          }
+          BGRender(target);
         }
         else
         {
@@ -684,27 +699,29 @@ static void LineUpdateEnd(uint8 *target)
 
 static void PRefreshLine(void)
 {
-        uint32 vofs;
-        uint8 X1;
+         uint32 vofs;
+        int X1;
+        vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
+        void (*PPU_hook_)(uint32 A) = PPU_hook;
+
 
-        vofs = 0;
-        if (BGAdrHI) vofs = 0x1000;
+         for(X1=33;X1;X1--)
+         {
+                uint32 zz2;
+                uint32 vadr;
+
+                zz2=(RefreshAddr>>10)&3;
+                PPU_hook_(0x2000|(RefreshAddr&0xFFF));
 
-        vofs+=(RefreshAddr>>12)&7;
+                vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
+
+               PPU_hook_(vadr);
 
-        for(X1=33;X1;X1--)
-        {
-                register uint8 no;
-                register uint8 zz2;
-                zz2=(uint8)((RefreshAddr>>10)&3);
-               PPU_hook(0x2000|(RefreshAddr&0xFFF));
-                no  = vnapage[zz2][(RefreshAddr&0x3ff)];
-                PPU_hook((no<<4)+vofs);
                 if((RefreshAddr&0x1f)==0x1f)
                  RefreshAddr^=0x41F;
                 else
                  RefreshAddr++;
-        }
+         }
 }
 
 /* This high-level graphics MMC5 emulation code was written
@@ -862,30 +879,34 @@ static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
 {
          int8 X1;
+        void (*PPU_hook_)(uint32 A) = PPU_hook;
+        uint32 rfraddr = RefreshAddr;
+        uint8 *page = vnapage[(rfraddr>>10)&3];
 
          for(X1=33;X1;X1--,P+=8)
          {
                 uint8 *C;
-                uint8 cc,zz,zz2;
+                uint8 cc,zz;
                 uint32 vadr;
 
-                zz=RefreshAddr&0x1F;
-                zz2=(RefreshAddr>>10)&3;
-                PPU_hook(0x2000|(RefreshAddr&0xFFF));
-                cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
-                cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
-                vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
+                zz=rfraddr&0x1F;
+                PPU_hook_(0x2000|(rfraddr&0xFFF));
+                cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
+                cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
+                vadr=(page[rfraddr&0x3ff]<<4)+vofs;
                 C = VRAMADR(vadr);
 
                #include "fceline.h"
 
-               PPU_hook(vadr);
+               PPU_hook_(vadr);
 
-                if((RefreshAddr&0x1f)==0x1f)
-                 RefreshAddr^=0x41F;
-                else
-                 RefreshAddr++;
+                if((rfraddr&0x1f)==0x1f) {
+                 rfraddr^=0x41F;
+                page = vnapage[(rfraddr>>10)&3];
+                } else
+                 rfraddr++;
          }
+        RefreshAddr = rfraddr;
 }
 
 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
@@ -1045,6 +1066,7 @@ void ResetMapping(void)
 int GameLoaded=0;
 void CloseGame(void)
 {
+ FCEUI_StopMovie();
  if(GameLoaded)
  {
   if(FCEUGameInfo.type!=GIT_NSF)
@@ -1052,7 +1074,7 @@ void CloseGame(void)
   #ifdef NETWORK
   if(FSettings.NetworkPlay) KillNetplay();
   #endif
-  GameInterface(GI_CLOSE);
+  GameInterface(GI_CLOSE, 0);
   CloseGenie();
   GameLoaded=0;
  }
@@ -1079,6 +1101,10 @@ void ResetGameLoaded(void)
        FCEUGameInfo.vidsys=GIV_USER;
        FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
        FCEUGameInfo.inputfc=-1;
+
+       FCEUGameInfo.soundchan=0;
+       FCEUGameInfo.soundrate=0;
+        FCEUGameInfo.cspecial=0;
 }
 
 char lastLoadedGameName [2048];
@@ -1091,7 +1117,7 @@ int NSFLoad(int fp);
 FCEUGI *FCEUI_LoadGame(char *name)
 {
        char name2[512];
-       int have_movie = 0;
+       int have_movie = 0, have_ips = 0;
         int fp;
 
         //Exit=1;
@@ -1111,24 +1137,39 @@ FCEUGI *FCEUI_LoadGame(char *name)
 
         {
         char *p = name2 + strlen(name2) - 4;
-        if (strcmp(p, ".fcm") == 0)
+        if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
+        if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
+        if (have_movie || have_ips)
         {
          // movie detected
-         printf("movie detected\n");
          FCEU_fclose(fp);
          *p = 0;
          fp=FCEU_fopen(name2,"rb");
-         if (!fp && p - name2 > 2)  p[-2] = 0;
-         fp=FCEU_fopen(name2,"rb");
+         if (!fp && p - name2 > 2)
+         {
+          for (p--; p > name2 && *p != '.'; p--);
+           *p = 0;
+          fp=FCEU_fopen(name2,"rb");
+         }
          if (!fp) {
-          printf("no ROM for movie\n");
+          printf("no ROM for ips/movie\n");
           LoadGameLastError = 2;
           return 0;
          }
-         have_movie = 1;
         }
        }
 
+       // do IPS patch
+       if (have_ips)
+       {
+        FCEU_fclose(fp);
+        FILE *ips = fopen(name, "rb");
+        if (!ips) return 0;
+         fp=FCEU_fopen_forcemem(name2);
+        if (!fp) { fclose(ips); return 0; }
+        ApplyIPS(ips, fp); // closes ips
+       }
+
         GetFileBase(name2);
         if(iNESLoad(name2,fp))
          goto endlseq;
@@ -1213,9 +1254,39 @@ int FCEUI_Initialize(void)
        FSettings.UsrFirstSLine[1]=0;
         FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
        FSettings.SoundVolume=100;
+
+       FCEUI_Initialize098();
+       FCEUI_SetEmuMode(0);
+
         return 1;
 }
 
+void FCEUI_Kill(void)
+{
+ FCEU_KillGenie();
+}
+
+static void EmLoop(void);
+
+int use098code = 0;
+void (*ResetNES)(void) = 0;
+void (*FCEUI_Emulate)(void) = 0;
+
+void FCEUI_SetEmuMode(int is_new)
+{
+   use098code = is_new;
+   if (is_new)
+   {
+    ResetNES=ResetNES098;
+    FCEUI_Emulate=FCEUI_Emulate098;
+   }
+   else
+   {
+    ResetNES=ResetNES081;
+    FCEUI_Emulate=EmLoop;
+   }
+}
+
 void MMC5_hb(int);     /* Ugh ugh ugh. */
 static void DoLine(void)
 {
@@ -1234,13 +1305,13 @@ static void DoLine(void)
 #ifdef FRAMESKIP
    if(!FSkip)
 #endif
-   if(SpriteON && spork)
-    CopySprites(target);
+   if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
+   {
+    if(SpriteON && spork)
+     CopySprites(target);
 
-#ifdef FRAMESKIP
-   if(!FSkip)
-#endif
-   LineUpdateEnd(target);
+    LineUpdateEnd(target);
+   }
    sphitx=0x100;
 
    if(ScreenON || SpriteON)
@@ -1273,7 +1344,7 @@ static void DoLine(void)
 }
 
 
-void EmLoop(void)
+static void EmLoop(void)
 {
  for(;;)
  {
@@ -1286,7 +1357,10 @@ void EmLoop(void)
   if(ppudead) /* Needed for Knight Rider, possibly others. */
   {
    //memset(XBuf, 0, 320*240);
-   X6502_Run(scanlines_per_frame*(256+85));
+   //X6502_Run(scanlines_per_frame*(256+85));
+   int lines;
+   for (lines=scanlines_per_frame;lines;lines--)
+     X6502_Run(256+85);
    ppudead--;
    goto update;
   }
@@ -1375,6 +1449,12 @@ void EmLoop(void)
   }
 
 update:
+  if(Exit)
+  {
+   //CloseGame();
+   break;
+  }
+
   {
    int ssize;
 
@@ -1398,47 +1478,9 @@ update:
    }
   }
 
-  if(Exit)
-  {
-   //CloseGame();
-   break;
-  }
-
  } // for
 }
 
-#ifdef FPS
-#include <sys/time.h>
-uint64 frcount;
-#endif
-void FCEUI_Emulate(void)
-{
-       #ifdef FPS
-        uint64 starttime,end;
-        struct timeval tv;
-       frcount=0;
-        gettimeofday(&tv,0);
-        starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
-       #endif
-       EmLoop();
-
-        #ifdef FPS
-        // Probably won't work well on Windows port; for
-       // debugging/speed testing.
-       {
-        uint64 w;
-        int i,frac;
-         gettimeofday(&tv,0);
-         end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
-        w=frcount*10000000000LL/(end-starttime);
-        i=w/10000;
-        frac=w-i*10000;
-         printf("Average FPS: %d.%04d\n",i,frac);
-       }
-        #endif
-
-}
-
 void FCEUI_CloseGame(void)
 {
         Exit=1;
@@ -1463,15 +1505,16 @@ static void PowerPPU(void)
        ResetPPU();
 }
 
-void ResetNES(void)
+void ResetNES081(void)
 {
         if(!GameLoaded) return;
-        GameInterface(GI_RESETM2);
+        GameInterface(GI_RESETM2, 0);
         ResetSound();
         ResetPPU();
         X6502_Reset();
 }
 
+#ifndef DEBUG_ASM_6502
 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
 {
  int x=0;
@@ -1483,6 +1526,7 @@ static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
   ptr++;
  }
 }
+#endif
 
 void PowerNES(void)
 {
@@ -1499,15 +1543,25 @@ void PowerNES(void)
         memset(RAM,0x00,0x800);
 #endif
         ResetMapping();
-       GameInterface(GI_POWER);
         PowerSound();
        PowerPPU();
-       timestampbase=0;
+
+       if (use098code)
+        FCEUPPU_Power();
+
+       /* Have the external game hardware "powered" after the internal NES stuff.
+          Needed for the NSF code and VS System code.
+       */
+       GameInterface(GI_POWER, 0);
+        if(FCEUGameInfo.type==GIT_VSUNI)
+         FCEU_VSUniPower();
 #ifdef ASM_6502
        if (geniestage)
         GenieSetPages(0);
 #endif
+       timestampbase=0;
        X6502_Power();
+       FCEU_PowerCheats();
 }