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