extend mmap wrapper functionality
[libpicofe.git] / gp2x / emu.c
index 82f1c1d..df9faac 100644 (file)
@@ -1,19 +1,30 @@
-// (c) Copyright 2006-2009 notaz, All rights reserved.\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
+/*\r
+ * (c) Copyright 2006-2010 notaz, All rights reserved.\r
+ *\r
+ * For performance reasons 3 renderers are exported for both MD and 32x modes:\r
+ * - 16bpp line renderer\r
+ * - 8bpp line renderer (slightly faster)\r
+ * - 8bpp tile renderer\r
+ * In 32x mode:\r
+ * - 32x layer is overlayed on top of 16bpp one\r
+ * - line internal one done on PicoDraw2FB, then mixed with 32x\r
+ * - tile internal one done on PicoDraw2FB, then mixed with 32x\r
+ */\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
+#include <unistd.h>\r
 \r
 #include "plat_gp2x.h"\r
 #include "soc.h"\r
+#include "soc_pollux.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 "../common/input.h"\r
 #include "../linux/sndout_oss.h"\r
 #include "version.h"\r
 \r
@@ -22,8 +33,6 @@
 #include <pico/sound/mix.h>\r
 #include <zlib/zlib.h>\r
 \r
-//#define PFRAMES\r
-\r
 #ifdef BENCHMARK\r
 #define OSD_FPS_X 220\r
 #else\r
 \r
 extern int crashed_940;\r
 \r
-static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
+static short __attribute__((aligned(4))) sndBuffer[2*(44100+100)/50];\r
 static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];\r
 unsigned char *PicoDraw2FB = PicoDraw2FB_;\r
-static int osd_fps_x;\r
+static int osd_fps_x, osd_y, doing_bg_frame;\r
+const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };\r
+const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };\r
+enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };\r
+\r
+static int (*emu_scan_begin)(unsigned int num) = NULL;\r
+static int (*emu_scan_end)(unsigned int num) = NULL;\r
 \r
 extern void *gp2x_screens[4];\r
 \r
-int plat_get_root_dir(char *dst, int len)\r
+\r
+void pemu_prep_defconfig(void)\r
 {\r
-       extern char **g_argv;\r
-       int j;\r
+       gp2x_soc_t soc;\r
 \r
-       strncpy(dst, g_argv[0], len);\r
-       len -= 32; // reserve\r
-       if (len < 0) len = 0;\r
-       dst[len] = 0;\r
-       for (j = strlen(dst); j > 0; j--)\r
-               if (dst[j] == '/') { dst[j+1] = 0; break; }\r
+       defaultConfig.CPUclock = default_cpu_clock;\r
+       defaultConfig.renderer32x = RT_8BIT_FAST;\r
+       defaultConfig.analog_deadzone = 50;\r
 \r
-       return j + 1;\r
+       soc = soc_detect();\r
+       if (soc == SOCID_MMSP2)\r
+               defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
+       else if (soc == SOCID_POLLUX) {\r
+               defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
+               defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;\r
+       }\r
 }\r
 \r
-\r
-static void scaling_update(void)\r
+void pemu_validate_config(void)\r
 {\r
-       PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r
-       switch (currentConfig.scaling) {\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
+       if (gp2x_dev_id != GP2X_DEV_GP2X)\r
+               PicoOpt &= ~POPT_EXT_FM;\r
+       if (gp2x_dev_id != GP2X_DEV_WIZ)\r
+               currentConfig.EmuOpt &= ~EOPT_WIZ_TEAR_FIX;\r
+\r
+       if (currentConfig.gamma < 10 || currentConfig.gamma > 300)\r
+               currentConfig.gamma = 100;\r
+\r
+       if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 1024)\r
+               currentConfig.CPUclock = default_cpu_clock;\r
 }\r
 \r
+static int get_renderer(void)\r
+{\r
+       if (PicoAHW & PAHW_32X)\r
+               return currentConfig.renderer32x;\r
+       else\r
+               return currentConfig.renderer;\r
+}\r
 \r
