linux port fixed; some cleanups
[libpicofe.git] / gp2x / emu.c
index 5f4a0e2..46dc3aa 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006-2007 notaz, All rights reserved.\r
+// (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
@@ -8,25 +8,23 @@
 #include <sys/time.h>\r
 #include <sys/stat.h>\r
 #include <sys/types.h>\r
-#include <linux/limits.h>\r
-#include <ctype.h>\r
-#include <unistd.h>\r
-\r
 #include <stdarg.h>\r
 \r
-#include "emu.h"\r
-#include "gp2x.h"\r
-#include "usbjoy.h"\r
-#include "menu.h"\r
+#include "plat_gp2x.h"\r
+#include "soc.h"\r
+#include "../common/plat.h"\r
+#include "../common/menu.h"\r
 #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
+#include "../common/input.h"\r
+#include "../linux/sndout_oss.h"\r
+#include "version.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
 #endif\r
 \r
 \r
-int engineState;\r
-int select_exits = 0;\r
-\r
-char romFileName[PATH_MAX];\r
-\r
 extern int crashed_940;\r
 \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 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, pico_inp_mode = 0;\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
@@ -66,7 +58,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 +69,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
@@ -103,12 +97,16 @@ void emu_Init(void)
 \r
 static void scaling_update(void)\r
 {\r
-       PicoOpt &= ~0x4100;\r
+       PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r
        switch (currentConfig.scaling) {\r
-               default: break; // off\r
-               case 1:  // hw hor\r
-               case 2:  PicoOpt |=  0x0100; break; // hw hor+vert\r
-               case 3:  PicoOpt |=  0x4000; break; // sw hor\r
+               default:break;\r
+               case EOPT_SCALE_HW_H:\r
+               case EOPT_SCALE_HW_HV:\r
+                       PicoOpt |= POPT_DIS_32C_BORDER;\r
+                       break;\r
+               case EOPT_SCALE_SW_H:\r
+                       PicoOpt |= POPT_EN_SOFTSCALE;\r
+                       break;\r
        }\r
 }\r
 \r
@@ -121,12 +119,8 @@ void emu_Deinit(void)
                SRam.changed = 0;\r
        }\r
 \r
-       if (!(currentConfig.EmuOpt & 0x20)) {\r
-               config_writelrom(PicoConfigFile);\r
-#ifndef NO_SYNC\r
-               sync();\r
-#endif\r
-       }\r
+       if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG))\r
+               emu_writelrom();\r
 \r
        free(PicoDraw2FB);\r
 \r
@@ -134,68 +128,53 @@ void emu_Deinit(void)
 \r
        // restore gamma\r
        if (gp2x_old_gamma != 100)\r
-               set_gamma(100, 0);\r
+               set_lcd_gamma(100, 0);\r
 }\r
 \r
 void emu_prepareDefaultConfig(void)\r
 {\r
+       gp2x_soc_t soc;\r
+\r
        memset(&defaultConfig, 0, sizeof(defaultConfig));\r
-       defaultConfig.EmuOpt    = 0x1d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r
-       defaultConfig.s_PicoOpt = 0x0f | 0x20e00; // | <- use_940, cd_pcm, cd_cdda, svp drc\r
+       defaultConfig.EmuOpt    = 0x9d | EOPT_RAM_TIMINGS | 0x600; // | <- confirm_save, cd_leds\r
+       defaultConfig.s_PicoOpt = 0x0f | 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.CPUclock = default_cpu_clock;\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
-}\r
+       defaultConfig.turbo_rate = 15;\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
+       soc = soc_detect();\r
+       if (soc == SOCID_MMSP2)\r
+               defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
 }\r
 \r
-void osd_text(int x, int y, const char *text)\r
+static void osd_text(int x, int y, const char *text)\r
 {\r
        int len = strlen(text)*8;\r
+       int *p, i, h, offs;\r
 \r
        if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               int *p, i, h;\r
-               x &= ~3; // align x\r
                len = (len+3) >> 2;\r
                for (h = 0; h < 8; h++) {\r
-                       p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r
-                       for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
+                       offs = (x + g_screen_width * (y+h)) & ~3;\r
+                       p = (int *) ((char *)g_screen_ptr + offs);\r
+                       for (i = len; i; i--, p++)\r
+                               *p = 0xe0e0e0e0;\r
                }\r
                emu_textOut8(x, y, text);\r
        } else {\r
-               int *p, i, h;\r
-               x &= ~1; // align x\r
                len = (len+1) >> 1;\r
                for (h = 0; h < 8; h++) {\r
-                       p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));\r
-                       for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r
+                       offs = (x + g_screen_width * (y+h)) & ~1;\r
+                       p = (int *) ((short *)g_screen_ptr + offs);\r
+                       for (i = len; i; i--, p++)\r
+                               *p = (*p >> 2) & 0x39e7;\r
                }\r
                emu_textOut16(x, y, text);\r
        }\r
