-// (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 "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 "cpuctrl.h"\r
\r
#include <Pico/PicoInt.h>\r
#include <Pico/Patch.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
static short sndBuffer[2*44100/50];\r
static char noticeMsg[64]; // notice msg to draw\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
+unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer\r
int state_slot = 0;\r
+int reset_timing = 0;\r
+int config_slot = 0, config_slot_current = 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
\r
/* checks if romFileName points to valid MegaCD image\r
* if so, checks for suitable BIOS */\r
-static int cd_check(char **bios_file)\r
+int emu_cd_check(int *pregion)\r
{\r
unsigned char buf[32];\r
pm_file *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
+ /* it seems we have a CD image here. Try to detect region now.. */\r
pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
pm_read(buf, 1, cd_f);\r
pm_close(cd_f);\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
+ if (pregion != NULL) *pregion = region;\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
+ return type;\r
}\r
\r
int emu_ReloadRom(void)\r
char *used_rom_name = romFileName;\r
char ext[5];\r
pm_file *rom;\r
- int ret, cd_state;\r
+ int ret, cd_state, cd_region, cfg_loaded = 0;\r
\r
printf("emu_ReloadRom(%s)\n", romFileName);\r
\r
get_ext(romFileName, ext);\r
}\r
\r
+ if ((PicoMCD & 1) && Pico_mcd != NULL)\r
+ Stop_CD();\r
+\r
// check for MegaCD image\r
- cd_state = cd_check(&used_rom_name);\r
- if (cd_state > 0) {\r
+ cd_state = emu_cd_check(&cd_region);\r
+ if (cd_state > 0)\r
+ {\r
+ // valid CD image, check for BIOS..\r
+\r
+ // we need to have config loaded at this point\r
+ ret = emu_ReadConfig(1, 1);\r
+ if (!ret) emu_ReadConfig(0, 1);\r
+ cfg_loaded = 1;\r
+\r
+ if (PicoRegionOverride) {\r
+ cd_region = PicoRegionOverride;\r
+ printf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");\r
+ }\r
+ if (!find_bios(cd_region, &used_rom_name)) {\r
+ // bios_help() ?\r
+ return 0;\r
+ }\r
+\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
+ }\r
+ else\r
+ {\r
+ if (PicoMCD & 1) Stop_CD();\r
PicoMCD &= ~1;\r
}\r
\r
return 0;\r
}\r
\r
+ menu_romload_prepare(used_rom_name);\r
+\r
if(rom_data) {\r
free(rom_data);\r
rom_data = 0;\r
sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r
printf("%s\n", menuErrorMsg);\r
pm_close(rom);\r
+ menu_romload_end();\r
return 0;\r
}\r
pm_close(rom);\r
+ menu_romload_end();\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
+ ((*(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
}\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
+ if (!cfg_loaded) {\r
+ ret = emu_ReadConfig(1, 1);\r
+ if (!ret) emu_ReadConfig(0, 1);\r
+ }\r
\r
printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r
if(PicoCartInsert(rom_data, rom_size)) {\r
}\r
\r
// additional movie stuff\r
- if(movie_data) {\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
+ PicoOpt |= 0x10040; // accurate timing, no VDP fifo timing\r
if(movie_data[0xF] >= 'A') {\r
if(movie_data[0x16] & 0x80) {\r
PicoRegionOverride = 8;\r
}\r
else\r
{\r
+ PicoOpt &= ~0x10000;\r
if(Pico.m.pal) {\r
strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");\r
} else {\r
\r
\r
static void emu_msg_cb(const char *msg);\r
+static void emu_msg_tray_open(void);\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
// make dirs for saves, cfgs, etc.\r
\r
PicoInit();\r
PicoMessage = emu_msg_cb;\r
-\r
-// logf = fopen("log.txt", "w");\r
+ PicoMCDopenTray = emu_msg_tray_open;\r
+ PicoMCDcloseTray = menu_loop_tray;\r
}\r
\r
\r
}\r
\r
\r
-int emu_ReadConfig(int game)\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, int no_defaults)\r
{\r
FILE *f;\r
- char cfg[512];\r
+ char cfg[512], extbuf[16];\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
- currentConfig.PicoCDBuffers = 64;\r
+ if (!no_defaults)\r
+ {\r
+ // set default config\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
strncpy(cfg, PicoConfigFile, 511);\r
+ if (config_slot != 0)\r
+ {\r
+ char *p = strrchr(cfg, '.');\r
+ if (p == NULL) p = cfg + strlen(cfg);\r
+ sprintf(extbuf, ".%i.pbcfg", config_slot);\r
+ strncpy(p, extbuf, 511 - (p - cfg));\r
+ }\r
cfg[511] = 0;\r
} else {\r
- romfname_ext(cfg, "cfg/", ".pbcfg");\r
+ if (config_slot != 0)\r
+ sprintf(extbuf, ".%i.pbcfg", config_slot);\r
+ else strcpy(extbuf, ".pbcfg");\r
+ romfname_ext(cfg, "cfg/", extbuf);\r
f = fopen(cfg, "rb");\r
if (!f) romfname_ext(cfg, NULL, ".pbcfg");\r
else fclose(f);\r
bread = fread(¤tConfig, 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
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
- }\r
+ scaling_update();\r
// some sanity checks\r
- if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
+ if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r
+ if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;\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
+ if (bread > 0) config_slot_current = config_slot;\r
+ return (bread > 0); // == sizeof(currentConfig));\r
}\r
\r
\r
int emu_WriteConfig(int game)\r
{\r
FILE *f;\r
- char cfg[512];\r
+ char cfg[512], extbuf[16];\r
int bwrite = 0;\r
\r
if (!game)\r
{\r
strncpy(cfg, PicoConfigFile, 511);\r
+ if (config_slot != 0)\r
+ {\r
+ char *p = strrchr(cfg, '.');\r
+ if (p == NULL) p = cfg + strlen(cfg);\r
+ sprintf(extbuf, ".%i.pbcfg", config_slot);\r
+ strncpy(p, extbuf, 511 - (p - cfg));\r
+ }\r
cfg[511] = 0;\r
} else {\r
- romfname_ext(cfg, "cfg/", ".pbcfg");\r
+ if (config_slot != 0)\r
+ sprintf(extbuf, ".%i.pbcfg", config_slot);\r
+ else strcpy(extbuf, ".pbcfg");\r
+ romfname_ext(cfg, "cfg/", extbuf);\r
}\r
\r
printf("emu_WriteConfig: %s ", cfg);\r
bwrite = fwrite(¤tConfig, 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
+ if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;\r
return (bwrite == sizeof(currentConfig));\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
+ set_gamma(100, 0);\r
+}\r
+\r
+static void text_out8_builtin(int x, int y, const char *text)\r
+{\r
+ int i,l,len=strlen(text);\r
+ unsigned char *screen = (unsigned char *)gp2x_screen + x + y*320;\r
+\r
+ /* always using built-in font */\r
+ for (i = 0; i < len; i++)\r
+ {\r
+ for (l=0;l<8;l++)\r
+ {\r
+ unsigned char fd = fontdata8x8[((text[i])*8)+l];\r
+ if (fd&0x80) screen[l*320+0]=0xf0;\r
+ if (fd&0x40) screen[l*320+1]=0xf0;\r
+ if (fd&0x20) screen[l*320+2]=0xf0;\r
+ if (fd&0x10) screen[l*320+3]=0xf0;\r
+ if (fd&0x08) screen[l*320+4]=0xf0;\r
+ if (fd&0x04) screen[l*320+5]=0xf0;\r
+ if (fd&0x02) screen[l*320+6]=0xf0;\r
+ if (fd&0x01) screen[l*320+7]=0xf0;\r
+ }\r
+ screen += 8;\r
+ }\r
+}\r
+\r
+static void text_out16_builtin(int x, int y, const char *text)\r
+{\r
+ int i,l,len=strlen(text);\r
+ unsigned short *screen = (unsigned short *)gp2x_screen + x + y*320;\r
+\r
+ for (i = 0; i < len; i++)\r
+ {\r
+ for (l=0;l<8;l++)\r
+ {\r
+ unsigned char fd = fontdata8x8[((text[i])*8)+l];\r
+ if(fd&0x80) screen[l*320+0]=0xffff;\r
+ if(fd&0x40) screen[l*320+1]=0xffff;\r
+ if(fd&0x20) screen[l*320+2]=0xffff;\r
+ if(fd&0x10) screen[l*320+3]=0xffff;\r
+ if(fd&0x08) screen[l*320+4]=0xffff;\r
+ if(fd&0x04) screen[l*320+5]=0xffff;\r
+ if(fd&0x02) screen[l*320+6]=0xffff;\r
+ if(fd&0x01) screen[l*320+7]=0xffff;\r
+ }\r
+ screen += 8;\r
+ }\r
}\r
\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
+ text_out8_builtin(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
+ text_out16_builtin(x, y, text);\r
}\r
}\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
}\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
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 RunEvents(unsigned int which)\r
{\r
if(which & 0x1800) { // save or load (but not both)\r
int do_it = 1;\r
- if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && emu_check_save_file(state_slot)) {\r
+ if ( emu_check_save_file(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
if(events & 0x6000) {\r
int vol = currentConfig.volume;\r
if (events & 0x2000) {\r
- if (vol < 90) vol++;\r
+ if (vol < 99) vol++;\r
} else {\r
if (vol > 0) vol--;\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(void)\r
+static void SkipFrame(int do_audio)\r
{\r
- PicoSkipFrame=1;\r
+ PicoSkipFrame=do_audio ? 1 : 2;\r
PicoFrame();\r
PicoSkipFrame=0;\r
}\r
void emu_forced_frame(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
- PicoOpt |= 0x10;\r
- PicoFrameFull();\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, framebuff+328*8);\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
+*/\r
PicoOpt = po_old;\r
+ currentConfig.EmuOpt = eo_old;\r
}\r
\r
static void simpleWait(int thissec, int lim_time)\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, 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
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
// prepare sound stuff\r
if(currentConfig.EmuOpt & 4) {\r
int snd_excess_add;\r
- if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {\r
+ if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
+ ((PicoOpt&0x200) && 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
+ sound_rerate(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
// 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
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
// 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(); frames_done++;\r
+ SkipFrame(1); frames_done++;\r
}\r
\r
frames_done -= target_fps; if (frames_done < 0) frames_done = 0;\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(); frames_done++;\r
+ SkipFrame(1); 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
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
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
\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
\r
- // if in 16bit mode, generate 8it image for menu background\r
- if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80))\r
+ // if in 8bit mode, generate 16bit image for menu background\r
+ if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
emu_forced_frame();\r
-\r
- // for menu bg\r
- gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
}\r
\r
\r
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
+ if(Pico.m.sram_reg & 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
+#ifndef NO_SYNC\r
sync();\r
+#endif\r
}\r
}\r
return ret;\r
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