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