1 // (c) Copyright 2007 notaz, All rights reserved.
2 // Free for non-commercial use.
4 // For commercial use, separate licencing terms must be obtained.
6 // don't like to use loads of #ifdefs, so duplicating GP2X code
13 #include <sys/syslimits.h> // PATH_MAX
15 #include <pspdisplay.h>
17 #include <pspiofilemgr.h>
24 #include "../common/menu.h"
25 #include "../common/emu.h"
26 #include "../common/readpng.h"
27 #include "../common/lprintf.h"
28 #include "../common/input.h"
31 #include <pico/pico_int.h>
32 #include <pico/patch.h>
33 #include <zlib/zlib.h>
36 #define pspKeyUnkn "???"
37 const char * const keyNames[] = {
38 "SELECT", pspKeyUnkn, pspKeyUnkn, "START", "UP", "RIGHT", "DOWN", "LEFT",
39 "L", "R", pspKeyUnkn, pspKeyUnkn, "TRIANGLE", "CIRCLE", "X", "SQUARE",
40 "HOME", "HOLD", "WLAN_UP", "REMOTE", "VOLUP", "VOLDOWN", "SCREEN", "NOTE",
41 pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, "NUB UP", "NUB RIGHT", "NUB DOWN", "NUB LEFT" // fake
44 static unsigned short bg_buffer[480*272] __attribute__((aligned(16)));
45 #define menu_screen psp_screen
47 void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
48 static void menu_prepare_bg(int use_game_bg, int use_fg);
51 static unsigned int inp_prev = 0;
53 void menu_draw_begin(void)
55 // short *src = (short *)bg_buffer, *dst = (short *)menu_screen;
58 // for (i = 272; i >= 0; i--, dst += 512, src += 480)
59 // memcpy32((int *)dst, (int *)src, 480*2/4);
61 sceGuSync(0,0); // sync with prev
62 sceGuStart(GU_DIRECT, guCmdList);
63 sceGuCopyImage(GU_PSM_5650, 0, 0, 480, 272, 480, bg_buffer, 0, 0, 512, menu_screen);
69 void menu_draw_end(void)
75 // --------- loading ROM screen ----------
77 static int lcdr_line = 0;
79 static void load_progress_cb(int percent)
81 int ln, len = percent * 480 / 100;
84 //sceDisplayWaitVblankStart();
86 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
88 if (len > 480) len = 480;
89 for (ln = 8; ln > 0; ln--, dst += 512)
90 memset(dst, 0xff, len*2);
93 static void cdload_progress_cb(int percent)
95 int ln, len = percent * 480 / 100;
100 smalltext_out16(1, lcdr_line++ * 10, "Processing CD image / MP3s", 0xffff);
101 smalltext_out16_lim(1, lcdr_line++ * 10, romFileName, 0xffff, 80);
104 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
106 if (len > 480) len = 480;
107 for (ln = 8; ln > 0; ln--, dst += 512)
108 memset(dst, 0xff, len*2);
111 void menu_romload_prepare(const char *rom_name)
113 const char *p = rom_name + strlen(rom_name);
114 while (p > rom_name && *p != '/') p--;
116 psp_video_switch_to_single();
117 if (rom_loaded) menu_draw_begin();
118 else memset32_uncached(psp_screen, 0, 512*272*2/4);
120 smalltext_out16(1, 1, "Loading", 0xffff);
121 smalltext_out16_lim(1, 10, p, 0xffff, 80);
122 PicoCartLoadProgressCB = load_progress_cb;
123 PicoCDLoadProgressCB = cdload_progress_cb;
127 void menu_romload_end(void)
129 PicoCartLoadProgressCB = PicoCDLoadProgressCB = NULL;
130 smalltext_out16(1, ++lcdr_line*10, "Starting emulation...", 0xffff);
133 // -------------- ROM selector --------------
141 // bbbb bggg gggr rrrr
142 static unsigned short file2color(const char *fname)
144 const char *ext = fname + strlen(fname) - 3;
145 static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso", "cue" };
146 static const char *other_exts[] = { "gmv", "pat" };
149 if (ext < fname) ext = fname;
150 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
151 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xfdf7;
152 for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
153 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
157 static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
162 n--; // exclude current dir (".")
167 // menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
170 menu_darken_bg((char *)menu_screen + 512*129*2, (char *)menu_screen + 512*129*2, 512*10, 0);
173 smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
174 for (i = 0; i < n; i++) {
176 if (pos < 0) continue;
178 if (namelist[i+1]->d_type & FIO_S_IFDIR) {
179 smalltext_out16_lim(14, pos*10, "/", 0xd7ff, 1);
180 smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xd7ff, 80-3);
182 unsigned short color = file2color(namelist[i+1]->d_name);
183 smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 80-2);
186 text_out16(5, 130, ">");
190 static int scandir_cmp(const void *p1, const void *p2)
192 struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;
193 if ((*d1)->d_type & (*d2)->d_type & FIO_S_IFDIR)
194 return strcasecmp((*d1)->d_name, (*d2)->d_name);
195 if ((*d1)->d_type & FIO_S_IFDIR) return -1; // put before
196 if ((*d2)->d_type & FIO_S_IFDIR) return 1;
197 return strcasecmp((*d1)->d_name, (*d2)->d_name);
200 static char *filter_exts[] = {
201 ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
205 static int scandir_filter(const struct my_dirent *ent)
210 if (ent == NULL || ent->d_name == NULL) return 0;
211 if (strlen(ent->d_name) < 5) return 1;
213 p = ent->d_name + strlen(ent->d_name) - 4;
215 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
217 if (strcasecmp(p, filter_exts[i]) == 0) return 0;
223 static int my_scandir(const char *dir, struct my_dirent ***namelist_out,
224 int(*filter)(const struct my_dirent *),
225 int(*compar)(const void *, const void *))
227 int ret = -1, dir_uid = -1, name_alloc = 4, name_count = 0;
228 struct my_dirent **namelist = NULL, *ent;
231 namelist = malloc(sizeof(*namelist) * name_alloc);
232 if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
234 // try to read first..
235 dir_uid = sceIoDopen(dir);
238 /* it is very important to clear SceIoDirent to be passed to sceIoDread(), */
239 /* or else it may crash, probably misinterpreting something in it. */
240 memset(&sce_ent, 0, sizeof(sce_ent));
241 ret = sceIoDread(dir_uid, &sce_ent);
244 lprintf("sceIoDread(\"%s\") failed with %i\n", dir, ret);
249 lprintf("sceIoDopen(\"%s\") failed with %i\n", dir, dir_uid);
253 ent = malloc(sizeof(*ent));
254 if (ent == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
255 ent->d_type = sce_ent.d_stat.st_mode;
256 strncpy(ent->d_name, sce_ent.d_name, sizeof(ent->d_name));
257 ent->d_name[sizeof(ent->d_name)-1] = 0;
258 if (filter == NULL || filter(ent))
259 namelist[name_count++] = ent;
262 if (name_count >= name_alloc)
266 tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
267 if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
271 memset(&sce_ent, 0, sizeof(sce_ent));
272 ret = sceIoDread(dir_uid, &sce_ent);
276 if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
280 *namelist_out = namelist;
284 if (namelist != NULL)
287 free(namelist[name_count]);
291 if (dir_uid >= 0) sceIoDclose(dir_uid);
296 static SceIoStat cpstat;
298 static char *romsel_loop(char *curr_path)
300 struct my_dirent **namelist;
301 int n, iret, sel = 0;
302 unsigned long inp = 0;
303 char *ret = NULL, *fname = NULL;
305 // is this a dir or a full path?
306 memset(&cpstat, 0, sizeof(cpstat));
307 iret = sceIoGetstat(curr_path, &cpstat);
308 if (iret >= 0 && (cpstat.st_mode & FIO_S_IFDIR)); // dir
309 else if (iret >= 0 && (cpstat.st_mode & FIO_S_IFREG)) { // file
311 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
316 else strcpy(curr_path, "ms0:/");
318 else strcpy(curr_path, "ms0:/"); // something else
320 n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
323 n = my_scandir("ms0:/", &namelist, scandir_filter, scandir_cmp);
326 lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
334 for (i = 1; i < n; i++) {
335 if (strcmp(namelist[i]->d_name, fname) == 0) {
344 draw_dirlist(curr_path, namelist, n, sel);
345 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_X|PBTN_CIRCLE, 0);
346 if(inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
347 if(inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
348 if(inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
349 if(inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
350 if(inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
351 if(inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
352 if(inp & PBTN_CIRCLE) // enter dir/select
354 if (namelist[sel+1]->d_type & FIO_S_IFDIR)
356 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
357 char *p, *newdir = malloc(newlen);
358 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
359 char *start = curr_path;
360 p = start + strlen(start) - 1;
361 while (*p == '/' && p > start) p--;
362 while (*p != '/' && *p != ':' && p > start) p--;
363 if (p <= start || *p == ':') strcpy(newdir, "ms0:/");
364 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
366 strcpy(newdir, curr_path);
367 p = newdir + strlen(newdir) - 1;
368 while (*p == '/' && p >= newdir) *p-- = 0;
370 strcat(newdir, namelist[sel+1]->d_name);
372 ret = romsel_loop(newdir);
376 else if (namelist[sel+1]->d_type & FIO_S_IFREG)
378 strcpy(romFileName, curr_path);
379 strcat(romFileName, "/");
380 strcat(romFileName, namelist[sel+1]->d_name);
385 if(inp & PBTN_X) break; // cancel
389 while(n--) free(namelist[n]);
396 // ------------ patch/gg menu ------------
398 static void draw_patchlist(int sel)
400 int start, i, pos, active;
406 for (i = 0; i < PicoPatchCount; i++) {
408 if (pos < 0) continue;
410 active = PicoPatches[i].active;
411 smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
412 smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
415 if (pos < 27) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
417 text_out16(5, 130, ">");
422 static void patches_menu_loop(void)
425 unsigned long inp = 0;
429 draw_patchlist(menu_sel);
430 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_X|PBTN_CIRCLE, 0);
431 if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
432 if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
433 if(inp &(PBTN_LEFT|PBTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
434 if(inp &(PBTN_RIGHT|PBTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
435 if(inp & PBTN_CIRCLE) { // action
436 if (menu_sel < PicoPatchCount)
437 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
440 if(inp & PBTN_X) return;
445 // ------------ savestate loader ------------
447 static int state_slot_flags = 0;
449 static void state_check_slots(void)
453 state_slot_flags = 0;
455 for (slot = 0; slot < 10; slot++)
457 if (emu_checkSaveFile(slot))
459 state_slot_flags |= 1 << slot;
464 static void *get_oldstate_for_preview(void)
466 unsigned char *ptr = malloc(sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram) + sizeof(Pico.video));
467 if (ptr == NULL) return NULL;
469 memcpy(ptr, Pico.vram, sizeof(Pico.vram));
470 memcpy(ptr + sizeof(Pico.vram), Pico.cram, sizeof(Pico.cram));
471 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram), Pico.vsram, sizeof(Pico.vsram));
472 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), &Pico.video, sizeof(Pico.video));
476 static void restore_oldstate(void *ptrx)
478 unsigned char *ptr = ptrx;
479 memcpy(Pico.vram, ptr, sizeof(Pico.vram));
480 memcpy(Pico.cram, ptr + sizeof(Pico.vram), sizeof(Pico.cram));
481 memcpy(Pico.vsram, ptr + sizeof(Pico.vram) + sizeof(Pico.cram), sizeof(Pico.vsram));
482 memcpy(&Pico.video,ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), sizeof(Pico.video));
486 static void draw_savestate_bg(int slot)
488 void *file, *oldstate;
491 fname = emu_GetSaveFName(1, 0, slot);
494 oldstate = get_oldstate_for_preview();
495 if (oldstate == NULL) return;
497 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
498 file = gzopen(fname, "rb");
499 emu_setSaveStateCbs(1);
501 file = fopen(fname, "rb");
502 emu_setSaveStateCbs(0);
506 if (PicoAHW & PAHW_MCD) {
507 PicoCdLoadStateGfx(file);
509 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
510 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
511 areaSeek(file, 0x2000, SEEK_CUR);
512 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
513 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
514 areaSeek(file, 0x221a0, SEEK_SET);
515 areaRead(&Pico.video, 1, sizeof(Pico.video), file);
521 menu_prepare_bg(1, 0);
523 restore_oldstate(oldstate);
526 static void draw_savestate_menu(int menu_sel, int is_loading)
528 int tl_x = 80+25, tl_y = 16+60, y, i;
530 if (state_slot_flags & (1 << menu_sel))
531 draw_savestate_bg(menu_sel);
534 text_out16(tl_x, 16+30, is_loading ? "Load state" : "Save state");
536 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
538 /* draw all 10 slots */
540 for (i = 0; i < 10; i++, y+=10)
542 text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
544 text_out16(tl_x, y, "back");
549 static int savestate_menu_loop(int is_loading)
551 static int menu_sel = 10;
552 int menu_sel_max = 10;
553 unsigned long inp = 0;
559 draw_savestate_menu(menu_sel, is_loading);
560 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_X|PBTN_CIRCLE, 0);
563 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
564 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
566 if(inp & PBTN_DOWN) {
568 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
569 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
571 if(inp & PBTN_CIRCLE) { // save/load
573 state_slot = menu_sel;
574 PicoStateProgressCB = emu_msg_cb; /* also suitable for menu */
575 if (emu_SaveLoadGame(is_loading, 0)) {
576 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
582 if(inp & PBTN_X) return 1;
586 // -------------- key config --------------
588 static char *action_binds(int player_idx, int action_mask)
590 static char strkeys[32*5];
594 for (i = 0; i < 32; i++) // i is key index
596 if (currentConfig.KeyBinds[i] & action_mask)
598 if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
600 strcat(strkeys, i >= 28 ? ", " : " + "); // nub "buttons" don't create combos
601 strcat(strkeys, keyNames[i]);
604 else strcpy(strkeys, keyNames[i]);
611 static void unbind_action(int action)
615 for (i = 0; i < 32; i++)
616 currentConfig.KeyBinds[i] &= ~action;
619 static int count_bound_keys(int action, int pl_idx)
623 for (i = 0; i < 32; i++)
625 if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
626 if (currentConfig.KeyBinds[i] & action) keys++;
632 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx, int sel)
634 int x, y, tl_y = 16+20, i;
637 if (player_idx >= 0) {
638 text_out16(80+80, 16, "Player %i controls", player_idx + 1);
641 text_out16(80+80, 16, "Emulator controls");
645 menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
648 for (i = 0; i < opt_cnt; i++, y+=10)
649 text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
651 text_out16(x, y, "Done");
654 text_out16(80+30, 220, "Press a button to bind/unbind");
655 text_out16(80+30, 230, "Use SELECT to clear");
656 text_out16(80+30, 240, "To bind UP/DOWN, hold SELECT");
657 text_out16(80+30, 250, "Select \"Done\" to exit");
659 text_out16(80+30, 230, "Use Options -> Save cfg");
660 text_out16(80+30, 240, "to save controls");
661 text_out16(80+30, 250, "Press X or O to exit");
666 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
668 int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
669 unsigned long inp = 0;
673 draw_key_config(opts, opt_cnt, player_idx, sel);
674 inp = in_menu_wait(CONFIGURABLE_KEYS|PBTN_SELECT, 1);
675 if (!(inp & PBTN_SELECT)) {
677 if(inp & PBTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
678 if(inp & PBTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
680 if (sel >= opt_cnt) {
681 if (inp & (PBTN_X|PBTN_CIRCLE)) break;
684 // if we are here, we want to bind/unbind something
685 if ((inp & PBTN_SELECT) && !prev_select)
686 unbind_action(opts[sel].mask);
687 prev_select = inp & PBTN_SELECT;
688 inp &= CONFIGURABLE_KEYS;
690 for (i = 0; i < 32; i++)
691 if (inp & (1 << i)) {
692 if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
693 currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
694 else currentConfig.KeyBinds[i] ^= opts[sel].mask;
695 if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
696 currentConfig.KeyBinds[i] &= ~(3 << 16);
697 currentConfig.KeyBinds[i] |= player_idx << 16;
703 menu_entry ctrlopt_entries[] =
705 { "Player 1", MB_NONE, MA_CTRL_PLAYER1, NULL, 0, 0, 0, 1, 0 },
706 { "Player 2", MB_NONE, MA_CTRL_PLAYER2, NULL, 0, 0, 0, 1, 0 },
707 { "Emulator controls", MB_NONE, MA_CTRL_EMU, NULL, 0, 0, 0, 1, 0 },
708 { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &PicoOpt, 0x020, 0, 0, 1, 1 },
709 { "Turbo rate", MB_RANGE, MA_CTRL_TURBO_RATE, ¤tConfig.turbo_rate, 0, 1, 30, 1, 1 },
710 { "Done", MB_NONE, MA_CTRL_DONE, NULL, 0, 0, 0, 1, 0 },
713 #define CTRLOPT_ENTRY_COUNT (sizeof(ctrlopt_entries) / sizeof(ctrlopt_entries[0]))
714 const int ctrlopt_entry_count = CTRLOPT_ENTRY_COUNT;
716 static void draw_kc_sel(int menu_sel)
718 int tl_x = 80+25+40, tl_y = 16+60, y;
722 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
724 me_draw(ctrlopt_entries, ctrlopt_entry_count, tl_x, tl_y, NULL, NULL);
730 // player2_flag, ?, ?, ?, ?, ?, ?, menu
731 // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
732 // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
733 me_bind_action emuctrl_actions[] =
735 { "Load State ", 1<<28 },
736 { "Save State ", 1<<27 },
737 { "Prev Save Slot ", 1<<25 },
738 { "Next Save Slot ", 1<<24 },
739 { "Switch Renderer ", 1<<26 },
740 { "Fast forward ", 1<<22 },
741 { "Pico Next page ", 1<<21 },
742 { "Pico Prev page ", 1<<20 },
743 { "Pico Switch input", 1<<19 },
747 static void kc_sel_loop(void)
749 int menu_sel = 5, menu_sel_max = 5;
750 unsigned long inp = 0;
755 draw_kc_sel(menu_sel);
756 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_X|PBTN_CIRCLE, 0);
757 selected_id = me_index2id(ctrlopt_entries, CTRLOPT_ENTRY_COUNT, menu_sel);
758 if (inp & (PBTN_LEFT|PBTN_RIGHT)) // multi choise
759 me_process(ctrlopt_entries, CTRLOPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0);
760 if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
761 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
762 if (inp & PBTN_CIRCLE) {
763 int is_6button = PicoOpt & POPT_6BTN_PAD;
764 switch (selected_id) {
765 case MA_CTRL_PLAYER1: key_config_loop(me_ctrl_actions, is_6button ? 15 : 11, 0); return;
766 case MA_CTRL_PLAYER2: key_config_loop(me_ctrl_actions, is_6button ? 15 : 11, 1); return;
767 case MA_CTRL_EMU: key_config_loop(emuctrl_actions,
768 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]) - 1, -1); return;
769 case MA_CTRL_DONE: if (!rom_loaded) emu_WriteConfig(0); return;
773 if (inp & PBTN_X) return;
778 // --------- sega/mega cd options ----------
780 menu_entry cdopt_entries[] =
782 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1, 0 },
783 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1, 0 },
784 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1, 0 },
785 { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, ¤tConfig.EmuOpt, 0x0400, 0, 0, 1, 1 },
786 { "CDDA audio", MB_ONOFF, MA_CDOPT_CDDA, &PicoOpt, 0x0800, 0, 0, 1, 1 },
787 { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &PicoOpt, 0x0400, 0, 0, 1, 1 },
788 { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1, 1 },
789 { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, &PicoOpt, 0x8000, 0, 0, 1, 1 },
790 { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&PicoOpt, 0x1000, 0, 0, 1, 1 },
791 { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &PicoOpt, 0x2000, 0, 0, 1, 1 },
792 { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1, 0 },
795 #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
796 const int cdopt_entry_count = CDOPT_ENTRY_COUNT;
801 char us[32], eu[32], jp[32];
804 static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
806 struct bios_names_t *bios_names = param;
811 case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
812 case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
813 case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
814 case MA_CDOPT_READAHEAD:
815 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
816 else strcpy(ra_buff, " OFF");
817 text_out16(x, y, "ReadAhead buffer %s", ra_buff);
823 static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
825 int tl_x = 80+25, tl_y = 16+60;
829 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
830 else strcpy(ra_buff, " OFF");
834 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
836 me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
838 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
839 if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
840 (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
841 (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
842 text_out16(tl_x, 250, "Press start to test selected BIOS");
847 static void cd_menu_loop_options(void)
849 static int menu_sel = 0;
850 int menu_sel_max = 10;
851 unsigned long inp = 0;
852 struct bios_names_t bios_names;
856 if (emu_findBios(4, &bios)) { // US
857 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
859 strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
860 } else strcpy(bios_names.us, "NOT FOUND");
862 if (emu_findBios(8, &bios)) { // EU
863 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
865 strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
866 } else strcpy(bios_names.eu, "NOT FOUND");
868 if (emu_findBios(1, &bios)) { // JP
869 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
871 strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
872 } else strcpy(bios_names.jp, "NOT FOUND");
878 draw_cd_menu_options(menu_sel, &bios_names);
879 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_X|PBTN_CIRCLE|PBTN_START, 0);
880 if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
881 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
882 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
883 if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
884 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0) &&
885 selected_id == MA_CDOPT_READAHEAD) {
886 if (inp & PBTN_LEFT) {
888 if (PicoCDBuffers < 2) PicoCDBuffers = 0;
890 if (PicoCDBuffers < 2) PicoCDBuffers = 2;
891 else PicoCDBuffers <<= 1;
892 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
896 if (inp & PBTN_CIRCLE) // toggleable options
897 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
898 selected_id == MA_CDOPT_DONE) {
901 if (inp & PBTN_START) {
902 switch (selected_id) { // BIOS testers
903 case MA_CDOPT_TESTBIOS_USA:
904 if (emu_findBios(4, &bios)) { // test US
905 strcpy(romFileName, bios);
906 engineState = PGS_ReloadRom;
910 case MA_CDOPT_TESTBIOS_EUR:
911 if (emu_findBios(8, &bios)) { // test EU
912 strcpy(romFileName, bios);
913 engineState = PGS_ReloadRom;
917 case MA_CDOPT_TESTBIOS_JAP:
918 if (emu_findBios(1, &bios)) { // test JP
919 strcpy(romFileName, bios);
920 engineState = PGS_ReloadRom;
928 if (inp & PBTN_X) return;
932 // --------- display options ----------
934 menu_entry opt3_entries[] =
936 { NULL, MB_NONE, MA_OPT3_SCALE, NULL, 0, 0, 0, 1, 1 },
937 { NULL, MB_NONE, MA_OPT3_HSCALE32, NULL, 0, 0, 0, 1, 1 },
938 { NULL, MB_NONE, MA_OPT3_HSCALE40, NULL, 0, 0, 0, 1, 1 },
939 { NULL, MB_ONOFF, MA_OPT3_FILTERING, ¤tConfig.scaling, 1, 0, 0, 1, 1 },
940 { NULL, MB_RANGE, MA_OPT3_GAMMAA, ¤tConfig.gamma, 0, -4, 16, 1, 1 },
941 { NULL, MB_RANGE, MA_OPT3_BLACKLVL, ¤tConfig.gamma2, 0, 0, 2, 1, 1 },
942 { NULL, MB_NONE, MA_OPT3_VSYNC, NULL, 0, 0, 0, 1, 1 },
943 { "Set to unscaled centered", MB_NONE, MA_OPT3_PRES_NOSCALE, NULL, 0, 0, 0, 1, 0 },
944 { "Set to 4:3 scaled", MB_NONE, MA_OPT3_PRES_SCALE43, NULL, 0, 0, 0, 1, 0 },
945 { "Set to fullscreen", MB_NONE, MA_OPT3_PRES_FULLSCR, NULL, 0, 0, 0, 1, 0 },
946 { "done", MB_NONE, MA_OPT3_DONE, NULL, 0, 0, 0, 1, 0 },
949 #define OPT3_ENTRY_COUNT (sizeof(opt3_entries) / sizeof(opt3_entries[0]))
950 const int opt3_entry_count = OPT3_ENTRY_COUNT;
953 static void menu_opt3_cust_draw(const menu_entry *entry, int x, int y, void *param)
958 text_out16(x, y, "Scale factor: %.2f", currentConfig.scale);
960 case MA_OPT3_HSCALE32:
961 text_out16(x, y, "Hor. scale (for low res. games): %.2f", currentConfig.hscale32);
963 case MA_OPT3_HSCALE40:
964 text_out16(x, y, "Hor. scale (for hi res. games): %.2f", currentConfig.hscale40);
966 case MA_OPT3_FILTERING:
967 text_out16(x, y, "Bilinear filtering %s", currentConfig.scaling?"ON":"OFF");
970 text_out16(x, y, "Gamma adjustment %2i", currentConfig.gamma);
972 case MA_OPT3_BLACKLVL:
973 text_out16(x, y, "Black level %2i", currentConfig.gamma2);
975 case MA_OPT3_VSYNC: {
976 char *val = " never";
977 if (currentConfig.EmuOpt & 0x2000)
978 val = (currentConfig.EmuOpt & 0x10000) ? "sometimes" : " always";
979 text_out16(x, y, "Wait for vsync (slow) %s", val);
986 static void menu_opt3_preview(int is_32col)
988 void *oldstate = NULL;
990 if (!rom_loaded || ((Pico.video.reg[12]&1)^1) != is_32col)
992 extern char bgdatac32_start[], bgdatac40_start[];
993 extern int bgdatac32_size, bgdatac40_size;
994 void *bgdata = is_32col ? bgdatac32_start : bgdatac40_start;
995 unsigned long insize = is_32col ? bgdatac32_size : bgdatac40_size, outsize = 65856;
997 ret = uncompress((Bytef *)bg_buffer, &outsize, bgdata, insize);
1000 if (rom_loaded) oldstate = get_oldstate_for_preview();
1001 memcpy(Pico.vram, bg_buffer, sizeof(Pico.vram));
1002 memcpy(Pico.cram, (char *)bg_buffer + 0x10000, 0x40*2);
1003 memcpy(Pico.vsram, (char *)bg_buffer + 0x10080, 0x40*2);
1004 memcpy(&Pico.video,(char *)bg_buffer + 0x10100, 0x40);
1007 lprintf("uncompress returned %i\n", ret);
1010 memset32_uncached(psp_screen, 0, 512*272*2/4);
1012 menu_prepare_bg(1, 0);
1014 if (oldstate) restore_oldstate(oldstate);
1017 static void draw_dispmenu_options(int menu_sel)
1019 int tl_x = 80, tl_y = 16+50;
1023 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 316);
1025 me_draw(opt3_entries, OPT3_ENTRY_COUNT, tl_x, tl_y, menu_opt3_cust_draw, NULL);
1030 static void dispmenu_loop_options(void)
1032 static int menu_sel = 0;
1033 int menu_sel_max, is_32col = (Pico.video.reg[12]&1)^1;
1034 unsigned long inp = 0;
1035 menu_id selected_id;
1037 menu_sel_max = me_count_enabled(opt3_entries, OPT3_ENTRY_COUNT) - 1;
1041 draw_dispmenu_options(menu_sel);
1042 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_X|PBTN_CIRCLE, 0);
1043 if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1044 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1045 selected_id = me_index2id(opt3_entries, OPT3_ENTRY_COUNT, menu_sel);
1046 if (selected_id == MA_OPT3_HSCALE40 && is_32col) { is_32col = 0; menu_opt3_preview(is_32col); }
1047 if (selected_id == MA_OPT3_HSCALE32 && !is_32col) { is_32col = 1; menu_opt3_preview(is_32col); }
1049 if (inp & (PBTN_LEFT|PBTN_RIGHT)) // multi choise
1051 float *setting = NULL;
1053 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0);
1054 switch (selected_id) {
1055 case MA_OPT3_SCALE: setting = ¤tConfig.scale; break;
1056 case MA_OPT3_HSCALE40: setting = ¤tConfig.hscale40; is_32col = 0; break;
1057 case MA_OPT3_HSCALE32: setting = ¤tConfig.hscale32; is_32col = 1; break;
1058 case MA_OPT3_FILTERING:
1059 case MA_OPT3_GAMMAA:
1060 case MA_OPT3_BLACKLVL: menu_opt3_preview(is_32col); break;
1062 tmp = ((currentConfig.EmuOpt>>13)&1) | ((currentConfig.EmuOpt>>15)&2);
1063 tmp = (inp & PBTN_LEFT) ? (tmp>>1) : ((tmp<<1)|1);
1064 if (tmp > 3) tmp = 3;
1065 currentConfig.EmuOpt &= ~0x12000;
1066 currentConfig.EmuOpt |= ((tmp&2)<<15) | ((tmp&1)<<13);
1070 if (setting != NULL) {
1071 while ((inp = psp_pad_read(0)) & (PBTN_LEFT|PBTN_RIGHT)) {
1072 *setting += (inp & PBTN_LEFT) ? -0.01 : 0.01;
1073 if (*setting <= 0) *setting = 0.01;
1074 menu_opt3_preview(is_32col);
1075 draw_dispmenu_options(menu_sel); // will wait vsync
1079 if (inp & PBTN_CIRCLE) { // toggleable options
1080 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, 1);
1081 switch (selected_id) {
1084 case MA_OPT3_PRES_NOSCALE:
1085 currentConfig.scale = currentConfig.hscale40 = currentConfig.hscale32 = 1.0;
1086 menu_opt3_preview(is_32col);
1088 case MA_OPT3_PRES_SCALE43:
1089 currentConfig.scale = 1.20;
1090 currentConfig.hscale40 = 1.00;
1091 currentConfig.hscale32 = 1.25;
1092 menu_opt3_preview(is_32col);
1094 case MA_OPT3_PRES_FULLSCR:
1095 currentConfig.scale = 1.20;
1096 currentConfig.hscale40 = 1.25;
1097 currentConfig.hscale32 = 1.56;
1098 menu_opt3_preview(is_32col);
1100 case MA_OPT3_FILTERING:
1101 menu_opt3_preview(is_32col);
1106 if (inp & PBTN_X) return;
1111 // --------- advanced options ----------
1113 menu_entry opt2_entries[] =
1115 { "Disable sprite limit", MB_ONOFF, MA_OPT2_NO_SPRITE_LIM, &PicoOpt, 0x40000, 0, 0, 1, 1 },
1116 { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &PicoOpt, 0x00004, 0, 0, 1, 1 },
1117 { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &PicoOpt, 0x00001, 0, 0, 1, 1 },
1118 { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496, &PicoOpt, 0x00002, 0, 0, 1, 1 },
1119 { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, ¤tConfig.EmuOpt, 0x00008, 0, 0, 1, 1 },
1120 { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, ¤tConfig.EmuOpt, 0x00020, 0, 0, 1, 1 },
1121 { "Status line in main menu", MB_ONOFF, MA_OPT2_STATUS_LINE, ¤tConfig.EmuOpt, 0x20000, 0, 0, 1, 1 },
1122 { "Disable idle loop patching",MB_ONOFF, MA_OPT2_NO_IDLE_LOOPS, &PicoOpt, 0x80000, 0, 0, 1, 1 },
1123 { "Disable frame limiter", MB_ONOFF, MA_OPT2_NO_FRAME_LIMIT, ¤tConfig.EmuOpt, 0x40000, 0, 0, 1, 1 },
1124 { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1, 0 },
1127 #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
1128 const int opt2_entry_count = OPT2_ENTRY_COUNT;
1131 static void draw_amenu_options(int menu_sel)
1133 int tl_x = 80+25, tl_y = 16+50;
1137 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
1139 me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1144 static void amenu_loop_options(void)
1146 static int menu_sel = 0;
1148 unsigned long inp = 0;
1149 menu_id selected_id;
1151 menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
1155 draw_amenu_options(menu_sel);
1156 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_X|PBTN_CIRCLE, 0);
1157 if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1158 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1159 selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
1160 if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
1161 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0) &&
1162 selected_id == MA_OPT2_GAMMA) {
1166 if (inp & PBTN_CIRCLE) { // toggleable options
1167 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
1168 selected_id == MA_OPT2_DONE) {
1172 if (inp & PBTN_X) return;
1176 // -------------- options --------------
1179 menu_entry opt_entries[] =
1181 { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1, 1 },
1182 { "Accurate sprites", MB_ONOFF, MA_OPT_ACC_SPRITES, &PicoOpt, 0x080, 0, 0, 0, 1 },
1183 { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, ¤tConfig.EmuOpt, 0x0002, 0, 0, 1, 1 },
1184 { NULL, MB_RANGE, MA_OPT_FRAMESKIP, ¤tConfig.Frameskip, 0, -1, 16, 1, 1 },
1185 { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, ¤tConfig.EmuOpt, 0x0004, 0, 0, 1, 1 },
1186 { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1, 1 },
1187 { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1, 1 },
1188 { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, ¤tConfig.EmuOpt, 0x0001, 0, 0, 1, 1 },
1189 { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1, 1 },
1190 { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1, 1 },
1191 { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1, 1 },
1192 { "[Display options]", MB_NONE, MA_OPT_DISP_OPTS, NULL, 0, 0, 0, 1, 0 },
1193 { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1, 0 },
1194 { "[Advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1, 0 },
1195 { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1, 0 },
1196 { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1, 0 },
1197 { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1, 0 },
1200 #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
1201 const int opt_entry_count = OPT_ENTRY_COUNT;
1204 static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
1206 char *str, str24[24];
1210 case MA_OPT_RENDERER:
1213 else if (currentConfig.EmuOpt & 0x80)
1216 str = " 8bit accurate"; // n/a
1217 text_out16(x, y, "Renderer: %s", str);
1219 case MA_OPT_FRAMESKIP:
1220 if (currentConfig.Frameskip < 0)
1221 strcpy(str24, "Auto");
1222 else sprintf(str24, "%i", currentConfig.Frameskip);
1223 text_out16(x, y, "Frameskip %s", str24);
1225 case MA_OPT_SOUND_QUALITY:
1226 str = (PicoOpt&0x08)?"stereo":"mono";
1227 text_out16(x, y, "Sound Quality: %5iHz %s", PsndRate, str);
1230 text_out16(x, y, "Region: %s", me_region_name(PicoRegionOverride, PicoAutoRgnOrder));
1232 case MA_OPT_CONFIRM_STATES:
1233 switch ((currentConfig.EmuOpt >> 9) & 5) {
1234 default: str = "OFF"; break;
1235 case 1: str = "writes"; break;
1236 case 4: str = "loads"; break;
1237 case 5: str = "both"; break;
1239 text_out16(x, y, "Confirm savestate %s", str);
1241 case MA_OPT_CPU_CLOCKS:
1242 text_out16(x, y, "CPU/bus clock %3i/%3iMHz", currentConfig.CPUclock, currentConfig.CPUclock/2);
1244 case MA_OPT_SAVECFG:
1246 if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
1247 text_out16(x, y, "Save cfg as default%s", str24);
1249 case MA_OPT_LOADCFG:
1250 text_out16(x, y, "Load cfg from profile %i", config_slot);
1253 lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
1259 static void draw_menu_options(int menu_sel)
1261 int tl_x = 80+25, tl_y = 16+24;
1265 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
1267 me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
1272 static int sndrate_prevnext(int rate, int dir)
1274 int i, rates[] = { 11025, 22050, 44100 };
1276 for (i = 0; i < 5; i++)
1277 if (rates[i] == rate) break;
1280 if (i > 2) return dir ? 44100 : 22050;
1281 if (i < 0) return dir ? 22050 : 11025;
1285 static void region_prevnext(int right)
1287 // jp_ntsc=1, jp_pal=2, usa=4, eu=8
1288 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
1291 if (!PicoRegionOverride) {
1292 for (i = 0; i < 6; i++)
1293 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1294 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
1295 else PicoRegionOverride=1;
1297 else PicoRegionOverride<<=1;
1298 if (PicoRegionOverride > 8) PicoRegionOverride = 8;
1300 if (!PicoRegionOverride) {
1301 for (i = 0; i < 6; i++)
1302 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1303 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
1305 else PicoRegionOverride>>=1;
1309 static void menu_options_save(void)
1311 if (PicoRegionOverride) {
1312 // force setting possibly changed..
1313 Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;
1315 if (!(PicoOpt & POPT_6BTN_PAD)) {
1316 // unbind XYZ MODE, just in case
1317 unbind_action(0xf00);
1321 static int menu_loop_options(void)
1323 static int menu_sel = 0;
1324 int menu_sel_max, ret;
1325 unsigned long inp = 0;
1326 menu_id selected_id;
1328 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_loaded);
1329 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1330 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1331 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1335 draw_menu_options(menu_sel);
1336 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_X|PBTN_CIRCLE, 0);
1337 if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1338 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1339 selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
1340 if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
1341 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0)) {
1342 switch (selected_id) {
1343 case MA_OPT_RENDERER:
1344 if ((PicoOpt & 0x10) || !(currentConfig.EmuOpt & 0x80)) {
1346 currentConfig.EmuOpt |= 0x80;
1349 currentConfig.EmuOpt &= ~0x80;
1352 case MA_OPT_SOUND_QUALITY:
1353 PsndRate = sndrate_prevnext(PsndRate, inp & PBTN_RIGHT);
1356 region_prevnext(inp & PBTN_RIGHT);
1358 case MA_OPT_CONFIRM_STATES: {
1359 int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
1360 n += (inp & PBTN_LEFT) ? -1 : 1;
1361 if (n < 0) n = 0; else if (n > 3) n = 3;
1362 n |= n << 1; n &= ~2;
1363 currentConfig.EmuOpt &= ~0xa00;
1364 currentConfig.EmuOpt |= n << 9;
1367 case MA_OPT_SAVE_SLOT:
1368 if (inp & PBTN_RIGHT) {
1369 state_slot++; if (state_slot > 9) state_slot = 0;
1370 } else {state_slot--; if (state_slot < 0) state_slot = 9;
1373 case MA_OPT_CPU_CLOCKS:
1374 while ((inp = psp_pad_read(0)) & (PBTN_LEFT|PBTN_RIGHT)) {
1375 currentConfig.CPUclock += (inp & PBTN_LEFT) ? -1 : 1;
1376 if (currentConfig.CPUclock < 19) currentConfig.CPUclock = 19;
1377 if (currentConfig.CPUclock > 333) currentConfig.CPUclock = 333;
1378 draw_menu_options(menu_sel); // will wait vsync
1381 case MA_OPT_SAVECFG:
1382 case MA_OPT_SAVECFG_GAME:
1383 case MA_OPT_LOADCFG:
1384 config_slot += (inp&PBTN_RIGHT) ? 1 : -1;
1385 if (config_slot > 9) config_slot = 0;
1386 if (config_slot < 0) config_slot = 9;
1387 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1388 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1389 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1392 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1397 if (inp & PBTN_CIRCLE) {
1398 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
1400 switch (selected_id)
1402 case MA_OPT_DISP_OPTS:
1403 dispmenu_loop_options();
1405 case MA_OPT_SCD_OPTS:
1406 cd_menu_loop_options();
1407 if (engineState == PGS_ReloadRom)
1408 return 0; // test BIOS
1410 case MA_OPT_ADV_OPTS:
1411 amenu_loop_options();
1413 case MA_OPT_SAVECFG: // done (update and write)
1414 menu_options_save();
1415 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
1416 else strcpy(menuErrorMsg, "failed to write config");
1418 case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
1419 menu_options_save();
1420 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
1421 else strcpy(menuErrorMsg, "failed to write config");
1423 case MA_OPT_LOADCFG:
1424 ret = emu_ReadConfig(1, 1);
1425 if (!ret) ret = emu_ReadConfig(0, 1);
1426 if (ret) strcpy(menuErrorMsg, "config loaded");
1427 else strcpy(menuErrorMsg, "failed to load config");
1430 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1436 menu_options_save();
1437 return 0; // done (update, no write)
1442 // -------------- credits --------------
1444 static void draw_menu_credits(void)
1446 int tl_x = 80+15, tl_y = 16+64, y;
1449 text_out16(tl_x, 16+20, "PicoDrive v" VERSION " (c) notaz, 2006-2008");
1452 text_out16(tl_x, y, "Credits:");
1453 text_out16(tl_x, (y+=10), "fDave: base code of PicoDrive");
1454 text_out16(tl_x, (y+=10), "Chui: Fame/C");
1455 text_out16(tl_x, (y+=10), "NJ: CZ80");
1456 text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
1457 text_out16(tl_x, (y+=10), "ps2dev.org people: PSP SDK/code");
1458 text_out16(tl_x, (y+=10), "ketchupgun: skin design");
1460 text_out16(tl_x, (y+=20), "special thanks (for docs, ideas):");
1461 text_out16(tl_x, (y+=10), " Charles MacDonald, Haze,");
1462 text_out16(tl_x, (y+=10), " Stephane Dallongeville,");
1463 text_out16(tl_x, (y+=10), " Lordus, Exophase, Rokas,");
1464 text_out16(tl_x, (y+=10), " Nemesis, Tasco Deluxe");
1470 // -------------- root menu --------------
1472 menu_entry main_entries[] =
1474 { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
1475 { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
1476 { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
1477 { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
1478 { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
1479 { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
1480 { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
1481 { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
1482 { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
1483 { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
1486 #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
1488 static void draw_menu_root(int menu_sel)
1490 const int tl_x = 86+70, tl_y = 16+70;
1495 if ((currentConfig.EmuOpt&0x20000) && (stat = psp_get_status_line()))
1496 text_out16(287, 12, "%s", stat);
1498 text_out16(tl_x, 48, "PicoDrive v" VERSION);
1500 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
1502 me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1505 if (menuErrorMsg[0])
1506 text_out16(10, 252, menuErrorMsg);
1511 static void menu_loop_root(void)
1513 static int menu_sel = 0;
1514 int ret, menu_sel_max;
1515 unsigned long inp = 0;
1517 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_loaded);
1518 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_loaded);
1519 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_loaded);
1520 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_loaded);
1521 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
1523 menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
1524 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1527 if (mp3_last_error != 0) {
1528 if (mp3_last_error == -1)
1529 sprintf(menuErrorMsg, "Unsupported mp3 format, use 44kHz stereo");
1530 else sprintf(menuErrorMsg, "mp3 init failed, code %08x", mp3_last_error);
1534 /* make sure action buttons are not pressed on entering menu */
1535 draw_menu_root(menu_sel);
1537 while (psp_pad_read(1) & (PBTN_X|PBTN_CIRCLE|PBTN_SELECT)) psp_msleep(50);
1541 draw_menu_root(menu_sel);
1542 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_X|PBTN_CIRCLE|PBTN_SELECT|PBTN_L|PBTN_R, 0);
1543 if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1544 if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1545 if((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R)) debug_menu_loop();
1546 if( inp & (PBTN_SELECT|PBTN_X)) {
1548 while (psp_pad_read(1) & (PBTN_SELECT|PBTN_X)) psp_msleep(50); // wait until released
1549 engineState = PGS_Running;
1553 if(inp & PBTN_CIRCLE) {
1554 menuErrorMsg[0] = 0; // clear error msg
1555 switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
1557 case MA_MAIN_RESUME_GAME:
1559 while (psp_pad_read(1) & PBTN_CIRCLE) psp_msleep(50);
1560 engineState = PGS_Running;
1564 case MA_MAIN_SAVE_STATE:
1566 if(savestate_menu_loop(0))
1568 engineState = PGS_Running;
1572 case MA_MAIN_LOAD_STATE:
1574 if(savestate_menu_loop(1))
1576 while (psp_pad_read(1) & PBTN_CIRCLE) psp_msleep(50);
1577 engineState = PGS_Running;
1581 case MA_MAIN_RESET_GAME:
1584 while (psp_pad_read(1) & PBTN_CIRCLE) psp_msleep(50);
1585 engineState = PGS_Running;
1589 case MA_MAIN_LOAD_ROM:
1591 char curr_path[PATH_MAX], *selfname;
1593 if ( (tstf = fopen(loadedRomFName, "rb")) )
1596 strcpy(curr_path, loadedRomFName);
1599 getcwd(curr_path, PATH_MAX);
1600 selfname = romsel_loop(curr_path);
1602 lprintf("selected file: %s\n", selfname);
1603 engineState = PGS_ReloadRom;
1608 case MA_MAIN_OPTIONS:
1609 ret = menu_loop_options();
1610 if (ret == 1) continue; // status update
1611 if (engineState == PGS_ReloadRom)
1612 return; // BIOS test
1614 case MA_MAIN_CONTROLS:
1617 case MA_MAIN_CREDITS:
1618 draw_menu_credits();
1621 while (!(inp & (PBTN_X|PBTN_CIRCLE)))
1622 inp = in_menu_wait(PBTN_X|PBTN_CIRCLE, 0);
1625 engineState = PGS_Quit;
1627 case MA_MAIN_PATCHES:
1628 if (rom_loaded && PicoPatches) {
1629 patches_menu_loop();
1631 strcpy(menuErrorMsg, "Patches applied");
1636 lprintf("%s: something unknown selected\n", __FUNCTION__);
1643 void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
1645 unsigned int *dest = dst;
1646 const unsigned int *srce = src;
1652 unsigned int p = *srce++;
1653 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
1660 unsigned int p = *srce++;
1661 *dest++ = (p&0xf79ef79e)>>1;
1666 static void menu_prepare_bg(int use_game_bg, int use_fg)
1670 // darken the active framebuffer
1671 unsigned short *dst = bg_buffer;
1672 unsigned short *src = use_fg ? psp_video_get_active_fb() : psp_screen;
1674 for (i = 272; i > 0; i--, dst += 480, src += 512)
1675 menu_darken_bg(dst, src, 480, 1);
1676 //memset32_uncached((int *)(bg_buffer + 480*264), 0, 480*8*2/4);
1680 // should really only happen once, on startup..
1681 memset32_uncached((int *)(void *)bg_buffer, 0, sizeof(bg_buffer)/4);
1682 readpng(bg_buffer, "skin/background.png", READPNG_BG);
1684 sceKernelDcacheWritebackAll();
1687 static void menu_gfx_prepare(void)
1689 menu_prepare_bg(rom_loaded, 1);
1696 void menu_loop(void)
1702 menuErrorMsg[0] = 0;
1706 // --------- CD tray close menu ----------
1708 static void draw_menu_tray(int menu_sel)
1710 int tl_x = 70, tl_y = 90, y;
1714 text_out16(tl_x, 20, "The unit is about to");
1715 text_out16(tl_x, 30, "close the CD tray.");
1718 text_out16(tl_x, y, "Load new CD image");
1719 text_out16(tl_x, (y+=10), "Insert nothing");
1722 text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
1724 if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
1729 int menu_loop_tray(void)
1731 int menu_sel = 0, menu_sel_max = 1;
1732 unsigned long inp = 0;
1733 char curr_path[PATH_MAX], *selfname;
1738 if ( (tstf = fopen(loadedRomFName, "rb")) )
1741 strcpy(curr_path, loadedRomFName);
1745 getcwd(curr_path, PATH_MAX);
1748 /* make sure action buttons are not pressed on entering menu */
1749 draw_menu_tray(menu_sel);
1750 while (psp_pad_read(1) & PBTN_CIRCLE) psp_msleep(50);
1754 draw_menu_tray(menu_sel);
1755 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_CIRCLE, 0);
1756 if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1757 if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1758 if(inp & PBTN_CIRCLE) {
1760 case 0: // select image
1761 selfname = romsel_loop(curr_path);
1764 cd_img_type cd_type;
1765 cd_type = emu_cdCheck(NULL, romFileName);
1766 if (cd_type != CIT_NOT_CD)
1767 ret = Insert_CD(romFileName, cd_type);
1769 sprintf(menuErrorMsg, "Load failed, invalid CD image?");
1770 lprintf("%s\n", menuErrorMsg);
1773 engineState = PGS_RestartRun;
1777 case 1: // insert nothing
1778 engineState = PGS_RestartRun;
1782 menuErrorMsg[0] = 0; // clear error msg