-void pemu_prep_defconfig(void)\r
+static void change_renderer(int diff)\r
 {\r
-       gp2x_soc_t soc;\r
+       int *r;\r
+       if (PicoAHW & PAHW_32X)\r
+               r = &currentConfig.renderer32x;\r
+       else\r
+               r = &currentConfig.renderer;\r
+       *r += diff;\r
 \r
-       memset(&defaultConfig, 0, sizeof(defaultConfig));\r
-       defaultConfig.EmuOpt    = 0x9d | EOPT_RAM_TIMINGS|EOPT_CONFIRM_SAVE|EOPT_EN_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 = default_cpu_clock;\r
-       defaultConfig.volume = 50;\r
-       defaultConfig.gamma = 100;\r
-       defaultConfig.scaling = 0;\r
-       defaultConfig.turbo_rate = 15;\r
+       // 8bpp fast is not there (yet?)\r
+       if ((PicoAHW & PAHW_SMS) && *r == RT_8BIT_FAST)\r
+               (*r)++;\r
 \r
-       soc = soc_detect();\r
-       if (soc == SOCID_MMSP2)\r
-               defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
-       else if (soc == SOCID_POLLUX)\r
-               defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
+       if      (*r >= RT_COUNT)\r
+               *r = 0;\r
+       else if (*r < 0)\r
+               *r = RT_COUNT - 1;\r
 }\r
 \r
+#define is_16bit_mode() \\r
+       (get_renderer() == RT_16BIT || (PicoAHW & PAHW_32X))\r
+\r
 static void (*osd_text)(int x, int y, const char *text);\r
 \r
 static void osd_text8(int x, int y, const char *text)\r
@@ -170,7 +190,7 @@ static void draw_cd_leds(void)
                scr_offs = pitch * 2 + 4;\r
        }\r
 \r
-       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r
+       if (!is_16bit_mode()) {\r
        #define p(x) px[(x) >> 2]\r
                // 8-bit modes\r
                unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);\r
@@ -197,7 +217,7 @@ static void draw_pico_ptr(void)
        int x, y, pitch = 320;\r
 \r
        // only if pen enabled and for 16bit modes\r
-       if (pico_inp_mode == 0 || (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r
+       if (pico_inp_mode == 0 || currentConfig.EmuOpt != RT_16BIT)\r
                return;\r
 \r
        x = pico_pen_x + PICO_PEN_ADJUST_X;\r
@@ -205,7 +225,7 @@ static void draw_pico_ptr(void)
        if (!(Pico.video.reg[12]&1) && !(PicoOpt & POPT_DIS_32C_BORDER))\r
                x += 32;\r
 \r
-       if (EOPT_WIZ_TEAR_FIX) {\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
                pitch = 240;\r
                p += (319 - x) * pitch + y;\r
        } else\r
@@ -218,22 +238,6 @@ static void draw_pico_ptr(void)
        p[pitch*2] ^= 0xffff;\r
 }\r
 \r
-static int EmuScanBegin16(unsigned int num)\r
-{\r
-       if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned short *) g_screen_ptr + g_screen_width * num;\r
-\r
-       return 0;\r
-}\r
-\r
-static int EmuScanBegin8(unsigned int num)\r
-{\r
-       if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned char *)  g_screen_ptr + g_screen_width * num;\r
-\r
-       return 0;\r
-}\r
-\r
 /* rot thing for Wiz */\r
 static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r
 \r
@@ -247,8 +251,6 @@ static int EmuScanEnd16_rot(unsigned int num)
 {\r
        if ((num & 3) != 3)\r
                return 0;\r
-       if (!(Pico.video.reg[1] & 8))\r
-               num += 8;\r
        rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r
                !(Pico.video.reg[12] & 1) && !(PicoOpt & POPT_EN_SOFTSCALE));\r
        return 0;\r
