A_SN's curve
[picodrive.git] / platform / gp2x / emu.c
index f3b4c02..0e1246c 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006 notaz, All rights reserved.\r
+// (c) Copyright 2006-2007 notaz, All rights reserved.\r
 // Free for non-commercial use.\r
 \r
 // For commercial use, separate licencing terms must be obtained.\r
@@ -310,7 +310,7 @@ int emu_ReloadRom(void)
 \r
        // detect wrong files (Pico crashes on very small files), also see if ROM EP is good\r
        if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\r
-         ((*(unsigned short *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r
+         ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r
                if (rom_data) free(rom_data);\r
                rom_data = 0;\r
                sprintf(menuErrorMsg, "Not a ROM selected.");\r
@@ -482,8 +482,8 @@ int emu_ReadConfig(int game)
                // set default config\r
                memset(&currentConfig, 0, sizeof(currentConfig));\r
                currentConfig.lastRomFile[0] = 0;\r
-               currentConfig.EmuOpt  = 0x1f | 0x400; // | cd_leds\r
-               currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940 | cd_pcm | cd_cdda\r
+               currentConfig.EmuOpt  = 0x1f | 0x600; // | confirm_save, cd_leds\r
+               currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda\r
                currentConfig.PsndRate = 22050; // 44100;\r
                currentConfig.PicoRegion = 0; // auto\r
                currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r
@@ -613,7 +613,7 @@ void emu_Deinit(void)
 \r
        // restore gamma\r
        if (gp2x_old_gamma != 100)\r
-               set_gamma(100);\r
+               set_gamma(100, 0);\r
 }\r
 \r
 \r
@@ -1063,11 +1063,11 @@ static void simpleWait(int thissec, int lim_time)
 void emu_Loop(void)\r
 {\r
        static int gp2x_old_clock = 200;\r
-       static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r
+       static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r
        char fpsbuff[24]; // fps count c string\r
        struct timeval tval; // timing\r
        int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;\r
-       int target_fps, target_frametime, lim_time, i;\r
+       int target_fps, target_frametime, lim_time, vsync_offset, i;\r
        char *notice = 0;\r
 \r
        printf("entered emu_Loop()\n");\r
@@ -1079,12 +1079,19 @@ void emu_Loop(void)
                printf(" done\n");\r
        }\r
 \r
-       if (gp2x_old_gamma != currentConfig.gamma) {\r
-               set_gamma(currentConfig.gamma);\r
+       if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
+               set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
                gp2x_old_gamma = currentConfig.gamma;\r
-               printf("updated gamma to %i\n", currentConfig.gamma);\r
+               printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
        }\r
 \r
+       if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
+               if (currentConfig.EmuOpt&0x2000)\r
+                    set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
+               else unset_LCD_custom_rate();\r
+       }\r
+\r
+       EmuOpt_old = currentConfig.EmuOpt;\r
        fpsbuff[0] = 0;\r
 \r
        // make sure we are in correct mode\r
@@ -1128,6 +1135,19 @@ void emu_Loop(void)
        // prepare CD buffer\r
        if (PicoMCD & 1) PicoCDBufferInit();\r
 \r
+       // calc vsync offset to sync timing code with vsync\r
+       if (currentConfig.EmuOpt&0x2000) {\r
+               gettimeofday(&tval, 0);\r
+               gp2x_video_wait_vsync();\r
+               gettimeofday(&tval, 0);\r
+               vsync_offset = tval.tv_usec;\r
+               while (vsync_offset >= target_frametime)\r
+                       vsync_offset -= target_frametime;\r
+               if (!vsync_offset) vsync_offset++;\r
+               printf("vsync_offset: %i\n", vsync_offset);\r
+       } else\r
+               vsync_offset = 0;\r
+\r
        // loop?\r
        while (engineState == PGS_Running)\r
        {\r
@@ -1210,10 +1230,8 @@ void emu_Loop(void)
                                if (frames_shown > frames_done) frames_shown = frames_done;\r
                        }\r
                }\r
