bugfixes, r171 release
[fceu.git] / drivers / gp2x / menu.c
index a4c2a32..ee599b4 100644 (file)
@@ -1,7 +1,5 @@
+// menu system for gpfce - FCE Ultra port\r
 // (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
 #include <stdio.h>\r
 #include <string.h>\r
 \r
 #include "../../input.h"\r
 #include "../../state.h"\r
+#include "../../palette.h"\r
+#include "readpng.h"\r
 \r
 #ifndef _DIRENT_HAVE_D_TYPE\r
 #error "need d_type for file browser\r
 #endif\r
 \r
-static int GP2X_PORT_REV =\r
-#include "rev.h"\r
-;\r
-\r
+extern int GP2X_PORT_REV;\r
 extern char lastLoadedGameName[PATH_MAX];\r
 extern int mmuhack_status;\r
 extern int soundvol;\r
@@ -44,12 +41,75 @@ static char *gp2xKeyNames[] = {
        "18???", "19???",  "1a???","PUSH",  "1c???","1d???", "1e???",    "1f???"\r
 };\r
 \r
+\r
+static char path_buffer[PATH_MAX];\r
+static unsigned short *menu_bg = 0;\r
+static int txt_xmin, txt_xmax, txt_ymin, txt_ymax;\r
+\r
 char menuErrorMsg[40] = {0, };\r
 \r
-// TODO\r
-void gp2x_fceu_copy_bg(void)\r
+static void menu_flip(void)\r
+{\r
+       gp2x_video_flush_cache();\r
+       gp2x_video_flip();\r
+}\r
+\r
+static void menu_darken_reset(void)\r
+{\r
+       txt_xmin = 320; txt_xmax = 0;\r
+       txt_ymin = 240; txt_ymax = 0;\r
+}\r
+\r
+static void gp2x_fceu_copy_bg(void)\r
+{\r
+       if (menu_bg)\r
+            memcpy(gp2x_screen, menu_bg, 320*240*2);\r
+       else memset(gp2x_screen, 0, 320*240*2);\r
+       menu_darken_reset();\r
+}\r
+\r
+static void menu_darken_text_bg(void)\r
+{\r
+       int x, y, xmin, xmax, ymax;\r
+       unsigned short *screen = gp2x_screen;\r
+\r
+       xmin = txt_xmin - 3;\r
+       if (xmin < 0) xmin = 0;\r
+       xmax = txt_xmax + 2;\r
+       if (xmax > 319) xmax = 319;\r
+\r
+       y = txt_ymin - 3;\r
+       if (y < 0) y = 0;\r
+       ymax = txt_ymax + 2;\r
+       if (ymax > 239) ymax = 239;\r
+\r
+       for (x = xmin; x <= xmax; x++)\r
+               screen[y*320+x] = 0xa514;\r
+       for (y++; y < ymax; y++)\r
+       {\r
+               screen[y*320+xmin] = 0xffff;\r
+               for (x = xmin+1; x < xmax; x++)\r
+               {\r
+                       unsigned int p = screen[y*320+x];\r
+                       if (p != 0xffff)\r
+                               screen[y*320+x] = ((p&0xf79e)>>1) - ((p&0xc618)>>3);\r
+               }\r
+               screen[y*320+xmax] = 0xffff;\r
+       }\r
+       for (x = xmin; x <= xmax; x++)\r
+               screen[y*320+x] = 0xffff;\r
+}\r
+\r
+static void gp2x_fceu_darken_all(void)\r
 {\r
-       memset(gp2x_screen, 0, 320*240*2);\r
+       unsigned int *screen = gp2x_screen;\r
+       int count = 320*240/2;\r
+\r
+       while (count--)\r
+       {\r
+               unsigned int p = screen[count];\r
+               screen[count] = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
+       }\r
 }\r
 \r
 // draws white text to current bbp15 screen\r
@@ -75,6 +135,10 @@ static void gp2x_text_out15_(int x, int y, const char *text)
                }\r
                screen += 8;\r
        }\r
+       if (x < txt_xmin) txt_xmin = x;\r
+       if (x+i*8 > txt_xmax) txt_xmax = x+i*8;\r
+       if (y < txt_ymin) txt_ymin = y;\r
+       if (y+8   > txt_ymax) txt_ymax = y+8;\r
 }\r
 \r
 void gp2x_text_out15(int x, int y, const char *texto, ...)\r
@@ -102,7 +166,7 @@ void gp2x_text_out15_lim(int x, int y, const char *texto, int max)
        gp2x_text_out15(x,y,buffer);\r
 }\r
 \r
-static void gp2x_smalltext16(int x, int y, const char *texto)\r
+static void gp2x_smalltext16(int x, int y, const char *texto, unsigned short color)\r
 {\r
        int i;\r
        unsigned char  *src;\r
@@ -123,7 +187,7 @@ static void gp2x_smalltext16(int x, int y, const char *texto)
                        int w = 0x20;\r
                        while (w)\r
                        {\r
-                               if( *src & w ) *dst = 0xffff;\r
+                               if( *src & w ) *dst = color;\r
                                dst++;\r
                                w>>=1;\r
                        }\r
@@ -134,7 +198,7 @@ static void gp2x_smalltext16(int x, int y, const char *texto)
        }\r
 }\r
 \r
