1 // (c) Copyright 2006,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>
23 #include "../common/menu.h"
24 #include "../common/emu.h"
25 #include "../common/readpng.h"
26 #include "../common/lprintf.h"
29 #include <Pico/PicoInt.h>
30 #include <Pico/Patch.h>
31 #include <zlib/zlib.h>
34 #define pspKeyUnkn "???"
35 static const char * const pspKeyNames[] = {
36 "SELECT", pspKeyUnkn, pspKeyUnkn, "START", "UP", "RIGHT", "DOWN", "LEFT",
37 "L", "R", pspKeyUnkn, pspKeyUnkn, "TRIANGLE", "CIRCLE", "X", "SQUARE",
38 "HOME", "HOLD", "WLAN_UP", "REMOTE", "VOLUP", "VOLDOWN", "SCREEN", "NOTE",
39 pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn, pspKeyUnkn
42 static unsigned short bg_buffer[480*272] __attribute__((aligned(16)));
43 #define menu_screen psp_screen
45 static void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
46 static void menu_prepare_bg(int use_game_bg, int use_fg);
49 static unsigned int inp_prev = 0;
51 static unsigned long wait_for_input(unsigned int interesting)
54 static int repeats = 0, wait = 50;
57 if (repeats == 2 || repeats == 4) wait /= 2;
58 if (repeats == 6) wait = 15;
60 for (i = 0; i < 6 && inp_prev == psp_pad_read(1); i++) {
61 if (i == 0) repeats++;
65 while ( !((ret = psp_pad_read(1)) & interesting) ) {
70 if (release || ret != inp_prev) {
76 // we don't need diagonals in menus
77 if ((ret&BTN_UP) && (ret&BTN_LEFT)) ret &= ~BTN_LEFT;
78 if ((ret&BTN_UP) && (ret&BTN_RIGHT)) ret &= ~BTN_RIGHT;
79 if ((ret&BTN_DOWN) && (ret&BTN_LEFT)) ret &= ~BTN_LEFT;
80 if ((ret&BTN_DOWN) && (ret&BTN_RIGHT)) ret &= ~BTN_RIGHT;
85 static void menu_draw_begin(void)
87 // short *src = (short *)bg_buffer, *dst = (short *)menu_screen;
90 // for (i = 272; i >= 0; i--, dst += 512, src += 480)
91 // memcpy32((int *)dst, (int *)src, 480*2/4);
93 sceGuSync(0,0); // sync with prev
94 sceGuStart(GU_DIRECT, guCmdList);
95 sceGuCopyImage(GU_PSM_5650, 0, 0, 480, 272, 480, bg_buffer, 0, 0, 512, menu_screen);
101 static void menu_draw_end(void)
107 // --------- loading ROM screen ----------
109 static void load_progress_cb(int percent)
111 int ln, len = percent * 480 / 100;
114 sceDisplayWaitVblankStart();
116 dst = (unsigned short *)menu_screen + 512*20;
118 if (len > 480) len = 480;
119 for (ln = 10; ln > 0; ln--, dst += 512)
120 memset(dst, 0xff, len*2);
123 void menu_romload_prepare(const char *rom_name)
125 const char *p = rom_name + strlen(rom_name);
126 while (p > rom_name && *p != '/') p--;
128 psp_video_switch_to_single();
131 smalltext_out16(1, 1, "Loading", 0xffff);
132 smalltext_out16_lim(1, 10, p, 0xffff, 80);
133 PicoCartLoadProgressCB = load_progress_cb;
136 void menu_romload_end(void)
138 PicoCartLoadProgressCB = NULL;
139 smalltext_out16(1, 30, "Starting emulation...", 0xffff);
142 // -------------- ROM selector --------------
145 #define DT_DIR FIO_SO_IFDIR
146 #define DT_REG FIO_SO_IFREG
150 unsigned char d_type;
154 // bbbb bggg gggr rrrr
155 static unsigned short file2color(const char *fname)
157 const char *ext = fname + strlen(fname) - 3;
158 static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso" };
159 static const char *other_exts[] = { "gmv", "pat" };
162 if (ext < fname) ext = fname;
163 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
164 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xfdf7;
165 for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
166 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
170 static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
175 n--; // exclude current dir (".")
179 if (rom_data == NULL) {
180 // menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
183 menu_darken_bg((char *)menu_screen + 512*129*2, (char *)menu_screen + 512*129*2, 512*10, 0);
186 smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
187 for (i = 0; i < n; i++) {
189 if (pos < 0) continue;
191 if (namelist[i+1]->d_type & DT_DIR) {
192 smalltext_out16_lim(14, pos*10, "/", 0xd7ff, 1);
193 smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xd7ff, 80-3);
195 unsigned short color = file2color(namelist[i+1]->d_name);
196 smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 80-2);
199 text_out16(5, 130, ">");
203 static int scandir_cmp(const void *p1, const void *p2)
205 struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;
206 if ((*d1)->d_type == (*d2)->d_type) return strcasecmp((*d1)->d_name, (*d2)->d_name);
207 if ((*d1)->d_type & DT_DIR) return -1; // put before
208 if ((*d2)->d_type & DT_DIR) return 1;
209 return strcasecmp((*d1)->d_name, (*d2)->d_name);
212 static char *filter_exts[] = {
213 ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
214 ".jpg", ".cue", ".pbp"
217 static int scandir_filter(const struct my_dirent *ent)
222 if (ent == NULL || ent->d_name == NULL) return 0;
223 if (strlen(ent->d_name) < 5) return 1;
225 p = ent->d_name + strlen(ent->d_name) - 4;
227 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
229 if (strcasecmp(p, filter_exts[i]) == 0) return 0;
235 static int my_scandir(const char *dir, struct my_dirent ***namelist_out,
236 int(*filter)(const struct my_dirent *),
237 int(*compar)(const void *, const void *))
239 int ret = -1, dir_uid = -1, name_alloc = 4, name_count = 0;
240 struct my_dirent **namelist = NULL, *ent;
243 namelist = malloc(sizeof(*namelist) * name_alloc);
244 if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
246 // try to read first..
247 dir_uid = sceIoDopen(dir);
250 /* it is very important to clear SceIoDirent to be passed to sceIoDread(), */
251 /* or else it may crash, probably misinterpreting something in it. */
252 memset(&sce_ent, 0, sizeof(sce_ent));
253 ret = sceIoDread(dir_uid, &sce_ent);
256 lprintf("sceIoDread(\"%s\") failed with %i\n", dir, ret);
261 lprintf("sceIoDopen(\"%s\") failed with %i\n", dir, dir_uid);
265 ent = malloc(sizeof(*ent));
266 if (ent == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
267 ent->d_type = sce_ent.d_stat.st_attr;
268 strncpy(ent->d_name, sce_ent.d_name, sizeof(ent->d_name));
269 ent->d_name[sizeof(ent->d_name)-1] = 0;
270 if (filter == NULL || filter(ent))
271 namelist[name_count++] = ent;
274 if (name_count >= name_alloc)
278 tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
279 if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
283 memset(&sce_ent, 0, sizeof(sce_ent));
284 ret = sceIoDread(dir_uid, &sce_ent);
288 if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
292 *namelist_out = namelist;
296 if (namelist != NULL)
299 free(namelist[name_count]);
303 if (dir_uid >= 0) sceIoDclose(dir_uid);
308 static char *romsel_loop(char *curr_path)
310 struct my_dirent **namelist;
311 int n, iret, sel = 0;
312 unsigned long inp = 0;
313 char *ret = NULL, *fname = NULL;
316 // is this a dir or a full path?
317 memset(&cpstat, 0, sizeof(cpstat));
318 iret = sceIoGetstat(curr_path, &cpstat);
319 if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFREG)) { // file
321 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
325 else if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFDIR)); // dir
326 else strcpy(curr_path, "ms0:/"); // something else
328 n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
331 n = my_scandir("ms0:/", &namelist, scandir_filter, scandir_cmp);
334 lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
342 for (i = 1; i < n; i++) {
343 if (strcmp(namelist[i]->d_name, fname) == 0) {
352 draw_dirlist(curr_path, namelist, n, sel);
353 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE);
354 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = n-2; }
355 if(inp & BTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
356 if(inp & BTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
357 if(inp & BTN_L) { sel-=24; if (sel < 0) sel = 0; }
358 if(inp & BTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
359 if(inp & BTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
360 if(inp & BTN_X) { // enter dir/select
361 if (namelist[sel+1]->d_type & DT_REG) {
362 strcpy(romFileName, curr_path);
363 strcat(romFileName, "/");
364 strcat(romFileName, namelist[sel+1]->d_name);
367 } else if (namelist[sel+1]->d_type & DT_DIR) {
368 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
369 char *p, *newdir = malloc(newlen);
370 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
371 char *start = curr_path;
372 p = start + strlen(start) - 1;
373 while (*p == '/' && p > start) p--;
374 while (*p != '/' && *p != ':' && p > start) p--;
375 if (p <= start || *p == ':' || p[-1] == ':') strcpy(newdir, "ms0:/");
376 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
378 strcpy(newdir, curr_path);
379 p = newdir + strlen(newdir) - 1;
380 while (*p == '/' && p >= newdir) *p-- = 0;
382 strcat(newdir, namelist[sel+1]->d_name);
384 ret = romsel_loop(newdir);
389 if(inp & BTN_CIRCLE) break; // cancel
393 while(n--) free(namelist[n]);
400 // ------------ debug menu ------------
402 char *debugString(void);
404 static void draw_debug(void)
406 char *p, *str = debugString();
412 for (line = 0; line < 24; line++)
414 while (*p && *p != '\n') p++;
416 if (len > 55) len = 55;
417 smalltext_out16_lim(1, line*10, str, 0xffff, len);
424 static void debug_menu_loop(void)
427 wait_for_input(BTN_X|BTN_CIRCLE);
430 // ------------ patch/gg menu ------------
432 static void draw_patchlist(int sel)
434 int start, i, pos, active;
440 for (i = 0; i < PicoPatchCount; i++) {
442 if (pos < 0) continue;
444 active = PicoPatches[i].active;
445 smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
446 smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
449 if (pos < 27) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
451 text_out16(5, 130, ">");
456 static void patches_menu_loop(void)
459 unsigned long inp = 0;
463 draw_patchlist(menu_sel);
464 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE);
465 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
466 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
467 if(inp &(BTN_LEFT|BTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
468 if(inp &(BTN_RIGHT|BTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
469 if(inp & BTN_X) { // action
470 if (menu_sel < PicoPatchCount)
471 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
474 if(inp & BTN_CIRCLE) return;
479 // ------------ savestate loader ------------
481 static int state_slot_flags = 0;
483 static void state_check_slots(void)
487 state_slot_flags = 0;
489 for (slot = 0; slot < 10; slot++)
491 if (emu_checkSaveFile(slot))
493 state_slot_flags |= 1 << slot;
498 static void *get_oldstate_for_preview(void)
500 unsigned char *ptr = malloc(sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram) + sizeof(Pico.video));
501 if (ptr == NULL) return NULL;
503 memcpy(ptr, Pico.vram, sizeof(Pico.vram));
504 memcpy(ptr + sizeof(Pico.vram), Pico.cram, sizeof(Pico.cram));
505 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram), Pico.vsram, sizeof(Pico.vsram));
506 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), &Pico.video, sizeof(Pico.video));
510 static void restore_oldstate(void *ptrx)
512 unsigned char *ptr = ptrx;
513 memcpy(Pico.vram, ptr, sizeof(Pico.vram));
514 memcpy(Pico.cram, ptr + sizeof(Pico.vram), sizeof(Pico.cram));
515 memcpy(Pico.vsram, ptr + sizeof(Pico.vram) + sizeof(Pico.cram), sizeof(Pico.vsram));
516 memcpy(&Pico.video,ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), sizeof(Pico.video));
520 static void draw_savestate_bg(int slot)
522 void *file, *oldstate;
525 fname = emu_GetSaveFName(1, 0, slot);
528 oldstate = get_oldstate_for_preview();
529 if (oldstate == NULL) return;
531 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
532 file = gzopen(fname, "rb");
533 emu_setSaveStateCbs(1);
535 file = fopen(fname, "rb");
536 emu_setSaveStateCbs(0);
541 PicoCdLoadStateGfx(file);
543 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
544 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
545 areaSeek(file, 0x2000, SEEK_CUR);
546 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
547 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
548 areaSeek(file, 0x221a0, SEEK_SET);
549 areaRead(&Pico.video, 1, sizeof(Pico.video), file);
555 menu_prepare_bg(1, 0);
557 restore_oldstate(oldstate);
560 static void draw_savestate_menu(int menu_sel, int is_loading)
562 int tl_x = 80+25, tl_y = 16+60, y, i;
564 if (state_slot_flags & (1 << menu_sel))
565 draw_savestate_bg(menu_sel);
568 text_out16(tl_x, 16+30, is_loading ? "Load state" : "Save state");
570 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
572 /* draw all 10 slots */
574 for (i = 0; i < 10; i++, y+=10)
576 text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
578 text_out16(tl_x, y, "back");
583 static int savestate_menu_loop(int is_loading)
585 static int menu_sel = 10;
586 int menu_sel_max = 10;
587 unsigned long inp = 0;
593 draw_savestate_menu(menu_sel, is_loading);
594 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE);
597 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
598 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
602 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
603 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
605 if(inp & BTN_X) { // save/load
607 state_slot = menu_sel;
608 PicoStateProgressCB = emu_msg_cb; /* also suitable for menu */
609 if (emu_SaveLoadGame(is_loading, 0)) {
610 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
616 if(inp & BTN_CIRCLE) return 1;
620 // -------------- key config --------------
622 static char *action_binds(int player_idx, int action_mask)
624 static char strkeys[32*5];
628 for (i = 0; i < 32; i++) // i is key index
630 if (currentConfig.KeyBinds[i] & action_mask)
632 if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
633 if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, pspKeyNames[i]); break; }
634 else strcpy(strkeys, pspKeyNames[i]);
641 static void unbind_action(int action)
645 for (i = 0; i < 32; i++)
646 currentConfig.KeyBinds[i] &= ~action;
649 static int count_bound_keys(int action, int pl_idx)
653 for (i = 0; i < 32; i++)
655 if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
656 if (currentConfig.KeyBinds[i] & action) keys++;
662 typedef struct { char *name; int mask; } bind_action_t;
664 static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)
666 int x, y, tl_y = 16+40, i;
669 if (player_idx >= 0) {
670 text_out16(80+80, 16+20, "Player %i controls", player_idx + 1);
673 text_out16(80+80, 16+20, "Emulator controls");
677 menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
680 for (i = 0; i < opt_cnt; i++, y+=10)
681 text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
683 text_out16(x, y, "Done");
686 text_out16(80+30, 180, "Press a button to bind/unbind");
687 text_out16(80+30, 190, "Use SELECT to clear");
688 text_out16(80+30, 200, "To bind UP/DOWN, hold SELECT");
689 text_out16(80+30, 210, "Select \"Done\" to exit");
691 text_out16(80+30, 190, "Use Options -> Save cfg");
692 text_out16(80+30, 200, "to save controls");
693 text_out16(80+30, 210, "Press X or O to exit");
698 static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)
700 int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
701 unsigned long inp = 0;
705 draw_key_config(opts, opt_cnt, player_idx, sel);
706 inp = wait_for_input(CONFIGURABLE_KEYS|BTN_SELECT);
707 if (!(inp & BTN_SELECT)) {
709 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
710 if(inp & BTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
712 if (sel >= opt_cnt) {
713 if (inp & (BTN_X|BTN_CIRCLE)) break;
716 // if we are here, we want to bind/unbind something
717 if ((inp & BTN_SELECT) && !prev_select)
718 unbind_action(opts[sel].mask);
719 prev_select = inp & BTN_SELECT;
720 inp &= CONFIGURABLE_KEYS;
722 for (i = 0; i < 32; i++)
723 if (inp & (1 << i)) {
724 if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
725 currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
726 else currentConfig.KeyBinds[i] ^= opts[sel].mask;
727 if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
728 currentConfig.KeyBinds[i] &= ~(3 << 16);
729 currentConfig.KeyBinds[i] |= player_idx << 16;
735 static void draw_kc_sel(int menu_sel)
737 int tl_x = 80+25+40, tl_y = 16+60, y;
741 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
743 text_out16(tl_x, y, "Player 1");
744 text_out16(tl_x, (y+=10), "Player 2");
745 text_out16(tl_x, (y+=10), "Emulator controls");
746 text_out16(tl_x, (y+=10), "Done");
752 // PicoPad[] format: MXYZ SACB RLDU
753 static bind_action_t ctrl_actions[] =
769 // player2_flag, ?, ?, ?, ?, ?, ?, menu
770 // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
771 // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
772 static bind_action_t emuctrl_actions[] =
774 { "Load State ", 1<<28 },
775 { "Save State ", 1<<27 },
776 { "Prev Save Slot ", 1<<25 },
777 { "Next Save Slot ", 1<<24 },
778 { "Switch Renderer", 1<<26 },
779 { "Volume Down ", 1<<30 },
780 { "Volume Up ", 1<<29 },
783 static void kc_sel_loop(void)
785 int menu_sel = 3, menu_sel_max = 3;
786 unsigned long inp = 0;
787 int is_6button = currentConfig.PicoOpt & 0x020;
791 draw_kc_sel(menu_sel);
792 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE);
793 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
794 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
797 case 0: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 0); return;
798 case 1: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 1); return;
799 case 2: key_config_loop(emuctrl_actions,
800 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;
801 case 3: if (rom_data == NULL) emu_WriteConfig(0); return;
805 if (inp & BTN_CIRCLE) return;
810 // --------- sega/mega cd options ----------
812 menu_entry cdopt_entries[] =
814 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1 },
815 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1 },
816 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1 },
817 { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, ¤tConfig.EmuOpt, 0x0400, 0, 0, 1 },
818 { "CDDA audio (using mp3s)", MB_ONOFF, MA_CDOPT_CDDA, ¤tConfig.PicoOpt, 0x0800, 0, 0, 1 },
819 { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, ¤tConfig.PicoOpt, 0x0400, 0, 0, 1 },
820 { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1 },
821 { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, ¤tConfig.PicoOpt, 0x8000, 0, 0, 1 },
822 { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,¤tConfig.PicoOpt, 0x1000, 0, 0, 1 },
823 { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, ¤tConfig.PicoOpt, 0x2000, 0, 0, 1 },
824 { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1 },
827 #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
832 char us[32], eu[32], jp[32];
835 static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
837 struct bios_names_t *bios_names = param;
842 case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
843 case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
844 case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
845 case MA_CDOPT_READAHEAD:
846 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
847 else strcpy(ra_buff, " OFF");
848 text_out16(x, y, "ReadAhead buffer %s", ra_buff);
854 static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
856 int tl_x = 80+25, tl_y = 16+60;
860 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
861 else strcpy(ra_buff, " OFF");
865 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
867 me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
869 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
870 if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
871 (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
872 (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
873 text_out16(tl_x, 210, "Press start to test selected BIOS");
878 static void cd_menu_loop_options(void)
880 static int menu_sel = 0;
881 int menu_sel_max = 10;
882 unsigned long inp = 0;
883 struct bios_names_t bios_names;
887 if (emu_findBios(4, &bios)) { // US
888 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;
889 strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
890 } else strcpy(bios_names.us, "NOT FOUND");
892 if (emu_findBios(8, &bios)) { // EU
893 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;
894 strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
895 } else strcpy(bios_names.eu, "NOT FOUND");
897 if (emu_findBios(1, &bios)) { // JP
898 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;
899 strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
900 } else strcpy(bios_names.jp, "NOT FOUND");
904 draw_cd_menu_options(menu_sel, &bios_names);
905 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE);
906 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
907 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
908 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
909 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
910 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
911 selected_id == MA_CDOPT_READAHEAD) {
912 if (inp & BTN_LEFT) {
914 if (PicoCDBuffers < 64) PicoCDBuffers = 0;
916 if (PicoCDBuffers < 64) PicoCDBuffers = 64;
917 else PicoCDBuffers <<= 1;
918 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
922 if (inp & BTN_X) { // toggleable options
923 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
924 selected_id == MA_CDOPT_DONE) {
927 switch (selected_id) { // BIOS testers
928 case MA_CDOPT_TESTBIOS_USA:
929 if (emu_findBios(4, &bios)) { // test US
930 strcpy(romFileName, bios);
931 engineState = PGS_ReloadRom;
935 case MA_CDOPT_TESTBIOS_EUR:
936 if (emu_findBios(8, &bios)) { // test EU
937 strcpy(romFileName, bios);
938 engineState = PGS_ReloadRom;
942 case MA_CDOPT_TESTBIOS_JAP:
943 if (emu_findBios(1, &bios)) { // test JP
944 strcpy(romFileName, bios);
945 engineState = PGS_ReloadRom;
953 if (inp & BTN_CIRCLE) return;
957 // --------- display options ----------
959 menu_entry opt3_entries[] =
961 { NULL, MB_NONE, MA_OPT3_SCALE, NULL, 0, 0, 0, 1 },
962 { NULL, MB_NONE, MA_OPT3_HSCALE32, NULL, 0, 0, 0, 1 },
963 { NULL, MB_NONE, MA_OPT3_HSCALE40, NULL, 0, 0, 0, 1 },
964 { NULL, MB_ONOFF, MA_OPT3_FILTERING, ¤tConfig.scaling, 1, 0, 0, 1 },
965 { NULL, MB_NONE, MA_OPT3_VSYNC, NULL, 0, 0, 0, 1 },
966 { "Set to unscaled centered", MB_NONE, MA_OPT3_PRES_NOSCALE, NULL, 0, 0, 0, 1 },
967 { "Set to fullscreen", MB_NONE, MA_OPT3_PRES_FULLSCR, NULL, 0, 0, 0, 1 },
968 { "done", MB_NONE, MA_OPT3_DONE, NULL, 0, 0, 0, 1 },
971 #define OPT3_ENTRY_COUNT (sizeof(opt3_entries) / sizeof(opt3_entries[0]))
974 static void menu_opt3_cust_draw(const menu_entry *entry, int x, int y, void *param)
979 text_out16(x, y, "Scale factor: %.2f", currentConfig.scale);
981 case MA_OPT3_HSCALE32:
982 text_out16(x, y, "Hor. scale (for low res. games): %.2f", currentConfig.hscale32);
984 case MA_OPT3_HSCALE40:
985 text_out16(x, y, "Hor. scale (for hi res. games): %.2f", currentConfig.hscale40);
987 case MA_OPT3_FILTERING:
988 text_out16(x, y, "Bilinear filtering %s", currentConfig.scaling?"ON":"OFF");
990 case MA_OPT3_VSYNC: {
991 char *val = " never";
992 if (currentConfig.EmuOpt & 0x2000)
993 val = (currentConfig.EmuOpt & 0x10000) ? "sometimes" : " always";
994 text_out16(x, y, "Wait for vsync (slow) %s", val);
1001 static void menu_opt3_preview(int is_32col)
1003 void *oldstate = NULL;
1005 if (rom_data == NULL || ((Pico.video.reg[12]&1)^1) != is_32col)
1007 extern char bgdatac32_start[], bgdatac40_start[];
1008 extern int bgdatac32_size, bgdatac40_size;
1009 void *bgdata = is_32col ? bgdatac32_start : bgdatac40_start;
1010 unsigned long insize = is_32col ? bgdatac32_size : bgdatac40_size, outsize = 65856;
1012 ret = uncompress((Bytef *)bg_buffer, &outsize, bgdata, insize);
1015 if (rom_data != NULL) oldstate = get_oldstate_for_preview();
1016 memcpy(Pico.vram, bg_buffer, sizeof(Pico.vram));
1017 memcpy(Pico.cram, (char *)bg_buffer + 0x10000, 0x40*2);
1018 memcpy(Pico.vsram, (char *)bg_buffer + 0x10080, 0x40*2);
1019 memcpy(&Pico.video,(char *)bg_buffer + 0x10100, 0x40);
1022 lprintf("uncompress returned %i\n", ret);
1025 memset32(psp_screen, 0, 512*272*2/4);
1027 menu_prepare_bg(1, 0);
1029 if (oldstate) restore_oldstate(oldstate);
1032 static void draw_dispmenu_options(int menu_sel)
1034 int tl_x = 80, tl_y = 16+50;
1038 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 316);
1040 me_draw(opt3_entries, OPT3_ENTRY_COUNT, tl_x, tl_y, menu_opt3_cust_draw, NULL);
1045 static void dispmenu_loop_options(void)
1047 static int menu_sel = 0;
1048 int menu_sel_max, is_32col = (Pico.video.reg[12]&1)^1;
1049 unsigned long inp = 0;
1050 menu_id selected_id;
1052 menu_sel_max = me_count_enabled(opt3_entries, OPT3_ENTRY_COUNT) - 1;
1056 draw_dispmenu_options(menu_sel);
1057 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE);
1058 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1059 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1060 selected_id = me_index2id(opt3_entries, OPT3_ENTRY_COUNT, menu_sel);
1061 if (selected_id == MA_OPT3_HSCALE40 && is_32col) { is_32col = 0; menu_opt3_preview(is_32col); }
1062 if (selected_id == MA_OPT3_HSCALE32 && !is_32col) { is_32col = 1; menu_opt3_preview(is_32col); }
1064 if (inp & (BTN_LEFT|BTN_RIGHT)) // multi choise
1066 float *setting = NULL;
1068 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0);
1069 switch (selected_id) {
1070 case MA_OPT3_SCALE: setting = ¤tConfig.scale; break;
1071 case MA_OPT3_HSCALE40: setting = ¤tConfig.hscale40; is_32col = 0; break;
1072 case MA_OPT3_HSCALE32: setting = ¤tConfig.hscale32; is_32col = 1; break;
1073 case MA_OPT3_FILTERING:menu_opt3_preview(is_32col); break;
1074 case MA_OPT3_VSYNC: tmp = ((currentConfig.EmuOpt>>13)&1) | ((currentConfig.EmuOpt>>15)&2);
1075 tmp = (inp & BTN_LEFT) ? (tmp>>1) : ((tmp<<1)|1);
1076 if (tmp > 3) tmp = 3;
1077 currentConfig.EmuOpt &= ~0x12000;
1078 currentConfig.EmuOpt |= ((tmp&2)<<15) | ((tmp&1)<<13);
1082 if (setting != NULL) {
1083 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1084 *setting += (inp & BTN_LEFT) ? -0.01 : 0.01;
1085 menu_opt3_preview(is_32col);
1086 draw_dispmenu_options(menu_sel); // will wait vsync
1090 if (inp & BTN_X) { // toggleable options
1091 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, 1);
1092 switch (selected_id) {
1095 case MA_OPT3_PRES_NOSCALE:
1096 currentConfig.scale = currentConfig.hscale40 = currentConfig.hscale32 = 1.0;
1097 menu_opt3_preview(is_32col);
1099 case MA_OPT3_PRES_FULLSCR:
1100 currentConfig.scale = 1.20;
1101 currentConfig.hscale40 = 1.25;
1102 currentConfig.hscale32 = 1.56;
1103 menu_opt3_preview(is_32col);
1105 case MA_OPT3_FILTERING:
1106 menu_opt3_preview(is_32col);
1111 if (inp & BTN_CIRCLE) return;
1116 // --------- advanced options ----------
1118 menu_entry opt2_entries[] =
1120 { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, ¤tConfig.PicoOpt,0x0004, 0, 0, 1 },
1121 { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, ¤tConfig.PicoOpt,0x0001, 0, 0, 1 },
1122 { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496,¤tConfig.PicoOpt,0x0002, 0, 0, 1 },
1123 { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, ¤tConfig.EmuOpt, 0x0008, 0, 0, 1 },
1124 { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, ¤tConfig.EmuOpt, 0x0020, 0, 0, 1 },
1125 { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1 },
1128 #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
1131 static void draw_amenu_options(int menu_sel)
1133 int tl_x = 80+25, tl_y = 16+50;
1137 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
1139 me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1144 static void amenu_loop_options(void)
1146 static int menu_sel = 0;
1148 unsigned long inp = 0;
1149 menu_id selected_id;
1151 menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
1155 draw_amenu_options(menu_sel);
1156 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE);
1157 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1158 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1159 selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
1160 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1161 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
1162 selected_id == MA_OPT2_GAMMA) {
1166 if (inp & BTN_X) { // toggleable options
1167 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
1168 selected_id == MA_OPT2_DONE) {
1172 if (inp & BTN_CIRCLE) return;
1176 // -------------- options --------------
1179 menu_entry opt_entries[] =
1181 { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1 },
1182 { "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, ¤tConfig.PicoOpt, 0x0040, 0, 0, 1 },
1183 { "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, ¤tConfig.PicoOpt, 0x0080, 0, 0, 1 },
1184 { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, ¤tConfig.EmuOpt, 0x0002, 0, 0, 1 },
1185 { NULL, MB_RANGE, MA_OPT_FRAMESKIP, ¤tConfig.Frameskip, 0, -1, 16, 1 },
1186 { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, ¤tConfig.EmuOpt, 0x0004, 0, 0, 1 },
1187 { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1 },
1188 { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, ¤tConfig.PicoOpt, 0x0020, 0, 0, 1 },
1189 { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1 },
1190 { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, ¤tConfig.EmuOpt, 0x0001, 0, 0, 1 },
1191 { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1 },
1192 { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1 },
1193 { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1 },
1194 { "[Display options]", MB_NONE, MA_OPT_DISP_OPTS, NULL, 0, 0, 0, 1 },
1195 { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1 },
1196 { "[Advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1 },
1197 { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1 },
1198 { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1 },
1199 { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1 },
1202 #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
1205 static const char *region_name(unsigned int code)
1207 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };
1208 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };
1212 while((code >>= 1)) i++;
1213 if (i > 4) return "unknown";
1216 static char name[24];
1217 strcpy(name, "Auto:");
1218 for (u = 0; u < 3; u++) {
1219 i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;
1220 while((code >>= 1)) i++;
1221 strcat(name, names_short[i]);
1228 static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
1230 char *str, str24[24];
1234 case MA_OPT_RENDERER:
1235 if (currentConfig.PicoOpt&0x10)
1237 else if (currentConfig.EmuOpt&0x80)
1240 str = " 8bit accurate"; // n/a
1241 text_out16(x, y, "Renderer: %s", str);
1243 case MA_OPT_FRAMESKIP:
1244 if (currentConfig.Frameskip < 0)
1245 strcpy(str24, "Auto");
1246 else sprintf(str24, "%i", currentConfig.Frameskip);
1247 text_out16(x, y, "Frameskip %s", str24);
1249 case MA_OPT_SOUND_QUALITY:
1250 str = (currentConfig.PicoOpt&0x08)?"stereo":"mono";
1251 text_out16(x, y, "Sound Quality: %5iHz %s", currentConfig.PsndRate, str);
1254 text_out16(x, y, "Region: %s", region_name(currentConfig.PicoRegion));
1256 case MA_OPT_CONFIRM_STATES:
1257 switch ((currentConfig.EmuOpt >> 9) & 5) {
1258 default: str = "OFF"; break;
1259 case 1: str = "writes"; break;
1260 case 4: str = "loads"; break;
1261 case 5: str = "both"; break;
1263 text_out16(x, y, "Confirm savestate %s", str);
1265 case MA_OPT_CPU_CLOCKS:
1266 text_out16(x, y, "CPU/bus clock %3i/%3iMHz", currentConfig.CPUclock, currentConfig.CPUclock/2);
1268 case MA_OPT_SAVECFG:
1270 if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
1271 text_out16(x, y, "Save cfg as default%s", str24);
1273 case MA_OPT_LOADCFG:
1274 text_out16(x, y, "Load cfg from profile %i", config_slot);
1277 lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
1283 static void draw_menu_options(int menu_sel)
1285 int tl_x = 80+25, tl_y = 16+24;
1289 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
1291 me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
1296 static int sndrate_prevnext(int rate, int dir)
1298 int i, rates[] = { 11025, 22050, 44100 };
1300 for (i = 0; i < 5; i++)
1301 if (rates[i] == rate) break;
1304 if (i > 2) return dir ? 44100 : 22050;
1305 if (i < 0) return dir ? 22050 : 11025;
1309 static void region_prevnext(int right)
1311 // jp_ntsc=1, jp_pal=2, usa=4, eu=8
1312 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
1315 if (!currentConfig.PicoRegion) {
1316 for (i = 0; i < 6; i++)
1317 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1318 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
1319 else currentConfig.PicoRegion=1;
1321 else currentConfig.PicoRegion<<=1;
1322 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;
1324 if (!currentConfig.PicoRegion) {
1325 for (i = 0; i < 6; i++)
1326 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1327 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
1329 else currentConfig.PicoRegion>>=1;
1333 static void menu_options_save(void)
1335 PicoOpt = currentConfig.PicoOpt;
1336 PsndRate = currentConfig.PsndRate;
1337 PicoRegionOverride = currentConfig.PicoRegion;
1338 if (!(PicoOpt & 0x20)) {
1339 // unbind XYZ MODE, just in case
1340 unbind_action(0xf00);
1344 static int menu_loop_options(void)
1346 static int menu_sel = 0;
1347 int menu_sel_max, ret;
1348 unsigned long inp = 0;
1349 menu_id selected_id;
1351 currentConfig.PicoOpt = PicoOpt;
1352 currentConfig.PsndRate = PsndRate;
1353 currentConfig.PicoRegion = PicoRegionOverride;
1355 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);
1356 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1357 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1358 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1362 draw_menu_options(menu_sel);
1363 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE);
1364 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1365 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1366 selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
1367 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1368 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0)) {
1369 switch (selected_id) {
1370 case MA_OPT_RENDERER:
1371 if ((currentConfig.PicoOpt&0x10) || !(currentConfig.EmuOpt &0x80)) {
1372 currentConfig.PicoOpt&= ~0x10;
1373 currentConfig.EmuOpt |= 0x80;
1375 currentConfig.PicoOpt|= 0x10;
1376 currentConfig.EmuOpt &= ~0x80;
1379 case MA_OPT_SOUND_QUALITY:
1380 currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & BTN_RIGHT);
1383 region_prevnext(inp & BTN_RIGHT);
1385 case MA_OPT_CONFIRM_STATES: {
1386 int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
1387 n += (inp & BTN_LEFT) ? -1 : 1;
1388 if (n < 0) n = 0; else if (n > 3) n = 3;
1389 n |= n << 1; n &= ~2;
1390 currentConfig.EmuOpt &= ~0xa00;
1391 currentConfig.EmuOpt |= n << 9;
1394 case MA_OPT_SAVE_SLOT:
1395 if (inp & BTN_RIGHT) {
1396 state_slot++; if (state_slot > 9) state_slot = 0;
1397 } else {state_slot--; if (state_slot < 0) state_slot = 9;
1400 case MA_OPT_CPU_CLOCKS:
1401 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1402 currentConfig.CPUclock += (inp & BTN_LEFT) ? -1 : 1;
1403 if (currentConfig.CPUclock < 19) currentConfig.CPUclock = 19;
1404 if (currentConfig.CPUclock > 333) currentConfig.CPUclock = 333;
1405 draw_menu_options(menu_sel); // will wait vsync
1408 case MA_OPT_SAVECFG:
1409 case MA_OPT_SAVECFG_GAME:
1410 case MA_OPT_LOADCFG:
1411 config_slot += (inp&BTN_RIGHT) ? 1 : -1;
1412 if (config_slot > 9) config_slot = 0;
1413 if (config_slot < 0) config_slot = 9;
1414 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1415 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1416 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1419 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1425 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
1427 switch (selected_id)
1429 case MA_OPT_DISP_OPTS:
1430 dispmenu_loop_options();
1432 case MA_OPT_SCD_OPTS:
1433 cd_menu_loop_options();
1434 if (engineState == PGS_ReloadRom)
1435 return 0; // test BIOS
1437 case MA_OPT_ADV_OPTS:
1438 amenu_loop_options();
1440 case MA_OPT_SAVECFG: // done (update and write)
1441 menu_options_save();
1442 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
1443 else strcpy(menuErrorMsg, "failed to write config");
1445 case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
1446 menu_options_save();
1447 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
1448 else strcpy(menuErrorMsg, "failed to write config");
1450 case MA_OPT_LOADCFG:
1451 ret = emu_ReadConfig(1, 1);
1452 if (!ret) ret = emu_ReadConfig(0, 1);
1453 if (ret) strcpy(menuErrorMsg, "config loaded");
1454 else strcpy(menuErrorMsg, "failed to load config");
1457 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1462 if(inp & BTN_CIRCLE) {
1463 menu_options_save();
1464 return 0; // done (update, no write)
1469 // -------------- credits --------------
1471 static void draw_menu_credits(void)
1473 int tl_x = 80+15, tl_y = 16+64, y;
1476 text_out16(tl_x, 16+20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");
1479 text_out16(tl_x, y, "Credits:");
1480 text_out16(tl_x, (y+=10), "fDave: base code of PicoDrive");
1481 text_out16(tl_x, (y+=10), "Chui: Fame/C");
1482 text_out16(tl_x, (y+=10), "NJ: CZ80");
1483 text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
1484 text_out16(tl_x, (y+=10), "Stephane Dallongeville:");
1485 text_out16(tl_x, (y+=10), " Gens code, base of Fame/C, CZ80");
1486 text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");
1487 text_out16(tl_x, (y+=10), "Haze: Genesis hw info");
1488 text_out16(tl_x, (y+=10), "ps2dev.org people: PSP SDK/code");
1489 text_out16(tl_x, (y+=10), "ketchupgun: skin design");
1495 // -------------- root menu --------------
1497 menu_entry main_entries[] =
1499 { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
1500 { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
1501 { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
1502 { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
1503 { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
1504 { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
1505 { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
1506 { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
1507 { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
1508 { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
1511 #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
1513 static void draw_menu_root(int menu_sel)
1515 const int tl_x = 80+70, tl_y = 16+70;
1519 text_out16(tl_x, 16+20, "PicoDrive v" VERSION);
1521 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
1523 me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1526 if (menuErrorMsg[0])
1527 text_out16(10, 252, menuErrorMsg);
1532 static void menu_loop_root(void)
1534 static int menu_sel = 0;
1535 int ret, menu_sel_max;
1536 unsigned long inp = 0;
1538 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_data != NULL);
1539 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_data != NULL);
1540 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_data != NULL);
1541 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_data != NULL);
1542 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
1544 menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
1545 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1547 /* make sure action buttons are not pressed on entering menu */
1548 draw_menu_root(menu_sel);
1550 while (psp_pad_read(1) & (BTN_X|BTN_CIRCLE|BTN_SELECT)) psp_msleep(50);
1554 draw_menu_root(menu_sel);
1555 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE|BTN_SELECT|BTN_L|BTN_R);
1556 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1557 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1558 if((inp & (BTN_L|BTN_R)) == (BTN_L|BTN_R)) debug_menu_loop();
1559 if( inp & (BTN_SELECT|BTN_CIRCLE)) {
1561 while (psp_pad_read(1) & (BTN_SELECT|BTN_CIRCLE)) psp_msleep(50); // wait until released
1562 engineState = PGS_Running;
1567 switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
1569 case MA_MAIN_RESUME_GAME:
1571 while (psp_pad_read(1) & BTN_X) psp_msleep(50);
1572 engineState = PGS_Running;
1576 case MA_MAIN_SAVE_STATE:
1578 if(savestate_menu_loop(0))
1580 engineState = PGS_Running;
1584 case MA_MAIN_LOAD_STATE:
1586 if(savestate_menu_loop(1))
1588 engineState = PGS_Running;
1592 case MA_MAIN_RESET_GAME:
1595 engineState = PGS_Running;
1599 case MA_MAIN_LOAD_ROM:
1601 char curr_path[PATH_MAX], *selfname;
1603 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1606 strcpy(curr_path, currentConfig.lastRomFile);
1609 getcwd(curr_path, PATH_MAX);
1610 selfname = romsel_loop(curr_path);
1612 lprintf("selected file: %s\n", selfname);
1613 engineState = PGS_ReloadRom;
1618 case MA_MAIN_OPTIONS:
1619 ret = menu_loop_options();
1620 if (ret == 1) continue; // status update
1621 if (engineState == PGS_ReloadRom)
1622 return; // BIOS test
1624 case MA_MAIN_CONTROLS:
1627 case MA_MAIN_CREDITS:
1628 draw_menu_credits();
1630 inp = wait_for_input(BTN_X|BTN_CIRCLE);
1633 engineState = PGS_Quit;
1635 case MA_MAIN_PATCHES:
1636 if (rom_data && PicoPatches) {
1637 patches_menu_loop();
1639 strcpy(menuErrorMsg, "Patches applied");
1644 lprintf("%s: something unknown selected\n", __FUNCTION__);
1648 menuErrorMsg[0] = 0; // clear error msg
1652 // warning: alignment
1653 static void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
1655 unsigned int *dest = dst;
1656 const unsigned int *srce = src;
1662 unsigned int p = *srce++;
1663 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
1670 unsigned int p = *srce++;
1671 *dest++ = (p&0xf79ef79e)>>1;
1676 static void menu_prepare_bg(int use_game_bg, int use_fg)
1680 // darken the active framebuffer
1681 unsigned short *dst = bg_buffer;
1682 unsigned short *src = use_fg ? psp_video_get_active_fb() : psp_screen;
1684 for (i = 272; i > 0; i--, dst += 480, src += 512)
1685 menu_darken_bg(dst, src, 480, 1);
1686 //memset32((int *)(bg_buffer + 480*264), 0, 480*8*2/4);
1690 // should really only happen once, on startup..
1691 memset32((int *)(void *)bg_buffer, 0, sizeof(bg_buffer)/4);
1692 readpng(bg_buffer, "skin/background.png", READPNG_BG);
1694 sceKernelDcacheWritebackAll();
1697 static void menu_gfx_prepare(void)
1699 menu_prepare_bg(rom_data != NULL, 1);
1706 void menu_loop(void)
1712 menuErrorMsg[0] = 0;
1716 // --------- CD tray close menu ----------
1718 static void draw_menu_tray(int menu_sel)
1720 int tl_x = 70, tl_y = 90, y;
1724 text_out16(tl_x, 20, "The unit is about to");
1725 text_out16(tl_x, 30, "close the CD tray.");
1728 text_out16(tl_x, y, "Load new CD image");
1729 text_out16(tl_x, (y+=10), "Insert nothing");
1732 text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
1734 if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
1739 int menu_loop_tray(void)
1741 int menu_sel = 0, menu_sel_max = 1;
1742 unsigned long inp = 0;
1743 char curr_path[PATH_MAX], *selfname;
1748 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1751 strcpy(curr_path, currentConfig.lastRomFile);
1755 getcwd(curr_path, PATH_MAX);
1758 /* make sure action buttons are not pressed on entering menu */
1759 draw_menu_tray(menu_sel);
1760 while (psp_pad_read(1) & BTN_X) psp_msleep(50);
1764 draw_menu_tray(menu_sel);
1765 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X);
1766 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1767 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1770 case 0: // select image
1771 selfname = romsel_loop(curr_path);
1773 int ret = -1, cd_type;
1774 cd_type = emu_cdCheck(NULL);
1776 ret = Insert_CD(romFileName, cd_type == 2);
1778 sprintf(menuErrorMsg, "Load failed, invalid CD image?");
1779 lprintf("%s\n", menuErrorMsg);
1782 engineState = PGS_RestartRun;
1786 case 1: // insert nothing
1787 engineState = PGS_RestartRun;
1791 menuErrorMsg[0] = 0; // clear error msg