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 static const char * const pspKeyNames[] = {
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 = 50;
56 int release = 0, count, i;
59 interesting |= (interesting & 0xf0) << 24; // also use analog
61 if (repeats == 2 || repeats == 4) wait /= 2;
62 if (repeats == 6) wait = 15;
64 for (i = 0; i < 6 && inp_prev == psp_pad_read(1); i++) {
65 if (i == 0) repeats++;
69 for (count = 0; !((ret = psp_pad_read(1)) & interesting) && count < 100; count++) {
74 if (release || ret != inp_prev) {
81 ret |= (ret & 0xf0000000) >> 24; // use analog as d-pad
83 // we don't need diagonals in menus
84 if ((ret&BTN_UP) && (ret&BTN_LEFT)) ret &= ~BTN_LEFT;
85 if ((ret&BTN_UP) && (ret&BTN_RIGHT)) ret &= ~BTN_RIGHT;
86 if ((ret&BTN_DOWN) && (ret&BTN_LEFT)) ret &= ~BTN_LEFT;
87 if ((ret&BTN_DOWN) && (ret&BTN_RIGHT)) ret &= ~BTN_RIGHT;
92 static void menu_draw_begin(void)
94 // short *src = (short *)bg_buffer, *dst = (short *)menu_screen;
97 // for (i = 272; i >= 0; i--, dst += 512, src += 480)
98 // memcpy32((int *)dst, (int *)src, 480*2/4);
100 sceGuSync(0,0); // sync with prev
101 sceGuStart(GU_DIRECT, guCmdList);
102 sceGuCopyImage(GU_PSM_5650, 0, 0, 480, 272, 480, bg_buffer, 0, 0, 512, menu_screen);
108 static void menu_draw_end(void)
114 // --------- loading ROM screen ----------
116 static int lcdr_line = 0;
118 static void load_progress_cb(int percent)
120 int ln, len = percent * 480 / 100;
123 //sceDisplayWaitVblankStart();
125 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
127 if (len > 480) len = 480;
128 for (ln = 8; ln > 0; ln--, dst += 512)
129 memset(dst, 0xff, len*2);
132 static void cdload_progress_cb(int percent)
134 int ln, len = percent * 480 / 100;
137 if (lcdr_line <= 2) {
139 smalltext_out16(1, lcdr_line++ * 10, "Processing CD image / MP3s", 0xffff);
140 smalltext_out16_lim(1, lcdr_line++ * 10, romFileName, 0xffff, 80);
143 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
145 if (len > 480) len = 480;
146 for (ln = 8; ln > 0; ln--, dst += 512)
147 memset(dst, 0xff, len*2);
150 void menu_romload_prepare(const char *rom_name)
152 const char *p = rom_name + strlen(rom_name);
153 while (p > rom_name && *p != '/') p--;
155 psp_video_switch_to_single();
156 if (rom_data) menu_draw_begin();
157 else memset32_uncached(psp_screen, 0, 512*272*2/4);
159 smalltext_out16(1, 1, "Loading", 0xffff);
160 smalltext_out16_lim(1, 10, p, 0xffff, 80);
161 PicoCartLoadProgressCB = load_progress_cb;
162 PicoCDLoadProgressCB = cdload_progress_cb;
166 void menu_romload_end(void)
168 PicoCartLoadProgressCB = PicoCDLoadProgressCB = NULL;
169 smalltext_out16(1, ++lcdr_line*10, "Starting emulation...", 0xffff);
172 // -------------- ROM selector --------------
175 #define DT_DIR FIO_SO_IFDIR
176 #define DT_REG FIO_SO_IFREG
180 unsigned char d_type;
184 // bbbb bggg gggr rrrr
185 static unsigned short file2color(const char *fname)
187 const char *ext = fname + strlen(fname) - 3;
188 static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso" };
189 static const char *other_exts[] = { "gmv", "pat" };
192 if (ext < fname) ext = fname;
193 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
194 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xfdf7;
195 for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
196 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
200 static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
205 n--; // exclude current dir (".")
209 if (rom_data == NULL) {
210 // menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
213 menu_darken_bg((char *)menu_screen + 512*129*2, (char *)menu_screen + 512*129*2, 512*10, 0);
216 smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
217 for (i = 0; i < n; i++) {
219 if (pos < 0) continue;
221 if (namelist[i+1]->d_type & DT_DIR) {
222 smalltext_out16_lim(14, pos*10, "/", 0xd7ff, 1);
223 smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xd7ff, 80-3);
225 unsigned short color = file2color(namelist[i+1]->d_name);
226 smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 80-2);
229 text_out16(5, 130, ">");
233 static int scandir_cmp(const void *p1, const void *p2)
235 struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;
236 if ((*d1)->d_type == (*d2)->d_type) return strcasecmp((*d1)->d_name, (*d2)->d_name);
237 if ((*d1)->d_type & DT_DIR) return -1; // put before
238 if ((*d2)->d_type & DT_DIR) return 1;
239 return strcasecmp((*d1)->d_name, (*d2)->d_name);
242 static char *filter_exts[] = {
243 ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
244 ".jpg", ".cue", ".pbp"
247 static int scandir_filter(const struct my_dirent *ent)
252 if (ent == NULL || ent->d_name == NULL) return 0;
253 if (strlen(ent->d_name) < 5) return 1;
255 p = ent->d_name + strlen(ent->d_name) - 4;
257 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
259 if (strcasecmp(p, filter_exts[i]) == 0) return 0;
265 static int my_scandir(const char *dir, struct my_dirent ***namelist_out,
266 int(*filter)(const struct my_dirent *),
267 int(*compar)(const void *, const void *))
269 int ret = -1, dir_uid = -1, name_alloc = 4, name_count = 0;
270 struct my_dirent **namelist = NULL, *ent;
273 namelist = malloc(sizeof(*namelist) * name_alloc);
274 if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
276 // try to read first..
277 dir_uid = sceIoDopen(dir);
280 /* it is very important to clear SceIoDirent to be passed to sceIoDread(), */
281 /* or else it may crash, probably misinterpreting something in it. */
282 memset(&sce_ent, 0, sizeof(sce_ent));
283 ret = sceIoDread(dir_uid, &sce_ent);
286 lprintf("sceIoDread(\"%s\") failed with %i\n", dir, ret);
291 lprintf("sceIoDopen(\"%s\") failed with %i\n", dir, dir_uid);
295 ent = malloc(sizeof(*ent));
296 if (ent == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
297 ent->d_type = sce_ent.d_stat.st_attr;
298 strncpy(ent->d_name, sce_ent.d_name, sizeof(ent->d_name));
299 ent->d_name[sizeof(ent->d_name)-1] = 0;
300 if (filter == NULL || filter(ent))
301 namelist[name_count++] = ent;
304 if (name_count >= name_alloc)
308 tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
309 if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
313 memset(&sce_ent, 0, sizeof(sce_ent));
314 ret = sceIoDread(dir_uid, &sce_ent);
318 if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
322 *namelist_out = namelist;
326 if (namelist != NULL)
329 free(namelist[name_count]);
333 if (dir_uid >= 0) sceIoDclose(dir_uid);
338 static char *romsel_loop(char *curr_path)
340 struct my_dirent **namelist;
341 int n, iret, sel = 0;
342 unsigned long inp = 0;
343 char *ret = NULL, *fname = NULL;
346 // is this a dir or a full path?
347 memset(&cpstat, 0, sizeof(cpstat));
348 iret = sceIoGetstat(curr_path, &cpstat);
349 if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFREG)) { // file
351 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
355 else if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFDIR)); // dir
356 else strcpy(curr_path, "ms0:/"); // something else
358 n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
361 n = my_scandir("ms0:/", &namelist, scandir_filter, scandir_cmp);
364 lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
372 for (i = 1; i < n; i++) {
373 if (strcmp(namelist[i]->d_name, fname) == 0) {
382 draw_dirlist(curr_path, namelist, n, sel);
383 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
384 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = n-2; }
385 if(inp & BTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
386 if(inp & BTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
387 if(inp & BTN_L) { sel-=24; if (sel < 0) sel = 0; }
388 if(inp & BTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
389 if(inp & BTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
390 if(inp & BTN_CIRCLE) { // enter dir/select
391 if (namelist[sel+1]->d_type & DT_REG) {
392 strcpy(romFileName, curr_path);
393 strcat(romFileName, "/");
394 strcat(romFileName, namelist[sel+1]->d_name);
397 } else if (namelist[sel+1]->d_type & DT_DIR) {
398 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
399 char *p, *newdir = malloc(newlen);
400 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
401 char *start = curr_path;
402 p = start + strlen(start) - 1;
403 while (*p == '/' && p > start) p--;
404 while (*p != '/' && *p != ':' && p > start) p--;
405 if (p <= start || *p == ':' || p[-1] == ':') strcpy(newdir, "ms0:/");
406 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
408 strcpy(newdir, curr_path);
409 p = newdir + strlen(newdir) - 1;
410 while (*p == '/' && p >= newdir) *p-- = 0;
412 strcat(newdir, namelist[sel+1]->d_name);
414 ret = romsel_loop(newdir);
419 if(inp & BTN_X) break; // cancel
423 while(n--) free(namelist[n]);
430 // ------------ debug menu ------------
432 char *debugString(void);
434 static void draw_debug(void)
436 char *p, *str = debugString();
442 for (line = 0; line < 24; line++)
444 while (*p && *p != '\n') p++;
446 if (len > 55) len = 55;
447 smalltext_out16_lim(1, line*10, str, 0xffff, len);
454 static void debug_menu_loop(void)
458 while (!(ret & (BTN_X|BTN_CIRCLE)))
459 ret = wait_for_input(BTN_X|BTN_CIRCLE, 0);
462 // ------------ patch/gg menu ------------
464 static void draw_patchlist(int sel)
466 int start, i, pos, active;
472 for (i = 0; i < PicoPatchCount; i++) {
474 if (pos < 0) continue;
476 active = PicoPatches[i].active;
477 smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
478 smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
481 if (pos < 27) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
483 text_out16(5, 130, ">");
488 static void patches_menu_loop(void)
491 unsigned long inp = 0;
495 draw_patchlist(menu_sel);
496 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
497 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
498 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
499 if(inp &(BTN_LEFT|BTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
500 if(inp &(BTN_RIGHT|BTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
501 if(inp & BTN_CIRCLE) { // action
502 if (menu_sel < PicoPatchCount)
503 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
506 if(inp & BTN_X) return;
511 // ------------ savestate loader ------------
513 static int state_slot_flags = 0;
515 static void state_check_slots(void)
519 state_slot_flags = 0;
521 for (slot = 0; slot < 10; slot++)
523 if (emu_checkSaveFile(slot))
525 state_slot_flags |= 1 << slot;
530 static void *get_oldstate_for_preview(void)
532 unsigned char *ptr = malloc(sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram) + sizeof(Pico.video));
533 if (ptr == NULL) return NULL;
535 memcpy(ptr, Pico.vram, sizeof(Pico.vram));
536 memcpy(ptr + sizeof(Pico.vram), Pico.cram, sizeof(Pico.cram));
537 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram), Pico.vsram, sizeof(Pico.vsram));
538 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), &Pico.video, sizeof(Pico.video));
542 static void restore_oldstate(void *ptrx)
544 unsigned char *ptr = ptrx;
545 memcpy(Pico.vram, ptr, sizeof(Pico.vram));
546 memcpy(Pico.cram, ptr + sizeof(Pico.vram), sizeof(Pico.cram));
547 memcpy(Pico.vsram, ptr + sizeof(Pico.vram) + sizeof(Pico.cram), sizeof(Pico.vsram));
548 memcpy(&Pico.video,ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), sizeof(Pico.video));
552 static void draw_savestate_bg(int slot)
554 void *file, *oldstate;
557 fname = emu_GetSaveFName(1, 0, slot);
560 oldstate = get_oldstate_for_preview();
561 if (oldstate == NULL) return;
563 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
564 file = gzopen(fname, "rb");
565 emu_setSaveStateCbs(1);
567 file = fopen(fname, "rb");
568 emu_setSaveStateCbs(0);
573 PicoCdLoadStateGfx(file);
575 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
576 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
577 areaSeek(file, 0x2000, SEEK_CUR);
578 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
579 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
580 areaSeek(file, 0x221a0, SEEK_SET);
581 areaRead(&Pico.video, 1, sizeof(Pico.video), file);
587 menu_prepare_bg(1, 0);
589 restore_oldstate(oldstate);
592 static void draw_savestate_menu(int menu_sel, int is_loading)
594 int tl_x = 80+25, tl_y = 16+60, y, i;
596 if (state_slot_flags & (1 << menu_sel))
597 draw_savestate_bg(menu_sel);
600 text_out16(tl_x, 16+30, is_loading ? "Load state" : "Save state");
602 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
604 /* draw all 10 slots */
606 for (i = 0; i < 10; i++, y+=10)
608 text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
610 text_out16(tl_x, y, "back");
615 static int savestate_menu_loop(int is_loading)
617 static int menu_sel = 10;
618 int menu_sel_max = 10;
619 unsigned long inp = 0;
625 draw_savestate_menu(menu_sel, is_loading);
626 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
629 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
630 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
634 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
635 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
637 if(inp & BTN_CIRCLE) { // save/load
639 state_slot = menu_sel;
640 PicoStateProgressCB = emu_msg_cb; /* also suitable for menu */
641 if (emu_SaveLoadGame(is_loading, 0)) {
642 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
648 if(inp & BTN_X) return 1;
652 // -------------- key config --------------
654 static char *action_binds(int player_idx, int action_mask)
656 static char strkeys[32*5];
660 for (i = 0; i < 32; i++) // i is key index
662 if (currentConfig.KeyBinds[i] & action_mask)
664 if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
666 strcat(strkeys, i >= 28 ? ", " : " + "); // nub "buttons" don't create combos
667 strcat(strkeys, pspKeyNames[i]);
670 else strcpy(strkeys, pspKeyNames[i]);
677 static void unbind_action(int action)
681 for (i = 0; i < 32; i++)
682 currentConfig.KeyBinds[i] &= ~action;
685 static int count_bound_keys(int action, int pl_idx)
689 for (i = 0; i < 32; i++)
691 if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
692 if (currentConfig.KeyBinds[i] & action) keys++;
698 typedef struct { char *name; int mask; } bind_action_t;
700 static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)
702 int x, y, tl_y = 16+40, i;
705 if (player_idx >= 0) {
706 text_out16(80+80, 16+20, "Player %i controls", player_idx + 1);
709 text_out16(80+80, 16+20, "Emulator controls");
713 menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
716 for (i = 0; i < opt_cnt; i++, y+=10)
717 text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
719 text_out16(x, y, "Done");
722 text_out16(80+30, 220, "Press a button to bind/unbind");
723 text_out16(80+30, 230, "Use SELECT to clear");
724 text_out16(80+30, 240, "To bind UP/DOWN, hold SELECT");
725 text_out16(80+30, 250, "Select \"Done\" to exit");
727 text_out16(80+30, 230, "Use Options -> Save cfg");
728 text_out16(80+30, 240, "to save controls");
729 text_out16(80+30, 250, "Press X or O to exit");
734 static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)
736 int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
737 unsigned long inp = 0;
741 draw_key_config(opts, opt_cnt, player_idx, sel);
742 inp = wait_for_input(CONFIGURABLE_KEYS|BTN_SELECT, 1);
743 if (!(inp & BTN_SELECT)) {
745 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
746 if(inp & BTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
748 if (sel >= opt_cnt) {
749 if (inp & (BTN_X|BTN_CIRCLE)) break;
752 // if we are here, we want to bind/unbind something
753 if ((inp & BTN_SELECT) && !prev_select)
754 unbind_action(opts[sel].mask);
755 prev_select = inp & BTN_SELECT;
756 inp &= CONFIGURABLE_KEYS;
758 for (i = 0; i < 32; i++)
759 if (inp & (1 << i)) {
760 if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
761 currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
762 else currentConfig.KeyBinds[i] ^= opts[sel].mask;
763 if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
764 currentConfig.KeyBinds[i] &= ~(3 << 16);
765 currentConfig.KeyBinds[i] |= player_idx << 16;
771 static void draw_kc_sel(int menu_sel)
773 int tl_x = 80+25+40, tl_y = 16+60, y;
777 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
779 text_out16(tl_x, y, "Player 1");
780 text_out16(tl_x, (y+=10), "Player 2");
781 text_out16(tl_x, (y+=10), "Emulator controls");
782 text_out16(tl_x, (y+=10), "Done");
788 // PicoPad[] format: MXYZ SACB RLDU
789 static bind_action_t ctrl_actions[] =
805 // player2_flag, ?, ?, ?, ?, ?, ?, menu
806 // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
807 // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
808 static bind_action_t emuctrl_actions[] =
810 { "Load State ", 1<<28 },
811 { "Save State ", 1<<27 },
812 { "Prev Save Slot ", 1<<25 },
813 { "Next Save Slot ", 1<<24 },
814 { "Switch Renderer", 1<<26 },
817 static void kc_sel_loop(void)
819 int menu_sel = 3, menu_sel_max = 3;
820 unsigned long inp = 0;
821 int is_6button = currentConfig.PicoOpt & 0x020;
825 draw_kc_sel(menu_sel);
826 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
827 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
828 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
829 if (inp & BTN_CIRCLE) {
831 case 0: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 0); return;
832 case 1: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 1); return;
833 case 2: key_config_loop(emuctrl_actions,
834 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;
835 case 3: if (rom_data == NULL) emu_WriteConfig(0); return;
839 if (inp & BTN_X) return;
844 // --------- sega/mega cd options ----------
846 menu_entry cdopt_entries[] =
848 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1 },
849 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1 },
850 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1 },
851 { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, ¤tConfig.EmuOpt, 0x0400, 0, 0, 1 },
852 { "CDDA audio (using mp3s)", MB_ONOFF, MA_CDOPT_CDDA, ¤tConfig.PicoOpt, 0x0800, 0, 0, 1 },
853 { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, ¤tConfig.PicoOpt, 0x0400, 0, 0, 1 },
854 { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1 },
855 { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, ¤tConfig.PicoOpt, 0x8000, 0, 0, 1 },
856 { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,¤tConfig.PicoOpt, 0x1000, 0, 0, 1 },
857 { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, ¤tConfig.PicoOpt, 0x2000, 0, 0, 1 },
858 { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1 },
861 #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
866 char us[32], eu[32], jp[32];
869 static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
871 struct bios_names_t *bios_names = param;
876 case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
877 case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
878 case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
879 case MA_CDOPT_READAHEAD:
880 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
881 else strcpy(ra_buff, " OFF");
882 text_out16(x, y, "ReadAhead buffer %s", ra_buff);
888 static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
890 int tl_x = 80+25, tl_y = 16+60;
894 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
895 else strcpy(ra_buff, " OFF");
899 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
901 me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
903 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
904 if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
905 (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
906 (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
907 text_out16(tl_x, 250, "Press start to test selected BIOS");
912 static void cd_menu_loop_options(void)
914 static int menu_sel = 0;
915 int menu_sel_max = 10;
916 unsigned long inp = 0;
917 struct bios_names_t bios_names;
921 if (emu_findBios(4, &bios)) { // US
922 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
924 strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
925 } else strcpy(bios_names.us, "NOT FOUND");
927 if (emu_findBios(8, &bios)) { // EU
928 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
930 strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
931 } else strcpy(bios_names.eu, "NOT FOUND");
933 if (emu_findBios(1, &bios)) { // JP
934 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
936 strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
937 } else strcpy(bios_names.jp, "NOT FOUND");
943 draw_cd_menu_options(menu_sel, &bios_names);
944 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE|BTN_START, 0);
945 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
946 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
947 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
948 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
949 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
950 selected_id == MA_CDOPT_READAHEAD) {
951 if (inp & BTN_LEFT) {
953 if (PicoCDBuffers < 64) PicoCDBuffers = 0;
955 if (PicoCDBuffers < 64) PicoCDBuffers = 64;
956 else PicoCDBuffers <<= 1;
957 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
961 if (inp & BTN_CIRCLE) // toggleable options
962 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
963 selected_id == MA_CDOPT_DONE) {
966 if (inp & BTN_START) {
967 switch (selected_id) { // BIOS testers
968 case MA_CDOPT_TESTBIOS_USA:
969 if (emu_findBios(4, &bios)) { // test US
970 strcpy(romFileName, bios);
971 engineState = PGS_ReloadRom;
975 case MA_CDOPT_TESTBIOS_EUR:
976 if (emu_findBios(8, &bios)) { // test EU
977 strcpy(romFileName, bios);
978 engineState = PGS_ReloadRom;
982 case MA_CDOPT_TESTBIOS_JAP:
983 if (emu_findBios(1, &bios)) { // test JP
984 strcpy(romFileName, bios);
985 engineState = PGS_ReloadRom;
993 if (inp & BTN_X) return;
997 // --------- display options ----------
999 menu_entry opt3_entries[] =
1001 { NULL, MB_NONE, MA_OPT3_SCALE, NULL, 0, 0, 0, 1 },
1002 { NULL, MB_NONE, MA_OPT3_HSCALE32, NULL, 0, 0, 0, 1 },
1003 { NULL, MB_NONE, MA_OPT3_HSCALE40, NULL, 0, 0, 0, 1 },
1004 { NULL, MB_ONOFF, MA_OPT3_FILTERING, ¤tConfig.scaling, 1, 0, 0, 1 },
1005 { NULL, MB_NONE, MA_OPT3_VSYNC, NULL, 0, 0, 0, 1 },
1006 { "Set to unscaled centered", MB_NONE, MA_OPT3_PRES_NOSCALE, NULL, 0, 0, 0, 1 },
1007 { "Set to fullscreen", MB_NONE, MA_OPT3_PRES_FULLSCR, NULL, 0, 0, 0, 1 },
1008 { "done", MB_NONE, MA_OPT3_DONE, NULL, 0, 0, 0, 1 },
1011 #define OPT3_ENTRY_COUNT (sizeof(opt3_entries) / sizeof(opt3_entries[0]))
1014 static void menu_opt3_cust_draw(const menu_entry *entry, int x, int y, void *param)
1019 text_out16(x, y, "Scale factor: %.2f", currentConfig.scale);
1021 case MA_OPT3_HSCALE32:
1022 text_out16(x, y, "Hor. scale (for low res. games): %.2f", currentConfig.hscale32);
1024 case MA_OPT3_HSCALE40:
1025 text_out16(x, y, "Hor. scale (for hi res. games): %.2f", currentConfig.hscale40);
1027 case MA_OPT3_FILTERING:
1028 text_out16(x, y, "Bilinear filtering %s", currentConfig.scaling?"ON":"OFF");
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_data == NULL || ((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_data != NULL) 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:menu_opt3_preview(is_32col); break;
1114 case MA_OPT3_VSYNC: tmp = ((currentConfig.EmuOpt>>13)&1) | ((currentConfig.EmuOpt>>15)&2);
1115 tmp = (inp & BTN_LEFT) ? (tmp>>1) : ((tmp<<1)|1);
1116 if (tmp > 3) tmp = 3;
1117 currentConfig.EmuOpt &= ~0x12000;
1118 currentConfig.EmuOpt |= ((tmp&2)<<15) | ((tmp&1)<<13);
1122 if (setting != NULL) {
1123 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1124 *setting += (inp & BTN_LEFT) ? -0.01 : 0.01;
1125 if (*setting <= 0) *setting = 0.01;
1126 menu_opt3_preview(is_32col);
1127 draw_dispmenu_options(menu_sel); // will wait vsync
1131 if (inp & BTN_CIRCLE) { // toggleable options
1132 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, 1);
1133 switch (selected_id) {
1136 case MA_OPT3_PRES_NOSCALE:
1137 currentConfig.scale = currentConfig.hscale40 = currentConfig.hscale32 = 1.0;
1138 menu_opt3_preview(is_32col);
1140 case MA_OPT3_PRES_FULLSCR:
1141 currentConfig.scale = 1.20;
1142 currentConfig.hscale40 = 1.25;
1143 currentConfig.hscale32 = 1.56;
1144 menu_opt3_preview(is_32col);
1146 case MA_OPT3_FILTERING:
1147 menu_opt3_preview(is_32col);
1152 if (inp & BTN_X) return;
1157 // --------- advanced options ----------
1159 menu_entry opt2_entries[] =
1161 { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, ¤tConfig.PicoOpt,0x00004, 0, 0, 1 },
1162 { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, ¤tConfig.PicoOpt,0x00001, 0, 0, 1 },
1163 { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496,¤tConfig.PicoOpt,0x00002, 0, 0, 1 },
1164 { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, ¤tConfig.EmuOpt, 0x00008, 0, 0, 1 },
1165 { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, ¤tConfig.EmuOpt, 0x00020, 0, 0, 1 },
1166 { "Status line in main menu", MB_ONOFF, MA_OPT2_STATUS_LINE, ¤tConfig.EmuOpt, 0x20000, 0, 0, 1 },
1167 { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1 },
1170 #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
1173 static void draw_amenu_options(int menu_sel)
1175 int tl_x = 80+25, tl_y = 16+50;
1179 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
1181 me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1186 static void amenu_loop_options(void)
1188 static int menu_sel = 0;
1190 unsigned long inp = 0;
1191 menu_id selected_id;
1193 menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
1197 draw_amenu_options(menu_sel);
1198 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
1199 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1200 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1201 selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
1202 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1203 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
1204 selected_id == MA_OPT2_GAMMA) {
1208 if (inp & BTN_CIRCLE) { // toggleable options
1209 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
1210 selected_id == MA_OPT2_DONE) {
1214 if (inp & BTN_X) return;
1218 // -------------- options --------------
1221 menu_entry opt_entries[] =
1223 { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1 },
1224 { "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, ¤tConfig.PicoOpt, 0x0040, 0, 0, 1 },
1225 { "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, ¤tConfig.PicoOpt, 0x0080, 0, 0, 1 },
1226 { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, ¤tConfig.EmuOpt, 0x0002, 0, 0, 1 },
1227 { NULL, MB_RANGE, MA_OPT_FRAMESKIP, ¤tConfig.Frameskip, 0, -1, 16, 1 },
1228 { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, ¤tConfig.EmuOpt, 0x0004, 0, 0, 1 },
1229 { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1 },
1230 { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, ¤tConfig.PicoOpt, 0x0020, 0, 0, 1 },
1231 { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1 },
1232 { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, ¤tConfig.EmuOpt, 0x0001, 0, 0, 1 },
1233 { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1 },
1234 { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1 },
1235 { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1 },
1236 { "[Display options]", MB_NONE, MA_OPT_DISP_OPTS, NULL, 0, 0, 0, 1 },
1237 { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1 },
1238 { "[Advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1 },
1239 { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1 },
1240 { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1 },
1241 { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1 },
1244 #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
1247 static const char *region_name(unsigned int code)
1249 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };
1250 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };
1254 while((code >>= 1)) i++;
1255 if (i > 4) return "unknown";
1258 static char name[24];
1259 strcpy(name, "Auto:");
1260 for (u = 0; u < 3; u++) {
1261 i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;
1262 while((code >>= 1)) i++;
1263 strcat(name, names_short[i]);
1270 static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
1272 char *str, str24[24];
1276 case MA_OPT_RENDERER:
1277 if (currentConfig.PicoOpt&0x10)
1279 else if (currentConfig.EmuOpt&0x80)
1282 str = " 8bit accurate"; // n/a
1283 text_out16(x, y, "Renderer: %s", str);
1285 case MA_OPT_FRAMESKIP:
1286 if (currentConfig.Frameskip < 0)
1287 strcpy(str24, "Auto");
1288 else sprintf(str24, "%i", currentConfig.Frameskip);
1289 text_out16(x, y, "Frameskip %s", str24);
1291 case MA_OPT_SOUND_QUALITY:
1292 str = (currentConfig.PicoOpt&0x08)?"stereo":"mono";
1293 text_out16(x, y, "Sound Quality: %5iHz %s", currentConfig.PsndRate, str);
1296 text_out16(x, y, "Region: %s", region_name(currentConfig.PicoRegion));
1298 case MA_OPT_CONFIRM_STATES:
1299 switch ((currentConfig.EmuOpt >> 9) & 5) {
1300 default: str = "OFF"; break;
1301 case 1: str = "writes"; break;
1302 case 4: str = "loads"; break;
1303 case 5: str = "both"; break;
1305 text_out16(x, y, "Confirm savestate %s", str);
1307 case MA_OPT_CPU_CLOCKS:
1308 text_out16(x, y, "CPU/bus clock %3i/%3iMHz", currentConfig.CPUclock, currentConfig.CPUclock/2);
1310 case MA_OPT_SAVECFG:
1312 if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
1313 text_out16(x, y, "Save cfg as default%s", str24);
1315 case MA_OPT_LOADCFG:
1316 text_out16(x, y, "Load cfg from profile %i", config_slot);
1319 lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
1325 static void draw_menu_options(int menu_sel)
1327 int tl_x = 80+25, tl_y = 16+24;
1331 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
1333 me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
1338 static int sndrate_prevnext(int rate, int dir)
1340 int i, rates[] = { 11025, 22050, 44100 };
1342 for (i = 0; i < 5; i++)
1343 if (rates[i] == rate) break;
1346 if (i > 2) return dir ? 44100 : 22050;
1347 if (i < 0) return dir ? 22050 : 11025;
1351 static void region_prevnext(int right)
1353 // jp_ntsc=1, jp_pal=2, usa=4, eu=8
1354 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
1357 if (!currentConfig.PicoRegion) {
1358 for (i = 0; i < 6; i++)
1359 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1360 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
1361 else currentConfig.PicoRegion=1;
1363 else currentConfig.PicoRegion<<=1;
1364 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;
1366 if (!currentConfig.PicoRegion) {
1367 for (i = 0; i < 6; i++)
1368 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1369 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
1371 else currentConfig.PicoRegion>>=1;
1375 static void menu_options_save(void)
1377 PicoOpt = currentConfig.PicoOpt;
1378 PsndRate = currentConfig.PsndRate;
1379 PicoRegionOverride = currentConfig.PicoRegion;
1380 if (!(PicoOpt & 0x20)) {
1381 // unbind XYZ MODE, just in case
1382 unbind_action(0xf00);
1386 static int menu_loop_options(void)
1388 static int menu_sel = 0;
1389 int menu_sel_max, ret;
1390 unsigned long inp = 0;
1391 menu_id selected_id;
1393 currentConfig.PicoOpt = PicoOpt;
1394 currentConfig.PsndRate = PsndRate;
1395 currentConfig.PicoRegion = PicoRegionOverride;
1397 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);
1398 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1399 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1400 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1404 draw_menu_options(menu_sel);
1405 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
1406 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1407 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1408 selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
1409 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1410 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0)) {
1411 switch (selected_id) {
1412 case MA_OPT_RENDERER:
1413 if ((currentConfig.PicoOpt&0x10) || !(currentConfig.EmuOpt &0x80)) {
1414 currentConfig.PicoOpt&= ~0x10;
1415 currentConfig.EmuOpt |= 0x80;
1417 currentConfig.PicoOpt|= 0x10;
1418 currentConfig.EmuOpt &= ~0x80;
1421 case MA_OPT_SOUND_QUALITY:
1422 currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & BTN_RIGHT);
1425 region_prevnext(inp & BTN_RIGHT);
1427 case MA_OPT_CONFIRM_STATES: {
1428 int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
1429 n += (inp & BTN_LEFT) ? -1 : 1;
1430 if (n < 0) n = 0; else if (n > 3) n = 3;
1431 n |= n << 1; n &= ~2;
1432 currentConfig.EmuOpt &= ~0xa00;
1433 currentConfig.EmuOpt |= n << 9;
1436 case MA_OPT_SAVE_SLOT:
1437 if (inp & BTN_RIGHT) {
1438 state_slot++; if (state_slot > 9) state_slot = 0;
1439 } else {state_slot--; if (state_slot < 0) state_slot = 9;
1442 case MA_OPT_CPU_CLOCKS:
1443 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1444 currentConfig.CPUclock += (inp & BTN_LEFT) ? -1 : 1;
1445 if (currentConfig.CPUclock < 19) currentConfig.CPUclock = 19;
1446 if (currentConfig.CPUclock > 333) currentConfig.CPUclock = 333;
1447 draw_menu_options(menu_sel); // will wait vsync
1450 case MA_OPT_SAVECFG:
1451 case MA_OPT_SAVECFG_GAME:
1452 case MA_OPT_LOADCFG:
1453 config_slot += (inp&BTN_RIGHT) ? 1 : -1;
1454 if (config_slot > 9) config_slot = 0;
1455 if (config_slot < 0) config_slot = 9;
1456 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1457 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1458 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1461 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1466 if (inp & BTN_CIRCLE) {
1467 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
1469 switch (selected_id)
1471 case MA_OPT_DISP_OPTS:
1472 dispmenu_loop_options();
1474 case MA_OPT_SCD_OPTS:
1475 cd_menu_loop_options();
1476 if (engineState == PGS_ReloadRom)
1477 return 0; // test BIOS
1479 case MA_OPT_ADV_OPTS:
1480 amenu_loop_options();
1482 case MA_OPT_SAVECFG: // done (update and write)
1483 menu_options_save();
1484 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
1485 else strcpy(menuErrorMsg, "failed to write config");
1487 case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
1488 menu_options_save();
1489 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
1490 else strcpy(menuErrorMsg, "failed to write config");
1492 case MA_OPT_LOADCFG:
1493 ret = emu_ReadConfig(1, 1);
1494 if (!ret) ret = emu_ReadConfig(0, 1);
1495 if (ret) strcpy(menuErrorMsg, "config loaded");
1496 else strcpy(menuErrorMsg, "failed to load config");
1499 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1505 menu_options_save();
1506 return 0; // done (update, no write)
1511 // -------------- credits --------------
1513 static void draw_menu_credits(void)
1515 int tl_x = 80+15, tl_y = 16+64, y;
1518 text_out16(tl_x, 16+20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");
1521 text_out16(tl_x, y, "Credits:");
1522 text_out16(tl_x, (y+=10), "fDave: base code of PicoDrive");
1523 text_out16(tl_x, (y+=10), "Chui: Fame/C");
1524 text_out16(tl_x, (y+=10), "NJ: CZ80");
1525 text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
1526 text_out16(tl_x, (y+=10), "Stephane Dallongeville:");
1527 text_out16(tl_x, (y+=10), " Gens code, base of Fame/C, CZ80");
1528 text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");
1529 text_out16(tl_x, (y+=10), "Haze: Genesis hw info");
1530 text_out16(tl_x, (y+=10), "ps2dev.org people: PSP SDK/code");
1531 text_out16(tl_x, (y+=10), "ketchupgun: skin design");
1537 // -------------- root menu --------------
1539 menu_entry main_entries[] =
1541 { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
1542 { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
1543 { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
1544 { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
1545 { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
1546 { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
1547 { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
1548 { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
1549 { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
1550 { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
1553 #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
1555 static void draw_menu_root(int menu_sel)
1557 const int tl_x = 86+70, tl_y = 16+70;
1562 if ((currentConfig.EmuOpt&0x20000) && (stat = psp_get_status_line()))
1563 text_out16(287, 12, "%s", stat);
1565 text_out16(tl_x, 48, "PicoDrive v" VERSION);
1567 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
1569 me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1572 if (menuErrorMsg[0])
1573 text_out16(10, 252, menuErrorMsg);
1578 static void menu_loop_root(void)
1580 static int menu_sel = 0;
1581 int ret, menu_sel_max;
1582 unsigned long inp = 0;
1584 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_data != NULL);
1585 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_data != NULL);
1586 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_data != NULL);
1587 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_data != NULL);
1588 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
1590 menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
1591 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1594 if (mp3_last_error != 0) {
1595 if (mp3_last_error == -1)
1596 sprintf(menuErrorMsg, "Unsupported mp3 format, use 44kHz stereo");
1597 else sprintf(menuErrorMsg, "mp3 init failed, code %08x", mp3_last_error);
1601 /* make sure action buttons are not pressed on entering menu */
1602 draw_menu_root(menu_sel);
1604 while (psp_pad_read(1) & (BTN_X|BTN_CIRCLE|BTN_SELECT)) psp_msleep(50);
1608 draw_menu_root(menu_sel);
1609 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE|BTN_SELECT|BTN_L|BTN_R, 0);
1610 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1611 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1612 if((inp & (BTN_L|BTN_R)) == (BTN_L|BTN_R)) debug_menu_loop();
1613 if( inp & (BTN_SELECT|BTN_X)) {
1615 while (psp_pad_read(1) & (BTN_SELECT|BTN_X)) psp_msleep(50); // wait until released
1616 engineState = PGS_Running;
1620 if(inp & BTN_CIRCLE) {
1621 menuErrorMsg[0] = 0; // clear error msg
1622 switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
1624 case MA_MAIN_RESUME_GAME:
1626 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
1627 engineState = PGS_Running;
1631 case MA_MAIN_SAVE_STATE:
1633 if(savestate_menu_loop(0))
1635 engineState = PGS_Running;
1639 case MA_MAIN_LOAD_STATE:
1641 if(savestate_menu_loop(1))
1643 engineState = PGS_Running;
1647 case MA_MAIN_RESET_GAME:
1650 engineState = PGS_Running;
1654 case MA_MAIN_LOAD_ROM:
1656 char curr_path[PATH_MAX], *selfname;
1658 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1661 strcpy(curr_path, currentConfig.lastRomFile);
1664 getcwd(curr_path, PATH_MAX);
1665 selfname = romsel_loop(curr_path);
1667 lprintf("selected file: %s\n", selfname);
1668 engineState = PGS_ReloadRom;
1673 case MA_MAIN_OPTIONS:
1674 ret = menu_loop_options();
1675 if (ret == 1) continue; // status update
1676 if (engineState == PGS_ReloadRom)
1677 return; // BIOS test
1679 case MA_MAIN_CONTROLS:
1682 case MA_MAIN_CREDITS:
1683 draw_menu_credits();
1686 while (!(inp & (BTN_X|BTN_CIRCLE)))
1687 inp = wait_for_input(BTN_X|BTN_CIRCLE, 0);
1690 engineState = PGS_Quit;
1692 case MA_MAIN_PATCHES:
1693 if (rom_data && PicoPatches) {
1694 patches_menu_loop();
1696 strcpy(menuErrorMsg, "Patches applied");
1701 lprintf("%s: something unknown selected\n", __FUNCTION__);
1708 // warning: alignment
1709 static void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
1711 unsigned int *dest = dst;
1712 const unsigned int *srce = src;
1718 unsigned int p = *srce++;
1719 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
1726 unsigned int p = *srce++;
1727 *dest++ = (p&0xf79ef79e)>>1;
1732 static void menu_prepare_bg(int use_game_bg, int use_fg)
1736 // darken the active framebuffer
1737 unsigned short *dst = bg_buffer;
1738 unsigned short *src = use_fg ? psp_video_get_active_fb() : psp_screen;
1740 for (i = 272; i > 0; i--, dst += 480, src += 512)
1741 menu_darken_bg(dst, src, 480, 1);
1742 //memset32_uncached((int *)(bg_buffer + 480*264), 0, 480*8*2/4);
1746 // should really only happen once, on startup..
1747 memset32_uncached((int *)(void *)bg_buffer, 0, sizeof(bg_buffer)/4);
1748 readpng(bg_buffer, "skin/background.png", READPNG_BG);
1750 sceKernelDcacheWritebackAll();
1753 static void menu_gfx_prepare(void)
1755 menu_prepare_bg(rom_data != NULL, 1);
1762 void menu_loop(void)
1768 menuErrorMsg[0] = 0;
1772 // --------- CD tray close menu ----------
1774 static void draw_menu_tray(int menu_sel)
1776 int tl_x = 70, tl_y = 90, y;
1780 text_out16(tl_x, 20, "The unit is about to");
1781 text_out16(tl_x, 30, "close the CD tray.");
1784 text_out16(tl_x, y, "Load new CD image");
1785 text_out16(tl_x, (y+=10), "Insert nothing");
1788 text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
1790 if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
1795 int menu_loop_tray(void)
1797 int menu_sel = 0, menu_sel_max = 1;
1798 unsigned long inp = 0;
1799 char curr_path[PATH_MAX], *selfname;
1804 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1807 strcpy(curr_path, currentConfig.lastRomFile);
1811 getcwd(curr_path, PATH_MAX);
1814 /* make sure action buttons are not pressed on entering menu */
1815 draw_menu_tray(menu_sel);
1816 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
1820 draw_menu_tray(menu_sel);
1821 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_CIRCLE, 0);
1822 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1823 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1824 if(inp & BTN_CIRCLE) {
1826 case 0: // select image
1827 selfname = romsel_loop(curr_path);
1829 int ret = -1, cd_type;
1830 cd_type = emu_cdCheck(NULL);
1832 ret = Insert_CD(romFileName, cd_type == 2);
1834 sprintf(menuErrorMsg, "Load failed, invalid CD image?");
1835 lprintf("%s\n", menuErrorMsg);
1838 engineState = PGS_RestartRun;
1842 case 1: // insert nothing
1843 engineState = PGS_RestartRun;
1847 menuErrorMsg[0] = 0; // clear error msg