bugfixes, new scaling, double ym upd at 940
[libpicofe.git] / gp2x / emu.c
index f43f9b7..99357b2 100644 (file)
@@ -63,21 +63,7 @@ unsigned char *framebuff = 0;  // temporary buffer for alt renderer
 int state_slot = 0;\r
 int reset_timing = 0;\r
 \r
-/*\r
-// tmp\r
-static FILE *logf = NULL;\r
 \r
-void pprintf(char *texto, ...)\r
-{\r
-       va_list args;\r
-\r
-       va_start(args,texto);\r
-       vfprintf(logf,texto,args);\r
-       va_end(args);\r
-       fflush(logf);\r
-       sync();\r
-}\r
-*/\r
 // utilities\r
 static void strlwr(char* string)\r
 {\r
@@ -467,6 +453,18 @@ static void find_combos(void)
 }\r
 \r
 \r
+void scaling_update(void)\r
+{\r
+       PicoOpt &= ~0x4100;\r
+       switch (currentConfig.scaling) {\r
+               default: break; // off\r
+               case 1:  // hw hor\r
+               case 2:  PicoOpt |=  0x0100; break; // hw hor+vert\r
+               case 3:  PicoOpt |=  0x4000; break; // sw hor\r
+       }\r
+}\r
+\r
+\r
 int emu_ReadConfig(int game)\r
 {\r
        FILE *f;\r
@@ -501,6 +499,7 @@ int emu_ReadConfig(int game)
                currentConfig.KeyBinds[22] = 1<<30; // vol down\r
                currentConfig.gamma = 100;\r
                currentConfig.PicoCDBuffers = 64;\r
+               currentConfig.scaling = 0;\r
                strncpy(cfg, PicoConfigFile, 511);\r
                cfg[511] = 0;\r
        } else {\r
@@ -516,7 +515,7 @@ int emu_ReadConfig(int game)
                bread = fread(&currentConfig, 1, sizeof(currentConfig), f);\r
                fclose(f);\r
        }\r
-       printf((bread == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
+       printf(bread > 0 ? "(ok)\n" : "(failed)\n");\r
 \r
        PicoOpt = currentConfig.PicoOpt;\r
        PsndRate = currentConfig.PsndRate;\r
@@ -527,6 +526,7 @@ int emu_ReadConfig(int game)
                actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
                actionNames[10] = "X"; actionNames[11] = "MODE";\r
        }\r
+       scaling_update();\r
        // some sanity checks\r
        if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
        if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r
@@ -536,7 +536,7 @@ int emu_ReadConfig(int game)
                currentConfig.KeyBinds[22] = 1<<30; // vol down\r
        }\r
 \r
-       return (bread == sizeof(currentConfig));\r
+       return (bread > 0); // == sizeof(currentConfig));\r
 }\r
 \r
 \r
@@ -565,7 +565,9 @@ int emu_WriteConfig(int game)
                bwrite = fwrite(&currentConfig, 1, sizeof(currentConfig), f);\r
                fflush(f);\r
                fclose(f);\r
+#ifndef NO_SYNC\r
                sync();\r
+#endif\r
        }\r
        printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
 \r
@@ -581,8 +583,23 @@ void emu_Deinit(void)
                SRam.changed = 0;\r
        }\r
 \r
-       if (!(currentConfig.EmuOpt & 0x20))\r
-               emu_WriteConfig(0);\r
+       if (!(currentConfig.EmuOpt & 0x20)) {\r
+               FILE *f = fopen(PicoConfigFile, "r+b");\r
+               if (!f) emu_WriteConfig(0);\r
+               else {\r
+                       // if we already have config, reload it, except last ROM\r
+                       fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);\r
+                       fread(&currentConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);\r
+                       fseek(f, 0, SEEK_SET);\r
+                       fwrite(&currentConfig, 1, sizeof(currentConfig), f);\r
+                       fflush(f);\r
+                       fclose(f);\r
+#ifndef NO_SYNC\r
+                       sync();\r
+#endif\r
+               }\r
+       }\r
+\r
        free(framebuff);\r
 \r
        PicoExit();\r
@@ -706,9 +723,13 @@ static void blit(const char *fps, const char *notice)
                }\r
        }\r
 \r
-       if (notice) osd_text(4, 232, notice);\r
-       if (emu_opt & 2)\r
-               osd_text(osd_fps_x, 232, fps);\r
+       if (notice || (emu_opt & 2)) {\r
+               int h = 232;\r
+               if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r
+               if (notice) osd_text(4, h, notice);\r
+               if (emu_opt & 2)\r
+                       osd_text(osd_fps_x, h, fps);\r
+       }\r
        if ((emu_opt & 0x400) && (PicoMCD & 1))\r
                cd_leds();\r
 \r
@@ -771,7 +792,9 @@ static void vidResetMode(void)
        }\r
        Pico.m.dirtyPal = 1;\r
        // reset scaling\r
-       gp2x_video_RGB_setscaling((PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
+       if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r
+            gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r
+       else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
 }\r
 \r
 \r
@@ -1133,7 +1156,9 @@ void emu_Loop(void)
                                        vidCpyM2 = vidCpyM2_32col;\r
                                }\r
                        }\r
-                       gp2x_video_RGB_setscaling(scalex, 240);\r
+                       if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
+                            gp2x_video_RGB_setscaling(8, scalex, 224);\r
+                       else gp2x_video_RGB_setscaling(0, scalex, 240);\r
                        oldmodes = modes;\r
                        clearArea(1);\r
                }\r
@@ -1171,7 +1196,7 @@ void emu_Loop(void)
                                if (frames_shown > frames_done) frames_shown = frames_done;\r
                        }\r
                }\r
-#if 0\r
+#if 1\r
                sprintf(fpsbuff, "%05i", Pico.m.frame_count);\r
 #endif\r
                lim_time = (frames_done+1) * target_frametime;\r
@@ -1446,7 +1471,9 @@ int emu_SaveLoadGame(int load, int sram)
                                ret = fwrite(sram_data, 1, sram_size, sramFile);\r
                                ret = (ret != sram_size) ? -1 : 0;\r
                                fclose(sramFile);\r
+#ifndef NO_SYNC\r
                                sync();\r
+#endif\r
                        }\r
                }\r
                return ret;\r
@@ -1470,8 +1497,10 @@ int emu_SaveLoadGame(int load, int sram)
                        ret = PmovState(load ? 6 : 5, PmovFile);\r
                        areaClose(PmovFile);\r
                        PmovFile = 0;\r
-                       if (!load) sync();\r
-                       else Pico.m.dirtyPal=1;\r
+                       if (load) Pico.m.dirtyPal=1;\r
+#ifndef NO_SYNC\r
+                       else sync();\r
+#endif\r
                }\r
                else    ret = -1;\r
                if (!ret)\r