debug bgm player, sound code refactoring
[libpicofe.git] / gp2x / emu.c
index 701bcd8..8e726e3 100644 (file)
 #include "../common/arm_utils.h"\r
 #include "../common/fonts.h"\r
 #include "../common/emu.h"\r
+#include "../common/config.h"\r
 #include "cpuctrl.h"\r
 \r
-#include <Pico/PicoInt.h>\r
-#include <Pico/Patch.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
@@ -43,15 +45,17 @@ char romFileName[PATH_MAX];
 \r
 extern int crashed_940;\r
 \r
-static short sndBuffer[2*44100/50];\r
+static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
 static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
 static int osd_fps_x;\r
-static int combo_keys = 0, combo_acts = 0;     // keys and actions which need button combos\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
+#define PICO_PEN_ADJUST_X 4\r
+#define PICO_PEN_ADJUST_Y 2\r
+static int pico_pen_x = 320/2, pico_pen_y = 240/2;\r
+\r
 static void emu_msg_cb(const char *msg);\r
 static void emu_msg_tray_open(void);\r
 \r
@@ -61,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
@@ -72,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
@@ -96,48 +102,6 @@ void emu_Init(void)
 }\r
 \r
 \r
-static void find_combos(void)\r
-{\r
-       int act, u;\r
-\r
-       // find out which keys and actions are combos\r
-       combo_keys = combo_acts = 0;\r
-       for (act = 0; act < 32; act++)\r
-       {\r
-               int keyc = 0, keyc2 = 0;\r
-               if (act == 16 || act == 17) continue; // player2 flag\r
-               if (act > 17)\r
-               {\r
-                       for (u = 0; u < 32; u++)\r
-                               if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r
-               }\r
-               else\r
-               {\r
-                       for (u = 0; u < 32; u++)\r
-                               if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1\r
-                                       (currentConfig.KeyBinds[u] & (1 << act))) keyc++;\r
-                       for (u = 0; u < 32; u++)\r
-                               if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2\r
-                                       (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;\r
-                       if (keyc2 > keyc) keyc = keyc2;\r
-               }\r
-               if (keyc > 1)\r
-               {\r
-                       // loop again and mark those keys and actions as combo\r
-                       for (u = 0; u < 32; u++)\r
-                       {\r
-                               if (currentConfig.KeyBinds[u] & (1 << act)) {\r
-                                       combo_keys |= 1 << u;\r
-                                       combo_acts |= 1 << act;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);\r
-}\r
-\r
-\r
 static void scaling_update(void)\r
 {\r
        PicoOpt &= ~0x4100;\r
@@ -159,20 +123,10 @@ void emu_Deinit(void)
        }\r
 \r
        if (!(currentConfig.EmuOpt & 0x20)) {\r
-               FILE *f = fopen(PicoConfigFile, "r+b");\r
-               if (!f) emu_WriteConfig(0);\r
-               else {\r
-                       // if we already have config, reload it, except last ROM\r
-                       fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);\r
-                       fread(&currentConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);\r
-                       fseek(f, 0, SEEK_SET);\r
-                       fwrite(&currentConfig, 1, sizeof(currentConfig), f);\r
-                       fflush(f);\r
-                       fclose(f);\r
+               config_writelrom(PicoConfigFile);\r
 #ifndef NO_SYNC\r
-                       sync();\r
+               sync();\r
 #endif\r
-               }\r
        }\r
 \r
        free(PicoDraw2FB);\r
@@ -184,34 +138,34 @@ void emu_Deinit(void)
                set_gamma(100, 0);\r
 }\r
 \r
-void emu_setDefaultConfig(void)\r
-{\r
-       memset(&currentConfig, 0, sizeof(currentConfig));\r
-       currentConfig.lastRomFile[0] = 0;\r
-       currentConfig.EmuOpt  = 0x1f | 0x600; // | confirm_save, cd_leds\r
-       currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda\r
-       currentConfig.PsndRate = 22050; // 44100;\r
-       currentConfig.PicoRegion = 0; // auto\r
-       currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r
-       currentConfig.Frameskip = -1; // auto\r
-       currentConfig.CPUclock = 200;\r
-       currentConfig.volume = 50;\r
-       currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
-       currentConfig.KeyBinds[ 4] = 1<<1;\r
-       currentConfig.KeyBinds[ 2] = 1<<2;\r
-       currentConfig.KeyBinds[ 6] = 1<<3;\r
-       currentConfig.KeyBinds[14] = 1<<4;\r
-       currentConfig.KeyBinds[13] = 1<<5;\r
-       currentConfig.KeyBinds[12] = 1<<6;\r
-       currentConfig.KeyBinds[ 8] = 1<<7;\r
-       currentConfig.KeyBinds[15] = 1<<26; // switch rend\r
-       currentConfig.KeyBinds[10] = 1<<27; // save state\r
-       currentConfig.KeyBinds[11] = 1<<28; // load state\r
-       currentConfig.KeyBinds[23] = 1<<29; // vol up\r
-       currentConfig.KeyBinds[22] = 1<<30; // vol down\r
-       currentConfig.gamma = 100;\r
-       currentConfig.PicoCDBuffers = 64;\r
-       currentConfig.scaling = 0;\r
+void emu_prepareDefaultConfig(void)\r
+{\r
+       memset(&defaultConfig, 0, sizeof(defaultConfig));\r
+       defaultConfig.EmuOpt    = 0x9d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r
+       defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;\r
+       defaultConfig.s_PsndRate = 44100;\r
+       defaultConfig.s_PicoRegion = 0; // auto\r
+       defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
+       defaultConfig.s_PicoCDBuffers = 0;\r
+       defaultConfig.Frameskip = -1; // auto\r
+       defaultConfig.CPUclock = 200;\r
+       defaultConfig.volume = 50;\r
+       defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
+       defaultConfig.KeyBinds[ 4] = 1<<1;\r
+       defaultConfig.KeyBinds[ 2] = 1<<2;\r
+       defaultConfig.KeyBinds[ 6] = 1<<3;\r
+       defaultConfig.KeyBinds[14] = 1<<4;\r
+       defaultConfig.KeyBinds[13] = 1<<5;\r
+       defaultConfig.KeyBinds[12] = 1<<6;\r
+       defaultConfig.KeyBinds[ 8] = 1<<7;\r
+       defaultConfig.KeyBinds[15] = 1<<26; // switch rend\r
+       defaultConfig.KeyBinds[10] = 1<<27; // save state\r
+       defaultConfig.KeyBinds[11] = 1<<28; // load state\r
+       defaultConfig.KeyBinds[23] = 1<<29; // vol up\r
+       defaultConfig.KeyBinds[22] = 1<<30; // vol down\r
+       defaultConfig.gamma = 100;\r
+       defaultConfig.scaling = 0;\r
+       defaultConfig.turbo_rate = 15;\r
 }\r
 \r
 void osd_text(int x, int y, const char *text)\r
@@ -239,7 +193,7 @@ void osd_text(int x, int y, const char *text)
        }\r
 }\r
 \r
