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