X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fcommon%2Femu.c;h=36539c65b07faf3a24b27eb753fa1526975b1a72;hb=7e7b446f29e6b571894cd8254240fb2ac266034d;hp=822d7aab9cb598628517fd8f0147b2648d6f0d97;hpb=a4edca53b489370b0814a74579acbcc183578355;p=picodrive.git diff --git a/platform/common/emu.c b/platform/common/emu.c index 822d7aa..36539c6 100644 --- a/platform/common/emu.c +++ b/platform/common/emu.c @@ -1,7 +1,10 @@ -// (c) Copyright 2006-2009 notaz, All rights reserved. -// Free for non-commercial use. - -// For commercial use, separate licencing terms must be obtained. +/* + * PicoDrive + * (C) notaz, 2007-2010 + * + * This work is licensed under the terms of MAME license. + * See COPYING file in the top-level directory. + */ #include #include @@ -10,14 +13,16 @@ #include #endif +#include "../libpicofe/posix.h" +#include "../libpicofe/input.h" +#include "../libpicofe/fonts.h" +#include "../libpicofe/sndout.h" +#include "../libpicofe/lprintf.h" +#include "../libpicofe/plat.h" #include "emu.h" -#include "menu.h" -#include "fonts.h" -#include "lprintf.h" -#include "config.h" -#include "plat.h" -#include "input.h" -#include "posix.h" +#include "input_pico.h" +#include "menu_pico.h" +#include "config_file.h" #include #include @@ -41,10 +46,11 @@ int pico_pen_x = 320/2, pico_pen_y = 240/2; int pico_inp_mode = 0; int engineState = PGS_Menu; +static short __attribute__((aligned(4))) sndBuffer[2*44100/50]; + /* tmp buff to reduce stack usage for plats with small stack */ static char static_buff[512]; -/* TODO: len checking */ -char rom_fname_reload[512]; +const char *rom_fname_reload; char rom_fname_loaded[512]; int rom_loaded = 0; int reset_timing = 0; @@ -143,7 +149,7 @@ static const char * const biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "Se 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 int find_bios(int region, const char **bios_file) { int i, count; const char * const *files; @@ -184,7 +190,7 @@ static int find_bios(int region, char **bios_file) } else { sprintf(static_buff, "no %s BIOS files found, read docs", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA"); - me_update_msg(static_buff); + menu_update_msg(static_buff); return 0; } } @@ -480,61 +486,70 @@ static void system_announce(void) emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra); } -// note: this function might mangle rom_fname // XXX: portions of this code should move to pico/ -int emu_reload_rom(char *rom_fname) +int emu_reload_rom(const char *rom_fname_in) { unsigned int rom_size = 0; - char *used_rom_name = rom_fname; + const char *used_rom_name = NULL; + char *rom_fname = NULL; unsigned char *rom_data = NULL; char ext[5]; pm_file *rom = NULL; int cd_state = CIT_NOT_CD; int ret, media_type, cd_region; int cfg_loaded = 0, bad_rom = 0; + int menu_romload_started = 0; + int retval = 0; - lprintf("emu_ReloadRom(%s)\n", rom_fname); + lprintf("emu_ReloadRom(%s)\n", rom_fname_in); + rom_fname = strdup(rom_fname_in); + if (rom_fname == NULL) + return 0; + + used_rom_name = rom_fname; get_ext(rom_fname, ext); - // check for movie file + // early cleanup + PicoPatchUnload(); if (movie_data) { free(movie_data); movie_data = 0; } + if (!strcmp(ext, ".gmv")) { // check for both gmv and rom int dummy; FILE *movie_file = fopen(rom_fname, "rb"); if (!movie_file) { - me_update_msg("Failed to open movie."); - return 0; + menu_update_msg("Failed to open movie."); + goto out; } fseek(movie_file, 0, SEEK_END); movie_size = ftell(movie_file); fseek(movie_file, 0, SEEK_SET); if (movie_size < 64+3) { - me_update_msg("Invalid GMV file."); + menu_update_msg("Invalid GMV file."); fclose(movie_file); - return 0; + goto out; } movie_data = malloc(movie_size); if (movie_data == NULL) { - me_update_msg("low memory."); + menu_update_msg("low memory."); fclose(movie_file); - return 0; + goto out; } dummy = fread(movie_data, 1, movie_size, movie_file); fclose(movie_file); if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) { - me_update_msg("Invalid GMV file."); - return 0; + menu_update_msg("Invalid GMV file."); + goto out; } dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname); if (!dummy) { - me_update_msg("Could't find a ROM for movie."); - return 0; + menu_update_msg("Could't find a ROM for movie."); + goto out; } get_ext(rom_fname, ext); lprintf("gmv loaded for %s\n", rom_fname); @@ -545,20 +560,19 @@ int emu_reload_rom(char *rom_fname) PicoPatchLoad(rom_fname); dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname); if (!dummy) { - me_update_msg("Could't find a ROM to patch."); - return 0; + menu_update_msg("Could't find a ROM to patch."); + goto out; } get_ext(rom_fname, ext); } media_type = detect_media(rom_fname); if (media_type == PM_BAD) { - me_update_msg("Not a ROM/CD img selected."); - return 0; + menu_update_msg("Not a ROM/CD img selected."); + goto out; } shutdown_MCD(); - PicoPatchUnload(); PicoCartUnload(); rom_loaded = 0; @@ -583,14 +597,14 @@ int emu_reload_rom(char *rom_fname) (cd_region == 8 ? "EU" : "JAP") : "USA"); } if (!find_bios(cd_region, &used_rom_name)) - return 0; + goto out; get_ext(used_rom_name, ext); PicoAHW |= PAHW_MCD; } else { - me_update_msg("Invalid CD image"); - return 0; + menu_update_msg("Invalid CD image"); + goto out; } } else if (media_type == PM_MARK3) { @@ -600,20 +614,21 @@ int emu_reload_rom(char *rom_fname) rom = pm_open(used_rom_name); if (rom == NULL) { - me_update_msg("Failed to open ROM"); - return 0; + menu_update_msg("Failed to open ROM"); + goto out; } menu_romload_prepare(used_rom_name); // also CD load + menu_romload_started = 1; used_rom_name = NULL; // uses static_buff ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0); pm_close(rom); if (ret != 0) { - if (ret == 2) me_update_msg("Out of memory"); - else if (ret == 3) me_update_msg("Read failed"); - else me_update_msg("PicoCartLoad() failed."); - goto fail; + if (ret == 2) menu_update_msg("Out of memory"); + else if (ret == 3) menu_update_msg("Read failed"); + else menu_update_msg("PicoCartLoad() failed."); + goto out; } // detect wrong files @@ -628,8 +643,8 @@ int emu_reload_rom(char *rom_fname) } if (bad_rom) { - me_update_msg("Bad ROM detected."); - goto fail; + menu_update_msg("Bad ROM detected."); + goto out; } // load config for this ROM (do this before insert to get correct region) @@ -642,8 +657,8 @@ int emu_reload_rom(char *rom_fname) emu_make_path(static_buff, "carthw.cfg", sizeof(static_buff)); if (PicoCartInsert(rom_data, rom_size, static_buff)) { - me_update_msg("Failed to load ROM."); - goto fail; + menu_update_msg("Failed to load ROM."); + goto out; } // insert CD if it was detected @@ -652,12 +667,13 @@ int emu_reload_rom(char *rom_fname) if (ret != 0) { PicoCartUnload(); rom_data = NULL; // freed by unload - me_update_msg("Insert_CD() failed, invalid CD image?"); - goto fail; + menu_update_msg("Insert_CD() failed, invalid CD image?"); + goto out; } } menu_romload_end(); + menu_romload_started = 0; if (PicoPatches) { PicoPatchPrepare(); @@ -697,13 +713,14 @@ int emu_reload_rom(char *rom_fname) if (currentConfig.EmuOpt & EOPT_EN_SRAM) emu_save_load_game(1, 1); - return 1; - -fail: - if (rom_data) + retval = 1; +out: + if (retval == 0 && rom_data) free(rom_data); - menu_romload_end(); - return 0; + if (menu_romload_started) + menu_romload_end(); + free(rom_fname); + return retval; } int emu_swap_cd(const char *fname) @@ -715,7 +732,7 @@ int emu_swap_cd(const char *fname) if (cd_type != CIT_NOT_CD) ret = Insert_CD(fname, cd_type); if (ret != 0) { - me_update_msg("Load failed, invalid CD image?"); + menu_update_msg("Load failed, invalid CD image?"); return 0; } @@ -1011,7 +1028,7 @@ char *emu_get_save_fname(int load, int is_sram, int slot) return NULL; } -int emu_check_save_file(int slot) +int emu_check_save_file(int slot, int *time) { return emu_get_save_fname(1, 0, slot) ? 1 : 0; } @@ -1238,7 +1255,7 @@ static void run_events_ui(unsigned int which) if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE)) { int do_it = 1; - if ( emu_check_save_file(state_slot) && + if ( emu_check_save_file(state_slot, NULL) && (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) || ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) ) { @@ -1257,13 +1274,13 @@ static void run_events_ui(unsigned int which) plat_status_msg_busy_first(tmp); in_set_config_int(0, IN_CFG_BLOCKING, 1); - while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK)) + while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK)) ; - while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) ) + while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MA3|PBTN_MBACK)) ) ; if (keys & PBTN_MBACK) do_it = 0; - while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK)) + while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK)) ; in_set_config_int(0, IN_CFG_BLOCKING, 0); } @@ -1291,7 +1308,7 @@ static void run_events_ui(unsigned int which) } emu_status_msg("SAVE SLOT %i [%s]", state_slot, - emu_check_save_file(state_slot) ? "USED" : "FREE"); + emu_check_save_file(state_slot, NULL) ? "USED" : "FREE"); } if (which & PEV_MENU) engineState = PGS_Menu; @@ -1356,6 +1373,7 @@ void emu_cmn_forced_frame(int no_scale, int do_emu) memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4); + PicoOpt &= ~POPT_ALT_RENDERER; PicoOpt |= POPT_ACC_SPRITES; if (!no_scale) PicoOpt |= POPT_EN_SOFTSCALE; @@ -1403,6 +1421,8 @@ void emu_init(void) PicoMessage = plat_status_msg_busy_next; PicoMCDopenTray = emu_tray_open; PicoMCDcloseTray = emu_tray_close; + + sndout_init(); } void emu_finish(void) @@ -1425,6 +1445,42 @@ void emu_finish(void) pprof_finish(); PicoExit(); + sndout_exit(); +} + +static void snd_write_nonblocking(int len) +{ + sndout_write_nb(PsndOut, len); +} + +void emu_sound_start(void) +{ + PsndOut = NULL; + + if (currentConfig.EmuOpt & EOPT_EN_SOUND) + { + int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0; + + PsndRerate(Pico.m.frame_count ? 1 : 0); + + printf("starting audio: %i len: %i stereo: %i, pal: %i\n", + PsndRate, PsndLen, is_stereo, Pico.m.pal); + sndout_start(PsndRate, is_stereo); + PicoWriteSound = snd_write_nonblocking; + plat_update_volume(0, 0); + memset(sndBuffer, 0, sizeof(sndBuffer)); + PsndOut = sndBuffer; + } +} + +void emu_sound_stop(void) +{ + sndout_stop(); +} + +void emu_sound_wait(void) +{ + sndout_wait(); } static void skip_frame(int do_audio) @@ -1460,7 +1516,9 @@ void emu_loop(void) if (PicoAHW & PAHW_MCD) PicoCDBufferInit(); + plat_video_loop_prepare(); pemu_loop_prep(); + pemu_sound_start(); /* number of ticks per frame */ if (Pico.m.pal) { @@ -1625,6 +1683,7 @@ void emu_loop(void) } pemu_loop_end(); + emu_sound_stop(); // pemu_loop_end() might want to do 1 frame for bg image, // so free CD buffer here