-static void cd_leds(void)\r
+static void draw_cd_leds(void)\r
 {\r
 //     static\r
        int old_reg;\r
@@ -267,18 +221,37 @@ static void cd_leds(void)
        }\r
 }\r
 \r
-static int EmuScan16(unsigned int num, void *sdata)\r
+static void draw_pico_ptr(void)\r
+{\r
+       unsigned short *p = (unsigned short *)gp2x_screen;\r
+\r
+       // only if pen enabled and for 16bit modes\r
+       if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;\r
+\r
+       if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))\r
+               p += 32;\r
+\r
+       p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);\r
+       p += pico_pen_x + PICO_PEN_ADJUST_X;\r
+       p[0]   ^= 0xffff;\r
+       p[319] ^= 0xffff;\r
+       p[320] ^= 0xffff;\r
+       p[321] ^= 0xffff;\r
+       p[640] ^= 0xffff;\r
+}\r
+\r
+static int EmuScanBegin16(unsigned int num)\r
 {\r
        if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);\r
+       DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;\r
 \r
        return 0;\r
 }\r
 \r
-static int EmuScan8(unsigned int num, void *sdata)\r
+static int EmuScanBegin8(unsigned int num)\r
 {\r
        if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned char *)  gp2x_screen + 320*(num+1);\r
+       DrawLineDest = (unsigned char *)  gp2x_screen + 320 * num;\r
 \r
        return 0;\r
 }\r