@@ -212,15 +191,15 @@ static void draw_cd_leds(void)
                // 8-bit modes\r
                unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
                unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
-               *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =\r
-               *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =\r
-               *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;\r
-               *(unsigned int *)((char *)gp2x_screen + 320*2+12) =\r
-               *(unsigned int *)((char *)gp2x_screen + 320*3+12) =\r
-               *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;\r
+               *(unsigned int *)((char *)g_screen_ptr + 320*2+ 4) =\r
+               *(unsigned int *)((char *)g_screen_ptr + 320*3+ 4) =\r
+               *(unsigned int *)((char *)g_screen_ptr + 320*4+ 4) = col_g;\r
+               *(unsigned int *)((char *)g_screen_ptr + 320*2+12) =\r
+               *(unsigned int *)((char *)g_screen_ptr + 320*3+12) =\r
+               *(unsigned int *)((char *)g_screen_ptr + 320*4+12) = col_r;\r
        } else {\r
                // 16-bit modes\r
-               unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);\r
+               unsigned int *p = (unsigned int *)((short *)g_screen_ptr + 320*2+4);\r
                unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r
                unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r
                *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
@@ -231,7 +210,7 @@ static void draw_cd_leds(void)
 \r
 static void draw_pico_ptr(void)\r
 {\r
-       unsigned short *p = (unsigned short *)gp2x_screen;\r
+       unsigned short *p = (unsigned short *)g_screen_ptr;\r
 \r
        // only if pen enabled and for 16bit modes\r
        if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;\r
@@ -251,7 +230,7 @@ static void draw_pico_ptr(void)
 static int EmuScanBegin16(unsigned int num)\r
 {\r
        if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;\r
+       DrawLineDest = (unsigned short *) g_screen_ptr + g_screen_width * num;\r
 \r
        return 0;\r
 }\r
@@ -259,7 +238,7 @@ static int EmuScanBegin16(unsigned int num)
 static int EmuScanBegin8(unsigned int num)\r
 {\r
        if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned char *)  gp2x_screen + 320 * num;\r
+       DrawLineDest = (unsigned char *)  g_screen_ptr + g_screen_width * num;\r
 \r
        return 0;\r
 }\r
@@ -284,14 +263,14 @@ static void blit(const char *fps, const char *notice)
                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
+               vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);\r
        }\r
        else if (!(emu_opt&0x80))\r
        {\r
                // 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
@@ -301,21 +280,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
@@ -329,8 +301,10 @@ static void blit(const char *fps, const char *notice)
 \r
        if (notice || (emu_opt & 2)) {\r
                int h = 232;\r
-               if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r
-               if (notice) osd_text(4, h, notice);\r
+               if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
+                       h -= 8;\r
+               if (notice)\r
+                       osd_text(4, h, notice);\r
                if (emu_opt & 2)\r
                        osd_text(osd_fps_x, h, fps);\r
        }\r
@@ -345,12 +319,12 @@ static void blit(const char *fps, const char *notice)
        if (!(PicoOpt&0x10)) {\r
                if (!(Pico.video.reg[1]&8)) {\r
                        if (currentConfig.EmuOpt&0x80) {\r
-                               DrawLineDest = (unsigned short *) gp2x_screen + 320*8;\r
+                               DrawLineDest = (unsigned short *) g_screen_ptr + 320*8;\r
                        } else {\r
-                               DrawLineDest = (unsigned char  *) gp2x_screen + 320*8;\r
+                               DrawLineDest = (unsigned char  *) g_screen_ptr + 320*8;\r
                        }\r
                } else {\r
-                       DrawLineDest = gp2x_screen;\r
+                       DrawLineDest = g_screen_ptr;\r
                }\r
        }\r
 }\r
@@ -396,7 +370,7 @@ static void vidResetMode(void)
        }\r
        Pico.m.dirtyPal = 1;\r
        // reset scaling\r
-       if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r
+       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
             gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r
        else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
 }\r
@@ -408,12 +382,12 @@ static void emu_msg_cb(const char *msg)
                // 8-bit renderers\r
                gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
                osd_text(4, 232, msg);\r
