-// (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
#include <stdio.h>\r
#include <stdlib.h>\r
#include <sys/time.h>\r
+#include <sys/stat.h>\r
+#include <sys/types.h>\r
#include <linux/limits.h>\r
#include <ctype.h>\r
#include <unistd.h>\r
#include "gp2x.h"\r
#include "usbjoy.h"\r
#include "menu.h"\r
-#include "asmutils.h"\r
+#include "../common/arm_utils.h"\r
+#include "../common/fonts.h"\r
+#include "../common/emu.h"\r
#include "cpuctrl.h"\r
\r
-#include "Pico/PicoInt.h"\r
-#include "zlib/zlib.h"\r
+#include <Pico/PicoInt.h>\r
+#include <Pico/Patch.h>\r
+#include <Pico/sound/mix.h>\r
+#include <zlib/zlib.h>\r
\r
+//#define PFRAMES\r
\r
#ifdef BENCHMARK\r
#define OSD_FPS_X 220\r
#define OSD_FPS_X 260\r
#endif\r
\r
-// PicoPad[] format: SACB RLDU\r
-char *actionNames[] = {\r
- "UP", "DOWN", "LEFT", "RIGHT", "B", "C", "A", "START",\r
- 0, 0, 0, 0, 0, 0, 0, 0, // Z, Y, X, MODE (enabled only when needed), ?, ?, ?, ?\r
- 0, 0, 0, 0, 0, 0, 0, "ENTER MENU", // player2_flag, ?, ?, ?, ?, ?, ?, menu\r
- "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",\r
- "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"\r
-};\r
\r
int engineState;\r
int select_exits = 0;\r
-char *PicoConfigFile = "picoconfig.bin";\r
-currentConfig_t currentConfig;\r
\r
char romFileName[PATH_MAX];\r
-unsigned char *rom_data = NULL;\r
\r
extern int crashed_940;\r
\r
-static short sndBuffer[2*44100/50];\r
-static char noticeMsg[64]; // notice msg to draw\r
+static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r
-static int reset_timing, osd_fps_x;\r
+static int osd_fps_x;\r
static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos\r
static int gp2x_old_gamma = 100;\r
-static unsigned char *movie_data = NULL;\r
-static int movie_size = 0;\r
-unsigned char *framebuff = 0; // temporary buffer for alt renderer\r
-int state_slot = 0;\r
+char noticeMsg[64]; // notice msg to draw\r
+unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer\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
- while ( (*string++ = (char)tolower(*string)) );\r
-}\r
+static void emu_msg_cb(const char *msg);\r
+static void emu_msg_tray_open(void);\r
\r
-static int try_rfn_cut(void)\r
-{\r
- FILE *tmp;\r
- char *p;\r
-\r
- p = romFileName + strlen(romFileName) - 1;\r
- for (; p > romFileName; p--)\r
- if (*p == '.') break;\r
- *p = 0;\r
-\r
- if((tmp = fopen(romFileName, "rb"))) {\r
- fclose(tmp);\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-static void get_ext(char *file, char *ext)\r
-{\r
- char *p;\r
-\r
- p = file + strlen(file) - 4;\r
- if (p < file) p = file;\r
- strncpy(ext, p, 4);\r
- ext[4] = 0;\r
- strlwr(ext);\r
-}\r
-\r
-char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };\r
-char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };\r
-char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r
-\r
-extern char **g_argv;\r
-\r
-int find_bios(int region, char **bios_file)\r
-{\r
- static char bios_path[1024];\r
- int i, j, count;\r
- char **files;\r
- FILE *f = NULL;\r
-\r
- if (region == 4) { // US\r
- files = biosfiles_us;\r
- count = sizeof(biosfiles_us) / sizeof(char *);\r
- } else if (region == 8) { // EU\r
- files = biosfiles_eu;\r
- count = sizeof(biosfiles_eu) / sizeof(char *);\r
- } else if (region == 1 || region == 2) {\r
- files = biosfiles_jp;\r
- count = sizeof(biosfiles_jp) / sizeof(char *);\r
- } else {\r
- return 0;\r
- }\r
-\r
- for (i = 0; i < count; i++)\r
- {\r
- strncpy(bios_path, g_argv[0], 1023);\r
- bios_path[1024-32] = 0;\r
- for (j = strlen(bios_path); j > 0; j--)\r
- if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }\r
- strcat(bios_path, files[i]);\r
- strcat(bios_path, ".bin");\r
- f = fopen(bios_path, "rb");\r
- if (f) break;\r
-\r
- bios_path[strlen(bios_path) - 4] = 0;\r
- strcat(bios_path, ".zip");\r
- f = fopen(bios_path, "rb");\r
- if (f) break;\r
- }\r
-\r
- if (f) {\r
- printf("using bios: %s\n", bios_path);\r
- fclose(f);\r
- if (bios_file) *bios_file = bios_path;\r
- return 1;\r
- } else {\r
- sprintf(menuErrorMsg, "no %s BIOS files found, read docs",\r
- region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
- printf("%s\n", menuErrorMsg);\r
- return 0;\r
- }\r
-}\r
\r
-/* checks if romFileName points to valid MegaCD image\r
- * if so, checks for suitable BIOS */\r
-static int cd_check(char *ext, char **bios_file)\r
+void emu_noticeMsgUpdated(void)\r
{\r
- unsigned char buf[32];\r
- FILE *cd_f;\r
- int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe\r
-\r
- cd_f = fopen(romFileName, "rb");\r
- if (!cd_f) return 0; // let the upper level handle this\r
-\r
- if (fread(buf, 1, 32, cd_f) != 32) {\r
- fclose(cd_f);\r
- return 0;\r
- }\r
-\r
- if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)\r
- if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)\r
- if (type == 0) {\r
- fclose(cd_f);\r
- return 0;\r
- }\r
-\r
- /* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */\r
- fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
- fread(buf, 1, 1, cd_f);\r
- fclose(cd_f);\r
-\r
- if (buf[0] == 0x64) region = 8; // EU\r
- if (buf[0] == 0xa1) region = 1; // JAP\r
-\r
- printf("detected %s Sega/Mega CD image with %s region\n",\r
- type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
-\r
- if (PicoRegionOverride) {\r
- region = PicoRegionOverride;\r
- printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
- }\r
-\r
- if(find_bios(region, bios_file))\r
- return type; // CD and BIOS detected\r
-\r
- return -1; // CD detected but load failed\r
+ gettimeofday(¬iceMsgTime, 0);\r
}\r
\r
-int emu_ReloadRom(void)\r
+void emu_getMainDir(char *dst, int len)\r
{\r
- unsigned int rom_size = 0;\r
- char *used_rom_name = romFileName;\r
- char ext[5];\r
- FILE *rom;\r
- int ret, cd_state;\r
-\r
- printf("emu_ReloadRom(%s)\n", romFileName);\r
-\r
- get_ext(romFileName, ext);\r
-\r
- // detect wrong extensions\r
- if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r
- sprintf(menuErrorMsg, "Not a ROM selected.");\r
- return 0;\r
- }\r
-\r
- // check for movie file\r
- if(movie_data) {\r
- free(movie_data);\r
- movie_data = 0;\r
- }\r
- if(!strcmp(ext, ".gmv")) {\r
- // check for both gmv and rom\r
- int dummy;\r
- FILE *movie_file = fopen(romFileName, "rb");\r
- if(!movie_file) {\r
- sprintf(menuErrorMsg, "Failed to open movie.");\r
- return 0;\r
- }\r
- fseek(movie_file, 0, SEEK_END);\r
- movie_size = ftell(movie_file);\r
- fseek(movie_file, 0, SEEK_SET);\r
- if(movie_size < 64+3) {\r
- sprintf(menuErrorMsg, "Invalid GMV file.");\r
- fclose(movie_file);\r
- return 0;\r
- }\r
- movie_data = malloc(movie_size);\r
- if(movie_data == NULL) {\r
- sprintf(menuErrorMsg, "low memory.");\r
- fclose(movie_file);\r
- return 0;\r
- }\r
- fread(movie_data, 1, movie_size, movie_file);\r
- fclose(movie_file);\r
- if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r
- sprintf(menuErrorMsg, "Invalid GMV file.");\r
- return 0;\r
- }\r
- dummy = try_rfn_cut() || try_rfn_cut();\r
- if (!dummy) {\r
- sprintf(menuErrorMsg, "Could't find a ROM for movie.");\r
- return 0;\r
- }\r
- get_ext(romFileName, ext);\r
- }\r
-\r
- // check for MegaCD image\r
- cd_state = cd_check(ext, &used_rom_name);\r
- if (cd_state > 0) {\r
- PicoMCD |= 1;\r
- get_ext(used_rom_name, ext);\r
- } else if (cd_state == -1) {\r
- // bios_help() ?\r
- return 0;\r
- } else {\r
- if (PicoMCD & 1) PicoExitMCD();\r
- PicoMCD &= ~1;\r
- }\r
-\r
- rom = fopen(used_rom_name, "rb");\r
- if(!rom) {\r
- sprintf(menuErrorMsg, "Failed to open rom.");\r
- return 0;\r
- }\r
-\r
- if(rom_data) {\r
- free(rom_data);\r
- rom_data = 0;\r
- rom_size = 0;\r
- }\r
-\r
- // zipfile support\r
- if(!strcasecmp(ext, ".zip")) {\r
- fclose(rom);\r
- ret = CartLoadZip(used_rom_name, &rom_data, &rom_size);\r
- if(ret) {\r
- if (ret == 4) strcpy(menuErrorMsg, "No ROMs found in zip.");\r
- else sprintf(menuErrorMsg, "Unzip failed with code %i", ret);\r
- printf("%s\n", menuErrorMsg);\r
- return 0;\r
- }\r
- } else {\r
- if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r
- sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r
- printf("%s\n", menuErrorMsg);\r
- fclose(rom);\r
- return 0;\r
- }\r
- fclose(rom);\r
- }\r
-\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
- if (rom_data) free(rom_data);\r
- rom_data = 0;\r
- sprintf(menuErrorMsg, "Not a ROM selected.");\r
- return 0;\r
- }\r
+ extern char **g_argv;\r
+ int j;\r
\r
- // load config for this ROM (do this before insert to get correct region)\r
- ret = emu_ReadConfig(1);\r
- if (!ret)\r
- emu_ReadConfig(0);\r
-\r
- printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r
- if(PicoCartInsert(rom_data, rom_size)) {\r
- sprintf(menuErrorMsg, "Failed to load ROM.");\r
- return 0;\r
- }\r
-\r
- Pico.m.frame_count = 0;\r
-\r
- // insert CD if it was detected\r
- if (cd_state > 0) {\r
- ret = Insert_CD(romFileName, cd_state == 2);\r
- if (ret != 0) {\r
- sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");\r
- printf("%s\n", menuErrorMsg);\r
- return 0;\r
- }\r
- }\r
-\r
- // emu_ReadConfig() might have messed currentConfig.lastRomFile\r
- strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);\r
- currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;\r
-\r
- // additional movie stuff\r
- if(movie_data) {\r
- if(movie_data[0x14] == '6')\r
- PicoOpt |= 0x20; // 6 button pad\r
- else PicoOpt &= ~0x20;\r
- PicoOpt |= 0x40; // accurate timing\r
- if(movie_data[0xF] >= 'A') {\r
- if(movie_data[0x16] & 0x80) {\r
- PicoRegionOverride = 8;\r
- } else {\r
- PicoRegionOverride = 4;\r
- }\r
- PicoReset(0);\r
- // TODO: bits 6 & 5\r
- }\r
- movie_data[0x18+30] = 0;\r
- sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);\r
- }\r
- else\r
- {\r
- if(Pico.m.pal) {\r
- strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");\r
- } else {\r
- strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");\r
- }\r
- }\r
- gettimeofday(¬iceMsgTime, 0);\r
-\r
- // load SRAM for this ROM\r
- if(currentConfig.EmuOpt & 1)\r
- emu_SaveLoadGame(1, 1);\r
-\r
- return 1;\r
+ strncpy(dst, g_argv[0], len);\r
+ len -= 32; // reserve\r
+ if (len < 0) len = 0;\r
+ dst[len] = 0;\r
+ for (j = strlen(dst); j > 0; j--)\r
+ if (dst[j] == '/') { dst[j+1] = 0; break; }\r
}\r
\r
-\r
void emu_Init(void)\r
{\r
// make temp buffer for alt renderer\r
- framebuff = malloc((8+320)*(8+240+8));\r
- if (!framebuff)\r
+ PicoDraw2FB = malloc((8+320)*(8+240+8));\r
+ if (!PicoDraw2FB)\r
{\r
- printf("framebuff == 0\n");\r
+ printf("PicoDraw2FB == 0\n");\r
}\r
\r
- PicoInit();\r
-\r
-// logf = fopen("log.txt", "w");\r
-}\r
-\r
+ // make dirs for saves, cfgs, etc.\r
+ mkdir("mds", 0777);\r
+ mkdir("srm", 0777);\r
+ mkdir("brm", 0777);\r
+ mkdir("cfg", 0777);\r
\r
-static void romfname_ext(char *dst, char *ext)\r
-{\r
- char *p;\r
-\r
- // make save filename\r
- for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;\r
- strncpy(dst, p, 511);\r
- dst[511-8] = 0;\r
- if(dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r
- strcat(dst, ext);\r
+ PicoInit();\r
+ PicoMessage = emu_msg_cb;\r
+ PicoMCDopenTray = emu_msg_tray_open;\r
+ PicoMCDcloseTray = menu_loop_tray;\r
}\r
\r
\r
combo_keys = combo_acts = 0;\r
for (act = 0; act < 32; act++)\r
{\r
- int keyc = 0;\r
- if (act == 16) continue; // player2 flag\r
- for (u = 0; u < 32; u++)\r
+ int keyc = 0, keyc2 = 0;\r
+ if (act == 16 || act == 17) continue; // player2 flag\r
+ if (act > 17)\r
+ {\r
+ for (u = 0; u < 32; u++)\r
+ if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r
+ }\r
+ else\r
{\r
- if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r
+ for (u = 0; u < 32; u++)\r
+ if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1\r
+ (currentConfig.KeyBinds[u] & (1 << act))) keyc++;\r
+ for (u = 0; u < 32; u++)\r
+ if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2\r
+ (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;\r
+ if (keyc2 > keyc) keyc = keyc2;\r
}\r
if (keyc > 1)\r
{\r
}\r
}\r
}\r
- // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);\r
-}\r
-\r
-\r
-int emu_ReadConfig(int game)\r
-{\r
- FILE *f;\r
- char cfg[512];\r
- int bread = 0;\r
\r
- if (!game)\r
- {\r
- // set default config\r
- memset(¤tConfig, 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.PsndRate = 22050; // 44100;\r
- currentConfig.PicoRegion = 0; // auto\r
- currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r
- currentConfig.Frameskip = -1; // auto\r
- currentConfig.CPUclock = 200;\r
- currentConfig.volume = 50;\r
- currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
- currentConfig.KeyBinds[ 4] = 1<<1;\r
- currentConfig.KeyBinds[ 2] = 1<<2;\r
- currentConfig.KeyBinds[ 6] = 1<<3;\r
- currentConfig.KeyBinds[14] = 1<<4;\r
- currentConfig.KeyBinds[13] = 1<<5;\r
- currentConfig.KeyBinds[12] = 1<<6;\r
- currentConfig.KeyBinds[ 8] = 1<<7;\r
- currentConfig.KeyBinds[15] = 1<<26; // switch rend\r
- currentConfig.KeyBinds[10] = 1<<27; // save state\r
- currentConfig.KeyBinds[11] = 1<<28; // load state\r
- currentConfig.KeyBinds[23] = 1<<29; // vol up\r
- currentConfig.KeyBinds[22] = 1<<30; // vol down\r
- currentConfig.gamma = 100;\r
- strncpy(cfg, PicoConfigFile, 511);\r
- cfg[511] = 0;\r
- } else {\r
- romfname_ext(cfg, ".pbcfg");\r
- }\r
-\r
- printf("emu_ReadConfig: %s ", cfg);\r
- f = fopen(cfg, "rb");\r
- if (f) {\r
- bread = fread(¤tConfig, 1, sizeof(currentConfig), f);\r
- fclose(f);\r
- }\r
- printf((bread == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
-\r
- PicoOpt = currentConfig.PicoOpt;\r
- PsndRate = currentConfig.PsndRate;\r
- PicoRegionOverride = currentConfig.PicoRegion;\r
- PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;\r
- if (PicoOpt & 0x20) {\r
- actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
- actionNames[10] = "X"; actionNames[11] = "MODE";\r
- }\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
- // if volume keys are unbound, bind them to volume control\r
- if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {\r
- currentConfig.KeyBinds[23] = 1<<29; // vol up\r
- currentConfig.KeyBinds[22] = 1<<30; // vol down\r
- }\r
-\r
- return (bread == sizeof(currentConfig));\r
+ // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);\r
}\r
\r
\r
-int emu_WriteConfig(int game)\r
+static void scaling_update(void)\r
{\r
- FILE *f;\r
- char cfg[512];\r
- int bwrite = 0;\r
-\r
- if (!game)\r
- {\r
- strncpy(cfg, PicoConfigFile, 511);\r
- cfg[511] = 0;\r
- } else {\r
- romfname_ext(cfg, ".pbcfg");\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
- printf("emu_WriteConfig: %s ", cfg);\r
- f = fopen(cfg, "wb");\r
- if (f) {\r
- currentConfig.PicoOpt = PicoOpt;\r
- currentConfig.PsndRate = PsndRate;\r
- currentConfig.PicoRegion = PicoRegionOverride;\r
- currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;\r
- bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);\r
- fflush(f);\r
- fclose(f);\r
- sync();\r
- }\r
- printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
-\r
- return (bwrite == sizeof(currentConfig));\r
}\r
\r
\r
SRam.changed = 0;\r
}\r
\r
- if (!(currentConfig.EmuOpt & 0x20))\r
- emu_WriteConfig(0);\r
- free(framebuff);\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(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);\r
+ fseek(f, 0, SEEK_SET);\r
+ fwrite(¤tConfig, 1, sizeof(currentConfig), f);\r
+ fflush(f);\r
+ fclose(f);\r
+#ifndef NO_SYNC\r
+ sync();\r
+#endif\r
+ }\r
+ }\r
+\r
+ free(PicoDraw2FB);\r
\r
PicoExit();\r
-// fclose(logf);\r
\r
// restore gamma\r
if (gp2x_old_gamma != 100)\r
- set_gamma(100);\r
-}\r
-\r
-\r
-void osd_text(int x, int y, char *text)\r
+ set_gamma(100, 0);\r
+}\r
+\r
+void emu_setDefaultConfig(void)\r
+{\r
+ memset(¤tConfig, 0, sizeof(currentConfig));\r
+ currentConfig.lastRomFile[0] = 0;\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
+ currentConfig.Frameskip = -1; // auto\r
+ currentConfig.CPUclock = 200;\r
+ currentConfig.volume = 50;\r
+ currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
+ currentConfig.KeyBinds[ 4] = 1<<1;\r
+ currentConfig.KeyBinds[ 2] = 1<<2;\r
+ currentConfig.KeyBinds[ 6] = 1<<3;\r
+ currentConfig.KeyBinds[14] = 1<<4;\r
+ currentConfig.KeyBinds[13] = 1<<5;\r
+ currentConfig.KeyBinds[12] = 1<<6;\r
+ currentConfig.KeyBinds[ 8] = 1<<7;\r
+ currentConfig.KeyBinds[15] = 1<<26; // switch rend\r
+ currentConfig.KeyBinds[10] = 1<<27; // save state\r
+ currentConfig.KeyBinds[11] = 1<<28; // load state\r
+ currentConfig.KeyBinds[23] = 1<<29; // vol up\r
+ currentConfig.KeyBinds[22] = 1<<30; // vol down\r
+ currentConfig.gamma = 100;\r
+ currentConfig.PicoCDBuffers = 64;\r
+ currentConfig.scaling = 0;\r
+}\r
+\r
+void osd_text(int x, int y, const char *text)\r
{\r
int len = strlen(text)*8;\r
\r
p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r
for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
}\r
- gp2x_text_out8_2(x, y, text, 0xf0);\r
+ emu_textOut8(x, y, text);\r
} else {\r
int *p, i, h;\r
x &= ~1; // align x\r
p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));\r
for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r
}\r
- gp2x_text_out15(x, y, text);\r
+ emu_textOut16(x, y, text);\r
}\r
}\r
\r
static void cd_leds(void)\r
{\r
- // mmu problems?\r
// static\r
int old_reg;\r
-// if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change\r
+// if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r
old_reg = Pico_mcd->s68k_regs[0];\r
\r
if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r
*p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
*p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
- *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
+ *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r
}\r
}\r
\r
int localPal[0x100];\r
static void (*vidCpyM2)(void *dest, void *src) = NULL;\r
\r
-static void blit(char *fps, char *notice)\r
+static void blit(const char *fps, const char *notice)\r
{\r
int emu_opt = currentConfig.EmuOpt;\r
\r
// feed new palette to our device\r
gp2x_video_setpalette(localPal, 0x40);\r
}\r
- vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);\r
+ vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
} else if (!(emu_opt&0x80)) {\r
// 8bit accurate renderer\r
if (Pico.m.dirtyPal) {\r
}\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
if (PicoOpt&0x10) {\r
gp2x_video_changemode(8);\r
} else if (currentConfig.EmuOpt&0x80) {\r
- gp2x_video_changemode(15);\r
+ gp2x_video_changemode(16);\r
PicoDrawSetColorFormat(1);\r
PicoScan = EmuScan16;\r
PicoScan(0, 0);\r
}\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
-static int check_save_file(void)\r
+static void emu_msg_cb(const char *msg)\r
{\r
- char saveFname[512];\r
- char ext[16];\r
- FILE *f;\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(¬iceMsgTime, 0);\r
+ noticeMsgTime.tv_sec -= 2;\r
+\r
+ /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r
+ reset_timing = 1;\r
+}\r
+\r
+static void emu_state_cb(const char *str)\r
+{\r
+ clearArea(0);\r
+ blit("", str);\r
+}\r
+\r
+static void emu_msg_tray_open(void)\r
+{\r
+ strcpy(noticeMsg, "CD tray opened");\r
+ gettimeofday(¬iceMsgTime, 0);\r
+}\r
+\r
+static void update_volume(int has_changed, int is_up)\r
+{\r
+ static int prev_frame = 0, wait_frames = 0;\r
+ int vol = currentConfig.volume;\r
+\r
+ if (has_changed)\r
+ {\r
+ if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
+ wait_frames++;\r
+ else {\r
+ if (is_up) {\r
+ if (vol < 99) vol++;\r
+ } else {\r
+ if (vol > 0) vol--;\r
+ }\r
+ wait_frames = 0;\r
+ gp2x_sound_volume(vol, vol);\r
+ currentConfig.volume = vol;\r
+ }\r
+ sprintf(noticeMsg, "VOL: %02i", vol);\r
+ gettimeofday(¬iceMsgTime, 0);\r
+ prev_frame = Pico.m.frame_count;\r
+ }\r
+\r
+ // set the right mixer func\r
+ if (!(PicoOpt&8)) return; // just use defaults for mono\r
+ if (vol >= 5)\r
+ PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
+ else {\r
+ mix_32_to_16l_level = 5 - vol;\r
+ PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
+ }\r
+}\r
\r
- ext[0] = 0;\r
- if(state_slot > 0 && state_slot < 10) sprintf(ext, ".%i", state_slot);\r
- strcat(ext, ".mds");\r
- if(currentConfig.EmuOpt & 8) strcat(ext, ".gz");\r
+static void change_fast_forward(int set_on)\r
+{\r
+ static void *set_PsndOut = NULL;\r
+ static int set_Frameskip, set_EmuOpt, is_on = 0;\r
\r
- romfname_ext(saveFname, ext);\r
- if ((f = fopen(saveFname, "rb"))) {\r
- fclose(f);\r
- return 1;\r
+ if (set_on && !is_on) {\r
+ set_PsndOut = PsndOut;\r
+ set_Frameskip = currentConfig.Frameskip;\r
+ set_EmuOpt = currentConfig.EmuOpt;\r
+ PsndOut = NULL;\r
+ currentConfig.Frameskip = 8;\r
+ currentConfig.EmuOpt &= ~4;\r
+ is_on = 1;\r
+ }\r
+ else if (!set_on && is_on) {\r
+ PsndOut = set_PsndOut;\r
+ currentConfig.Frameskip = set_Frameskip;\r
+ currentConfig.EmuOpt = set_EmuOpt;\r
+ PsndRerate(1);\r
+ update_volume(0, 0);\r
+ reset_timing = 1;\r
+ is_on = 0;\r
}\r
- return 0;\r
}\r
\r
static void RunEvents(unsigned int which)\r
{\r
- if(which & 0x1800) { // save or load (but not both)\r
+ if (which & 0x1800) // save or load (but not both)\r
+ {\r
int do_it = 1;\r
- if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && check_save_file()) {\r
+ if ( emu_checkSaveFile(state_slot) &&\r
+ (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load\r
+ (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r
unsigned long keys;\r
- blit("", "OVERWRITE SAVE? (Y=yes, X=no)");\r
+ blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r
while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
usleep(50*1024);\r
if (keys & GP2X_X) do_it = 0;\r
clearArea(0);\r
}\r
if (do_it) {\r
- blit("", (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
- emu_SaveLoadGame(which & 0x1000, 0);\r
+ osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
+ PicoStateProgressCB = emu_state_cb;\r
+ gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r
+ emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
+ PicoStateProgressCB = NULL;\r
}\r
\r
reset_timing = 1;\r
}\r
- if(which & 0x0400) { // switch renderer\r
+ if (which & 0x0400) // switch renderer\r
+ {\r
if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
else currentConfig.EmuOpt &= ~0x80;\r
\r
gettimeofday(¬iceMsgTime, 0);\r
}\r
- if(which & 0x0300) {\r
+ if (which & 0x0300)\r
+ {\r
if(which&0x0200) {\r
state_slot -= 1;\r
if(state_slot < 0) state_slot = 9;\r
state_slot += 1;\r
if(state_slot > 9) state_slot = 0;\r
}\r
- sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, check_save_file() ? "USED" : "FREE");\r
+ sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
gettimeofday(¬iceMsgTime, 0);\r
}\r
- if(which & 0x0080) {\r
+ if (which & 0x0080) {\r
engineState = PGS_Menu;\r
}\r
}\r
\r
\r
-static void updateMovie(void)\r
-{\r
- int offs = Pico.m.frame_count*3 + 0x40;\r
- if (offs+3 > movie_size) {\r
- free(movie_data);\r
- movie_data = 0;\r
- strcpy(noticeMsg, "END OF MOVIE.");\r
- printf("END OF MOVIE.\n");\r
- gettimeofday(¬iceMsgTime, 0);\r
- } else {\r
- // MXYZ SACB RLDU\r
- PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r
- if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A\r
- if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B\r
- if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A\r
- PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r
- if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A\r
- if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B\r
- if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A\r
- PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r
- if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r
- if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r
- PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r
- if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r
- if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r
- }\r
-}\r
-\r
-\r
static void updateKeys(void)\r
{\r
unsigned long keys, allActions[2] = { 0, 0 }, events;\r
events = (allActions[0] | allActions[1]) >> 16;\r
\r
// volume is treated in special way and triggered every frame\r
- if(events & 0x6000) {\r
- int vol = currentConfig.volume;\r
- if (events & 0x2000) {\r
- if (vol < 90) vol++;\r
- } else {\r
- if (vol > 0) vol--;\r
- }\r
- gp2x_sound_volume(vol, vol);\r
- sprintf(noticeMsg, "VOL: %02i", vol);\r
- gettimeofday(¬iceMsgTime, 0);\r
- currentConfig.volume = vol;\r
- }\r
+ if (events & 0x6000)\r
+ update_volume(1, events & 0x2000);\r
+\r
+ if ((events ^ prevEvents) & 0x40)\r
+ change_fast_forward(events & 0x40);\r
\r
events &= ~prevEvents;\r
if (events) RunEvents(events);\r
- if (movie_data) updateMovie();\r
+ if (movie_data) emu_updateMovie();\r
\r
prevEvents = (allActions[0] | allActions[1]) >> 16;\r
}\r
{\r
if (PicoOpt&8) len<<=1;\r
\r
- gp2x_sound_write(PsndOut, len<<1);\r
+ /* avoid writing audio when lagging behind to prevent audio lag */\r
+ if (PicoSkipFrame != 2)\r
+ gp2x_sound_write(PsndOut, len<<1);\r
}\r
\r
\r
-static void SkipFrame(int do_sound)\r
+static void SkipFrame(int do_audio)\r
{\r
- void *sndbuff_tmp = 0;\r
- if (PsndOut && !do_sound) {\r
- sndbuff_tmp = PsndOut;\r
- PsndOut = 0;\r
- }\r
-\r
- PicoSkipFrame=1;\r
+ PicoSkipFrame=do_audio ? 1 : 2;\r
PicoFrame();\r
PicoSkipFrame=0;\r
-\r
- if (sndbuff_tmp && !do_sound) {\r
- PsndOut = sndbuff_tmp;\r
- }\r
}\r
\r
\r
+void emu_forcedFrame(void)\r
+{\r
+ int po_old = PicoOpt;\r
+ int eo_old = currentConfig.EmuOpt;\r
+\r
+ PicoOpt &= ~0x0010;\r
+ PicoOpt |= 0x4080; // soft_scale | acc_sprites\r
+ currentConfig.EmuOpt |= 0x80;\r
+\r
+ //vidResetMode();\r
+ PicoDrawSetColorFormat(1);\r
+ PicoScan = EmuScan16;\r
+ PicoScan(0, 0);\r
+ Pico.m.dirtyPal = 1;\r
+ PicoFrameDrawOnly();\r
+\r
+/*\r
+ if (!(Pico.video.reg[12]&1)) {\r
+ vidCpyM2 = vidCpyM2_32col;\r
+ clearArea(1);\r
+ } else vidCpyM2 = vidCpyM2_40col;\r
+\r
+ vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
+ vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+ gp2x_video_setpalette(localPal, 0x40);\r
+*/\r
+ PicoOpt = po_old;\r
+ currentConfig.EmuOpt = eo_old;\r
+}\r
+\r
static void simpleWait(int thissec, int lim_time)\r
{\r
struct timeval tval;\r
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, 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
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
vidResetMode();\r
+ scaling_update();\r
Pico.m.dirtyPal = 1;\r
oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
find_combos();\r
reset_timing = 1;\r
\r
// prepare sound stuff\r
- if(currentConfig.EmuOpt & 4) {\r
+ if (currentConfig.EmuOpt & 4)\r
+ {\r
int snd_excess_add;\r
- if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {\r
- /* if 940 is turned off, we need it to be put back to sleep */\r
- if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {\r
- Reset940(1, 2);\r
- Pause940(1);\r
- }\r
- sound_rerate(1);\r
+ if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
+ ((PicoOpt&0x200) && crashed_940)) {\r
+ PsndRerate(Pico.m.frame_count ? 1 : 0);\r
}\r
- //excess_samples = PsndRate - PsndLen*target_fps;\r
snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
- printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
+ printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
+ PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
PicoWriteSound = updateSound;\r
+ update_volume(0, 0);\r
memset(sndBuffer, 0, sizeof(sndBuffer));\r
PsndOut = sndBuffer;\r
PsndRate_old = PsndRate;\r
- PsndLen_real = PsndLen;\r
PicoOpt_old = PicoOpt;\r
pal_old = Pico.m.pal;\r
} else {\r
- PsndOut = 0;\r
+ PsndOut = NULL;\r
}\r
\r
+ // 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
int modes;\r
\r
gettimeofday(&tval, 0);\r
- if(reset_timing) {\r
+ if (reset_timing) {\r
reset_timing = 0;\r
thissec = tval.tv_sec;\r
frames_shown = frames_done = tval.tv_usec/target_frametime;\r
}\r
\r
// show notice message?\r
- if(noticeMsgTime.tv_sec) {\r
+ if (noticeMsgTime.tv_sec)\r
+ {\r
static int noticeMsgSum;\r
if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
\r
// check for mode changes\r
modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
- if (modes != oldmodes) {\r
+ if (modes != oldmodes)\r
+ {\r
int scalex = 320;\r
osd_fps_x = OSD_FPS_X;\r
if (modes & 4) {\r
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
\r
// second changed?\r
- if(thissec != tval.tv_sec) {\r
+ if (thissec != tval.tv_sec)\r
+ {\r
#ifdef BENCHMARK\r
static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
- if(++bench == 10) {\r
+ if (++bench == 10) {\r
bench = 0;\r
bench_fps_s = bench_fps;\r
bf[bfp++ & 3] = bench_fps;\r
bench_fps += frames_shown;\r
sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
#else\r
- if(currentConfig.EmuOpt & 2)\r
+ if (currentConfig.EmuOpt & 2)\r
sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
+ if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
#endif\r
thissec = tval.tv_sec;\r
\r
- if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
+ if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
frames_done = frames_shown = 0;\r
} else {\r
// it is quite common for this implementation to leave 1 fame unfinished\r
if (frames_shown > frames_done) frames_shown = frames_done;\r
}\r
}\r
+#ifdef PFRAMES\r
+ sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
+#endif\r
\r
- lim_time = (frames_done+1) * target_frametime;\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
SkipFrame(1); frames_done++;\r
- if (PsndOut) { // do framelimitting if sound is enabled\r
+ if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
gettimeofday(&tval, 0);\r
if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
if(tval.tv_usec < lim_time) { // we are too fast\r
}\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 >= 300000) {\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*2); frames_done++;\r
continue;\r
}\r
\r
\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 >= 300000) // slowdown detection\r
+ reset_timing = 1;\r
+ else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
{\r
+ // sleep or vsync 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
+ if (!reset_timing && 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
frames_done++; frames_shown++;\r
}\r
\r
+ change_fast_forward(0);\r
+\r
+ if (PicoMCD & 1) PicoCDBufferFree();\r
+\r
// save SRAM\r
if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
- blit("", "Writing SRAM/BRAM..");\r
+ emu_state_cb("Writing SRAM/BRAM..");\r
emu_SaveLoadGame(0, 1);\r
SRam.changed = 0;\r
}\r
\r
- // if in 16bit mode, generate 8it image for menu background\r
- if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80)) {\r
- PicoOpt |= 0x10;\r
- Pico.m.dirtyPal = 1;\r
- PicoFrameFull();\r
- blit("", NULL); blit("", NULL); blit("", NULL); blit("", NULL); // be sure buffer3 gets updated\r
- PicoOpt &= ~0x10;\r
- }\r
+ // if in 8bit mode, generate 16bit image for menu background\r
+ if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
+ emu_forcedFrame();\r
}\r
\r
\r
}\r
\r
\r
-size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r
-{\r
- return gzread(file, p, _n);\r
-}\r
-\r
-\r
-size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r
-{\r
- return gzwrite(file, p, _n);\r
-}\r
-\r
-\r
-int emu_SaveLoadGame(int load, int sram)\r
-{\r
- int ret = 0;\r
- char saveFname[512];\r
-\r
- // make save filename\r
- romfname_ext(saveFname, "");\r
- if(sram) strcat(saveFname, (PicoMCD&1) ? ".brm" : ".srm");\r
- else {\r
- if(state_slot > 0 && state_slot < 10) sprintf(saveFname, "%s.%i", saveFname, state_slot);\r
- strcat(saveFname, ".mds");\r
- }\r
-\r
- printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r
-\r
- if(sram) {\r
- FILE *sramFile;\r
- int sram_size;\r
- unsigned char *sram_data;\r
- if (PicoMCD&1) {\r
- sram_size = 0x2000;\r
- sram_data = Pico_mcd->bram;\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
-\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
- } 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
-\r
- if(sram_size) {\r
- sramFile = fopen(saveFname, "wb");\r
- ret = fwrite(sram_data, 1, sram_size, sramFile);\r
- ret = (ret != sram_size) ? -1 : 0;\r
- fclose(sramFile);\r
- sync();\r
- }\r
- }\r
- return ret;\r
- }\r
- else\r
- {\r
- void *PmovFile = NULL;\r
- // try gzip first\r
- if(currentConfig.EmuOpt & 8) {\r
- strcat(saveFname, ".gz");\r
- if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
- areaRead = gzRead2;\r
- areaWrite = gzWrite2;\r
- areaEof = (areaeof *) gzeof;\r
- areaSeek = (areaseek *) gzseek;\r
- if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
- } else\r
- saveFname[strlen(saveFname)-3] = 0;\r
- }\r
- if(!PmovFile) { // gzip failed or was disabled\r
- if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
- areaRead = (arearw *) fread;\r
- areaWrite = (arearw *) fwrite;\r
- areaEof = (areaeof *) feof;\r
- areaSeek = (areaseek *) fseek;\r
- }\r
- }\r
- if(PmovFile) {\r
- ret = PmovState(load ? 6 : 5, PmovFile);\r
- if(areaRead == gzRead2)\r
- gzclose(PmovFile);\r
- else fclose ((FILE *) PmovFile);\r
- PmovFile = 0;\r
- if (!load) sync();\r
- else Pico.m.dirtyPal=1;\r
- }\r
- else ret = -1;\r
- if (!ret)\r
- strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");\r
- else\r
- {\r
- strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");\r
- ret = -1;\r
- }\r
-\r
- gettimeofday(¬iceMsgTime, 0);\r
- return ret;\r
- }\r
-}\r