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"
30 #include <Pico/PicoInt.h>
31 #include <Pico/Patch.h>
32 #include <zlib/zlib.h>
35 #define pspKeyUnkn "???"
36 const char * const keyNames[] = {
37 "SELECT", pspKeyUnkn, pspKeyUnkn, "START", "UP", "RIGHT", "DOWN", "LEFT",
38 "L", "R", pspKeyUnkn, pspKeyUnkn, "TRIANGLE", "CIRCLE", "X", "SQUARE",
39 "HOME", "HOLD", "WLAN_UP", "REMOTE", "VOLUP", "VOLDOWN", "SCREEN", "NOTE",
40 pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, "NUB UP", "NUB RIGHT", "NUB DOWN", "NUB LEFT" // fake
43 static unsigned short bg_buffer[480*272] __attribute__((aligned(16)));
44 #define menu_screen psp_screen
46 static void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
47 static void menu_prepare_bg(int use_game_bg, int use_fg);
50 static unsigned int inp_prev = 0;
52 static unsigned long wait_for_input(unsigned int interesting, int is_key_config)
55 static int repeats = 0, wait = 6;
56 int release = 0, count, i;
59 interesting |= (interesting & 0xf0) << 24; // also use analog
61 if (repeats == 2) wait = 3;
62 else if (repeats == 4) wait = 2;
63 else if (repeats == 6) wait = 1;
65 for (i = 0; i < wait && inp_prev == gp2x_joystick_read(1); i++) {
67 for (i = 0; i < wait && inp_prev == psp_pad_read(1); i++) {
68 if (i == 0) repeats++;
72 for (count = 0; !((ret = psp_pad_read(1)) & interesting) && count < 100; count++) {
77 if (release || ret != inp_prev) {
84 ret |= (ret & 0xf0000000) >> 24; // use analog as d-pad
86 // we don't need diagonals in menus
87 if ((ret&BTN_UP) && (ret&BTN_LEFT)) ret &= ~BTN_LEFT;
88 if ((ret&BTN_UP) && (ret&BTN_RIGHT)) ret &= ~BTN_RIGHT;
89 if ((ret&BTN_DOWN) && (ret&BTN_LEFT)) ret &= ~BTN_LEFT;
90 if ((ret&BTN_DOWN) && (ret&BTN_RIGHT)) ret &= ~BTN_RIGHT;
95 static void menu_draw_begin(void)
97 // short *src = (short *)bg_buffer, *dst = (short *)menu_screen;
100 // for (i = 272; i >= 0; i--, dst += 512, src += 480)
101 // memcpy32((int *)dst, (int *)src, 480*2/4);
103 sceGuSync(0,0); // sync with prev
104 sceGuStart(GU_DIRECT, guCmdList);
105 sceGuCopyImage(GU_PSM_5650, 0, 0, 480, 272, 480, bg_buffer, 0, 0, 512, menu_screen);
111 static void menu_draw_end(void)
117 // --------- loading ROM screen ----------
119 static int lcdr_line = 0;
121 static void load_progress_cb(int percent)
123 int ln, len = percent * 480 / 100;
126 //sceDisplayWaitVblankStart();
128 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
130 if (len > 480) len = 480;
131 for (ln = 8; ln > 0; ln--, dst += 512)
132 memset(dst, 0xff, len*2);
135 static void cdload_progress_cb(int percent)
137 int ln, len = percent * 480 / 100;
140 if (lcdr_line <= 2) {
142 smalltext_out16(1, lcdr_line++ * 10, "Processing CD image / MP3s", 0xffff);
143 smalltext_out16_lim(1, lcdr_line++ * 10, romFileName, 0xffff, 80);
146 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
148 if (len > 480) len = 480;
149 for (ln = 8; ln > 0; ln--, dst += 512)
150 memset(dst, 0xff, len*2);
153 void menu_romload_prepare(const char *rom_name)
155 const char *p = rom_name + strlen(rom_name);
156 while (p > rom_name && *p != '/') p--;
158 psp_video_switch_to_single();
159 if (rom_loaded) menu_draw_begin();
160 else memset32_uncached(psp_screen, 0, 512*272*2/4);
162 smalltext_out16(1, 1, "Loading", 0xffff);
163 smalltext_out16_lim(1, 10, p, 0xffff, 80);
164 PicoCartLoadProgressCB = load_progress_cb;
165 PicoCDLoadProgressCB = cdload_progress_cb;
169 void menu_romload_end(void)
171 PicoCartLoadProgressCB = PicoCDLoadProgressCB = NULL;
172 smalltext_out16(1, ++lcdr_line*10, "Starting emulation...", 0xffff);
175 // -------------- ROM selector --------------
178 #define DT_DIR FIO_SO_IFDIR
179 #define DT_REG FIO_SO_IFREG
183 unsigned char d_type;
187 // bbbb bggg gggr rrrr
188 static unsigned short file2color(const char *fname)
190 const char *ext = fname + strlen(fname) - 3;
191 static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso", "cue" };
192 static const char *other_exts[] = { "gmv", "pat" };
195 if (ext < fname) ext = fname;
196 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
197 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xfdf7;
198 for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
199 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
203 static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
208 n--; // exclude current dir (".")
213 // menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
216 menu_darken_bg((char *)menu_screen + 512*129*2, (char *)menu_screen + 512*129*2, 512*10, 0);
219 smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
220 for (i = 0; i < n; i++) {
222 if (pos < 0) continue;
224 if (namelist[i+1]->d_type & DT_DIR) {
225 smalltext_out16_lim(14, pos*10, "/", 0xd7ff, 1);
226 smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xd7ff, 80-3);
228 unsigned short color = file2color(namelist[i+1]->d_name);
229 smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 80-2);
232 text_out16(5, 130, ">");
236 static int scandir_cmp(const void *p1, const void *p2)
238 struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;
239 if ((*d1)->d_type == (*d2)->d_type) return strcasecmp((*d1)->d_name, (*d2)->d_name);
240 if ((*d1)->d_type & DT_DIR) return -1; // put before
241 if ((*d2)->d_type & DT_DIR) return 1;
242 return strcasecmp((*d1)->d_name, (*d2)->d_name);
245 static char *filter_exts[] = {
246 ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
250 static int scandir_filter(const struct my_dirent *ent)
255 if (ent == NULL || ent->d_name == NULL) return 0;
256 if (strlen(ent->d_name) < 5) return 1;
258 p = ent->d_name + strlen(ent->d_name) - 4;
260 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
262 if (strcasecmp(p, filter_exts[i]) == 0) return 0;
268 static int my_scandir(const char *dir, struct my_dirent ***namelist_out,
269 int(*filter)(const struct my_dirent *),
270 int(*compar)(const void *, const void *))
272 int ret = -1, dir_uid = -1, name_alloc = 4, name_count = 0;
273 struct my_dirent **namelist = NULL, *ent;
276 namelist = malloc(sizeof(*namelist) * name_alloc);
277 if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
279 // try to read first..
280 dir_uid = sceIoDopen(dir);
283 /* it is very important to clear SceIoDirent to be passed to sceIoDread(), */
284 /* or else it may crash, probably misinterpreting something in it. */
285 memset(&sce_ent, 0, sizeof(sce_ent));
286 ret = sceIoDread(dir_uid, &sce_ent);
289 lprintf("sceIoDread(\"%s\") failed with %i\n", dir, ret);
294 lprintf("sceIoDopen(\"%s\") failed with %i\n", dir, dir_uid);
298 ent = malloc(sizeof(*ent));
299 if (ent == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
300 ent->d_type = sce_ent.d_stat.st_attr;
301 strncpy(ent->d_name, sce_ent.d_name, sizeof(ent->d_name));
302 ent->d_name[sizeof(ent->d_name)-1] = 0;
303 if (filter == NULL || filter(ent))
304 namelist[name_count++] = ent;
307 if (name_count >= name_alloc)
311 tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
312 if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
316 memset(&sce_ent, 0, sizeof(sce_ent));
317 ret = sceIoDread(dir_uid, &sce_ent);
321 if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
325 *namelist_out = namelist;
329 if (namelist != NULL)
332 free(namelist[name_count]);
336 if (dir_uid >= 0) sceIoDclose(dir_uid);
341 static SceIoStat cpstat;
343 static char *romsel_loop(char *curr_path)
345 struct my_dirent **namelist;
346 int n, iret, sel = 0;
347 unsigned long inp = 0;
348 char *ret = NULL, *fname = NULL;
350 // is this a dir or a full path?
351 memset(&cpstat, 0, sizeof(cpstat));
352 iret = sceIoGetstat(curr_path, &cpstat);
353 if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFREG)) { // file
355 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
360 else strcpy(curr_path, "ms0:/");
362 else if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFDIR)); // dir
363 else strcpy(curr_path, "ms0:/"); // something else
365 n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
368 n = my_scandir("ms0:/", &namelist, scandir_filter, scandir_cmp);
371 lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
379 for (i = 1; i < n; i++) {
380 if (strcmp(namelist[i]->d_name, fname) == 0) {
389 draw_dirlist(curr_path, namelist, n, sel);
390 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
391 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = n-2; }
392 if(inp & BTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
393 if(inp & BTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
394 if(inp & BTN_L) { sel-=24; if (sel < 0) sel = 0; }
395 if(inp & BTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
396 if(inp & BTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
397 if(inp & BTN_CIRCLE) { // enter dir/select
398 if (namelist[sel+1]->d_type & DT_REG) {
399 strcpy(romFileName, curr_path);
400 strcat(romFileName, "/");
401 strcat(romFileName, namelist[sel+1]->d_name);
404 } else if (namelist[sel+1]->d_type & DT_DIR) {
405 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
406 char *p, *newdir = malloc(newlen);
407 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
408 char *start = curr_path;
409 p = start + strlen(start) - 1;
410 while (*p == '/' && p > start) p--;
411 while (*p != '/' && *p != ':' && p > start) p--;
412 if (p <= start || *p == ':') strcpy(newdir, "ms0:/");
413 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
415 strcpy(newdir, curr_path);
416 p = newdir + strlen(newdir) - 1;
417 while (*p == '/' && p >= newdir) *p-- = 0;
419 strcat(newdir, namelist[sel+1]->d_name);
421 ret = romsel_loop(newdir);
426 if(inp & BTN_X) break; // cancel
430 while(n--) free(namelist[n]);
437 // ------------ debug menu ------------
439 char *debugString(void);
441 static void draw_debug(void)
443 char *p, *str = debugString();
449 for (line = 0; line < 24; line++)
451 while (*p && *p != '\n') p++;
453 if (len > 55) len = 55;
454 smalltext_out16_lim(1, line*10, str, 0xffff, len);
461 static void debug_menu_loop(void)
465 while (!(ret & (BTN_X|BTN_CIRCLE)))
466 ret = wait_for_input(BTN_X|BTN_CIRCLE, 0);
469 // ------------ patch/gg menu ------------
471 static void draw_patchlist(int sel)
473 int start, i, pos, active;
479 for (i = 0; i < PicoPatchCount; i++) {
481 if (pos < 0) continue;
483 active = PicoPatches[i].active;
484 smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
485 smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
488 if (pos < 27) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
490 text_out16(5, 130, ">");
495 static void patches_menu_loop(void)
498 unsigned long inp = 0;
502 draw_patchlist(menu_sel);
503 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
504 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
505 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
506 if(inp &(BTN_LEFT|BTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
507 if(inp &(BTN_RIGHT|BTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
508 if(inp & BTN_CIRCLE) { // action
509 if (menu_sel < PicoPatchCount)
510 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
513 if(inp & BTN_X) return;
518 // ------------ savestate loader ------------
520 static int state_slot_flags = 0;
522 static void state_check_slots(void)
526 state_slot_flags = 0;
528 for (slot = 0; slot < 10; slot++)
530 if (emu_checkSaveFile(slot))
532 state_slot_flags |= 1 << slot;
537 static void *get_oldstate_for_preview(void)
539 unsigned char *ptr = malloc(sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram) + sizeof(Pico.video));
540 if (ptr == NULL) return NULL;
542 memcpy(ptr, Pico.vram, sizeof(Pico.vram));
543 memcpy(ptr + sizeof(Pico.vram), Pico.cram, sizeof(Pico.cram));
544 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram), Pico.vsram, sizeof(Pico.vsram));
545 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), &Pico.video, sizeof(Pico.video));
549 static void restore_oldstate(void *ptrx)
551 unsigned char *ptr = ptrx;
552 memcpy(Pico.vram, ptr, sizeof(Pico.vram));
553 memcpy(Pico.cram, ptr + sizeof(Pico.vram), sizeof(Pico.cram));
554 memcpy(Pico.vsram, ptr + sizeof(Pico.vram) + sizeof(Pico.cram), sizeof(Pico.vsram));
555 memcpy(&Pico.video,ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), sizeof(Pico.video));
559 static void draw_savestate_bg(int slot)
561 void *file, *oldstate;
564 fname = emu_GetSaveFName(1, 0, slot);
567 oldstate = get_oldstate_for_preview();
568 if (oldstate == NULL) return;
570 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
571 file = gzopen(fname, "rb");
572 emu_setSaveStateCbs(1);
574 file = fopen(fname, "rb");
575 emu_setSaveStateCbs(0);
579 if (PicoAHW & PAHW_MCD) {
580 PicoCdLoadStateGfx(file);
582 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
583 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
584 areaSeek(file, 0x2000, SEEK_CUR);
585 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
586 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
587 areaSeek(file, 0x221a0, SEEK_SET);
588 areaRead(&Pico.video, 1, sizeof(Pico.video), file);
594 menu_prepare_bg(1, 0);
596 restore_oldstate(oldstate);
599 static void draw_savestate_menu(int menu_sel, int is_loading)
601 int tl_x = 80+25, tl_y = 16+60, y, i;
603 if (state_slot_flags & (1 << menu_sel))
604 draw_savestate_bg(menu_sel);
607 text_out16(tl_x, 16+30, is_loading ? "Load state" : "Save state");
609 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
611 /* draw all 10 slots */
613 for (i = 0; i < 10; i++, y+=10)
615 text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
617 text_out16(tl_x, y, "back");
622 static int savestate_menu_loop(int is_loading)
624 static int menu_sel = 10;
625 int menu_sel_max = 10;
626 unsigned long inp = 0;
632 draw_savestate_menu(menu_sel, is_loading);
633 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
636 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
637 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
641 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
642 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
644 if(inp & BTN_CIRCLE) { // save/load
646 state_slot = menu_sel;
647 PicoStateProgressCB = emu_msg_cb; /* also suitable for menu */
648 if (emu_SaveLoadGame(is_loading, 0)) {
649 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
655 if(inp & BTN_X) return 1;
659 // -------------- key config --------------
661 static char *action_binds(int player_idx, int action_mask)
663 static char strkeys[32*5];
667 for (i = 0; i < 32; i++) // i is key index
669 if (currentConfig.KeyBinds[i] & action_mask)
671 if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
673 strcat(strkeys, i >= 28 ? ", " : " + "); // nub "buttons" don't create combos
674 strcat(strkeys, keyNames[i]);
677 else strcpy(strkeys, keyNames[i]);
684 static void unbind_action(int action)
688 for (i = 0; i < 32; i++)
689 currentConfig.KeyBinds[i] &= ~action;
692 static int count_bound_keys(int action, int pl_idx)
696 for (i = 0; i < 32; i++)
698 if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
699 if (currentConfig.KeyBinds[i] & action) keys++;
705 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx, int sel)
707 int x, y, tl_y = 16+40, i;
710 if (player_idx >= 0) {
711 text_out16(80+80, 16+20, "Player %i controls", player_idx + 1);
714 text_out16(80+80, 16+20, "Emulator controls");
718 menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
721 for (i = 0; i < opt_cnt; i++, y+=10)
722 text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
724 text_out16(x, y, "Done");
727 text_out16(80+30, 220, "Press a button to bind/unbind");
728 text_out16(80+30, 230, "Use SELECT to clear");
729 text_out16(80+30, 240, "To bind UP/DOWN, hold SELECT");
730 text_out16(80+30, 250, "Select \"Done\" to exit");
732 text_out16(80+30, 230, "Use Options -> Save cfg");
733 text_out16(80+30, 240, "to save controls");
734 text_out16(80+30, 250, "Press X or O to exit");
739 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
741 int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
742 unsigned long inp = 0;
746 draw_key_config(opts, opt_cnt, player_idx, sel);
747 inp = wait_for_input(CONFIGURABLE_KEYS|BTN_SELECT, 1);
748 if (!(inp & BTN_SELECT)) {
750 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
751 if(inp & BTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
753 if (sel >= opt_cnt) {
754 if (inp & (BTN_X|BTN_CIRCLE)) break;
757 // if we are here, we want to bind/unbind something
758 if ((inp & BTN_SELECT) && !prev_select)
759 unbind_action(opts[sel].mask);
760 prev_select = inp & BTN_SELECT;
761 inp &= CONFIGURABLE_KEYS;
763 for (i = 0; i < 32; i++)
764 if (inp & (1 << i)) {
765 if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
766 currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
767 else currentConfig.KeyBinds[i] ^= opts[sel].mask;
768 if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
769 currentConfig.KeyBinds[i] &= ~(3 << 16);
770 currentConfig.KeyBinds[i] |= player_idx << 16;
776 static void draw_kc_sel(int menu_sel)
778 int tl_x = 80+25+40, tl_y = 16+60, y;
782 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
784 text_out16(tl_x, y, "Player 1");
785 text_out16(tl_x, (y+=10), "Player 2");
786 text_out16(tl_x, (y+=10), "Emulator controls");
787 text_out16(tl_x, (y+=10), "Done");
793 // player2_flag, ?, ?, ?, ?, ?, ?, menu
794 // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
795 // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
796 me_bind_action emuctrl_actions[] =
798 { "Load State ", 1<<28 },
799 { "Save State ", 1<<27 },
800 { "Prev Save Slot ", 1<<25 },
801 { "Next Save Slot ", 1<<24 },
802 { "Switch Renderer", 1<<26 },
806 static void kc_sel_loop(void)
808 int menu_sel = 3, menu_sel_max = 3;
809 unsigned long inp = 0;
810 int is_6button = PicoOpt & POPT_6BTN_PAD;
814 draw_kc_sel(menu_sel);
815 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
816 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
817 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
818 if (inp & BTN_CIRCLE) {
820 case 0: key_config_loop(me_ctrl_actions, is_6button ? 12 : 8, 0); return;
821 case 1: key_config_loop(me_ctrl_actions, is_6button ? 12 : 8, 1); return;
822 case 2: key_config_loop(emuctrl_actions,
823 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]) - 1, -1); return;
824 case 3: if (!rom_loaded) emu_WriteConfig(0); return;
828 if (inp & BTN_X) return;
833 // --------- sega/mega cd options ----------
835 menu_entry cdopt_entries[] =
837 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1, 0 },
838 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1, 0 },
839 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1, 0 },
840 { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, ¤tConfig.EmuOpt, 0x0400, 0, 0, 1, 1 },
841 { "CDDA audio (using mp3s)", MB_ONOFF, MA_CDOPT_CDDA, &PicoOpt, 0x0800, 0, 0, 1, 1 },
842 { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &PicoOpt, 0x0400, 0, 0, 1, 1 },
843 { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1, 1 },
844 { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, &PicoOpt, 0x8000, 0, 0, 1, 1 },
845 { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&PicoOpt, 0x1000, 0, 0, 1, 1 },
846 { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &PicoOpt, 0x2000, 0, 0, 1, 1 },
847 { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1, 0 },
850 #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
851 const int cdopt_entry_count = CDOPT_ENTRY_COUNT;
856 char us[32], eu[32], jp[32];
859 static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
861 struct bios_names_t *bios_names = param;
866 case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
867 case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
868 case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
869 case MA_CDOPT_READAHEAD:
870 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
871 else strcpy(ra_buff, " OFF");
872 text_out16(x, y, "ReadAhead buffer %s", ra_buff);
878 static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
880 int tl_x = 80+25, tl_y = 16+60;
884 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
885 else strcpy(ra_buff, " OFF");
889 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
891 me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
893 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
894 if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
895 (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
896 (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
897 text_out16(tl_x, 250, "Press start to test selected BIOS");
902 static void cd_menu_loop_options(void)
904 static int menu_sel = 0;
905 int menu_sel_max = 10;
906 unsigned long inp = 0;
907 struct bios_names_t bios_names;
911 if (emu_findBios(4, &bios)) { // US
912 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
914 strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
915 } else strcpy(bios_names.us, "NOT FOUND");
917 if (emu_findBios(8, &bios)) { // EU
918 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
920 strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
921 } else strcpy(bios_names.eu, "NOT FOUND");
923 if (emu_findBios(1, &bios)) { // JP
924 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
926 strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
927 } else strcpy(bios_names.jp, "NOT FOUND");
933 draw_cd_menu_options(menu_sel, &bios_names);
934 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE|BTN_START, 0);
935 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
936 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
937 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
938 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
939 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
940 selected_id == MA_CDOPT_READAHEAD) {
941 if (inp & BTN_LEFT) {
943 if (PicoCDBuffers < 2) PicoCDBuffers = 0;
945 if (PicoCDBuffers < 2) PicoCDBuffers = 2;
946 else PicoCDBuffers <<= 1;
947 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
951 if (inp & BTN_CIRCLE) // toggleable options
952 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
953 selected_id == MA_CDOPT_DONE) {
956 if (inp & BTN_START) {
957 switch (selected_id) { // BIOS testers
958 case MA_CDOPT_TESTBIOS_USA:
959 if (emu_findBios(4, &bios)) { // test US
960 strcpy(romFileName, bios);
961 engineState = PGS_ReloadRom;
965 case MA_CDOPT_TESTBIOS_EUR:
966 if (emu_findBios(8, &bios)) { // test EU
967 strcpy(romFileName, bios);
968 engineState = PGS_ReloadRom;
972 case MA_CDOPT_TESTBIOS_JAP:
973 if (emu_findBios(1, &bios)) { // test JP
974 strcpy(romFileName, bios);
975 engineState = PGS_ReloadRom;
983 if (inp & BTN_X) return;
987 // --------- display options ----------
989 menu_entry opt3_entries[] =
991 { NULL, MB_NONE, MA_OPT3_SCALE, NULL, 0, 0, 0, 1, 1 },
992 { NULL, MB_NONE, MA_OPT3_HSCALE32, NULL, 0, 0, 0, 1, 1 },
993 { NULL, MB_NONE, MA_OPT3_HSCALE40, NULL, 0, 0, 0, 1, 1 },
994 { NULL, MB_ONOFF, MA_OPT3_FILTERING, ¤tConfig.scaling, 1, 0, 0, 1, 1 },
995 { NULL, MB_RANGE, MA_OPT3_GAMMAA, ¤tConfig.gamma, 0, -4, 16, 1, 1 },
996 { NULL, MB_RANGE, MA_OPT3_BLACKLVL, ¤tConfig.gamma2, 0, 0, 2, 1, 1 },
997 { NULL, MB_NONE, MA_OPT3_VSYNC, NULL, 0, 0, 0, 1, 1 },
998 { "Set to unscaled centered", MB_NONE, MA_OPT3_PRES_NOSCALE, NULL, 0, 0, 0, 1, 0 },
999 { "Set to 4:3 scaled", MB_NONE, MA_OPT3_PRES_SCALE43, NULL, 0, 0, 0, 1, 0 },
1000 { "Set to fullscreen", MB_NONE, MA_OPT3_PRES_FULLSCR, NULL, 0, 0, 0, 1, 0 },
1001 { "done", MB_NONE, MA_OPT3_DONE, NULL, 0, 0, 0, 1, 0 },
1004 #define OPT3_ENTRY_COUNT (sizeof(opt3_entries) / sizeof(opt3_entries[0]))
1005 const int opt3_entry_count = OPT3_ENTRY_COUNT;
1008 static void menu_opt3_cust_draw(const menu_entry *entry, int x, int y, void *param)
1013 text_out16(x, y, "Scale factor: %.2f", currentConfig.scale);
1015 case MA_OPT3_HSCALE32:
1016 text_out16(x, y, "Hor. scale (for low res. games): %.2f", currentConfig.hscale32);
1018 case MA_OPT3_HSCALE40:
1019 text_out16(x, y, "Hor. scale (for hi res. games): %.2f", currentConfig.hscale40);
1021 case MA_OPT3_FILTERING:
1022 text_out16(x, y, "Bilinear filtering %s", currentConfig.scaling?"ON":"OFF");
1024 case MA_OPT3_GAMMAA:
1025 text_out16(x, y, "Gamma adjustment %2i", currentConfig.gamma);
1027 case MA_OPT3_BLACKLVL:
1028 text_out16(x, y, "Black level %2i", currentConfig.gamma2);
1030 case MA_OPT3_VSYNC: {
1031 char *val = " never";
1032 if (currentConfig.EmuOpt & 0x2000)
1033 val = (currentConfig.EmuOpt & 0x10000) ? "sometimes" : " always";
1034 text_out16(x, y, "Wait for vsync (slow) %s", val);
1041 static void menu_opt3_preview(int is_32col)
1043 void *oldstate = NULL;
1045 if (!rom_loaded || ((Pico.video.reg[12]&1)^1) != is_32col)
1047 extern char bgdatac32_start[], bgdatac40_start[];
1048 extern int bgdatac32_size, bgdatac40_size;
1049 void *bgdata = is_32col ? bgdatac32_start : bgdatac40_start;
1050 unsigned long insize = is_32col ? bgdatac32_size : bgdatac40_size, outsize = 65856;
1052 ret = uncompress((Bytef *)bg_buffer, &outsize, bgdata, insize);
1055 if (rom_loaded) oldstate = get_oldstate_for_preview();
1056 memcpy(Pico.vram, bg_buffer, sizeof(Pico.vram));
1057 memcpy(Pico.cram, (char *)bg_buffer + 0x10000, 0x40*2);
1058 memcpy(Pico.vsram, (char *)bg_buffer + 0x10080, 0x40*2);
1059 memcpy(&Pico.video,(char *)bg_buffer + 0x10100, 0x40);
1062 lprintf("uncompress returned %i\n", ret);
1065 memset32_uncached(psp_screen, 0, 512*272*2/4);
1067 menu_prepare_bg(1, 0);
1069 if (oldstate) restore_oldstate(oldstate);
1072 static void draw_dispmenu_options(int menu_sel)
1074 int tl_x = 80, tl_y = 16+50;
1078 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 316);
1080 me_draw(opt3_entries, OPT3_ENTRY_COUNT, tl_x, tl_y, menu_opt3_cust_draw, NULL);
1085 static void dispmenu_loop_options(void)
1087 static int menu_sel = 0;
1088 int menu_sel_max, is_32col = (Pico.video.reg[12]&1)^1;
1089 unsigned long inp = 0;
1090 menu_id selected_id;
1092 menu_sel_max = me_count_enabled(opt3_entries, OPT3_ENTRY_COUNT) - 1;
1096 draw_dispmenu_options(menu_sel);
1097 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
1098 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1099 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1100 selected_id = me_index2id(opt3_entries, OPT3_ENTRY_COUNT, menu_sel);
1101 if (selected_id == MA_OPT3_HSCALE40 && is_32col) { is_32col = 0; menu_opt3_preview(is_32col); }
1102 if (selected_id == MA_OPT3_HSCALE32 && !is_32col) { is_32col = 1; menu_opt3_preview(is_32col); }
1104 if (inp & (BTN_LEFT|BTN_RIGHT)) // multi choise
1106 float *setting = NULL;
1108 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0);
1109 switch (selected_id) {
1110 case MA_OPT3_SCALE: setting = ¤tConfig.scale; break;
1111 case MA_OPT3_HSCALE40: setting = ¤tConfig.hscale40; is_32col = 0; break;
1112 case MA_OPT3_HSCALE32: setting = ¤tConfig.hscale32; is_32col = 1; break;
1113 case MA_OPT3_FILTERING:
1114 case MA_OPT3_GAMMAA:
1115 case MA_OPT3_BLACKLVL: menu_opt3_preview(is_32col); break;
1117 tmp = ((currentConfig.EmuOpt>>13)&1) | ((currentConfig.EmuOpt>>15)&2);
1118 tmp = (inp & BTN_LEFT) ? (tmp>>1) : ((tmp<<1)|1);
1119 if (tmp > 3) tmp = 3;
1120 currentConfig.EmuOpt &= ~0x12000;
1121 currentConfig.EmuOpt |= ((tmp&2)<<15) | ((tmp&1)<<13);
1125 if (setting != NULL) {
1126 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1127 *setting += (inp & BTN_LEFT) ? -0.01 : 0.01;
1128 if (*setting <= 0) *setting = 0.01;
1129 menu_opt3_preview(is_32col);
1130 draw_dispmenu_options(menu_sel); // will wait vsync
1134 if (inp & BTN_CIRCLE) { // toggleable options
1135 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, 1);
1136 switch (selected_id) {
1139 case MA_OPT3_PRES_NOSCALE:
1140 currentConfig.scale = currentConfig.hscale40 = currentConfig.hscale32 = 1.0;
1141 menu_opt3_preview(is_32col);
1143 case MA_OPT3_PRES_SCALE43:
1144 currentConfig.scale = 1.20;
1145 currentConfig.hscale40 = 1.00;
1146 currentConfig.hscale32 = 1.25;
1147 menu_opt3_preview(is_32col);
1149 case MA_OPT3_PRES_FULLSCR:
1150 currentConfig.scale = 1.20;
1151 currentConfig.hscale40 = 1.25;
1152 currentConfig.hscale32 = 1.56;
1153 menu_opt3_preview(is_32col);
1155 case MA_OPT3_FILTERING:
1156 menu_opt3_preview(is_32col);
1161 if (inp & BTN_X) return;
1166 // --------- advanced options ----------
1168 menu_entry opt2_entries[] =
1170 { "Disable sprite limit", MB_ONOFF, MA_OPT2_NO_SPRITE_LIM, &PicoOpt, 0x40000, 0, 0, 1, 1 },
1171 { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &PicoOpt, 0x00004, 0, 0, 1, 1 },
1172 { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &PicoOpt, 0x00001, 0, 0, 1, 1 },
1173 { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496, &PicoOpt, 0x00002, 0, 0, 1, 1 },
1174 { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, ¤tConfig.EmuOpt, 0x00008, 0, 0, 1, 1 },
1175 { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, ¤tConfig.EmuOpt, 0x00020, 0, 0, 1, 1 },
1176 { "Status line in main menu", MB_ONOFF, MA_OPT2_STATUS_LINE, ¤tConfig.EmuOpt, 0x20000, 0, 0, 1, 1 },
1177 { "Disable frame limitter", MB_ONOFF, MA_OPT2_NO_FRAME_LIMIT, ¤tConfig.EmuOpt, 0x40000, 0, 0, 1, 1 },
1178 { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1, 0 },
1181 #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
1182 const int opt2_entry_count = OPT2_ENTRY_COUNT;
1185 static void draw_amenu_options(int menu_sel)
1187 int tl_x = 80+25, tl_y = 16+50;
1191 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
1193 me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1198 static void amenu_loop_options(void)
1200 static int menu_sel = 0;
1202 unsigned long inp = 0;
1203 menu_id selected_id;
1205 menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
1209 draw_amenu_options(menu_sel);
1210 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
1211 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1212 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1213 selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
1214 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1215 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
1216 selected_id == MA_OPT2_GAMMA) {
1220 if (inp & BTN_CIRCLE) { // toggleable options
1221 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
1222 selected_id == MA_OPT2_DONE) {
1226 if (inp & BTN_X) return;
1230 // -------------- options --------------
1233 menu_entry opt_entries[] =
1235 { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1, 1 },
1236 { "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, &PicoOpt, 0x0040, 0, 0, 1, 1 },
1237 { "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, &PicoOpt, 0x0080, 0, 0, 1, 1 },
1238 { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, ¤tConfig.EmuOpt, 0x0002, 0, 0, 1, 1 },
1239 { NULL, MB_RANGE, MA_OPT_FRAMESKIP, ¤tConfig.Frameskip, 0, -1, 16, 1, 1 },
1240 { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, ¤tConfig.EmuOpt, 0x0004, 0, 0, 1, 1 },
1241 { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1, 1 },
1242 { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &PicoOpt, 0x0020, 0, 0, 1, 1 },
1243 { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1, 1 },
1244 { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, ¤tConfig.EmuOpt, 0x0001, 0, 0, 1, 1 },
1245 { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1, 1 },
1246 { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1, 1 },
1247 { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1, 1 },
1248 { "[Display options]", MB_NONE, MA_OPT_DISP_OPTS, NULL, 0, 0, 0, 1, 0 },
1249 { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1, 0 },
1250 { "[Advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1, 0 },
1251 { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1, 0 },
1252 { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1, 0 },
1253 { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1, 0 },
1256 #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
1257 const int opt_entry_count = OPT_ENTRY_COUNT;
1260 static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
1262 char *str, str24[24];
1266 case MA_OPT_RENDERER:
1269 else if (currentConfig.EmuOpt & 0x80)
1272 str = " 8bit accurate"; // n/a
1273 text_out16(x, y, "Renderer: %s", str);
1275 case MA_OPT_FRAMESKIP:
1276 if (currentConfig.Frameskip < 0)
1277 strcpy(str24, "Auto");
1278 else sprintf(str24, "%i", currentConfig.Frameskip);
1279 text_out16(x, y, "Frameskip %s", str24);
1281 case MA_OPT_SOUND_QUALITY:
1282 str = (PicoOpt&0x08)?"stereo":"mono";
1283 text_out16(x, y, "Sound Quality: %5iHz %s", PsndRate, str);
1286 text_out16(x, y, "Region: %s", me_region_name(PicoRegionOverride, PicoAutoRgnOrder));
1288 case MA_OPT_CONFIRM_STATES:
1289 switch ((currentConfig.EmuOpt >> 9) & 5) {
1290 default: str = "OFF"; break;
1291 case 1: str = "writes"; break;
1292 case 4: str = "loads"; break;
1293 case 5: str = "both"; break;
1295 text_out16(x, y, "Confirm savestate %s", str);
1297 case MA_OPT_CPU_CLOCKS:
1298 text_out16(x, y, "CPU/bus clock %3i/%3iMHz", currentConfig.CPUclock, currentConfig.CPUclock/2);
1300 case MA_OPT_SAVECFG:
1302 if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
1303 text_out16(x, y, "Save cfg as default%s", str24);
1305 case MA_OPT_LOADCFG:
1306 text_out16(x, y, "Load cfg from profile %i", config_slot);
1309 lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
1315 static void draw_menu_options(int menu_sel)
1317 int tl_x = 80+25, tl_y = 16+24;
1321 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
1323 me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
1328 static int sndrate_prevnext(int rate, int dir)
1330 int i, rates[] = { 11025, 22050, 44100 };
1332 for (i = 0; i < 5; i++)
1333 if (rates[i] == rate) break;
1336 if (i > 2) return dir ? 44100 : 22050;
1337 if (i < 0) return dir ? 22050 : 11025;
1341 static void region_prevnext(int right)
1343 // jp_ntsc=1, jp_pal=2, usa=4, eu=8
1344 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
1347 if (!PicoRegionOverride) {
1348 for (i = 0; i < 6; i++)
1349 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1350 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
1351 else PicoRegionOverride=1;
1353 else PicoRegionOverride<<=1;
1354 if (PicoRegionOverride > 8) PicoRegionOverride = 8;
1356 if (!PicoRegionOverride) {
1357 for (i = 0; i < 6; i++)
1358 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1359 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
1361 else PicoRegionOverride>>=1;
1365 static void menu_options_save(void)
1367 if (PicoRegionOverride) {
1368 // force setting possibly changed..
1369 Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;
1371 if (!(PicoOpt & POPT_6BTN_PAD)) {
1372 // unbind XYZ MODE, just in case
1373 unbind_action(0xf00);
1377 static int menu_loop_options(void)
1379 static int menu_sel = 0;
1380 int menu_sel_max, ret;
1381 unsigned long inp = 0;
1382 menu_id selected_id;
1384 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_loaded);
1385 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1386 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1387 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1391 draw_menu_options(menu_sel);
1392 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
1393 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1394 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1395 selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
1396 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1397 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0)) {
1398 switch (selected_id) {
1399 case MA_OPT_RENDERER:
1400 if ((PicoOpt & 0x10) || !(currentConfig.EmuOpt & 0x80)) {
1402 currentConfig.EmuOpt |= 0x80;
1405 currentConfig.EmuOpt &= ~0x80;
1408 case MA_OPT_SOUND_QUALITY:
1409 PsndRate = sndrate_prevnext(PsndRate, inp & BTN_RIGHT);
1412 region_prevnext(inp & BTN_RIGHT);
1414 case MA_OPT_CONFIRM_STATES: {
1415 int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
1416 n += (inp & BTN_LEFT) ? -1 : 1;
1417 if (n < 0) n = 0; else if (n > 3) n = 3;
1418 n |= n << 1; n &= ~2;
1419 currentConfig.EmuOpt &= ~0xa00;
1420 currentConfig.EmuOpt |= n << 9;
1423 case MA_OPT_SAVE_SLOT:
1424 if (inp & BTN_RIGHT) {
1425 state_slot++; if (state_slot > 9) state_slot = 0;
1426 } else {state_slot--; if (state_slot < 0) state_slot = 9;
1429 case MA_OPT_CPU_CLOCKS:
1430 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1431 currentConfig.CPUclock += (inp & BTN_LEFT) ? -1 : 1;
1432 if (currentConfig.CPUclock < 19) currentConfig.CPUclock = 19;
1433 if (currentConfig.CPUclock > 333) currentConfig.CPUclock = 333;
1434 draw_menu_options(menu_sel); // will wait vsync
1437 case MA_OPT_SAVECFG:
1438 case MA_OPT_SAVECFG_GAME:
1439 case MA_OPT_LOADCFG:
1440 config_slot += (inp&BTN_RIGHT) ? 1 : -1;
1441 if (config_slot > 9) config_slot = 0;
1442 if (config_slot < 0) config_slot = 9;
1443 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1444 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1445 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1448 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1453 if (inp & BTN_CIRCLE) {
1454 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
1456 switch (selected_id)
1458 case MA_OPT_DISP_OPTS:
1459 dispmenu_loop_options();
1461 case MA_OPT_SCD_OPTS:
1462 cd_menu_loop_options();
1463 if (engineState == PGS_ReloadRom)
1464 return 0; // test BIOS
1466 case MA_OPT_ADV_OPTS:
1467 amenu_loop_options();
1469 case MA_OPT_SAVECFG: // done (update and write)
1470 menu_options_save();
1471 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
1472 else strcpy(menuErrorMsg, "failed to write config");
1474 case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
1475 menu_options_save();
1476 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
1477 else strcpy(menuErrorMsg, "failed to write config");
1479 case MA_OPT_LOADCFG:
1480 ret = emu_ReadConfig(1, 1);
1481 if (!ret) ret = emu_ReadConfig(0, 1);
1482 if (ret) strcpy(menuErrorMsg, "config loaded");
1483 else strcpy(menuErrorMsg, "failed to load config");
1486 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1492 menu_options_save();
1493 return 0; // done (update, no write)
1498 // -------------- credits --------------
1500 static void draw_menu_credits(void)
1502 int tl_x = 80+15, tl_y = 16+64, y;
1505 text_out16(tl_x, 16+20, "PicoDrive v" VERSION " (c) notaz, 2006-2008");
1508 text_out16(tl_x, y, "Credits:");
1509 text_out16(tl_x, (y+=10), "fDave: base code of PicoDrive");
1510 text_out16(tl_x, (y+=10), "Chui: Fame/C");
1511 text_out16(tl_x, (y+=10), "NJ: CZ80");
1512 text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
1513 text_out16(tl_x, (y+=10), "Stephane Dallongeville:");
1514 text_out16(tl_x, (y+=10), " Gens code, base of Fame/C, CZ80");
1515 text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");
1516 text_out16(tl_x, (y+=10), "Haze: Genesis hw info");
1517 text_out16(tl_x, (y+=10), "ps2dev.org people: PSP SDK/code");
1518 text_out16(tl_x, (y+=10), "ketchupgun: skin design");
1524 // -------------- root menu --------------
1526 menu_entry main_entries[] =
1528 { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
1529 { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
1530 { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
1531 { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
1532 { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
1533 { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
1534 { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
1535 { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
1536 { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
1537 { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
1540 #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
1542 static void draw_menu_root(int menu_sel)
1544 const int tl_x = 86+70, tl_y = 16+70;
1549 if ((currentConfig.EmuOpt&0x20000) && (stat = psp_get_status_line()))
1550 text_out16(287, 12, "%s", stat);
1552 text_out16(tl_x, 48, "PicoDrive v" VERSION);
1554 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
1556 me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1559 if (menuErrorMsg[0])
1560 text_out16(10, 252, menuErrorMsg);
1565 static void menu_loop_root(void)
1567 static int menu_sel = 0;
1568 int ret, menu_sel_max;
1569 unsigned long inp = 0;
1571 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_loaded);
1572 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_loaded);
1573 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_loaded);
1574 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_loaded);
1575 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
1577 menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
1578 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1581 if (mp3_last_error != 0) {
1582 if (mp3_last_error == -1)
1583 sprintf(menuErrorMsg, "Unsupported mp3 format, use 44kHz stereo");
1584 else sprintf(menuErrorMsg, "mp3 init failed, code %08x", mp3_last_error);
1588 /* make sure action buttons are not pressed on entering menu */
1589 draw_menu_root(menu_sel);
1591 while (psp_pad_read(1) & (BTN_X|BTN_CIRCLE|BTN_SELECT)) psp_msleep(50);
1595 draw_menu_root(menu_sel);
1596 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE|BTN_SELECT|BTN_L|BTN_R, 0);
1597 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1598 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1599 if((inp & (BTN_L|BTN_R)) == (BTN_L|BTN_R)) debug_menu_loop();
1600 if( inp & (BTN_SELECT|BTN_X)) {
1602 while (psp_pad_read(1) & (BTN_SELECT|BTN_X)) psp_msleep(50); // wait until released
1603 engineState = PGS_Running;
1607 if(inp & BTN_CIRCLE) {
1608 menuErrorMsg[0] = 0; // clear error msg
1609 switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
1611 case MA_MAIN_RESUME_GAME:
1613 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
1614 engineState = PGS_Running;
1618 case MA_MAIN_SAVE_STATE:
1620 if(savestate_menu_loop(0))
1622 engineState = PGS_Running;
1626 case MA_MAIN_LOAD_STATE:
1628 if(savestate_menu_loop(1))
1630 engineState = PGS_Running;
1634 case MA_MAIN_RESET_GAME:
1637 engineState = PGS_Running;
1641 case MA_MAIN_LOAD_ROM:
1643 char curr_path[PATH_MAX], *selfname;
1645 if ( (tstf = fopen(lastRomFile, "rb")) )
1648 strcpy(curr_path, lastRomFile);
1651 getcwd(curr_path, PATH_MAX);
1652 selfname = romsel_loop(curr_path);
1654 lprintf("selected file: %s\n", selfname);
1655 engineState = PGS_ReloadRom;
1660 case MA_MAIN_OPTIONS:
1661 ret = menu_loop_options();
1662 if (ret == 1) continue; // status update
1663 if (engineState == PGS_ReloadRom)
1664 return; // BIOS test
1666 case MA_MAIN_CONTROLS:
1669 case MA_MAIN_CREDITS:
1670 draw_menu_credits();
1673 while (!(inp & (BTN_X|BTN_CIRCLE)))
1674 inp = wait_for_input(BTN_X|BTN_CIRCLE, 0);
1677 engineState = PGS_Quit;
1679 case MA_MAIN_PATCHES:
1680 if (rom_loaded && PicoPatches) {
1681 patches_menu_loop();
1683 strcpy(menuErrorMsg, "Patches applied");
1688 lprintf("%s: something unknown selected\n", __FUNCTION__);
1695 // warning: alignment
1696 static void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
1698 unsigned int *dest = dst;
1699 const unsigned int *srce = src;
1705 unsigned int p = *srce++;
1706 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
1713 unsigned int p = *srce++;
1714 *dest++ = (p&0xf79ef79e)>>1;
1719 static void menu_prepare_bg(int use_game_bg, int use_fg)
1723 // darken the active framebuffer
1724 unsigned short *dst = bg_buffer;
1725 unsigned short *src = use_fg ? psp_video_get_active_fb() : psp_screen;
1727 for (i = 272; i > 0; i--, dst += 480, src += 512)
1728 menu_darken_bg(dst, src, 480, 1);
1729 //memset32_uncached((int *)(bg_buffer + 480*264), 0, 480*8*2/4);
1733 // should really only happen once, on startup..
1734 memset32_uncached((int *)(void *)bg_buffer, 0, sizeof(bg_buffer)/4);
1735 readpng(bg_buffer, "skin/background.png", READPNG_BG);
1737 sceKernelDcacheWritebackAll();
1740 static void menu_gfx_prepare(void)
1742 menu_prepare_bg(rom_loaded, 1);
1749 void menu_loop(void)
1755 menuErrorMsg[0] = 0;
1759 // --------- CD tray close menu ----------
1761 static void draw_menu_tray(int menu_sel)
1763 int tl_x = 70, tl_y = 90, y;
1767 text_out16(tl_x, 20, "The unit is about to");
1768 text_out16(tl_x, 30, "close the CD tray.");
1771 text_out16(tl_x, y, "Load new CD image");
1772 text_out16(tl_x, (y+=10), "Insert nothing");
1775 text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
1777 if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
1782 int menu_loop_tray(void)
1784 int menu_sel = 0, menu_sel_max = 1;
1785 unsigned long inp = 0;
1786 char curr_path[PATH_MAX], *selfname;
1791 if ( (tstf = fopen(lastRomFile, "rb")) )
1794 strcpy(curr_path, lastRomFile);
1798 getcwd(curr_path, PATH_MAX);
1801 /* make sure action buttons are not pressed on entering menu */
1802 draw_menu_tray(menu_sel);
1803 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
1807 draw_menu_tray(menu_sel);
1808 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_CIRCLE, 0);
1809 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1810 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1811 if(inp & BTN_CIRCLE) {
1813 case 0: // select image
1814 selfname = romsel_loop(curr_path);
1817 cd_img_type cd_type;
1818 cd_type = emu_cdCheck(NULL);
1819 if (cd_type != CIT_NOT_CD)
1820 ret = Insert_CD(romFileName, cd_type);
1822 sprintf(menuErrorMsg, "Load failed, invalid CD image?");
1823 lprintf("%s\n", menuErrorMsg);
1826 engineState = PGS_RestartRun;
1830 case 1: // insert nothing
1831 engineState = PGS_RestartRun;
1835 menuErrorMsg[0] = 0; // clear error msg