debug bgm player, sound code refactoring
[libpicofe.git] / gp2x / emu.c
index 12d70e3..8e726e3 100644 (file)
@@ -24,9 +24,9 @@
 #include "../common/config.h"\r
 #include "cpuctrl.h"\r
 \r
-#include <Pico/PicoInt.h>\r
-#include <Pico/Patch.h>\r
-#include <Pico/sound/mix.h>\r
+#include <pico/pico_int.h>\r
+#include <pico/patch.h>\r
+#include <pico/sound/mix.h>\r
 #include <zlib/zlib.h>\r
 \r
 //#define PFRAMES\r
@@ -49,7 +49,6 @@ static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
 static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
 static int osd_fps_x;\r
 static int gp2x_old_gamma = 100;\r
-char noticeMsg[64];                    // notice msg to draw\r
 unsigned char *PicoDraw2FB = NULL;  // temporary buffer for alt renderer\r
 int reset_timing = 0;\r
 \r
@@ -66,7 +65,7 @@ void emu_noticeMsgUpdated(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
-void emu_getMainDir(char *dst, int len)\r
+int emu_getMainDir(char *dst, int len)\r
 {\r
        extern char **g_argv;\r
        int j;\r
@@ -77,6 +76,8 @@ void emu_getMainDir(char *dst, int len)
        dst[len] = 0;\r
        for (j = strlen(dst); j > 0; j--)\r
                if (dst[j] == '/') { dst[j+1] = 0; break; }\r
+\r
+       return j + 1;\r
 }\r
 \r
 void emu_Init(void)\r
@@ -167,16 +168,6 @@ void emu_prepareDefaultConfig(void)
        defaultConfig.turbo_rate = 15;\r
 }\r
 \r
-void emu_setDefaultConfig(void)\r
-{\r
-       memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));\r
-       PicoOpt = currentConfig.s_PicoOpt;\r
-       PsndRate = currentConfig.s_PsndRate;\r
-       PicoRegionOverride = currentConfig.s_PicoRegion;\r
-       PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
-       PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r
-}\r
-\r
 void osd_text(int x, int y, const char *text)\r
 {\r
        int len = strlen(text)*8;\r
@@ -292,7 +283,7 @@ static void blit(const char *fps, const char *notice)
                // 8bit accurate renderer\r
                if (Pico.m.dirtyPal)\r
                {\r
-                       int pallen = 0x40;\r
+                       int pallen = 0xc0;\r
                        Pico.m.dirtyPal = 0;\r
                        if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r
                        {\r
@@ -302,21 +293,14 @@ static void blit(const char *fps, const char *notice)
                                memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r
                                pallen = 0x100;\r
                        }\r
-                       else if (rendstatus & PDRAW_ACC_SPRITES) {\r
-                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-                               memcpy32(localPal+0x40, localPal, 0x40);\r
-                               memcpy32(localPal+0x80, localPal, 0x40);\r
-                               memcpy32(localPal+0xc0, localPal, 0x40);\r
-                               pallen = 0x100;\r
-                       }\r
                        else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
                                vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
                                vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
-                               pallen = 0xc0;\r
                        }\r
                        else {\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                               memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r
                        }\r
                        if (pallen > 0xc0) {\r
                                localPal[0xc0] = 0x0000c000;\r
@@ -681,6 +665,46 @@ static void updateSound(int len)
                gp2x_sound_write(PsndOut, len<<1);\r
 }\r
 \r
+void emu_startSound(void)\r
+{\r
+       static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
+       int target_fps = Pico.m.pal ? 50 : 60;\r
+\r
+       PsndOut = NULL;\r
+\r
+       // prepare sound stuff\r
+       if (currentConfig.EmuOpt & 4)\r
+       {\r
+               int snd_excess_add;\r
+               if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
+                               ((PicoOpt&0x200) && crashed_940)) {\r
+                       PsndRerate(Pico.m.frame_count ? 1 : 0);\r
+               }\r
+               snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
+               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
+                       PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
+               gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
+               gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
+               PicoWriteSound = updateSound;\r
+               update_volume(0, 0);\r
+               memset(sndBuffer, 0, sizeof(sndBuffer));\r
+               PsndOut = sndBuffer;\r
+               PsndRate_old = PsndRate;\r
+               PicoOpt_old  = PicoOpt;\r
+               pal_old = Pico.m.pal;\r
+       }\r
+}\r
+\r
+void emu_endSound(void)\r
+{\r
+}\r
+\r
+/* wait until we can write more sound */\r
+void emu_waitSound(void)\r
+{\r
+       // don't need to do anything, writes will block by themselves\r
+}\r
+\r
 \r
 static void SkipFrame(int do_audio)\r
 {\r
@@ -792,8 +816,7 @@ static void tga_dump(void)
 \r
 void emu_Loop(void)\r
 {\r
-       static int gp2x_old_clock = 200;\r
-       static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;\r
+       static int gp2x_old_clock = 200, EmuOpt_old = 0;\r
        char fpsbuff[24]; // fps count c string\r
        struct timeval tval; // timing\r
        int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
@@ -837,29 +860,7 @@ void emu_Loop(void)
        target_frametime = 1000000/target_fps;\r
        reset_timing = 1;\r
 \r
-       // prepare sound stuff\r
-       if (currentConfig.EmuOpt & 4)\r
-       {\r
-               int snd_excess_add;\r
-               if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
-                               ((PicoOpt&0x200) && crashed_940)) {\r
-                       PsndRerate(Pico.m.frame_count ? 1 : 0);\r
-               }\r
-               snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
-               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
-                       PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
-               gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
-               gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
-               PicoWriteSound = updateSound;\r
-               update_volume(0, 0);\r
-               memset(sndBuffer, 0, sizeof(sndBuffer));\r
-               PsndOut = sndBuffer;\r
-               PsndRate_old = PsndRate;\r
-               PicoOpt_old  = PicoOpt;\r
-               pal_old = Pico.m.pal;\r
-       } else {\r
-               PsndOut = NULL;\r
-       }\r
+       emu_startSound();\r
 \r
        // prepare CD buffer\r
        if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r