-               gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);\r
+               gp2x_memcpy_all_buffers((char *)g_screen_ptr+320*232, 320*232, 320*8);\r
        } else {\r
                // 16bit accurate renderer\r
                gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
                osd_text(4, 232, msg);\r
-               gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);\r
+               gp2x_memcpy_all_buffers((char *)g_screen_ptr+320*232*2, 320*232*2, 320*8*2);\r
        }\r
        gettimeofday(&noticeMsgTime, 0);\r
        noticeMsgTime.tv_sec -= 2;\r
@@ -434,48 +408,56 @@ static void emu_msg_tray_open(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
-static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r
+static void RunEventsPico(unsigned int events)\r
 {\r
-       if (events & (1 << 3)) {\r
-               pico_inp_mode++;\r
-               if (pico_inp_mode > 2) pico_inp_mode = 0;\r
-               switch (pico_inp_mode) {\r
-                       case 2: strcpy(noticeMsg, "Input: Pen on Pad      "); break;\r
-                       case 1: strcpy(noticeMsg, "Input: Pen on Storyware"); break;\r
-                       case 0: strcpy(noticeMsg, "Input: Joytick         ");\r
-                               PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
-                               break;\r
-               }\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-       if (events & (1 << 4)) {\r
-               PicoPicohw.page--;\r
-               if (PicoPicohw.page < 0) PicoPicohw.page = 0;\r
-               sprintf(noticeMsg, "Page %i                 ", PicoPicohw.page);\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-       if (events & (1 << 5)) {\r
-               PicoPicohw.page++;\r
-               if (PicoPicohw.page > 6) PicoPicohw.page = 6;\r
-               sprintf(noticeMsg, "Page %i                 ", PicoPicohw.page);\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-       if (pico_inp_mode != 0) {\r
-               PicoPad[0] &= ~0x0f; // release UDLR\r
-               if (gp2x_keys & GP2X_UP)   { pico_pen_y--; if (pico_pen_y < 0) pico_pen_y = 0; }\r
-               if (gp2x_keys & GP2X_DOWN) { pico_pen_y++; if (pico_pen_y > 239-PICO_PEN_ADJUST_Y) pico_pen_y = 239-PICO_PEN_ADJUST_Y; }\r
-               if (gp2x_keys & GP2X_LEFT) { pico_pen_x--; if (pico_pen_x < 0) pico_pen_x = 0; }\r
-               if (gp2x_keys & GP2X_RIGHT) {\r
-                       int lim = (Pico.video.reg[12]&1) ? 319 : 255;\r
-                       pico_pen_x++;\r
-                       if (pico_pen_x > lim-PICO_PEN_ADJUST_X)\r
-                               pico_pen_x = lim-PICO_PEN_ADJUST_X;\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
-               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
+               else\r
+                       pdown_frames = 0;\r
+\r
+               //if (ret == 0)\r
+               //      PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
        }\r
+\r
+       if (PicoPad[0] & 1) pico_pen_y--;\r
+       if (PicoPad[0] & 2) pico_pen_y++;\r
+       if (PicoPad[0] & 4) pico_pen_x--;\r
+       if (PicoPad[0] & 8) pico_pen_x++;\r
+       PicoPad[0] &= ~0x0f; // release UDLR\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
@@ -494,7 +476,7 @@ static void update_volume(int has_changed, int is_up)
                                if (vol >  0) vol--;\r
                        }\r
                        wait_frames = 0;\r
-                       gp2x_sound_volume(vol, vol);\r
+                       sndout_oss_setvol(vol, vol);\r
                        currentConfig.volume = vol;\r
                }\r
                sprintf(noticeMsg, "VOL: %02i", vol);\r
@@ -512,52 +494,43 @@ static void update_volume(int has_changed, int is_up)
        }\r
 }\r
 \r
-static void change_fast_forward(int set_on)\r
-{\r
-       static void *set_PsndOut = NULL;\r
-       static int set_Frameskip, set_EmuOpt, is_on = 0;\r
-\r
-       if (set_on && !is_on) {\r
-               set_PsndOut = PsndOut;\r
-               set_Frameskip = currentConfig.Frameskip;\r
-               set_EmuOpt = currentConfig.EmuOpt;\r
-               PsndOut = NULL;\r
-               currentConfig.Frameskip = 8;\r
-               currentConfig.EmuOpt &= ~4;\r
-               is_on = 1;\r
-       }\r
-       else if (!set_on && is_on) {\r
-               PsndOut = set_PsndOut;\r
-               currentConfig.Frameskip = set_Frameskip;\r
-               currentConfig.EmuOpt = set_EmuOpt;\r
-               PsndRerate(1);\r
-               update_volume(0, 0);\r
-               reset_timing = 1;\r
-               is_on = 0;\r
-       }\r
-}\r
-\r
 static void RunEvents(unsigned int which)\r
 {\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
-                               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
+                               (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load\r
+                                (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save\r
+               {\r
+                       const char *nm;\r
+                       char tmp[64];\r
+                       int keys, len;\r
+\r
+                       strcpy(tmp, (which & 0x1000) ? "LOAD STATE? " : "OVERWRITE SAVE? ");\r
+                       len = strlen(tmp);\r
+                       nm = in_get_key_name(-1, -PBTN_MA3);\r
+                       snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);\r
+                       len = strlen(tmp);\r
+                       nm = in_get_key_name(-1, -PBTN_MBACK);\r
+                       snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);\r
+\r
+                       blit("", tmp);\r
+\r
+                       in_set_blocking(1);\r
+                       while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK));   // wait for release\r
+                       while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) ); // .. press\r
+                       if (keys & PBTN_MBACK)\r
+                               do_it = 0;\r
+                       while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK));   // .. release\r
+                       in_set_blocking(0);\r
+\r
                        clearArea(0);\r
                }\r
                if (do_it) {\r
                        osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
                        PicoStateProgressCB = emu_state_cb;\r
-                       gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r
+                       gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
                        emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
                        PicoStateProgressCB = NULL;\r
                }\r
