X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=platform%2Fcommon%2Femu.c;h=36539c65b07faf3a24b27eb753fa1526975b1a72;hb=7e7b446f29e6b571894cd8254240fb2ac266034d;hp=8066796b4ba7cc2e13204474a3fe775dafffa10b;hpb=cff531af94bd9c9c89ae162e80f48ddc26a4e504;p=picodrive.git diff --git a/platform/common/emu.c b/platform/common/emu.c index 8066796..36539c6 100644 --- a/platform/common/emu.c +++ b/platform/common/emu.c @@ -16,12 +16,13 @@ #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 "input_pico.h" #include "menu_pico.h" -#include "config.h" +#include "config_file.h" #include #include @@ -45,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; @@ -147,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; @@ -484,21 +486,28 @@ 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_in); - lprintf("emu_ReloadRom(%s)\n", rom_fname); + rom_fname = strdup(rom_fname_in); + if (rom_fname == NULL) + return 0; + used_rom_name = rom_fname; get_ext(rom_fname, ext); // early cleanup @@ -515,7 +524,7 @@ int emu_reload_rom(char *rom_fname) FILE *movie_file = fopen(rom_fname, "rb"); if (!movie_file) { menu_update_msg("Failed to open movie."); - return 0; + goto out; } fseek(movie_file, 0, SEEK_END); movie_size = ftell(movie_file); @@ -523,24 +532,24 @@ int emu_reload_rom(char *rom_fname) if (movie_size < 64+3) { menu_update_msg("Invalid GMV file."); fclose(movie_file); - return 0; + goto out; } movie_data = malloc(movie_size); if (movie_data == NULL) { 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) { menu_update_msg("Invalid GMV file."); - return 0; + goto out; } dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname); if (!dummy) { menu_update_msg("Could't find a ROM for movie."); - return 0; + goto out; } get_ext(rom_fname, ext); lprintf("gmv loaded for %s\n", rom_fname); @@ -552,7 +561,7 @@ int emu_reload_rom(char *rom_fname) dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname); if (!dummy) { menu_update_msg("Could't find a ROM to patch."); - return 0; + goto out; } get_ext(rom_fname, ext); } @@ -560,7 +569,7 @@ int emu_reload_rom(char *rom_fname) media_type = detect_media(rom_fname); if (media_type == PM_BAD) { menu_update_msg("Not a ROM/CD img selected."); - return 0; + goto out; } shutdown_MCD(); @@ -588,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 { menu_update_msg("Invalid CD image"); - return 0; + goto out; } } else if (media_type == PM_MARK3) { @@ -606,10 +615,11 @@ int emu_reload_rom(char *rom_fname) rom = pm_open(used_rom_name); if (rom == NULL) { menu_update_msg("Failed to open ROM"); - return 0; + 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); @@ -618,7 +628,7 @@ int emu_reload_rom(char *rom_fname) 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 fail; + goto out; } // detect wrong files @@ -634,7 +644,7 @@ int emu_reload_rom(char *rom_fname) if (bad_rom) { menu_update_msg("Bad ROM detected."); - goto fail; + goto out; } // load config for this ROM (do this before insert to get correct region) @@ -648,7 +658,7 @@ 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)) { menu_update_msg("Failed to load ROM."); - goto fail; + goto out; } // insert CD if it was detected @@ -658,11 +668,12 @@ int emu_reload_rom(char *rom_fname) PicoCartUnload(); rom_data = NULL; // freed by unload menu_update_msg("Insert_CD() failed, invalid CD image?"); - goto fail; + goto out; } } menu_romload_end(); + menu_romload_started = 0; if (PicoPatches) { PicoPatchPrepare(); @@ -702,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) @@ -1409,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) @@ -1431,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) @@ -1466,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) { @@ -1631,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