@@ -264,85 +266,150 @@ static int EmuScanEnd8_rot(unsigned int num)
 {\r
        if ((num & 3) != 3)\r
                return 0;\r
-       if (!(Pico.video.reg[1] & 8))\r
-               num += 8;\r
        rotated_blit8(g_screen_ptr, rot_buff, num + 1,\r
                !(Pico.video.reg[12] & 1));\r
        return 0;\r
 }\r
 \r
-int localPal[0x100];\r
-static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border) = NULL;\r
+/* line doublers */\r
+static unsigned int ld_counter;\r
+static int ld_left, ld_lines;\r
+\r
+static int EmuScanBegin16_ld(unsigned int num)\r
+{\r
+       if ((signed int)(ld_counter - num) > 100)\r
+               ld_counter = 0;\r
+\r
+       if (emu_scan_begin)\r
+               return emu_scan_begin(ld_counter);\r
+       else\r
+               DrawLineDest = (char *)g_screen_ptr + 320 * ld_counter * gp2x_current_bpp / 8;\r
+\r
+       return 0;\r
+}\r
+\r
+static int EmuScanEnd16_ld(unsigned int num)\r
+{\r
+       void *oldline = DrawLineDest;\r
+\r
+       if (emu_scan_end)\r
+               emu_scan_end(ld_counter);\r
+\r
+       ld_counter++;\r
+       ld_left--;\r
+       if (ld_left <= 0) {\r
+               ld_left = ld_lines;\r
+\r
+               EmuScanBegin16_ld(num);\r
+               memcpy32(DrawLineDest, oldline, 320 * gp2x_current_bpp / 8 / 4);\r
+               if (emu_scan_end)\r
+                       emu_scan_end(ld_counter);\r
+\r
+               ld_counter++;\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+static int localPal[0x100];\r
+static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border);\r
+static int (*make_local_pal)(int fast_mode);\r
+\r
+static int make_local_pal_md(int fast_mode)\r
+{\r
+       int pallen = 0xc0;\r
+\r
+       bgr444_to_rgb32(localPal, Pico.cram);\r
+       if (fast_mode)\r
+               return 0x40;\r
+\r
+       if (Pico.video.reg[0xC] & 8) { // shadow/hilight mode\r
+               bgr444_to_rgb32_sh(localPal, Pico.cram);\r
+               localPal[0xc0] = 0x0000c000;\r
+               localPal[0xd0] = 0x00c00000;\r
+               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
+               localPal[0xf0] = 0x00ffffff;\r
+               pallen = 0x100;\r
+       }\r
+       else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
+               bgr444_to_rgb32(localPal+0x40, HighPal);\r
+               bgr444_to_rgb32(localPal+0x80, HighPal+0x40);\r
+       }\r
+       else\r
+               memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r
+\r
+       return pallen;\r
+}\r
+\r
+static int make_local_pal_sms(int fast_mode)\r
+{\r
+       unsigned short *spal = Pico.cram;\r
+       unsigned int *dpal = (void *)localPal;\r
+       unsigned int i, t;\r
+\r
+       for (i = 0x40; i > 0; i--) {\r
+               t = *spal++;\r
+               t = ((t & 0x0003) << 22) | ((t & 0x000c) << 12) | ((t & 0x0030) << 2);\r
+               t |= t >> 2;\r
+               t |= t >> 4;\r
+               *dpal++ = t;\r
+       }\r
+\r
+       return 0x40;\r
+}\r
 \r
