bugfixes in sram memhandlers
[libpicofe.git] / psp / menu.c
CommitLineData
63b796ca 1// (c) Copyright 2007 notaz, All rights reserved.
2951214e 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
c6334564 338static SceIoStat cpstat;
339
2951214e 340static char *romsel_loop(char *curr_path)
341{
342 struct my_dirent **namelist;
703e4c7b 343 int n, iret, sel = 0;
2951214e 344 unsigned long inp = 0;
345 char *ret = NULL, *fname = NULL;
346
347 // is this a dir or a full path?
703e4c7b 348 memset(&cpstat, 0, sizeof(cpstat));
349 iret = sceIoGetstat(curr_path, &cpstat);
350 if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFREG)) { // file
2951214e 351 char *p;
352 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
c6334564 353 if (p > curr_path) {
354 *p = 0;
355 fname = p+1;
356 }
357 else strcpy(curr_path, "ms0:/");
2951214e 358 }
703e4c7b 359 else if (iret >= 0 && (cpstat.st_attr & FIO_SO_IFDIR)); // dir
360 else strcpy(curr_path, "ms0:/"); // something else
2951214e 361
362 n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
363 if (n < 0) {
364 // try root..
703e4c7b 365 n = my_scandir("ms0:/", &namelist, scandir_filter, scandir_cmp);
2951214e 366 if (n < 0) {
367 // oops, we failed
368 lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
369 return NULL;
370 }
371 }
372
373 // try to find sel
374 if (fname != NULL) {
375 int i;
376 for (i = 1; i < n; i++) {
377 if (strcmp(namelist[i]->d_name, fname) == 0) {
378 sel = i - 1;
379 break;
380 }
381 }
382 }
383
384 for (;;)
385 {
386 draw_dirlist(curr_path, namelist, n, sel);
16e89bed 387 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
2951214e 388 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = n-2; }
389 if(inp & BTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
390 if(inp & BTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
391 if(inp & BTN_L) { sel-=24; if (sel < 0) sel = 0; }
392 if(inp & BTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
393 if(inp & BTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
16e89bed 394 if(inp & BTN_CIRCLE) { // enter dir/select
2951214e 395 if (namelist[sel+1]->d_type & DT_REG) {
396 strcpy(romFileName, curr_path);
397 strcat(romFileName, "/");
398 strcat(romFileName, namelist[sel+1]->d_name);
399 ret = romFileName;
400 break;
401 } else if (namelist[sel+1]->d_type & DT_DIR) {
402 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
403 char *p, *newdir = malloc(newlen);
404 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
405 char *start = curr_path;
406 p = start + strlen(start) - 1;
407 while (*p == '/' && p > start) p--;
703e4c7b 408 while (*p != '/' && *p != ':' && p > start) p--;
c6334564 409 if (p <= start || *p == ':') strcpy(newdir, "ms0:/");
2951214e 410 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
411 } else {
412 strcpy(newdir, curr_path);
413 p = newdir + strlen(newdir) - 1;
414 while (*p == '/' && p >= newdir) *p-- = 0;
415 strcat(newdir, "/");
416 strcat(newdir, namelist[sel+1]->d_name);
417 }
418 ret = romsel_loop(newdir);
419 free(newdir);
420 break;
421 }
422 }
16e89bed 423 if(inp & BTN_X) break; // cancel
2951214e 424 }
425
426 if (n > 0) {
427 while(n--) free(namelist[n]);
428 free(namelist);
429 }
430
431 return ret;
432}
433
434// ------------ debug menu ------------
435
436char *debugString(void);
437
438static void draw_debug(void)
439{
440 char *p, *str = debugString();
441 int len, line;
442
443 menu_draw_begin();
444
445 p = str;
446 for (line = 0; line < 24; line++)
447 {
448 while (*p && *p != '\n') p++;
449 len = p - str;
450 if (len > 55) len = 55;
451 smalltext_out16_lim(1, line*10, str, 0xffff, len);
452 if (*p == 0) break;
453 p++; str = p;
454 }
455 menu_draw_end();
456}
457
458static void debug_menu_loop(void)
459{
5ecedd0c 460 int ret = 0;
2951214e 461 draw_debug();
5ecedd0c 462 while (!(ret & (BTN_X|BTN_CIRCLE)))
463 ret = wait_for_input(BTN_X|BTN_CIRCLE, 0);
2951214e 464}
465
466// ------------ patch/gg menu ------------
467
468static void draw_patchlist(int sel)
469{
470 int start, i, pos, active;
471
472 start = 13 - sel;
473
474 menu_draw_begin();
475
476 for (i = 0; i < PicoPatchCount; i++) {
477 pos = start + i;
478 if (pos < 0) continue;
479 if (pos > 26) break;
480 active = PicoPatches[i].active;
481 smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
482 smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
483 }
484 pos = start + i;
485 if (pos < 27) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
486
487 text_out16(5, 130, ">");
488 menu_draw_end();
489}
490
491
492static void patches_menu_loop(void)
493{
494 int menu_sel = 0;
495 unsigned long inp = 0;
496
497 for(;;)
498 {
499 draw_patchlist(menu_sel);
16e89bed 500 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_L|BTN_R|BTN_X|BTN_CIRCLE, 0);
2951214e 501 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
502 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
503 if(inp &(BTN_LEFT|BTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
504 if(inp &(BTN_RIGHT|BTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
16e89bed 505 if(inp & BTN_CIRCLE) { // action
2951214e 506 if (menu_sel < PicoPatchCount)
507 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
508 else return;
509 }
16e89bed 510 if(inp & BTN_X) return;
2951214e 511 }
512
513}
514
515// ------------ savestate loader ------------
516
517static int state_slot_flags = 0;
518
519static void state_check_slots(void)
520{
521 int slot;
522
523 state_slot_flags = 0;
524
525 for (slot = 0; slot < 10; slot++)
526 {
527 if (emu_checkSaveFile(slot))
528 {
529 state_slot_flags |= 1 << slot;
530 }
531 }
532}
533
6f748c47 534static void *get_oldstate_for_preview(void)
535{
536 unsigned char *ptr = malloc(sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram) + sizeof(Pico.video));
537 if (ptr == NULL) return NULL;
538
539 memcpy(ptr, Pico.vram, sizeof(Pico.vram));
540 memcpy(ptr + sizeof(Pico.vram), Pico.cram, sizeof(Pico.cram));
541 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram), Pico.vsram, sizeof(Pico.vsram));
542 memcpy(ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), &Pico.video, sizeof(Pico.video));
543 return ptr;
544}
545
546static void restore_oldstate(void *ptrx)
547{
548 unsigned char *ptr = ptrx;
549 memcpy(Pico.vram, ptr, sizeof(Pico.vram));
550 memcpy(Pico.cram, ptr + sizeof(Pico.vram), sizeof(Pico.cram));
551 memcpy(Pico.vsram, ptr + sizeof(Pico.vram) + sizeof(Pico.cram), sizeof(Pico.vsram));
552 memcpy(&Pico.video,ptr + sizeof(Pico.vram) + sizeof(Pico.cram) + sizeof(Pico.vsram), sizeof(Pico.video));
553 free(ptrx);
554}
555
2951214e 556static void draw_savestate_bg(int slot)
557{
6f748c47 558 void *file, *oldstate;
2951214e 559 char *fname;
560
561 fname = emu_GetSaveFName(1, 0, slot);
562 if (!fname) return;
563
6f748c47 564 oldstate = get_oldstate_for_preview();
565 if (oldstate == NULL) return;
2951214e 566
567 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
568 file = gzopen(fname, "rb");
569 emu_setSaveStateCbs(1);
570 } else {
571 file = fopen(fname, "rb");
572 emu_setSaveStateCbs(0);
573 }
574
575 if (file) {
576 if (PicoMCD & 1) {
577 PicoCdLoadStateGfx(file);
578 } else {
579 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
580 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
581 areaSeek(file, 0x2000, SEEK_CUR);
582 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
583 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
584 areaSeek(file, 0x221a0, SEEK_SET);
585 areaRead(&Pico.video, 1, sizeof(Pico.video), file);
586 }
587 areaClose(file);
588 }
589
590 emu_forcedFrame();
a52e1f62 591 menu_prepare_bg(1, 0);
2951214e 592
6f748c47 593 restore_oldstate(oldstate);
2951214e 594}
595
596static void draw_savestate_menu(int menu_sel, int is_loading)
597{
598 int tl_x = 80+25, tl_y = 16+60, y, i;
599
600 if (state_slot_flags & (1 << menu_sel))
601 draw_savestate_bg(menu_sel);
602 menu_draw_begin();
603
604 text_out16(tl_x, 16+30, is_loading ? "Load state" : "Save state");
605
606 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
607
608 /* draw all 10 slots */
609 y = tl_y;
610 for (i = 0; i < 10; i++, y+=10)
611 {
612 text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
613 }
614 text_out16(tl_x, y, "back");
615
616 menu_draw_end();
617}
618
619static int savestate_menu_loop(int is_loading)
620{
621 static int menu_sel = 10;
622 int menu_sel_max = 10;
623 unsigned long inp = 0;
624
625 state_check_slots();
626
627 for(;;)
628 {
629 draw_savestate_menu(menu_sel, is_loading);
16e89bed 630 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
2951214e 631 if(inp & BTN_UP ) {
632 do {
633 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
634 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
635 }
636 if(inp & BTN_DOWN) {
637 do {
638 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
639 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
640 }
16e89bed 641 if(inp & BTN_CIRCLE) { // save/load
2951214e 642 if (menu_sel < 10) {
643 state_slot = menu_sel;
6f748c47 644 PicoStateProgressCB = emu_msg_cb; /* also suitable for menu */
2951214e 645 if (emu_SaveLoadGame(is_loading, 0)) {
646 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
647 return 1;
648 }
649 return 0;
650 } else return 1;
651 }
16e89bed 652 if(inp & BTN_X) return 1;
2951214e 653 }
654}
655
656// -------------- key config --------------
657
658static char *action_binds(int player_idx, int action_mask)
659{
660 static char strkeys[32*5];
661 int i;
662
663 strkeys[0] = 0;
664 for (i = 0; i < 32; i++) // i is key index
665 {
666 if (currentConfig.KeyBinds[i] & action_mask)
667 {
668 if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
16e89bed 669 if (strkeys[0]) {
670 strcat(strkeys, i >= 28 ? ", " : " + "); // nub "buttons" don't create combos
671 strcat(strkeys, pspKeyNames[i]);
672 break;
673 }
2951214e 674 else strcpy(strkeys, pspKeyNames[i]);
675 }
676 }
677
678 return strkeys;
679}
680
681static void unbind_action(int action)
682{
683 int i;
684
685 for (i = 0; i < 32; i++)
686 currentConfig.KeyBinds[i] &= ~action;
687}
688
689static int count_bound_keys(int action, int pl_idx)
690{
691 int i, keys = 0;
692
693 for (i = 0; i < 32; i++)
694 {
695 if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
696 if (currentConfig.KeyBinds[i] & action) keys++;
697 }
698
699 return keys;
700}
701
702typedef struct { char *name; int mask; } bind_action_t;
703
704static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)
705{
706 int x, y, tl_y = 16+40, i;
707
708 menu_draw_begin();
709 if (player_idx >= 0) {
710 text_out16(80+80, 16+20, "Player %i controls", player_idx + 1);
711 x = 80+80;
712 } else {
713 text_out16(80+80, 16+20, "Emulator controls");
714 x = 80+40;
715 }
716
717 menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
718
719 y = tl_y;
720 for (i = 0; i < opt_cnt; i++, y+=10)
721 text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
722
723 text_out16(x, y, "Done");
724
725 if (sel < opt_cnt) {
16e89bed 726 text_out16(80+30, 220, "Press a button to bind/unbind");
727 text_out16(80+30, 230, "Use SELECT to clear");
728 text_out16(80+30, 240, "To bind UP/DOWN, hold SELECT");
729 text_out16(80+30, 250, "Select \"Done\" to exit");
2951214e 730 } else {
16e89bed 731 text_out16(80+30, 230, "Use Options -> Save cfg");
732 text_out16(80+30, 240, "to save controls");
733 text_out16(80+30, 250, "Press X or O to exit");
2951214e 734 }
735 menu_draw_end();
736}
737
738static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)
739{
740 int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
741 unsigned long inp = 0;
742
743 for (;;)
744 {
745 draw_key_config(opts, opt_cnt, player_idx, sel);
16e89bed 746 inp = wait_for_input(CONFIGURABLE_KEYS|BTN_SELECT, 1);
2951214e 747 if (!(inp & BTN_SELECT)) {
748 prev_select = 0;
749 if(inp & BTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
750 if(inp & BTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
751 }
752 if (sel >= opt_cnt) {
753 if (inp & (BTN_X|BTN_CIRCLE)) break;
754 else continue;
755 }
756 // if we are here, we want to bind/unbind something
757 if ((inp & BTN_SELECT) && !prev_select)
758 unbind_action(opts[sel].mask);
759 prev_select = inp & BTN_SELECT;
760 inp &= CONFIGURABLE_KEYS;
761 inp &= ~BTN_SELECT;
762 for (i = 0; i < 32; i++)
763 if (inp & (1 << i)) {
764 if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
765 currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
766 else currentConfig.KeyBinds[i] ^= opts[sel].mask;
767 if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
768 currentConfig.KeyBinds[i] &= ~(3 << 16);
769 currentConfig.KeyBinds[i] |= player_idx << 16;
770 }
771 }
772 }
773}
774
775static void draw_kc_sel(int menu_sel)
776{
777 int tl_x = 80+25+40, tl_y = 16+60, y;
778
779 y = tl_y;
780 menu_draw_begin();
781 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
782
783 text_out16(tl_x, y, "Player 1");
784 text_out16(tl_x, (y+=10), "Player 2");
785 text_out16(tl_x, (y+=10), "Emulator controls");
786 text_out16(tl_x, (y+=10), "Done");
787
788 menu_draw_end();
789}
790
791
792// PicoPad[] format: MXYZ SACB RLDU
793static bind_action_t ctrl_actions[] =
794{
795 { "UP ", 0x001 },
796 { "DOWN ", 0x002 },
797 { "LEFT ", 0x004 },
798 { "RIGHT ", 0x008 },
799 { "A ", 0x040 },
800 { "B ", 0x010 },
801 { "C ", 0x020 },
802 { "START ", 0x080 },
803 { "MODE ", 0x800 },
804 { "X ", 0x400 },
805 { "Y ", 0x200 },
806 { "Z ", 0x100 },
807};
808
809// player2_flag, ?, ?, ?, ?, ?, ?, menu
810// "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
811// "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
812static bind_action_t emuctrl_actions[] =
813{
814 { "Load State ", 1<<28 },
815 { "Save State ", 1<<27 },
816 { "Prev Save Slot ", 1<<25 },
817 { "Next Save Slot ", 1<<24 },
818 { "Switch Renderer", 1<<26 },
2951214e 819};
820
821static void kc_sel_loop(void)
822{
823 int menu_sel = 3, menu_sel_max = 3;
824 unsigned long inp = 0;
825 int is_6button = currentConfig.PicoOpt & 0x020;
826
827 while (1)
828 {
829 draw_kc_sel(menu_sel);
16e89bed 830 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE, 0);
2951214e 831 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
832 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
16e89bed 833 if (inp & BTN_CIRCLE) {
2951214e 834 switch (menu_sel) {
835 case 0: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 0); return;
836 case 1: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 1); return;
837 case 2: key_config_loop(emuctrl_actions,
838 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;
839 case 3: if (rom_data == NULL) emu_WriteConfig(0); return;
840 default: return;
841 }
842 }
16e89bed 843 if (inp & BTN_X) return;
2951214e 844 }
845}
846
847
848// --------- sega/mega cd options ----------
849
850menu_entry cdopt_entries[] =
851{
852 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1 },
853 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1 },
854 { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1 },
855 { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, &currentConfig.EmuOpt, 0x0400, 0, 0, 1 },
856 { "CDDA audio (using mp3s)", MB_ONOFF, MA_CDOPT_CDDA, &currentConfig.PicoOpt, 0x0800, 0, 0, 1 },
857 { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &currentConfig.PicoOpt, 0x0400, 0, 0, 1 },
858 { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1 },
859 { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, &currentConfig.PicoOpt, 0x8000, 0, 0, 1 },
860 { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&currentConfig.PicoOpt, 0x1000, 0, 0, 1 },
861 { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &currentConfig.PicoOpt, 0x2000, 0, 0, 1 },
862 { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1 },
863};
864
865#define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
866
867
868struct bios_names_t
869{
870 char us[32], eu[32], jp[32];
871};
872
873static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
874{
875 struct bios_names_t *bios_names = param;
876 char ra_buff[16];
877
878 switch (entry->id)
879 {
880 case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
881 case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
882 case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
883 case MA_CDOPT_READAHEAD:
884 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
885 else strcpy(ra_buff, " OFF");
886 text_out16(x, y, "ReadAhead buffer %s", ra_buff);
887 break;
888 default:break;
889 }
890}
891
892static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
893{
894 int tl_x = 80+25, tl_y = 16+60;
895 menu_id selected_id;
896 char ra_buff[16];
897
898 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
899 else strcpy(ra_buff, " OFF");
900
901 menu_draw_begin();
902
903 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
904
905 me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
906
907 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
908 if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
909 (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
910 (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
16e89bed 911 text_out16(tl_x, 250, "Press start to test selected BIOS");
2951214e 912
913 menu_draw_end();
914}
915
916static void cd_menu_loop_options(void)
917{
918 static int menu_sel = 0;
919 int menu_sel_max = 10;
920 unsigned long inp = 0;
921 struct bios_names_t bios_names;
922 menu_id selected_id;
923 char *bios, *p;
924
925 if (emu_findBios(4, &bios)) { // US
60a10527 926 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
927 if (*p == '/') p++;
2951214e 928 strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
929 } else strcpy(bios_names.us, "NOT FOUND");
930
931 if (emu_findBios(8, &bios)) { // EU
60a10527 932 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
933 if (*p == '/') p++;
2951214e 934 strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
935 } else strcpy(bios_names.eu, "NOT FOUND");
936
937 if (emu_findBios(1, &bios)) { // JP
60a10527 938 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
939 if (*p == '/') p++;
2951214e 940 strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
941 } else strcpy(bios_names.jp, "NOT FOUND");
942
a6df06b7 943 menuErrorMsg[0] = 0;
944
945 for (;;)
2951214e 946 {
947 draw_cd_menu_options(menu_sel, &bios_names);
fe9e3b25 948 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE|BTN_START, 0);
2951214e 949 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
950 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
951 selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
952 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
953 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
954 selected_id == MA_CDOPT_READAHEAD) {
955 if (inp & BTN_LEFT) {
956 PicoCDBuffers >>= 1;
957 if (PicoCDBuffers < 64) PicoCDBuffers = 0;
958 } else {
959 if (PicoCDBuffers < 64) PicoCDBuffers = 64;
960 else PicoCDBuffers <<= 1;
961 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
962 }
963 }
964 }
fe9e3b25 965 if (inp & BTN_CIRCLE) // toggleable options
2951214e 966 if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
967 selected_id == MA_CDOPT_DONE) {
968 return;
969 }
fe9e3b25 970 if (inp & BTN_START) {
2951214e 971 switch (selected_id) { // BIOS testers
972 case MA_CDOPT_TESTBIOS_USA:
973 if (emu_findBios(4, &bios)) { // test US
974 strcpy(romFileName, bios);
975 engineState = PGS_ReloadRom;
976 return;
977 }
978 break;
979 case MA_CDOPT_TESTBIOS_EUR:
980 if (emu_findBios(8, &bios)) { // test EU
981 strcpy(romFileName, bios);
982 engineState = PGS_ReloadRom;
983 return;
984 }
985 break;
986 case MA_CDOPT_TESTBIOS_JAP:
987 if (emu_findBios(1, &bios)) { // test JP
988 strcpy(romFileName, bios);
989 engineState = PGS_ReloadRom;
990 return;
991 }
992 break;
993 default:
994 break;
995 }
996 }
16e89bed 997 if (inp & BTN_X) return;
2951214e 998 }
999}
1000
6f748c47 1001// --------- display options ----------
1002
1003menu_entry opt3_entries[] =
1004{
1005 { NULL, MB_NONE, MA_OPT3_SCALE, NULL, 0, 0, 0, 1 },
1006 { NULL, MB_NONE, MA_OPT3_HSCALE32, NULL, 0, 0, 0, 1 },
1007 { NULL, MB_NONE, MA_OPT3_HSCALE40, NULL, 0, 0, 0, 1 },
1008 { NULL, MB_ONOFF, MA_OPT3_FILTERING, &currentConfig.scaling, 1, 0, 0, 1 },
4b8f4f3c 1009 { NULL, MB_NONE, MA_OPT3_VSYNC, NULL, 0, 0, 0, 1 },
6f748c47 1010 { "Set to unscaled centered", MB_NONE, MA_OPT3_PRES_NOSCALE, NULL, 0, 0, 0, 1 },
c6334564 1011 { "Set to 4:3 scaled", MB_NONE, MA_OPT3_PRES_SCALE43, NULL, 0, 0, 0, 1 },
6f748c47 1012 { "Set to fullscreen", MB_NONE, MA_OPT3_PRES_FULLSCR, NULL, 0, 0, 0, 1 },
1013 { "done", MB_NONE, MA_OPT3_DONE, NULL, 0, 0, 0, 1 },
1014};
1015
1016#define OPT3_ENTRY_COUNT (sizeof(opt3_entries) / sizeof(opt3_entries[0]))
1017
1018
1019static void menu_opt3_cust_draw(const menu_entry *entry, int x, int y, void *param)
1020{
1021 switch (entry->id)
1022 {
1023 case MA_OPT3_SCALE:
1024 text_out16(x, y, "Scale factor: %.2f", currentConfig.scale);
1025 break;
1026 case MA_OPT3_HSCALE32:
1027 text_out16(x, y, "Hor. scale (for low res. games): %.2f", currentConfig.hscale32);
1028 break;
1029 case MA_OPT3_HSCALE40:
1030 text_out16(x, y, "Hor. scale (for hi res. games): %.2f", currentConfig.hscale40);
1031 break;
1032 case MA_OPT3_FILTERING:
1033 text_out16(x, y, "Bilinear filtering %s", currentConfig.scaling?"ON":"OFF");
1034 break;
4b8f4f3c 1035 case MA_OPT3_VSYNC: {
1036 char *val = " never";
1037 if (currentConfig.EmuOpt & 0x2000)
1038 val = (currentConfig.EmuOpt & 0x10000) ? "sometimes" : " always";
c93fb19e 1039 text_out16(x, y, "Wait for vsync (slow) %s", val);
4b8f4f3c 1040 break;
1041 }
6f748c47 1042 default: break;
1043 }
1044}
1045
1046static void menu_opt3_preview(int is_32col)
1047{
1048 void *oldstate = NULL;
1049
1050 if (rom_data == NULL || ((Pico.video.reg[12]&1)^1) != is_32col)
1051 {
1052 extern char bgdatac32_start[], bgdatac40_start[];
1053 extern int bgdatac32_size, bgdatac40_size;
1054 void *bgdata = is_32col ? bgdatac32_start : bgdatac40_start;
1055 unsigned long insize = is_32col ? bgdatac32_size : bgdatac40_size, outsize = 65856;
1056 int ret;
6f748c47 1057 ret = uncompress((Bytef *)bg_buffer, &outsize, bgdata, insize);
1058 if (ret == 0)
1059 {
1060 if (rom_data != NULL) oldstate = get_oldstate_for_preview();
1061 memcpy(Pico.vram, bg_buffer, sizeof(Pico.vram));
1062 memcpy(Pico.cram, (char *)bg_buffer + 0x10000, 0x40*2);
1063 memcpy(Pico.vsram, (char *)bg_buffer + 0x10080, 0x40*2);
1064 memcpy(&Pico.video,(char *)bg_buffer + 0x10100, 0x40);
1065 }
1066 else
1067 lprintf("uncompress returned %i\n", ret);
1068 }
1069
5ecedd0c 1070 memset32_uncached(psp_screen, 0, 512*272*2/4);
6f748c47 1071 emu_forcedFrame();
a52e1f62 1072 menu_prepare_bg(1, 0);
6f748c47 1073
1074 if (oldstate) restore_oldstate(oldstate);
1075}
1076
1077static void draw_dispmenu_options(int menu_sel)
1078{
4b8f4f3c 1079 int tl_x = 80, tl_y = 16+50;
6f748c47 1080
1081 menu_draw_begin();
1082
4b8f4f3c 1083 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 316);
6f748c47 1084
1085 me_draw(opt3_entries, OPT3_ENTRY_COUNT, tl_x, tl_y, menu_opt3_cust_draw, NULL);
1086
1087 menu_draw_end();
1088}
1089
1090static void dispmenu_loop_options(void)
1091{
1092 static int menu_sel = 0;
c93fb19e 1093 int menu_sel_max, is_32col = (Pico.video.reg[12]&1)^1;
6f748c47 1094 unsigned long inp = 0;
1095 menu_id selected_id;
1096
1097 menu_sel_max = me_count_enabled(opt3_entries, OPT3_ENTRY_COUNT) - 1;
1098
4b8f4f3c 1099 for (;;)
6f748c47 1100 {
1101 draw_dispmenu_options(menu_sel);
16e89bed 1102 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
6f748c47 1103 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1104 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1105 selected_id = me_index2id(opt3_entries, OPT3_ENTRY_COUNT, menu_sel);
1106 if (selected_id == MA_OPT3_HSCALE40 && is_32col) { is_32col = 0; menu_opt3_preview(is_32col); }
1107 if (selected_id == MA_OPT3_HSCALE32 && !is_32col) { is_32col = 1; menu_opt3_preview(is_32col); }
1108
1109 if (inp & (BTN_LEFT|BTN_RIGHT)) // multi choise
1110 {
1111 float *setting = NULL;
4b8f4f3c 1112 int tmp;
6f748c47 1113 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0);
1114 switch (selected_id) {
1115 case MA_OPT3_SCALE: setting = &currentConfig.scale; break;
1116 case MA_OPT3_HSCALE40: setting = &currentConfig.hscale40; is_32col = 0; break;
1117 case MA_OPT3_HSCALE32: setting = &currentConfig.hscale32; is_32col = 1; break;
1118 case MA_OPT3_FILTERING:menu_opt3_preview(is_32col); break;
4b8f4f3c 1119 case MA_OPT3_VSYNC: tmp = ((currentConfig.EmuOpt>>13)&1) | ((currentConfig.EmuOpt>>15)&2);
1120 tmp = (inp & BTN_LEFT) ? (tmp>>1) : ((tmp<<1)|1);
1121 if (tmp > 3) tmp = 3;
1122 currentConfig.EmuOpt &= ~0x12000;
1123 currentConfig.EmuOpt |= ((tmp&2)<<15) | ((tmp&1)<<13);
1124 break;
6f748c47 1125 default: break;
1126 }
1127 if (setting != NULL) {
1128 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1129 *setting += (inp & BTN_LEFT) ? -0.01 : 0.01;
5ecedd0c 1130 if (*setting <= 0) *setting = 0.01;
6f748c47 1131 menu_opt3_preview(is_32col);
1132 draw_dispmenu_options(menu_sel); // will wait vsync
1133 }
1134 }
1135 }
16e89bed 1136 if (inp & BTN_CIRCLE) { // toggleable options
6f748c47 1137 me_process(opt3_entries, OPT3_ENTRY_COUNT, selected_id, 1);
1138 switch (selected_id) {
1139 case MA_OPT3_DONE:
1140 return;
1141 case MA_OPT3_PRES_NOSCALE:
1142 currentConfig.scale = currentConfig.hscale40 = currentConfig.hscale32 = 1.0;
1143 menu_opt3_preview(is_32col);
1144 break;
c6334564 1145 case MA_OPT3_PRES_SCALE43:
1146 currentConfig.scale = 1.20;
1147 currentConfig.hscale40 = 1.00;
1148 currentConfig.hscale32 = 1.25;
1149 menu_opt3_preview(is_32col);
1150 break;
6f748c47 1151 case MA_OPT3_PRES_FULLSCR:
1152 currentConfig.scale = 1.20;
1153 currentConfig.hscale40 = 1.25;
1154 currentConfig.hscale32 = 1.56;
1155 menu_opt3_preview(is_32col);
1156 break;
1157 case MA_OPT3_FILTERING:
1158 menu_opt3_preview(is_32col);
1159 break;
1160 default: break;
1161 }
1162 }
16e89bed 1163 if (inp & BTN_X) return;
6f748c47 1164 }
1165}
1166
2951214e 1167
1168// --------- advanced options ----------
1169
1170menu_entry opt2_entries[] =
1171{
c6334564 1172 { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &currentConfig.PicoOpt,0x00004, 0, 0, 1 },
1173 { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &currentConfig.PicoOpt,0x00001, 0, 0, 1 },
1174 { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496, &currentConfig.PicoOpt,0x00002, 0, 0, 1 },
1175 { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, &currentConfig.EmuOpt, 0x00008, 0, 0, 1 },
1176 { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, &currentConfig.EmuOpt, 0x00020, 0, 0, 1 },
1177 { "Status line in main menu", MB_ONOFF, MA_OPT2_STATUS_LINE, &currentConfig.EmuOpt, 0x20000, 0, 0, 1 },
1178 { "Disable frame limitter", MB_ONOFF, MA_OPT2_NO_FRAME_LIMIT, &currentConfig.EmuOpt, 0x40000, 0, 0, 1 },
1179 { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1 },
2951214e 1180};
1181
1182#define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
1183
1184
1185static void draw_amenu_options(int menu_sel)
1186{
1187 int tl_x = 80+25, tl_y = 16+50;
1188
1189 menu_draw_begin();
1190
1191 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
1192
1193 me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1194
1195 menu_draw_end();
1196}
1197
1198static void amenu_loop_options(void)
1199{
1200 static int menu_sel = 0;
1201 int menu_sel_max;
1202 unsigned long inp = 0;
1203 menu_id selected_id;
1204
1205 menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
1206
1207 for(;;)
1208 {
1209 draw_amenu_options(menu_sel);
16e89bed 1210 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
2951214e 1211 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1212 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1213 selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
1214 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1215 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0) &&
1216 selected_id == MA_OPT2_GAMMA) {
6f748c47 1217 // TODO?
2951214e 1218 }
1219 }
16e89bed 1220 if (inp & BTN_CIRCLE) { // toggleable options
2951214e 1221 if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
1222 selected_id == MA_OPT2_DONE) {
1223 return;
1224 }
1225 }
16e89bed 1226 if (inp & BTN_X) return;
2951214e 1227 }
1228}
1229
1230// -------------- options --------------
1231
1232
1233menu_entry opt_entries[] =
1234{
1235 { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1 },
2951214e 1236 { "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, &currentConfig.PicoOpt, 0x0040, 0, 0, 1 },
1237 { "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, &currentConfig.PicoOpt, 0x0080, 0, 0, 1 },
1238 { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, &currentConfig.EmuOpt, 0x0002, 0, 0, 1 },
1239 { NULL, MB_RANGE, MA_OPT_FRAMESKIP, &currentConfig.Frameskip, 0, -1, 16, 1 },
1240 { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, &currentConfig.EmuOpt, 0x0004, 0, 0, 1 },
1241 { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1 },
1242 { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &currentConfig.PicoOpt, 0x0020, 0, 0, 1 },
1243 { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1 },
1244 { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, &currentConfig.EmuOpt, 0x0001, 0, 0, 1 },
1245 { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1 },
1246 { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1 },
2b90fc61 1247 { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1 },
6f748c47 1248 { "[Display options]", MB_NONE, MA_OPT_DISP_OPTS, NULL, 0, 0, 0, 1 },
2951214e 1249 { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1 },
6f748c47 1250 { "[Advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1 },
2951214e 1251 { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1 },
1252 { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1 },
1253 { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1 },
1254};
1255
1256#define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
1257
1258
1259static const char *region_name(unsigned int code)
1260{
1261 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };
1262 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };
1263 int u, i = 0;
1264 if (code) {
1265 code <<= 1;
1266 while((code >>= 1)) i++;
1267 if (i > 4) return "unknown";
1268 return names[i];
1269 } else {
1270 static char name[24];
1271 strcpy(name, "Auto:");
1272 for (u = 0; u < 3; u++) {
1273 i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;
1274 while((code >>= 1)) i++;
1275 strcat(name, names_short[i]);
1276 }
1277 return name;
1278 }
1279}
1280
1281
1282static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
1283{
1284 char *str, str24[24];
1285
1286 switch (entry->id)
1287 {
1288 case MA_OPT_RENDERER:
1289 if (currentConfig.PicoOpt&0x10)
2aea862d 1290 str = "fast";
2951214e 1291 else if (currentConfig.EmuOpt&0x80)
2aea862d 1292 str = "accurate";
2951214e 1293 else
2aea862d 1294 str = " 8bit accurate"; // n/a
1295 text_out16(x, y, "Renderer: %s", str);
2951214e 1296 break;
1297 case MA_OPT_FRAMESKIP:
1298 if (currentConfig.Frameskip < 0)
1299 strcpy(str24, "Auto");
1300 else sprintf(str24, "%i", currentConfig.Frameskip);
1301 text_out16(x, y, "Frameskip %s", str24);
1302 break;
1303 case MA_OPT_SOUND_QUALITY:
1304 str = (currentConfig.PicoOpt&0x08)?"stereo":"mono";
1305 text_out16(x, y, "Sound Quality: %5iHz %s", currentConfig.PsndRate, str);
1306 break;
1307 case MA_OPT_REGION:
1308 text_out16(x, y, "Region: %s", region_name(currentConfig.PicoRegion));
1309 break;
1310 case MA_OPT_CONFIRM_STATES:
1311 switch ((currentConfig.EmuOpt >> 9) & 5) {
1312 default: str = "OFF"; break;
1313 case 1: str = "writes"; break;
1314 case 4: str = "loads"; break;
1315 case 5: str = "both"; break;
1316 }
1317 text_out16(x, y, "Confirm savestate %s", str);
1318 break;
2b90fc61 1319 case MA_OPT_CPU_CLOCKS:
1320 text_out16(x, y, "CPU/bus clock %3i/%3iMHz", currentConfig.CPUclock, currentConfig.CPUclock/2);
1321 break;
2951214e 1322 case MA_OPT_SAVECFG:
1323 str24[0] = 0;
1324 if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
1325 text_out16(x, y, "Save cfg as default%s", str24);
1326 break;
1327 case MA_OPT_LOADCFG:
1328 text_out16(x, y, "Load cfg from profile %i", config_slot);
1329 break;
1330 default:
1331 lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
1332 break;
1333 }
1334}
1335
1336
2951214e 1337static void draw_menu_options(int menu_sel)
1338{
1339 int tl_x = 80+25, tl_y = 16+24;
1340
1341 menu_draw_begin();
1342
1343 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
1344
1345 me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
1346
1347 menu_draw_end();
1348}
1349
1350static int sndrate_prevnext(int rate, int dir)
1351{
1352 int i, rates[] = { 11025, 22050, 44100 };
1353
1354 for (i = 0; i < 5; i++)
1355 if (rates[i] == rate) break;
1356
1357 i += dir ? 1 : -1;
1358 if (i > 2) return dir ? 44100 : 22050;
1359 if (i < 0) return dir ? 22050 : 11025;
1360 return rates[i];
1361}
1362
1363static void region_prevnext(int right)
1364{
1365 // jp_ntsc=1, jp_pal=2, usa=4, eu=8
1366 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
1367 int i;
1368 if (right) {
1369 if (!currentConfig.PicoRegion) {
1370 for (i = 0; i < 6; i++)
1371 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1372 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
1373 else currentConfig.PicoRegion=1;
1374 }
1375 else currentConfig.PicoRegion<<=1;
1376 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;
1377 } else {
1378 if (!currentConfig.PicoRegion) {
1379 for (i = 0; i < 6; i++)
1380 if (rgn_orders[i] == PicoAutoRgnOrder) break;
1381 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
1382 }
1383 else currentConfig.PicoRegion>>=1;
1384 }
1385}
1386
1387static void menu_options_save(void)
1388{
1389 PicoOpt = currentConfig.PicoOpt;
1390 PsndRate = currentConfig.PsndRate;
1391 PicoRegionOverride = currentConfig.PicoRegion;
1392 if (!(PicoOpt & 0x20)) {
1393 // unbind XYZ MODE, just in case
1394 unbind_action(0xf00);
1395 }
1396}
1397
1398static int menu_loop_options(void)
1399{
1400 static int menu_sel = 0;
1401 int menu_sel_max, ret;
1402 unsigned long inp = 0;
1403 menu_id selected_id;
1404
1405 currentConfig.PicoOpt = PicoOpt;
1406 currentConfig.PsndRate = PsndRate;
1407 currentConfig.PicoRegion = PicoRegionOverride;
1408
1409 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);
1410 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1411 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1412 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1413
1414 while (1)
1415 {
1416 draw_menu_options(menu_sel);
16e89bed 1417 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
2951214e 1418 if (inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1419 if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1420 selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
1421 if (inp & (BTN_LEFT|BTN_RIGHT)) { // multi choise
1422 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&BTN_RIGHT) ? 1 : 0)) {
1423 switch (selected_id) {
1424 case MA_OPT_RENDERER:
2aea862d 1425 if ((currentConfig.PicoOpt&0x10) || !(currentConfig.EmuOpt &0x80)) {
1426 currentConfig.PicoOpt&= ~0x10;
1427 currentConfig.EmuOpt |= 0x80;
2951214e 1428 } else {
2aea862d 1429 currentConfig.PicoOpt|= 0x10;
1430 currentConfig.EmuOpt &= ~0x80;
2951214e 1431 }
1432 break;
1433 case MA_OPT_SOUND_QUALITY:
2aea862d 1434 currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & BTN_RIGHT);
2951214e 1435 break;
1436 case MA_OPT_REGION:
1437 region_prevnext(inp & BTN_RIGHT);
1438 break;
1439 case MA_OPT_CONFIRM_STATES: {
1440 int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
1441 n += (inp & BTN_LEFT) ? -1 : 1;
1442 if (n < 0) n = 0; else if (n > 3) n = 3;
1443 n |= n << 1; n &= ~2;
1444 currentConfig.EmuOpt &= ~0xa00;
1445 currentConfig.EmuOpt |= n << 9;
1446 break;
1447 }
1448 case MA_OPT_SAVE_SLOT:
1449 if (inp & BTN_RIGHT) {
1450 state_slot++; if (state_slot > 9) state_slot = 0;
1451 } else {state_slot--; if (state_slot < 0) state_slot = 9;
1452 }
1453 break;
2b90fc61 1454 case MA_OPT_CPU_CLOCKS:
1455 while ((inp = psp_pad_read(0)) & (BTN_LEFT|BTN_RIGHT)) {
1456 currentConfig.CPUclock += (inp & BTN_LEFT) ? -1 : 1;
1457 if (currentConfig.CPUclock < 19) currentConfig.CPUclock = 19;
1458 if (currentConfig.CPUclock > 333) currentConfig.CPUclock = 333;
1459 draw_menu_options(menu_sel); // will wait vsync
1460 }
1461 break;
2951214e 1462 case MA_OPT_SAVECFG:
1463 case MA_OPT_SAVECFG_GAME:
1464 case MA_OPT_LOADCFG:
1465 config_slot += (inp&BTN_RIGHT) ? 1 : -1;
1466 if (config_slot > 9) config_slot = 0;
1467 if (config_slot < 0) config_slot = 9;
1468 me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
1469 menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
1470 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1471 break;
1472 default:
1473 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1474 break;
1475 }
1476 }
1477 }
16e89bed 1478 if (inp & BTN_CIRCLE) {
2951214e 1479 if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
1480 {
1481 switch (selected_id)
1482 {
6f748c47 1483 case MA_OPT_DISP_OPTS:
1484 dispmenu_loop_options();
1485 break;
2951214e 1486 case MA_OPT_SCD_OPTS:
1487 cd_menu_loop_options();
1488 if (engineState == PGS_ReloadRom)
1489 return 0; // test BIOS
1490 break;
1491 case MA_OPT_ADV_OPTS:
1492 amenu_loop_options();
1493 break;
1494 case MA_OPT_SAVECFG: // done (update and write)
1495 menu_options_save();
1496 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
1497 else strcpy(menuErrorMsg, "failed to write config");
1498 return 1;
1499 case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
1500 menu_options_save();
1501 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
1502 else strcpy(menuErrorMsg, "failed to write config");
1503 return 1;
1504 case MA_OPT_LOADCFG:
1505 ret = emu_ReadConfig(1, 1);
1506 if (!ret) ret = emu_ReadConfig(0, 1);
1507 if (ret) strcpy(menuErrorMsg, "config loaded");
1508 else strcpy(menuErrorMsg, "failed to load config");
1509 return 1;
1510 default:
1511 //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
1512 break;
1513 }
1514 }
1515 }
16e89bed 1516 if(inp & BTN_X) {
2951214e 1517 menu_options_save();
1518 return 0; // done (update, no write)
1519 }
1520 }
1521}
1522
1523// -------------- credits --------------
1524
1525static void draw_menu_credits(void)
1526{
1527 int tl_x = 80+15, tl_y = 16+64, y;
1528 menu_draw_begin();
1529
1530 text_out16(tl_x, 16+20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");
1531
1532 y = tl_y;
1533 text_out16(tl_x, y, "Credits:");
4b8f4f3c 1534 text_out16(tl_x, (y+=10), "fDave: base code of PicoDrive");
1535 text_out16(tl_x, (y+=10), "Chui: Fame/C");
1536 text_out16(tl_x, (y+=10), "NJ: CZ80");
2951214e 1537 text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
2951214e 1538 text_out16(tl_x, (y+=10), "Stephane Dallongeville:");
4b8f4f3c 1539 text_out16(tl_x, (y+=10), " Gens code, base of Fame/C, CZ80");
1540 text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");
2951214e 1541 text_out16(tl_x, (y+=10), "Haze: Genesis hw info");
4b8f4f3c 1542 text_out16(tl_x, (y+=10), "ps2dev.org people: PSP SDK/code");
2951214e 1543 text_out16(tl_x, (y+=10), "ketchupgun: skin design");
1544
1545 menu_draw_end();
1546}
1547
1548
1549// -------------- root menu --------------
1550
1551menu_entry main_entries[] =
1552{
1553 { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
1554 { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
1555 { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
1556 { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
1557 { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
1558 { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
1559 { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
1560 { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
1561 { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
1562 { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
1563};
1564
1565#define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
1566
1567static void draw_menu_root(int menu_sel)
1568{
16e89bed 1569 const int tl_x = 86+70, tl_y = 16+70;
1570 char *stat = NULL;
2951214e 1571
1572 menu_draw_begin();
1573
16e89bed 1574 if ((currentConfig.EmuOpt&0x20000) && (stat = psp_get_status_line()))
1575 text_out16(287, 12, "%s", stat);
1576
1577 text_out16(tl_x, 48, "PicoDrive v" VERSION);
2951214e 1578
1579 menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
1580
1581 me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
1582
1583 // error
6f748c47 1584 if (menuErrorMsg[0])
1585 text_out16(10, 252, menuErrorMsg);
2951214e 1586 menu_draw_end();
1587}
1588
1589
1590static void menu_loop_root(void)
1591{
1592 static int menu_sel = 0;
1593 int ret, menu_sel_max;
1594 unsigned long inp = 0;
1595
1596 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_data != NULL);
1597 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_data != NULL);
1598 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_data != NULL);
1599 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_data != NULL);
1600 me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
1601
1602 menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
1603 if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
1604
a6df06b7 1605 // mp3 errors?
1606 if (mp3_last_error != 0) {
1607 if (mp3_last_error == -1)
1608 sprintf(menuErrorMsg, "Unsupported mp3 format, use 44kHz stereo");
1609 else sprintf(menuErrorMsg, "mp3 init failed, code %08x", mp3_last_error);
1610 mp3_last_error = 0;
1611 }
1612
2951214e 1613 /* make sure action buttons are not pressed on entering menu */
1614 draw_menu_root(menu_sel);
1615
703e4c7b 1616 while (psp_pad_read(1) & (BTN_X|BTN_CIRCLE|BTN_SELECT)) psp_msleep(50);
2951214e 1617
1618 for (;;)
1619 {
1620 draw_menu_root(menu_sel);
16e89bed 1621 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_X|BTN_CIRCLE|BTN_SELECT|BTN_L|BTN_R, 0);
2951214e 1622 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1623 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
1624 if((inp & (BTN_L|BTN_R)) == (BTN_L|BTN_R)) debug_menu_loop();
16e89bed 1625 if( inp & (BTN_SELECT|BTN_X)) {
2951214e 1626 if (rom_data) {
16e89bed 1627 while (psp_pad_read(1) & (BTN_SELECT|BTN_X)) psp_msleep(50); // wait until released
2951214e 1628 engineState = PGS_Running;
1629 break;
1630 }
1631 }
16e89bed 1632 if(inp & BTN_CIRCLE) {
a6df06b7 1633 menuErrorMsg[0] = 0; // clear error msg
2951214e 1634 switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
1635 {
1636 case MA_MAIN_RESUME_GAME:
1637 if (rom_data) {
16e89bed 1638 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
2951214e 1639 engineState = PGS_Running;
1640 return;
1641 }
1642 break;
1643 case MA_MAIN_SAVE_STATE:
1644 if (rom_data) {
1645 if(savestate_menu_loop(0))
1646 continue;
1647 engineState = PGS_Running;
1648 return;
1649 }
1650 break;
1651 case MA_MAIN_LOAD_STATE:
1652 if (rom_data) {
1653 if(savestate_menu_loop(1))
1654 continue;
1655 engineState = PGS_Running;
1656 return;
1657 }
1658 break;
1659 case MA_MAIN_RESET_GAME:
1660 if (rom_data) {
1661 emu_ResetGame();
1662 engineState = PGS_Running;
1663 return;
1664 }
1665 break;
1666 case MA_MAIN_LOAD_ROM:
1667 {
1668 char curr_path[PATH_MAX], *selfname;
1669 FILE *tstf;
1670 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1671 {
1672 fclose(tstf);
1673 strcpy(curr_path, currentConfig.lastRomFile);
1674 }
1675 else
1676 getcwd(curr_path, PATH_MAX);
1677 selfname = romsel_loop(curr_path);
1678 if (selfname) {
1679 lprintf("selected file: %s\n", selfname);
1680 engineState = PGS_ReloadRom;
1681 return;
1682 }
1683 break;
1684 }
1685 case MA_MAIN_OPTIONS:
1686 ret = menu_loop_options();
1687 if (ret == 1) continue; // status update
1688 if (engineState == PGS_ReloadRom)
1689 return; // BIOS test
1690 break;
1691 case MA_MAIN_CONTROLS:
1692 kc_sel_loop();
1693 break;
1694 case MA_MAIN_CREDITS:
1695 draw_menu_credits();
1696 psp_msleep(500);
16e89bed 1697 inp = 0;
1698 while (!(inp & (BTN_X|BTN_CIRCLE)))
1699 inp = wait_for_input(BTN_X|BTN_CIRCLE, 0);
2951214e 1700 break;
1701 case MA_MAIN_EXIT:
1702 engineState = PGS_Quit;
1703 return;
1704 case MA_MAIN_PATCHES:
1705 if (rom_data && PicoPatches) {
1706 patches_menu_loop();
1707 PicoPatchApply();
1708 strcpy(menuErrorMsg, "Patches applied");
1709 continue;
1710 }
1711 break;
1712 default:
1713 lprintf("%s: something unknown selected\n", __FUNCTION__);
1714 break;
1715 }
1716 }
2951214e 1717 }
1718}
1719
1720// warning: alignment
1721static void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
1722{
1723 unsigned int *dest = dst;
1724 const unsigned int *srce = src;
1725 pixels /= 2;
1726 if (darker)
1727 {
1728 while (pixels--)
1729 {
1730 unsigned int p = *srce++;
1731 *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
1732 }
1733 }
1734 else
1735 {
1736 while (pixels--)
1737 {
1738 unsigned int p = *srce++;
1739 *dest++ = (p&0xf79ef79e)>>1;
1740 }
1741 }
1742}
1743
a52e1f62 1744static void menu_prepare_bg(int use_game_bg, int use_fg)
2951214e 1745{
2951214e 1746 if (use_game_bg)
1747 {
1748 // darken the active framebuffer
6f748c47 1749 unsigned short *dst = bg_buffer;
a52e1f62 1750 unsigned short *src = use_fg ? psp_video_get_active_fb() : psp_screen;
6f748c47 1751 int i;
1752 for (i = 272; i > 0; i--, dst += 480, src += 512)
1753 menu_darken_bg(dst, src, 480, 1);
5ecedd0c 1754 //memset32_uncached((int *)(bg_buffer + 480*264), 0, 480*8*2/4);
2951214e 1755 }
1756 else
1757 {
1758 // should really only happen once, on startup..
5ecedd0c 1759 memset32_uncached((int *)(void *)bg_buffer, 0, sizeof(bg_buffer)/4);
2951214e 1760 readpng(bg_buffer, "skin/background.png", READPNG_BG);
1761 }
6f748c47 1762 sceKernelDcacheWritebackAll();
2951214e 1763}
1764
1765static void menu_gfx_prepare(void)
1766{
a52e1f62 1767 menu_prepare_bg(rom_data != NULL, 1);
2951214e 1768
1769 menu_draw_begin();
1770 menu_draw_end();
1771}
1772
1773
1774void menu_loop(void)
1775{
1776 menu_gfx_prepare();
1777
1778 menu_loop_root();
1779
1780 menuErrorMsg[0] = 0;
1781}
1782
1783
1784// --------- CD tray close menu ----------
1785
1786static void draw_menu_tray(int menu_sel)
1787{
1788 int tl_x = 70, tl_y = 90, y;
1789
1790 menu_draw_begin();
1791
1792 text_out16(tl_x, 20, "The unit is about to");
1793 text_out16(tl_x, 30, "close the CD tray.");
1794
1795 y = tl_y;
1796 text_out16(tl_x, y, "Load new CD image");
1797 text_out16(tl_x, (y+=10), "Insert nothing");
1798
1799 // draw cursor
1800 text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
1801 // error
1802 if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
1803 menu_draw_end();
1804}
1805
1806
1807int menu_loop_tray(void)
1808{
1809 int menu_sel = 0, menu_sel_max = 1;
1810 unsigned long inp = 0;
1811 char curr_path[PATH_MAX], *selfname;
1812 FILE *tstf;
1813
1814 menu_gfx_prepare();
1815
1816 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
1817 {
1818 fclose(tstf);
1819 strcpy(curr_path, currentConfig.lastRomFile);
1820 }
1821 else
1822 {
1823 getcwd(curr_path, PATH_MAX);
1824 }
1825
1826 /* make sure action buttons are not pressed on entering menu */
1827 draw_menu_tray(menu_sel);
16e89bed 1828 while (psp_pad_read(1) & BTN_CIRCLE) psp_msleep(50);
2951214e 1829
1830 for (;;)
1831 {
1832 draw_menu_tray(menu_sel);
5ecedd0c 1833 inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_CIRCLE, 0);
2951214e 1834 if(inp & BTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
1835 if(inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
16e89bed 1836 if(inp & BTN_CIRCLE) {
2951214e 1837 switch (menu_sel) {
1838 case 0: // select image
1839 selfname = romsel_loop(curr_path);
1840 if (selfname) {
1841 int ret = -1, cd_type;
1842 cd_type = emu_cdCheck(NULL);
1843 if (cd_type > 0)
1844 ret = Insert_CD(romFileName, cd_type == 2);
1845 if (ret != 0) {
1846 sprintf(menuErrorMsg, "Load failed, invalid CD image?");
1847 lprintf("%s\n", menuErrorMsg);
1848 continue;
1849 }
1850 engineState = PGS_RestartRun;
1851 return 1;
1852 }
1853 break;
1854 case 1: // insert nothing
1855 engineState = PGS_RestartRun;
1856 return 0;
1857 }
1858 }
1859 menuErrorMsg[0] = 0; // clear error msg
1860 }
1861}
1862
1863