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