X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Femu.c;h=e4cce672d70f188718d329d10a8e365025306132;hb=dd5fd4778d797a1ef4767604b1c57c924ed0f64b;hp=723dfb74050e3d8c79a37a3c72353f694f117925;hpb=11f4e72292ab00c686f2a2288e4f1cda3a0f7abd;p=libpicofe.git diff --git a/common/emu.c b/common/emu.c index 723dfb7..e4cce67 100644 --- a/common/emu.c +++ b/common/emu.c @@ -14,6 +14,7 @@ #include "menu.h" #include "fonts.h" #include "lprintf.h" +#include "config.h" #include #include @@ -26,15 +27,21 @@ #elif defined(__GIZ__) #include "../gizmondo/giz.h" #define SCREEN_WIDTH 321 - #define SCREEN_BUFFER giz_screen // ? + #define SCREEN_BUFFER giz_screen +#elif defined(PSP) + #include "../psp/psp.h" + #define SCREEN_WIDTH 512 + #define SCREEN_BUFFER psp_screen #endif -char *PicoConfigFile = "picoconfig.bin"; -currentConfig_t currentConfig; -unsigned char *rom_data = NULL; +char *PicoConfigFile = "config.cfg"; +currentConfig_t currentConfig, defaultConfig; +int rom_loaded = 0; char noticeMsg[64]; int state_slot = 0; int config_slot = 0, config_slot_current = 0; +char lastRomFile[512]; +int kb_combo_keys = 0, kb_combo_acts = 0; // keys and actions which need button combos unsigned char *movie_data = NULL; static int movie_size = 0; @@ -82,8 +89,8 @@ static void get_ext(char *file, char *ext) strlwr_(ext); } -char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" }; -char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" }; +char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" }; +char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" }; char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" }; int emu_findBios(int region, char **bios_file) @@ -133,6 +140,23 @@ int emu_findBios(int region, char **bios_file) } } +/* check if the name begins with BIOS name */ +/* +static int emu_isBios(const char *name) +{ + int i; + for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++) + if (strstr(name, biosfiles_us[i]) != NULL) return 1; + for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++) + if (strstr(name, biosfiles_eu[i]) != NULL) return 1; + for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++) + if (strstr(name, biosfiles_jp[i]) != NULL) return 1; + return 0; +} +*/ + +static unsigned char id_header[0x100]; + /* checks if romFileName points to valid MegaCD image * if so, checks for suitable BIOS */ int emu_cdCheck(int *pregion) @@ -156,6 +180,9 @@ int emu_cdCheck(int *pregion) return 0; } + pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET); + pm_read(id_header, sizeof(id_header), cd_f); + /* it seems we have a CD image here. Try to detect region now.. */ pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET); pm_read(buf, 1, cd_f); @@ -172,10 +199,64 @@ int emu_cdCheck(int *pregion) return type; } +static int extract_text(char *dest, unsigned char *src, int len, int swab) +{ + char *p = dest; + int i; + + if (swab) swab = 1; + + for (i = len - 1; i >= 0; i--) + { + if (src[i^swab] != ' ') break; + } + len = i + 1; + + for (i = 0; i < len; i++) + { + unsigned char s = src[i^swab]; + if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' && + s != '[' && s != ']' && s != '\\') + { + *p++ = s; + } + else + { + sprintf(p, "\\%02x", s); + p += 3; + } + } + + return p - dest; +} + +char *emu_makeRomId(void) +{ + static char id_string[3+0x11+0x11+0x30+16]; + int pos, swab = 1; + + if (PicoAHW & PAHW_MCD) { + strcpy(id_string, "CD|"); + swab = 0; + } + else strcpy(id_string, "MD|"); + pos = 3; + + pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial + id_string[pos] = '|'; pos++; + pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region + id_string[pos] = '|'; pos++; + pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name + id_string[pos] = 0; + + return id_string; +} + int emu_ReloadRom(void) { unsigned int rom_size = 0; char *used_rom_name = romFileName; + unsigned char *rom_data = NULL; char ext[5]; pm_file *rom; int ret, cd_state, cd_region, cfg_loaded = 0; @@ -185,7 +266,7 @@ int emu_ReloadRom(void) get_ext(romFileName, ext); // detect wrong extensions - if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz + if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz sprintf(menuErrorMsg, "Not a ROM selected."); return 0; } @@ -193,11 +274,12 @@ int emu_ReloadRom(void) PicoPatchUnload(); // check for movie file - if(movie_data) { + if (movie_data) { free(movie_data); movie_data = 0; } - if(!strcmp(ext, ".gmv")) { + if (!strcmp(ext, ".gmv")) + { // check for both gmv and rom int dummy; FILE *movie_file = fopen(romFileName, "rb"); @@ -243,13 +325,14 @@ int emu_ReloadRom(void) get_ext(romFileName, ext); } - if ((PicoMCD & 1) && Pico_mcd != NULL) + if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL) Stop_CD(); // check for MegaCD image cd_state = emu_cdCheck(&cd_region); if (cd_state > 0) { + PicoAHW |= PAHW_MCD; // valid CD image, check for BIOS.. // we need to have config loaded at this point @@ -263,33 +346,30 @@ int emu_ReloadRom(void) } if (!emu_findBios(cd_region, &used_rom_name)) { // bios_help() ? + PicoAHW &= ~PAHW_MCD; return 0; } - PicoMCD |= 1; get_ext(used_rom_name, ext); } else { - if (PicoMCD & 1) Stop_CD(); - PicoMCD &= ~1; + if (PicoAHW & PAHW_MCD) Stop_CD(); + PicoAHW &= ~PAHW_MCD; } rom = pm_open(used_rom_name); - if(!rom) { + if (!rom) { sprintf(menuErrorMsg, "Failed to open rom."); return 0; } - menu_romload_prepare(used_rom_name); + menu_romload_prepare(used_rom_name); // also CD load - if(rom_data) { - free(rom_data); - rom_data = 0; - rom_size = 0; - } + PicoCartUnload(); + rom_loaded = 0; - if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) { + if ( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) { sprintf(menuErrorMsg, "PicoCartLoad() failed."); lprintf("%s\n", menuErrorMsg); pm_close(rom); @@ -297,26 +377,28 @@ int emu_ReloadRom(void) return 0; } pm_close(rom); - menu_romload_end(); // 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 || + 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; } // load config for this ROM (do this before insert to get correct region) + if (!(PicoAHW & PAHW_MCD)) + memcpy(id_header, rom_data + 0x100, sizeof(id_header)); if (!cfg_loaded) { ret = emu_ReadConfig(1, 1); if (!ret) emu_ReadConfig(0, 1); } lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size); - if(PicoCartInsert(rom_data, rom_size)) { + if (PicoCartInsert(rom_data, rom_size)) { sprintf(menuErrorMsg, "Failed to load ROM."); + menu_romload_end(); return 0; } @@ -328,13 +410,12 @@ int emu_ReloadRom(void) if (ret != 0) { sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?"); lprintf("%s\n", menuErrorMsg); + menu_romload_end(); return 0; } } - // emu_ReadConfig() might have messed currentConfig.lastRomFile - strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1); - currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0; + menu_romload_end(); if (PicoPatches) { PicoPatchPrepare(); @@ -344,16 +425,16 @@ int emu_ReloadRom(void) // additional movie stuff if (movie_data) { if(movie_data[0x14] == '6') - PicoOpt |= 0x20; // 6 button pad - else PicoOpt &= ~0x20; - PicoOpt |= 0x10040; // accurate timing, no VDP fifo timing + PicoOpt |= POPT_6BTN_PAD; // 6 button pad + else PicoOpt &= ~POPT_6BTN_PAD; + PicoOpt |= POPT_DIS_VDP_FIFO|POPT_ACC_TIMING; // accurate timing, no VDP fifo timing if(movie_data[0xF] >= 'A') { if(movie_data[0x16] & 0x80) { PicoRegionOverride = 8; } else { PicoRegionOverride = 4; } - PicoReset(0); + PicoReset(); // TODO: bits 6 & 5 } movie_data[0x18+30] = 0; @@ -361,7 +442,7 @@ int emu_ReloadRom(void) } else { - PicoOpt &= ~0x10000; + PicoOpt &= ~POPT_DIS_VDP_FIFO; if(Pico.m.pal) { strcpy(noticeMsg, "PAL SYSTEM / 50 FPS"); } else { @@ -371,9 +452,12 @@ int emu_ReloadRom(void) emu_noticeMsgUpdated(); // load SRAM for this ROM - if(currentConfig.EmuOpt & 1) + if (currentConfig.EmuOpt & 1) emu_SaveLoadGame(1, 1); + strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1); + lastRomFile[sizeof(lastRomFile)-1] = 0; + rom_loaded = 1; return 1; } @@ -397,54 +481,100 @@ static void romfname_ext(char *dst, const char *prefix, const char *ext) } +static void make_config_cfg(char *cfg) +{ + strncpy(cfg, PicoConfigFile, 511); + if (config_slot != 0) + { + char *p = strrchr(cfg, '.'); + if (p == NULL) p = cfg + strlen(cfg); + sprintf(p, ".%i.cfg", config_slot); + } + cfg[511] = 0; +} + int emu_ReadConfig(int game, int no_defaults) { + char cfg[512]; FILE *f; - char cfg[512], extbuf[16]; - int bread = 0; + int ret; if (!game) { if (!no_defaults) + emu_setDefaultConfig(); + make_config_cfg(cfg); + ret = config_readsect(cfg, NULL); + } + else + { + char *sect = emu_makeRomId(); + + // try new .cfg way + if (config_slot != 0) + sprintf(cfg, "game.%i.cfg", config_slot); + else strcpy(cfg, "game.cfg"); + + ret = -1; + if (config_havesect(cfg, sect)) { + // read user's config + int vol = currentConfig.volume; emu_setDefaultConfig(); + ret = config_readsect(cfg, sect); + currentConfig.volume = vol; // make vol global (bah) } - strncpy(cfg, PicoConfigFile, 511); - if (config_slot != 0) + else { - char *p = strrchr(cfg, '.'); - if (p == NULL) p = cfg + strlen(cfg); - sprintf(extbuf, ".%i.pbcfg", config_slot); - strncpy(p, extbuf, 511 - (p - cfg)); + // read global config, and apply game_def.cfg on top + make_config_cfg(cfg); + config_readsect(cfg, NULL); + ret = config_readsect("game_def.cfg", sect); } - cfg[511] = 0; - } else { - if (config_slot != 0) - sprintf(extbuf, ".%i.pbcfg", config_slot); - else strcpy(extbuf, ".pbcfg"); - romfname_ext(cfg, "cfg/", 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); + if (ret != 0) + { + // fall back to old game specific cfg + char extbuf[16]; + if (config_slot != 0) + sprintf(extbuf, ".%i.pbcfg", config_slot); + else strcpy(extbuf, ".pbcfg"); + romfname_ext(cfg, "cfg/", extbuf); + f = fopen(cfg, "rb"); + if (!f) { + romfname_ext(cfg, NULL, ".pbcfg"); + f = fopen(cfg, "rb"); + } + if (f) { + int bread; + fseek(f, 512, SEEK_SET); // skip unused lrom buffer + bread = fread(¤tConfig, 1, sizeof(currentConfig), f); + lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)"); + fclose(f); + ret = 0; + } + + if (ret == 0) { + PicoOpt = currentConfig.s_PicoOpt; + PsndRate = currentConfig.s_PsndRate; + PicoRegionOverride = currentConfig.s_PicoRegion; + PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder; + // PicoCDBuffers = currentConfig.s_PicoCDBuffers; // ignore in this case + } + } + else + { + lprintf("loaded cfg from sect \"%s\"\n", sect); + } } - 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 @@ -453,54 +583,45 @@ int emu_ReadConfig(int game, int no_defaults) currentConfig.KeyBinds[22] = 1<<30; // vol down } #endif - if (bread > 0) config_slot_current = config_slot; - return (bread > 0); // == sizeof(currentConfig)); + if (ret == 0) config_slot_current = config_slot; + return (ret == 0); } -int emu_WriteConfig(int game) +int emu_WriteConfig(int is_game) { - FILE *f; - char cfg[512], extbuf[16]; - int bwrite = 0; + char cfg[512], *game_sect = NULL; + int ret, write_lrom = 0; - if (!game) + if (!is_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)); + sprintf(p, ".%i.cfg", config_slot); } cfg[511] = 0; + write_lrom = 1; } else { if (config_slot != 0) - sprintf(extbuf, ".%i.pbcfg", config_slot); - else strcpy(extbuf, ".pbcfg"); - romfname_ext(cfg, "cfg/", extbuf); + sprintf(cfg, "game.%i.cfg", config_slot); + else strcpy(cfg, "game.cfg"); + game_sect = emu_makeRomId(); + lprintf("emu_WriteConfig: sect \"%s\"\n", game_sect); } 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); + ret = config_writesect(cfg, game_sect); + if (write_lrom) config_writelrom(cfg); #ifndef NO_SYNC - sync(); + sync(); #endif - } - lprintf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n"); + lprintf((ret == 0) ? "(ok)\n" : "(failed)\n"); - if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot; - return (bwrite == sizeof(currentConfig)); + if (ret == 0) config_slot_current = config_slot; + return ret == 0; } @@ -551,6 +672,47 @@ void emu_textOut16(int x, int y, const char *text) } } +void emu_findKeyBindCombos(void) +{ + int act, u; + + // find out which keys and actions are combos + kb_combo_keys = kb_combo_acts = 0; + for (act = 0; act < 32; act++) + { + int keyc = 0, keyc2 = 0; + if (act == 16 || act == 17) continue; // player2 flag + if (act > 17) + { + for (u = 0; u < 32; u++) + if (currentConfig.KeyBinds[u] & (1 << act)) keyc++; + } + else + { + for (u = 0; u < 32; u++) + if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1 + (currentConfig.KeyBinds[u] & (1 << act))) keyc++; + for (u = 0; u < 32; u++) + if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2 + (currentConfig.KeyBinds[u] & (1 << act))) keyc2++; + if (keyc2 > keyc) keyc = keyc2; + } + if (keyc > 1) + { + // loop again and mark those keys and actions as combo + for (u = 0; u < 32; u++) + { + if (currentConfig.KeyBinds[u] & (1 << act)) { + kb_combo_keys |= 1 << u; + kb_combo_acts |= 1 << act; + } + } + } + } + + // printf("combo keys/acts: %08x %08x\n", kb_combo_keys, kb_combo_acts); +} + void emu_updateMovie(void) { @@ -611,11 +773,11 @@ char *emu_GetSaveFName(int load, int is_sram, int slot) if (is_sram) { - romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm"); + romfname_ext(saveFname, (PicoAHW&1) ? "brm/" : "srm/", (PicoAHW&1) ? ".brm" : ".srm"); if (load) { if (try_ropen_file(saveFname)) return saveFname; // try in current dir.. - romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm"); + romfname_ext(saveFname, NULL, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm"); if (try_ropen_file(saveFname)) return saveFname; return NULL; // give up } @@ -693,8 +855,9 @@ int emu_SaveLoadGame(int load, int sram) int sram_size; unsigned char *sram_data; int truncate = 1; - if (PicoMCD&1) { - if (PicoOpt&0x8000) { // MCD RAM cart? + if (PicoAHW & PAHW_MCD) + { + if (PicoOpt&POPT_EN_MCD_RAMCART) { sram_size = 0x12000; sram_data = SRam.data; if (sram_data) @@ -711,12 +874,13 @@ int emu_SaveLoadGame(int load, int sram) } if (!sram_data) return 0; // SRam forcefully disabled for this game - if (load) { + if (load) + { sramFile = fopen(saveFname, "rb"); if(!sramFile) return -1; fread(sram_data, 1, sram_size, sramFile); fclose(sramFile); - if ((PicoMCD&1) && (PicoOpt&0x8000)) + if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART)) memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4); } else { // sram save needs some special processing @@ -776,3 +940,4 @@ int emu_SaveLoadGame(int load, int sram) return ret; } } +