ips patches, 0.4 r162 rel?
[fceu.git] / fce.c
diff --git a/fce.c b/fce.c
index a5cc4b0..89a54a3 100644 (file)
--- a/fce.c
+++ b/fce.c
@@ -39,6 +39,7 @@
 #include       "fds.h"
 #include       "ines.h"
 #include       "unif.h"
+#include       "vsuni.h"
 #include        "cheat.h"
 
 #include       "state.h"
@@ -94,10 +95,8 @@ uint8 PPUCHRRAM;
 static uint8 deemp=0;
 static int deempcnt[8];
 
-int tosprite=256;
-
 FCEUGI FCEUGameInfo;
-void (*GameInterface)(int h);
+void (*GameInterface)(int h, void *param);
 
 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
 
@@ -147,6 +146,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
@@ -203,6 +204,9 @@ void FlushGenieRW(void)
    ARead[x+0x8000]=AReadG[x];
    BWrite[x+0x8000]=BWriteG[x];
   }
+#ifdef ASM_6502
+  GenieSetPages(1);
+#endif
   free(AReadG);
   free(BWriteG);
   AReadG=0;
@@ -297,30 +301,63 @@ uint8 PAL=0;
 static int linestartts;
 static int tofix=0;
 
-static void ResetRL(void)
+static uint8 *Plinef;
+
+extern uint8 sprlinebuf[256+8];
+extern int32 sphitx;
+extern uint8 sphitdata;
+
+extern int spork;       /* spork the world.  Any sprites on this line?
+                           Then this will be set to 1.  Needed for zapper
+                           emulation and *gasp* sprite emulation.
+                        */
+
+static void ResetRL(uint8 *target)
 {
+ if(InputScanlineHook)
+  InputScanlineHook(0,0,0,0);
+ Plinef=target;
  linestartts=timestamp*48+X6502_GetCycleCount();
  tofix=1;
 }
 
 static INLINE void Fixit1(void);
 
-static void TryFixit1(void)
+/* faking FCEUPPU_LineUpdate() from later versions of the emu */
+static void FakedLineUpdate(void)
 {
  #define TOFIXNUM (272-0x4)
  int lastpixel;
 
- if (scanline < 240 && tofix)
+ if (scanline >= 240) return;
+
+ if (tofix || sphitx != 0x100)
  {
   lastpixel = (timestamp*48-linestartts)>>4;
   if (PAL) lastpixel += lastpixel>>4;
-
   //printf("lastpixel: %i\n", lastpixel);
+ }
 
-  if(lastpixel>=TOFIXNUM)
+ if (tofix && lastpixel>=TOFIXNUM)
+ {
+  Fixit1();
+  tofix=0;
+ }
+
+ // CheckSpriteHit()
+ if(sphitx!=0x100)
+ {
+  int l=lastpixel-16;
+  int x;
+
+  for(x=sphitx;x<(sphitx+8) && x<l;x++)
   {
-   Fixit1();
-   tofix=0;
+   if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
+   {
+    PPU_status|=0x40;
+    sphitx=0x100;
+    break;
+   }
   }
  }
 }
@@ -352,7 +389,7 @@ static DECLFR(A2002)
        /* merged */
                         uint8 ret;
 
-                       TryFixit1();
+                       FakedLineUpdate();
                         ret = PPU_status;
                         ret|=PPUGenLatch&0x1F;
                         vtoggle=0;
@@ -365,7 +402,7 @@ static DECLFR(A2002)
 static DECLFR(A200x)
 {
        /* merged */
-                       TryFixit1();
+                       FakedLineUpdate();
                         return PPUGenLatch;
 }
 
@@ -375,7 +412,7 @@ static DECLFR(A2007)
                         uint8 ret;
                        uint32 tmp=RefreshAddr&0x3FFF;
 
-                       TryFixit1();
+                       FakedLineUpdate();
 
                         ret=VRAMBuffer;
 