-void pemu_update_display(const char *fps, const char *notice)\r
+void pemu_finalize_frame(const char *fps, const char *notice)\r
 {\r
        int emu_opt = currentConfig.EmuOpt;\r
+       int ret;\r
 \r
-       if (PicoOpt & POPT_ALT_RENDERER)\r
+       if (PicoAHW & PAHW_32X)\r
+               ; // nothing to do\r
+       else if (get_renderer() == RT_8BIT_FAST)\r
        {\r
                // 8bit fast renderer\r
                if (Pico.m.dirtyPal) {\r
                        Pico.m.dirtyPal = 0;\r
-                       vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                       ret = make_local_pal(1);\r
                        // feed new palette to our device\r
-                       gp2x_video_setpalette(localPal, 0x40);\r
+                       gp2x_video_setpalette(localPal, ret);\r
                }\r
                // a hack for VR\r
-               if (PicoRead16Hook == PicoSVPRead16)\r
+               if (PicoAHW & PAHW_SVP)\r
                        memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
                // do actual copy\r
                vidcpyM2(g_screen_ptr, PicoDraw2FB+328*8,\r
                        !(Pico.video.reg[12] & 1), !(PicoOpt & POPT_DIS_32C_BORDER));\r
        }\r
-       else if (!(emu_opt & EOPT_16BPP))\r
+       else if (get_renderer() == RT_8BIT_ACC)\r
        {\r
                // 8bit accurate renderer\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
-                       {\r
-                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-                               vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
-                               vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\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
-                       }\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
-                               localPal[0xd0] = 0x00c00000;\r
-                               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
-                               localPal[0xf0] = 0x00ffffff;\r
-                       }\r
-                       gp2x_video_setpalette(localPal, pallen);\r
+                       ret = make_local_pal(0);\r
+                       gp2x_video_setpalette(localPal, ret);\r
                }\r
        }\r
 \r
-       if (notice || (emu_opt & 2)) {\r
-               int h = 232;\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
-       if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
+       if (notice)\r
+               osd_text(4, osd_y, notice);\r
+       if (emu_opt & EOPT_SHOW_FPS)\r
+               osd_text(osd_fps_x, osd_y, fps);\r
+       if ((PicoAHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))\r
                draw_cd_leds();\r
        if (PicoAHW & PAHW_PICO)\r
                draw_pico_ptr();\r
+}\r
 \r
+void plat_video_flip(void)\r
+{\r
+       int stride = g_screen_width;\r
        gp2x_video_flip();\r
+\r
+       if (is_16bit_mode())\r
+               stride *= 2;\r
+       PicoDrawSetOutBuf(g_screen_ptr, stride);\r
 }\r
 \r
 /* XXX */\r
