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