extend mmap wrapper functionality
[libpicofe.git] / gp2x / emu.c
index 8bd1b66..df9faac 100644 (file)
@@ -1,34 +1,38 @@
-// (c) Copyright 2006-2007 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 <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 "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 "cpuctrl.h"\r
-\r
-#include <Pico/PicoInt.h>\r
-#include <Pico/Patch.h>\r
+#include "../common/config.h"\r
+#include "../common/input.h"\r
+#include "../linux/sndout_oss.h"\r
+#include "version.h"\r
+\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
-\r
 #ifdef BENCHMARK\r
 #define OSD_FPS_X 220\r
 #else\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 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
+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, 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 void emu_msg_cb(const char *msg);\r
-static void emu_msg_tray_open(void);\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
-void emu_noticeMsgUpdated(void)\r
+\r
+void pemu_prep_defconfig(void)\r
 {\r
-       gettimeofday(&noticeMsgTime, 0);\r
+       gp2x_soc_t soc;\r
+\r
+       defaultConfig.CPUclock = default_cpu_clock;\r
+       defaultConfig.renderer32x = RT_8BIT_FAST;\r
+       defaultConfig.analog_deadzone = 50;\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
+               defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;\r
+       }\r
 }\r
 \r
-void emu_getMainDir(char *dst, int len)\r
+void pemu_validate_config(void)\r
 {\r
-       extern char **g_argv;\r
-       int j;\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
-       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
+       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
-void emu_Init(void)\r
+static int get_renderer(void)\r
 {\r
-       // make temp buffer for alt renderer\r
-       PicoDraw2FB = malloc((8+320)*(8+240+8));\r
-       if (!PicoDraw2FB)\r
-       {\r
-               printf("PicoDraw2FB == 0\n");\r
-       }\r
-\r
-       // make dirs for saves, cfgs, etc.\r
-       mkdir("mds", 0777);\r
-       mkdir("srm", 0777);\r
-       mkdir("brm", 0777);\r
-       mkdir("cfg", 0777);\r
+       if (PicoAHW & PAHW_32X)\r
+               return currentConfig.renderer32x;\r
+       else\r
+               return currentConfig.renderer;\r
+}\r
 \r
-       PicoInit();\r
-       PicoMessage = emu_msg_cb;\r
-       PicoMCDopenTray = emu_msg_tray_open;\r
-       PicoMCDcloseTray = menu_loop_tray;\r
+static void change_renderer(int diff)\r
+{\r
+       int *r;\r
+       if (PicoAHW & PAHW_32X)\r
+               r = &currentConfig.renderer32x;\r
+       else\r
+               r = &currentConfig.renderer;\r
+       *r += diff;\r
+\r
+       // 8bpp fast is not there (yet?)\r
+       if ((PicoAHW & PAHW_SMS) && *r == RT_8BIT_FAST)\r
+               (*r)++;\r
+\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 find_combos(void)\r
-{\r
-       int act, u;\r
+static void (*osd_text)(int x, int y, const char *text);\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;\r
-               if (act == 16) continue; // player2 flag\r
-               for (u = 0; u < 32; u++)\r
-               {\r
-                       if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\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
+static void osd_text8(int x, int y, const char *text)\r
+{\r
+       int len = strlen(text)*8;\r
+       int *p, i, h, offs;\r
+\r
+       len = (len+3) >> 2;\r
+       for (h = 0; h < 8; h++) {\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
-       // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);\r
+       emu_text_out8(x, y, text);\r
 }\r
 \r
+static void osd_text16(int x, int y, const char *text)\r
+{\r
+       int len = strlen(text)*8;\r
+       int *p, i, h, offs;\r
+\r
+       len = (len+1) >> 1;\r
+       for (h = 0; h < 8; h++) {\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_text_out16(x, y, text);\r
+}\r
 \r
-static void scaling_update(void)\r
+static void osd_text8_rot(int x, int y, const char *text)\r
 {\r
-       PicoOpt &= ~0x4100;\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
+       int len = strlen(text) * 8;\r
+       char *p = (char *)g_screen_ptr + 240*(320-x) + y;\r
+\r
+       while (len--) {\r
+               memset(p, 0xe0, 8);\r
+               p -= 240;\r
        }\r
+\r
+       emu_text_out8_rot(x, y, text);\r
 }\r
 \r
+static void osd_text16_rot(int x, int y, const char *text)\r
+{\r
+       int len = strlen(text) * 8;\r
+       short *p = (short *)g_screen_ptr + 240*(320-x) + y;\r
 \r
-void emu_Deinit(void)\r
+       while (len--) {\r
+               memset(p, 0, 8*2);\r
+               p -= 240;\r
+       }\r
+\r
+       emu_text_out16_rot(x, y, text);\r
+}\r
+\r
+static void draw_cd_leds(void)\r
 {\r
-       // save SRAM\r
-       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
-               emu_SaveLoadGame(0, 1);\r
-               SRam.changed = 0;\r
+       int led_reg, pitch, scr_offs, led_offs;\r
+       led_reg = Pico_mcd->s68k_regs[0];\r
+\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
+               pitch = 240;\r
+               led_offs = -pitch * 6;\r
+               scr_offs = pitch * (320 - 4);\r
+       } else {\r
+               pitch = 320;\r
+               led_offs = 4;\r
+               scr_offs = pitch * 2 + 4;\r
        }\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
-#ifndef NO_SYNC\r
-                       sync();\r
-#endif\r
-               }\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
+               unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
+               unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
+               p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
+               p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
+       #undef p\r
+       } else {\r
+       #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r
+               // 16-bit modes\r
+               unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);\r
+               unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;\r
+               unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;\r
+               p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
+               p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
+       #undef p\r
        }\r
+}\r
+\r
+static void draw_pico_ptr(void)\r
+{\r
+       unsigned short *p = (unsigned short *)g_screen_ptr;\r
+       int x, y, pitch = 320;\r
+\r
+       // only if pen enabled and for 16bit modes\r
+       if (pico_inp_mode == 0 || currentConfig.EmuOpt != RT_16BIT)\r
+               return;\r
 \r
-       free(PicoDraw2FB);\r
+       x = pico_pen_x + PICO_PEN_ADJUST_X;\r
+       y = pico_pen_y + PICO_PEN_ADJUST_Y;\r
+       if (!(Pico.video.reg[12]&1) && !(PicoOpt & POPT_DIS_32C_BORDER))\r
+               x += 32;\r
 \r
-       PicoExit();\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
+               pitch = 240;\r
+               p += (319 - x) * pitch + y;\r
+       } else\r
+               p += x + y * pitch;\r
 \r
-       // restore gamma\r
-       if (gp2x_old_gamma != 100)\r
-               set_gamma(100, 0);\r
+       p[0]       ^= 0xffff;\r
+       p[pitch-1] ^= 0xffff;\r
+       p[pitch]   ^= 0xffff;\r
+       p[pitch+1] ^= 0xffff;\r
+       p[pitch*2] ^= 0xffff;\r
 }\r
 \r
-void emu_setDefaultConfig(void)\r
+/* rot thing for Wiz */\r
+static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r
+\r
+static int EmuScanBegin16_rot(unsigned int num)\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
+       DrawLineDest = rot_buff + (num & 3) * 320 * 2;\r
+       return 0;\r
 }\r
 \r
