int emu_SaveLoadSRAM(int load) { int ret = 0; static char saveFname[512]; romfname_ext(saveFname, NULL, (PicoMCD & 1) ? ".brm" : ".srm"); if (load && !try_ropen_file(saveFname)) return 0; FILE *sramFile; int sram_size; unsigned char *sram_data; int truncate = 1; if (PicoMCD & 1) { if (PicoOpt & PicoOpt_enable_cd_ramcart) { // MCD RAM cart? sram_size = 0x12000; sram_data = SRam.data; if (sram_data) memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4); } else { sram_size = 0x2000; sram_data = Pico_mcd->bram; truncate = 0; // the .brm may contain RAM cart data after normal brm } } else { sram_size = SRam.end - SRam.start + 1; if(Pico.m.sram_reg & 4) sram_size=0x2000; sram_data = SRam.data; } if (!sram_data) return 0; // SRam forcefully disabled for this game if (load) { sramFile = fopen(saveFname, "rb"); if(!sramFile) return -1; fread(sram_data, 1, sram_size, sramFile); fclose(sramFile); if ((PicoMCD & 1) && (PicoOpt & PicoOpt_enable_cd_ramcart)) memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4); } else { // sram save needs some special processing // see if we have anything to save for (; sram_size > 0; sram_size--) if (sram_data[sram_size - 1]) break; if (sram_size) { sramFile = fopen(saveFname, truncate ? "wb" : "r+b"); if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry if (!sramFile) return -1; ret = fwrite(sram_data, 1, sram_size, sramFile); ret = (ret != sram_size) ? -1 : 0; fclose(sramFile); } } return ret; }
int emu_WriteConfig(int game) { FILE *f; char cfg[512], extbuf[16]; int bwrite = 0; if (!game) { strncpy(cfg, PicoConfigFile, 511); if (config_slot != 0) { char *p = strrchr(cfg, '.'); if (p == NULL) p = cfg + strlen(cfg); sprintf(extbuf, ".%i.pbcfg", config_slot); strncpy(p, extbuf, 511 - (p - cfg)); } cfg[511] = 0; } else { if (config_slot != 0) sprintf(extbuf, ".%i.pbcfg", config_slot); else strcpy(extbuf, ".pbcfg"); romfname_ext(cfg, cfgPath, extbuf); } lprintf("emu_WriteConfig: %s ", cfg); f = fopen(cfg, "wb"); if (f) { currentConfig.PicoOpt = PicoOpt; currentConfig.PsndRate = PsndRate; currentConfig.PicoRegion = PicoRegionOverride; currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder; currentConfig.PicoCDBuffers = PicoCDBuffers; bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f); fflush(f); fclose(f); #ifndef NO_SYNC sync(); #endif } lprintf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n"); if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot; return (bwrite == sizeof(currentConfig)); }
char *emu_GetSaveFName(int load, int is_sram, int slot) { static char saveFname[512]; char ext[16]; if (is_sram) { romfname_ext(saveFname, (PicoMCD&1) ? brmPath : srmPath, (PicoMCD&1) ? ".brm" : ".srm"); if (load) { if (try_ropen_file(saveFname)) return saveFname; // try in current dir.. romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm"); if (try_ropen_file(saveFname)) return saveFname; return NULL; // give up } } else { ext[0] = 0; if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot); strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds"); romfname_ext(saveFname, mdsPath, ext); if (load) { if (try_ropen_file(saveFname)) return saveFname; romfname_ext(saveFname, NULL, ext); if (try_ropen_file(saveFname)) return saveFname; if (currentConfig.EmuOpt & 8) { ext[0] = 0; if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot); strcat(ext, ".mds"); romfname_ext(saveFname, mdsPath, ext); if (try_ropen_file(saveFname)) return saveFname; romfname_ext(saveFname, NULL, ext); if (try_ropen_file(saveFname)) return saveFname; } return NULL; } } return saveFname; }
int emu_ReloadRom(void) { unsigned int rom_size = 0; char *used_rom_name = romFileName; char ext[5]; pm_file *rom; int ret, cd_state, cd_region, cfg_loaded = 0; lprintf("emu_ReloadRom(%s)\n", romFileName); get_ext(romFileName, ext); // detect wrong extensions if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz sprintf(menuErrorMsg, "Not a ROM selected."); return 0; } PicoPatchUnload(); // check for movie file if(movie_data) { free(movie_data); movie_data = 0; } #if 0 if(!strcmp(ext, ".gmv")) { // check for both gmv and rom int dummy; FILE *movie_file = fopen(romFileName, "rb"); if(!movie_file) { sprintf(menuErrorMsg, "Failed to open movie."); return 0; } fseek(movie_file, 0, SEEK_END); movie_size = ftell(movie_file); fseek(movie_file, 0, SEEK_SET); if(movie_size < 64+3) { sprintf(menuErrorMsg, "Invalid GMV file."); fclose(movie_file); return 0; } movie_data = malloc(movie_size); if(movie_data == NULL) { sprintf(menuErrorMsg, "low memory."); fclose(movie_file); return 0; } fread(movie_data, 1, movie_size, movie_file); fclose(movie_file); if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) { sprintf(menuErrorMsg, "Invalid GMV file."); return 0; } dummy = try_rfn_cut() || try_rfn_cut(); if (!dummy) { sprintf(menuErrorMsg, "Could't find a ROM for movie."); return 0; } get_ext(romFileName, ext); } else if (!strcmp(ext, ".pat")) { int dummy; PicoPatchLoad(romFileName); dummy = try_rfn_cut() || try_rfn_cut(); if (!dummy) { sprintf(menuErrorMsg, "Could't find a ROM to patch."); return 0; } get_ext(romFileName, ext); } #else // load GG patches list char ggFname[512]; romfname_ext(ggFname, NULL, ".cht"); PicoPatchLoad(ggFname); #endif if ((PicoMCD & 1) && Pico_mcd != NULL) Stop_CD(); // check for MegaCD image cd_state = emu_cdCheck(&cd_region); if (cd_state > 0) { // valid CD image, check for BIOS.. #if 0 // we need to have config loaded at this point ret = emu_ReadConfig(READ_CONFIG_FOR_GAME, READ_CONFIG_SKIP_DEF); if (!ret) emu_ReadConfig(READ_CONFIG_GLOBAL, READ_CONFIG_SKIP_DEF); #endif cfg_loaded = 1; if (PicoRegionOverride) { cd_region = PicoRegionOverride; lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA"); } if (!emu_findBios(cd_region, &used_rom_name)) { // bios_help() ? return 0; } PicoMCD |= 1; get_ext(used_rom_name, ext); } else { if (PicoMCD & 1) Stop_CD(); PicoMCD &= ~1; } ips_define(ipsFileName); rom = pm_open(used_rom_name); if(!rom) { sprintf(menuErrorMsg, "Failed to open rom."); return 0; } menu_romload_prepare(used_rom_name); // also CD load if(rom_data) { free(rom_data); rom_data = 0; rom_size = 0; } if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) { sprintf(menuErrorMsg, "PicoCartLoad() failed."); lprintf("%s\n", menuErrorMsg); pm_close(rom); menu_romload_end(); return 0; } pm_close(rom); // detect wrong files (Pico crashes on very small files), also see if ROM EP is good if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 || ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) { if (rom_data) free(rom_data); rom_data = 0; sprintf(menuErrorMsg, "Not a ROM selected."); menu_romload_end(); return 0; } #if 0 // load config for this ROM (do this before insert to get correct region) if (!cfg_loaded) { ret = emu_ReadConfig(READ_CONFIG_FOR_GAME, READ_CONFIG_SKIP_DEF); if (!ret) emu_ReadConfig(READ_CONFIG_GLOBAL, READ_CONFIG_SKIP_DEF); } #endif lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size); if(PicoCartInsert(rom_data, rom_size)) { sprintf(menuErrorMsg, "Failed to load ROM."); menu_romload_end(); return 0; } Pico.m.frame_count = 0; // insert CD if it was detected if (cd_state > 0) { ret = Insert_CD(romFileName, cd_state == 2); if (ret != 0) { sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?"); lprintf("%s\n", menuErrorMsg); menu_romload_end(); return 0; } } menu_romload_end(); #if 0 if (!emu_isBios(romFileName)) { // emu_ReadConfig() might have messed currentConfig.lastRomFile strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1); currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0; } #endif if (PicoPatches) { PicoPatchPrepare(); PicoPatchApply(); } // additional movie stuff if (movie_data) { if(movie_data[0x14] == '6') PicoOpt |= PicoOpt_6button_gamepad; // 6 button pad else PicoOpt &= ~PicoOpt_6button_gamepad; PicoOpt |= (PicoOpt_accurate_timing | PicoOpt_disable_vdp_fifo); // accurate timing, no VDP fifo timing if(movie_data[0xF] >= 'A') { if(movie_data[0x16] & 0x80) { PicoRegionOverride = 8; } else { PicoRegionOverride = 4; } PicoReset(0); // TODO: bits 6 & 5 } movie_data[0x18+30] = 0; sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]); } else { PicoOpt &= ~PicoOpt_disable_vdp_fifo; if(Pico.m.pal) { strcpy(noticeMsg, "PAL SYSTEM / 50 FPS"); } else { strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS"); } } emu_noticeMsgUpdated(); // load SRAM for this ROM emu_SaveLoadSRAM(1); return 1; }
int emu_ReadConfig(int game, int no_defaults) { FILE *f; char cfg[512], extbuf[16]; int bread = 0; if (!game) { if (!no_defaults) { emu_setDefaultConfig(); } strncpy(cfg, PicoConfigFile, 511); if (config_slot != 0) { char *p = strrchr(cfg, '.'); if (p == NULL) p = cfg + strlen(cfg); sprintf(extbuf, ".%i.pbcfg", config_slot); strncpy(p, extbuf, 511 - (p - cfg)); } cfg[511] = 0; } else { if (config_slot != 0) sprintf(extbuf, ".%i.pbcfg", config_slot); else strcpy(extbuf, ".pbcfg"); romfname_ext(cfg, cfgPath, extbuf); f = fopen(cfg, "rb"); if (!f) romfname_ext(cfg, NULL, ".pbcfg"); else fclose(f); } lprintf("emu_ReadConfig: %s ", cfg); f = fopen(cfg, "rb"); if (f) { bread = fread(¤tConfig, 1, sizeof(currentConfig), f); fclose(f); } lprintf(bread > 0 ? "(ok)\n" : "(failed)\n"); PicoOpt = currentConfig.PicoOpt; PsndRate = currentConfig.PsndRate; PicoRegionOverride = currentConfig.PicoRegion; PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder; PicoCDBuffers = currentConfig.PicoCDBuffers; //scaling_update(); // some sanity checks if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200; #ifdef PSP if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0; #else if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100; #endif if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50; #ifdef __GP2X__ // if volume keys are unbound, bind them to volume control if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) { currentConfig.KeyBinds[23] = 1<<29; // vol up currentConfig.KeyBinds[22] = 1<<30; // vol down } #endif if (bread > 0) config_slot_current = config_slot; return (bread > 0); // == sizeof(currentConfig)); }