-static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
+static void gp2x_smalltext16_lim(int x, int y, const char *texto, unsigned short color, int max)\r
 {\r
        char    buffer[320/6+1];\r
 \r
@@ -143,7 +207,7 @@ static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)
        if (max < 0) max = 0;\r
        buffer[max] = 0;\r
 \r
-       gp2x_smalltext16(x, y, buffer);\r
+       gp2x_smalltext16(x, y, buffer, color);\r
 }\r
 \r
 \r
@@ -235,6 +299,22 @@ static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)
 \r
 // -------------- ROM selector --------------\r
 \r
+// rrrr rggg gggb bbbb\r
+static unsigned short file2color(const char *fname)\r
+{\r
+       const char *ext = fname + strlen(fname) - 3;\r
+       static const char *rom_exts[]   = { "zip", "nes", "fds", "unf", "nez", "nif" }; // nif is for unif\r
+       static const char *other_exts[] = { "nsf", "ips", "fcm" };\r
+       int i;\r
+\r
+       if (ext < fname) ext = fname;\r
+       for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
+               if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff;\r
+       for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)\r
+               if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;\r
+       return 0xffff;\r
+}\r
+\r
 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
 {\r
        int start, i, pos;\r
@@ -242,24 +322,25 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
        start = 12 - sel;\r
        n--; // exclude current dir (".")\r
 \r
-       //memset(gp2x_screen, 0, 320*240);\r
        gp2x_fceu_copy_bg();\r
+       gp2x_fceu_darken_all();\r
 \r
        if(start - 2 >= 0)\r
-               gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);\r
+               gp2x_smalltext16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);\r
        for (i = 0; i < n; i++) {\r
                pos = start + i;\r
                if (pos < 0)  continue;\r
                if (pos > 23) break;\r
                if (namelist[i+1]->d_type == DT_DIR) {\r
-                       gp2x_smalltext8_lim(14,   pos*10, "/", 1);\r
-                       gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
+                       gp2x_smalltext16_lim(14,   pos*10, "/", 0xfff6, 1);\r
+                       gp2x_smalltext16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xfff6, 53-3);\r
                } else {\r
-                       gp2x_smalltext8_lim(14,   pos*10, namelist[i+1]->d_name, 53-2);\r
+                       unsigned short color = file2color(namelist[i+1]->d_name);\r
+                       gp2x_smalltext16_lim(14,   pos*10, namelist[i+1]->d_name, color, 53-2);\r
                }\r
        }\r
        gp2x_text_out15(5, 120, ">");\r
-       gp2x_video_flip();\r
+       menu_flip();\r
 }\r
 \r
 static int scandir_cmp(const void *p1, const void *p2)\r
@@ -293,11 +374,11 @@ static int scandir_filter(const struct dirent *ent)
        return 1;\r
 }\r
 \r
