menu.c 51 KB

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