@@ -580,7 +553,7 @@ static void RunEvents(unsigned int which)
                        strcpy(noticeMsg, " 8bit accurate renderer");\r
                }\r
 \r
-               gettimeofday(&noticeMsgTime, 0);\r
+               emu_noticeMsgUpdated();\r
        }\r
        if (which & 0x0300)\r
        {\r
@@ -592,7 +565,7 @@ static void RunEvents(unsigned int which)
                        if(state_slot > 9) state_slot = 0;\r
                }\r
                sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
-               gettimeofday(&noticeMsgTime, 0);\r
+               emu_noticeMsgUpdated();\r
        }\r
        if (which & 0x0080) {\r
                engineState = PGS_Menu;\r
@@ -601,66 +574,17 @@ static void RunEvents(unsigned int which)
 \r
 static void updateKeys(void)\r
 {\r
-       unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;\r
+       unsigned int allActions[2] = { 0, 0 }, events;\r
        static unsigned int prevEvents = 0;\r
-       int joy, i;\r
 \r
-       keys = gp2x_joystick_read(0);\r
-       if (keys & GP2X_SELECT) {\r
-               engineState = select_exits ? PGS_Quit : PGS_Menu;\r
-               // wait until select is released, so menu would not resume game\r
-               while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
-       }\r
+       /* FIXME: player2 */\r
+       allActions[0] = in_update();\r
 \r
-       keys &= CONFIGURABLE_KEYS;\r
-       keys2 = keys;\r
+       PicoPad[0] = allActions[0] & 0xfff;\r
+       PicoPad[1] = allActions[1] & 0xfff;\r
 \r
-       for (i = 0; i < 32; i++)\r
-       {\r
-               if (keys2 & (1 << i))\r
-               {\r
-                       int pl, acts = currentConfig.KeyBinds[i];\r
-                       if (!acts) continue;\r
-                       pl = (acts >> 16) & 1;\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
-                                       for (; u < 32; 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 & ~kb_combo_acts;\r
-                       } else {\r
-                               allActions[pl] |= acts;\r
-                       }\r
-               }\r
-       }\r
-\r
-       // add joy inputs\r
-       if (num_of_joys > 0)\r
-       {\r
-               gp2x_usbjoy_update();\r
-               for (joy = 0; joy < num_of_joys; joy++) {\r
-                       int btns = gp2x_usbjoy_check2(joy);\r
-                       for (i = 0; i < 32; i++) {\r
-                               if (btns & (1 << i)) {\r
-                                       int acts = currentConfig.JoyBinds[joy][i];\r
-                                       int pl = (acts >> 16) & 1;\r
-                                       allActions[pl] |= acts;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       PicoPad[0] = (unsigned short) allActions[0];\r
-       PicoPad[1] = (unsigned short) allActions[1];\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
@@ -668,14 +592,16 @@ static void updateKeys(void)
        if (events & 0x6000)\r
                update_volume(1, events & 0x2000);\r
 \r
-       if ((events ^ prevEvents) & 0x40)\r
-               change_fast_forward(events & 0x40);\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
-\r
+               RunEventsPico(events);\r
        if (events) RunEvents(events);\r
        if (movie_data) emu_updateMovie();\r
 \r
@@ -689,7 +615,47 @@ static void updateSound(int len)
 \r
        /* avoid writing audio when lagging behind to prevent audio lag */\r
        if (PicoSkipFrame != 2)\r
-               gp2x_sound_write(PsndOut, len<<1);\r
+               sndout_oss_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
+               sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);\r
+               sndout_oss_setvol(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
@@ -701,13 +667,13 @@ 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
@@ -722,7 +688,7 @@ void emu_forcedFrame(void)
                clearArea(1);\r
        } else  vidCpyM2 = vidCpyM2_40col;\r
 \r
-       vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
+       vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);\r
        vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
        gp2x_video_setpalette(localPal, 0x40);\r
 */\r
@@ -730,6 +696,11 @@ 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
@@ -780,11 +751,11 @@ static void tga_dump(void)
 #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(oldscr[i] != CONV(((unsigned short *)g_screen_ptr)[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
+                       oldscr[i] = CONV(((unsigned short *)g_screen_ptr)[i]);\r
                sprintf(name, "%05i.tga", Pico.m.frame_count);\r
                f = fopen(name, "wb");\r
                if (!f) { printf("!f\n"); exit(1); }\r
@@ -798,8 +769,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 = -1, 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
@@ -809,25 +779,38 @@ void emu_Loop(void)
 \r
        printf("entered emu_Loop()\n");\r
 \r
+       if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
+               if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
+                       set_ram_timings();\r
+               else\r
+                       unset_ram_timings();\r
+       }\r
+\r
+       if (gp2x_old_clock < 0)\r
+               gp2x_old_clock = default_cpu_clock;\r
        if (gp2x_old_clock != currentConfig.CPUclock) {\r
                printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
-               set_FCLK(currentConfig.CPUclock);\r
+               gp2x_set_cpuclk(currentConfig.CPUclock);\r
                gp2x_old_clock = currentConfig.CPUclock;\r
                printf(" done\n");\r
        }\r
 \r
        if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
-               set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
+               set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
                gp2x_old_gamma = currentConfig.gamma;\r
                printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
        }\r
 \r
-       if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
-               if (currentConfig.EmuOpt&0x2000)\r
-                    set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
-               else unset_LCD_custom_rate();\r
+       if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_PSYNC) {\r
+               if (currentConfig.EmuOpt & EOPT_PSYNC)\r
+                       set_lcd_custom_rate(Pico.m.pal);\r
+               else\r
+                       unset_lcd_custom_rate();\r
        }\r
 \r
+       if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r
+               gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r
+\r
        EmuOpt_old = currentConfig.EmuOpt;\r
        fpsbuff[0] = 0;\r
 \r
@@ -836,36 +819,13 @@ void emu_Loop(void)
        scaling_update();\r
        Pico.m.dirtyPal = 1;\r
        oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\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
-       {\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
@@ -930,7 +890,8 @@ void emu_Loop(void)
                                        vidCpyM2 = vidCpyM2_32col;\r
                                }\r
                        }\r
