X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Femu.c;h=1d57e226e08eafc6d41d55271fbbb4c1e9b7e5b0;hb=a479005630268d6f2adfcc0f9562593f643c4c2c;hp=8ec61a2a7dd780eb0991dedbeedbd5aa9f7b4537;hpb=0ae25549b5066ea8b42216ec875f5c3c5d5bd8d6;p=libpicofe.git diff --git a/common/emu.c b/common/emu.c index 8ec61a2..1d57e22 100644 --- a/common/emu.c +++ b/common/emu.c @@ -18,6 +18,7 @@ #include #include +#include #include #if defined(__GP2X__) @@ -34,12 +35,14 @@ #define SCREEN_BUFFER psp_screen #endif -char *PicoConfigFile = "picoconfig.cfg"; +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; @@ -87,8 +90,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) @@ -139,6 +142,7 @@ 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; @@ -150,8 +154,9 @@ static int emu_isBios(const char *name) if (strstr(name, biosfiles_jp[i]) != NULL) return 1; return 0; } +*/ -static unsigned char scd_id_header[0x100]; +static unsigned char id_header[0x100]; /* checks if romFileName points to valid MegaCD image * if so, checks for suitable BIOS */ @@ -159,9 +164,24 @@ int emu_cdCheck(int *pregion) { unsigned char buf[32]; pm_file *cd_f; - int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe + int region = 4; // 1: Japan, 4: US, 8: Europe + char ext[5], *fname = romFileName; + cue_track_type type = CT_UNKNOWN; + cue_data_t *cue_data = NULL; + + get_ext(romFileName, ext); + if (strcasecmp(ext, ".cue") == 0) { + cue_data = cue_parse(romFileName); + if (cue_data != NULL) { + fname = cue_data->tracks[1].fname; + type = cue_data->tracks[1].type; + } + } + + cd_f = pm_open(fname); + if (cue_data != NULL) + cue_destroy(cue_data); - cd_f = pm_open(romFileName); if (!cd_f) return 0; // let the upper level handle this if (pm_read(buf, 32, cd_f) != 32) { @@ -169,18 +189,27 @@ int emu_cdCheck(int *pregion) return 0; } - if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO) - if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN) - if (type == 0) { + if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) { + if (type && type != CT_ISO) + elprintf(EL_STATUS, ".cue has wrong type: %i", type); + type = CT_ISO; // Sega CD (ISO) + } + if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) { + if (type && type != CT_BIN) + elprintf(EL_STATUS, ".cue has wrong type: %i", type); + type = CT_BIN; // Sega CD (BIN) + } + + if (type == CT_UNKNOWN) { pm_close(cd_f); return 0; } - pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET); - pm_read(scd_id_header, sizeof(scd_id_header), cd_f); + pm_seek(cd_f, (type == CT_ISO) ? 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_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET); pm_read(buf, 1, cd_f); pm_close(cd_f); @@ -188,14 +217,14 @@ int emu_cdCheck(int *pregion) if (buf[0] == 0xa1) region = 1; // JAP lprintf("detected %s Sega/Mega CD image with %s region\n", - type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA"); + type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA"); if (pregion != NULL) *pregion = region; return type; } -static int extract_text(char *dest, unsigned char *src, int len, int swab) +static int extract_text(char *dest, const unsigned char *src, int len, int swab) { char *p = dest; int i; @@ -229,31 +258,22 @@ static int extract_text(char *dest, unsigned char *src, int len, int swab) char *emu_makeRomId(void) { static char id_string[3+0x11+0x11+0x30+16]; - unsigned char *id_header; - int pos; + int pos, swab = 1; - if (Pico.rom == NULL) { - id_string[0] = 0; - return id_string; - } - - if (PicoMCD & 1) { - id_header = scd_id_header; + if (PicoAHW & PAHW_MCD) { strcpy(id_string, "CD|"); - } else { - id_header = Pico.rom + 0x100; - strcpy(id_string, "MD|"); + swab = 0; } + else strcpy(id_string, "MD|"); pos = 3; - pos += extract_text(id_string + pos, id_header + 0x80, 0x10, 1); // seral + 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, 0x10, 1); // region + 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, 1); // overseas name + pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name id_string[pos] = 0; - printf("id_string: %s\n", id_string); return id_string; } @@ -283,7 +303,8 @@ int emu_ReloadRom(void) 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"); @@ -329,13 +350,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) + if (cd_state != CIT_NOT_CD) { + PicoAHW |= PAHW_MCD; // valid CD image, check for BIOS.. // we need to have config loaded at this point @@ -349,16 +371,16 @@ 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); @@ -391,6 +413,8 @@ int emu_ReloadRom(void) } // 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); @@ -403,11 +427,9 @@ int emu_ReloadRom(void) 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 (cd_state != CIT_NOT_CD) { + ret = Insert_CD(romFileName, cd_state); if (ret != 0) { sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?"); lprintf("%s\n", menuErrorMsg); @@ -418,13 +440,6 @@ int emu_ReloadRom(void) menu_romload_end(); - 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; - } - if (PicoPatches) { PicoPatchPrepare(); PicoPatchApply(); @@ -433,16 +448,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; // 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; @@ -450,7 +465,7 @@ int emu_ReloadRom(void) } else { - PicoOpt &= ~0x10000; + PicoOpt &= ~POPT_DIS_VDP_FIFO; if(Pico.m.pal) { strcpy(noticeMsg, "PAL SYSTEM / 50 FPS"); } else { @@ -463,6 +478,8 @@ int emu_ReloadRom(void) if (currentConfig.EmuOpt & 1) emu_SaveLoadGame(1, 1); + strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1); + lastRomFile[sizeof(lastRomFile)-1] = 0; rom_loaded = 1; return 1; } @@ -487,6 +504,18 @@ 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]; @@ -497,30 +526,38 @@ int emu_ReadConfig(int game, int no_defaults) { 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(p, ".%i.cfg", config_slot); - } - cfg[511] = 0; + make_config_cfg(cfg); ret = config_readsect(cfg, NULL); } else { - if (!no_defaults) - emu_setDefaultConfig(); + 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 = config_readsect(cfg, emu_makeRomId()); + + 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) + } + else + { + // 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); + } if (ret != 0) { - // fall back to old + // fall back to old game specific cfg char extbuf[16]; if (config_slot != 0) sprintf(extbuf, ".%i.pbcfg", config_slot); @@ -532,7 +569,9 @@ int emu_ReadConfig(int game, int no_defaults) f = fopen(cfg, "rb"); } if (f) { - int bread = fread(¤tConfig, 1, sizeof(currentConfig), 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; @@ -543,11 +582,12 @@ int emu_ReadConfig(int game, int no_defaults) PsndRate = currentConfig.s_PsndRate; PicoRegionOverride = currentConfig.s_PicoRegion; PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder; - PicoCDBuffers = currentConfig.s_PicoCDBuffers; + // PicoCDBuffers = currentConfig.s_PicoCDBuffers; // ignore in this case } } else { + lprintf("loaded cfg from sect \"%s\"\n", sect); } } @@ -555,6 +595,7 @@ int emu_ReadConfig(int game, int no_defaults) if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200; #ifdef PSP if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0; + if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0; #else if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100; #endif @@ -592,10 +633,12 @@ int emu_WriteConfig(int is_game) 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); ret = config_writesect(cfg, game_sect); + if (write_lrom) config_writelrom(cfg); #ifndef NO_SYNC sync(); #endif @@ -653,6 +696,53 @@ void emu_textOut16(int x, int y, const char *text) } } +#ifdef PSP +#define MAX_COMBO_KEY 23 +#else +#define MAX_COMBO_KEY 31 +#endif + +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 <= MAX_COMBO_KEY; u++) + if (currentConfig.KeyBinds[u] & (1 << act)) keyc++; + } + else + { + for (u = 0; u <= MAX_COMBO_KEY; u++) + if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1 + (currentConfig.KeyBinds[u] & (1 << act))) keyc++; + for (u = 0; u <= MAX_COMBO_KEY; 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 <= MAX_COMBO_KEY; 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) { @@ -713,11 +803,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 } @@ -795,8 +885,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) @@ -813,12 +904,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 @@ -878,3 +970,4 @@ int emu_SaveLoadGame(int load, int sram) return ret; } } +