@@ -379,7 +446,7 @@ void plat_video_wait_vsync(void)
 \r
 void plat_status_msg_clear(void)\r
 {\r
-       int is_8bit = (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP);\r
+       int is_8bit = !is_16bit_mode();\r
        if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
                /* ugh.. */\r
                int i, u, *p;\r
@@ -408,7 +475,8 @@ void plat_status_msg_clear(void)
 void plat_status_msg_busy_next(const char *msg)\r
 {\r
        plat_status_msg_clear();\r
-       pemu_update_display("", msg);\r
+       pemu_finalize_frame("", msg);\r
+       plat_video_flip();\r
        emu_status_msg("");\r
 \r
        /* assumption: msg_busy_next gets called only when\r
@@ -422,49 +490,72 @@ void plat_status_msg_busy_first(const char *msg)
        plat_status_msg_busy_next(msg);\r
 }\r
 \r
-static void vidResetMode(void)\r
+static void vid_reset_mode(void)\r
 {\r
-       PicoScanEnd = NULL;\r
+       int gp2x_mode = 16;\r
+       int renderer = get_renderer();\r
 \r
-       if (PicoOpt & POPT_ALT_RENDERER) {\r
-               if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
-                       gp2x_video_changemode(-8);\r
-                       vidcpyM2 = vidcpy_m2_rot;\r
-                       osd_text = osd_text8_rot;\r
-               } else {\r
-                       gp2x_video_changemode(8);\r
-                       vidcpyM2 = vidcpy_m2;\r
-                       osd_text = osd_text8;\r
-               }\r
+       PicoOpt &= ~POPT_ALT_RENDERER;\r
+       emu_scan_begin = NULL;\r
+       emu_scan_end = NULL;\r
+\r
+       switch (renderer) {\r
+       case RT_16BIT:\r
+               PicoDrawSetOutFormat(PDF_RGB555, 0);\r
+               PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
+               break;\r
+       case RT_8BIT_ACC:\r
+               PicoDrawSetOutFormat(PDF_8BIT, 0);\r
+               PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);\r
+               gp2x_mode = 8;\r
+               break;\r
+       case RT_8BIT_FAST:\r
+               PicoOpt |= POPT_ALT_RENDERER;\r
+               PicoDrawSetOutFormat(PDF_NONE, 0);\r
+               vidcpyM2 = vidcpy_m2;\r
+               gp2x_mode = 8;\r
+               break;\r
+       default:\r
+               printf("bad renderer\n");\r
+               break;\r
        }\r
-       else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
-               PicoDrawSetColorFormat(1);\r
-               if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
-                       gp2x_video_changemode(-16);\r
-                       PicoScanBegin = EmuScanBegin16_rot;\r
-                       PicoScanEnd = EmuScanEnd16_rot;\r
-                       osd_text = osd_text16_rot;\r
-               } else {\r
-                       gp2x_video_changemode(16);\r
-                       PicoScanBegin = EmuScanBegin16;\r
-                       osd_text = osd_text16;\r
+\r
+       if (PicoAHW & PAHW_32X) {\r
+               // Wiz 16bit is an exception, uses line rendering due to rotation mess\r
+               if (renderer == RT_16BIT && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {\r
+                       PicoDrawSetOutFormat(PDF_RGB555, 1);\r
+                       PicoDraw32xSetFrameMode(0, 0);\r
                }\r
+               else {\r
+                       PicoDrawSetOutFormat(PDF_NONE, 0);\r
+                       PicoDraw32xSetFrameMode(1, 0);\r
+               }\r
+               PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
+               gp2x_mode = 16;\r
        }\r
-       else {\r
-               PicoDrawSetColorFormat(2);\r
-               if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
-                       gp2x_video_changemode(-8);\r
-                       PicoScanBegin = EmuScanBegin8_rot;\r
-                       PicoScanEnd = EmuScanEnd8_rot;\r
-                       osd_text = osd_text8_rot;\r
-               } else {\r
-                       gp2x_video_changemode(8);\r
-                       PicoScanBegin = EmuScanBegin8;\r
-                       osd_text = osd_text8;\r
+\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
+               if ((PicoAHW & PAHW_32X) || renderer == RT_16BIT) {\r
+                       emu_scan_begin = EmuScanBegin16_rot;\r
+                       emu_scan_end = EmuScanEnd16_rot;\r
+               }\r
+               else if (renderer == RT_8BIT_ACC) {\r
+                       emu_scan_begin = EmuScanBegin8_rot;\r
+                       emu_scan_end = EmuScanEnd8_rot;\r
                }\r
+               else if (renderer == RT_8BIT_FAST)\r
+                       vidcpyM2 = vidcpy_m2_rot;\r
        }\r
 \r
-       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r
+       PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);\r
+\r
+       if (is_16bit_mode())\r
+               osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : osd_text16;\r
+       else\r
+               osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;\r
+\r
+       gp2x_video_wait_vsync();\r
+       if (!is_16bit_mode()) {\r
                // setup pal for 8-bit modes\r
                localPal[0xc0] = 0x0000c000; // MCD LEDs\r
                localPal[0xd0] = 0x00c00000;\r
@@ -472,46 +563,79 @@ static void vidResetMode(void)
                localPal[0xf0] = 0x00ffffff;\r
                gp2x_video_setpalette(localPal, 0x100);\r
                gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
-               gp2x_video_flip();\r
        }\r
+       else\r
+               gp2x_memset_all_buffers(0, 0, 320*240*2);\r
+\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)\r
+               gp2x_mode = -gp2x_mode;\r
+\r
+       gp2x_video_changemode(gp2x_mode);\r
+\r
        Pico.m.dirtyPal = 1;\r
 \r
-       // reset scaling\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
+       PicoOpt &= ~POPT_EN_SOFTSCALE;\r
+       if (currentConfig.scaling == EOPT_SCALE_SW)\r
+               PicoOpt |= POPT_EN_SOFTSCALE;\r
+\r
+       // palette converters for 8bit modes\r
+       make_local_pal = (PicoAHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r
 }\r
 \r
-void plat_video_toggle_renderer(int is_next, int is_menu)\r
+void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
 {\r
-       /* alt, 16bpp, 8bpp */\r
-       if (PicoOpt & POPT_ALT_RENDERER) {\r
-               PicoOpt &= ~POPT_ALT_RENDERER;\r
-               if (is_next)\r
-                       currentConfig.EmuOpt |= EOPT_16BPP;\r
-       } else if (!(currentConfig.EmuOpt & EOPT_16BPP)) {\r
-               if (is_next)\r
-                       PicoOpt |= POPT_ALT_RENDERER;\r
-               else\r
-                       currentConfig.EmuOpt |= EOPT_16BPP;\r
-       } else {\r
-               currentConfig.EmuOpt &= ~EOPT_16BPP;\r
-               if (!is_next)\r
-                       PicoOpt |= POPT_ALT_RENDERER;\r
-       }\r
+       int scalex = 320, scaley = 240;\r
+       int ln_offs = 0;\r
 \r
-       if (is_menu)\r
+       if (doing_bg_frame)\r
                return;\r
 \r
-       vidResetMode();\r
+       osd_fps_x = OSD_FPS_X;\r
+       osd_y = 232;\r
 \r
-       if (PicoOpt & POPT_ALT_RENDERER) {\r
-               emu_status_msg(" 8bit fast renderer");\r
-       } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
-               emu_status_msg("16bit accurate renderer");\r
-       } else {\r
-               emu_status_msg(" 8bit accurate renderer");\r
+       /* set up hwscaling here */\r
+       PicoOpt &= ~POPT_DIS_32C_BORDER;\r
+       if (is_32cols && currentConfig.scaling == EOPT_SCALE_HW) {\r
+               scalex = 256;\r
+               PicoOpt |= POPT_DIS_32C_BORDER;\r
+               osd_fps_x = OSD_FPS_X - 64;\r
+       }\r
+\r
+       if (currentConfig.vscaling == EOPT_SCALE_HW) {\r
+               ln_offs = start_line;\r
+               scaley = line_count;\r
+               osd_y = start_line + line_count - 8;\r
+       }\r
+\r
+       gp2x_video_RGB_setscaling(ln_offs, scalex, scaley);\r
+\r
+       /* line doubling */\r
+       if (currentConfig.vscaling == EOPT_SCALE_SW && line_count < 240) {\r
+               ld_lines = ld_left = line_count / (240 - line_count);\r
+               PicoDrawSetCallbacks(EmuScanBegin16_ld, EmuScanEnd16_ld);\r
        }\r
+\r
+       // clear whole screen in all buffers\r
+       if (!is_16bit_mode())\r
+               gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
+       else\r
+               gp2x_memset_all_buffers(0, 0, 320*240*2);\r
+}\r
+\r
+void plat_video_toggle_renderer(int change, int is_menu_call)\r
+{\r
+       change_renderer(change);\r
+\r
+       if (is_menu_call)\r
+               return;\r
+\r
+       vid_reset_mode();\r
+       rendstatus_old = -1;\r
+\r
+       if (PicoAHW & PAHW_32X)\r
+               emu_status_msg(renderer_names32x[get_renderer()]);\r
+       else\r
+               emu_status_msg(renderer_names[get_renderer()]);\r
 }\r
 \r
 #if 0 // TODO\r
@@ -588,14 +712,13 @@ void plat_update_volume(int has_changed, int is_up)
        }\r
 }\r
 \r
