optimizations, fixes, hacks, psp, ...
[libpicofe.git] / psp / menu.c
CommitLineData
2951214e 1// (c) Copyright 2006,2007 notaz, All rights reserved.
2// Free for non-commercial use.
3
4// For commercial use, separate licencing terms must be obtained.
5
6// don't like to use loads of #ifdefs, so duplicating GP2X code
7// horribly instead
8
2951214e 9#include <string.h>
10#include <stdlib.h>
11#include <wchar.h>
12#include <unistd.h>
2951214e 13#include <sys/syslimits.h> // PATH_MAX
14
15#include <pspdisplay.h>
16#include <pspgu.h>
17#include <pspiofilemgr.h>
6f748c47 18#include <psputils.h>
2951214e 19
20#include "psp.h"
21#include "emu.h"
22#include "menu.h"
a6df06b7 23#include "mp3.h"
2951214e 24#include "../common/menu.h"
25#include "../common/emu.h"
26#include "../common/readpng.h"
27#include "../common/lprintf.h"
28#include "version.h"
29
30#include <Pico/PicoInt.h>
31#include <Pico/Patch.h>
32#include <zlib/zlib.h>
33
34
35#define pspKeyUnkn "???"
36static const char * const pspKeyNames[] = {
16e89bed 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
2951214e 41};
42
6f748c47 43static unsigned short bg_buffer[480*272] __attribute__((aligned(16)));
2951214e 44#define menu_screen psp_screen
45
46static void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
a52e1f62 47static void menu_prepare_bg(int use_game_bg, int use_fg);
2951214e 48
49
50static unsigned int inp_prev = 0;
51
16e89bed 52static unsigned long wait_for_input(unsigned int interesting, int is_key_config)
2951214e 53{
54 unsigned int ret;
55 static int repeats = 0, wait = 50;
16e89bed 56 int release = 0, count, i;
57
58 if (!is_key_config)
59 interesting |= (interesting & 0xf0) << 24; // also use analog
2951214e 60
61 if (repeats == 2 || repeats == 4) wait /= 2;
62 if (repeats == 6) wait = 15;
63
703e4c7b 64 for (i = 0; i < 6 && inp_prev == psp_pad_read(1); i++) {
2951214e 65 if (i == 0) repeats++;
66 psp_msleep(wait);
67 }
68
16e89bed 69 for (count = 0; !((ret = psp_pad_read(1)) & interesting) && count < 100; count++) {
2951214e 70 psp_msleep(50);
71 release = 1;
72 }
73
74 if (release || ret != inp_prev) {
75 repeats = 0;
76 wait = 50;
77 }
78 inp_prev = ret;
79
16e89bed 80 if (!is_key_config)
81 ret |= (ret & 0xf0000000) >> 24; // use analog as d-pad
82
2951214e 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;
88
89 return ret;
90}
91
92static void menu_draw_begin(void)
93{
94 // short *src = (short *)bg_buffer, *dst = (short *)menu_screen;
95 // int i;
96
97 // for (i = 272; i >= 0; i--, dst += 512, src += 480)
98 // memcpy32((int *)dst, (int *)src, 480*2/4);
99
db298784 100 sceGuSync(0,0); // sync with prev
2951214e 101 sceGuStart(GU_DIRECT, guCmdList);
102 sceGuCopyImage(GU_PSM_5650, 0, 0, 480, 272, 480, bg_buffer, 0, 0, 512, menu_screen);
103 sceGuFinish();
db298784 104 sceGuSync(0,0);
2951214e 105}
106
107
108static void menu_draw_end(void)
109{
703e4c7b 110 psp_video_flip(1);
2951214e 111}
112
113
114// --------- loading ROM screen ----------
115
a6df06b7 116static int lcdr_line = 0;
117
2951214e 118static void load_progress_cb(int percent)
119{
120 int ln, len = percent * 480 / 100;
121 unsigned short *dst;
122
a6df06b7 123 //sceDisplayWaitVblankStart();
124
125 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
126
127 if (len > 480) len = 480;
128 for (ln = 8; ln > 0; ln--, dst += 512)
129 memset(dst, 0xff, len*2);
130}
131
132static void cdload_progress_cb(int percent)
133{
134 int ln, len = percent * 480 / 100;
135 unsigned short *dst;
136
137 if (lcdr_line <= 2) {
138 lcdr_line++;
139 smalltext_out16(1, lcdr_line++ * 10, "Processing CD image / MP3s", 0xffff);
140 smalltext_out16_lim(1, lcdr_line++ * 10, romFileName, 0xffff, 80);
141 }
2951214e 142
a6df06b7 143 dst = (unsigned short *)menu_screen + 512*10*lcdr_line;
2951214e 144
145 if (len > 480) len = 480;
a6df06b7 146 for (ln = 8; ln > 0; ln--, dst += 512)
2951214e 147 memset(dst, 0xff, len*2);
148}
149
150void menu_romload_prepare(const char *rom_name)
151{
152 const char *p = rom_name + strlen(rom_name);
153 while (p > rom_name && *p != '/') p--;
154
155 psp_video_switch_to_single();
a6df06b7 156 if (rom_data) menu_draw_begin();
5ecedd0c 157 else memset32_uncached(psp_screen, 0, 512*272*2/4);
2951214e 158
159 smalltext_out16(1, 1, "Loading", 0xffff);
160 smalltext_out16_lim(1, 10, p, 0xffff, 80);
161 PicoCartLoadProgressCB = load_progress_cb;
a6df06b7 162 PicoCDLoadProgressCB = cdload_progress_cb;
163 lcdr_line = 2;
2951214e 164}
165
166void menu_romload_end(void)
167{
a6df06b7 168 PicoCartLoadProgressCB = PicoCDLoadProgressCB = NULL;
169 smalltext_out16(1, ++lcdr_line*10, "Starting emulation...", 0xffff);
2951214e 170}
171
172// -------------- ROM selector --------------
173
174// SceIoDirent
175#define DT_DIR FIO_SO_IFDIR
176#define DT_REG FIO_SO_IFREG
177
178struct my_dirent
179{
180 unsigned char d_type;
181 char d_name[255];
182};
183
703e4c7b 184// bbbb bggg gggr rrrr
2951214e 185static unsigned short file2color(const char *fname)
186{
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" };
190 int i;
191
192 if (ext < fname) ext = fname;
193 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
703e4c7b 194 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xfdf7;
2951214e 195 for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
196 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
197 return 0xffff;
198}
199
200static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
201{
202 int start, i, pos;
203
204 start = 13 - sel;
205 n--; // exclude current dir (".")
206
207 menu_draw_begin();
208
209 if (rom_data == NULL) {
210// menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
211 }
212
213 menu_darken_bg((char *)menu_screen + 512*129*2, (char *)menu_screen + 512*129*2, 512*10, 0);
214
215 if (start - 2 >= 0)
216 smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
217 for (i = 0; i < n; i++) {
218 pos = start + i;
219 if (pos < 0) continue;
220 if (pos > 26) break;
221 if (namelist[i+1]->d_type & DT_DIR) {
703e4c7b 222 smalltext_out16_lim(14, pos*10, "/", 0xd7ff, 1);
a52e1f62 223 smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xd7ff, 80-3);
2951214e 224 } else {
225 unsigned short color = file2color(namelist[i+1]->d_name);
a52e1f62 226 smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 80-2);
2951214e 227 }
228 }
229 text_out16(5, 130, ">");
230 menu_draw_end();
231}
232
233static int scandir_cmp(const void *p1, const void *p2)
234{
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);
240}
241
242static char *filter_exts[] = {
243 ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
244 ".jpg", ".cue", ".pbp"
245};
246
247static int scandir_filter(const struct my_dirent *ent)
248{
249 const char *p;
250 int i;
251
252 if (ent == NULL || ent->d_name == NULL) return 0;
253 if (strlen(ent->d_name) < 5) return 1;
254
255 p = ent->d_name + strlen(ent->d_name) - 4;
256
257 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
258 {
259 if (strcasecmp(p, filter_exts[i]) == 0) return 0;
260 }
261
262 return 1;
263}
264
265static 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 *))
268{
269 int ret = -1, dir_uid = -1, name_alloc = 4, name_count = 0;
270 struct my_dirent **namelist = NULL, *ent;
271 SceIoDirent sce_ent;
272
273 namelist = malloc(sizeof(*namelist) * name_alloc);
274 if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
275
276 // try to read first..
277 dir_uid = sceIoDopen(dir);
278 if (dir_uid >= 0)
279 {
703e4c7b 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));
2951214e 283 ret = sceIoDread(dir_uid, &sce_ent);
284 if (ret < 0)
285 {
286 lprintf("sceIoDread(\"%s\") failed with %i\n", dir, ret);
287 goto fail;
288 }
289 }
290 else
291 lprintf("sceIoDopen(\"%s\") failed with %i\n", dir, dir_uid);
292
293 while (ret > 0)
294 {
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));
703e4c7b 299 ent->d_name[sizeof(ent->d_name)-1] = 0;
2951214e 300 if (filter == NULL || filter(ent))
301 namelist[name_count++] = ent;
302 else free(ent);
303
304 if (name_count >= name_alloc)
305 {
306 void *tmp;
307 name_alloc *= 2;
308 tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
309 if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
310 namelist = tmp;
311 }
312
703e4c7b 313 memset(&sce_ent, 0, sizeof(sce_ent));
2951214e 314 ret = sceIoDread(dir_uid, &sce_ent);
315 }
316
317 // sort
318 if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
319
320 // all done.
321 ret = name_count;
322 *namelist_out = namelist;
323 goto end;
324
325fail:
326 if (namelist != NULL)
327 {
328 while (name_count--)
329 free(namelist[name_count]);
330 free(namelist);
331 }
332end:
333 if (dir_uid >= 0) sceIoDclose(dir_uid);
334 return ret;
335}
336
337
338static char *romsel_loop(char *curr_path)
339{
340 struct my_dirent **namelist;
703e4c7b 341 int n, iret, sel = 0;
2951214e 342 unsigned long inp = 0;
343 char *ret = NULL, *fname = NULL;
703e4c7b 344 SceIoStat cpstat;
2951214e 345
346 // is this a dir or a full path?
703e4c7b 347 memset(&cpstat, 0, sizeof(cpstat));
348 iret = sceIoGetstat(curr_path, &cpstat);
349 if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFREG)) { // file
2951214e 350 char *p;
351 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
352 *p = 0;
353 fname = p+1;
354 }
703e4c7b 355 else if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFDIR)); // dir
356 else strcpy(curr_path, "ms0:/"); // something else
2951214e 357
358 n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
359 if (n < 0) {
360 // try root..
703e4c7b 361 n = my_scandir("ms0:/", &namelist, scandir_filter, scandir_cmp);
2951214e 362 if (n < 0) {
363 // oops, we failed
364 lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
365 return NULL;
366 }
367 }
368
369 // try to find sel
370 if (fname != NULL) {
371 int i;
372 for (i = 1; i < n; i++) {
373 if (strcmp(namelist[i]->d_name, fname) == 0) {
374 sel = i - 1;
375 break;
376 }
377 }
378 }
379
380 for (;;)
381 {
382 draw_dirlist(curr_path, namelist, n, sel);
16e89bed 383 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
2951214e 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; }
16e89bed 390 if(inp & BTN_CIRCLE) { // enter dir/select
2951214e 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);
395 ret = romFileName;
396 break;
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--;
703e4c7b 404 while (*p != '/' && *p != ':' && p > start) p--;
405 if (p <= start || *p == ':' || p[-1] == ':') strcpy(newdir, "ms0:/");
2951214e 406 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
407 } else {
408 strcpy(newdir, curr_path);
409 p = newdir + strlen(newdir) - 1;
410 while (*p == '/' && p >= newdir) *p-- = 0;
411 strcat(newdir, "/");
412 strcat(newdir, namelist[sel+1]->d_name);
413 }
414 ret = romsel_loop(newdir);
415 free(newdir);
416 break;
417 }
418 }
16e89bed 419 if(inp & BTN_X) break; // cancel
2951214e 420 }
421
422 if (n > 0) {
423 while(n--) free(namelist[n]);
424 free(namelist);
425 }
426
427 return ret;
428}
429
430// ------------ debug menu ------------
431
432char *debugString(void);
433
434static void draw_debug(void)
435{
436 char *p, *str = debugString();
437 int len, line;
438
439 menu_draw_begin();
440
441 p = str;
442 for (line = 0; line < 24; line++)
443 {
444 while (*p && *p != '\n') p++;
445 len = p - str;
446 if (len > 55) len = 55;
447 smalltext_out16_lim(1, line*10, str, 0xffff, len);
448 if (*p == 0) break;
449 p++; str = p;
450 }
451 menu_draw_end();
452}
453
454static void debug_menu_loop(void)
455{
5ecedd0c 456 int ret = 0;
2951214e 457 draw_debug();
5ecedd0c 458 while (!(ret & (BTN_X|BTN_CIRCLE)))
459 ret = wait_for_input(BTN_X|BTN_CIRCLE, 0);
2951214e 460}
461
462// ------------ patch/gg menu ------------
463
464static void draw_patchlist(int sel)
465{
466 int start, i, pos, active;
467
468 start = 13 - sel;
469
470 menu_draw_begin();
471
472 for (i = 0; i < PicoPatchCount; i++) {
473 pos = start + i;
474 if (pos < 0) continue;
475 if (pos > 26) break;
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);
479 }
480 pos = start + i;
481 if (pos < 27) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
482
483 text_out16(5, 130, ">");
484 menu_draw_end();
485}
486
487
488static void patches_menu_loop(void)
489{
490 int menu_sel = 0;
491 unsigned long inp = 0;
492
493 for(;;)
494 {
495 draw_patchlist(menu_sel);
16e89bed 496 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
2951214e 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; }
16e89bed 501 if(inp & BTN_CIRCLE) { // action
2951214e 502 if (menu_sel < PicoPatchCount)
503 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
504 else return;
505 }
16e89bed 506 if(inp & BTN_X) return;
2951214e 507 }
508
509}
510
511// ------------ savestate loader ------------
512
513static int state_slot_flags = 0;
514
515static void state_check_slots(void)
516{
517 int slot;
518
519 state_slot_flags = 0;
520
521 for (slot = 0; slot < 10; slot++)
522 {
523 if (emu_checkSaveFile(slot))
524 {
525 state_slot_flags |= 1 << slot;
526 }
527 }
528}
529
6f748c47 530static void *get_oldstate_for_preview(void)
531{
532 unsigned char *ptr = malloc(sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram) + sizeof(Pico.video));
533 if (ptr == NULL) return NULL;
534
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));
539 return ptr;
540}
541
542static void restore_oldstate(void *ptrx)
543{
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));
549 free(ptrx);
550}
551
2951214e 552static void draw_savestate_bg(int slot)
553{
6f748c47 554 void *file, *oldstate;
2951214e 555 char *fname;
556
557 fname = emu_GetSaveFName(1, 0, slot);
558 if (!fname) return;
559
6f748c47 560 oldstate = get_oldstate_for_preview();
561 if (oldstate == NULL) return;
2951214e 562
563 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
564 file = gzopen(fname, "rb");
565 emu_setSaveStateCbs(1);
566 } else {
567 file = fopen(fname, "rb");
568 emu_setSaveStateCbs(0);
569 }
570
571 if (file) {
572 if (PicoMCD & 1) {
573 PicoCdLoadStateGfx(file);
574 } else {
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);
582 }
583 areaClose(file);
584 }
585
586 emu_forcedFrame();
a52e1f62 587 menu_prepare_bg(1, 0);
2951214e 588
6f748c47 589 restore_oldstate(oldstate);
2951214e 590}
591
592static void draw_savestate_menu(int menu_sel, int is_loading)
593{
594 int tl_x = 80+25, tl_y = 16+60, y, i;
595
596 if (state_slot_flags & (1 << menu_sel))
597 draw_savestate_bg(menu_sel);
598 menu_draw_begin();
599
600 text_out16(tl_x, 16+30, is_loading ? "Load state" : "Save state");
601
602 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
603
604 /* draw all 10 slots */
605 y = tl_y;
606 for (i = 0; i < 10; i++, y+=10)
607 {
608 text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
609 }
610 text_out16(tl_x, y, "back");
611
612 menu_draw_end();
613}
614
615static int savestate_menu_loop(int is_loading)
616{
617 static int menu_sel = 10;
618 int menu_sel_max = 10;
619 unsigned long inp = 0;
620
621 state_check_slots();
622
623 for(;;)
624 {
625 draw_savestate_menu(menu_sel, is_loading);
16e89bed 626 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
2951214e 627 if(inp & BTN_UP ) {
628 do {
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);
631 }
632 if(inp & BTN_DOWN) {
633 do {
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);
636 }
16e89bed 637 if(inp & BTN_CIRCLE) { // save/load
2951214e 638 if (menu_sel < 10) {
639 state_slot = menu_sel;
6f748c47 640 PicoStateProgressCB = emu_msg_cb; /* also suitable for menu */
2951214e 641 if (emu_SaveLoadGame(is_loading, 0)) {
642 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
643 return 1;
644 }
645 return 0;
646 } else return 1;
647 }
16e89bed 648 if(inp & BTN_X) return 1;
2951214e 649 }
650}
651
652// -------------- key config --------------
653
654static char *action_binds(int player_idx, int action_mask)
655{
656 static char strkeys[32*5];
657 int i;
658
659 strkeys[0] = 0;
660 for (i = 0; i < 32; i++) // i is key index
661 {
662 if (currentConfig.KeyBinds[i] & action_mask)
663 {
664 if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
16e89bed 665 if (strkeys[0]) {
666 strcat(strkeys, i >= 28 ? ", " : " + "); // nub "buttons" don't create combos
667 strcat(strkeys, pspKeyNames[i]);
668 break;
669 }
2951214e 670 else strcpy(strkeys, pspKeyNames[i]);
671 }
672 }
673
674 return strkeys;
675}
676
677static void unbind_action(int action)
678{
679 int i;
680
681 for (i = 0; i < 32; i++)
682 currentConfig.KeyBinds[i] &= ~action;
683}
684
685static int count_bound_keys(int action, int pl_idx)
686{
687 int i, keys = 0;
688
689 for (i = 0; i < 32; i++)
690 {
691 if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
692 if (currentConfig.KeyBinds[i] & action) keys++;
693 }
694
695 return keys;
696}
697
698typedef struct { char *name; int mask; } bind_action_t;
699
700static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)
701{
702 int x, y, tl_y = 16+40, i;
703
704 menu_draw_begin();
705 if (player_idx >= 0) {
706 text_out16(80+80, 16+20, "Player %i controls", player_idx + 1);
707 x = 80+80;
708 } else {
709 text_out16(80+80, 16+20, "Emulator controls");
710 x = 80+40;
711 }
712
713 menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
714
715 y = tl_y;
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));
718
719 text_out16(x, y, "Done");
720
721 if (sel < opt_cnt) {
16e89bed 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");
2951214e 726 } else {
16e89bed 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");
2951214e 730 }
731 menu_draw_end();
732}
733
734static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)
735{
736 int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
737 unsigned long inp = 0;
738
739 for (;;)
740 {
741 draw_key_config(opts, opt_cnt, player_idx, sel);
16e89bed 742 inp = wait_for_input(CONFIGURABLE_KEYS|BTN_SELECT, 1);
2951214e 743 if (!(inp & BTN_SELECT)) {
744 prev_select = 0;
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; }
747 }
748 if (sel >= opt_cnt) {
749 if (inp & (BTN_X|BTN_CIRCLE)) break;
750 else continue;
751 }
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;
757 inp &= ~BTN_SELECT;
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;
766 }
767 }
768 }
769}
770
771static void draw_kc_sel(int menu_sel)
772{
773 int tl_x = 80+25+40, tl_y = 16+60, y;
774
775 y = tl_y;
776 menu_draw_begin();
777 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
778
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");
783
784 menu_draw_end();
785}
786
787
788// PicoPad[] format: MXYZ SACB RLDU
789static bind_action_t ctrl_actions[] =
790{
791 { "UP ", 0x001 },
792 { "DOWN ", 0x002 },
793 { "LEFT ", 0x004 },
794 { "RIGHT ", 0x008 },
795 { "A ", 0x040 },
796 { "B ", 0x010 },
797 { "C ", 0x020 },
798 { "START ", 0x080 },
799 { "MODE ", 0x800 },
800 { "X ", 0x400 },
801 { "Y ", 0x200 },
802 { "Z ", 0x100 },
803};
804
805// player2_flag, ?, ?, ?, ?, ?, ?, menu
806// "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
807// "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
808static bind_action_t emuctrl_actions[] =
809{
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 },
2951214e 815};
816
817static void kc_sel_loop(void)
818{
819 int menu_sel = 3, menu_sel_max = 3;
820 unsigned long inp = 0;
821 int is_6button = currentConfig.PicoOpt & 0x020;
822
823 while (1)
824 {
825 draw_kc_sel(menu_sel);
16e89bed 826 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
2951214e 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; }
16e89bed 829 if (inp & BTN_CIRCLE) {
2951214e 830 switch (menu_sel) {
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;
836 default: return;
837 }
838 }
16e89bed 839 if (inp & BTN_X) return;
2951214e 840 }
841}
842
843
844// --------- sega/mega cd options ----------
845
846menu_entry cdopt_entries[] =
847{
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, &currentConfig.EmuOpt, 0x0400, 0, 0, 1 },
852 { "CDDA audio (using mp3s)", MB_ONOFF, MA_CDOPT_CDDA, &currentConfig.PicoOpt, 0x0800, 0, 0, 1 },
853 { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &currentConfig.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, &currentConfig.PicoOpt, 0x8000, 0, 0, 1 },
856 { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&currentConfig.PicoOpt, 0x1000, 0, 0, 1 },
857 { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &currentConfig.PicoOpt, 0x2000, 0, 0, 1 },
858 { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1 },
859};
860
861#define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
862
863
864struct bios_names_t
865{
866 char us[32], eu[32], jp[32];
867};
868
869static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
870{
871 struct bios_names_t *bios_names = param;
872 char ra_buff[16];
873
874 switch (entry->id)
875 {
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);
883 break;
884 default:break;
885 }
886}
887
888static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
889{
890 int tl_x = 80+25, tl_y = 16+60;
891 menu_id selected_id;
892 char ra_buff[16];
893
894 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
895 else strcpy(ra_buff, " OFF");
896
897 menu_draw_begin();
898
899 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
900
901 me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
902
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")))
16e89bed 907 text_out16(tl_x, 250, "Press start to test selected BIOS");
2951214e 908
909 menu_draw_end();
910}
911
912static void cd_menu_loop_options(void)
913{
914 static int menu_sel = 0;
915 int menu_sel_max = 10;
916 unsigned long inp = 0;
917 struct bios_names_t bios_names;
918 menu_id selected_id;
919 char *bios, *p;
920
921 if (emu_findBios(4, &bios)) { // US
60a10527 922 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
923 if (*p == '/') p++;
2951214e 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");
926
927 if (emu_findBios(8, &bios)) { // EU
60a10527 928 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
929 if (*p == '/') p++;
2951214e 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");
932
933 if (emu_findBios(1, &bios)) { // JP
60a10527 934 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
935 if (*p == '/') p++;
2951214e 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");
938
a6df06b7 939 menuErrorMsg[0] = 0;
940
941 for (;;)
2951214e 942 {
943 draw_cd_menu_options(menu_sel, &bios_names);
16e89bed 944 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
2951214e 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) {
952 PicoCDBuffers >>= 1;
953 if (PicoCDBuffers < 64) PicoCDBuffers = 0;
954 } else {
955 if (PicoCDBuffers < 64) PicoCDBuffers = 64;
956 else PicoCDBuffers <<= 1;
957 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
958 }
959 }
960 }
16e89bed 961 if (inp & BTN_CIRCLE) { // toggleable options
2951214e 962 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
963 selected_id == MA_CDOPT_DONE) {
964 return;
965 }
966 switch (selected_id) { // BIOS testers
967 case MA_CDOPT_TESTBIOS_USA:
968 if (emu_findBios(4, &bios)) { // test US
969 strcpy(romFileName, bios);
970 engineState = PGS_ReloadRom;
971 return;
972 }
973 break;
974 case MA_CDOPT_TESTBIOS_EUR:
975 if (emu_findBios(8, &bios)) { // test EU
976 strcpy(romFileName, bios);
977 engineState = PGS_ReloadRom;
978 return;
979 }
980 break;
981 case MA_CDOPT_TESTBIOS_JAP:
982 if (emu_findBios(1, &bios)) { // test JP
983 strcpy(romFileName, bios);
984 engineState = PGS_ReloadRom;
985 return;
986 }
987 break;
988 default:
989 break;
990 }
991 }
16e89bed 992 if (inp & BTN_X) return;
2951214e 993 }
994}
995
6f748c47 996// --------- display options ----------
997
998menu_entry opt3_entries[] =
999{
1000 { NULL, MB_NONE, MA_OPT3_SCALE, NULL, 0, 0, 0, 1 },
1001 { NULL, MB_NONE, MA_OPT3_HSCALE32, NULL, 0, 0, 0, 1 },
1002 { NULL, MB_NONE, MA_OPT3_HSCALE40, NULL, 0, 0, 0, 1 },
1003 { NULL, MB_ONOFF, MA_OPT3_FILTERING, &currentConfig.scaling, 1, 0, 0, 1 },
4b8f4f3c 1004 { NULL, MB_NONE, MA_OPT3_VSYNC, NULL, 0, 0, 0, 1 },
6f748c47 1005 { "Set to unscaled centered", MB_NONE, MA_OPT3_PRES_NOSCALE, NULL, 0, 0, 0, 1 },
1006 { "Set to fullscreen", MB_NONE, MA_OPT3_PRES_FULLSCR, NULL, 0, 0, 0, 1 },
1007 { "done", MB_NONE, MA_OPT3_DONE, NULL, 0, 0, 0, 1 },
1008};
1009
1010#define OPT3_ENTRY_COUNT (sizeof(opt3_entries) / sizeof(opt3_entries[0]))
1011
1012
1013static void menu_opt3_cust_draw(const menu_entry *entry, int x, int y, void *param)
1014{
1015 switch (entry->id)
1016 {
1017 case MA_OPT3_SCALE:
1018 text_out16(x, y, "Scale factor: %.2f", currentConfig.scale);
1019 break;
1020 case MA_OPT3_HSCALE32:
1021 text_out16(x, y, "Hor. scale (for low res. games): %.2f", currentConfig.hscale32);
1022 break;
1023 case MA_OPT3_HSCALE40:
1024 text_out16(x, y, "Hor. scale (for hi res. games): %.2f", currentConfig.hscale40);
1025 break;
1026 case MA_OPT3_FILTERING:
1027 text_out16(x, y, "Bilinear filtering %s", currentConfig.scaling?"ON":"OFF");
1028 break;
4b8f4f3c 1029 case MA_OPT3_VSYNC: {
1030 char *val = " never";
1031 if (currentConfig.EmuOpt & 0x2000)
1032 val = (currentConfig.EmuOpt & 0x10000) ? "sometimes" : " always";
c93fb19e 1033 text_out16(x, y, "Wait for vsync (slow) %s", val);
4b8f4f3c 1034 break;
1035 }
6f748c47 1036 default: break;
1037 }
1038}
1039
1040static void menu_opt3_preview(int is_32col)
1041{
1042 void *oldstate = NULL;
1043
1044 if (rom_data == NULL || ((Pico.video.reg[12]&1)^1) != is_32col)
1045 {
1046 extern char bgdatac32_start[], bgdatac40_start[];
1047 extern int bgdatac32_size, bgdatac40_size;
1048 void *bgdata = is_32col ? bgdatac32_start : bgdatac40_start;
1049 unsigned long insize = is_32col ? bgdatac32_size : bgdatac40_size, outsize = 65856;
1050 int ret;
6f748c47 1051 ret = uncompress((Bytef *)bg_buffer, &outsize, bgdata, insize);
1052 if (ret == 0)
1053 {
1054 if (rom_data != NULL) oldstate = get_oldstate_for_preview();
1055 memcpy(Pico.vram, bg_buffer, sizeof(Pico.vram));
1056 memcpy(Pico.cram, (char *)bg_buffer + 0x10000, 0x40*2);
1057 memcpy(Pico.vsram, (char *)bg_buffer + 0x10080, 0x40*2);
1058 memcpy(&Pico.video,(char *)bg_buffer + 0x10100, 0x40);
1059 }
1060 else
1061 lprintf("uncompress returned %i\n", ret);
1062 }
1063
5ecedd0c 1064 memset32_uncached(psp_screen, 0, 512*272*2/4);
6f748c47 1065 emu_forcedFrame();
a52e1f62 1066 menu_prepare_bg(1, 0);
6f748c47 1067
1068 if (oldstate) restore_oldstate(oldstate);
1069}
1070
1071static void draw_dispmenu_options(int menu_sel)
1072{
4b8f4f3c 1073 int tl_x = 80, tl_y = 16+50;
6f748c47 1074
1075 menu_draw_begin();
1076
4b8f4f3c 1077 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 316);
6f748c47 1078
1079 me_draw(opt3_entries, OPT3_ENTRY_COUNT, tl_x, tl_y, menu_opt3_cust_draw, NULL);
1080
1081 menu_draw_end();
1082}
1083
1084static void dispmenu_loop_options(void)
1085{
1086 static int menu_sel = 0;
c93fb19e 1087 int menu_sel_max, is_32col = (Pico.video.reg[12]&1)^1;
6f748c47 1088 unsigned long inp = 0;
1089 menu_id selected_id;
1090
1091 menu_sel_max = me_count_enabled(opt3_entries, OPT3_ENTRY_COUNT) - 1;
1092
4b8f4f3c 1093 for (;;)
6f748c47 1094 {
1095 draw_dispmenu_options(menu_sel);
16e89bed 1096 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
6f748c47 1097 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1098 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1099 selected_id = me_index2id(opt3_entries, OPT3_ENTRY_COUNT, menu_sel);
1100 if (selected_id == MA_OPT3_HSCALE40 && is_32col) { is_32col = 0; menu_opt3_preview(is_32col); }
1101 if (selected_id == MA_OPT3_HSCALE32 && !is_32col) { is_32col = 1; menu_opt3_preview(is_32col); }
1102
1103 if (inp & (BTN_LEFT|BTN_RIGHT)) // multi choise
1104 {
1105 float *setting = NULL;
4b8f4f3c 1106 int tmp;
6f748c47 1107 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0);
1108 switch (selected_id) {
1109 case MA_OPT3_SCALE: setting = &currentConfig.scale; break;
1110 case MA_OPT3_HSCALE40: setting = &currentConfig.hscale40; is_32col = 0; break;
1111 case MA_OPT3_HSCALE32: setting = &currentConfig.hscale32; is_32col = 1; break;
1112 case MA_OPT3_FILTERING:menu_opt3_preview(is_32col); break;
4b8f4f3c 1113 case MA_OPT3_VSYNC: tmp = ((currentConfig.EmuOpt>>13)&1) | ((currentConfig.EmuOpt>>15)&2);
1114 tmp = (inp & BTN_LEFT) ? (tmp>>1) : ((tmp<<1)|1);
1115 if (tmp > 3) tmp = 3;
1116 currentConfig.EmuOpt &= ~0x12000;
1117 currentConfig.EmuOpt |= ((tmp&2)<<15) | ((tmp&1)<<13);
1118 break;
6f748c47 1119 default: break;
1120 }
1121 if (setting != NULL) {
1122 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1123 *setting += (inp & BTN_LEFT) ? -0.01 : 0.01;
5ecedd0c 1124 if (*setting <= 0) *setting = 0.01;
6f748c47 1125 menu_opt3_preview(is_32col);
1126 draw_dispmenu_options(menu_sel); // will wait vsync
1127 }
1128 }
1129 }
16e89bed 1130 if (inp & BTN_CIRCLE) { // toggleable options
6f748c47 1131 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, 1);
1132 switch (selected_id) {
1133 case MA_OPT3_DONE:
1134 return;
1135 case MA_OPT3_PRES_NOSCALE:
1136 currentConfig.scale = currentConfig.hscale40 = currentConfig.hscale32 = 1.0;
1137 menu_opt3_preview(is_32col);
1138 break;
1139 case MA_OPT3_PRES_FULLSCR:
1140 currentConfig.scale = 1.20;
1141 currentConfig.hscale40 = 1.25;
1142 currentConfig.hscale32 = 1.56;
1143 menu_opt3_preview(is_32col);
1144 break;
1145 case MA_OPT3_FILTERING:
1146 menu_opt3_preview(is_32col);
1147 break;
1148 default: break;
1149 }
1150 }
16e89bed 1151 if (inp & BTN_X) return;
6f748c47 1152 }
1153}
1154
2951214e 1155
1156// --------- advanced options ----------
1157
1158menu_entry opt2_entries[] =
1159{
16e89bed 1160 { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &currentConfig.PicoOpt,0x00004, 0, 0, 1 },
1161 { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &currentConfig.PicoOpt,0x00001, 0, 0, 1 },
1162 { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496,&currentConfig.PicoOpt,0x00002, 0, 0, 1 },
1163 { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, &currentConfig.EmuOpt, 0x00008, 0, 0, 1 },
1164 { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, &currentConfig.EmuOpt, 0x00020, 0, 0, 1 },
1165 { "Status line in main menu", MB_ONOFF, MA_OPT2_STATUS_LINE, &currentConfig.EmuOpt, 0x20000, 0, 0, 1 },
2951214e 1166 { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1 },
1167};
1168
1169#define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
1170
1171
1172static void draw_amenu_options(int menu_sel)
1173{
1174 int tl_x = 80+25, tl_y = 16+50;
1175
1176 menu_draw_begin();
1177
1178 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
1179
1180 me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1181
1182 menu_draw_end();
1183}
1184
1185static void amenu_loop_options(void)
1186{
1187 static int menu_sel = 0;
1188 int menu_sel_max;
1189 unsigned long inp = 0;
1190 menu_id selected_id;
1191
1192 menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
1193
1194 for(;;)
1195 {
1196 draw_amenu_options(menu_sel);
16e89bed 1197 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
2951214e 1198 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1199 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1200 selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
1201 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1202 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
1203 selected_id == MA_OPT2_GAMMA) {
6f748c47 1204 // TODO?
2951214e 1205 }
1206 }
16e89bed 1207 if (inp & BTN_CIRCLE) { // toggleable options
2951214e 1208 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
1209 selected_id == MA_OPT2_DONE) {
1210 return;
1211 }
1212 }
16e89bed 1213 if (inp & BTN_X) return;
2951214e 1214 }
1215}
1216
1217// -------------- options --------------
1218
1219
1220menu_entry opt_entries[] =
1221{
1222 { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1 },
2951214e 1223 { "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, &currentConfig.PicoOpt, 0x0040, 0, 0, 1 },
1224 { "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, &currentConfig.PicoOpt, 0x0080, 0, 0, 1 },
1225 { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, &currentConfig.EmuOpt, 0x0002, 0, 0, 1 },
1226 { NULL, MB_RANGE, MA_OPT_FRAMESKIP, &currentConfig.Frameskip, 0, -1, 16, 1 },
1227 { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, &currentConfig.EmuOpt, 0x0004, 0, 0, 1 },
1228 { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1 },
1229 { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &currentConfig.PicoOpt, 0x0020, 0, 0, 1 },
1230 { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1 },
1231 { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, &currentConfig.EmuOpt, 0x0001, 0, 0, 1 },
1232 { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1 },
1233 { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1 },
2b90fc61 1234 { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1 },
6f748c47 1235 { "[Display options]", MB_NONE, MA_OPT_DISP_OPTS, NULL, 0, 0, 0, 1 },
2951214e 1236 { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1 },
6f748c47 1237 { "[Advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1 },
2951214e 1238 { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1 },
1239 { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1 },
1240 { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1 },
1241};
1242
1243#define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
1244
1245
1246static const char *region_name(unsigned int code)
1247{
1248 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };
1249 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };
1250 int u, i = 0;
1251 if (code) {
1252 code <<= 1;
1253 while((code >>= 1)) i++;
1254 if (i > 4) return "unknown";
1255 return names[i];
1256 } else {
1257 static char name[24];
1258 strcpy(name, "Auto:");
1259 for (u = 0; u < 3; u++) {
1260 i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;
1261 while((code >>= 1)) i++;
1262 strcat(name, names_short[i]);
1263 }
1264 return name;
1265 }
1266}
1267
1268
1269static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
1270{
1271 char *str, str24[24];
1272
1273 switch (entry->id)
1274 {
1275 case MA_OPT_RENDERER:
1276 if (currentConfig.PicoOpt&0x10)
2aea862d 1277 str = "fast";
2951214e 1278 else if (currentConfig.EmuOpt&0x80)
2aea862d 1279 str = "accurate";
2951214e 1280 else
2aea862d 1281 str = " 8bit accurate"; // n/a
1282 text_out16(x, y, "Renderer: %s", str);
2951214e 1283 break;
1284 case MA_OPT_FRAMESKIP:
1285 if (currentConfig.Frameskip < 0)
1286 strcpy(str24, "Auto");
1287 else sprintf(str24, "%i", currentConfig.Frameskip);
1288 text_out16(x, y, "Frameskip %s", str24);
1289 break;
1290 case MA_OPT_SOUND_QUALITY:
1291 str = (currentConfig.PicoOpt&0x08)?"stereo":"mono";
1292 text_out16(x, y, "Sound Quality: %5iHz %s", currentConfig.PsndRate, str);
1293 break;
1294 case MA_OPT_REGION:
1295 text_out16(x, y, "Region: %s", region_name(currentConfig.PicoRegion));
1296 break;
1297 case MA_OPT_CONFIRM_STATES:
1298 switch ((currentConfig.EmuOpt >> 9) & 5) {
1299 default: str = "OFF"; break;
1300 case 1: str = "writes"; break;
1301 case 4: str = "loads"; break;
1302 case 5: str = "both"; break;
1303 }
1304 text_out16(x, y, "Confirm savestate %s", str);
1305 break;
2b90fc61 1306 case MA_OPT_CPU_CLOCKS:
1307 text_out16(x, y, "CPU/bus clock %3i/%3iMHz", currentConfig.CPUclock, currentConfig.CPUclock/2);
1308 break;
2951214e 1309 case MA_OPT_SAVECFG:
1310 str24[0] = 0;
1311 if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
1312 text_out16(x, y, "Save cfg as default%s", str24);
1313 break;
1314 case MA_OPT_LOADCFG:
1315 text_out16(x, y, "Load cfg from profile %i", config_slot);
1316 break;
1317 default:
1318 lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
1319 break;
1320 }
1321}
1322
1323
2951214e 1324static void draw_menu_options(int menu_sel)
1325{
1326 int tl_x = 80+25, tl_y = 16+24;
1327
1328 menu_draw_begin();
1329
1330 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
1331
1332 me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
1333
1334 menu_draw_end();
1335}
1336
1337static int sndrate_prevnext(int rate, int dir)
1338{
1339 int i, rates[] = { 11025, 22050, 44100 };
1340
1341 for (i = 0; i < 5; i++)
1342 if (rates[i] == rate) break;
1343
1344 i += dir ? 1 : -1;
1345 if (i > 2) return dir ? 44100 : 22050;
1346 if (i < 0) return dir ? 22050 : 11025;
1347 return rates[i];
1348}
1349
1350static void region_prevnext(int right)
1351{
1352 // jp_ntsc=1, jp_pal=2, usa=4, eu=8
1353 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
1354 int i;
1355 if (right) {
1356 if (!currentConfig.PicoRegion) {
1357 for (i = 0; i < 6; i++)
1358 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1359 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
1360 else currentConfig.PicoRegion=1;
1361 }
1362 else currentConfig.PicoRegion<<=1;
1363 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;
1364 } else {
1365 if (!currentConfig.PicoRegion) {
1366 for (i = 0; i < 6; i++)
1367 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1368 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
1369 }
1370 else currentConfig.PicoRegion>>=1;
1371 }
1372}
1373
1374static void menu_options_save(void)
1375{
1376 PicoOpt = currentConfig.PicoOpt;
1377 PsndRate = currentConfig.PsndRate;
1378 PicoRegionOverride = currentConfig.PicoRegion;
1379 if (!(PicoOpt & 0x20)) {
1380 // unbind XYZ MODE, just in case
1381 unbind_action(0xf00);
1382 }
1383}
1384
1385static int menu_loop_options(void)
1386{
1387 static int menu_sel = 0;
1388 int menu_sel_max, ret;
1389 unsigned long inp = 0;
1390 menu_id selected_id;
1391
1392 currentConfig.PicoOpt = PicoOpt;
1393 currentConfig.PsndRate = PsndRate;
1394 currentConfig.PicoRegion = PicoRegionOverride;
1395
1396 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);
1397 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1398 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1399 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1400
1401 while (1)
1402 {
1403 draw_menu_options(menu_sel);
16e89bed 1404 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
2951214e 1405 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1406 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1407 selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
1408 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1409 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0)) {
1410 switch (selected_id) {
1411 case MA_OPT_RENDERER:
2aea862d 1412 if ((currentConfig.PicoOpt&0x10) || !(currentConfig.EmuOpt &0x80)) {
1413 currentConfig.PicoOpt&= ~0x10;
1414 currentConfig.EmuOpt |= 0x80;
2951214e 1415 } else {
2aea862d 1416 currentConfig.PicoOpt|= 0x10;
1417 currentConfig.EmuOpt &= ~0x80;
2951214e 1418 }
1419 break;
1420 case MA_OPT_SOUND_QUALITY:
2aea862d 1421 currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & BTN_RIGHT);
2951214e 1422 break;
1423 case MA_OPT_REGION:
1424 region_prevnext(inp & BTN_RIGHT);
1425 break;
1426 case MA_OPT_CONFIRM_STATES: {
1427 int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
1428 n += (inp & BTN_LEFT) ? -1 : 1;
1429 if (n < 0) n = 0; else if (n > 3) n = 3;
1430 n |= n << 1; n &= ~2;
1431 currentConfig.EmuOpt &= ~0xa00;
1432 currentConfig.EmuOpt |= n << 9;
1433 break;
1434 }
1435 case MA_OPT_SAVE_SLOT:
1436 if (inp & BTN_RIGHT) {
1437 state_slot++; if (state_slot > 9) state_slot = 0;
1438 } else {state_slot--; if (state_slot < 0) state_slot = 9;
1439 }
1440 break;
2b90fc61 1441 case MA_OPT_CPU_CLOCKS:
1442 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1443 currentConfig.CPUclock += (inp & BTN_LEFT) ? -1 : 1;
1444 if (currentConfig.CPUclock < 19) currentConfig.CPUclock = 19;
1445 if (currentConfig.CPUclock > 333) currentConfig.CPUclock = 333;
1446 draw_menu_options(menu_sel); // will wait vsync
1447 }
1448 break;
2951214e 1449 case MA_OPT_SAVECFG:
1450 case MA_OPT_SAVECFG_GAME:
1451 case MA_OPT_LOADCFG:
1452 config_slot += (inp&BTN_RIGHT) ? 1 : -1;
1453 if (config_slot > 9) config_slot = 0;
1454 if (config_slot < 0) config_slot = 9;
1455 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1456 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1457 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1458 break;
1459 default:
1460 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1461 break;
1462 }
1463 }
1464 }
16e89bed 1465 if (inp & BTN_CIRCLE) {
2951214e 1466 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
1467 {
1468 switch (selected_id)
1469 {
6f748c47 1470 case MA_OPT_DISP_OPTS:
1471 dispmenu_loop_options();
1472 break;
2951214e 1473 case MA_OPT_SCD_OPTS:
1474 cd_menu_loop_options();
1475 if (engineState == PGS_ReloadRom)
1476 return 0; // test BIOS
1477 break;
1478 case MA_OPT_ADV_OPTS:
1479 amenu_loop_options();
1480 break;
1481 case MA_OPT_SAVECFG: // done (update and write)
1482 menu_options_save();
1483 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
1484 else strcpy(menuErrorMsg, "failed to write config");
1485 return 1;
1486 case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
1487 menu_options_save();
1488 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
1489 else strcpy(menuErrorMsg, "failed to write config");
1490 return 1;
1491 case MA_OPT_LOADCFG:
1492 ret = emu_ReadConfig(1, 1);
1493 if (!ret) ret = emu_ReadConfig(0, 1);
1494 if (ret) strcpy(menuErrorMsg, "config loaded");
1495 else strcpy(menuErrorMsg, "failed to load config");
1496 return 1;
1497 default:
1498 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1499 break;
1500 }
1501 }
1502 }
16e89bed 1503 if(inp & BTN_X) {
2951214e 1504 menu_options_save();
1505 return 0; // done (update, no write)
1506 }
1507 }
1508}
1509
1510// -------------- credits --------------
1511
1512static void draw_menu_credits(void)
1513{
1514 int tl_x = 80+15, tl_y = 16+64, y;
1515 menu_draw_begin();
1516
1517 text_out16(tl_x, 16+20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");
1518
1519 y = tl_y;
1520 text_out16(tl_x, y, "Credits:");
4b8f4f3c 1521 text_out16(tl_x, (y+=10), "fDave: base code of PicoDrive");
1522 text_out16(tl_x, (y+=10), "Chui: Fame/C");
1523 text_out16(tl_x, (y+=10), "NJ: CZ80");
2951214e 1524 text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
2951214e 1525 text_out16(tl_x, (y+=10), "Stephane Dallongeville:");
4b8f4f3c 1526 text_out16(tl_x, (y+=10), " Gens code, base of Fame/C, CZ80");
1527 text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");
2951214e 1528 text_out16(tl_x, (y+=10), "Haze: Genesis hw info");
4b8f4f3c 1529 text_out16(tl_x, (y+=10), "ps2dev.org people: PSP SDK/code");
2951214e 1530 text_out16(tl_x, (y+=10), "ketchupgun: skin design");
1531
1532 menu_draw_end();
1533}
1534
1535
1536// -------------- root menu --------------
1537
1538menu_entry main_entries[] =
1539{
1540 { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
1541 { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
1542 { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
1543 { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
1544 { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
1545 { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
1546 { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
1547 { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
1548 { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
1549 { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
1550};
1551
1552#define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
1553
1554static void draw_menu_root(int menu_sel)
1555{
16e89bed 1556 const int tl_x = 86+70, tl_y = 16+70;
1557 char *stat = NULL;
2951214e 1558
1559 menu_draw_begin();
1560
16e89bed 1561 if ((currentConfig.EmuOpt&0x20000) && (stat = psp_get_status_line()))
1562 text_out16(287, 12, "%s", stat);
1563
1564 text_out16(tl_x, 48, "PicoDrive v" VERSION);
2951214e 1565
1566 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
1567
1568 me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1569
1570 // error
6f748c47 1571 if (menuErrorMsg[0])
1572 text_out16(10, 252, menuErrorMsg);
2951214e 1573 menu_draw_end();
1574}
1575
1576
1577static void menu_loop_root(void)
1578{
1579 static int menu_sel = 0;
1580 int ret, menu_sel_max;
1581 unsigned long inp = 0;
1582
1583 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_data != NULL);
1584 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_data != NULL);
1585 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_data != NULL);
1586 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_data != NULL);
1587 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
1588
1589 menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
1590 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1591
a6df06b7 1592 // mp3 errors?
1593 if (mp3_last_error != 0) {
1594 if (mp3_last_error == -1)
1595 sprintf(menuErrorMsg, "Unsupported mp3 format, use 44kHz stereo");
1596 else sprintf(menuErrorMsg, "mp3 init failed, code %08x", mp3_last_error);
1597 mp3_last_error = 0;
1598 }
1599
2951214e 1600 /* make sure action buttons are not pressed on entering menu */
1601 draw_menu_root(menu_sel);
1602
703e4c7b 1603 while (psp_pad_read(1) & (BTN_X|BTN_CIRCLE|BTN_SELECT)) psp_msleep(50);
2951214e 1604
1605 for (;;)
1606 {
1607 draw_menu_root(menu_sel);
16e89bed 1608 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE|BTN_SELECT|BTN_L|BTN_R, 0);
2951214e 1609 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1610 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1611 if((inp & (BTN_L|BTN_R)) == (BTN_L|BTN_R)) debug_menu_loop();
16e89bed 1612 if( inp & (BTN_SELECT|BTN_X)) {
2951214e 1613 if (rom_data) {
16e89bed 1614 while (psp_pad_read(1) & (BTN_SELECT|BTN_X)) psp_msleep(50); // wait until released
2951214e 1615 engineState = PGS_Running;
1616 break;
1617 }
1618 }
16e89bed 1619 if(inp & BTN_CIRCLE) {
a6df06b7 1620 menuErrorMsg[0] = 0; // clear error msg
2951214e 1621 switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
1622 {
1623 case MA_MAIN_RESUME_GAME:
1624 if (rom_data) {
16e89bed 1625 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
2951214e 1626 engineState = PGS_Running;
1627 return;
1628 }
1629 break;
1630 case MA_MAIN_SAVE_STATE:
1631 if (rom_data) {
1632 if(savestate_menu_loop(0))
1633 continue;
1634 engineState = PGS_Running;
1635 return;
1636 }
1637 break;
1638 case MA_MAIN_LOAD_STATE:
1639 if (rom_data) {
1640 if(savestate_menu_loop(1))
1641 continue;
1642 engineState = PGS_Running;
1643 return;
1644 }
1645 break;
1646 case MA_MAIN_RESET_GAME:
1647 if (rom_data) {
1648 emu_ResetGame();
1649 engineState = PGS_Running;
1650 return;
1651 }
1652 break;
1653 case MA_MAIN_LOAD_ROM:
1654 {
1655 char curr_path[PATH_MAX], *selfname;
1656 FILE *tstf;
1657 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1658 {
1659 fclose(tstf);
1660 strcpy(curr_path, currentConfig.lastRomFile);
1661 }
1662 else
1663 getcwd(curr_path, PATH_MAX);
1664 selfname = romsel_loop(curr_path);
1665 if (selfname) {
1666 lprintf("selected file: %s\n", selfname);
1667 engineState = PGS_ReloadRom;
1668 return;
1669 }
1670 break;
1671 }
1672 case MA_MAIN_OPTIONS:
1673 ret = menu_loop_options();
1674 if (ret == 1) continue; // status update
1675 if (engineState == PGS_ReloadRom)
1676 return; // BIOS test
1677 break;
1678 case MA_MAIN_CONTROLS:
1679 kc_sel_loop();
1680 break;
1681 case MA_MAIN_CREDITS:
1682 draw_menu_credits();
1683 psp_msleep(500);
16e89bed 1684 inp = 0;
1685 while (!(inp & (BTN_X|BTN_CIRCLE)))
1686 inp = wait_for_input(BTN_X|BTN_CIRCLE, 0);
2951214e 1687 break;
1688 case MA_MAIN_EXIT:
1689 engineState = PGS_Quit;
1690 return;
1691 case MA_MAIN_PATCHES:
1692 if (rom_data && PicoPatches) {
1693 patches_menu_loop();
1694 PicoPatchApply();
1695 strcpy(menuErrorMsg, "Patches applied");
1696 continue;
1697 }
1698 break;
1699 default:
1700 lprintf("%s: something unknown selected\n", __FUNCTION__);
1701 break;
1702 }
1703 }
2951214e 1704 }
1705}
1706
1707// warning: alignment
1708static void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
1709{
1710 unsigned int *dest = dst;
1711 const unsigned int *srce = src;
1712 pixels /= 2;
1713 if (darker)
1714 {
1715 while (pixels--)
1716 {
1717 unsigned int p = *srce++;
1718 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
1719 }
1720 }
1721 else
1722 {
1723 while (pixels--)
1724 {
1725 unsigned int p = *srce++;
1726 *dest++ = (p&0xf79ef79e)>>1;
1727 }
1728 }
1729}
1730
a52e1f62 1731static void menu_prepare_bg(int use_game_bg, int use_fg)
2951214e 1732{
2951214e 1733 if (use_game_bg)
1734 {
1735 // darken the active framebuffer
6f748c47 1736 unsigned short *dst = bg_buffer;
a52e1f62 1737 unsigned short *src = use_fg ? psp_video_get_active_fb() : psp_screen;
6f748c47 1738 int i;
1739 for (i = 272; i > 0; i--, dst += 480, src += 512)
1740 menu_darken_bg(dst, src, 480, 1);
5ecedd0c 1741 //memset32_uncached((int *)(bg_buffer + 480*264), 0, 480*8*2/4);
2951214e 1742 }
1743 else
1744 {
1745 // should really only happen once, on startup..
5ecedd0c 1746 memset32_uncached((int *)(void *)bg_buffer, 0, sizeof(bg_buffer)/4);
2951214e 1747 readpng(bg_buffer, "skin/background.png", READPNG_BG);
1748 }
6f748c47 1749 sceKernelDcacheWritebackAll();
2951214e 1750}
1751
1752static void menu_gfx_prepare(void)
1753{
a52e1f62 1754 menu_prepare_bg(rom_data != NULL, 1);
2951214e 1755
1756 menu_draw_begin();
1757 menu_draw_end();
1758}
1759
1760
1761void menu_loop(void)
1762{
1763 menu_gfx_prepare();
1764
1765 menu_loop_root();
1766
1767 menuErrorMsg[0] = 0;
1768}
1769
1770
1771// --------- CD tray close menu ----------
1772
1773static void draw_menu_tray(int menu_sel)
1774{
1775 int tl_x = 70, tl_y = 90, y;
1776
1777 menu_draw_begin();
1778
1779 text_out16(tl_x, 20, "The unit is about to");
1780 text_out16(tl_x, 30, "close the CD tray.");
1781
1782 y = tl_y;
1783 text_out16(tl_x, y, "Load new CD image");
1784 text_out16(tl_x, (y+=10), "Insert nothing");
1785
1786 // draw cursor
1787 text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
1788 // error
1789 if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
1790 menu_draw_end();
1791}
1792
1793
1794int menu_loop_tray(void)
1795{
1796 int menu_sel = 0, menu_sel_max = 1;
1797 unsigned long inp = 0;
1798 char curr_path[PATH_MAX], *selfname;
1799 FILE *tstf;
1800
1801 menu_gfx_prepare();
1802
1803 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1804 {
1805 fclose(tstf);
1806 strcpy(curr_path, currentConfig.lastRomFile);
1807 }
1808 else
1809 {
1810 getcwd(curr_path, PATH_MAX);
1811 }
1812
1813 /* make sure action buttons are not pressed on entering menu */
1814 draw_menu_tray(menu_sel);
16e89bed 1815 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
2951214e 1816
1817 for (;;)
1818 {
1819 draw_menu_tray(menu_sel);
5ecedd0c 1820 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_CIRCLE, 0);
2951214e 1821 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1822 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
16e89bed 1823 if(inp & BTN_CIRCLE) {
2951214e 1824 switch (menu_sel) {
1825 case 0: // select image
1826 selfname = romsel_loop(curr_path);
1827 if (selfname) {
1828 int ret = -1, cd_type;
1829 cd_type = emu_cdCheck(NULL);
1830 if (cd_type > 0)
1831 ret = Insert_CD(romFileName, cd_type == 2);
1832 if (ret != 0) {
1833 sprintf(menuErrorMsg, "Load failed, invalid CD image?");
1834 lprintf("%s\n", menuErrorMsg);
1835 continue;
1836 }
1837 engineState = PGS_RestartRun;
1838 return 1;
1839 }
1840 break;
1841 case 1: // insert nothing
1842 engineState = PGS_RestartRun;
1843 return 0;
1844 }
1845 }
1846 menuErrorMsg[0] = 0; // clear error msg
1847 }
1848}
1849
1850