-                       if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
+                       /* want vertical scaling and game is not in 240 line mode */\r
+                       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(modes&8))\r
                             gp2x_video_RGB_setscaling(8, scalex, 224);\r
                        else gp2x_video_RGB_setscaling(0, scalex, 240);\r
                        oldmodes = modes;\r
@@ -1044,7 +1005,7 @@ void emu_Loop(void)
                 frames_done++;  frames_shown++;\r
        }\r
 \r
-       change_fast_forward(0);\r
+       emu_changeFastForward(0);\r
 \r
        if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
 \r
@@ -1057,7 +1018,7 @@ void emu_Loop(void)
 \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
@@ -1067,3 +1028,24 @@ void emu_ResetGame(void)
        reset_timing = 1;\r
 }\r
 \r
+const char *plat_get_credits(void)\r
+{\r
+       return "PicoDrive v" VERSION " (c) notaz, 2006-2009\n\n\n"\r
+               "Credits:\n"\r
+               "fDave: Cyclone 68000 core,\n"\r
+               "      base code of PicoDrive\n"\r
+               "Reesy & FluBBa: DrZ80 core\n"\r
+               "MAME devs: YM2612 and SN76496 cores\n"\r
+               "rlyeh and others: minimal SDK\n"\r
+               "Squidge: mmuhack\n"\r
+               "Dzz: ARM940 sample\n"\r
+               "GnoStiC / Puck2099: USB joy code\n"\r
+               "craigix: GP2X hardware\n"\r
+               "ketchupgun: skin design\n"\r
+               "\n"\r
+               "special thanks (for docs, ideas):\n"\r
+               " Charles MacDonald, Haze,\n"\r
+               " Stephane Dallongeville,\n"\r
+               " Lordus, Exophase, Rokas,\n"\r
+               " Nemesis, Tasco Deluxe";\r
+}\r