-static char *romsel_loop(char *curr_path)\r
+static char *filesel_loop(char *curr_path, char *final_dest)\r
 {\r
        struct dirent **namelist;\r
        DIR *dir;\r
-       int n, sel = 0;\r
+       int n, newlen, sel = 0;\r
        unsigned long inp = 0;\r
        char *ret = NULL, *fname = NULL;\r
 \r
@@ -346,15 +427,18 @@ static char *romsel_loop(char *curr_path)
                if(inp & GP2X_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
                if(inp & GP2X_B)     { // enter dir/select\r
                        again:\r
-                       if (namelist[sel+1]->d_type == DT_REG) {\r
-                               strcpy(lastLoadedGameName, curr_path);\r
-                               strcat(lastLoadedGameName, "/");\r
-                               strcat(lastLoadedGameName, namelist[sel+1]->d_name);\r
-                               ret = lastLoadedGameName;\r
+                       newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
+                       if (namelist[sel+1]->d_type == DT_REG) { // file selected\r
+                               if (final_dest == NULL) final_dest = malloc(newlen);\r
+                               if (final_dest == NULL) break;\r
+                               strcpy(final_dest, curr_path);\r
+                               strcat(final_dest, "/");\r
+                               strcat(final_dest, namelist[sel+1]->d_name);\r
+                               ret = final_dest;\r
                                break;\r
                        } else if (namelist[sel+1]->d_type == DT_DIR) {\r
-                               int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
                                char *p, *newdir = malloc(newlen);\r
+                               if (newdir == NULL) break;\r
                                if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
                                        char *start = curr_path;\r
                                        p = start + strlen(start) - 1;\r
@@ -365,20 +449,23 @@ static char *romsel_loop(char *curr_path)
                                } else {\r
                                        strcpy(newdir, curr_path);\r
                                        p = newdir + strlen(newdir) - 1;\r
-                                       while (*p == '/' && p >= newdir) *p-- = 0;\r
+                                       while (p >= newdir && *p == '/') *p-- = 0;\r
                                        strcat(newdir, "/");\r
                                        strcat(newdir, namelist[sel+1]->d_name);\r
                                }\r
-                               ret = romsel_loop(newdir);\r
+                               ret = filesel_loop(newdir, final_dest);\r
                                free(newdir);\r
                                break;\r
                        } else {\r
                                // unknown file type, happens on NTFS mounts. Try to guess.\r
-                               FILE *tstf; int tmp;\r
-                               strcpy(lastLoadedGameName, curr_path);\r
-                               strcat(lastLoadedGameName, "/");\r
-                               strcat(lastLoadedGameName, namelist[sel+1]->d_name);\r
-                               tstf = fopen(lastLoadedGameName, "rb");\r
+                               char *tstfn; FILE *tstf; int tmp;\r
+                               tstfn = malloc(newlen);\r
+                               if (tstfn == NULL) break;\r
+                               strcpy(tstfn, curr_path);\r
+                               strcat(tstfn, "/");\r
+                               strcat(tstfn, namelist[sel+1]->d_name);\r
+                               tstf = fopen(tstfn, "rb");\r
+                               free(tstfn);\r
                                if (tstf != NULL)\r
                                {\r
                                        if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
@@ -402,47 +489,69 @@ static char *romsel_loop(char *curr_path)
 \r
 // ------------ patch/gg menu ------------\r
 \r
-#if 0 // TODO?\r
-static void draw_patchlist(int sel)\r
+extern void *cheats;\r
+static int cheat_count = 0, cheat_start, cheat_pos;\r
+\r
+static int countcallb(char *name, uint32 a, uint8 v, int compare, int s, int type, void *data)\r
 {\r
-       int start, i, pos;\r
+       cheat_count++;\r
+       return 1;\r
+}\r
 \r
-       start = 12 - sel;\r
+static int clistcallb(char *name, uint32 a, uint8 v, int compare, int s, int type, void *data)\r
+{\r
+       int pos;\r
+\r
+       pos = cheat_start + cheat_pos;\r
+       cheat_pos++;\r
+       if (pos < 0)  return 1;\r
+       if (pos > 23) return 0;\r
+\r
+       gp2x_smalltext16_lim(14,     pos*10, s ? "ON " : "OFF", 0xffff, 3);\r
+       gp2x_smalltext16_lim(14+6*4, pos*10, type ? "S" : "R", 0xffff, 1);\r
+       gp2x_smalltext16_lim(14+6*6, pos*10, name, 0xffff, 53-8);\r
+\r
+       return 1;\r
+}\r
+\r
+static void draw_patchlist(int sel)\r
+{\r
+       int pos;\r
 \r
        gp2x_fceu_copy_bg();\r
+       gp2x_fceu_darken_all();\r
 \r
-       for (i = 0; i < PicoPatchCount; i++) {\r
-               pos = start + i;\r
-               if (pos < 0)  continue;\r
-               if (pos > 23) break;\r
-               gp2x_smalltext8_lim(14,     pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);\r
-               gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);\r
-       }\r
-       pos = start + i;\r
-       if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);\r
+       cheat_start = 12 - sel;\r
+       cheat_pos = 0;\r
+       FCEUI_ListCheats(clistcallb,0);\r
+\r
+       pos = cheat_start + cheat_pos;\r
+       if (pos < 24) gp2x_smalltext16_lim(14, pos*10, "done", 0xffff, 4);\r
 \r
        gp2x_text_out15(5, 120, ">");\r
-       gp2x_video_flip();\r
+       menu_flip();\r
 }\r
 \r
-\r
 void patches_menu_loop(void)\r
 {\r
        int menu_sel = 0;\r
        unsigned long inp = 0;\r
 \r
+       cheat_count = 0;\r
+       FCEUI_ListCheats(countcallb,0);\r
+\r
        for(;;)\r
        {\r
                draw_patchlist(menu_sel);\r
                inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
-               if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
-               if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
+               if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = cheat_count; }\r
+               if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > cheat_count) menu_sel = 0; }\r
                if(inp &(GP2X_LEFT|GP2X_L))  { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
-               if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
+               if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > cheat_count) menu_sel = cheat_count; }\r
                if(inp & GP2X_B) { // action\r
-                       if (menu_sel < PicoPatchCount)\r
-                               PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
-                       else    return;\r
+                       if (menu_sel < cheat_count)\r
+                            FCEUI_ToggleCheat(menu_sel);\r
+                       else return;\r
                }\r
                if(inp & GP2X_X) return;\r
        }\r
@@ -451,6 +560,7 @@ void patches_menu_loop(void)
 \r
 // ------------ savestate loader ------------\r
 \r
+#if 0\r
 static void menu_prepare_bg(void);\r
 \r
 static int state_slot_flags = 0;\r
@@ -544,7 +654,7 @@ static void draw_savestate_menu(int menu_sel, int is_loading)
        // draw cursor\r
        gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
 \r
-       gp2x_video_flip();\r
+       menu_flip();\r
 }\r
 \r
 static int savestate_menu_loop(int is_loading)\r