@@ -290,7 +263,8 @@ static void blit(const char *fps, const char *notice)
 {\r
        int emu_opt = currentConfig.EmuOpt;\r
 \r
-       if (PicoOpt&0x10) {\r
+       if (PicoOpt&0x10)\r
+       {\r
                // 8bit fast renderer\r
                if (Pico.m.dirtyPal) {\r
                        Pico.m.dirtyPal = 0;\r
@@ -298,30 +272,43 @@ static void blit(const char *fps, const char *notice)
                        // feed new palette to our device\r
                        gp2x_video_setpalette(localPal, 0x40);\r
                }\r
+               // a hack for VR\r
+               if (PicoRead16Hook == PicoSVPRead16)\r
+                       memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
+               // do actual copy\r
                vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
-       } else if (!(emu_opt&0x80)) {\r
+       }\r
+       else if (!(emu_opt&0x80))\r
+       {\r
                // 8bit accurate renderer\r
-               if (Pico.m.dirtyPal) {\r
+               if (Pico.m.dirtyPal)\r
+               {\r
+                       int pallen = 0xc0;\r
                        Pico.m.dirtyPal = 0;\r
-                       if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\r
+                       if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r
+                       {\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
                                vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
                                vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r
-                               blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);\r
-                               localPal[0xc0] = 0x0000c000;\r
-                               localPal[0xd0] = 0x00c00000;\r
-                               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
-                               localPal[0xf0] = 0x00ffffff;\r
-                               gp2x_video_setpalette(localPal, 0x100);\r
-                       } else if (rendstatus & 0x20) { // mid-frame palette changes\r
+                               memcpy32(localPal+0xc0, localPal+0x40, 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
-                               gp2x_video_setpalette(localPal, 0xc0);\r
-                       else {\r
+                       }\r
+                       else {\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-                               gp2x_video_setpalette(localPal, 0x40);\r
+                               memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r
                        }\r
+                       if (pallen > 0xc0) {\r
+                               localPal[0xc0] = 0x0000c000;\r
+                               localPal[0xd0] = 0x00c00000;\r
+                               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
+                               localPal[0xf0] = 0x00ffffff;\r
+                       }\r
+                       gp2x_video_setpalette(localPal, pallen);\r
                }\r
        }\r
 \r
@@ -332,8 +319,10 @@ static void blit(const char *fps, const char *notice)
                if (emu_opt & 2)\r
                        osd_text(osd_fps_x, h, fps);\r
        }\r
-       if ((emu_opt & 0x400) && (PicoMCD & 1))\r
-               cd_leds();\r
+       if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
+               draw_cd_leds();\r
+       if (PicoAHW & PAHW_PICO)\r
+               draw_pico_ptr();\r
 \r
        //gp2x_video_wait_vsync();\r
        gp2x_video_flip();\r
@@ -374,13 +363,11 @@ static void vidResetMode(void)
        } else if (currentConfig.EmuOpt&0x80) {\r
                gp2x_video_changemode(16);\r
                PicoDrawSetColorFormat(1);\r
-               PicoScan = EmuScan16;\r
-               PicoScan(0, 0);\r
+               PicoScanBegin = EmuScanBegin16;\r
        } else {\r
                gp2x_video_changemode(8);\r
                PicoDrawSetColorFormat(2);\r
-               PicoScan = EmuScan8;\r
-               PicoScan(0, 0);\r
+               PicoScanBegin = EmuScanBegin8;\r
        }\r
        if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
                // setup pal for 8-bit modes\r
@@ -432,18 +419,107 @@ static void emu_msg_tray_open(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
+static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r
+{\r
+       int ret, px, py, lim_x;\r
+       static int pdown_frames = 0;\r
+\r
+       emu_RunEventsPico(events);\r
+\r
+       if (pico_inp_mode == 0) return;\r
+\r
+       // for F200\r
+       ret = gp2x_touchpad_read(&px, &py);\r
+       if (ret >= 0)\r
+       {\r
+               if (ret > 35000)\r
+               {\r
+                       if (pdown_frames++ > 5)\r
+                               PicoPad[0] |= 0x20;\r
+\r
+                       pico_pen_x = px;\r
+                       pico_pen_y = py;\r
+                       if (!(Pico.video.reg[12]&1)) {\r
+                               pico_pen_x -= 32;\r
+                               if (pico_pen_x <   0) pico_pen_x = 0;\r
+                               if (pico_pen_x > 248) pico_pen_x = 248;\r
+                       }\r
+                       if (pico_pen_y > 224) pico_pen_y = 224;\r
+               }\r
+               else\r
+                       pdown_frames = 0;\r
+\r
+               //if (ret == 0)\r
+               //      PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
+       }\r
+\r
+       PicoPad[0] &= ~0x0f; // release UDLR\r
+       if (gp2x_keys & GP2X_UP)    pico_pen_y--;\r
+       if (gp2x_keys & GP2X_DOWN)  pico_pen_y++;\r
+       if (gp2x_keys & GP2X_LEFT)  pico_pen_x--;\r
+       if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;\r
+\r
+       lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r
+       if (pico_pen_y < 8) pico_pen_y = 8;\r
+       if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;\r
+       if (pico_pen_x < 0) pico_pen_x = 0;\r
+       if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;\r
+\r
+       PicoPicohw.pen_pos[0] = pico_pen_x;\r
+       if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;\r
+       PicoPicohw.pen_pos[0] += 0x3c;\r
+       PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r
+}\r
+\r
+static void update_volume(int has_changed, int is_up)\r
+{\r
+       static int prev_frame = 0, wait_frames = 0;\r
+       int vol = currentConfig.volume;\r
+\r
+       if (has_changed)\r
+       {\r
+               if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
+                       wait_frames++;\r
+               else {\r
+                       if (is_up) {\r
+                               if (vol < 99) vol++;\r
+                       } else {\r
+                               if (vol >  0) vol--;\r
+                       }\r
+                       wait_frames = 0;\r
+                       gp2x_sound_volume(vol, vol);\r
+                       currentConfig.volume = vol;\r
+               }\r
+               sprintf(noticeMsg, "VOL: %02i", vol);\r
+               gettimeofday(&noticeMsgTime, 0);\r
+               prev_frame = Pico.m.frame_count;\r
+       }\r
+\r
+       // set the right mixer func\r
+       if (!(PicoOpt&8)) return; // just use defaults for mono\r
+       if (vol >= 5)\r
+               PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
+       else {\r
+               mix_32_to_16l_level = 5 - vol;\r
+               PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
+       }\r
+}\r
+\r
 static void RunEvents(unsigned int which)\r
 {\r
-       if(which & 0x1800) { // save or load (but not both)\r
+       if (which & 0x1800) // save or load (but not both)\r
+       {\r
                int do_it = 1;\r
                if ( emu_checkSaveFile(state_slot) &&\r
                                (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) ||   // load\r
                                 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r
                        unsigned long keys;\r
                        blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r
-                       while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
+                       while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
                                usleep(50*1024);\r
                        if (keys & GP2X_X) do_it = 0;\r
+                       while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release\r
+                               usleep(50*1024);\r
                        clearArea(0);\r
                }\r
                if (do_it) {\r
@@ -456,7 +532,8 @@ static void RunEvents(unsigned int which)
 \r
                reset_timing = 1;\r
        }\r
-       if(which & 0x0400) { // switch renderer\r
+       if (which & 0x0400) // switch renderer\r
+       {\r
                if      (  PicoOpt&0x10)             { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
                else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
                else   currentConfig.EmuOpt &= ~0x80;\r
@@ -473,7 +550,8 @@ static void RunEvents(unsigned int which)
 \r
                gettimeofday(&noticeMsgTime, 0);\r
        }\r
-       if(which & 0x0300) {\r
+       if (which & 0x0300)\r
+       {\r
                if(which&0x0200) {\r
                        state_slot -= 1;\r
                        if(state_slot < 0) state_slot = 9;\r
@@ -484,16 +562,15 @@ static void RunEvents(unsigned int which)
                sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
                gettimeofday(&noticeMsgTime, 0);\r
        }\r
-       if(which & 0x0080) {\r
+       if (which & 0x0080) {\r
                engineState = PGS_Menu;\r
        }\r
 }\r
 \r
-\r
 static void updateKeys(void)\r
 {\r
-       unsigned long keys, allActions[2] = { 0, 0 }, events;\r
-       static unsigned long prevEvents = 0;\r
+       unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;\r
+       static unsigned int prevEvents = 0;\r
        int joy, i;\r
 \r
        keys = gp2x_joystick_read(0);\r
@@ -504,26 +581,30 @@ static void updateKeys(void)
        }\r
 \r
        keys &= CONFIGURABLE_KEYS;\r
+       keys2 = keys;\r
 \r
        for (i = 0; i < 32; i++)\r
        {\r
-               if (keys & (1 << i)) {\r
+               if (keys2 & (1 << i))\r
+               {\r
                        int pl, acts = currentConfig.KeyBinds[i];\r
                        if (!acts) continue;\r
                        pl = (acts >> 16) & 1;\r
-                       if (combo_keys & (1 << i)) {\r
-                               int u = i+1, acts_c = acts & combo_acts;\r
+                       if (kb_combo_keys & (1 << i))\r
+                       {\r
+                               int u = i+1, acts_c = acts & kb_combo_acts;\r
                                // let's try to find the other one\r
-                               if (acts_c)\r
+                               if (acts_c) {\r
                                        for (; u < 32; u++)\r
-                                               if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {\r
-                                                       allActions[pl] |= acts_c;\r
-                                                       keys &= ~((1 << i) | (1 << u));\r
+                                               if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
+                                                       allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r
+                                                       keys2 &= ~((1 << i) | (1 << u));\r
                                                        break;\r
                                                }\r
+                               }\r
                                // add non-combo actions if combo ones were not found\r
                                if (!acts_c || u == 32)\r
-                                       allActions[pl] |= acts & ~combo_acts;\r
+                                       allActions[pl] |= acts & ~kb_combo_acts;\r
                        } else {\r
                                allActions[pl] |= acts;\r
                        }\r
@@ -535,9 +616,9 @@ static void updateKeys(void)
        {\r
                gp2x_usbjoy_update();\r
                for (joy = 0; joy < num_of_joys; joy++) {\r
-                       int keys = gp2x_usbjoy_check2(joy);\r
+                       int btns = gp2x_usbjoy_check2(joy);\r
                        for (i = 0; i < 32; i++) {\r
-                               if (keys & (1 << i)) {\r
+                               if (btns & (1 << i)) {\r
                                        int acts = currentConfig.JoyBinds[joy][i];\r
                                        int pl = (acts >> 16) & 1;\r
                                        allActions[pl] |= acts;\r
@@ -546,26 +627,28 @@ static void updateKeys(void)
                }\r
        }\r
 \r
-       PicoPad[0] = (unsigned short) allActions[0];\r
-       PicoPad[1] = (unsigned short) allActions[1];\r
+       PicoPad[0] = allActions[0] & 0xfff;\r
+       PicoPad[1] = allActions[1] & 0xfff;\r
+\r
+       if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);\r
+       if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);\r
 \r
        events = (allActions[0] | allActions[1]) >> 16;\r
 \r
        // volume is treated in special way and triggered every frame\r
-       if(events & 0x6000) {\r
-               int vol = currentConfig.volume;\r
-               if (events & 0x2000) {\r
-                       if (vol < 99) vol++;\r
-               } else {\r
-                       if (vol >  0) vol--;\r
-               }\r
-               gp2x_sound_volume(vol, vol);\r
-               sprintf(noticeMsg, "VOL: %02i", vol);\r
-               gettimeofday(&noticeMsgTime, 0);\r
-               currentConfig.volume = vol;\r
+       if (events & 0x6000)\r
+               update_volume(1, events & 0x2000);\r
+\r
+       if ((events ^ prevEvents) & 0x40) {\r
+               emu_changeFastForward(events & 0x40);\r
+               update_volume(0, 0);\r
+               reset_timing = 1;\r
        }\r
 \r
        events &= ~prevEvents;\r
+\r
+       if (PicoAHW == PAHW_PICO)\r
+               RunEventsPico(events, keys);\r
        if (events) RunEvents(events);\r
        if (movie_data) emu_updateMovie();\r
 \r
@@ -582,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
@@ -591,19 +714,18 @@ static void SkipFrame(int do_audio)
 }\r
 \r
 \r
-void emu_forcedFrame(void)\r
+void emu_forcedFrame(int opts)\r
 {\r
        int po_old = PicoOpt;\r
        int eo_old = currentConfig.EmuOpt;\r
 \r
-       PicoOpt &= ~0x0010;\r
-       PicoOpt |=  0x4080; // soft_scale | acc_sprites\r
+       PicoOpt &= ~0x10;\r
+       PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites\r
        currentConfig.EmuOpt |= 0x80;\r
 \r
        //vidResetMode();\r
        PicoDrawSetColorFormat(1);\r
-       PicoScan = EmuScan16;\r
-       PicoScan(0, 0);\r
+       PicoScanBegin = EmuScanBegin16;\r
        Pico.m.dirtyPal = 1;\r
        PicoFrameDrawOnly();\r
 \r
@@ -621,31 +743,85 @@ void emu_forcedFrame(void)
        currentConfig.EmuOpt = eo_old;\r
 }\r
 \r
+void emu_platformDebugCat(char *str)\r
+{\r
+       // nothing\r
+}\r
+\r
 static void simpleWait(int thissec, int lim_time)\r
 {\r
        struct timeval tval;\r
 \r
        spend_cycles(1024);\r
        gettimeofday(&tval, 0);\r
-       if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+       if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
 \r
-       while(tval.tv_usec < lim_time)\r
+       while (tval.tv_usec < lim_time)\r
        {\r
                spend_cycles(1024);\r
                gettimeofday(&tval, 0);\r
-               if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+               if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
        }\r
 }\r
 \r
 \r
+#if 0\r
+static void tga_dump(void)\r
+{\r
+#define BYTE unsigned char\r
+#define WORD unsigned short\r
+       struct\r
+       {\r
+               BYTE IDLength;        /* 00h  Size of Image ID field */\r
+               BYTE ColorMapType;    /* 01h  Color map type */\r
+               BYTE ImageType;       /* 02h  Image type code */\r
+               WORD CMapStart;       /* 03h  Color map origin */\r
+               WORD CMapLength;      /* 05h  Color map length */\r
+               BYTE CMapDepth;       /* 07h  Depth of color map entries */\r
+               WORD XOffset;         /* 08h  X origin of image */\r
+               WORD YOffset;         /* 0Ah  Y origin of image */\r
+               WORD Width;           /* 0Ch  Width of image */\r
+               WORD Height;          /* 0Eh  Height of image */\r
+               BYTE PixelDepth;      /* 10h  Image pixel size */\r
+               BYTE ImageDescriptor; /* 11h  Image descriptor byte */\r
+       } __attribute__((packed)) TGAHEAD;\r
+       static unsigned short oldscr[320*240];\r
+       FILE *f; char name[128]; int i;\r
+\r
+       memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
+       TGAHEAD.ImageType = 2;\r
+       TGAHEAD.Width = 320;\r
+       TGAHEAD.Height = 240;\r
+       TGAHEAD.PixelDepth = 16;\r
+       TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
+\r
+#define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
+\r
+       for (i = 0; i < 320*240; i++)\r
+               if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
+       if (i < 320*240)\r
+       {\r
+               for (i = 0; i < 320*240; i++)\r
+                       oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
+               sprintf(name, "%05i.tga", Pico.m.frame_count);\r
+               f = fopen(name, "wb");\r
+               if (!f) { printf("!f\n"); exit(1); }\r
+               fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
+               fwrite(oldscr, 1, 320*240*2, f);\r
+               fclose(f);\r
+       }\r
+}\r
+#endif\r
+\r
+\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 thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;\r
-       int target_fps, target_frametime, lim_time, vsync_offset, i;\r
+       int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
+       int  frames_done,  frames_shown,  thissec; // actual frames\r
+       int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
        char *notice = 0;\r
 \r
        printf("entered emu_Loop()\n");\r
@@ -677,42 +853,17 @@ void emu_Loop(void)
        scaling_update();\r
        Pico.m.dirtyPal = 1;\r
        oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
-       find_combos();\r
+       emu_findKeyBindCombos();\r
 \r
        // pal/ntsc might have changed, reset related stuff\r
        target_fps = Pico.m.pal ? 50 : 60;\r
        target_frametime = 1000000/target_fps;\r
        reset_timing = 1;\r
 \r
-       // prepare sound stuff\r
-       if(currentConfig.EmuOpt & 4) {\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
-                       /* if 940 is turned off, we need it to be put back to sleep */\r
-                       if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {\r
-                               Reset940(1, 2);\r
-                               Pause940(1);\r
-                       }\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
-               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 = 0;\r
-       }\r
+       emu_startSound();\r
 \r
        // prepare CD buffer\r
-       if (PicoMCD & 1) PicoCDBufferInit();\r
+       if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
 \r
        // calc vsync offset to sync timing code with vsync\r
        if (currentConfig.EmuOpt&0x2000) {\r
@@ -727,20 +878,24 @@ void emu_Loop(void)
        } else\r
                vsync_offset = 0;\r
 \r
-       // loop?\r
+       frames_done = frames_shown = thissec =\r
+       pframes_done = pframes_shown = pthissec = 0;\r
+\r
+       // loop\r
        while (engineState == PGS_Running)\r
        {\r
                int modes;\r
 \r
                gettimeofday(&tval, 0);\r
-               if(reset_timing) {\r
+               if (reset_timing) {\r
                        reset_timing = 0;\r
-                       thissec = tval.tv_sec;\r
-                       frames_shown = frames_done = tval.tv_usec/target_frametime;\r
+                       pthissec = tval.tv_sec;\r
+                       pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
                }\r
 \r
                // show notice message?\r
-               if(noticeMsgTime.tv_sec) {\r
+               if (noticeMsgTime.tv_sec)\r
+               {\r
                        static int noticeMsgSum;\r
                        if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
                                noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
@@ -755,7 +910,8 @@ void emu_Loop(void)
 \r
                // check for mode changes\r
                modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
-               if (modes != oldmodes) {\r
+               if (modes != oldmodes)\r
+               {\r
                        int scalex = 320;\r
                        osd_fps_x = OSD_FPS_X;\r
                        if (modes & 4) {\r
@@ -777,10 +933,11 @@ void emu_Loop(void)
                }\r
 \r
                // second changed?\r
-               if(thissec != tval.tv_sec) {\r
+               if (thissec != tval.tv_sec)\r
+               {\r
 #ifdef BENCHMARK\r
                        static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
-                       if(++bench == 10) {\r
+                       if (++bench == 10) {\r
                                bench = 0;\r
                                bench_fps_s = bench_fps;\r
                                bf[bfp++ & 3] = bench_fps;\r
@@ -789,45 +946,55 @@ void emu_Loop(void)
                        bench_fps += frames_shown;\r
                        sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
 #else\r
-                       if(currentConfig.EmuOpt & 2)\r
+                       if (currentConfig.EmuOpt & 2) {\r
                                sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
+                               if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
+                       }\r
 #endif\r
+                       frames_shown = frames_done = 0;\r
                        thissec = tval.tv_sec;\r
+               }\r
+#ifdef PFRAMES\r
+               sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
+#endif\r
 \r
-                       if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
-                               frames_done = frames_shown = 0;\r
+               if (pthissec != tval.tv_sec)\r
+               {\r
+                       if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
+                               pframes_done = pframes_shown = 0;\r
                        } else {\r
                                // it is quite common for this implementation to leave 1 fame unfinished\r
                                // when second changes, but we don't want buffer to starve.\r
-                               if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r
+                               if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
                                        updateKeys();\r
-                                       SkipFrame(1); frames_done++;\r
+                                       SkipFrame(1); pframes_done++;\r
                                }\r
 \r
-                               frames_done  -= target_fps; if (frames_done  < 0) frames_done  = 0;\r
-                               frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;\r
-                               if (frames_shown > frames_done) frames_shown = frames_done;\r
+                               pframes_done  -= target_fps; if (pframes_done  < 0) pframes_done  = 0;\r
+                               pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
+                               if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
                        }\r
+                       pthissec = tval.tv_sec;\r
                }\r
-#ifdef PFRAMES\r
-               sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
-#endif\r
 \r
-               lim_time = (frames_done+1) * target_frametime + vsync_offset;\r
-               if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
+               lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
+               if (currentConfig.Frameskip >= 0) // frameskip enabled\r
+               {\r
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
                                updateKeys();\r
-                               SkipFrame(1); frames_done++;\r
-                               if (PsndOut) { // do framelimitting if sound is enabled\r
+                               SkipFrame(1); pframes_done++; frames_done++;\r
+                               if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
                                        gettimeofday(&tval, 0);\r
-                                       if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-                                       if(tval.tv_usec < lim_time) { // we are too fast\r
-                                               simpleWait(thissec, lim_time);\r
+                                       if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+                                       if (tval.tv_usec < lim_time) { // we are too fast\r
+                                               simpleWait(pthissec, lim_time);\r
                                        }\r
                                }\r
                                lim_time += target_frametime;\r
                        }\r
-               } else if(tval.tv_usec > lim_time) { // auto frameskip\r
+               }\r
+               else if (tval.tv_usec > lim_time) // auto frameskip\r
+               {\r
                        // no time left for this frame - skip\r
                        if (tval.tv_usec - lim_time >= 300000) {\r
                                /* something caused a slowdown for us (disk access? cache flush?)\r
@@ -836,74 +1003,16 @@ void emu_Loop(void)
                                continue;\r
                        }\r
                        updateKeys();\r
-                       SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;\r
+                       SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
                        continue;\r
                }\r
 \r
                updateKeys();\r
                PicoFrame();\r
 \r
-#if 0\r
-if (Pico.m.frame_count == 31563) {\r
-       FILE *f;\r
-       f = fopen("ram_p.bin", "wb");\r
-       if (!f) { printf("!f\n"); exit(1); }\r
-       fwrite(Pico.ram, 1, 0x10000, f);\r
-       fclose(f);\r
-       exit(0);\r
-}\r
-#endif\r
-#if 0\r
-               // debug\r
-               {\r
-                       #define BYTE unsigned char\r
-                       #define WORD unsigned short\r
-                       struct\r
-                       {\r
-                               BYTE IDLength;        /* 00h  Size of Image ID field */\r
-                               BYTE ColorMapType;    /* 01h  Color map type */\r
-                               BYTE ImageType;       /* 02h  Image type code */\r
-                               WORD CMapStart;       /* 03h  Color map origin */\r
-                               WORD CMapLength;      /* 05h  Color map length */\r
-                               BYTE CMapDepth;       /* 07h  Depth of color map entries */\r
-                               WORD XOffset;         /* 08h  X origin of image */\r
-                               WORD YOffset;         /* 0Ah  Y origin of image */\r
-                               WORD Width;           /* 0Ch  Width of image */\r
-                               WORD Height;          /* 0Eh  Height of image */\r
-                               BYTE PixelDepth;      /* 10h  Image pixel size */\r
-                               BYTE ImageDescriptor; /* 11h  Image descriptor byte */\r
-                       } __attribute__((packed)) TGAHEAD;\r
-                       static unsigned short oldscr[320*240];\r
-                       FILE *f; char name[128]; int i;\r
-\r
-                       memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
-                       TGAHEAD.ImageType = 2;\r
-                       TGAHEAD.Width = 320;\r
-                       TGAHEAD.Height = 240;\r
-                       TGAHEAD.PixelDepth = 16;\r
-                       TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
-\r
-                       #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
-\r
-                       for (i = 0; i < 320*240; i++)\r
-                               if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
-                       if (i < 320*240)\r
-                       {\r
-                               for (i = 0; i < 320*240; i++)\r
-                                       oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
-                               sprintf(name, "%05i.tga", Pico.m.frame_count);\r
-                               f = fopen(name, "wb");\r
-                               if (!f) { printf("!f\n"); exit(1); }\r
-                               fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
-                               fwrite(oldscr, 1, 320*240*2, f);\r
-                               fclose(f);\r
-                       }\r
-               }\r
-#endif\r
-\r
                // check time\r
                gettimeofday(&tval, 0);\r
-               if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+               if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
 \r
                if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
                        reset_timing = 1;\r
@@ -911,26 +1020,28 @@ if (Pico.m.frame_count == 31563) {
                {\r
                        // sleep or vsync if we are still too fast\r
                        // usleep sleeps for ~20ms minimum, so it is not a solution here\r
-                       if(tval.tv_usec < lim_time)\r
+                       if (!reset_timing && tval.tv_usec < lim_time)\r
                        {\r
                                // we are too fast\r
                                if (vsync_offset) {\r
                                        if (lim_time - tval.tv_usec > target_frametime/2)\r
-                                               simpleWait(thissec, lim_time - target_frametime/4);\r
+                                               simpleWait(pthissec, lim_time - target_frametime/4);\r
                                        gp2x_video_wait_vsync();\r
                                } else {\r
-                                       simpleWait(thissec, lim_time);\r
+                                       simpleWait(pthissec, lim_time);\r
                                }\r
                        }\r
                }\r
 \r
                blit(fpsbuff, notice);\r
 \r
-               frames_done++; frames_shown++;\r
+               pframes_done++; pframes_shown++;\r
+                frames_done++;  frames_shown++;\r
        }\r
 \r
+       emu_changeFastForward(0);\r
 \r
-       if (PicoMCD & 1) PicoCDBufferFree();\r
+       if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
 \r
        // save SRAM\r
        if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
@@ -941,14 +1052,13 @@ if (Pico.m.frame_count == 31563) {
 \r
        // if in 8bit mode, generate 16bit image for menu background\r
        if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
-               emu_forcedFrame();\r
+               emu_forcedFrame(POPT_EN_SOFTSCALE);\r
 }\r
 \r
 \r
 void emu_ResetGame(void)\r
 {\r
-       PicoReset(0);\r
+       PicoReset();\r
        reset_timing = 1;\r
 }\r
 \r
-\r