-static void updateSound(int len)\r
+static void oss_write_nonblocking(int len)\r
 {\r
-       if (PicoOpt & POPT_EN_STEREO)\r
-               len <<= 1;\r
+       // sndout_oss_can_write() is not reliable, only use with no_frmlimit\r
+       if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r
+               return;\r
 \r
-       /* avoid writing audio when lagging behind to prevent audio lag */\r
-       if (PicoSkipFrame != 2)\r
-               sndout_oss_write(PsndOut, len<<1);\r
+       sndout_oss_write_nb(PsndOut, len);\r
 }\r
 \r
 void pemu_sound_start(void)\r
@@ -608,37 +731,49 @@ void pemu_sound_start(void)
        if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
        {\r
                int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
-               int target_fps = Pico.m.pal ? 50 : 60;\r
-               int snd_excess_add;\r
+               int snd_rate_oss = PsndRate;\r
                gp2x_soc_t soc;\r
 \r
+               memset(sndBuffer, 0, sizeof(sndBuffer));\r
+               PsndOut = sndBuffer;\r
+               PicoWriteSound = oss_write_nonblocking;\r
+               plat_update_volume(0, 0);\r
+\r
+               printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
+                       PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
+               sndout_oss_start(snd_rate_oss, is_stereo, 1);\r
+               sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
+\r
+               soc = soc_detect();\r
+               if (soc == SOCID_POLLUX)\r
+                       PsndRate = pollux_get_real_snd_rate(PsndRate);\r
+\r
                #define SOUND_RERATE_FLAGS (POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO|POPT_EXT_FM|POPT_EN_MCD_CDDA)\r
                if (PsndRate != PsndRate_old || Pico.m.pal != pal_old || ((PicoOpt & POPT_EXT_FM) && crashed_940) ||\r
                                ((PicoOpt ^ PicoOpt_old) & SOUND_RERATE_FLAGS)) {\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, is_stereo, Pico.m.pal);\r
-               sndout_oss_start(PsndRate, 16, is_stereo);\r
-               sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
-               PicoWriteSound = updateSound;\r
-               plat_update_volume(0, 0);\r
-               memset(sndBuffer, 0, sizeof(sndBuffer));\r
-               PsndOut = sndBuffer;\r
+\r
                PsndRate_old = PsndRate;\r
                PicoOpt_old  = PicoOpt;\r
                pal_old = Pico.m.pal;\r
-\r
-               /* Wiz's sound hardware needs more prebuffer */\r
-               soc = soc_detect();\r
-               if (soc == SOCID_POLLUX)\r
-                       updateSound(PsndLen);\r
        }\r
 }\r
 \r