@@ -620,7 +730,7 @@ static char *action_binds(int player_idx, int action_mask)
                {\r
                        if (Settings.JoyBinds[joy][i] & action_mask)\r
                        {\r
-                               if (player_idx >= 0 && ((Settings.KeyBinds[i] >> 16) & 3) != player_idx) continue;\r
+                               if (player_idx >= 0 && ((Settings.JoyBinds[joy][i] >> 16) & 3) != player_idx) continue;\r
                                if (strkeys[0]) {\r
                                        strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
                                        break;\r
@@ -644,15 +754,14 @@ static void unbind_action(int action)
                        Settings.JoyBinds[u][i] &= ~action;\r
 }\r
 \r
-static int count_bound_keys(int action, int is_joy)\r
+static int count_bound_keys(int action, int joy)\r
 {\r
-       int i, u, keys = 0;\r
+       int i, keys = 0;\r
 \r
-       if (is_joy)\r
+       if (joy)\r
        {\r
-               for (u = 0; u < 4; u++)\r
-                       for (i = 0; i < 32; i++)\r
-                               if (Settings.JoyBinds[u][i] & action) keys++;\r
+               for (i = 0; i < 32; i++)\r
+                       if (Settings.JoyBinds[joy-1][i] & action) keys++;\r
        }\r
        else\r
        {\r
@@ -682,15 +791,21 @@ static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_i
        // draw cursor\r
        gp2x_text_out15(x - 16, tl_y + sel*10, ">");\r
 \r
+       menu_darken_text_bg();\r
+       menu_darken_reset();\r
+\r
        if (sel < opt_cnt) {\r
                gp2x_text_out15(30, 190, "Press a button to bind/unbind");\r
                gp2x_text_out15(30, 200, "Use VOL+ to clear");\r
                gp2x_text_out15(30, 210, "To bind UP/DOWN, hold VOL-");\r
                gp2x_text_out15(30, 220, "Select \"Done\" to exit");\r
        } else {\r
+               gp2x_text_out15(30, 200, "Use Options -> Save cfg");\r
+               gp2x_text_out15(30, 210, "to save controls");\r
                gp2x_text_out15(30, 220, "Press B or X to exit");\r
        }\r
-       gp2x_video_flip();\r
+       menu_darken_text_bg();\r
+       menu_flip();\r
 }\r
 \r
 static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)\r
@@ -726,10 +841,12 @@ static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_i
                                                Settings.KeyBinds[i] |= player_idx << 16;\r
                                        }\r
                                }\r
-               } else if (sel < opt_cnt) {\r
+               }\r
+               else if (sel < opt_cnt)\r
+               {\r
                        for (i = 0; i < 32; i++)\r
                                if (inp & (1 << i)) {\r
-                                       if (count_bound_keys(opts[sel].mask, 1) >= 1) // disallow combos for usbjoy\r
+                                       if (count_bound_keys(opts[sel].mask, joy) >= 1) // disallow combos for usbjoy\r
                                             Settings.JoyBinds[joy-1][i] &= ~opts[sel].mask;\r
                                        else Settings.JoyBinds[joy-1][i] ^=  opts[sel].mask;\r
                                        if (player_idx >= 0) {\r
@@ -767,7 +884,8 @@ static void draw_kc_sel(int menu_sel)
                gp2x_text_out15(tl_x, (y+=10), "none");\r
        }\r
 \r
-       gp2x_video_flip();\r
+       menu_darken_text_bg();\r
+       menu_flip();\r
 }\r
 \r
 // b_turbo,a_turbo  RLDU SEBA\r
@@ -787,9 +905,13 @@ static bind_action_t ctrl_actions[] =
 \r
 static bind_action_t emuctrl_actions[] =\r
 {\r
-       { "Save State     ", 1<<31 },\r
-       { "Load State     ", 1<<30 },\r
-       { "Next State Slot", 1<<29 },\r
+       { "Save State       ", 1<<31 },\r
+       { "Load State       ", 1<<30 },\r
+       { "Next State Slot  ", 1<<29 },\r
+       { "Prev State Slot  ", 1<<28 },\r
+       { "FDS Insert/Eject ", 1<<27 },\r
+       { "FDS Select Disk  ", 1<<26 },\r
+       { "VSUni Insert Coin", 1<<25 },\r
 };\r
 \r
 static void kc_sel_loop(void)\r
@@ -806,9 +928,10 @@ static void kc_sel_loop(void)
                if(inp & GP2X_B) {\r
                        switch (menu_sel) {\r
                                case 0: key_config_loop(ctrl_actions, 10, 0); return;\r
-                               case 1: key_config_loop(ctrl_actions,  8, 1); return;\r
+                               case 1: key_config_loop(ctrl_actions, 10, 1); return;\r
                                case 2: key_config_loop(emuctrl_actions,\r
                                                sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;\r
+                               case 3: if (!fceugi) SaveConfig(NULL); return;\r
                                default: return;\r
                        }\r
                }\r
@@ -819,44 +942,130 @@ static void kc_sel_loop(void)
 \r
 // --------- FCEU options ----------\r
 \r
+extern int ntsccol,ntschue,ntsctint;\r
+extern int srendlinev[2];\r
+extern int erendlinev[2];\r
+extern int eoptions;\r
+extern char *cpalette;\r
+extern void LoadCPalette(void);\r
+\r
+\r
+static void int_incdec(int *p, int inc, int min, int max)\r
+{\r
+       *p += inc;\r
+       if      (*p < min) *p = min;\r
+       else if (*p > max) *p = max;\r
+}\r
+\r
 static void draw_fcemenu_options(int menu_sel)\r
 {\r
        int tl_x = 25, tl_y = 60, y;\r
+       char cpal[32];\r
+\r
+       if (cpalette != NULL)\r
+       {\r
+               char *p = cpalette + strlen(cpalette) - 1;\r
+               while (*p != '/' && p > cpalette) p--;\r
+               if (*p == '/') p++;\r
+               strncpy(cpal, p, 16);\r
+               cpal[16] = 0;\r
+       }\r
+       else strcpy(cpal, "           OFF");\r
 \r
        y = tl_y;\r
        gp2x_fceu_copy_bg();\r
 \r
-       gp2x_text_out15(tl_x,  y,      "                           %s", "OFF"); // 0\r
-       gp2x_text_out15(tl_x, (y+=10), "Done");\r
+       gp2x_text_out15(tl_x,  y,      "Custom palette: %s", cpal);                             // 0\r
+       gp2x_text_out15(tl_x, (y+=10), "NTSC Color Emulation       %s", ntsccol?"ON":"OFF");\r
+       gp2x_text_out15(tl_x, (y+=10), "  Tint (default: 56)       %i", ntsctint);\r
+       gp2x_text_out15(tl_x, (y+=10), "  Hue  (default: 72)       %i", ntschue);\r
+       gp2x_text_out15(tl_x, (y+=10), "First visible line (NTSC)  %i", srendlinev[0]);\r
+       gp2x_text_out15(tl_x, (y+=10), "Last visible line (NTSC)   %i", erendlinev[0]);         // 5\r
+       gp2x_text_out15(tl_x, (y+=10), "First visible line (PAL)   %i", srendlinev[1]);\r
+       gp2x_text_out15(tl_x, (y+=10), "Last visible line (PAL)    %i", erendlinev[1]);\r
+       gp2x_text_out15(tl_x, (y+=10), "Clip 8 left/right columns  %s", (eoptions&EO_CLIPSIDES)?"ON":"OFF");\r
+       gp2x_text_out15(tl_x, (y+=10), "Disable 8 sprite limit     %s", (eoptions&EO_NO8LIM)?"ON":"OFF");\r
+       gp2x_text_out15(tl_x, (y+=10), "Enable authentic GameGenie %s", (eoptions&EO_GG)?"ON":"OFF");\r
+       gp2x_text_out15(tl_x, (y+=10), "Done");                                                 // 11\r
 \r
        // draw cursor\r
        gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
 \r
-       gp2x_video_flip();\r
+       if (menu_sel == 0) {\r
+               menu_darken_text_bg();\r
+               menu_darken_reset();\r
+\r
+               gp2x_text_out15(30, 210, "Press B to browse,");\r
+               gp2x_text_out15(30, 220, "START to use default");\r
+       }\r
+\r
+       menu_darken_text_bg();\r
+       menu_flip();\r
 }\r
 \r
 static void fcemenu_loop_options(void)\r
 {\r
-       int menu_sel = 0, menu_sel_max = 1;\r
+       int menu_sel = 0, menu_sel_max = 11, i;\r
        unsigned long inp = 0;\r
 \r
+       FCEUI_GetNTSCTH(&ntsctint, &ntschue);\r
+\r
        for(;;)\r
        {\r
                draw_fcemenu_options(menu_sel);\r
-               inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
+               inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);\r
                if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
                if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
                if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
                        switch (menu_sel) {\r
-                               case  0: break;\r
-                               case  1: return;\r
+                               case  1: ntsccol = !ntsccol; break;\r
+                               case  8: eoptions^=EO_CLIPSIDES; break;\r
+                               case  9: eoptions^=EO_NO8LIM; break;\r
+                               case 10: eoptions^=EO_GG; break;\r
+                               case 11: return;\r
+                       }\r
+               }\r
+               if(inp & (GP2X_X|GP2X_A)) {\r
+                       for(i=0;i<2;i++)\r
+                       {\r
+                               if(srendlinev[i]<0 || srendlinev[i]>239) srendlinev[i]=0;\r
+                               if(erendlinev[i]<srendlinev[i] || erendlinev[i]>239) erendlinev[i]=239;\r
                        }\r
+                       FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);\r
+                       FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);\r
+                       FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);\r
+                       FCEUI_SetGameGenie(eoptions&EO_GG);\r
+                       if (cpalette) LoadCPalette();\r
+                       else FCEUI_SetPaletteArray(0); // set to default\r
+                       FCEU_ResetPalette();\r
+                       return;\r
                }\r
