b127077add0d4bcf68a8b4e7f30298b48d9ce418
[picodrive.git] / platform / common / menu_pico.c
1 /*
2  * PicoDrive
3  * (C) notaz, 2010,2011
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 #include <stdio.h>
9 #include <string.h>
10 #include <time.h>
11
12 #include "emu.h"
13 #include "menu_pico.h"
14 #include "input_pico.h"
15 #include "version.h"
16
17 #include <pico/pico.h>
18 #include <pico/patch.h>
19
20 #ifdef PANDORA
21 #define MENU_X2 1
22 #else
23 #define MENU_X2 0
24 #endif
25
26 // FIXME
27 #define REVISION "0"
28
29 static const char *rom_exts[] = {
30         "zip",
31         "bin", "smd", "gen", "md",
32         "iso", "cso", "cue",
33         "32x",
34         "sms",
35         NULL
36 };
37
38 // rrrr rggg gggb bbbb
39 static unsigned short fname2color(const char *fname)
40 {
41         static const char *other_exts[] = { "gmv", "pat" };
42         const char *ext;
43         int i;
44
45         ext = strrchr(fname, '.');
46         if (ext++ == NULL) {
47                 ext = fname + strlen(fname) - 3;
48                 if (ext < fname) ext = fname;
49         }
50
51         for (i = 0; rom_exts[i] != NULL; i++)
52                 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff; // FIXME: mk defines
53         for (i = 0; i < array_size(other_exts); i++)
54                 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
55         return 0xffff;
56 }
57
58 #include "../libpicofe/menu.c"
59
60 static const char *men_dummy[] = { NULL };
61
62 /* platform specific options and handlers */
63 #if   defined(__GP2X__)
64 #include "../gp2x/menu.c"
65 #elif defined(PANDORA)
66 #include "../pandora/menu.c"
67 #else
68 #define MENU_OPTIONS_GFX
69 #define MENU_OPTIONS_ADV
70 #endif
71
72 static void make_bg(int no_scale)
73 {
74         unsigned short *src = (void *)g_menubg_src_ptr;
75         int w = g_screen_width, h = g_screen_height;
76         short *dst;
77         int x, y;
78
79         if (src == NULL) {
80                 memset(g_menubg_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
81                 return;
82         }
83
84         if (!no_scale && g_menuscreen_w / w >= 2 && g_menuscreen_h / h >= 2)
85         {
86                 unsigned int t, *d = g_menubg_ptr;
87                 d += (g_menuscreen_h / 2 - h * 2 / 2)
88                         * g_menuscreen_w / 2;
89                 d += (g_menuscreen_w / 2 - w * 2 / 2) / 2;
90                 for (y = 0; y < h; y++, src += w, d += g_menuscreen_w*2/2) {
91                         for (x = 0; x < w; x++) {
92                                 t = src[x];
93                                 t = ((t & 0xf79e)>>1) - ((t & 0xc618)>>3);
94                                 t |= t << 16;
95                                 d[x] = d[x + g_menuscreen_w / 2] = t;
96                         }
97                 }
98                 return;
99         }
100
101         if (w > g_menuscreen_w)
102                 w = g_menuscreen_w;
103         if (h > g_menuscreen_h)
104                 h = g_menuscreen_h;
105         dst = (short *)g_menubg_ptr +
106                 (g_menuscreen_h / 2 - h / 2) * g_menuscreen_w +
107                 (g_menuscreen_w / 2 - w / 2);
108
109         // darken the active framebuffer
110         for (; h > 0; dst += g_menuscreen_w, src += g_screen_width, h--)
111                 menu_darken_bg(dst, src, w, 1);
112 }
113
114 static void menu_enter(int is_rom_loaded)
115 {
116         if (is_rom_loaded)
117         {
118                 make_bg(0);
119         }
120         else
121         {
122                 int pos;
123                 char buff[256];
124                 pos = plat_get_skin_dir(buff, 256);
125                 strcpy(buff + pos, "background.png");
126
127                 // should really only happen once, on startup..
128                 if (readpng(g_menubg_ptr, buff, READPNG_BG,
129                                                 g_menuscreen_w, g_menuscreen_h) < 0)
130                         memset(g_menubg_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
131         }
132
133         plat_video_menu_enter(is_rom_loaded);
134 }
135
136 static void draw_savestate_bg(int slot)
137 {
138         const char *fname;
139         void *tmp_state;
140
141         fname = emu_get_save_fname(1, 0, slot, NULL);
142         if (!fname)
143                 return;
144
145         tmp_state = PicoTmpStateSave();
146
147         PicoStateLoadGfx(fname);
148
149         /* do a frame and fetch menu bg */
150         pemu_forced_frame(0, 0);
151
152         make_bg(0);
153
154         PicoTmpStateRestore(tmp_state);
155 }
156
157 // --------- loading ROM screen ----------
158
159 static int cdload_called = 0;
160
161 static void load_progress_cb(int percent)
162 {
163         int ln, len = percent * g_menuscreen_w / 100;
164         unsigned short *dst;
165
166         if (len > g_menuscreen_w)
167                 len = g_menuscreen_w;
168
169         menu_draw_begin(0, 1);
170         dst = (unsigned short *)g_menuscreen_ptr + g_menuscreen_w * me_sfont_h * 2;
171         for (ln = me_sfont_h - 2; ln > 0; ln--, dst += g_menuscreen_w)
172                 memset(dst, 0xff, len * 2);
173         menu_draw_end();
174 }
175
176 static void cdload_progress_cb(const char *fname, int percent)
177 {
178         int ln, len = percent * g_menuscreen_w / 100;
179         unsigned short *dst;
180
181         menu_draw_begin(0, 1);
182         dst = (unsigned short *)g_menuscreen_ptr + g_menuscreen_w * me_sfont_h * 2;
183         memset(dst, 0xff, g_menuscreen_w * (me_sfont_h - 2) * 2);
184
185         smalltext_out16(1, 3 * me_sfont_h, "Processing CD image / MP3s", 0xffff);
186         smalltext_out16(1, 4 * me_sfont_h, fname, 0xffff);
187         dst += g_menuscreen_w * me_sfont_h * 3;
188
189         if (len > g_menuscreen_w)
190                 len = g_menuscreen_w;
191
192         for (ln = (me_sfont_h - 2); ln > 0; ln--, dst += g_menuscreen_w)
193                 memset(dst, 0xff, len * 2);
194         menu_draw_end();
195
196         cdload_called = 1;
197 }
198
199 void menu_romload_prepare(const char *rom_name)
200 {
201         const char *p = rom_name + strlen(rom_name);
202         int i;
203
204         while (p > rom_name && *p != '/')
205                 p--;
206
207         /* fill all buffers, callbacks won't update in full */
208         for (i = 0; i < 3; i++) {
209                 menu_draw_begin(1, 1);
210                 smalltext_out16(1, 1, "Loading", 0xffff);
211                 smalltext_out16(1, me_sfont_h, p, 0xffff);
212                 menu_draw_end();
213         }
214
215         PicoCartLoadProgressCB = load_progress_cb;
216         PicoCDLoadProgressCB = cdload_progress_cb;
217         cdload_called = 0;
218 }
219
220 void menu_romload_end(void)
221 {
222         PicoCartLoadProgressCB = NULL;
223         PicoCDLoadProgressCB = NULL;
224
225         menu_draw_begin(0, 1);
226         smalltext_out16(1, (cdload_called ? 6 : 3) * me_sfont_h,
227                 "Starting emulation...", 0xffff);
228         menu_draw_end();
229 }
230
231 // ------------ patch/gg menu ------------
232
233 static void draw_patchlist(int sel)
234 {
235         int max_cnt, start, i, pos, active;
236
237         max_cnt = g_menuscreen_h / me_sfont_h;
238         start = max_cnt / 2 - sel;
239
240         menu_draw_begin(1, 0);
241
242         for (i = 0; i < PicoPatchCount; i++) {
243                 pos = start + i;
244                 if (pos < 0) continue;
245                 if (pos >= max_cnt) break;
246                 active = PicoPatches[i].active;
247                 smalltext_out16(14,                pos * me_sfont_h, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff);
248                 smalltext_out16(14 + me_sfont_w*4, pos * me_sfont_h, PicoPatches[i].name,    active ? 0xfff6 : 0xffff);
249         }
250         pos = start + i;
251         if (pos < max_cnt)
252                 smalltext_out16(14, pos * me_sfont_h, "done", 0xffff);
253
254         text_out16(5, max_cnt / 2 * me_sfont_h, ">");
255         menu_draw_end();
256 }
257
258 static void menu_loop_patches(void)
259 {
260         static int menu_sel = 0;
261         int inp;
262
263         for (;;)
264         {
265                 draw_patchlist(menu_sel);
266                 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R
267                                 |PBTN_MOK|PBTN_MBACK, NULL, 33);
268                 if (inp & PBTN_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
269                 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
270                 if (inp &(PBTN_LEFT|PBTN_L))  { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
271                 if (inp &(PBTN_RIGHT|PBTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
272                 if (inp & PBTN_MOK) { // action
273                         if (menu_sel < PicoPatchCount)
274                                 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
275                         else    break;
276                 }
277                 if (inp & PBTN_MBACK)
278                         break;
279         }
280 }
281
282 // -------------- key config --------------
283
284 // PicoPad[] format: MXYZ SACB RLDU
285 me_bind_action me_ctrl_actions[] =
286 {
287         { "UP     ", 0x0001 },
288         { "DOWN   ", 0x0002 },
289         { "LEFT   ", 0x0004 },
290         { "RIGHT  ", 0x0008 },
291         { "A      ", 0x0040 },
292         { "B      ", 0x0010 },
293         { "C      ", 0x0020 },
294         { "A turbo", 0x4000 },
295         { "B turbo", 0x1000 },
296         { "C turbo", 0x2000 },
297         { "START  ", 0x0080 },
298         { "MODE   ", 0x0800 },
299         { "X      ", 0x0400 },
300         { "Y      ", 0x0200 },
301         { "Z      ", 0x0100 },
302         { NULL,      0 },
303 };
304
305 me_bind_action emuctrl_actions[] =
306 {
307         { "Load State       ", PEV_STATE_LOAD },
308         { "Save State       ", PEV_STATE_SAVE },
309         { "Prev Save Slot   ", PEV_SSLOT_PREV },
310         { "Next Save Slot   ", PEV_SSLOT_NEXT },
311         { "Switch Renderer  ", PEV_SWITCH_RND },
312         { "Volume Down      ", PEV_VOL_DOWN },
313         { "Volume Up        ", PEV_VOL_UP },
314         { "Fast forward     ", PEV_FF },
315         { "Enter Menu       ", PEV_MENU },
316         { "Pico Next page   ", PEV_PICO_PNEXT },
317         { "Pico Prev page   ", PEV_PICO_PPREV },
318         { "Pico Switch input", PEV_PICO_SWINP },
319         { NULL,                0 }
320 };
321
322 static int key_config_loop_wrap(int id, int keys)
323 {
324         switch (id) {
325                 case MA_CTRL_PLAYER1:
326                         key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 0);
327                         break;
328                 case MA_CTRL_PLAYER2:
329                         key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 1);
330                         break;
331                 case MA_CTRL_EMU:
332                         key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);
333                         break;
334                 default:
335                         break;
336         }
337         return 0;
338 }
339
340 static const char *mgn_dev_name(int id, int *offs)
341 {
342         const char *name = NULL;
343         static int it = 0;
344
345         if (id == MA_CTRL_DEV_FIRST)
346                 it = 0;
347
348         for (; it < IN_MAX_DEVS; it++) {
349                 name = in_get_dev_name(it, 1, 1);
350                 if (name != NULL)
351                         break;
352         }
353
354         it++;
355         return name;
356 }
357
358 static int mh_saveloadcfg(int id, int keys);
359 static const char *mgn_saveloadcfg(int id, int *offs);
360
361 const char *indev_names[] = { "none", "3 button pad", "6 button pad", NULL };
362
363 static menu_entry e_menu_keyconfig[] =
364 {
365         mee_handler_id("Player 1",          MA_CTRL_PLAYER1,    key_config_loop_wrap),
366         mee_handler_id("Player 2",          MA_CTRL_PLAYER2,    key_config_loop_wrap),
367         mee_handler_id("Emulator controls", MA_CTRL_EMU,        key_config_loop_wrap),
368         mee_enum      ("Input device 1",    MA_OPT_INPUT_DEV0,  currentConfig.input_dev0, indev_names),
369         mee_enum      ("Input device 2",    MA_OPT_INPUT_DEV1,  currentConfig.input_dev1, indev_names),
370         mee_range     ("Turbo rate",        MA_CTRL_TURBO_RATE, currentConfig.turbo_rate, 1, 30),
371         mee_range     ("Analog deadzone",   MA_CTRL_DEADZONE,   currentConfig.analog_deadzone, 1, 99),
372         mee_cust_nosave("Save global config",       MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
373         mee_cust_nosave("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
374         mee_label     (""),
375         mee_label     ("Input devices:"),
376         mee_label_mk  (MA_CTRL_DEV_FIRST, mgn_dev_name),
377         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
378         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
379         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
380         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
381         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
382         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
383         mee_end,
384 };
385
386 static int menu_loop_keyconfig(int id, int keys)
387 {
388         static int sel = 0;
389
390         me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, PicoGameLoaded);
391         me_loop(e_menu_keyconfig, &sel);
392
393         PicoSetInputDevice(0, currentConfig.input_dev0);
394         PicoSetInputDevice(1, currentConfig.input_dev1);
395
396         return 0;
397 }
398
399 // ------------ SCD options menu ------------
400
401 static const char h_cdleds[] = "Show power/CD LEDs of emulated console";
402 static const char h_cdda[]   = "Play audio tracks from mp3s/wavs/bins";
403 static const char h_cdpcm[]  = "Emulate PCM audio chip for effects/voices/music";
404 static const char h_srcart[] = "Emulate the save RAM cartridge accessory\n"
405                                 "most games don't need this";
406 static const char h_scfx[]   = "Emulate scale/rotate ASIC chip for graphics effects\n"
407                                 "disable to improve performance";
408 static const char h_bsync[]  = "More accurate mode for CPUs (needed for some games)\n"
409                                 "disable to improve performance";
410
411 static menu_entry e_menu_cd_options[] =
412 {
413         mee_onoff_h("CD LEDs",              MA_CDOPT_LEDS,          currentConfig.EmuOpt, EOPT_EN_CD_LEDS, h_cdleds),
414         mee_onoff_h("CDDA audio",           MA_CDOPT_CDDA,          PicoOpt, POPT_EN_MCD_CDDA, h_cdda),
415         mee_onoff_h("PCM audio",            MA_CDOPT_PCM,           PicoOpt, POPT_EN_MCD_PCM, h_cdpcm),
416         mee_onoff_h("SaveRAM cart",         MA_CDOPT_SAVERAM,       PicoOpt, POPT_EN_MCD_RAMCART, h_srcart),
417         mee_onoff_h("Scale/Rot. fx",        MA_CDOPT_SCALEROT_CHIP, PicoOpt, POPT_EN_MCD_GFX, h_scfx),
418         mee_end,
419 };
420
421 static int menu_loop_cd_options(int id, int keys)
422 {
423         static int sel = 0;
424         me_loop(e_menu_cd_options, &sel);
425         return 0;
426 }
427
428 // ------------ 32X options menu ------------
429
430 #ifndef NO_32X
431
432 // convert from multiplier of VClk
433 static int mh_opt_sh2cycles(int id, int keys)
434 {
435         int *khz = (id == MA_32XOPT_MSH2_CYCLES) ?
436                 &currentConfig.msh2_khz : &currentConfig.ssh2_khz;
437
438         if (keys & (PBTN_LEFT|PBTN_RIGHT))
439                 *khz += (keys & PBTN_LEFT) ? -50 : 50;
440         if (keys & (PBTN_L|PBTN_R))
441                 *khz += (keys & PBTN_L) ? -500 : 500;
442
443         if (*khz < 1)
444                 *khz = 1;
445         else if (*khz > 0x7fffffff / 1000)
446                 *khz = 0x7fffffff / 1000;
447
448         return 0;
449 }
450
451 static const char *mgn_opt_sh2cycles(int id, int *offs)
452 {
453         int khz = (id == MA_32XOPT_MSH2_CYCLES) ?
454                 currentConfig.msh2_khz : currentConfig.ssh2_khz;
455
456         sprintf(static_buff, "%d", khz);
457         return static_buff;
458 }
459
460 static const char h_32x_enable[] = "Enable emulation of the 32X addon";
461 static const char h_pwm[]        = "Disabling may improve performance, but break sound";
462 static const char h_sh2cycles[]  = "Cycles/millisecond (similar to DOSBox)\n"
463                                    "lower values speed up emulation but break games\n"
464                                    "at least 11000 recommended for compatibility";
465
466 static menu_entry e_menu_32x_options[] =
467 {
468         mee_onoff_h   ("32X enabled",       MA_32XOPT_ENABLE_32X,  PicoOpt, POPT_EN_32X, h_32x_enable),
469         mee_enum      ("32X renderer",      MA_32XOPT_RENDERER,    currentConfig.renderer32x, renderer_names32x),
470         mee_onoff_h   ("PWM sound",         MA_32XOPT_PWM,         PicoOpt, POPT_EN_PWM, h_pwm),
471         mee_cust_h    ("Master SH2 cycles", MA_32XOPT_MSH2_CYCLES, mh_opt_sh2cycles, mgn_opt_sh2cycles, h_sh2cycles),
472         mee_cust_h    ("Slave SH2 cycles",  MA_32XOPT_SSH2_CYCLES, mh_opt_sh2cycles, mgn_opt_sh2cycles, h_sh2cycles),
473         mee_end,
474 };
475
476 static int menu_loop_32x_options(int id, int keys)
477 {
478         static int sel = 0;
479
480         me_enable(e_menu_32x_options, MA_32XOPT_RENDERER, renderer_names32x[0] != NULL);
481         me_loop(e_menu_32x_options, &sel);
482
483         Pico32xSetClocks(currentConfig.msh2_khz * 1000, currentConfig.msh2_khz * 1000);
484
485         return 0;
486 }
487
488 #endif
489
490 // ------------ adv options menu ------------
491
492 static menu_entry e_menu_adv_options[] =
493 {
494         mee_onoff     ("SRAM/BRAM saves",          MA_OPT_SRAM_STATES,    currentConfig.EmuOpt, EOPT_EN_SRAM),
495         mee_onoff     ("Disable sprite limit",     MA_OPT2_NO_SPRITE_LIM, PicoOpt, POPT_DIS_SPRITE_LIM),
496         mee_onoff     ("Emulate Z80",              MA_OPT2_ENABLE_Z80,    PicoOpt, POPT_EN_Z80),
497         mee_onoff     ("Emulate YM2612 (FM)",      MA_OPT2_ENABLE_YM2612, PicoOpt, POPT_EN_FM),
498         mee_onoff     ("Emulate SN76496 (PSG)",    MA_OPT2_ENABLE_SN76496,PicoOpt, POPT_EN_PSG),
499         mee_onoff     ("gzip savestates",          MA_OPT2_GZIP_STATES,   currentConfig.EmuOpt, EOPT_GZIP_SAVES),
500         mee_onoff     ("Don't save last used ROM", MA_OPT2_NO_LAST_ROM,   currentConfig.EmuOpt, EOPT_NO_AUTOSVCFG),
501         mee_onoff     ("Disable idle loop patching",MA_OPT2_NO_IDLE_LOOPS,PicoOpt, POPT_DIS_IDLE_DET),
502         mee_onoff     ("Disable frame limiter",    MA_OPT2_NO_FRAME_LIMIT,currentConfig.EmuOpt, EOPT_NO_FRMLIMIT),
503         mee_onoff     ("Enable dynarecs",          MA_OPT2_DYNARECS,      PicoOpt, POPT_EN_DRC),
504         mee_onoff     ("Status line in main menu", MA_OPT2_STATUS_LINE,   currentConfig.EmuOpt, EOPT_SHOW_RTC),
505         MENU_OPTIONS_ADV
506         mee_end,
507 };
508
509 static int menu_loop_adv_options(int id, int keys)
510 {
511         static int sel = 0;
512         me_loop(e_menu_adv_options, &sel);
513         return 0;
514 }
515
516 // ------------ gfx options menu ------------
517
518 static const char h_gamma[] = "Gamma/brightness adjustment (default 1.00)";
519
520 static const char *mgn_aopt_gamma(int id, int *offs)
521 {
522         sprintf(static_buff, "%i.%02i", currentConfig.gamma / 100, currentConfig.gamma % 100);
523         return static_buff;
524 }
525
526 static menu_entry e_menu_gfx_options[] =
527 {
528         mee_enum   ("Video output mode", MA_OPT_VOUT_MODE, plat_target.vout_method, men_dummy),
529         mee_enum   ("Renderer",          MA_OPT_RENDERER, currentConfig.renderer, renderer_names),
530         mee_enum   ("Filter",            MA_OPT3_FILTERING, currentConfig.filter, men_dummy),
531         mee_range_cust_h("Gamma correction", MA_OPT2_GAMMA, currentConfig.gamma, 1, 300, mgn_aopt_gamma, h_gamma),
532         MENU_OPTIONS_GFX
533         mee_end,
534 };
535
536 static int menu_loop_gfx_options(int id, int keys)
537 {
538         static int sel = 0;
539
540         me_enable(e_menu_gfx_options, MA_OPT_RENDERER, renderer_names[0] != NULL);
541         me_loop(e_menu_gfx_options, &sel);
542
543         return 0;
544 }
545
546 // ------------ options menu ------------
547
548 static menu_entry e_menu_options[];
549
550 static int sndrate_prevnext(int rate, int dir)
551 {
552         static const int rates[] = { 8000, 11025, 16000, 22050, 44100 };
553         int i;
554
555         for (i = 0; i < 5; i++)
556                 if (rates[i] == rate) break;
557
558         i += dir ? 1 : -1;
559         if (i > 4) {
560                 if (!(PicoOpt & POPT_EN_STEREO)) {
561                         PicoOpt |= POPT_EN_STEREO;
562                         return rates[0];
563                 }
564                 return rates[4];
565         }
566         if (i < 0) {
567                 if (PicoOpt & POPT_EN_STEREO) {
568                         PicoOpt &= ~POPT_EN_STEREO;
569                         return rates[4];
570                 }
571                 return rates[0];
572         }
573         return rates[i];
574 }
575
576 static void region_prevnext(int right)
577 {
578         // jp_ntsc=1, jp_pal=2, usa=4, eu=8
579         static const int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
580         int i;
581
582         if (right) {
583                 if (!PicoRegionOverride) {
584                         for (i = 0; i < 6; i++)
585                                 if (rgn_orders[i] == PicoAutoRgnOrder) break;
586                         if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
587                         else PicoRegionOverride=1;
588                 }
589                 else
590                         PicoRegionOverride <<= 1;
591                 if (PicoRegionOverride > 8)
592                         PicoRegionOverride = 8;
593         } else {
594                 if (!PicoRegionOverride) {
595                         for (i = 0; i < 6; i++)
596                                 if (rgn_orders[i] == PicoAutoRgnOrder) break;
597                         if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
598                 }
599                 else
600                         PicoRegionOverride >>= 1;
601         }
602 }
603
604 static int mh_opt_misc(int id, int keys)
605 {
606         switch (id) {
607         case MA_OPT_SOUND_QUALITY:
608                 PsndRate = sndrate_prevnext(PsndRate, keys & PBTN_RIGHT);
609                 break;
610         case MA_OPT_REGION:
611                 region_prevnext(keys & PBTN_RIGHT);
612                 break;
613         default:
614                 break;
615         }
616         return 0;
617 }
618
619 static int mh_saveloadcfg(int id, int keys)
620 {
621         int ret;
622
623         if (keys & (PBTN_LEFT|PBTN_RIGHT)) { // multi choice
624                 config_slot += (keys & PBTN_LEFT) ? -1 : 1;
625                 if (config_slot < 0) config_slot = 9;
626                 else if (config_slot > 9) config_slot = 0;
627                 me_enable(e_menu_options, MA_OPT_LOADCFG, config_slot != config_slot_current);
628                 return 0;
629         }
630
631         switch (id) {
632         case MA_OPT_SAVECFG:
633         case MA_OPT_SAVECFG_GAME:
634                 if (emu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0))
635                         menu_update_msg("config saved");
636                 else
637                         menu_update_msg("failed to write config");
638                 break;
639         case MA_OPT_LOADCFG:
640                 ret = emu_read_config(rom_fname_loaded, 1);
641                 if (!ret) ret = emu_read_config(NULL, 1);
642                 if (ret)  menu_update_msg("config loaded");
643                 else      menu_update_msg("failed to load config");
644                 break;
645         default:
646                 return 0;
647         }
648
649         return 1;
650 }
651
652 static int mh_restore_defaults(int id, int keys)
653 {
654         emu_set_defconfig();
655         menu_update_msg("defaults restored");
656         return 1;
657 }
658
659 static const char *mgn_opt_fskip(int id, int *offs)
660 {
661         if (currentConfig.Frameskip < 0)
662                 return "Auto";
663         sprintf(static_buff, "%d", currentConfig.Frameskip);
664         return static_buff;
665 }
666
667 static const char *mgn_opt_sound(int id, int *offs)
668 {
669         const char *str2;
670         *offs = -8;
671         str2 = (PicoOpt & POPT_EN_STEREO) ? "stereo" : "mono";
672         sprintf(static_buff, "%5iHz %s", PsndRate, str2);
673         return static_buff;
674 }
675
676 static const char *mgn_opt_region(int id, int *offs)
677 {
678         static const char *names[] = { "Auto", "      Japan NTSC", "      Japan PAL", "      USA", "      Europe" };
679         static const char *names_short[] = { "", " JP", " JP", " US", " EU" };
680         int code = PicoRegionOverride;
681         int u, i = 0;
682
683         *offs = -6;
684         if (code) {
685                 code <<= 1;
686                 while ((code >>= 1)) i++;
687                 if (i > 4)
688                         return "unknown";
689                 return names[i];
690         } else {
691                 strcpy(static_buff, "Auto:");
692                 for (u = 0; u < 3; u++) {
693                         code = (PicoAutoRgnOrder >> u*4) & 0xf;
694                         for (i = 0; code; code >>= 1, i++)
695                                 ;
696                         strcat(static_buff, names_short[i]);
697                 }
698                 return static_buff;
699         }
700 }
701
702 static const char *mgn_saveloadcfg(int id, int *offs)
703 {
704         static_buff[0] = 0;
705         if (config_slot != 0)
706                 sprintf(static_buff, "[%i]", config_slot);
707         return static_buff;
708 }
709
710 static const char *men_confirm_save[] = { "OFF", "writes", "loads", "both", NULL };
711 static const char h_confirm_save[]    = "Ask for confirmation when overwriting save,\n"
712                                         "loading state or both";
713
714 static menu_entry e_menu_options[] =
715 {
716         mee_range     ("Save slot",                MA_OPT_SAVE_SLOT,     state_slot, 0, 9),
717         mee_range_cust("Frameskip",                MA_OPT_FRAMESKIP,     currentConfig.Frameskip, -1, 16, mgn_opt_fskip),
718         mee_cust      ("Region",                   MA_OPT_REGION,        mh_opt_misc, mgn_opt_region),
719         mee_onoff     ("Show FPS",                 MA_OPT_SHOW_FPS,      currentConfig.EmuOpt, EOPT_SHOW_FPS),
720         mee_onoff     ("Enable sound",             MA_OPT_ENABLE_SOUND,  currentConfig.EmuOpt, EOPT_EN_SOUND),
721         mee_cust      ("Sound Quality",            MA_OPT_SOUND_QUALITY, mh_opt_misc, mgn_opt_sound),
722         mee_enum_h    ("Confirm savestate",        MA_OPT_CONFIRM_STATES,currentConfig.confirm_save, men_confirm_save, h_confirm_save),
723         mee_range     ("",                         MA_OPT_CPU_CLOCKS,    currentConfig.CPUclock, 20, 3200),
724         mee_handler   ("[Display options]",        menu_loop_gfx_options),
725         mee_handler   ("[Sega/Mega CD options]",   menu_loop_cd_options),
726 #ifndef NO_32X
727         mee_handler   ("[32X options]",            menu_loop_32x_options),
728 #endif
729         mee_handler   ("[Advanced options]",       menu_loop_adv_options),
730         mee_cust_nosave("Save global config",      MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
731         mee_cust_nosave("Save cfg for loaded game",MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
732         mee_cust_nosave("Load cfg from profile",   MA_OPT_LOADCFG, mh_saveloadcfg, mgn_saveloadcfg),
733         mee_handler   ("Restore defaults",         mh_restore_defaults),
734         mee_end,
735 };
736
737 static int menu_loop_options(int id, int keys)
738 {
739         static int sel = 0;
740
741         me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, PicoGameLoaded);
742         me_enable(e_menu_options, MA_OPT_LOADCFG, config_slot != config_slot_current);
743
744         me_loop(e_menu_options, &sel);
745
746         return 0;
747 }
748
749 // ------------ debug menu ------------
750
751 #include <pico/debug.h>
752
753 extern void SekStepM68k(void);
754
755 static void mplayer_loop(void)
756 {
757         pemu_sound_start();
758
759         while (1)
760         {
761                 PDebugZ80Frame();
762                 if (in_menu_wait_any(NULL, 0) & PBTN_MA3)
763                         break;
764                 emu_sound_wait();
765         }
766
767         emu_sound_stop();
768 }
769
770 static void draw_text_debug(const char *str, int skip, int from)
771 {
772         const char *p;
773         int line;
774
775         p = str;
776         while (skip-- > 0)
777         {
778                 while (*p && *p != '\n')
779                         p++;
780                 if (*p == 0 || p[1] == 0)
781                         return;
782                 p++;
783         }
784
785         str = p;
786         for (line = from; line < g_menuscreen_h / me_sfont_h; line++)
787         {
788                 smalltext_out16(1, line * me_sfont_h, str, 0xffff);
789                 while (*p && *p != '\n')
790                         p++;
791                 if (*p == 0)
792                         break;
793                 p++; str = p;
794         }
795 }
796
797 #ifdef __GNUC__
798 #define COMPILER "gcc " __VERSION__
799 #else
800 #define COMPILER
801 #endif
802
803 static void draw_frame_debug(void)
804 {
805         char layer_str[48] = "layers:                   ";
806         if (PicoDrawMask & PDRAW_LAYERB_ON)      memcpy(layer_str +  8, "B", 1);
807         if (PicoDrawMask & PDRAW_LAYERA_ON)      memcpy(layer_str + 10, "A", 1);
808         if (PicoDrawMask & PDRAW_SPRITES_LOW_ON) memcpy(layer_str + 12, "spr_lo", 6);
809         if (PicoDrawMask & PDRAW_SPRITES_HI_ON)  memcpy(layer_str + 19, "spr_hi", 6);
810         if (PicoDrawMask & PDRAW_32X_ON)         memcpy(layer_str + 26, "32x", 4);
811
812         pemu_forced_frame(1, 0);
813         make_bg(1);
814
815         smalltext_out16(4, 1, "build: r" REVISION "  "__DATE__ " " __TIME__ " " COMPILER, 0xffff);
816         smalltext_out16(4, g_menuscreen_h - me_sfont_h, layer_str, 0xffff);
817 }
818
819 static void debug_menu_loop(void)
820 {
821         int inp, mode = 0;
822         int spr_offs = 0, dumped = 0;
823         char *tmp;
824
825         while (1)
826         {
827                 menu_draw_begin(1, 0);
828                 switch (mode)
829                 {
830                         case 0: tmp = PDebugMain();
831                                 plat_debug_cat(tmp);
832                                 draw_text_debug(tmp, 0, 0);
833                                 if (dumped) {
834                                         smalltext_out16(g_menuscreen_w - 6 * me_sfont_h,
835                                                 g_menuscreen_h - me_mfont_h, "dumped", 0xffff);
836                                         dumped = 0;
837                                 }
838                                 break;
839                         case 1: draw_frame_debug();
840                                 break;
841                         case 2: pemu_forced_frame(1, 0);
842                                 make_bg(1);
843                                 PDebugShowSpriteStats((unsigned short *)g_menuscreen_ptr + (g_menuscreen_h/2 - 240/2)*g_menuscreen_w +
844                                         g_menuscreen_w/2 - 320/2, g_menuscreen_w);
845                                 break;
846                         case 3: memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
847                                 PDebugShowPalette(g_menuscreen_ptr, g_menuscreen_w);
848                                 PDebugShowSprite((unsigned short *)g_menuscreen_ptr + g_menuscreen_w*120 + g_menuscreen_w/2 + 16,
849                                         g_menuscreen_w, spr_offs);
850                                 draw_text_debug(PDebugSpriteList(), spr_offs, 6);
851                                 break;
852                         case 4: tmp = PDebug32x();
853                                 draw_text_debug(tmp, 0, 0);
854                                 break;
855                 }
856                 menu_draw_end();
857
858                 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |
859                                         PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, NULL, 70);
860                 if (inp & PBTN_MBACK) return;
861                 if (inp & PBTN_L) { mode--; if (mode < 0) mode = 4; }
862                 if (inp & PBTN_R) { mode++; if (mode > 4) mode = 0; }
863                 switch (mode)
864                 {
865                         case 0:
866                                 if (inp & PBTN_MOK)
867                                         PDebugCPUStep();
868                                 if (inp & PBTN_MA3) {
869                                         while (inp & PBTN_MA3)
870                                                 inp = in_menu_wait_any(NULL, -1);
871                                         mplayer_loop();
872                                 }
873                                 if ((inp & (PBTN_MA2|PBTN_LEFT)) == (PBTN_MA2|PBTN_LEFT)) {
874                                         mkdir("dumps", 0777);
875                                         PDebugDumpMem();
876                                         while (inp & PBTN_MA2) inp = in_menu_wait_any(NULL, -1);
877                                         dumped = 1;
878                                 }
879                                 break;
880                         case 1:
881                                 if (inp & PBTN_LEFT)  PicoDrawMask ^= PDRAW_LAYERB_ON;
882                                 if (inp & PBTN_RIGHT) PicoDrawMask ^= PDRAW_LAYERA_ON;
883                                 if (inp & PBTN_DOWN)  PicoDrawMask ^= PDRAW_SPRITES_LOW_ON;
884                                 if (inp & PBTN_UP)    PicoDrawMask ^= PDRAW_SPRITES_HI_ON;
885                                 if (inp & PBTN_MA2)   PicoDrawMask ^= PDRAW_32X_ON;
886                                 if (inp & PBTN_MOK) {
887                                         PsndOut = NULL; // just in case
888                                         PicoSkipFrame = 1;
889                                         PicoFrame();
890                                         PicoSkipFrame = 0;
891                                         while (inp & PBTN_MOK) inp = in_menu_wait_any(NULL, -1);
892                                 }
893                                 break;
894                         case 3:
895                                 if (inp & PBTN_DOWN)  spr_offs++;
896                                 if (inp & PBTN_UP)    spr_offs--;
897                                 if (spr_offs < 0) spr_offs = 0;
898                                 break;
899                 }
900         }
901 }
902
903 // ------------ main menu ------------
904
905 static void draw_frame_credits(void)
906 {
907         smalltext_out16(4, 1, "build: " __DATE__ " " __TIME__, 0xe7fc);
908 }
909
910 static const char credits[] =
911         "PicoDrive v" VERSION " (c) notaz, 2006-2013\n\n\n"
912         "Credits:\n"
913         "fDave: initial code\n"
914 #ifdef EMU_C68K
915         "      Cyclone 68000 core\n"
916 #else
917         "Stef, Chui: FAME/C 68k core\n"
918 #endif
919 #ifdef _USE_DRZ80
920         "Reesy & FluBBa: DrZ80 core\n"
921 #else
922         "Stef, NJ: CZ80 core\n"
923 #endif
924         "MAME devs: SH2, YM2612 and SN76496 cores\n"
925         "Eke, Stef: some Sega CD code\n"
926         "Inder, ketchupgun: graphics\n"
927 #ifdef __GP2X__
928         "Squidge: mmuhack\n"
929         "Dzz: ARM940 sample\n"
930 #endif
931         "\n"
932         "special thanks (for docs, ideas):\n"
933         " Charles MacDonald, Haze,\n"
934         " Stephane Dallongeville,\n"
935         " Lordus, Exophase, Rokas,\n"
936         " Eke, Nemesis, Tasco Deluxe";
937
938 static void menu_main_draw_status(void)
939 {
940         static time_t last_bat_read = 0;
941         static int last_bat_val = -1;
942         unsigned short *bp = g_screen_ptr;
943         int bat_h = me_mfont_h * 2 / 3;
944         int i, u, w, wfill, batt_val;
945         struct tm *tmp;
946         time_t ltime;
947         char time_s[16];
948
949         if (!(currentConfig.EmuOpt & EOPT_SHOW_RTC))
950                 return;
951
952         ltime = time(NULL);
953         tmp = gmtime(&ltime);
954         strftime(time_s, sizeof(time_s), "%H:%M", tmp);
955
956         text_out16(g_screen_width - me_mfont_w * 6, me_mfont_h + 2, time_s);
957
958         if (ltime - last_bat_read > 10) {
959                 last_bat_read = ltime;
960                 last_bat_val = batt_val = plat_target_bat_capacity_get();
961         }
962         else
963                 batt_val = last_bat_val;
964
965         if (batt_val < 0 || batt_val > 100)
966                 return;
967
968         /* battery info */
969         bp += (me_mfont_h * 2 + 2) * g_screen_width + g_screen_width - me_mfont_w * 3 - 3;
970         for (i = 0; i < me_mfont_w * 2; i++)
971                 bp[i] = menu_text_color;
972         for (i = 0; i < me_mfont_w * 2; i++)
973                 bp[i + g_screen_width * bat_h] = menu_text_color;
974         for (i = 0; i <= bat_h; i++)
975                 bp[i * g_screen_width] =
976                 bp[i * g_screen_width + me_mfont_w * 2] = menu_text_color;
977         for (i = 2; i < bat_h - 1; i++)
978                 bp[i * g_screen_width - 1] =
979                 bp[i * g_screen_width - 2] = menu_text_color;
980
981         w = me_mfont_w * 2 - 1;
982         wfill = batt_val * w / 100;
983         for (u = 1; u < bat_h; u++)
984                 for (i = 0; i < wfill; i++)
985                         bp[(w - i) + g_screen_width * u] = menu_text_color;
986 }
987
988 static int main_menu_handler(int id, int keys)
989 {
990         const char *ret_name;
991
992         switch (id)
993         {
994         case MA_MAIN_RESUME_GAME:
995                 if (PicoGameLoaded)
996                         return 1;
997                 break;
998         case MA_MAIN_SAVE_STATE:
999                 if (PicoGameLoaded)
1000                         return menu_loop_savestate(0);
1001                 break;
1002         case MA_MAIN_LOAD_STATE:
1003                 if (PicoGameLoaded)
1004                         return menu_loop_savestate(1);
1005                 break;
1006         case MA_MAIN_RESET_GAME:
1007                 if (PicoGameLoaded) {
1008                         emu_reset_game();
1009                         return 1;
1010                 }
1011                 break;
1012         case MA_MAIN_LOAD_ROM:
1013                 rom_fname_reload = NULL;
1014                 ret_name = menu_loop_romsel(rom_fname_loaded,
1015                         sizeof(rom_fname_loaded), rom_exts, NULL);
1016                 if (ret_name != NULL) {
1017                         lprintf("selected file: %s\n", ret_name);
1018                         rom_fname_reload = ret_name;
1019                         engineState = PGS_ReloadRom;
1020                         return 1;
1021                 }
1022                 break;
1023         case MA_MAIN_CHANGE_CD:
1024                 if (PicoAHW & PAHW_MCD) {
1025                         // if cd is loaded, cdd_unload() triggers eject and
1026                         // returns 1, else we'll select and load new CD here
1027                         if (!cdd_unload())
1028                                 menu_loop_tray();
1029                         return 1;
1030                 }
1031                 break;
1032         case MA_MAIN_CREDITS:
1033                 draw_menu_message(credits, draw_frame_credits);
1034                 in_menu_wait(PBTN_MOK|PBTN_MBACK, NULL, 70);
1035                 break;
1036         case MA_MAIN_EXIT:
1037                 engineState = PGS_Quit;
1038                 return 1;
1039         case MA_MAIN_PATCHES:
1040                 if (PicoGameLoaded && PicoPatches) {
1041                         menu_loop_patches();
1042                         PicoPatchApply();
1043                         menu_update_msg("Patches applied");
1044                 }
1045                 break;
1046         default:
1047                 lprintf("%s: something unknown selected\n", __FUNCTION__);
1048                 break;
1049         }
1050
1051         return 0;
1052 }
1053
1054 static menu_entry e_menu_main[] =
1055 {
1056         mee_label     ("PicoDrive " VERSION),
1057         mee_label     (""),
1058         mee_label     (""),
1059         mee_label     (""),
1060         mee_handler_id("Resume game",        MA_MAIN_RESUME_GAME, main_menu_handler),
1061         mee_handler_id("Save State",         MA_MAIN_SAVE_STATE,  main_menu_handler),
1062         mee_handler_id("Load State",         MA_MAIN_LOAD_STATE,  main_menu_handler),
1063         mee_handler_id("Reset game",         MA_MAIN_RESET_GAME,  main_menu_handler),
1064         mee_handler_id("Load new ROM/ISO",   MA_MAIN_LOAD_ROM,    main_menu_handler),
1065         mee_handler_id("Change CD/ISO",      MA_MAIN_CHANGE_CD,   main_menu_handler),
1066         mee_handler   ("Change options",                          menu_loop_options),
1067         mee_handler   ("Configure controls",                      menu_loop_keyconfig),
1068         mee_handler_id("Credits",            MA_MAIN_CREDITS,     main_menu_handler),
1069         mee_handler_id("Patches / GameGenie",MA_MAIN_PATCHES,     main_menu_handler),
1070         mee_handler_id("Exit",               MA_MAIN_EXIT,        main_menu_handler),
1071         mee_end,
1072 };
1073
1074 void menu_loop(void)
1075 {
1076         static int sel = 0;
1077
1078         me_enable(e_menu_main, MA_MAIN_RESUME_GAME, PicoGameLoaded);
1079         me_enable(e_menu_main, MA_MAIN_SAVE_STATE,  PicoGameLoaded);
1080         me_enable(e_menu_main, MA_MAIN_LOAD_STATE,  PicoGameLoaded);
1081         me_enable(e_menu_main, MA_MAIN_RESET_GAME,  PicoGameLoaded);
1082         me_enable(e_menu_main, MA_MAIN_CHANGE_CD,   PicoAHW & PAHW_MCD);
1083         me_enable(e_menu_main, MA_MAIN_PATCHES, PicoPatches != NULL);
1084
1085         menu_enter(PicoGameLoaded);
1086         in_set_config_int(0, IN_CFG_BLOCKING, 1);
1087         me_loop_d(e_menu_main, &sel, NULL, menu_main_draw_status);
1088
1089         if (PicoGameLoaded) {
1090                 if (engineState == PGS_Menu)
1091                         engineState = PGS_Running;
1092                 /* wait until menu, ok, back is released */
1093                 while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
1094                         ;
1095         }
1096
1097         in_set_config_int(0, IN_CFG_BLOCKING, 0);
1098         plat_video_menu_leave();
1099 }
1100
1101 // --------- CD tray close menu ----------
1102
1103 static int mh_tray_load_cd(int id, int keys)
1104 {
1105         const char *ret_name;
1106
1107         rom_fname_reload = NULL;
1108         ret_name = menu_loop_romsel(rom_fname_loaded,
1109                         sizeof(rom_fname_loaded), rom_exts, NULL);
1110         if (ret_name == NULL)
1111                 return 0;
1112
1113         rom_fname_reload = ret_name;
1114         engineState = PGS_RestartRun;
1115         return emu_swap_cd(ret_name);
1116 }
1117
1118 static int mh_tray_nothing(int id, int keys)
1119 {
1120         return 1;
1121 }
1122
1123 static menu_entry e_menu_tray[] =
1124 {
1125         mee_label  ("The CD tray has opened."),
1126         mee_label  (""),
1127         mee_label  (""),
1128         mee_handler("Load CD image",  mh_tray_load_cd),
1129         mee_handler("Insert nothing", mh_tray_nothing),
1130         mee_end,
1131 };
1132
1133 int menu_loop_tray(void)
1134 {
1135         int ret = 1, sel = 0;
1136
1137         menu_enter(PicoGameLoaded);
1138
1139         in_set_config_int(0, IN_CFG_BLOCKING, 1);
1140         me_loop(e_menu_tray, &sel);
1141
1142         if (engineState != PGS_RestartRun) {
1143                 engineState = PGS_RestartRun;
1144                 ret = 0; /* no CD inserted */
1145         }
1146
1147         while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
1148                 ;
1149         in_set_config_int(0, IN_CFG_BLOCKING, 0);
1150         plat_video_menu_leave();
1151
1152         return ret;
1153 }
1154
1155 void menu_update_msg(const char *msg)
1156 {
1157         strncpy(menu_error_msg, msg, sizeof(menu_error_msg));
1158         menu_error_msg[sizeof(menu_error_msg) - 1] = 0;
1159
1160         menu_error_time = plat_get_ticks_ms();
1161         lprintf("msg: %s\n", menu_error_msg);
1162 }
1163
1164 // ------------ util ------------
1165
1166 /* hidden options for config engine only */
1167 static menu_entry e_menu_hidden[] =
1168 {
1169         mee_onoff("Accurate sprites", MA_OPT_ACC_SPRITES, PicoOpt, 0x080),
1170         mee_onoff("autoload savestates", MA_OPT_AUTOLOAD_SAVE, g_autostateld_opt, 1),
1171         mee_end,
1172 };
1173
1174 static menu_entry *e_menu_table[] =
1175 {
1176         e_menu_options,
1177         e_menu_gfx_options,
1178         e_menu_adv_options,
1179         e_menu_cd_options,
1180 #ifndef NO_32X
1181         e_menu_32x_options,
1182 #endif
1183         e_menu_keyconfig,
1184         e_menu_hidden,
1185 };
1186
1187 static menu_entry *me_list_table = NULL;
1188 static menu_entry *me_list_i = NULL;
1189
1190 menu_entry *me_list_get_first(void)
1191 {
1192         me_list_table = me_list_i = e_menu_table[0];
1193         return me_list_i;
1194 }
1195
1196 menu_entry *me_list_get_next(void)
1197 {
1198         int i;
1199
1200         me_list_i++;
1201         if (me_list_i->name != NULL)
1202                 return me_list_i;
1203
1204         for (i = 0; i < array_size(e_menu_table); i++)
1205                 if (me_list_table == e_menu_table[i])
1206                         break;
1207
1208         if (i + 1 < array_size(e_menu_table))
1209                 me_list_table = me_list_i = e_menu_table[i + 1];
1210         else
1211                 me_list_table = me_list_i = NULL;
1212
1213         return me_list_i;
1214 }
1215
1216 void menu_init(void)
1217 {
1218         int i;
1219
1220         menu_init_base();
1221
1222         i = 0;
1223 #if defined(_SVP_DRC) || defined(DRC_SH2)
1224         i = 1;
1225 #endif
1226         me_enable(e_menu_adv_options, MA_OPT2_DYNARECS, i);
1227
1228         i = me_id2offset(e_menu_gfx_options, MA_OPT_VOUT_MODE);
1229         e_menu_gfx_options[i].data = plat_target.vout_methods;
1230         me_enable(e_menu_gfx_options, MA_OPT_VOUT_MODE,
1231                 plat_target.vout_methods != NULL);
1232
1233         i = me_id2offset(e_menu_gfx_options, MA_OPT3_FILTERING);
1234         e_menu_gfx_options[i].data = plat_target.hwfilters;
1235         me_enable(e_menu_gfx_options, MA_OPT3_FILTERING,
1236                 plat_target.hwfilters != NULL);
1237
1238         me_enable(e_menu_gfx_options, MA_OPT2_GAMMA,
1239                 plat_target.gamma_set != NULL);
1240
1241         i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
1242         e_menu_options[i].enabled = 0;
1243         if (plat_target.cpu_clock_set != NULL) {
1244                 e_menu_options[i].name = "CPU clock";
1245                 e_menu_options[i].enabled = 1;
1246         }
1247 }