add configure, revive pnd build, unify/refactor things
[picodrive.git] / platform / common / emu.c
index a417926..8eda13a 100644 (file)
@@ -1,7 +1,10 @@
-// (c) Copyright 2006-2009 notaz, All rights reserved.\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
+/*\r
+ * PicoDrive\r
+ * (C) notaz, 2007-2010\r
+ *\r
+ * This work is licensed under the terms of MAME license.\r
+ * See COPYING file in the top-level directory.\r
+ */\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #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
@@ -42,10 +46,11 @@ int pico_pen_x = 320/2, pico_pen_y = 240/2;
 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
@@ -144,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"   };\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
@@ -481,21 +486,28 @@ static void system_announce(void)
        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);\r
+       lprintf("emu_ReloadRom(%s)\n", rom_fname_in);\r
+\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
@@ -512,7 +524,7 @@ int emu_reload_rom(char *rom_fname)
                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
@@ -520,24 +532,24 @@ int emu_reload_rom(char *rom_fname)
                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
@@ -549,7 +561,7 @@ int emu_reload_rom(char *rom_fname)
                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
@@ -557,7 +569,7 @@ int emu_reload_rom(char *rom_fname)
        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
@@ -585,14 +597,14 @@ int emu_reload_rom(char *rom_fname)
                                        (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
@@ -603,10 +615,11 @@ int emu_reload_rom(char *rom_fname)
        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
@@ -615,7 +628,7 @@ int emu_reload_rom(char *rom_fname)
                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
@@ -631,7 +644,7 @@ int emu_reload_rom(char *rom_fname)
 \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
@@ -645,7 +658,7 @@ int emu_reload_rom(char *rom_fname)
        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
@@ -655,11 +668,12 @@ int emu_reload_rom(char *rom_fname)
                        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
@@ -699,13 +713,14 @@ int emu_reload_rom(char *rom_fname)
        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
@@ -1406,6 +1421,8 @@ void emu_init(void)
        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
@@ -1428,6 +1445,65 @@ void emu_finish(void)
        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 emu_loop_prep(void)\r
+{\r
+       static int pal_old = -1;\r
+       static int filter_old = -1;\r
+\r
+       if (currentConfig.CPUclock != plat_target_cpu_clock_get())\r
+               plat_target_cpu_clock_set(currentConfig.CPUclock);\r
+\r
+       if (Pico.m.pal != pal_old) {\r
+               plat_target_lcdrate_set(Pico.m.pal);\r
+               pal_old = Pico.m.pal;\r
+       }\r
+\r
+       if (currentConfig.filter != filter_old) {\r
+               plat_target_hwfilter_set(currentConfig.filter);\r
+               filter_old = currentConfig.filter;\r
+       }\r
+\r
+       plat_target_gamma_set(currentConfig.gamma, 0);\r
+\r
+       pemu_loop_prep();\r
 }\r
 \r
 static void skip_frame(int do_audio)\r
@@ -1463,7 +1539,9 @@ void emu_loop(void)
        if (PicoAHW & PAHW_MCD)\r
                PicoCDBufferInit();\r
 \r
-       pemu_loop_prep();\r
+       plat_video_loop_prepare();\r
+       emu_loop_prep();\r
+       pemu_sound_start();\r
 \r
        /* number of ticks per frame */\r
        if (Pico.m.pal) {\r
@@ -1628,6 +1706,7 @@ void emu_loop(void)
        }\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