#include "menu.h"\r
#include "fonts.h"\r
#include "lprintf.h"\r
+#include "config.h"\r
\r
#include <Pico/PicoInt.h>\r
#include <Pico/Patch.h>\r
#define SCREEN_BUFFER psp_screen\r
#endif\r
\r
-char *PicoConfigFile = "picoconfig.bin";\r
-currentConfig_t currentConfig;\r
-unsigned char *rom_data = NULL;\r
+char *PicoConfigFile = "picoconfig.cfg";\r
+currentConfig_t currentConfig, defaultConfig;\r
+int rom_loaded = 0;\r
char noticeMsg[64];\r
int state_slot = 0;\r
int config_slot = 0, config_slot_current = 0;\r
}\r
}\r
\r
+/* check if the name begins with BIOS name */\r
+static int emu_isBios(const char *name)\r
+{\r
+ int i;\r
+ for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)\r
+ if (strstr(name, biosfiles_us[i]) != NULL) return 1;\r
+ for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)\r
+ if (strstr(name, biosfiles_eu[i]) != NULL) return 1;\r
+ for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)\r
+ if (strstr(name, biosfiles_jp[i]) != NULL) return 1;\r
+ return 0;\r
+}\r
+\r
+static unsigned char scd_id_header[0x100];\r
+\r
/* checks if romFileName points to valid MegaCD image\r
* if so, checks for suitable BIOS */\r
int emu_cdCheck(int *pregion)\r
return 0;\r
}\r
\r
+ pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);\r
+ pm_read(scd_id_header, sizeof(scd_id_header), cd_f);\r
+\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
return type;\r
}\r
\r
+static int extract_text(char *dest, unsigned char *src, int len, int swab)\r
+{\r
+ char *p = dest;\r
+ int i;\r
+\r
+ if (swab) swab = 1;\r
+\r
+ for (i = len - 1; i >= 0; i--)\r
+ {\r
+ if (src[i^swab] != ' ') break;\r
+ }\r
+ len = i + 1;\r
+\r
+ for (i = 0; i < len; i++)\r
+ {\r
+ unsigned char s = src[i^swab];\r
+ if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&\r
+ s != '[' && s != ']' && s != '\\')\r
+ {\r
+ *p++ = s;\r
+ }\r
+ else\r
+ {\r
+ sprintf(p, "\\%02x", s);\r
+ p += 3;\r
+ }\r
+ }\r
+\r
+ return p - dest;\r
+}\r
+\r
+char *emu_makeRomId(void)\r
+{\r
+ static char id_string[3+0x11+0x11+0x30+16];\r
+ unsigned char *id_header;\r
+ int pos;\r
+\r
+ if (Pico.rom == NULL) {\r
+ id_string[0] = 0;\r
+ return id_string;\r
+ }\r
+\r
+ if (PicoMCD & 1) {\r
+ id_header = scd_id_header;\r
+ strcpy(id_string, "CD|");\r
+ } else {\r
+ id_header = Pico.rom + 0x100;\r
+ strcpy(id_string, "MD|");\r
+ }\r
+ pos = 3;\r
+\r
+ pos += extract_text(id_string + pos, id_header + 0x80, 0x10, 1); // seral\r
+ id_string[pos] = '|'; pos++;\r
+ pos += extract_text(id_string + pos, id_header + 0xf0, 0x10, 1); // region\r
+ id_string[pos] = '|'; pos++;\r
+ pos += extract_text(id_string + pos, id_header + 0x50, 0x30, 1); // overseas name\r
+ id_string[pos] = 0;\r
+\r
+ printf("id_string: %s\n", id_string);\r
+ return id_string;\r
+}\r
+\r
int emu_ReloadRom(void)\r
{\r
unsigned int rom_size = 0;\r
char *used_rom_name = romFileName;\r
+ unsigned char *rom_data = NULL;\r
char ext[5];\r
pm_file *rom;\r
int ret, cd_state, cd_region, cfg_loaded = 0;\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
+ 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
PicoPatchUnload();\r
\r
// check for movie file\r
- if(movie_data) {\r
+ if (movie_data) {\r
free(movie_data);\r
movie_data = 0;\r
}\r
- if(!strcmp(ext, ".gmv")) {\r
+ if (!strcmp(ext, ".gmv")) {\r
// check for both gmv and rom\r
int dummy;\r
FILE *movie_file = fopen(romFileName, "rb");\r
}\r
\r
rom = pm_open(used_rom_name);\r
- if(!rom) {\r
+ if (!rom) {\r
sprintf(menuErrorMsg, "Failed to open rom.");\r
return 0;\r
}\r
\r
menu_romload_prepare(used_rom_name); // also CD load\r
\r
- if(rom_data) {\r
- free(rom_data);\r
- rom_data = 0;\r
- rom_size = 0;\r
- }\r
+ PicoCartUnload();\r
+ rom_loaded = 0;\r
\r
- if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r
+ if ( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r
sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r
lprintf("%s\n", menuErrorMsg);\r
pm_close(rom);\r
pm_close(rom);\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
+ if (rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\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
menu_romload_end();\r
return 0;\r
}\r
\r
lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r
- if(PicoCartInsert(rom_data, rom_size)) {\r
+ if (PicoCartInsert(rom_data, rom_size)) {\r
sprintf(menuErrorMsg, "Failed to load ROM.");\r
menu_romload_end();\r
return 0;\r
\r
menu_romload_end();\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
+ if (!emu_isBios(romFileName))\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
\r
if (PicoPatches) {\r
PicoPatchPrepare();\r
emu_noticeMsgUpdated();\r
\r
// load SRAM for this ROM\r
- if(currentConfig.EmuOpt & 1)\r
+ if (currentConfig.EmuOpt & 1)\r
emu_SaveLoadGame(1, 1);\r
\r
+ rom_loaded = 1;\r
return 1;\r
}\r
\r
\r
int emu_ReadConfig(int game, int no_defaults)\r
{\r
+ char cfg[512];\r
FILE *f;\r
- char cfg[512], extbuf[16];\r
- int bread = 0;\r
+ int ret;\r
\r
if (!game)\r
{\r
if (!no_defaults)\r
- {\r
emu_setDefaultConfig();\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
+ sprintf(p, ".%i.cfg", config_slot);\r
}\r
cfg[511] = 0;\r
- } else {\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
+ ret = config_readsect(cfg, NULL);\r
}\r
+ else\r
+ {\r
+ if (!no_defaults)\r
+ emu_setDefaultConfig();\r
\r
- lprintf("emu_ReadConfig: %s ", cfg);\r
- f = fopen(cfg, "rb");\r
- if (f) {\r
- bread = fread(¤tConfig, 1, sizeof(currentConfig), f);\r
- fclose(f);\r
+ // try new .cfg way\r
+ if (config_slot != 0)\r
+ sprintf(cfg, "game.%i.cfg", config_slot);\r
+ else strcpy(cfg, "game.cfg");\r
+ ret = config_readsect(cfg, emu_makeRomId());\r
+\r
+ if (ret != 0)\r
+ {\r
+ // fall back to old\r
+ char extbuf[16];\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) {\r
+ romfname_ext(cfg, NULL, ".pbcfg");\r
+ f = fopen(cfg, "rb");\r
+ }\r
+ if (f) {\r
+ int bread = fread(¤tConfig, 1, sizeof(currentConfig), f);\r
+ lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)");\r
+ fclose(f);\r
+ ret = 0;\r
+ }\r
+\r
+ if (ret == 0) {\r
+ PicoOpt = currentConfig.s_PicoOpt;\r
+ PsndRate = currentConfig.s_PsndRate;\r
+ PicoRegionOverride = currentConfig.s_PicoRegion;\r
+ PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
+ PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ }\r
}\r
- lprintf(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
- //scaling_update();\r
// some sanity checks\r
if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
+#ifdef PSP\r
+ if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;\r
+#else\r
if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r
+#endif\r
if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;\r
#ifdef __GP2X__\r
// if volume keys are unbound, bind them to volume control\r
currentConfig.KeyBinds[22] = 1<<30; // vol down\r
}\r
#endif\r
- if (bread > 0) config_slot_current = config_slot;\r
- return (bread > 0); // == sizeof(currentConfig));\r
+ if (ret == 0) config_slot_current = config_slot;\r
+ return (ret == 0);\r
}\r
\r
\r
-int emu_WriteConfig(int game)\r
+int emu_WriteConfig(int is_game)\r
{\r
- FILE *f;\r
- char cfg[512], extbuf[16];\r
- int bwrite = 0;\r
+ char cfg[512], *game_sect = NULL;\r
+ int ret, write_lrom = 0;\r
\r
- if (!game)\r
+ if (!is_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
+ sprintf(p, ".%i.cfg", config_slot);\r
}\r
cfg[511] = 0;\r
+ write_lrom = 1;\r
} else {\r
if (config_slot != 0)\r
- sprintf(extbuf, ".%i.pbcfg", config_slot);\r
- else strcpy(extbuf, ".pbcfg");\r
- romfname_ext(cfg, "cfg/", extbuf);\r
+ sprintf(cfg, "game.%i.cfg", config_slot);\r
+ else strcpy(cfg, "game.cfg");\r
+ game_sect = emu_makeRomId();\r
}\r
\r
lprintf("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
- currentConfig.PicoCDBuffers = PicoCDBuffers;\r
- bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);\r
- fflush(f);\r
- fclose(f);\r
+ ret = config_writesect(cfg, game_sect);\r
#ifndef NO_SYNC\r
- sync();\r
+ sync();\r
#endif\r
- }\r
- lprintf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
+ lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");\r
\r
- if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;\r
- return (bwrite == sizeof(currentConfig));\r
+ if (ret == 0) config_slot_current = config_slot;\r
+ return ret == 0;\r
}\r
\r
\r