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