-               if(inp & (GP2X_X|GP2X_A)) return;\r
                if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
                        switch (menu_sel) {\r
-                               case 0:\r
-                                       break;\r
+                               case  2: int_incdec(&ntsctint,      (inp & GP2X_LEFT) ? -1 : 1, 0, 128); break;\r
+                               case  3: int_incdec(&ntschue,       (inp & GP2X_LEFT) ? -1 : 1, 0, 128); break;\r
+                               case  4: int_incdec(&srendlinev[0], (inp & GP2X_LEFT) ? -1 : 1, 0, 239); break;\r
+                               case  5: int_incdec(&erendlinev[0], (inp & GP2X_LEFT) ? -1 : 1, 0, 239); break;\r
+                               case  6: int_incdec(&srendlinev[1], (inp & GP2X_LEFT) ? -1 : 1, 0, 239); break;\r
+                               case  7: int_incdec(&erendlinev[1], (inp & GP2X_LEFT) ? -1 : 1, 0, 239); break;\r
+                       }\r
+               }\r
+               if(menu_sel == 0 && (inp & (GP2X_START|GP2X_B))) { // custom palette\r
+                       if ((inp & GP2X_START) && cpalette) {\r
+                               free(cpalette);\r
+                               cpalette=NULL;\r
+                       }\r
+                       else if (inp & GP2X_B) {\r
+                               char *selfname;\r
+                               if (cpalette) strncpy(path_buffer, cpalette, sizeof(path_buffer));\r
+                               else getcwd(path_buffer, PATH_MAX);\r
+                               path_buffer[sizeof(path_buffer)-1] = 0;\r
+\r
+                               selfname = filesel_loop(path_buffer, NULL);\r
+                               if (selfname) {\r
+                                       if (cpalette) free(cpalette);\r
+                                       cpalette = selfname;\r
+                               }\r
                        }\r
                }\r
        }\r
