X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fcommon%2Femu.c;h=88d80b7e9c742bdeb00b826e0281114c7e8df70b;hb=0d83abe234136b0cbf3f2ae020760d9f57d84af6;hp=5bd34e879c6d065396767b3958153015cde83287;hpb=b24e0f6ce6e8a59ba43597520709ca79fa36ff8e;p=picodrive.git diff --git a/platform/common/emu.c b/platform/common/emu.c index 5bd34e8..88d80b7 100644 --- a/platform/common/emu.c +++ b/platform/common/emu.c @@ -1,4 +1,4 @@ -// (c) Copyright 2006-2007 notaz, All rights reserved. +// (c) Copyright 2006-2009 notaz, All rights reserved. // Free for non-commercial use. // For commercial use, separate licencing terms must be obtained. @@ -22,9 +22,10 @@ #include #include #include -#include +#define STATUS_MSG_TIMEOUT 2000 + void *g_screen_ptr; #if !SCREEN_SIZE_FIXED @@ -78,9 +79,9 @@ static int try_rfn_cut(char *fname) return 0; } -static void get_ext(char *file, char *ext) +static void get_ext(const char *file, char *ext) { - char *p; + const char *p; p = file + strlen(file) - 4; if (p < file) p = file; @@ -92,23 +93,29 @@ static void get_ext(char *file, char *ext) void emu_status_msg(const char *format, ...) { va_list vl; + int ret; va_start(vl, format); - vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl); + ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl); va_end(vl); + /* be sure old text gets overwritten */ + for (; ret < 28; ret++) + noticeMsg[ret] = ' '; + noticeMsg[ret] = 0; + notice_msg_time = plat_get_ticks_ms(); } -static const char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" }; -static const char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" }; -static const char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" }; +static const char * const biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" }; +static const char * const biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" }; +static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" }; static int find_bios(int region, char **bios_file) { static char bios_path[1024]; int i, count; - const char **files; + const char * const *files; FILE *f = NULL; if (region == 4) { // US @@ -169,12 +176,13 @@ static unsigned char id_header[0x100]; /* checks if fname points to valid MegaCD image * if so, checks for suitable BIOS */ -int emu_cd_check(int *pregion, char *fname_in) +static int emu_cd_check(int *pregion, const char *fname_in) { + const char *fname = fname_in; unsigned char buf[32]; pm_file *cd_f; int region = 4; // 1: Japan, 4: US, 8: Europe - char ext[5], *fname = fname_in; + char ext[5]; cue_track_type type = CT_UNKNOWN; cue_data_t *cue_data = NULL; @@ -398,8 +406,8 @@ int emu_reload_rom(char *rom_fname) // valid CD image, check for BIOS.. // we need to have config loaded at this point - ret = emu_read_config(1, 1); - if (!ret) emu_read_config(0, 1); + ret = emu_read_config(1, 0); + if (!ret) emu_read_config(0, 0); cfg_loaded = 1; if (PicoRegionOverride) { @@ -456,8 +464,8 @@ int emu_reload_rom(char *rom_fname) if (!(PicoAHW & PAHW_MCD)) memcpy(id_header, rom_data + 0x100, sizeof(id_header)); if (!cfg_loaded) { - ret = emu_read_config(1, 1); - if (!ret) emu_read_config(0, 1); + ret = emu_read_config(1, 0); + if (!ret) emu_read_config(0, 0); } lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size); @@ -507,13 +515,14 @@ int emu_reload_rom(char *rom_fname) emu_status_msg(Pico.m.pal ? "PAL SYSTEM / 50 FPS" : "NTSC SYSTEM / 60 FPS"); } + strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1); + rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0; + rom_loaded = 1; + // load SRAM for this ROM if (currentConfig.EmuOpt & EOPT_EN_SRAM) emu_save_load_game(1, 1); - strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1); - rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0; - rom_loaded = 1; return 1; fail2: @@ -523,6 +532,24 @@ fail: return 0; } +int emu_swap_cd(const char *fname) +{ + cd_img_type cd_type; + int ret = -1; + + cd_type = emu_cd_check(NULL, fname); + if (cd_type != CIT_NOT_CD) + ret = Insert_CD(fname, cd_type); + if (ret != 0) { + me_update_msg("Load failed, invalid CD image?"); + return 0; + } + + strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1); + rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0; + return 1; +} + static void romfname_ext(char *dst, const char *prefix, const char *ext) { char *p; @@ -586,10 +613,11 @@ int emu_read_config(int game, int no_defaults) char cfg[512]; int ret; + if (!no_defaults) + emu_set_defconfig(); + if (!game) { - if (!no_defaults) - emu_set_defconfig(); make_config_cfg(cfg); ret = config_readsect(cfg, NULL); } @@ -616,7 +644,8 @@ int emu_read_config(int game, int no_defaults) // 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); + emu_make_path(cfg, "game_def.cfg", sizeof(cfg)); + ret = config_readsect(cfg, sect); } if (ret == 0) @@ -737,18 +766,6 @@ void update_movie(void) } } - -static size_t gzRead2(void *p, size_t _size, size_t _n, void *file) -{ - return gzread(file, p, _n); -} - - -static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file) -{ - return gzwrite(file, p, _n); -} - static int try_ropen_file(const char *fname) { FILE *f; @@ -768,7 +785,8 @@ char *emu_get_save_fname(int load, int is_sram, int slot) if (is_sram) { - romfname_ext(saveFname, (PicoAHW&1) ? "brm"PATH_SEP : "srm"PATH_SEP, (PicoAHW&1) ? ".brm" : ".srm"); + romfname_ext(saveFname, (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, + (PicoAHW & PAHW_MCD) ? ".brm" : ".srm"); if (load) { if (try_ropen_file(saveFname)) return saveFname; // try in current dir.. @@ -812,23 +830,6 @@ int emu_check_save_file(int slot) return emu_get_save_fname(1, 0, slot) ? 1 : 0; } -void emu_setSaveStateCbs(int gz) -{ - if (gz) { - areaRead = gzRead2; - areaWrite = gzWrite2; - areaEof = (areaeof *) gzeof; - areaSeek = (areaseek *) gzseek; - areaClose = (areaclose *) gzclose; - } else { - areaRead = (arearw *) fread; - areaWrite = (arearw *) fwrite; - areaEof = (areaeof *) feof; - areaSeek = (areaseek *) fseek; - areaClose = (areaclose *) fclose; - } -} - int emu_save_load_game(int load, int sram) { int ret = 0; @@ -899,34 +900,13 @@ int emu_save_load_game(int load, int sram) } else { - void *PmovFile = NULL; - if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) - { - if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) { - emu_setSaveStateCbs(1); - if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY); - } - } - else - { - if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) { - emu_setSaveStateCbs(0); - } - } - if(PmovFile) { - ret = PmovState(load ? 6 : 5, PmovFile); - areaClose(PmovFile); - PmovFile = 0; - if (load) Pico.m.dirtyPal=1; + ret = PicoState(saveFname, !load); + if (!ret) { #ifndef NO_SYNC - else sync(); + if (!load) sync(); #endif - } - else ret = -1; - if (!ret) - emu_status_msg(load ? "GAME LOADED" : "GAME SAVED"); - else - { + emu_status_msg(load ? "STATE LOADED" : "STATE SAVED"); + } else { emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED"); ret = -1; } @@ -960,9 +940,14 @@ void emu_set_fastforward(int set_on) } } -static void emu_msg_tray_open(void) +static void emu_tray_open(void) +{ + engineState = PGS_TrayMenu; +} + +static void emu_tray_close(void) { - emu_status_msg("CD tray opened"); + emu_status_msg("CD tray closed."); } void emu_reset_game(void) @@ -1086,7 +1071,7 @@ static void run_events_ui(unsigned int which) in_set_blocking(0); } if (do_it) { - plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING GAME" : "SAVING GAME"); + plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE"); PicoStateProgressCB = plat_status_msg_busy_next; emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0); PicoStateProgressCB = NULL; @@ -1184,8 +1169,8 @@ void emu_init(void) PicoInit(); PicoMessage = plat_status_msg_busy_next; - PicoMCDopenTray = emu_msg_tray_open; - PicoMCDcloseTray = menu_loop_tray; + PicoMCDopenTray = emu_tray_open; + PicoMCDcloseTray = emu_tray_close; } void emu_finish(void) @@ -1248,9 +1233,6 @@ void emu_loop(void) if (PicoAHW & PAHW_MCD) PicoCDBufferInit(); - if (currentConfig.EmuOpt & EOPT_PSYNC) - plat_video_wait_vsync(); - pemu_loop_prep(); timestamp_fps = get_ticks(); @@ -1258,6 +1240,8 @@ void emu_loop(void) frames_done = frames_shown = pframes_done = 0; + plat_video_wait_vsync(); + /* loop with resync every 1 sec. */ while (engineState == PGS_Running) { @@ -1276,7 +1260,7 @@ void emu_loop(void) if (notice_msg_time != 0) { static int noticeMsgSum; - if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(2000)) { + if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(STATUS_MSG_TIMEOUT)) { notice_msg_time = 0; plat_status_msg_clear(); notice_msg = NULL; @@ -1325,10 +1309,18 @@ void emu_loop(void) if (timestamp - timestamp_base >= ms_to_ticks(1000)) { - if (PsndOut == 0 && currentConfig.Frameskip >= 0) + if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && currentConfig.Frameskip >= 0) pframes_done = 0; - else + else { pframes_done -= target_fps; + /* don't allow it to drift during heavy slowdowns */ + if (pframes_done < -5) { + reset_timing = 1; + continue; + } + if (pframes_done < -2) + pframes_done = -2; + } timestamp_base += ms_to_ticks(1000); } @@ -1343,10 +1335,10 @@ void emu_loop(void) pframes_done++; frames_done++; diff_lim += target_frametime; - if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled + if (!(currentConfig.EmuOpt & EOPT_NO_FRMLIMIT)) { timestamp = get_ticks(); diff = timestamp - timestamp_base; - if (diff < diff_lim) // we are too fast + if (!reset_timing && diff < diff_lim) // we are too fast plat_wait_till_us(timestamp_base + diff_lim); } } @@ -1354,7 +1346,7 @@ void emu_loop(void) else if (diff > diff_lim) { /* no time left for this frame - skip */ - if (diff - diff_lim >= ms_to_ticks(300)) { + if (diff - diff_lim >= ms_to_ticks(200)) { /* if too much behind, reset instead */ reset_timing = 1; continue; @@ -1369,7 +1361,7 @@ void emu_loop(void) PicoFrame(); /* frame limiter */ - if (!reset_timing && (PsndOut != NULL || currentConfig.Frameskip < 0)) + if (!reset_timing && !(currentConfig.EmuOpt & EOPT_NO_FRMLIMIT)) { timestamp = get_ticks(); diff = timestamp - timestamp_base; @@ -1378,12 +1370,9 @@ void emu_loop(void) if (diff < diff_lim) { // we are too fast - if (currentConfig.EmuOpt & EOPT_PSYNC) { - if (diff_lim - diff > target_frametime/2) - plat_wait_till_us(timestamp_base + target_frametime/4); + plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4); + if (currentConfig.EmuOpt & EOPT_VSYNC) plat_video_wait_vsync(); - } else - plat_wait_till_us(timestamp_base + diff_lim); } } @@ -1394,9 +1383,6 @@ void emu_loop(void) emu_set_fastforward(0); - if (PicoAHW & PAHW_MCD) - PicoCDBufferFree(); - // save SRAM if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) { plat_status_msg_busy_first("Writing SRAM/BRAM..."); @@ -1404,9 +1390,11 @@ void emu_loop(void) SRam.changed = 0; } - // do menu background to be sure it's right - pemu_forced_frame(POPT_EN_SOFTSCALE); - pemu_loop_end(); + + // pemu_loop_end() might want to do 1 frame for bg image, + // so free CD buffer here + if (PicoAHW & PAHW_MCD) + PicoCDBufferFree(); }