@@ -400,7 +437,7 @@ static DECLFR(A2007)
 static DECLFW(B2000)
 {
        /* NMI2? */
-               TryFixit1();
+               FakedLineUpdate();
                 PPUGenLatch=V;
                 PPU[0]=V;
                TempAddr&=0xF3FF;
@@ -410,7 +447,7 @@ static DECLFW(B2000)
 static DECLFW(B2001)
 {
        /* merged */
-                 TryFixit1();
+                 FakedLineUpdate();
                   PPUGenLatch=V;
                  PPU[1]=V;
                  if(V&0xE0)
@@ -455,7 +492,7 @@ static DECLFW(B2005)
 {
        /* merged */
                uint32 tmp=TempAddr;
-               TryFixit1();
+               FakedLineUpdate();
                PPUGenLatch=V;
                if (!vtoggle)
                 {
@@ -477,7 +514,7 @@ static DECLFW(B2005)
 static DECLFW(B2006)
 {
        /* merged */
-                      TryFixit1();
+                      FakedLineUpdate();
 
                        PPUGenLatch=V;
                        if(!vtoggle)
@@ -559,7 +596,6 @@ void BGRender(uint8 *target)
 }
 
 #ifdef FRAMESKIP
-int FSkip_setting=-1; // auto
 int FSkip=0;
 void FCEUI_FrameSkip(int x)
 {
@@ -568,81 +604,90 @@ void FCEUI_FrameSkip(int x)
 #endif
 
 /*     This is called at the beginning of each visible scanline */
-static void Loop6502(void)
+static void LineUpdate(uint8 *target)
 {
        uint32 tem;
-       int x;
-        uint8 *target=XBuf+scanline*320+32;
+       int y;
 
-        if(ScreenON || SpriteON)
-        {
-        /* 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.
+       /* 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.
         */
-        #ifdef FRAMESKIP
-        if(!FSkip)
+       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)
         {
-        #endif
-         if(ScreenON)
-         {
-          if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
-           BGRender(target);
-          else
-          {
-           if(PPU_hook)
-            PRefreshLine();
-          }
-         }
-         else
-         {
-          tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
-          tem|=0x40404040;
-          FCEU_dwmemset(target,tem,264);
-         }
-        #ifdef FRAMESKIP
+          BGRender(target);
         }
-        #endif
-        if (SpriteON && scanline)
-         RefreshSprite(target);
-        #ifdef FRAMESKIP
-        if(!FSkip)
+        else
         {
-        #endif
-         if(PPU[1]&0x01)
-         {
-          for(x=63;x>=0;x--)
-           ((uint32 *)target)[x]=((uint32*)target)[x]&0xF0F0F0F0;
-         }
-#ifdef GP2X
-          if((PPU[1]>>5)==0x7) block_or(target, 256, 0xc0);
-          else if(PPU[1]&0xE0) block_andor(target, 256, 0x3f, 0x40);
-          else                 block_andor(target, 256, 0x3f, 0x80);
-#else
-          if((PPU[1]>>5)==0x7)
-           for(x=63;x>=0;x--)
-            ((uint32 *)target)[x]=(((uint32*)target)[x])|0xc0c0c0c0;
-          else if(PPU[1]&0xE0)
-           for(x=63;x>=0;x--)
-            ((uint32 *)target)[x]=(((uint32*)target)[x]&0x3f3f3f3f)|0x40404040;
-          else
-            for(x=63;x>=0;x--)
-             ((uint32 *)target)[x]=(((uint32*)target)[x]&0x3f3f3f3f)|0x80808080;
-#endif
-         // black borders
-         ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
-         ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
-        #ifdef FRAMESKIP
+          tem=Pal[0]|0x40;
+          tem|=tem << 8;
+          tem|=tem << 16;
+          FCEU_dwmemset(target,tem,256);
         }
-        #endif
-       }
-       else
-       {
-        tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
-        FCEU_dwmemset(target,tem,256);
        }
+
         if(InputScanlineHook)
-         InputScanlineHook(target, scanline);
+         InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
+}
+
+
+static void LineUpdateEnd(uint8 *target)
+{
+#ifdef GP2X
+ if(ScreenON || SpriteON)  // Yes, very el-cheapo.
+ {
+  if(PPU[1]&0x01)
+   block_and(target, 256, 0x30);
+ }
+ if((PPU[1]>>5)==0x7)
+  block_or(target, 256, 0xc0);
+ else if(PPU[1]&0xE0)
+  block_or(target, 256, 0x40);
+ else
+  block_andor(target, 256, 0x3f, 0x80);
+#else
+ int x;
+
+ if(ScreenON || SpriteON)  // Yes, very el-cheapo.
+ {
+  if(PPU[1]&0x01)
+  {
+   for(x=63;x>=0;x--)
+   *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
+  }
+ }
+ if((PPU[1]>>5)==0x7)
+ {
+  for(x=63;x>=0;x--)
+   *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
+ }
+ else if(PPU[1]&0xE0)
+  for(x=63;x>=0;x--)
+   *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
+ else
+  for(x=63;x>=0;x--)
+   *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
+#endif
+
+ // black borders
+ ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
+ ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
 }
 
 #define PAL(c)  ((c)+cc)
@@ -650,27 +695,29 @@ static void Loop6502(void)
 
 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;
 
-        vofs+=(RefreshAddr>>12)&7;
+         for(X1=33;X1;X1--)
+         {
+                uint32 zz2;
+                uint32 vadr;
+
+                zz2=(RefreshAddr>>10)&3;
+                PPU_hook_(0x2000|(RefreshAddr&0xFFF));
+
+                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
@@ -828,30 +875,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
@@ -966,36 +1017,6 @@ void Fixit1(void)
 }
 
 
-/*      This is called at the beginning of all h-blanks on visible lines. */
-static void DoHBlank(void)
-{
- if(ScreenON || SpriteON)
-  FetchSpriteData();
- if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
- {
-  X6502_Run(6);
-  Fixit2();
-  X6502_Run(4);
-  GameHBIRQHook();
-  X6502_Run(85-16-10);
- }
- else
- {
-  X6502_Run(6);  // Tried 65, caused problems with Slalom(maybe others)
-  Fixit2();
-  X6502_Run(85-6-16);
- }
- if(GameHBIRQHook2 && (ScreenON || SpriteON))
-  GameHBIRQHook2();
- //PPU_hook(0,-1);
- //fprintf(stderr,"%3d: $%04x\n",scanline,RefreshAddr);
- scanline++;
- if (scanline<240)
-  ResetRL();
- X6502_Run(16);
-}
-
-
 // ============================//
 // end of new code
 // ===========================//
@@ -1041,6 +1062,7 @@ void ResetMapping(void)
 int GameLoaded=0;
 void CloseGame(void)
 {
+ FCEUI_StopMovie();
  if(GameLoaded)
  {
   if(FCEUGameInfo.type!=GIT_NSF)
@@ -1048,7 +1070,7 @@ void CloseGame(void)
   #ifdef NETWORK
   if(FSettings.NetworkPlay) KillNetplay();
   #endif
-  GameInterface(GI_CLOSE);
+  GameInterface(GI_CLOSE, 0);
   CloseGenie();
   GameLoaded=0;
  }
@@ -1078,6 +1100,7 @@ void ResetGameLoaded(void)
 }
 
 char lastLoadedGameName [2048];
+int LoadGameLastError = 0;
 int UNIFLoad(const char *name, int fp);
 int iNESLoad(const char *name, int fp);
 int FDSLoad(const char *name, int fp);
@@ -1086,10 +1109,11 @@ 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;
+        //Exit=1;
+       LoadGameLastError = 0;
         ResetGameLoaded();
 
        strncpy(name2, name, sizeof(name2));
@@ -1099,27 +1123,44 @@ FCEUGI *FCEUI_LoadGame(char *name)
        if(!fp)
         {
         FCEU_PrintError("Error opening \"%s\"!",name);
+        LoadGameLastError = 1;
         return 0;
        }
 
         {
         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)
+         {
+          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;
         }
        }
 
-       strcpy(lastLoadedGameName, name2);
+       // 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))
@@ -1133,6 +1174,8 @@ FCEUGI *FCEUI_LoadGame(char *name)
 
         FCEU_PrintError("An error occurred while loading the file.");
         FCEU_fclose(fp);
+       // format handlers may set LoadGameLastError themselves.
+       if (LoadGameLastError == 0) LoadGameLastError = 3;
         return 0;
 
         endlseq:
@@ -1160,6 +1203,9 @@ FCEUGI *FCEUI_LoadGame(char *name)
 
        if (have_movie)
                FCEUI_LoadMovie(name, 1);
+
+       strcpy(lastLoadedGameName, name2);
+
         return(&FCEUGameInfo);
 }
 
@@ -1204,29 +1250,62 @@ int FCEUI_Initialize(void)
 }
 
 void MMC5_hb(int);     /* Ugh ugh ugh. */
-static INLINE void Thingo(void)
+static void DoLine(void)
 {
-   Loop6502();
+   uint8 *target=XBuf+scanline*320+32;
+
+   LineUpdate(target);
 
    if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
 
-   // check: Battletoads & Double Dragon
-   if(tosprite>=256)
+   X6502_Run(256);
+
+   // check: Battletoads & Double Dragon, Addams Family
+   // sky glitches in SMB1 if done wrong
+   FakedLineUpdate();
+
+#ifdef FRAMESKIP
+   if(!FSkip)
+#endif
+   if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
    {
-    X6502_Run(256);
+    if(SpriteON && spork)
+     CopySprites(target);
+
+    LineUpdateEnd(target);
+   }
+   sphitx=0x100;
+
+   if(ScreenON || SpriteON)
+    FetchSpriteData();
+
+   // DoHBlank();
+   if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
+   {
+    X6502_Run(6);
+    Fixit2();
+    X6502_Run(4);
+    GameHBIRQHook();
+    X6502_Run(85-10-16);
    }
    else
    {
-     // sky glitches in SMB1 if done wrong
-     X6502_Run(tosprite);
-     PPU[2]|=0x40;
-     X6502_Run(256-tosprite);
-     tosprite = 256;
+    X6502_Run(6);  // Tried 65, caused problems with Slalom(maybe others)
+    Fixit2();
+    X6502_Run(85-6-16);
    }
-   TryFixit1();
-   DoHBlank();
+
+   if(SpriteON)
+    RefreshSprites();
+   if(GameHBIRQHook2 && (ScreenON || SpriteON))
+    GameHBIRQHook2();
+   scanline++;
+   if (scanline<240)
+    ResetRL(XBuf+scanline*320+32);
+   X6502_Run(16);
 }
 
+
 void EmLoop(void)
 {
  for(;;)
@@ -1291,7 +1370,8 @@ void EmLoop(void)
     RefreshAddr=TempAddr;
     if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
    }
-   ResetRL();
+   spork=0;
+   ResetRL(XBuf+32);
 
    X6502_Run(16-kook);
    kook ^= 1;
@@ -1312,7 +1392,7 @@ void EmLoop(void)
    for(scanline=0;scanline<240;)       // scanline is incremented in  DoLine.  Evil. :/
    {
     deempcnt[deemp]++;
-    Thingo();
+    DoLine();
    }
    if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
    for(x=1,max=0,maxref=0;x<7;x++)
@@ -1328,6 +1408,12 @@ void EmLoop(void)
   }
 
 update:
+  if(Exit)
+  {
+   //CloseGame();
+   break;
+  }
+
   {
    int ssize;
 
@@ -1351,12 +1437,6 @@ update:
    }
   }
 
-  if(Exit)
-  {
-   CloseGame();
-   break;
-  }
-
  } // for
 }
 
@@ -1419,12 +1499,24 @@ static void PowerPPU(void)
 void ResetNES(void)
 {
         if(!GameLoaded) return;
-        GameInterface(GI_RESETM2);
+        GameInterface(GI_RESETM2, 0);
         ResetSound();
         ResetPPU();
         X6502_Reset();
 }
 
+static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
+{
+ int x=0;
+ while(size)
+ {
+  *ptr=(x&4)?0xFF:0x00;
+  x++;
+  size--;
+  ptr++;
+ }
+}
+
 void PowerNES(void)
 {
         if(!GameLoaded) return;
@@ -1434,13 +1526,24 @@ void PowerNES(void)
 
         GeniePower();
 
+#ifndef DEBUG_ASM_6502
+        FCEU_MemoryRand(RAM,0x800);
+#else
         memset(RAM,0x00,0x800);
+#endif
         ResetMapping();
-       GameInterface(GI_POWER);
         PowerSound();
        PowerPPU();
+       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();
 }