@@ -866,7 +1075,7 @@ static void fcemenu_loop_options(void)
 \r
 static void draw_menu_options(int menu_sel)\r
 {\r
-       int tl_x = 25, tl_y = 32, y;\r
+       int tl_x = 25, tl_y = 20, y;\r
        char strframeskip[8], *strscaling, *strssconfirm;\r
        char *mms = mmuhack_status ? "active)  " : "inactive)";\r
 \r
@@ -892,27 +1101,37 @@ static void draw_menu_options(int menu_sel)
        gp2x_text_out15(tl_x,  y,      "Scaling:       %s", strscaling);                                // 0\r
        gp2x_text_out15(tl_x, (y+=10), "Show FPS                   %s", Settings.showfps?"ON":"OFF");   // 1\r
        gp2x_text_out15(tl_x, (y+=10), "Frameskip                  %s", strframeskip);                  // 2\r
+       gp2x_text_out15(tl_x, (y+=10), "Accurate renderer (slow)   %s", Settings.accurate_mode?"ON":"OFF");\r
        gp2x_text_out15(tl_x, (y+=10), "Enable sound               %s", soundvol?"ON":"OFF");\r
-       gp2x_text_out15(tl_x, (y+=10), "Sound Rate:           %5iHz", Settings.sound_rate);             // 4\r
+       gp2x_text_out15(tl_x, (y+=10), "Sound Rate:           %5iHz", Settings.sound_rate);             // 5\r
        gp2x_text_out15(tl_x, (y+=10), "Force Region:              %s",\r
-               Settings.region_force == 2 ? "PAL" : Settings.region_force == 1 ? "NTSC" : "OFF");      // 5\r
-       gp2x_text_out15(tl_x, (y+=10), "Use SRAM savestates        %s", "OFF");\r
+               Settings.region_force == 2 ? "PAL" : Settings.region_force == 1 ? "NTSC" : "OFF");      // 6\r
        gp2x_text_out15(tl_x, (y+=10), "Turbo rate                 %iHz", (Settings.turbo_rate_add*60/2) >> 24);\r
        gp2x_text_out15(tl_x, (y+=10), "Confirm savestate          %s", strssconfirm);                  // 8\r
        gp2x_text_out15(tl_x, (y+=10), "Save slot                  %i", CurrentState);\r
        gp2x_text_out15(tl_x, (y+=10), "Faster RAM timings         %s", Settings.ramtimings?"ON":"OFF");\r
        gp2x_text_out15(tl_x, (y+=10), "squidgehack (now %s %s",   mms, Settings.mmuhack?"ON":"OFF");   // 11\r
        gp2x_text_out15(tl_x, (y+=10), "Gamma correction           %i.%02i", Settings.gamma / 100, Settings.gamma%100);\r
-       gp2x_text_out15(tl_x, (y+=10), "GP2X CPU clock             %iMhz", Settings.cpuclock);          // 13\r
+       gp2x_text_out15(tl_x, (y+=10), "Perfect VSYNC              %s", Settings.perfect_vsync?"ON":"OFF");\r
+       gp2x_text_out15(tl_x, (y+=10), "GP2X CPU clock             %iMhz", Settings.cpuclock);          // 14\r
        gp2x_text_out15(tl_x, (y+=10), "[FCE Ultra options]");\r
-       gp2x_text_out15(tl_x, (y+=10), "Save cfg as default");\r
+       gp2x_text_out15(tl_x, (y+=10), "Save cfg as default");                                          // 16\r
        if (fceugi)\r
                gp2x_text_out15(tl_x, (y+=10), "Save cfg for current game only");\r
 \r
        // draw cursor\r
        gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
 \r
-       gp2x_video_flip();\r
+       if (menu_sel == 3) {\r
+               gp2x_text_out15(tl_x, 210, "ROM reload required for this");\r
+               gp2x_text_out15(tl_x, 220, "setting to take effect");\r
+       } else if (menu_sel == 10 || menu_sel == 11) {\r
+               gp2x_text_out15(tl_x, 210, "Emu restart required for this");\r
+               gp2x_text_out15(tl_x, 220, "setting to take effect");\r
+       }\r
+\r
+       menu_darken_text_bg();\r
+       menu_flip();\r
 }\r
 \r
 static int sndrate_prevnext(int rate, int dir)\r
@@ -928,16 +1147,8 @@ static int sndrate_prevnext(int rate, int dir)
        return rates[i];\r
 }\r
 \r
-static void int_incdec(int *p, int inc, int min, int max)\r
-{\r
-       *p += inc;\r
-       if      (*p < min) *p = min;\r
-       else if (*p > max) *p = max;\r
-}\r
-\r
 static void config_commit(void)\r
 {\r
-       gp2x_cpuclock_gamma_update();\r
        if (Settings.region_force)\r
                FCEUI_SetVidSystem(Settings.region_force - 1);\r
 }\r