+static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };\r
+\r
 void pemu_sound_stop(void)\r
 {\r
+       int i;\r
+\r
+       /* get back from Pollux pain */\r
+       PsndRate += 1000;\r
+       for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {\r
+               if (PsndRate >= sound_rates[i]) {\r
+                       PsndRate = sound_rates[i];\r
+                       break;\r
+               }\r
+       }\r
 }\r
 \r
 void pemu_sound_wait(void)\r
@@ -646,49 +781,22 @@ void pemu_sound_wait(void)
        // don't need to do anything, writes will block by themselves\r
 }\r
 \r
-\r
-void pemu_forced_frame(int opts)\r
+void pemu_forced_frame(int no_scale, int do_emu)\r
 {\r
-       int po_old = PicoOpt;\r
-       int eo_old = currentConfig.EmuOpt;\r
-\r
-       PicoOpt &= ~POPT_ALT_RENDERER;\r
-       PicoOpt |= opts|POPT_ACC_SPRITES;\r
-       currentConfig.EmuOpt |= EOPT_16BPP;\r
-\r
-       PicoDrawSetColorFormat(1);\r
-       PicoScanBegin = EmuScanBegin16;\r
-       PicoScanEnd = NULL;\r
+       doing_bg_frame = 1;\r
+       PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
+       PicoDraw32xSetFrameMode(0, 0);\r
+       PicoDrawSetCallbacks(NULL, NULL);\r
        Pico.m.dirtyPal = 1;\r
-       PicoFrameDrawOnly();\r
 \r
-       PicoOpt = po_old;\r
-       currentConfig.EmuOpt = eo_old;\r
-}\r
+       emu_cmn_forced_frame(no_scale, do_emu);\r
 \r
