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