@@ -945,7 +1156,7 @@ static void config_commit(void)
 static int menu_loop_options(void)\r
 {\r
        static int menu_sel = 0;\r
-       int menu_sel_max = 15;\r
+       int ret, menu_sel_max = 16;\r
        unsigned long inp = 0;\r
 \r
        if (fceugi) menu_sel_max++;\r
@@ -958,19 +1169,25 @@ static int menu_loop_options(void)
                if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
                if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
                        switch (menu_sel) {\r
-                               case  1: Settings.showfps    = !Settings.showfps; break;\r
-                               case  3: soundvol = soundvol ? 0 : 100; break;\r
-                               case 10: Settings.ramtimings = !Settings.ramtimings; break;\r
-                               case 11: Settings.mmuhack    = !Settings.mmuhack; break;\r
-                               case 14: fcemenu_loop_options(); break;\r
-                               case 15: // done (update and write)\r
+                               case  1: Settings.showfps       = !Settings.showfps; break;\r
+                               case  3: Settings.accurate_mode = !Settings.accurate_mode; break;\r
+                               case  4: soundvol = soundvol ? 0 : 50; break;\r
+                               case 10: Settings.ramtimings    = !Settings.ramtimings; break;\r
+                               case 11: Settings.mmuhack       = !Settings.mmuhack; break;\r
+                               case 13: Settings.perfect_vsync = !Settings.perfect_vsync; break;\r
+                               case 15: fcemenu_loop_options(); break;\r
+                               case 16: // done (update and write)\r
                                        config_commit();\r
-                                       SaveConfig(NULL);\r
+                                       ret = SaveConfig(NULL);\r
+                                       strcpy(menuErrorMsg, ret == 0 ? "default config saved" : "config save failed");\r
                                        return 1;\r
-                               case 16: // done (update and write for current game)\r
-                                       config_commit();\r
+                               case 17: // done (update and write for current game)\r
                                        if (lastLoadedGameName[0])\r
-                                               SaveConfig(lastLoadedGameName);\r
+                                       {\r
+                                               config_commit();\r
+                                               ret = SaveConfig(lastLoadedGameName);\r
+                                               strcpy(menuErrorMsg, ret == 0 ? "game config saved" : "config save failed");\r
+                                       }\r
                                        return 1;\r
                        }\r
                }\r
@@ -982,11 +1199,11 @@ static int menu_loop_options(void)
                        switch (menu_sel) {\r
                                case  0: int_incdec(&Settings.scaling,   (inp & GP2X_LEFT) ? -1 : 1,  0,  3); break;\r
                                case  2: int_incdec(&Settings.frameskip, (inp & GP2X_LEFT) ? -1 : 1, -1, 32); break;\r
-                               case  4:\r
+                               case  5:\r
                                        Settings.sound_rate = sndrate_prevnext(Settings.sound_rate, inp & GP2X_RIGHT);\r
                                        InitSound();\r
                                        break;\r
-                               case  5: int_incdec(&Settings.region_force,   (inp & GP2X_LEFT) ? -1 : 1, 0, 2); break;\r
+                               case  6: int_incdec(&Settings.region_force,   (inp & GP2X_LEFT) ? -1 : 1, 0, 2); break;\r
                                case  7: {\r
                                        int hz = Settings.turbo_rate_add*60/2 >> 24;\r
                                        int_incdec(&hz, (inp & GP2X_LEFT) ? -1 : 1, 1, 30);\r
@@ -996,7 +1213,7 @@ static int menu_loop_options(void)
                                case  8: int_incdec(&Settings.sstate_confirm, (inp & GP2X_LEFT) ? -1 : 1, 0, 3); break;\r
                                case  9: int_incdec(&CurrentState,            (inp & GP2X_LEFT) ? -1 : 1, 0, 9); break;\r
                                case 12: int_incdec(&Settings.gamma,          (inp & GP2X_LEFT) ? -1 : 1, 0, 300); break;\r
-                               case 13:\r
+                               case 14:\r
                                        while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
                                                Settings.cpuclock += (inp & GP2X_LEFT) ? -1 : 1;\r
                                                if (Settings.cpuclock < 0) Settings.cpuclock = 0; // 0 ~ do not change\r
@@ -1013,7 +1230,7 @@ static int menu_loop_options(void)
 \r
 static void draw_menu_credits(void)\r
 {\r
-       char vstr[16];\r
+       char vstr[32];\r
 \r
        //int tl_x = 15, tl_y = 70;\r
        gp2x_fceu_copy_bg();\r
@@ -1023,7 +1240,7 @@ static void draw_menu_credits(void)
        gp2x_text_out15(20,  40, "(c) notaz, 2007");\r
 \r
        gp2x_text_out15(20,  70, "Based on FCE Ultra versions");\r
-       gp2x_text_out15(20,  80, "0.81 and 0.98.15");\r
+       gp2x_text_out15(20,  80, "0.81 and 0.98.1x");\r
 \r
        gp2x_text_out15(20, 110, "         - Credits - ");\r
        gp2x_text_out15(20, 130, "Bero: FCE");\r
@@ -1034,7 +1251,8 @@ static void draw_menu_credits(void)
        gp2x_text_out15(20, 180, "  cpuctrl, gamma libs");\r
        gp2x_text_out15(20, 190, "Squidge: squidgehack");\r
 \r
-       gp2x_video_flip();\r
+       menu_darken_text_bg();\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1042,7 +1260,7 @@ static void draw_menu_credits(void)
 \r
 static void draw_menu_root(int menu_sel)\r
 {\r
-       int tl_x = 30, tl_y = 128, y;\r
+       int tl_x = 30, tl_y = 126, y;\r
        gp2x_fceu_copy_bg();\r
 \r
        y = tl_y;\r
@@ -1059,20 +1277,25 @@ static void draw_menu_root(int menu_sel)
        gp2x_text_out15(tl_x, (y+=10), "Controls");\r
        gp2x_text_out15(tl_x, (y+=10), "Credits");\r
        gp2x_text_out15(tl_x, (y+=10), "Exit");\r
-// TODO\r
-//     if (PicoPatches)\r
-//             gp2x_text_out15(tl_x, (y+=10), "Patches / GameGenie");\r
+\r
+       if (cheats)\r
+               gp2x_text_out15(tl_x, (y+=10), "Cheats");\r
 \r
        // draw cursor\r
        gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
-       // error\r
-       if (menuErrorMsg[0]) gp2x_text_out15(1, 230, menuErrorMsg);\r
+\r
+       menu_darken_text_bg();\r
+       menu_darken_reset();\r
+\r
+       // error / version\r
+       if (menuErrorMsg[0]) gp2x_text_out15(1, 229, menuErrorMsg);\r
        else {\r
                char vstr[16];\r
                sprintf(vstr, "v" GP2X_PORT_VERSION " r%i", GP2X_PORT_REV);\r
-               gp2x_text_out15(320-strlen(vstr)*8-1, 230, vstr);\r
+               gp2x_text_out15(320-strlen(vstr)*8-1, 228, vstr);\r
        }\r
-       gp2x_video_flip();\r
+       menu_darken_text_bg();\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1081,22 +1304,9 @@ static int menu_loop_root(void)
        int ret, menu_sel_max = 8, menu_sel_min = 4;\r
        static int menu_sel = 4;\r
        unsigned long inp = 0;\r
-       char curr_path[PATH_MAX], *selfname;\r
-       FILE *tstf;\r
-\r
-       if ( (tstf = fopen(lastLoadedGameName, "rb")) )\r
-       {\r
-               fclose(tstf);\r
-               strncpy(curr_path, lastLoadedGameName, sizeof(curr_path));\r
-               curr_path[sizeof(curr_path)-1] = 0;\r
-       }\r
-       else\r
-       {\r
-               getcwd(curr_path, PATH_MAX);\r
-       }\r
 \r
        if (fceugi) menu_sel_min = 0;\r
-// TODO        if (PicoPatches) menu_sel_max = 9;\r
+       if (cheats) menu_sel_max = 9;\r
        if (menu_sel < menu_sel_min || menu_sel > menu_sel_max)\r
                menu_sel = menu_sel_min;\r
 \r
@@ -1154,12 +1364,29 @@ static int menu_loop_root(void)
                                        }\r
                                        break;\r
                                case 4: // select rom\r
-                                       selfname = romsel_loop(curr_path);\r
+                               {\r
+                                       FILE *tstf;\r
+                                       char *selfname;\r
+\r
+                                       if ( (tstf = fopen(lastLoadedGameName, "rb")) )\r
+                                       {\r
+                                               fclose(tstf);\r
+                                               strncpy(path_buffer, lastLoadedGameName, sizeof(path_buffer));\r
+                                               path_buffer[sizeof(path_buffer)-1] = 0;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               getcwd(path_buffer, PATH_MAX);\r
+                                       }\r
+\r
+                                       selfname = filesel_loop(path_buffer, lastLoadedGameName);\r
                                        if (selfname) {\r
                                                printf("selected file: %s\n", selfname);\r
+                                               while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);\r
                                                return 2;\r
                                        }\r
                                        break;\r
+                               }\r
                                case 5: // options\r
                                        ret = menu_loop_options();\r
                                        if (ret == 1) continue; // status update\r
@@ -1175,6 +1402,7 @@ static int menu_loop_root(void)
                                case 8: // exit\r
                                        return 1;\r
                                case 9: // patches/gg\r
+                                       patches_menu_loop();\r
                                        break;\r
                        }\r
                }\r
