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