-#if 0\r
-               sprintf(fpsbuff, "%05i", Pico.m.frame_count);\r
-#endif\r
-               lim_time = (frames_done+1) * target_frametime;\r
+\r
+               lim_time = (frames_done+1) * target_frametime + vsync_offset;\r
                if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
                                updateKeys();\r
@@ -1309,12 +1327,18 @@ if (Pico.m.frame_count == 31563) {
                        reset_timing = 1;\r
                else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
                {\r
-                       // sleep if we are still too fast\r
+                       // sleep or vsync if we are still too fast\r
                        // usleep sleeps for ~20ms minimum, so it is not a solution here\r
                        if(tval.tv_usec < lim_time)\r
                        {\r
                                // we are too fast\r
-                               simpleWait(thissec, lim_time);\r
+                               if (vsync_offset) {\r
+                                       if (lim_time - tval.tv_usec > target_frametime/2)\r
+                                               simpleWait(thissec, lim_time - target_frametime/4);\r
+                                       gp2x_video_wait_vsync();\r
+                               } else {\r
+                                       simpleWait(thissec, lim_time);\r
+                               }\r
                        }\r
                }\r
 \r
@@ -1328,7 +1352,7 @@ if (Pico.m.frame_count == 31563) {
 \r
        // save SRAM\r
        if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
-               osd_text(4, 232, "Writing SRAM/BRAM..");\r
+               emu_state_cb("Writing SRAM/BRAM..");\r
                emu_SaveLoadGame(0, 1);\r
                SRam.changed = 0;\r
        }\r
@@ -1459,29 +1483,42 @@ int emu_SaveLoadGame(int load, int sram)
                FILE *sramFile;\r
                int sram_size;\r
                unsigned char *sram_data;\r
+               int truncate = 1;\r
                if (PicoMCD&1) {\r
-                       sram_size = 0x2000;\r
-                       sram_data = Pico_mcd->bram;\r
+                       if (PicoOpt&0x8000) { // MCD RAM cart?\r
+                               sram_size = 0x12000;\r
+                               sram_data = SRam.data;\r
+                               if (sram_data)\r
+                                       memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r
+                       } else {\r
+                               sram_size = 0x2000;\r
+                               sram_data = Pico_mcd->bram;\r
+                               truncate  = 0; // the .brm may contain RAM cart data after normal brm\r
+                       }\r
                } else {\r
                        sram_size = SRam.end-SRam.start+1;\r
                        if(SRam.reg_back & 4) sram_size=0x2000;\r
                        sram_data = SRam.data;\r
                }\r
-               if(!sram_data) return 0; // SRam forcefully disabled for this game\r
+               if (!sram_data) return 0; // SRam forcefully disabled for this game\r
 \r
-               if(load) {\r
+               if (load) {\r
                        sramFile = fopen(saveFname, "rb");\r
                        if(!sramFile) return -1;\r
                        fread(sram_data, 1, sram_size, sramFile);\r
                        fclose(sramFile);\r
+                       if ((PicoMCD&1) && (PicoOpt&0x8000))\r
+                               memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r
                } else {\r
                        // sram save needs some special processing\r
                        // see if we have anything to save\r
-                       for(; sram_size > 0; sram_size--)\r
-                               if(sram_data[sram_size-1]) break;\r
+                       for (; sram_size > 0; sram_size--)\r
+                               if (sram_data[sram_size-1]) break;\r
 \r
-                       if(sram_size) {\r
-                               sramFile = fopen(saveFname, "wb");\r
+                       if (sram_size) {\r
+                               sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r
+                               if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r
+                               if (!sramFile) return -1;\r
                                ret = fwrite(sram_data, 1, sram_size, sramFile);\r
                                ret = (ret != sram_size) ? -1 : 0;\r
                                fclose(sramFile);\r