perfect vsync
[picodrive.git] / platform / gp2x / menu.c
1 // (c) Copyright 2006 notaz, All rights reserved.\r
2 // Free for non-commercial use.\r
3 \r
4 // For commercial use, separate licencing terms must be obtained.\r
5 \r
6 #include <stdio.h>\r
7 #include <string.h>\r
8 #include <stdlib.h>\r
9 #include <stdarg.h>\r
10 #include <unistd.h>\r
11 #include <dirent.h>\r
12 \r
13 #include "gp2x.h"\r
14 #include "emu.h"\r
15 #include "menu.h"\r
16 #include "fonts.h"\r
17 #include "usbjoy.h"\r
18 #include "asmutils.h"\r
19 #include "version.h"\r
20 \r
21 #include <Pico/PicoInt.h>\r
22 #include <Pico/Patch.h>\r
23 #include <zlib/zlib.h>\r
24 \r
25 #ifndef _DIRENT_HAVE_D_TYPE\r
26 #error "need d_type for file browser\r
27 #endif\r
28 \r
29 extern char *actionNames[];\r
30 extern char romFileName[PATH_MAX];\r
31 extern char *rom_data;\r
32 extern int  mmuhack_status;\r
33 extern int  state_slot;\r
34 \r
35 static char *gp2xKeyNames[] = {\r
36         "UP",    "???",    "LEFT", "???",  "DOWN", "???", "RIGHT",    "???",\r
37         "START", "SELECT", "L",    "R",    "A",    "B",   "X",        "Y",\r
38         "???",   "???",    "???",  "???",  "???",  "???", "VOL DOWN", "VOL UP",\r
39         "???",   "???",    "???",  "PUSH", "???",  "???", "???",      "???"\r
40 };\r
41 \r
42 char menuErrorMsg[40] = {0, };\r
43 \r
44 \r
45 static void gp2x_text(unsigned char *screen, int x, int y, const char *text, int color)\r
46 {\r
47         int i,l;\r
48 \r
49         screen = screen + x + y*320;\r
50 \r
51         for (i = 0; i < strlen(text); i++)\r
52         {\r
53                 for (l=0;l<8;l++)\r
54                 {\r
55                         if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=color;\r
56                         if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=color;\r
57                         if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=color;\r
58                         if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=color;\r
59                         if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=color;\r
60                         if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=color;\r
61                         if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=color;\r
62                         if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=color;\r
63                 }\r
64                 screen += 8;\r
65         }\r
66 }\r
67 \r
68 // draws white text to current bbp15 screen\r
69 void gp2x_text_out15(int x, int y, const char *text)\r
70 {\r
71         int i,l;\r
72         unsigned short *screen = gp2x_screen;\r
73 \r
74         screen = screen + x + y*320;\r
75 \r
76         for (i = 0; i < strlen(text); i++)\r
77         {\r
78                 for (l=0;l<8;l++)\r
79                 {\r
80                         if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;\r
81                         if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;\r
82                         if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;\r
83                         if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;\r
84                         if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;\r
85                         if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;\r
86                         if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;\r
87                         if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;\r
88                 }\r
89                 screen += 8;\r
90         }\r
91 }\r
92 \r
93 \r
94 void gp2x_text_out8(int x, int y, const char *texto, ...)\r
95 {\r
96         va_list args;\r
97         char    buffer[512];\r
98 \r
99         va_start(args,texto);\r
100         vsprintf(buffer,texto,args);\r
101         va_end(args);\r
102 \r
103         gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
104 }\r
105 \r
106 \r
107 void gp2x_text_out8_2(int x, int y, const char *texto, int color)\r
108 {\r
109         gp2x_text(gp2x_screen, x, y, texto, color);\r
110 }\r
111 \r
112 void gp2x_text_out8_lim(int x, int y, const char *texto, int max)\r
113 {\r
114         char    buffer[320/8+1];\r
115 \r
116         strncpy(buffer, texto, 320/8);\r
117         if (max > 320/8) max = 320/8;\r
118         if (max < 0) max = 0;\r
119         buffer[max] = 0;\r
120 \r
121         gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
122 }\r
123 \r
124 static void gp2x_smalltext8(int x, int y, const char *texto)\r
125 {\r
126         int i;\r
127         unsigned char *src, *dst;\r
128 \r
129         for (i = 0;; i++, x += 6)\r
130         {\r
131                 unsigned char c = (unsigned char) texto[i];\r
132                 int h = 8;\r
133 \r
134                 if (!c) break;\r
135 \r
136                 src = fontdata6x8[c];\r
137                 dst = (unsigned char *)gp2x_screen + x + y*320;\r
138 \r
139                 while (h--)\r
140                 {\r
141                         int w = 0x20;\r
142                         while (w)\r
143                         {\r
144                                 if( *src & w ) *dst = 0xf0;\r
145                                 dst++;\r
146                                 w>>=1;\r
147                         }\r
148                         src++;\r
149 \r
150                         dst += 320-6;\r
151                 }\r
152         }\r
153 }\r
154 \r
155 static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
156 {\r
157         char    buffer[320/6+1];\r
158 \r
159         strncpy(buffer, texto, 320/6);\r
160         if (max > 320/6) max = 320/6;\r
161         if (max < 0) max = 0;\r
162         buffer[max] = 0;\r
163 \r
164         gp2x_smalltext8(x, y, buffer);\r
165 }\r
166 \r
167 \r
168 static unsigned long inp_prev = 0;\r
169 static int inp_prevjoy = 0;\r
170 \r
171 static unsigned long wait_for_input(unsigned long interesting)\r
172 {\r
173         unsigned long ret;\r
174         static int repeats = 0, wait = 50*1000;\r
175         int release = 0, i;\r
176 \r
177         if (repeats == 2 || repeats == 4) wait /= 2;\r
178         if (repeats == 6) wait = 15 * 1000;\r
179 \r
180         for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {\r
181                 if (i == 0) repeats++;\r
182                 if (wait >= 30*1000) usleep(wait); // usleep sleeps for ~30ms minimum\r
183                 else spend_cycles(wait * currentConfig.CPUclock);\r
184         }\r
185 \r
186         while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {\r
187                 usleep(50000);\r
188                 release = 1;\r
189         }\r
190 \r
191         if (release || ret != inp_prev) {\r
192                 repeats = 0;\r
193                 wait = 50*1000;\r
194         }\r
195         inp_prev = ret;\r
196         inp_prevjoy = 0;\r
197 \r
198         // we don't need diagonals in menus\r
199         if ((ret&GP2X_UP)   && (ret&GP2X_LEFT))  ret &= ~GP2X_LEFT;\r
200         if ((ret&GP2X_UP)   && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
201         if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT))  ret &= ~GP2X_LEFT;\r
202         if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
203 \r
204         return ret;\r
205 }\r
206 \r
207 static unsigned long input2_read(unsigned long interesting, int *joy)\r
208 {\r
209         unsigned long ret;\r
210         int i;\r
211 \r
212         do\r
213         {\r
214                 *joy = 0;\r
215                 if ((ret = gp2x_joystick_read(0) & interesting)) break;\r
216                 gp2x_usbjoy_update();\r
217                 for (i = 0; i < num_of_joys; i++) {\r
218                         ret = gp2x_usbjoy_check2(i);\r
219                         if (ret) { *joy = i + 1; break; }\r
220                 }\r
221                 if (ret) break;\r
222         }\r
223         while(0);\r
224 \r
225         return ret;\r
226 }\r
227 \r
228 // similar to wait_for_input(), but returns joy num\r
229 static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)\r
230 {\r
231         unsigned long ret;\r
232         const int wait = 300*1000;\r
233         int i;\r
234 \r
235         if (inp_prevjoy == 0) inp_prev &= interesting;\r
236         for (i = 0; i < 6; i++) {\r
237                 ret = input2_read(interesting, joy);\r
238                 if (*joy != inp_prevjoy || ret != inp_prev) break;\r
239                 usleep(wait/6);\r
240         }\r
241 \r
242         while ( !(ret = input2_read(interesting, joy)) ) {\r
243                 usleep(50000);\r
244         }\r
245 \r
246         inp_prev = ret;\r
247         inp_prevjoy = *joy;\r
248 \r
249         return ret;\r
250 }\r
251 \r
252 \r
253 \r
254 // -------------- ROM selector --------------\r
255 \r
256 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
257 {\r
258         int start, i, pos;\r
259 \r
260         start = 12 - sel;\r
261         n--; // exclude current dir (".")\r
262 \r
263         //memset(gp2x_screen, 0, 320*240);\r
264         gp2x_pd_clone_buffer2();\r
265 \r
266         if(start - 2 >= 0)\r
267                 gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);\r
268         for (i = 0; i < n; i++) {\r
269                 pos = start + i;\r
270                 if (pos < 0)  continue;\r
271                 if (pos > 23) break;\r
272                 if (namelist[i+1]->d_type == DT_DIR) {\r
273                         gp2x_smalltext8_lim(14,   pos*10, "/", 1);\r
274                         gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
275                 } else {\r
276                         gp2x_smalltext8_lim(14,   pos*10, namelist[i+1]->d_name, 53-2);\r
277                 }\r
278         }\r
279         gp2x_text_out8(5, 120, ">");\r
280         gp2x_video_flip2();\r
281 }\r
282 \r
283 static int scandir_cmp(const void *p1, const void *p2)\r
284 {\r
285         struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
286         if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
287         if ((*d1)->d_type == DT_DIR) return -1; // put before\r
288         if ((*d2)->d_type == DT_DIR) return  1;\r
289         return alphasort(d1, d2);\r
290 }\r
291 \r
292 static char *filter_exts[] = {\r
293         ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r
294         ".jpg", ".gpe", ".cue"\r
295 };\r
296 \r
297 static int scandir_filter(const struct dirent *ent)\r
298 {\r
299         const char *p;\r
300         int i;\r
301 \r
302         if (ent == NULL || ent->d_name == NULL) return 0;\r
303         if (strlen(ent->d_name) < 5) return 1;\r
304 \r
305         p = ent->d_name + strlen(ent->d_name) - 4;\r
306 \r
307         for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)\r
308         {\r
309                 if (strcmp(p, filter_exts[i]) == 0) return 0;\r
310         }\r
311 \r
312         return 1;\r
313 }\r
314 \r
315 static char *romsel_loop(char *curr_path)\r
316 {\r
317         struct dirent **namelist;\r
318         DIR *dir;\r
319         int n, sel = 0;\r
320         unsigned long inp = 0;\r
321         char *ret = NULL, *fname = NULL;\r
322 \r
323         // is this a dir or a full path?\r
324         if ((dir = opendir(curr_path))) {\r
325                 closedir(dir);\r
326         } else {\r
327                 char *p;\r
328                 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);\r
329                 *p = 0;\r
330                 fname = p+1;\r
331         }\r
332 \r
333         n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
334         if (n < 0) {\r
335                 // try root\r
336                 n = scandir("/", &namelist, scandir_filter, scandir_cmp);\r
337                 if (n < 0) {\r
338                         // oops, we failed\r
339                         printf("dir: "); printf(curr_path); printf("\n");\r
340                         perror("scandir");\r
341                         return NULL;\r
342                 }\r
343         }\r
344 \r
345         // try to find sel\r
346         if (fname != NULL) {\r
347                 int i;\r
348                 for (i = 1; i < n; i++) {\r
349                         if (strcmp(namelist[i]->d_name, fname) == 0) {\r
350                                 sel = i - 1;\r
351                                 break;\r
352                         }\r
353                 }\r
354         }\r
355 \r
356         for (;;)\r
357         {\r
358                 draw_dirlist(curr_path, namelist, n, sel);\r
359                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
360                 if(inp & GP2X_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
361                 if(inp & GP2X_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
362                 if(inp & GP2X_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }\r
363                 if(inp & GP2X_L)     { sel-=24; if (sel < 0)   sel = 0; }\r
364                 if(inp & GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
365                 if(inp & GP2X_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
366                 if(inp & GP2X_B)     { // enter dir/select\r
367                         again:\r
368                         if (namelist[sel+1]->d_type == DT_REG) {\r
369                                 strcpy(romFileName, curr_path);\r
370                                 strcat(romFileName, "/");\r
371                                 strcat(romFileName, namelist[sel+1]->d_name);\r
372                                 ret = romFileName;\r
373                                 break;\r
374                         } else if (namelist[sel+1]->d_type == DT_DIR) {\r
375                                 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
376                                 char *p, *newdir = malloc(newlen);\r
377                                 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
378                                         char *start = curr_path;\r
379                                         p = start + strlen(start) - 1;\r
380                                         while (*p == '/' && p > start) p--;\r
381                                         while (*p != '/' && p > start) p--;\r
382                                         if (p <= start) strcpy(newdir, "/");\r
383                                         else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
384                                 } else {\r
385                                         strcpy(newdir, curr_path);\r
386                                         p = newdir + strlen(newdir) - 1;\r
387                                         while (*p == '/' && p >= newdir) *p-- = 0;\r
388                                         strcat(newdir, "/");\r
389                                         strcat(newdir, namelist[sel+1]->d_name);\r
390                                 }\r
391                                 ret = romsel_loop(newdir);\r
392                                 free(newdir);\r
393                                 break;\r
394                         } else {\r
395                                 // unknown file type, happens on NTFS mounts. Try to guess.\r
396                                 FILE *tstf; int tmp;\r
397                                 strcpy(romFileName, curr_path);\r
398                                 strcat(romFileName, "/");\r
399                                 strcat(romFileName, namelist[sel+1]->d_name);\r
400                                 tstf = fopen(romFileName, "rb");\r
401                                 if (tstf != NULL)\r
402                                 {\r
403                                         if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
404                                                 namelist[sel+1]->d_type = DT_REG;\r
405                                         else    namelist[sel+1]->d_type = DT_DIR;\r
406                                         fclose(tstf);\r
407                                         goto again;\r
408                                 }\r
409                         }\r
410                 }\r
411                 if(inp & GP2X_X) break; // cancel\r
412         }\r
413 \r
414         if (n > 0) {\r
415                 while(n--) free(namelist[n]);\r
416                 free(namelist);\r
417         }\r
418 \r
419         return ret;\r
420 }\r
421 \r
422 // ------------ debug menu ------------\r
423 \r
424 char *debugString(void);\r
425 \r
426 static void draw_debug(void)\r
427 {\r
428         char *p, *str = debugString();\r
429         int len, line;\r
430 \r
431         gp2x_pd_clone_buffer2();\r
432 \r
433         p = str;\r
434         for (line = 0; line < 24; line++)\r
435         {\r
436                 while (*p && *p != '\n') p++;\r
437                 len = p - str;\r
438                 if (len > 55) len = 55;\r
439                 gp2x_smalltext8_lim(1, line*10, str, len);\r
440                 if (*p == 0) break;\r
441                 p++; str = p;\r
442         }\r
443         gp2x_video_flip2();\r
444 }\r
445 \r
446 static void debug_menu_loop(void)\r
447 {\r
448         draw_debug();\r
449         wait_for_input(GP2X_B|GP2X_X);\r
450 }\r
451 \r
452 // ------------ patch/gg menu ------------\r
453 \r
454 static void draw_patchlist(int sel)\r
455 {\r
456         int start, i, pos;\r
457 \r
458         start = 12 - sel;\r
459 \r
460         gp2x_pd_clone_buffer2();\r
461 \r
462         for (i = 0; i < PicoPatchCount; i++) {\r
463                 pos = start + i;\r
464                 if (pos < 0)  continue;\r
465                 if (pos > 23) break;\r
466                 gp2x_smalltext8_lim(14,     pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);\r
467                 gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);\r
468         }\r
469         pos = start + i;\r
470         if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);\r
471 \r
472         gp2x_text_out8(5, 120, ">");\r
473         gp2x_video_flip2();\r
474 }\r
475 \r
476 \r
477 static void patches_menu_loop(void)\r
478 {\r
479         int menu_sel = 0;\r
480         unsigned long inp = 0;\r
481 \r
482         for(;;)\r
483         {\r
484                 draw_patchlist(menu_sel);\r
485                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
486                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
487                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
488                 if(inp &(GP2X_LEFT|GP2X_L))  { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
489                 if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
490                 if(inp & GP2X_B) { // action\r
491                         if (menu_sel < PicoPatchCount)\r
492                                 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
493                         else    return;\r
494                 }\r
495                 if(inp & GP2X_X) return;\r
496         }\r
497 \r
498 }\r
499 \r
500 // ------------ savestate loader ------------\r
501 \r
502 static void menu_prepare_bg(void);\r
503 \r
504 static int state_slot_flags = 0;\r
505 \r
506 static void state_check_slots(void)\r
507 {\r
508         int slot;\r
509 \r
510         state_slot_flags = 0;\r
511 \r
512         for (slot = 0; slot < 10; slot++)\r
513         {\r
514                 if (emu_check_save_file(slot))\r
515                 {\r
516                         state_slot_flags |= 1 << slot;\r
517                 }\r
518         }\r
519 }\r
520 \r
521 static void draw_savestate_bg(int slot)\r
522 {\r
523         struct PicoVideo tmp_pv;\r
524         unsigned short tmp_cram[0x40];\r
525         unsigned short tmp_vsram[0x40];\r
526         void *tmp_vram, *file;\r
527         char *fname;\r
528 \r
529         fname = emu_GetSaveFName(1, 0, slot);\r
530         if (!fname) return;\r
531 \r
532         tmp_vram = malloc(sizeof(Pico.vram));\r
533         if (tmp_vram == NULL) return;\r
534 \r
535         memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));\r
536         memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));\r
537         memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));\r
538         memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));\r
539 \r
540         if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {\r
541                 file = gzopen(fname, "rb");\r
542                 emu_set_save_cbs(1);\r
543         } else {\r
544                 file = fopen(fname, "rb");\r
545                 emu_set_save_cbs(0);\r
546         }\r
547 \r
548         if (file) {\r
549                 if (PicoMCD & 1) {\r
550                         PicoCdLoadStateGfx(file);\r
551                 } else {\r
552                         areaSeek(file, 0x10020, SEEK_SET);  // skip header and RAM in state file\r
553                         areaRead(Pico.vram, 1, sizeof(Pico.vram), file);\r
554                         areaSeek(file, 0x2000, SEEK_CUR);\r
555                         areaRead(Pico.cram, 1, sizeof(Pico.cram), file);\r
556                         areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);\r
557                         areaSeek(file, 0x221a0, SEEK_SET);\r
558                         areaRead(&Pico.video, 1, sizeof(Pico.video), file);\r
559                 }\r
560                 areaClose(file);\r
561         }\r
562 \r
563         emu_forced_frame();\r
564         gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
565         menu_prepare_bg();\r
566 \r
567         memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
568         memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
569         memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));\r
570         memcpy(&Pico.video, &tmp_pv,  sizeof(Pico.video));\r
571         free(tmp_vram);\r
572 }\r
573 \r
574 static void draw_savestate_menu(int menu_sel, int is_loading)\r
575 {\r
576         int tl_x = 25, tl_y = 60, y, i;\r
577 \r
578         if (state_slot_flags & (1 << menu_sel))\r
579                 draw_savestate_bg(menu_sel);\r
580         gp2x_pd_clone_buffer2();\r
581 \r
582         gp2x_text_out8(tl_x, 30, is_loading ? "Load state" : "Save state");\r
583 \r
584         /* draw all 10 slots */\r
585         y = tl_y;\r
586         for (i = 0; i < 10; i++, y+=10)\r
587         {\r
588                 gp2x_text_out8(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
589         }\r
590         gp2x_text_out8(tl_x, y, "back");\r
591 \r
592         // draw cursor\r
593         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
594 \r
595         gp2x_video_flip2();\r
596 }\r
597 \r
598 static int savestate_menu_loop(int is_loading)\r
599 {\r
600         static int menu_sel = 10;\r
601         int menu_sel_max = 10;\r
602         unsigned long inp = 0;\r
603 \r
604         state_check_slots();\r
605 \r
606         for(;;)\r
607         {\r
608                 draw_savestate_menu(menu_sel, is_loading);\r
609                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
610                 if(inp & GP2X_UP  ) {\r
611                         do {\r
612                                 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;\r
613                         } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
614                 }\r
615                 if(inp & GP2X_DOWN) {\r
616                         do {\r
617                                 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;\r
618                         } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
619                 }\r
620                 if(inp & GP2X_B) { // save/load\r
621                         if (menu_sel < 10) {\r
622                                 state_slot = menu_sel;\r
623                                 if (emu_SaveLoadGame(is_loading, 0)) {\r
624                                         strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");\r
625                                         return 1;\r
626                                 }\r
627                                 return 0;\r
628                         } else  return 1;\r
629                 }\r
630                 if(inp & GP2X_X) return 1;\r
631         }\r
632 }\r
633 \r
634 // -------------- key config --------------\r
635 \r
636 static char *usb_joy_key_name(int joy, int num)\r
637 {\r
638         static char name[16];\r
639         switch (num)\r
640         {\r
641                 case 0: sprintf(name, "Joy%i UP", joy); break;\r
642                 case 1: sprintf(name, "Joy%i DOWN", joy); break;\r
643                 case 2: sprintf(name, "Joy%i LEFT", joy); break;\r
644                 case 3: sprintf(name, "Joy%i RIGHT", joy); break;\r
645                 default:sprintf(name, "Joy%i b%i", joy, num-3); break;\r
646         }\r
647         return name;\r
648 }\r
649 \r
650 static void draw_key_config(int curr_act, int is_p2)\r
651 {\r
652         char strkeys[32*5];\r
653         int joy, i;\r
654 \r
655         strkeys[0] = 0;\r
656         for (i = 0; i < 32; i++)\r
657         {\r
658                 if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
659                 {\r
660                         if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
661                         if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
662                         else strcpy(strkeys, gp2xKeyNames[i]);\r
663                 }\r
664         }\r
665         for (joy = 0; joy < num_of_joys; joy++)\r
666         {\r
667                 for (i = 0; i < 32; i++)\r
668                 {\r
669                         if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
670                         {\r
671                                 if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
672                                 if (strkeys[0]) {\r
673                                         strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
674                                         break;\r
675                                 }\r
676                                 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));\r
677                         }\r
678                 }\r
679         }\r
680 \r
681         //memset(gp2x_screen, 0, 320*240);\r
682         gp2x_pd_clone_buffer2();\r
683         gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
684         gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
685 \r
686         gp2x_text_out8(30, 180, "Use SELECT to change action");\r
687         gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
688         gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
689         gp2x_text_out8(30, 210, "  press any key to finish");\r
690         gp2x_video_flip2();\r
691 }\r
692 \r
693 static void key_config_loop(int is_p2)\r
694 {\r
695         int curr_act = 0, joy = 0, i;\r
696         unsigned long inp = 0;\r
697 \r
698         for (;;)\r
699         {\r
700                 draw_key_config(curr_act, is_p2);\r
701                 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
702                 // printf("got %08lX from joy %i\n", inp, joy);\r
703                 if (joy == 0) {\r
704                         if (inp & GP2X_SELECT) {\r
705                                 curr_act++;\r
706                                 while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
707                                 if (curr_act > 31) curr_act = 0;\r
708                         }\r
709                         inp &= CONFIGURABLE_KEYS;\r
710                         inp &= ~GP2X_SELECT;\r
711                 }\r
712                 if (curr_act == 31 && inp) break;\r
713                 if (joy == 0) {\r
714                         for (i = 0; i < 32; i++)\r
715                                 if (inp & (1 << i)) {\r
716                                         currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
717                                         if (is_p2) currentConfig.KeyBinds[i] |=  (1 << 16); // player 2 flag\r
718                                         else       currentConfig.KeyBinds[i] &= ~(1 << 16);\r
719                                 }\r
720                 } else {\r
721                         for (i = 0; i < 32; i++)\r
722                                 if (inp & (1 << i)) {\r
723                                         currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
724                                         if (is_p2) currentConfig.JoyBinds[joy-1][i] |=  (1 << 16);\r
725                                         else       currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
726                                 }\r
727                 }\r
728         }\r
729 }\r
730 \r
731 static void draw_kc_sel(int menu_sel)\r
732 {\r
733         int tl_x = 25+40, tl_y = 60, y, i;\r
734         char joyname[36];\r
735 \r
736         y = tl_y;\r
737         //memset(gp2x_screen, 0, 320*240);\r
738         gp2x_pd_clone_buffer2();\r
739         gp2x_text_out8(tl_x, y,       "Player 1");\r
740         gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
741         gp2x_text_out8(tl_x, (y+=10), "Done");\r
742 \r
743         // draw cursor\r
744         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
745 \r
746         tl_x = 25;\r
747         gp2x_text_out8(tl_x, (y=110), "USB joys detected:");\r
748         if (num_of_joys > 0) {\r
749                 for (i = 0; i < num_of_joys; i++) {\r
750                         strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
751                         gp2x_text_out8(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
752                 }\r
753         } else {\r
754                 gp2x_text_out8(tl_x, (y+=10), "none");\r
755         }\r
756 \r
757 \r
758         gp2x_video_flip2();\r
759 }\r
760 \r
761 static void kc_sel_loop(void)\r
762 {\r
763         int menu_sel = 2, menu_sel_max = 2;\r
764         unsigned long inp = 0;\r
765 \r
766         for(;;)\r
767         {\r
768                 draw_kc_sel(menu_sel);\r
769                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
770                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
771                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
772                 if(inp & GP2X_B) {\r
773                         switch (menu_sel) {\r
774                                 case 0: key_config_loop(0); return;\r
775                                 case 1: key_config_loop(1); return;\r
776                                 default: return;\r
777                         }\r
778                 }\r
779                 if(inp & GP2X_X) return;\r
780         }\r
781 }\r
782 \r
783 \r
784 \r
785 // --------- sega/mega cd options ----------\r
786 \r
787 static void draw_cd_menu_options(int menu_sel, char *b_us, char *b_eu, char *b_jp)\r
788 {\r
789         int tl_x = 25, tl_y = 60, y;\r
790         char ra_buff[16];\r
791 \r
792         if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);\r
793         else strcpy(ra_buff, "     OFF");\r
794 \r
795         y = tl_y;\r
796         //memset(gp2x_screen, 0, 320*240);\r
797         gp2x_pd_clone_buffer2();\r
798 \r
799         gp2x_text_out8(tl_x, y,       "USA BIOS:     %s", b_us); // 0\r
800         gp2x_text_out8(tl_x, (y+=10), "EUR BIOS:     %s", b_eu); // 1\r
801         gp2x_text_out8(tl_x, (y+=10), "JAP BIOS:     %s", b_jp); // 2\r
802         gp2x_text_out8(tl_x, (y+=10), "CD LEDs                    %s", (currentConfig.EmuOpt &0x0400)?"ON":"OFF"); // 3\r
803         gp2x_text_out8(tl_x, (y+=10), "CDDA audio (using mp3s)    %s", (currentConfig.PicoOpt&0x0800)?"ON":"OFF"); // 4\r
804         gp2x_text_out8(tl_x, (y+=10), "PCM audio                  %s", (currentConfig.PicoOpt&0x0400)?"ON":"OFF"); // 5\r
805         gp2x_text_out8(tl_x, (y+=10), "ReadAhead buffer      %s", ra_buff); // 6\r
806         gp2x_text_out8(tl_x, (y+=10), "SaveRAM cart               %s", (currentConfig.PicoOpt&0x8000)?"ON":"OFF"); // 7\r
807         gp2x_text_out8(tl_x, (y+=10), "Scale/Rot. fx (slow)       %s", (currentConfig.PicoOpt&0x1000)?"ON":"OFF"); // 8\r
808         gp2x_text_out8(tl_x, (y+=10), "Better sync (slow)         %s", (currentConfig.PicoOpt&0x2000)?"ON":"OFF"); // 9\r
809         gp2x_text_out8(tl_x, (y+=10), "Done");\r
810 \r
811         // draw cursor\r
812         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
813 \r
814         if ((menu_sel == 0 && strcmp(b_us, "NOT FOUND")) ||\r
815                 (menu_sel == 1 && strcmp(b_eu, "NOT FOUND")) ||\r
816                 (menu_sel == 2 && strcmp(b_jp, "NOT FOUND")))\r
817                         gp2x_text_out8(tl_x, 220, "Press start to test selected BIOS");\r
818 \r
819         gp2x_video_flip2();\r
820 }\r
821 \r
822 static void cd_menu_loop_options(void)\r
823 {\r
824         static int menu_sel = 0;\r
825         int menu_sel_max = 10;\r
826         unsigned long inp = 0;\r
827         char bios_us[32], bios_eu[32], bios_jp[32], *bios, *p;\r
828 \r
829         if (find_bios(4, &bios)) { // US\r
830                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
831                 strncpy(bios_us, p, 31); bios_us[31] = 0;\r
832         } else  strcpy(bios_us, "NOT FOUND");\r
833 \r
834         if (find_bios(8, &bios)) { // EU\r
835                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
836                 strncpy(bios_eu, p, 31); bios_eu[31] = 0;\r
837         } else  strcpy(bios_eu, "NOT FOUND");\r
838 \r
839         if (find_bios(1, &bios)) { // JP\r
840                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
841                 strncpy(bios_jp, p, 31); bios_jp[31] = 0;\r
842         } else  strcpy(bios_jp, "NOT FOUND");\r
843 \r
844         for(;;)\r
845         {\r
846                 draw_cd_menu_options(menu_sel, bios_us, bios_eu, bios_jp);\r
847                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);\r
848                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
849                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
850                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
851                         switch (menu_sel) {\r
852                                 case  3: currentConfig.EmuOpt ^=0x0400; break;\r
853                                 case  4: currentConfig.PicoOpt^=0x0800; break;\r
854                                 case  5: currentConfig.PicoOpt^=0x0400; break;\r
855                                 case  6:\r
856                                         if (inp & GP2X_LEFT) {\r
857                                                 PicoCDBuffers >>= 1;\r
858                                                 if (PicoCDBuffers < 64) PicoCDBuffers = 0;\r
859                                         } else {\r
860                                                 if (PicoCDBuffers < 64) PicoCDBuffers = 64;\r
861                                                 else PicoCDBuffers <<= 1;\r
862                                                 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M\r
863                                         }\r
864                                         break;\r
865                                 case  7: currentConfig.PicoOpt^=0x8000; break;\r
866                                 case  8: currentConfig.PicoOpt^=0x1000; break;\r
867                                 case  9: currentConfig.PicoOpt^=0x2000; break;\r
868                                 case 10: return;\r
869                         }\r
870                 }\r
871                 if(inp & (GP2X_X|GP2X_A)) return;\r
872                 if(inp &  GP2X_START) { // BIOS testers\r
873                         switch (menu_sel) {\r
874                                 case 0: if (find_bios(4, &bios)) { // test US\r
875                                                 strcpy(romFileName, bios);\r
876                                                 engineState = PGS_ReloadRom;\r
877                                                 return;\r
878                                         }\r
879                                         break;\r
880                                 case 1: if (find_bios(8, &bios)) { // test EU\r
881                                                 strcpy(romFileName, bios);\r
882                                                 engineState = PGS_ReloadRom;\r
883                                                 return;\r
884                                         }\r
885                                         break;\r
886                                 case 2: if (find_bios(1, &bios)) { // test JP\r
887                                                 strcpy(romFileName, bios);\r
888                                                 engineState = PGS_ReloadRom;\r
889                                                 return;\r
890                                         }\r
891                                         break;\r
892                         }\r
893                 }\r
894         }\r
895 }\r
896 \r
897 \r
898 // --------- advanced options ----------\r
899 \r
900 static void draw_amenu_options(int menu_sel)\r
901 {\r
902         int tl_x = 25, tl_y = 50, y;\r
903         char *mms = mmuhack_status ? "active)  " : "inactive)";\r
904 \r
905         y = tl_y;\r
906         //memset(gp2x_screen, 0, 320*240);\r
907         gp2x_pd_clone_buffer2();\r
908 \r
909         gp2x_text_out8(tl_x, y,       "Gamma correction           %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100); // 0\r
910         gp2x_text_out8(tl_x, (y+=10), "A_SN's gamma curve         %s", (currentConfig.EmuOpt &0x1000)?"ON":"OFF");\r
911         gp2x_text_out8(tl_x, (y+=10), "Perfecf vsync              %s", (currentConfig.EmuOpt &0x2000)?"ON":"OFF");\r
912         gp2x_text_out8(tl_x, (y+=10), "Emulate Z80                %s", (currentConfig.PicoOpt&0x0004)?"ON":"OFF");\r
913         gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM)        %s", (currentConfig.PicoOpt&0x0001)?"ON":"OFF");\r
914         gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG)      %s", (currentConfig.PicoOpt&0x0002)?"ON":"OFF"); // 5\r
915         gp2x_text_out8(tl_x, (y+=10), "gzip savestates            %s", (currentConfig.EmuOpt &0x0008)?"ON":"OFF");\r
916         gp2x_text_out8(tl_x, (y+=10), "Don't save last used ROM   %s", (currentConfig.EmuOpt &0x0020)?"ON":"OFF");\r
917         gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
918         gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings      %s", (currentConfig.EmuOpt &0x0100)?"ON":"OFF");\r
919         gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s",   mms, (currentConfig.EmuOpt &0x0010)?"ON":"OFF"); // 10\r
920         gp2x_text_out8(tl_x, (y+=10), "Done");\r
921 \r
922         // draw cursor\r
923         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
924 \r
925         gp2x_video_flip2();\r
926 }\r
927 \r
928 static void amenu_loop_options(void)\r
929 {\r
930         static int menu_sel = 0;\r
931         int menu_sel_max = 11;\r
932         unsigned long inp = 0;\r
933 \r
934         for(;;)\r
935         {\r
936                 draw_amenu_options(menu_sel);\r
937                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
938                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
939                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
940                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
941                         switch (menu_sel) {\r
942                                 case  1: currentConfig.EmuOpt ^=0x1000; break;\r
943                                 case  2: currentConfig.EmuOpt ^=0x2000; break;\r
944                                 case  3: currentConfig.PicoOpt^=0x0004; break;\r
945                                 case  4: currentConfig.PicoOpt^=0x0001; break;\r
946                                 case  5: currentConfig.PicoOpt^=0x0002; break;\r
947                                 case  6: currentConfig.EmuOpt ^=0x0008; break;\r
948                                 case  7: currentConfig.EmuOpt ^=0x0020; break;\r
949                                 case  9: currentConfig.EmuOpt ^=0x0100; break;\r
950                                 case 10: currentConfig.EmuOpt ^=0x0010; break;\r
951                                 case 11: return;\r
952                         }\r
953                 }\r
954                 if(inp & (GP2X_X|GP2X_A)) return;\r
955                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
956                         switch (menu_sel) {\r
957                                 case 0:\r
958                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
959                                                 currentConfig.gamma += (inp & GP2X_LEFT) ? -1 : 1;\r
960                                                 if (currentConfig.gamma <   1) currentConfig.gamma =   1;\r
961                                                 if (currentConfig.gamma > 300) currentConfig.gamma = 300;\r
962                                                 draw_amenu_options(menu_sel);\r
963                                                 usleep(18*1000);\r
964                                         }\r
965                                         break;\r
966                         }\r
967                 }\r
968         }\r
969 }\r
970 \r
971 // -------------- options --------------\r
972 \r
973 static const char *region_name(unsigned int code)\r
974 {\r
975         static const char *names[] = { "Auto", "      Japan NTSC", "      Japan PAL", "      USA", "      Europe" };\r
976         static const char *names_short[] = { "", " JP", " JP", " US", " EU" };\r
977         int u, i = 0;\r
978         if (code) {\r
979                 code <<= 1;\r
980                 while((code >>= 1)) i++;\r
981                 if (i > 4) return "unknown";\r
982                 return names[i];\r
983         } else {\r
984                 static char name[24];\r
985                 strcpy(name, "Auto:");\r
986                 for (u = 0; u < 3; u++) {\r
987                         i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;\r
988                         while((code >>= 1)) i++;\r
989                         strcat(name, names_short[i]);\r
990                 }\r
991                 return name;\r
992         }\r
993 }\r
994 \r
995 static void draw_menu_options(int menu_sel)\r
996 {\r
997         int tl_x = 25, tl_y = 32, y;\r
998         char monostereo[8], strframeskip[8], *strrend, *strscaling, *strssconfirm;\r
999 \r
1000         strcpy(monostereo, (currentConfig.PicoOpt&0x08)?"stereo":"mono");\r
1001         if (currentConfig.Frameskip < 0)\r
1002                  strcpy(strframeskip, "Auto");\r
1003         else sprintf(strframeskip, "%i", currentConfig.Frameskip);\r
1004         if (currentConfig.PicoOpt&0x10) {\r
1005                 strrend = " 8bit fast";\r
1006         } else if (currentConfig.EmuOpt&0x80) {\r
1007                 strrend = "16bit accurate";\r
1008         } else {\r
1009                 strrend = " 8bit accurate";\r
1010         }\r
1011         switch (currentConfig.scaling) {\r
1012                 default: strscaling = "            OFF";   break;\r
1013                 case 1:  strscaling = "hw horizontal";     break;\r
1014                 case 2:  strscaling = "hw horiz. + vert."; break;\r
1015                 case 3:  strscaling = "sw horizontal";     break;\r
1016         }\r
1017         switch ((currentConfig.EmuOpt >> 9) & 5) {\r
1018                 default: strssconfirm = "OFF";    break;\r
1019                 case 1:  strssconfirm = "writes"; break;\r
1020                 case 4:  strssconfirm = "loads";  break;\r
1021                 case 5:  strssconfirm = "both";   break;\r
1022         }\r
1023 \r
1024         y = tl_y;\r
1025         //memset(gp2x_screen, 0, 320*240);\r
1026         gp2x_pd_clone_buffer2();\r
1027 \r
1028         gp2x_text_out8(tl_x, y,       "Renderer:            %s", strrend); // 0\r
1029         gp2x_text_out8(tl_x, (y+=10), "Scaling:       %s", strscaling);    // 1\r
1030         gp2x_text_out8(tl_x, (y+=10), "Accurate timing (slower)   %s", (currentConfig.PicoOpt&0x040)?"ON":"OFF"); // 2\r
1031         gp2x_text_out8(tl_x, (y+=10), "Accurate sprites (slower)  %s", (currentConfig.PicoOpt&0x080)?"ON":"OFF"); // 3\r
1032         gp2x_text_out8(tl_x, (y+=10), "Show FPS                   %s", (currentConfig.EmuOpt &0x002)?"ON":"OFF"); // 4\r
1033         gp2x_text_out8(tl_x, (y+=10), "Frameskip                  %s", strframeskip);\r
1034         gp2x_text_out8(tl_x, (y+=10), "Enable sound               %s", (currentConfig.EmuOpt &0x004)?"ON":"OFF"); // 6\r
1035         gp2x_text_out8(tl_x, (y+=10), "Sound Quality:     %5iHz %s",   currentConfig.PsndRate, monostereo);\r
1036         gp2x_text_out8(tl_x, (y+=10), "Use ARM940 core for sound  %s", (currentConfig.PicoOpt&0x200)?"ON":"OFF"); // 8\r
1037         gp2x_text_out8(tl_x, (y+=10), "6 button pad               %s", (currentConfig.PicoOpt&0x020)?"ON":"OFF"); // 9\r
1038         gp2x_text_out8(tl_x, (y+=10), "Region:              %s",       region_name(currentConfig.PicoRegion));\r
1039         gp2x_text_out8(tl_x, (y+=10), "Use SRAM/BRAM savestates   %s", (currentConfig.EmuOpt &0x001)?"ON":"OFF"); // 11\r
1040         gp2x_text_out8(tl_x, (y+=10), "Confirm savestate          %s", strssconfirm); // 12\r
1041         gp2x_text_out8(tl_x, (y+=10), "Save slot                  %i", state_slot); // 13\r
1042         gp2x_text_out8(tl_x, (y+=10), "GP2X CPU clocks            %iMhz", currentConfig.CPUclock);\r
1043         gp2x_text_out8(tl_x, (y+=10), "[Sega/Mega CD options]");\r
1044         gp2x_text_out8(tl_x, (y+=10), "[advanced options]");            // 16\r
1045         gp2x_text_out8(tl_x, (y+=10), "Save cfg as default");\r
1046         if (rom_data)\r
1047                 gp2x_text_out8(tl_x, (y+=10), "Save cfg for current game only");\r
1048 \r
1049         // draw cursor\r
1050         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
1051 \r
1052         gp2x_video_flip2();\r
1053 }\r
1054 \r
1055 static int sndrate_prevnext(int rate, int dir)\r
1056 {\r
1057         int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
1058 \r
1059         for (i = 0; i < 5; i++)\r
1060                 if (rates[i] == rate) break;\r
1061 \r
1062         i += dir ? 1 : -1;\r
1063         if (i > 4) return dir ? 44100 : 22050;\r
1064         if (i < 0) return dir ? 11025 : 8000;\r
1065         return rates[i];\r
1066 }\r
1067 \r
1068 static void region_prevnext(int right)\r
1069 {\r
1070         // jp_ntsc=1, jp_pal=2, usa=4, eu=8\r
1071         static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };\r
1072         int i;\r
1073         if (right) {\r
1074                 if (!currentConfig.PicoRegion) {\r
1075                         for (i = 0; i < 6; i++)\r
1076                                 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
1077                         if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];\r
1078                         else currentConfig.PicoRegion=1;\r
1079                 }\r
1080                 else currentConfig.PicoRegion<<=1;\r
1081                 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;\r
1082         } else {\r
1083                 if (!currentConfig.PicoRegion) {\r
1084                         for (i = 0; i < 6; i++)\r
1085                                 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
1086                         if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];\r
1087                 }\r
1088                 else currentConfig.PicoRegion>>=1;\r
1089         }\r
1090 }\r
1091 \r
1092 static void menu_options_save(void)\r
1093 {\r
1094         PicoOpt = currentConfig.PicoOpt;\r
1095         PsndRate = currentConfig.PsndRate;\r
1096         PicoRegionOverride = currentConfig.PicoRegion;\r
1097         if (PicoOpt & 0x20) {\r
1098                 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
1099                 actionNames[10] = "X"; actionNames[11] = "MODE";\r
1100         } else {\r
1101                 actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
1102         }\r
1103         scaling_update();\r
1104 }\r
1105 \r
1106 static int menu_loop_options(void)\r
1107 {\r
1108         static int menu_sel = 0;\r
1109         int menu_sel_max = 17;\r
1110         unsigned long inp = 0;\r
1111 \r
1112         if (rom_data) menu_sel_max++;\r
1113         currentConfig.PicoOpt = PicoOpt;\r
1114         currentConfig.PsndRate = PsndRate;\r
1115         currentConfig.PicoRegion = PicoRegionOverride;\r
1116 \r
1117         for(;;)\r
1118         {\r
1119                 draw_menu_options(menu_sel);\r
1120                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
1121                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
1122                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
1123                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
1124                         switch (menu_sel) {\r
1125                                 case  2: currentConfig.PicoOpt^=0x040; break;\r
1126                                 case  3: currentConfig.PicoOpt^=0x080; break;\r
1127                                 case  4: currentConfig.EmuOpt ^=0x002; break;\r
1128                                 case  6: currentConfig.EmuOpt ^=0x004; break;\r
1129                                 case  8: currentConfig.PicoOpt^=0x200; break;\r
1130                                 case  9: currentConfig.PicoOpt^=0x020; break;\r
1131                                 case 11: currentConfig.EmuOpt ^=0x001; break;\r
1132                                 case 15: cd_menu_loop_options();\r
1133                                         if (engineState == PGS_ReloadRom)\r
1134                                                 return 0; // test BIOS\r
1135                                         break;\r
1136                                 case 16: amenu_loop_options();    break;\r
1137                                 case 17: // done (update and write)\r
1138                                         menu_options_save();\r
1139                                         if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");\r
1140                                         else strcpy(menuErrorMsg, "failed to write config");\r
1141                                         return 1;\r
1142                                 case 18: // done (update and write for current game)\r
1143                                         menu_options_save();\r
1144                                         if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");\r
1145                                         else strcpy(menuErrorMsg, "failed to write config");\r
1146                                         return 1;\r
1147                         }\r
1148                 }\r
1149                 if(inp & (GP2X_X|GP2X_A)) {\r
1150                         menu_options_save();\r
1151                         return 0;  // done (update, no write)\r
1152                 }\r
1153                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
1154                         switch (menu_sel) {\r
1155                                 case  0:\r
1156                                         if (inp & GP2X_LEFT) {\r
1157                                                 if      (  currentConfig.PicoOpt&0x10) currentConfig.PicoOpt&= ~0x10;\r
1158                                                 else if (!(currentConfig.EmuOpt &0x80))currentConfig.EmuOpt |=  0x80;\r
1159                                                 else if (  currentConfig.EmuOpt &0x80) break;\r
1160                                         } else {\r
1161                                                 if      (  currentConfig.PicoOpt&0x10) break;\r
1162                                                 else if (!(currentConfig.EmuOpt &0x80))currentConfig.PicoOpt|=  0x10;\r
1163                                                 else if (  currentConfig.EmuOpt &0x80) currentConfig.EmuOpt &= ~0x80;\r
1164                                         }\r
1165                                         break;\r
1166                                 case  1:\r
1167                                         currentConfig.scaling += (inp & GP2X_LEFT) ? -1 : 1;\r
1168                                         if (currentConfig.scaling < 0) currentConfig.scaling = 0;\r
1169                                         if (currentConfig.scaling > 3) currentConfig.scaling = 3;\r
1170                                         break;\r
1171                                 case  5:\r
1172                                         currentConfig.Frameskip += (inp & GP2X_LEFT) ? -1 : 1;\r
1173                                         if (currentConfig.Frameskip < 0)  currentConfig.Frameskip = -1;\r
1174                                         if (currentConfig.Frameskip > 32) currentConfig.Frameskip = 32;\r
1175                                         break;\r
1176                                 case  7:\r
1177                                         if ((inp & GP2X_RIGHT) && currentConfig.PsndRate == 44100 && !(currentConfig.PicoOpt&0x08)) {\r
1178                                                 currentConfig.PsndRate = 8000;  currentConfig.PicoOpt|= 0x08;\r
1179                                         } else if ((inp & GP2X_LEFT) && currentConfig.PsndRate == 8000 && (currentConfig.PicoOpt&0x08)) {\r
1180                                                 currentConfig.PsndRate = 44100; currentConfig.PicoOpt&=~0x08;\r
1181                                         } else currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & GP2X_RIGHT);\r
1182                                         break;\r
1183                                 case 10:\r
1184                                         region_prevnext(inp & GP2X_RIGHT);\r
1185                                         break;\r
1186                                 case 12: {\r
1187                                         int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);\r
1188                                         n += (inp & GP2X_LEFT) ? -1 : 1;\r
1189                                         if (n < 0) n = 0; else if (n > 3) n = 3;\r
1190                                         n |= n << 1; n &= ~2;\r
1191                                         currentConfig.EmuOpt &= ~0xa00;\r
1192                                         currentConfig.EmuOpt |= n << 9;\r
1193                                         break;\r
1194                                 }\r
1195                                 case 13:\r
1196                                         if (inp & GP2X_RIGHT) {\r
1197                                                 state_slot++; if (state_slot > 9) state_slot = 0;\r
1198                                         } else {state_slot--; if (state_slot < 0) state_slot = 9;\r
1199                                         }\r
1200                                         break;\r
1201                                 case 14:\r
1202                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
1203                                                 currentConfig.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;\r
1204                                                 if (currentConfig.CPUclock < 1) currentConfig.CPUclock = 1;\r
1205                                                 draw_menu_options(menu_sel);\r
1206                                                 usleep(50*1000);\r
1207                                         }\r
1208                                         break;\r
1209                         }\r
1210                 }\r
1211         }\r
1212 }\r
1213 \r
1214 // -------------- credits --------------\r
1215 \r
1216 static void draw_menu_credits(void)\r
1217 {\r
1218         int tl_x = 15, tl_y = 70, y;\r
1219         //memset(gp2x_screen, 0, 320*240);\r
1220         gp2x_pd_clone_buffer2();\r
1221 \r
1222         gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
1223         y = tl_y;\r
1224         gp2x_text_out8(tl_x, y, "Credits:");\r
1225         gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
1226         gp2x_text_out8(tl_x, (y+=10), "      base code of PicoDrive");\r
1227         gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
1228         gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
1229         gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
1230         gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
1231         gp2x_text_out8(tl_x, (y+=10), "      opensource Gens");\r
1232         gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
1233         gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
1234         gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
1235         gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
1236         gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
1237         gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
1238 \r
1239         gp2x_video_flip2();\r
1240 }\r
1241 \r
1242 \r
1243 // -------------- root menu --------------\r
1244 \r
1245 static void draw_menu_root(int menu_sel)\r
1246 {\r
1247         int tl_x = 70, tl_y = 70, y;\r
1248         //memset(gp2x_screen, 0, 320*240);\r
1249         gp2x_pd_clone_buffer2();\r
1250 \r
1251         gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
1252 \r
1253         y = tl_y;\r
1254         if (rom_data) {\r
1255                 gp2x_text_out8(tl_x, y,       "Resume game");\r
1256                 gp2x_text_out8(tl_x, (y+=10), "Save State");\r
1257                 gp2x_text_out8(tl_x, (y+=10), "Load State");\r
1258                 gp2x_text_out8(tl_x, (y+=10), "Reset game");\r
1259         } else {\r
1260                 y += 30;\r
1261         }\r
1262         gp2x_text_out8(tl_x, (y+=10), "Load new ROM/ISO");\r
1263         gp2x_text_out8(tl_x, (y+=10), "Change options");\r
1264         gp2x_text_out8(tl_x, (y+=10), "Configure controls");\r
1265         gp2x_text_out8(tl_x, (y+=10), "Credits");\r
1266         gp2x_text_out8(tl_x, (y+=10), "Exit");\r
1267         if (PicoPatches)\r
1268                 gp2x_text_out8(tl_x, (y+=10), "Patches / GameGenie");\r
1269 \r
1270         // draw cursor\r
1271         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
1272         // error\r
1273         if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
1274         gp2x_video_flip2();\r
1275 }\r
1276 \r
1277 \r
1278 static void menu_loop_root(void)\r
1279 {\r
1280         static int menu_sel = 4;\r
1281         int ret, menu_sel_max = 8, menu_sel_min = 4;\r
1282         unsigned long inp = 0;\r
1283         char curr_path[PATH_MAX], *selfname;\r
1284         FILE *tstf;\r
1285 \r
1286         if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
1287         {\r
1288                 fclose(tstf);\r
1289                 strcpy(curr_path, currentConfig.lastRomFile);\r
1290         }\r
1291         else\r
1292         {\r
1293                 getcwd(curr_path, PATH_MAX);\r
1294         }\r
1295 \r
1296         if (rom_data) menu_sel_min = 0;\r
1297         if (PicoPatches) menu_sel_max = 9;\r
1298 \r
1299         /* make sure action buttons are not pressed on entering menu */\r
1300         draw_menu_root(menu_sel);\r
1301         while (gp2x_joystick_read(1) & (GP2X_B|GP2X_X|GP2X_SELECT)) usleep(50*1000);\r
1302 \r
1303         for (;;)\r
1304         {\r
1305                 draw_menu_root(menu_sel);\r
1306                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT|GP2X_L|GP2X_R);\r
1307                 if(inp & GP2X_UP  )  { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
1308                 if(inp & GP2X_DOWN)  { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
1309                 if((inp & (GP2X_L|GP2X_R)) == (GP2X_L|GP2X_R)) debug_menu_loop();\r
1310                 if(inp &(GP2X_SELECT|GP2X_X)){\r
1311                         if (rom_data) {\r
1312                                 while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released\r
1313                                 engineState = PGS_Running;\r
1314                                 break;\r
1315                         }\r
1316                 }\r
1317                 if(inp & GP2X_B   )  {\r
1318                         switch (menu_sel) {\r
1319                                 case 0: // resume game\r
1320                                         if (rom_data) {\r
1321                                                 while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);\r
1322                                                 engineState = PGS_Running;\r
1323                                                 return;\r
1324                                         }\r
1325                                         break;\r
1326                                 case 1: // save state\r
1327                                         if (rom_data) {\r
1328                                                 if(savestate_menu_loop(0))\r
1329                                                         continue;\r
1330                                                 engineState = PGS_Running;\r
1331                                                 return;\r
1332                                         }\r
1333                                         break;\r
1334                                 case 2: // load state\r
1335                                         if (rom_data) {\r
1336                                                 if(savestate_menu_loop(1))\r
1337                                                         continue;\r
1338                                                 engineState = PGS_Running;\r
1339                                                 return;\r
1340                                         }\r
1341                                         break;\r
1342                                 case 3: // reset game\r
1343                                         if (rom_data) {\r
1344                                                 emu_ResetGame();\r
1345                                                 engineState = PGS_Running;\r
1346                                                 return;\r
1347                                         }\r
1348                                         break;\r
1349                                 case 4: // select rom\r
1350                                         selfname = romsel_loop(curr_path);\r
1351                                         if (selfname) {\r
1352                                                 printf("selected file: %s\n", selfname);\r
1353                                                 engineState = PGS_ReloadRom;\r
1354                                         }\r
1355                                         return;\r
1356                                 case 5: // options\r
1357                                         ret = menu_loop_options();\r
1358                                         if (ret == 1) continue; // status update\r
1359                                         if (engineState == PGS_ReloadRom)\r
1360                                                 return; // BIOS test\r
1361                                         break;\r
1362                                 case 6: // controls\r
1363                                         kc_sel_loop();\r
1364                                         break;\r
1365                                 case 7: // credits\r
1366                                         draw_menu_credits();\r
1367                                         usleep(500*1000);\r
1368                                         inp = wait_for_input(GP2X_B|GP2X_X);\r
1369                                         break;\r
1370                                 case 8: // exit\r
1371                                         engineState = PGS_Quit;\r
1372                                         return;\r
1373                                 case 9: // patches/gg\r
1374                                         if (rom_data && PicoPatches) {\r
1375                                                 patches_menu_loop();\r
1376                                                 PicoPatchApply();\r
1377                                                 strcpy(menuErrorMsg, "Patches applied");\r
1378                                                 continue;\r
1379                                         }\r
1380                                         break;\r
1381                         }\r
1382                 }\r
1383                 menuErrorMsg[0] = 0; // clear error msg\r
1384         }\r
1385 }\r
1386 \r
1387 \r
1388 static void menu_prepare_bg(void)\r
1389 {\r
1390         extern int localPal[0x100];\r
1391         int c, i;\r
1392 \r
1393         // don't clear old palette just for fun (but make it dark)\r
1394         for (i = 0x100-1; i >= 0; i--) {\r
1395                 c = localPal[i];\r
1396                 localPal[i] = ((c >> 1) & 0x007f7f7f) - ((c >> 3) & 0x001f1f1f);\r
1397         }\r
1398         localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
1399         localPal[0xf0] = 0x00ffffff;\r
1400 \r
1401         gp2x_video_setpalette(localPal, 0x100);\r
1402 }\r
1403 \r
1404 static void menu_gfx_prepare(void)\r
1405 {\r
1406         menu_prepare_bg();\r
1407 \r
1408         // switch to 8bpp\r
1409         gp2x_video_changemode2(8);\r
1410         gp2x_video_RGB_setscaling(0, 320, 240);\r
1411         gp2x_video_flip2();\r
1412 }\r
1413 \r
1414 \r
1415 void menu_loop(void)\r
1416 {\r
1417         menu_gfx_prepare();\r
1418 \r
1419         menu_loop_root();\r
1420 \r
1421         menuErrorMsg[0] = 0;\r
1422 }\r
1423 \r
1424 \r
1425 // --------- CD tray close menu ----------\r
1426 \r
1427 static void draw_menu_tray(int menu_sel)\r
1428 {\r
1429         int tl_x = 70, tl_y = 90, y;\r
1430         memset(gp2x_screen, 0xe0, 320*240);\r
1431 \r
1432         gp2x_text_out8(tl_x, 20, "The unit is about to");\r
1433         gp2x_text_out8(tl_x, 30, "close the CD tray.");\r
1434 \r
1435         y = tl_y;\r
1436         gp2x_text_out8(tl_x, y,       "Load new CD image");\r
1437         gp2x_text_out8(tl_x, (y+=10), "Insert nothing");\r
1438 \r
1439         // draw cursor\r
1440         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
1441         // error\r
1442         if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
1443         gp2x_video_flip2();\r
1444 }\r
1445 \r
1446 \r
1447 int menu_loop_tray(void)\r
1448 {\r
1449         int menu_sel = 0, menu_sel_max = 1;\r
1450         unsigned long inp = 0;\r
1451         char curr_path[PATH_MAX], *selfname;\r
1452         FILE *tstf;\r
1453 \r
1454         gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
1455         menu_gfx_prepare();\r
1456 \r
1457         if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
1458         {\r
1459                 fclose(tstf);\r
1460                 strcpy(curr_path, currentConfig.lastRomFile);\r
1461         }\r
1462         else\r
1463         {\r
1464                 getcwd(curr_path, PATH_MAX);\r
1465         }\r
1466 \r
1467         /* make sure action buttons are not pressed on entering menu */\r
1468         draw_menu_tray(menu_sel);\r
1469         while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);\r
1470 \r
1471         for (;;)\r
1472         {\r
1473                 draw_menu_tray(menu_sel);\r
1474                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B);\r
1475                 if(inp & GP2X_UP  )  { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
1476                 if(inp & GP2X_DOWN)  { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
1477                 if(inp & GP2X_B   )  {\r
1478                         switch (menu_sel) {\r
1479                                 case 0: // select image\r
1480                                         selfname = romsel_loop(curr_path);\r
1481                                         if (selfname) {\r
1482                                                 int ret = -1, cd_type;\r
1483                                                 cd_type = emu_cd_check(NULL);\r
1484                                                 if (cd_type > 0)\r
1485                                                         ret = Insert_CD(romFileName, cd_type == 2);\r
1486                                                 if (ret != 0) {\r
1487                                                         sprintf(menuErrorMsg, "Load failed, invalid CD image?");\r
1488                                                         printf("%s\n", menuErrorMsg);\r
1489                                                         continue;\r
1490                                                 }\r
1491                                                 engineState = PGS_RestartRun;\r
1492                                                 return 1;\r
1493                                         }\r
1494                                         break;\r
1495                                 case 1: // insert nothing\r
1496                                         engineState = PGS_RestartRun;\r
1497                                         return 0;\r
1498                         }\r
1499                 }\r
1500                 menuErrorMsg[0] = 0; // clear error msg\r
1501         }\r
1502 }\r
1503 \r
1504 \r