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