-void plat_debug_cat(char *str)\r
-{\r
+       g_menubg_src_ptr = g_screen_ptr;\r
+       doing_bg_frame = 0;\r
 }\r
 \r
-void pemu_video_mode_change(int is_32col, int is_240_lines)\r
+void plat_debug_cat(char *str)\r
 {\r
-       int scalex = 320;\r
-       osd_fps_x = OSD_FPS_X;\r
-       if (is_32col && (PicoOpt & POPT_DIS_32C_BORDER)) {\r
-               scalex = 256;\r
-               osd_fps_x = OSD_FPS_X - 64;\r
-       }\r
-       /* want vertical scaling and game is not in 240 line mode */\r
-       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !is_240_lines)\r
-               gp2x_video_RGB_setscaling(8, scalex, 224);\r
-       else\r
-               gp2x_video_RGB_setscaling(0, scalex, 240);\r
-\r
-       // clear whole screen in all buffers\r
-       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r
-               gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
-       else\r
-               gp2x_memset_all_buffers(0, 0, 320*240*2);\r
 }\r
 \r
 #if 0\r
@@ -743,6 +851,9 @@ void pemu_loop_prep(void)
 {\r
        static int gp2x_old_clock = -1, EmuOpt_old = 0, pal_old = 0;\r
        static int gp2x_old_gamma = 100;\r
+       gp2x_soc_t soc;\r
+\r
+       soc = soc_detect();\r
 \r
        if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
                if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
@@ -753,26 +864,30 @@ void pemu_loop_prep(void)
 \r
        if (gp2x_old_clock < 0)\r
                gp2x_old_clock = default_cpu_clock;\r
-       if (gp2x_old_clock != currentConfig.CPUclock) {\r
+       if (gp2x_old_clock != currentConfig.CPUclock && gp2x_set_cpuclk != NULL) {\r
                printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\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_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
+       if (gp2x_old_gamma != currentConfig.gamma || ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_A_SN_GAMMA)) {\r
+               set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt & EOPT_A_SN_GAMMA));\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 ^ currentConfig.EmuOpt) & EOPT_PSYNC) || Pico.m.pal != pal_old) {\r
-               if (currentConfig.EmuOpt & EOPT_PSYNC)\r
+       if (((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_VSYNC) || Pico.m.pal != pal_old) {\r
+               if ((currentConfig.EmuOpt & EOPT_VSYNC) || soc == SOCID_POLLUX)\r
                        set_lcd_custom_rate(Pico.m.pal);\r
-               else\r
+               else if (EmuOpt_old & EOPT_VSYNC)\r
                        unset_lcd_custom_rate();\r
        }\r
 \r
+       if (gp2x_dev_id == GP2X_DEV_CAANOO)\r
+               in_set_config_int(in_name_to_id("evdev:pollux-analog"), IN_CFG_ABS_DEAD_ZONE,\r
+                               currentConfig.analog_deadzone);\r
+\r
        if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r
                gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r
 \r
@@ -780,34 +895,21 @@ void pemu_loop_prep(void)
        pal_old = Pico.m.pal;\r
 \r
        // make sure we are in correct mode\r
-       vidResetMode();\r
-       scaling_update();\r
+       change_renderer(0);\r
+       vid_reset_mode();\r
+\r
+       // dirty buffers better go now than during gameplay\r
+       sync();\r
+       sleep(0);\r
 \r
        pemu_sound_start();\r
 }\r
 \r
 void pemu_loop_end(void)\r
 {\r
-}\r
+       pemu_sound_stop();\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
+       /* do one more frame for menu bg */\r
+       pemu_forced_frame(0, 1);\r
 }\r
+\r