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