-void osd_text(int x, int y, const char *text)\r
+static int EmuScanEnd16_rot(unsigned int num)\r
 {\r
-       int len = strlen(text)*8;\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
-               }\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
-               }\r
-               emu_textOut16(x, y, text);\r
-       }\r
+       if ((num & 3) != 3)\r
+               return 0;\r
+       rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r
+               !(Pico.video.reg[12] & 1) && !(PicoOpt & POPT_EN_SOFTSCALE));\r
+       return 0;\r
 }\r
 \r
-static void cd_leds(void)\r
+static int EmuScanBegin8_rot(unsigned int num)\r
 {\r
-//     static\r
-       int old_reg;\r
-//     if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r
-       old_reg = Pico_mcd->s68k_regs[0];\r
+       DrawLineDest = rot_buff + (num & 3) * 320;\r
+       return 0;\r
+}\r
 \r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               // 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
-       } else {\r
-               // 16-bit modes\r
-               unsigned int *p = (unsigned int *)((short *)gp2x_screen + 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
-               *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
-               *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r
-       }\r
+static int EmuScanEnd8_rot(unsigned int num)\r
+{\r
+       if ((num & 3) != 3)\r
+               return 0;\r
+       rotated_blit8(g_screen_ptr, rot_buff, num + 1,\r
+               !(Pico.video.reg[12] & 1));\r
+       return 0;\r
 }\r
 \r
-static int EmuScan16(unsigned int num, void *sdata)\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 (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);\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 EmuScan8(unsigned int num, void *sdata)\r
+static int EmuScanEnd16_ld(unsigned int num)\r
 {\r
-       if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned char *)  gp2x_screen + 320*(num+1);\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
-int localPal[0x100];\r
-static void (*vidCpyM2)(void *dest, void *src) = NULL;\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
-static void blit(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&0x10) {\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
-               vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
-       } else if (!(emu_opt&0x80)) {\r
+               // a hack for VR\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 (get_renderer() == RT_8BIT_ACC)\r
+       {\r
                // 8bit accurate renderer\r
-               if (Pico.m.dirtyPal) {\r
+               if (Pico.m.dirtyPal)\r
+               {\r
                        Pico.m.dirtyPal = 0;\r
-                       if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\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
-                               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
-                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-                               gp2x_video_setpalette(localPal, 0x40);\r
-                       }\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 == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r
-               if (notice) osd_text(4, h, notice);\r
-               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 (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
-       //gp2x_video_wait_vsync();\r
+void plat_video_flip(void)\r
+{\r
+       int stride = g_screen_width;\r
        gp2x_video_flip();\r
 \r
-       if (!(PicoOpt&0x10)) {\r
-               if (!(Pico.video.reg[1]&8)) {\r
-                       if (currentConfig.EmuOpt&0x80) {\r
-                               DrawLineDest = (unsigned short *) gp2x_screen + 320*8;\r
-                       } else {\r
-                               DrawLineDest = (unsigned char  *) gp2x_screen + 320*8;\r
-                       }\r
-               } else {\r
-                       DrawLineDest = gp2x_screen;\r
-               }\r
-       }\r
+       if (is_16bit_mode())\r
+               stride *= 2;\r
+       PicoDrawSetOutBuf(g_screen_ptr, stride);\r
 }\r
 \r
-\r
-// clears whole screen or just the notice area (in all buffers)\r
-static void clearArea(int full)\r
+/* XXX */\r
+#ifdef __GP2X__\r
+unsigned int plat_get_ticks_ms(void)\r
 {\r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               // 8-bit renderers\r
-               if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
-               else      gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
-       } else {\r
-               // 16bit accurate renderer\r
-               if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r
-               else      gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
-       }\r
+       return gp2x_get_ticks_ms();\r
 }\r
 \r
+unsigned int plat_get_ticks_us(void)\r
+{\r
+       return gp2x_get_ticks_us();\r
+}\r
+#endif\r
 \r
-static void vidResetMode(void)\r
+void plat_wait_till_us(unsigned int us_to)\r
 {\r
-       if (PicoOpt&0x10) {\r
-               gp2x_video_changemode(8);\r
-       } else if (currentConfig.EmuOpt&0x80) {\r
-               gp2x_video_changemode(16);\r
-               PicoDrawSetColorFormat(1);\r
-               PicoScan = EmuScan16;\r
-               PicoScan(0, 0);\r
-       } else {\r
-               gp2x_video_changemode(8);\r
-               PicoDrawSetColorFormat(2);\r
-               PicoScan = EmuScan8;\r
-               PicoScan(0, 0);\r
-       }\r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               // setup pal for 8-bit modes\r
-               localPal[0xc0] = 0x0000c000; // MCD LEDs\r
-               localPal[0xd0] = 0x00c00000;\r
-               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
-               localPal[0xf0] = 0x00ffffff;\r
-               gp2x_video_setpalette(localPal, 0x100);\r
-               gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
-               gp2x_video_flip();\r
+       unsigned int now;\r
+\r
+       spend_cycles(1024);\r
+       now = plat_get_ticks_us();\r
+\r
+       while ((signed int)(us_to - now) > 512)\r
+       {\r
+               spend_cycles(1024);\r
+               now = plat_get_ticks_us();\r
        }\r
-       Pico.m.dirtyPal = 1;\r
-       // reset scaling\r
-       if (currentConfig.scaling == 2 && !(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
 \r
+void plat_video_wait_vsync(void)\r
+{\r
+       gp2x_video_wait_vsync();\r
+}\r
 \r
-static void emu_msg_cb(const char *msg)\r
+void plat_status_msg_clear(void)\r
 {\r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               // 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
-       } 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
+       int is_8bit = !is_16bit_mode();\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
+               /* ugh.. */\r
+               int i, u, *p;\r
+               if (is_8bit) {\r
+                       for (i = 0; i < 4; i++) {\r
+                               p = (int *)gp2x_screens[i] + (240-8) / 4;\r
+                               for (u = 320; u > 0; u--, p += 240/4)\r
+                                       p[0] = p[1] = 0xe0e0e0e0;\r
+                       }\r
+               } else {\r
+                       for (i = 0; i < 4; i++) {\r
+                               p = (int *)gp2x_screens[i] + (240-8)*2 / 4;\r
+                               for (u = 320; u > 0; u--, p += 240*2/4)\r
+                                       p[0] = p[1] = p[2] = p[3] = 0;\r
+                       }\r
+               }\r
+               return;\r
        }\r
-       gettimeofday(&noticeMsgTime, 0);\r
-       noticeMsgTime.tv_sec -= 2;\r
 \r
-       /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r
-       reset_timing = 1;\r
+       if (is_8bit)\r
+               gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
+       else\r
+               gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
 }\r
 \r
-static void emu_state_cb(const char *str)\r
+void plat_status_msg_busy_next(const char *msg)\r
 {\r
-       clearArea(0);\r
-       blit("", str);\r
+       plat_status_msg_clear();\r
+       pemu_finalize_frame("", msg);\r
+       plat_video_flip();\r
+       emu_status_msg("");\r
+\r
+       /* assumption: msg_busy_next gets called only when\r
+        * something slow is about to happen */\r
+       reset_timing = 1;\r
 }\r
 \r
-static void emu_msg_tray_open(void)\r
+void plat_status_msg_busy_first(const char *msg)\r
 {\r
-       strcpy(noticeMsg, "CD tray opened");\r
-       gettimeofday(&noticeMsgTime, 0);\r
+       gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
+       plat_status_msg_busy_next(msg);\r
 }\r
 \r
-static void RunEvents(unsigned int which)\r
+static void vid_reset_mode(void)\r
 {\r
-       if(which & 0x1800) { // save or load (but not both)\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
-                       clearArea(0);\r
+       int gp2x_mode = 16;\r
+       int renderer = get_renderer();\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
+\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
-               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
-                       emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
-                       PicoStateProgressCB = NULL;\r
+               else {\r
+                       PicoDrawSetOutFormat(PDF_NONE, 0);\r
+                       PicoDraw32xSetFrameMode(1, 0);\r
                }\r
-\r
-               reset_timing = 1;\r
+               PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
+               gp2x_mode = 16;\r
        }\r
-       if(which & 0x0400) { // switch renderer\r
-               if      (  PicoOpt&0x10)             { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
-               else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
-               else   currentConfig.EmuOpt &= ~0x80;\r
 \r
-               vidResetMode();\r
-\r
-               if (PicoOpt&0x10) {\r
-                       strcpy(noticeMsg, " 8bit fast renderer");\r
-               } else if (currentConfig.EmuOpt&0x80) {\r
-                       strcpy(noticeMsg, "16bit accurate renderer");\r
-               } else {\r
-                       strcpy(noticeMsg, " 8bit accurate renderer");\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
-\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-       if(which & 0x0300) {\r
-               if(which&0x0200) {\r
-                       state_slot -= 1;\r
-                       if(state_slot < 0) state_slot = 9;\r
-               } else {\r
-                       state_slot += 1;\r
-                       if(state_slot > 9) state_slot = 0;\r
+               else if (renderer == RT_8BIT_ACC) {\r
+                       emu_scan_begin = EmuScanBegin8_rot;\r
+                       emu_scan_end = EmuScanEnd8_rot;\r
                }\r
-               sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
-               gettimeofday(&noticeMsgTime, 0);\r
+               else if (renderer == RT_8BIT_FAST)\r
+                       vidcpyM2 = vidcpy_m2_rot;\r
        }\r
-       if(which & 0x0080) {\r
-               engineState = PGS_Menu;\r
-       }\r
-}\r
 \r
+       PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);\r
 \r
-static void updateKeys(void)\r
-{\r
-       unsigned long keys, allActions[2] = { 0, 0 }, events;\r
-       static unsigned long prevEvents = 0;\r
-       int joy, i;\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
-       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
+       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
+               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
+               localPal[0xf0] = 0x00ffffff;\r
+               gp2x_video_setpalette(localPal, 0x100);\r
+               gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
        }\r
+       else\r
+               gp2x_memset_all_buffers(0, 0, 320*240*2);\r
 \r
-       keys &= CONFIGURABLE_KEYS;\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)\r
+               gp2x_mode = -gp2x_mode;\r
 \r
-       for (i = 0; i < 32; i++)\r
-       {\r
-               if (keys & (1 << i)) {\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
-                               // let's try to find the other one\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
-                                                       break;\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
-                       } else {\r
-                               allActions[pl] |= acts;\r
-                       }\r
-               }\r
-       }\r
+       gp2x_video_changemode(gp2x_mode);\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 keys = gp2x_usbjoy_check2(joy);\r
-                       for (i = 0; i < 32; i++) {\r
-                               if (keys & (1 << i)) {\r
-                                       int acts = currentConfig.JoyBinds[joy][i];\r
-                                       int pl = (acts >> 16) & 1;\r
-                                       allActions[pl] |= acts;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
+       Pico.m.dirtyPal = 1;\r
 \r
-       PicoPad[0] = (unsigned short) allActions[0];\r
-       PicoPad[1] = (unsigned short) allActions[1];\r
+       PicoOpt &= ~POPT_EN_SOFTSCALE;\r
+       if (currentConfig.scaling == EOPT_SCALE_SW)\r
+               PicoOpt |= POPT_EN_SOFTSCALE;\r
 \r
-       events = (allActions[0] | allActions[1]) >> 16;\r
+       // palette converters for 8bit modes\r
+       make_local_pal = (PicoAHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r
+}\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
-       }\r
+void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
+{\r
+       int scalex = 320, scaley = 240;\r
+       int ln_offs = 0;\r
 \r
-       events &= ~prevEvents;\r
-       if (events) RunEvents(events);\r
-       if (movie_data) emu_updateMovie();\r
+       if (doing_bg_frame)\r
+               return;\r
 \r
-       prevEvents = (allActions[0] | allActions[1]) >> 16;\r
-}\r
+       osd_fps_x = OSD_FPS_X;\r
+       osd_y = 232;\r
 \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
-static void updateSound(int len)\r
-{\r
-       if (PicoOpt&8) len<<=1;\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
-       /* avoid writing audio when lagging behind to prevent audio lag */\r
-       if (PicoSkipFrame != 2)\r
-               gp2x_sound_write(PsndOut, len<<1);\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
-static void SkipFrame(int do_audio)\r
-{\r
-       PicoSkipFrame=do_audio ? 1 : 2;\r
-       PicoFrame();\r
-       PicoSkipFrame=0;\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
-\r
-void emu_forcedFrame(void)\r
+void plat_video_toggle_renderer(int change, int is_menu_call)\r
 {\r
-       int po_old = PicoOpt;\r
-       int eo_old = currentConfig.EmuOpt;\r
+       change_renderer(change);\r
 \r
-       PicoOpt &= ~0x0010;\r
-       PicoOpt |=  0x4080; // soft_scale | acc_sprites\r
-       currentConfig.EmuOpt |= 0x80;\r
+       if (is_menu_call)\r
+               return;\r
 \r
-       //vidResetMode();\r
-       PicoDrawSetColorFormat(1);\r
-       PicoScan = EmuScan16;\r
-       PicoScan(0, 0);\r
-       Pico.m.dirtyPal = 1;\r
-       PicoFrameDrawOnly();\r
+       vid_reset_mode();\r
+       rendstatus_old = -1;\r
 \r
-/*\r
-       if (!(Pico.video.reg[12]&1)) {\r
-               vidCpyM2 = vidCpyM2_32col;\r
-               clearArea(1);\r
-       } else  vidCpyM2 = vidCpyM2_40col;\r
-\r
-       vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
-       vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-       gp2x_video_setpalette(localPal, 0x40);\r
-*/\r
-       PicoOpt = po_old;\r
-       currentConfig.EmuOpt = eo_old;\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
-static void simpleWait(int thissec, int lim_time)\r
+#if 0 // TODO\r
+static void RunEventsPico(unsigned int events)\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
+       int ret, px, py, lim_x;\r
+       static int pdown_frames = 0;\r
 \r
-       while(tval.tv_usec < lim_time)\r
+       // for F200\r
+       ret = gp2x_touchpad_read(&px, &py);\r
+       if (ret >= 0)\r
        {\r
-               spend_cycles(1024);\r
-               gettimeofday(&tval, 0);\r
-               if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\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
+#endif\r
 \r
-\r
-void emu_Loop(void)\r
+void plat_update_volume(int has_changed, int is_up)\r
 {\r
-       static int gp2x_old_clock = 200;\r
-       static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_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
-       char *notice = 0;\r
+       static int prev_frame = 0, wait_frames = 0;\r
+       int vol = currentConfig.volume;\r
+       int need_low_volume = 0;\r
+       gp2x_soc_t soc;\r
 \r
-       printf("entered emu_Loop()\n");\r
+       soc = soc_detect();\r
+       if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r
+               need_low_volume = 1;\r
 \r
-       if (gp2x_old_clock != currentConfig.CPUclock) {\r
-               printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
-               set_FCLK(currentConfig.CPUclock);\r
-               gp2x_old_clock = currentConfig.CPUclock;\r
-               printf(" done\n");\r
+       if (has_changed)\r
+       {\r
+               if (need_low_volume && vol < 5 && 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
+                       sndout_oss_setvol(vol, vol);\r
+                       currentConfig.volume = vol;\r
+               }\r
+               emu_status_msg("VOL: %02i", vol);\r
+               prev_frame = Pico.m.frame_count;\r
        }\r
 \r
-       if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
-               set_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
+       if (!need_low_volume)\r
+               return;\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
+       /* set the right mixer func */\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
-       EmuOpt_old = currentConfig.EmuOpt;\r
-       fpsbuff[0] = 0;\r
+static void oss_write_nonblocking(int len)\r
+{\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
-       // make sure we are in correct mode\r
-       vidResetMode();\r
-       scaling_update();\r
-       Pico.m.dirtyPal = 1;\r
-       oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
-       find_combos();\r
+       sndout_oss_write_nb(PsndOut, len);\r
+}\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
+void pemu_sound_start(void)\r
+{\r
+       static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
+\r
+       PsndOut = NULL;\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
-                       sound_rerate(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
+       if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
+       {\r
+               int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\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
+\r
                PsndRate_old = PsndRate;\r
-               PsndLen_real = PsndLen;\r
                PicoOpt_old  = PicoOpt;\r
                pal_old = Pico.m.pal;\r
-       } else {\r
-               PsndOut = 0;\r
-       }\r
-\r
-       // prepare CD buffer\r
-       if (PicoMCD & 1) PicoCDBufferInit();\r
-\r
-       // calc vsync offset to sync timing code with vsync\r
-       if (currentConfig.EmuOpt&0x2000) {\r
-               gettimeofday(&tval, 0);\r
-               gp2x_video_wait_vsync();\r
-               gettimeofday(&tval, 0);\r
-               vsync_offset = tval.tv_usec;\r
-               while (vsync_offset >= target_frametime)\r
-                       vsync_offset -= target_frametime;\r
-               if (!vsync_offset) vsync_offset++;\r
-               printf("vsync_offset: %i\n", vsync_offset);\r
-       } else\r
-               vsync_offset = 0;\r
-\r
-       // loop?\r
-       while (engineState == PGS_Running)\r
-       {\r
-               int modes;\r
+       }\r
+}\r
 \r
-               gettimeofday(&tval, 0);\r
-               if(reset_timing) {\r
-                       reset_timing = 0;\r
-                       thissec = tval.tv_sec;\r
-                       frames_shown = frames_done = tval.tv_usec/target_frametime;\r
-               }\r
+static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };\r
 \r
-               // show notice message?\r
-               if(noticeMsgTime.tv_sec) {\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
-                               clearArea(0);\r
-                               notice = 0;\r
-                       } else {\r
-                               int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
-                               if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
-                               notice = noticeMsg;\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
-               // check for mode changes\r
-               modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
-               if (modes != oldmodes) {\r
-                       int scalex = 320;\r
-                       osd_fps_x = OSD_FPS_X;\r
-                       if (modes & 4) {\r
-                               vidCpyM2 = vidCpyM2_40col;\r
-                       } else {\r
-                               if (PicoOpt & 0x100) {\r
-                                       vidCpyM2 = vidCpyM2_32col_nobord;\r
-                                       scalex = 256;\r
-                                       osd_fps_x = OSD_FPS_X - 64;\r
-                               } else {\r
-                                       vidCpyM2 = vidCpyM2_32col;\r
-                               }\r
-                       }\r
-                       if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
-                            gp2x_video_RGB_setscaling(8, scalex, 224);\r
-                       else gp2x_video_RGB_setscaling(0, scalex, 240);\r
-                       oldmodes = modes;\r
-                       clearArea(1);\r
-               }\r
+void pemu_sound_wait(void)\r
+{\r
+       // don't need to do anything, writes will block by themselves\r
+}\r
 \r
-               // second changed?\r
-               if(thissec != tval.tv_sec) {\r
-#ifdef BENCHMARK\r
-                       static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
-                       if(++bench == 10) {\r
-                               bench = 0;\r
-                               bench_fps_s = bench_fps;\r
-                               bf[bfp++ & 3] = bench_fps;\r
-                               bench_fps = 0;\r
-                       }\r
-                       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
-                               sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
-#endif\r
-                       thissec = tval.tv_sec;\r
+void pemu_forced_frame(int no_scale, int do_emu)\r
+{\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
 \r
-                       if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
-                               frames_done = frames_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
-                                       updateKeys();\r
-                                       SkipFrame(1); frames_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
-                       }\r
-               }\r
-#ifdef PFRAMES\r
-               sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
-#endif\r
+       emu_cmn_forced_frame(no_scale, do_emu);\r
 \r
-               lim_time = (frames_done+1) * target_frametime + vsync_offset;\r
-               if(currentConfig.Frameskip >= 0) { // frameskip enabled\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
-                                       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
-                                       }\r
-                               }\r
-                               lim_time += target_frametime;\r
-                       }\r
-               } else if(tval.tv_usec > lim_time) { // auto frameskip\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
-                                * try to recover by resetting timing... */\r
-                               reset_timing = 1;\r
-                               continue;\r
-                       }\r
-                       updateKeys();\r
-                       SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;\r
-                       continue;\r
-               }\r
+       g_menubg_src_ptr = g_screen_ptr;\r
+       doing_bg_frame = 0;\r
+}\r
 \r
-               updateKeys();\r
-               PicoFrame();\r
+void plat_debug_cat(char *str)\r
+{\r
+}\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
+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 *)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 *)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
+               fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
+               fwrite(oldscr, 1, 320*240*2, f);\r
+               fclose(f);\r
+       }\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
-\r
-               if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
-                       reset_timing = 1;\r
-               else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
-               {\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
-                       {\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
-                                       gp2x_video_wait_vsync();\r
-                               } else {\r
-                                       simpleWait(thissec, lim_time);\r
-                               }\r
-                       }\r
-               }\r
+void pemu_loop_prep(void)\r
+{\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
-               blit(fpsbuff, notice);\r
+       soc = soc_detect();\r
 \r
-               frames_done++; frames_shown++;\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 && 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 (PicoMCD & 1) PicoCDBufferFree();\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
-       // save SRAM\r
-       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
-               emu_state_cb("Writing SRAM/BRAM..");\r
-               emu_SaveLoadGame(0, 1);\r
-               SRam.changed = 0;\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 if (EmuOpt_old & EOPT_VSYNC)\r
+                       unset_lcd_custom_rate();\r
        }\r
 \r
-       // if in 8bit mode, generate 16bit image for menu background\r
-       if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
-               emu_forcedFrame();\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
-void emu_ResetGame(void)\r
-{\r
-       PicoReset(0);\r
-       reset_timing = 1;\r
+       EmuOpt_old = currentConfig.EmuOpt;\r
+       pal_old = Pico.m.pal;\r
+\r
+       // make sure we are in correct mode\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
+       pemu_sound_stop();\r
+\r
+       /* do one more frame for menu bg */\r
+       pemu_forced_frame(0, 1);\r
+}\r
 \r