bugfixes, cd/Memory.s
[picodrive.git] / platform / gp2x / emu.c
index 3d6cd3a..632ad18 100644 (file)
@@ -393,6 +393,8 @@ int emu_ReloadRom(void)
 }\r
 \r
 \r
+static void emu_msg_cb(const char *msg);\r
+\r
 void emu_Init(void)\r
 {\r
        // make temp buffer for alt renderer\r
@@ -409,6 +411,7 @@ void emu_Init(void)
        mkdir("cfg", 0777);\r
 \r
        PicoInit();\r
+       PicoMessage = emu_msg_cb;\r
 \r
 //     logf = fopen("log.txt", "w");\r
 }\r
@@ -496,6 +499,7 @@ int emu_ReadConfig(int game)
                currentConfig.KeyBinds[23] = 1<<29; // vol up\r
                currentConfig.KeyBinds[22] = 1<<30; // vol down\r
                currentConfig.gamma = 100;\r
+               currentConfig.PicoCDBuffers = 64;\r
                strncpy(cfg, PicoConfigFile, 511);\r
                cfg[511] = 0;\r
        } else {\r
@@ -517,6 +521,7 @@ int emu_ReadConfig(int game)
        PsndRate = currentConfig.PsndRate;\r
        PicoRegionOverride = currentConfig.PicoRegion;\r
        PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;\r
+       PicoCDBuffers = currentConfig.PicoCDBuffers;\r
        if (PicoOpt & 0x20) {\r
                actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
                actionNames[10] = "X"; actionNames[11] = "MODE";\r
@@ -545,7 +550,7 @@ int emu_WriteConfig(int game)
                strncpy(cfg, PicoConfigFile, 511);\r
                cfg[511] = 0;\r
        } else {\r
-               romfname_ext(cfg, "cfg", ".pbcfg");\r
+               romfname_ext(cfg, "cfg/", ".pbcfg");\r
        }\r
 \r
        printf("emu_WriteConfig: %s ", cfg);\r
@@ -555,6 +560,7 @@ int emu_WriteConfig(int game)
                currentConfig.PsndRate = PsndRate;\r
                currentConfig.PicoRegion = PicoRegionOverride;\r
                currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;\r
+               currentConfig.PicoCDBuffers = PicoCDBuffers;\r
                bwrite = fwrite(&currentConfig, 1, sizeof(currentConfig), f);\r
                fflush(f);\r
                fclose(f);\r
@@ -768,6 +774,23 @@ static void vidResetMode(void)
 }\r
 \r
 \r
+static void emu_msg_cb(const char *msg)\r
+{\r
+       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+               // 8-bit renderers\r
+               gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
+               osd_text(4, 232, msg);\r
+               gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);\r
+       } else {\r
+               // 16bit accurate renderer\r
+               gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
+               osd_text(4, 232, msg);\r
+               gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);\r
+       }\r
+       gettimeofday(&noticeMsgTime, 0);\r
+       noticeMsgTime.tv_sec -= 2;\r
+}\r
+\r
 static void emu_state_cb(const char *str)\r
 {\r
        clearArea(0);\r
@@ -950,21 +973,11 @@ static void updateSound(int len)
 }\r
 \r
 \r
-static void SkipFrame(int do_sound)\r
+static void SkipFrame(void)\r
 {\r
-       void *sndbuff_tmp = 0;\r
-       if (PsndOut && !do_sound) {\r
-               sndbuff_tmp = PsndOut;\r
-               PsndOut = 0;\r
-       }\r
-\r
        PicoSkipFrame=1;\r
        PicoFrame();\r
        PicoSkipFrame=0;\r
-\r
-       if (sndbuff_tmp && !do_sound) {\r
-               PsndOut = sndbuff_tmp;\r
-       }\r
 }\r
 \r
 \r
@@ -1068,6 +1081,9 @@ void emu_Loop(void)
                PsndOut = 0;\r
        }\r
 \r
+       // prepare CD buffer\r
+       if (PicoMCD & 1) PicoCDBufferInit();\r
+\r
        // loop?\r
        while (engineState == PGS_Running)\r
        {\r
@@ -1140,7 +1156,7 @@ void emu_Loop(void)
                                // when second changes, but we don't want buffer to starve.\r
                                if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r
                                        updateKeys();\r
-                                       SkipFrame(1); frames_done++;\r
+                                       SkipFrame(); frames_done++;\r
                                }\r
 \r
                                frames_done  -= target_fps; if (frames_done  < 0) frames_done  = 0;\r
@@ -1153,7 +1169,7 @@ void emu_Loop(void)
                if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
                                updateKeys();\r
-                               SkipFrame(1); frames_done++;\r
+                               SkipFrame(); frames_done++;\r
                                if (PsndOut) { // do framelimitting if sound is enabled\r
                                        gettimeofday(&tval, 0);\r
                                        if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
@@ -1165,8 +1181,14 @@ void emu_Loop(void)
                        }\r
                } else if(tval.tv_usec > lim_time) { // auto frameskip\r
                        // no time left for this frame - skip\r
+                       if (tval.tv_usec - lim_time >= 0x300000) {\r
+                               /* something caused a slowdown for us (disk access? cache flush?)\r
+                                * try to recover by resetting timing... */\r
+                               reset_timing = 1;\r
+                               continue;\r
+                       }\r
                        updateKeys();\r
-                       SkipFrame(tval.tv_usec < lim_time+target_frametime); frames_done++;\r
+                       SkipFrame(/*tval.tv_usec < lim_time+target_frametime*/); frames_done++;\r
                        continue;\r
                }\r
 \r
@@ -1233,14 +1255,14 @@ if (Pico.m.frame_count == 31563) {
 \r
                // check time\r
                gettimeofday(&tval, 0);\r
-               if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+               if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
 \r
-               // sleep if we are still too fast\r
-               if(PsndOut != 0 || currentConfig.Frameskip < 0)\r
+               if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 0x300000) // slowdown detection\r
+                       reset_timing = 1;\r
+               else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
                {\r
+                       // sleep if we are still too fast\r
                        // usleep sleeps for ~20ms minimum, so it is not a solution here\r
-                       gettimeofday(&tval, 0);\r
-                       if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
                        if(tval.tv_usec < lim_time)\r
                        {\r
                                // we are too fast\r
@@ -1253,6 +1275,9 @@ if (Pico.m.frame_count == 31563) {
                frames_done++; frames_shown++;\r
        }\r
 \r
+\r
+       if (PicoMCD & 1) PicoCDBufferFree();\r
+\r
        // save SRAM\r
        if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
                osd_text(4, 232, "Writing SRAM/BRAM..");\r