#include "../libpicofe/posix.h"\r
#include "../libpicofe/input.h"\r
#include "../libpicofe/fonts.h"\r
+#include "../libpicofe/sndout.h"\r
#include "../libpicofe/lprintf.h"\r
#include "../libpicofe/plat.h"\r
#include "emu.h"\r
#include "input_pico.h"\r
#include "menu_pico.h"\r
-#include "config.h"\r
+#include "config_file.h"\r
\r
#include <pico/pico_int.h>\r
#include <pico/patch.h>\r
int pico_inp_mode = 0;\r
int engineState = PGS_Menu;\r
\r
+static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
+\r
/* tmp buff to reduce stack usage for plats with small stack */\r
static char static_buff[512];\r
-/* TODO: len checking */\r
-char rom_fname_reload[512];\r
+const char *rom_fname_reload;\r
char rom_fname_loaded[512];\r
int rom_loaded = 0;\r
int reset_timing = 0;\r
static const char * const biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };\r
static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r
\r
-static int find_bios(int region, char **bios_file)\r
+static int find_bios(int region, const char **bios_file)\r
{\r
int i, count;\r
const char * const *files;\r
emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);\r
}\r
\r
-// note: this function might mangle rom_fname\r
// XXX: portions of this code should move to pico/\r
-int emu_reload_rom(char *rom_fname)\r
+int emu_reload_rom(const char *rom_fname_in)\r
{\r
unsigned int rom_size = 0;\r
- char *used_rom_name = rom_fname;\r
+ const char *used_rom_name = NULL;\r
+ char *rom_fname = NULL;\r
unsigned char *rom_data = NULL;\r
char ext[5];\r
pm_file *rom = NULL;\r
int cd_state = CIT_NOT_CD;\r
int ret, media_type, cd_region;\r
int cfg_loaded = 0, bad_rom = 0;\r
+ int menu_romload_started = 0;\r
+ int retval = 0;\r
+\r
+ lprintf("emu_ReloadRom(%s)\n", rom_fname_in);\r
\r
- lprintf("emu_ReloadRom(%s)\n", rom_fname);\r
+ rom_fname = strdup(rom_fname_in);\r
+ if (rom_fname == NULL)\r
+ return 0;\r
\r
+ used_rom_name = rom_fname;\r
get_ext(rom_fname, ext);\r
\r
// early cleanup\r
FILE *movie_file = fopen(rom_fname, "rb");\r
if (!movie_file) {\r
menu_update_msg("Failed to open movie.");\r
- return 0;\r
+ goto out;\r
}\r
fseek(movie_file, 0, SEEK_END);\r
movie_size = ftell(movie_file);\r
if (movie_size < 64+3) {\r
menu_update_msg("Invalid GMV file.");\r
fclose(movie_file);\r
- return 0;\r
+ goto out;\r
}\r
movie_data = malloc(movie_size);\r
if (movie_data == NULL) {\r
menu_update_msg("low memory.");\r
fclose(movie_file);\r
- return 0;\r
+ goto out;\r
}\r
dummy = fread(movie_data, 1, movie_size, movie_file);\r
fclose(movie_file);\r
if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r
menu_update_msg("Invalid GMV file.");\r
- return 0;\r
+ goto out;\r
}\r
dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r
if (!dummy) {\r
menu_update_msg("Could't find a ROM for movie.");\r
- return 0;\r
+ goto out;\r
}\r
get_ext(rom_fname, ext);\r
lprintf("gmv loaded for %s\n", rom_fname);\r
dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r
if (!dummy) {\r
menu_update_msg("Could't find a ROM to patch.");\r
- return 0;\r
+ goto out;\r
}\r
get_ext(rom_fname, ext);\r
}\r
media_type = detect_media(rom_fname);\r
if (media_type == PM_BAD) {\r
menu_update_msg("Not a ROM/CD img selected.");\r
- return 0;\r
+ goto out;\r
}\r
\r
shutdown_MCD();\r
(cd_region == 8 ? "EU" : "JAP") : "USA");\r
}\r
if (!find_bios(cd_region, &used_rom_name))\r
- return 0;\r
+ goto out;\r
\r
get_ext(used_rom_name, ext);\r
PicoAHW |= PAHW_MCD;\r
}\r
else {\r
menu_update_msg("Invalid CD image");\r
- return 0;\r
+ goto out;\r
}\r
}\r
else if (media_type == PM_MARK3) {\r
rom = pm_open(used_rom_name);\r
if (rom == NULL) {\r
menu_update_msg("Failed to open ROM");\r
- return 0;\r
+ goto out;\r
}\r
\r
menu_romload_prepare(used_rom_name); // also CD load\r
+ menu_romload_started = 1;\r
used_rom_name = NULL; // uses static_buff\r
\r
ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);\r
if (ret == 2) menu_update_msg("Out of memory");\r
else if (ret == 3) menu_update_msg("Read failed");\r
else menu_update_msg("PicoCartLoad() failed.");\r
- goto fail;\r
+ goto out;\r
}\r
\r
// detect wrong files\r
\r
if (bad_rom) {\r
menu_update_msg("Bad ROM detected.");\r
- goto fail;\r
+ goto out;\r
}\r
\r
// load config for this ROM (do this before insert to get correct region)\r
emu_make_path(static_buff, "carthw.cfg", sizeof(static_buff));\r
if (PicoCartInsert(rom_data, rom_size, static_buff)) {\r
menu_update_msg("Failed to load ROM.");\r
- goto fail;\r
+ goto out;\r
}\r
\r
// insert CD if it was detected\r
PicoCartUnload();\r
rom_data = NULL; // freed by unload\r
menu_update_msg("Insert_CD() failed, invalid CD image?");\r
- goto fail;\r
+ goto out;\r
}\r
}\r
\r
menu_romload_end();\r
+ menu_romload_started = 0;\r
\r
if (PicoPatches) {\r
PicoPatchPrepare();\r
if (currentConfig.EmuOpt & EOPT_EN_SRAM)\r
emu_save_load_game(1, 1);\r
\r
- return 1;\r
-\r
-fail:\r
- if (rom_data)\r
+ retval = 1;\r
+out:\r
+ if (retval == 0 && rom_data)\r
free(rom_data);\r
- menu_romload_end();\r
- return 0;\r
+ if (menu_romload_started)\r
+ menu_romload_end();\r
+ free(rom_fname);\r
+ return retval;\r
}\r
\r
int emu_swap_cd(const char *fname)\r
PicoMessage = plat_status_msg_busy_next;\r
PicoMCDopenTray = emu_tray_open;\r
PicoMCDcloseTray = emu_tray_close;\r
+\r
+ sndout_init();\r
}\r
\r
void emu_finish(void)\r
pprof_finish();\r
\r
PicoExit();\r
+ sndout_exit();\r
+}\r
+\r
+static void snd_write_nonblocking(int len)\r
+{\r
+ sndout_write_nb(PsndOut, len);\r
+}\r
+\r
+void emu_sound_start(void)\r
+{\r
+ PsndOut = NULL;\r
+\r
+ if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
+ {\r
+ int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
+\r
+ PsndRerate(Pico.m.frame_count ? 1 : 0);\r
+\r
+ printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
+ PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
+ sndout_start(PsndRate, is_stereo);\r
+ PicoWriteSound = snd_write_nonblocking;\r
+ plat_update_volume(0, 0);\r
+ memset(sndBuffer, 0, sizeof(sndBuffer));\r
+ PsndOut = sndBuffer;\r
+ }\r
+}\r
+\r
+void emu_sound_stop(void)\r
+{\r
+ sndout_stop();\r
+}\r
+\r
+void emu_sound_wait(void)\r
+{\r
+ sndout_wait();\r
}\r
\r
static void skip_frame(int do_audio)\r
if (PicoAHW & PAHW_MCD)\r
PicoCDBufferInit();\r
\r
+ plat_video_loop_prepare();\r
pemu_loop_prep();\r
+ pemu_sound_start();\r
\r
/* number of ticks per frame */\r
if (Pico.m.pal) {\r
}\r
\r
pemu_loop_end();\r
+ emu_sound_stop();\r
\r
// pemu_loop_end() might want to do 1 frame for bg image,\r
// so free CD buffer here\r