add configure, revive pnd build, unify/refactor things
[picodrive.git] / platform / common / emu.c
index 76765a4..8eda13a 100644 (file)
 #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
@@ -45,6 +46,8 @@ 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
 const char *rom_fname_reload;\r
@@ -1418,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
@@ -1440,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
@@ -1476,7 +1540,8 @@ void emu_loop(void)
                PicoCDBufferInit();\r
 \r
        plat_video_loop_prepare();\r
-       pemu_loop_prep();\r
+       emu_loop_prep();\r
+       pemu_sound_start();\r
 \r
        /* number of ticks per frame */\r
        if (Pico.m.pal) {\r
@@ -1641,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