@@ -1183,9 +1411,33 @@ static int menu_loop_root(void)
 }\r
 \r
 \r
+extern unsigned short gp2x_palette16[256];\r
+\r
 static void menu_prepare_bg(void)\r
 {\r
-       // TODO...\r
+       menu_bg = malloc(320*240*2);\r
+       if (menu_bg == NULL) return;\r
+\r
+       if (fceugi)\r
+       {\r
+               /* raw emu frame should now be at gp2x_screen */\r
+               if (Settings.scaling != 0)\r
+               {\r
+                       soft_scale((char *)gp2x_screen + 32, gp2x_palette16, srendline, erendline-srendline);\r
+                       if (srendline)\r
+                               memset32((int *)((char *)gp2x_screen + 32), 0, srendline*320*2/4);\r
+                       memcpy(menu_bg, gp2x_screen + 32, 320*240*2);\r
+               }\r
+               else\r
+               {\r
+                       convert2RGB555(menu_bg, gp2x_screen, gp2x_palette16, 320*240);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               memset32((int *)menu_bg, 0, 320*240*2/4);\r
+               readpng(menu_bg, "background.png");\r
+       }\r
 }\r
 \r
 static void menu_gfx_prepare(void)\r
@@ -1195,8 +1447,8 @@ static void menu_gfx_prepare(void)
        // switch bpp\r
        gp2x_video_changemode(16);\r
        gp2x_video_set_offs(0);\r
-       gp2x_video_RGB_setscaling(0, 320, 240);\r
-       gp2x_video_flip();\r
+       gp2x_video_RGB_setscaling(320, 240);\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1208,6 +1460,8 @@ int gp2x_menu_do(void)
 \r
        ret = menu_loop_root();\r
 \r
+       if (menu_bg) free(menu_bg);\r
+       menu_bg = NULL;\r
        menuErrorMsg[0] = 0;\r
 \r
        return ret;\r