menu.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2006-2008
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. // don't like to use loads of #ifdefs, so duplicating GP2X code
  9. // horribly instead
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <wchar.h>
  14. #include <unistd.h> // for getcwd cegcc implementation
  15. #include <dirent.h> // for opendir
  16. #include <windows.h>
  17. #include "kgsdk/Framework.h"
  18. #include "kgsdk/Framework2D.h"
  19. #include "giz.h"
  20. #include "emu.h"
  21. #include "menu.h"
  22. #include "../common/arm_utils.h"
  23. #include "../common/menu.h"
  24. #include "../common/emu.h"
  25. #include "../common/readpng.h"
  26. #include "../common/input.h"
  27. #include "../common/version.h"
  28. #include <pico/pico_int.h>
  29. #include <pico/patch.h>
  30. #include <zlib.h>
  31. #define gizKeyUnkn "???"
  32. const char * const keyNames[] = {
  33. "LEFT", "RIGHT", "UP", "DOWN", "STOP", "PLAY", "FORWARD", "REWIND",
  34. "L", "R", "HOME", "VOLUME", "BRIGHTNESS", "ALARM", "POWER", gizKeyUnkn,
  35. gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn,
  36. gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn
  37. };
  38. static unsigned char *bg_buffer = gfx_buffer + 321*240*2;
  39. unsigned char *menu_screen = gfx_buffer; /* draw here and blit later, to avoid flicker */
  40. void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
  41. static void menu_prepare_bg(int use_game_bg);
  42. void menu_draw_begin(int use_bgbuff)
  43. {
  44. if (use_bgbuff)
  45. memcpy((int *)menu_screen, (int *)bg_buffer, 321*240*2);
  46. }
  47. void menu_draw_end(void)
  48. {
  49. giz_screen = fb_lock(0);
  50. if (giz_screen == NULL)
  51. {
  52. lprintf("%s: Framework2D_LockBuffer() returned NULL\n", __FUNCTION__);
  53. return;
  54. }
  55. memcpy(giz_screen, (int *)menu_screen, 321*240*2);
  56. fb_unlock();
  57. giz_screen = NULL;
  58. fb_flip();
  59. }
  60. // --------- loading ROM screen ----------
  61. static void load_progress_cb(int percent)
  62. {
  63. int ln, len = percent * 320 / 100;
  64. unsigned short *dst;
  65. menu_draw_begin(0);
  66. dst = (unsigned short *)menu_screen + 321*20;
  67. if (len > 320) len = 320;
  68. for (ln = 10; ln > 0; ln--, dst += 321)
  69. memset(dst, 0xff, len*2);
  70. menu_draw_end();
  71. }
  72. void menu_romload_prepare(const char *rom_name)
  73. {
  74. const char *p = rom_name + strlen(rom_name);
  75. while (p > rom_name && *p != '/') p--;
  76. if (!rom_loaded)
  77. memset(menu_screen, 0, 321*240*2);
  78. menu_draw_begin(1);
  79. smalltext_out16(1, 1, "Loading", 0xffff);
  80. smalltext_out16_lim(1, 10, p, 0xffff, 53);
  81. menu_draw_end();
  82. PicoCartLoadProgressCB = load_progress_cb;
  83. }
  84. void menu_romload_end(void)
  85. {
  86. PicoCartLoadProgressCB = NULL;
  87. menu_draw_begin(0);
  88. smalltext_out16(1, 30, "Starting emulation...", 0xffff);
  89. menu_draw_end();
  90. }
  91. // -------------- ROM selector --------------
  92. #define DT_DIR 1
  93. #define DT_REG 2
  94. struct my_dirent
  95. {
  96. unsigned char d_type;
  97. char d_name[0];
  98. };
  99. // rrrr rggg gggb bbbb
  100. #if 1
  101. static unsigned short file2color(const char *fname)
  102. {
  103. const char *ext = fname + strlen(fname) - 3;
  104. static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso" };
  105. static const char *other_exts[] = { "gmv", "pat" };
  106. int i;
  107. if (ext < fname) ext = fname;
  108. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  109. if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff;
  110. for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
  111. if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
  112. return 0xffff;
  113. }
  114. static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
  115. {
  116. int start, i, pos;
  117. start = 12 - sel;
  118. n--; // exclude current dir (".")
  119. menu_draw_begin(1);
  120. if (!rom_loaded) {
  121. menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
  122. }
  123. menu_darken_bg((char *)menu_screen + 321*120*2, (char *)menu_screen + 321*120*2, 321*8, 0);
  124. if(start - 2 >= 0)
  125. smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
  126. for (i = 0; i < n; i++) {
  127. pos = start + i;
  128. if (pos < 0) continue;
  129. if (pos > 23) break;
  130. if (namelist[i+1]->d_type == DT_DIR) {
  131. smalltext_out16_lim(14, pos*10, "/", 0xfff6, 1);
  132. smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xfff6, 53-3);
  133. } else {
  134. unsigned short color = file2color(namelist[i+1]->d_name);
  135. smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 53-2);
  136. }
  137. }
  138. text_out16(5, 120, ">");
  139. menu_draw_end();
  140. }
  141. static int scandir_cmp(const void *p1, const void *p2)
  142. {
  143. struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;
  144. if ((*d1)->d_type == (*d2)->d_type) return strcasecmp((*d1)->d_name, (*d2)->d_name);
  145. if ((*d1)->d_type == DT_DIR) return -1; // put before
  146. if ((*d2)->d_type == DT_DIR) return 1;
  147. return strcasecmp((*d1)->d_name, (*d2)->d_name);
  148. }
  149. static char *filter_exts[] = {
  150. ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
  151. ".jpg", ".cue", ".exe", ".dll"
  152. };
  153. static int scandir_filter(const struct my_dirent *ent)
  154. {
  155. const char *p;
  156. int i;
  157. if (ent == NULL || ent->d_name == NULL) return 0;
  158. if (strlen(ent->d_name) < 5) return 1;
  159. p = ent->d_name + strlen(ent->d_name) - 4;
  160. for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
  161. {
  162. if (strcasecmp(p, filter_exts[i]) == 0) return 0;
  163. }
  164. return 1;
  165. }
  166. static int cstr2wstr(wchar_t *dst, const char *src)
  167. {
  168. int i, len;
  169. for (i = len = strlen(src); i >= 0; i--)
  170. dst[i] = src[i];
  171. return len;
  172. }
  173. static void wstr2cstr(char *dst, const wchar_t *src)
  174. {
  175. int i;
  176. for (i = wcslen(src); i >= 0; i--)
  177. dst[i] = src[i];
  178. }
  179. static int my_scandir(const char *dir, struct my_dirent ***namelist_out,
  180. int(*filter)(const struct my_dirent *),
  181. int(*compar)(const void *, const void *))
  182. {
  183. wchar_t *wdir;
  184. int i, len, ret = -1, name_alloc = 4, name_count = 0;
  185. struct my_dirent **namelist = NULL, *ent;
  186. WIN32_FIND_DATA finddata;
  187. HANDLE fh = INVALID_HANDLE_VALUE;
  188. BOOL bRet;
  189. wdir = malloc(sizeof(wdir[0]) * MAX_PATH);
  190. if (wdir == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
  191. namelist = malloc(sizeof(*namelist) * name_alloc);
  192. if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
  193. // try to read first..
  194. len = cstr2wstr(wdir, dir);
  195. for (i = 0; i < len; i++)
  196. if (wdir[i] == '/') wdir[i] = '\\';
  197. if (wdir[len-1] != '\\') wdir[len++] = '\\';
  198. wdir[len++] = '*';
  199. wdir[len++] = 0;
  200. /*if (wdir[len-1] == '\\') wdir[len-1] = 0;*/
  201. bRet = 1;
  202. fh = FindFirstFile(wdir, &finddata);
  203. if (fh == INVALID_HANDLE_VALUE)
  204. {
  205. if (GetLastError() != ERROR_NO_MORE_FILES)
  206. {
  207. lprintf("FindFirstFile(\"%s\") failed with %lu\n", dir, GetLastError());
  208. goto fail;
  209. }
  210. bRet = 0;
  211. }
  212. // add "." and ".."
  213. ent = malloc(sizeof(*ent) + 3);
  214. ent->d_type = DT_DIR; strcpy(ent->d_name, ".");
  215. namelist[name_count++] = ent;
  216. ent = malloc(sizeof(*ent) + 3);
  217. ent->d_type = DT_DIR; strcpy(ent->d_name, "..");
  218. namelist[name_count++] = ent;
  219. while (bRet)
  220. {
  221. ent = malloc(sizeof(*ent) + wcslen(finddata.cFileName) + 1);
  222. ent->d_type = (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
  223. wstr2cstr(ent->d_name, finddata.cFileName);
  224. if (filter == NULL || filter(ent))
  225. namelist[name_count++] = ent;
  226. else free(ent);
  227. if (name_count >= name_alloc)
  228. {
  229. void *tmp;
  230. name_alloc *= 2;
  231. tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
  232. if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
  233. namelist = tmp;
  234. }
  235. bRet = FindNextFile(fh, &finddata);
  236. }
  237. // sort
  238. if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
  239. // all done.
  240. ret = name_count;
  241. *namelist_out = namelist;
  242. goto end;
  243. fail:
  244. if (namelist != NULL)
  245. {
  246. while (name_count--)
  247. free(namelist[name_count]);
  248. free(namelist);
  249. }
  250. end:
  251. if (fh != INVALID_HANDLE_VALUE) FindClose(fh);
  252. if (wdir != NULL) free(wdir);
  253. return ret;
  254. }
  255. static char *romsel_loop(char *curr_path)
  256. {
  257. struct my_dirent **namelist;
  258. DIR *dir;
  259. int n, sel = 0;
  260. unsigned long inp = 0;
  261. char *ret = NULL, *fname = NULL;
  262. // is this a dir or a full path?
  263. if ((dir = opendir(curr_path))) {
  264. closedir(dir);
  265. } else {
  266. char *p;
  267. for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
  268. *p = 0;
  269. fname = p+1;
  270. }
  271. n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
  272. if (n < 0) {
  273. // try root..
  274. n = my_scandir("/", &namelist, scandir_filter, scandir_cmp);
  275. if (n < 0) {
  276. // oops, we failed
  277. lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
  278. return NULL;
  279. }
  280. }
  281. // try to find sel
  282. if (fname != NULL) {
  283. int i;
  284. for (i = 1; i < n; i++) {
  285. if (strcmp(namelist[i]->d_name, fname) == 0) {
  286. sel = i - 1;
  287. break;
  288. }
  289. }
  290. }
  291. for (;;)
  292. {
  293. draw_dirlist(curr_path, namelist, n, sel);
  294. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_PLAY|PBTN_STOP);
  295. if(inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
  296. if(inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
  297. if(inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
  298. if(inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
  299. if(inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
  300. if(inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
  301. if(inp & PBTN_PLAY) { // enter dir/select
  302. if (namelist[sel+1]->d_type == DT_REG) {
  303. strcpy(romFileName, curr_path);
  304. strcat(romFileName, "/");
  305. strcat(romFileName, namelist[sel+1]->d_name);
  306. ret = romFileName;
  307. break;
  308. } else if (namelist[sel+1]->d_type == DT_DIR) {
  309. int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
  310. char *p, *newdir = malloc(newlen);
  311. if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
  312. char *start = curr_path;
  313. p = start + strlen(start) - 1;
  314. while (*p == '/' && p > start) p--;
  315. while (*p != '/' && p > start) p--;
  316. if (p <= start) strcpy(newdir, "/");
  317. else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
  318. } else {
  319. strcpy(newdir, curr_path);
  320. p = newdir + strlen(newdir) - 1;
  321. while (*p == '/' && p >= newdir) *p-- = 0;
  322. strcat(newdir, "/");
  323. strcat(newdir, namelist[sel+1]->d_name);
  324. }
  325. ret = romsel_loop(newdir);
  326. free(newdir);
  327. break;
  328. }
  329. }
  330. if(inp & PBTN_STOP) break; // cancel
  331. }
  332. if (n > 0) {
  333. while(n--) free(namelist[n]);
  334. free(namelist);
  335. }
  336. return ret;
  337. }
  338. #else
  339. static char *romsel_loop(char *curr_path)
  340. {
  341. return NULL;
  342. }
  343. #endif
  344. // ------------ patch/gg menu ------------
  345. static void draw_patchlist(int sel)
  346. {
  347. int start, i, pos, active;
  348. start = 12 - sel;
  349. menu_draw_begin(1);
  350. for (i = 0; i < PicoPatchCount; i++) {
  351. pos = start + i;
  352. if (pos < 0) continue;
  353. if (pos > 23) break;
  354. active = PicoPatches[i].active;
  355. smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
  356. smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
  357. }
  358. pos = start + i;
  359. if (pos < 24) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
  360. text_out16(5, 120, ">");
  361. menu_draw_end();
  362. }
  363. static void patches_menu_loop(void)
  364. {
  365. int menu_sel = 0;
  366. unsigned long inp = 0;
  367. for(;;)
  368. {
  369. draw_patchlist(menu_sel);
  370. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_PLAY|PBTN_STOP);
  371. if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
  372. if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
  373. if(inp &(PBTN_LEFT|PBTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
  374. if(inp &(PBTN_RIGHT|PBTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
  375. if(inp & PBTN_PLAY) { // action
  376. if (menu_sel < PicoPatchCount)
  377. PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
  378. else return;
  379. }
  380. if(inp & PBTN_STOP) return;
  381. }
  382. }
  383. // ------------ savestate loader ------------
  384. static int state_slot_flags = 0;
  385. static void state_check_slots(void)
  386. {
  387. int slot;
  388. state_slot_flags = 0;
  389. for (slot = 0; slot < 10; slot++)
  390. {
  391. if (emu_checkSaveFile(slot))
  392. {
  393. state_slot_flags |= 1 << slot;
  394. }
  395. }
  396. }
  397. static void draw_savestate_bg(int slot)
  398. {
  399. struct PicoVideo tmp_pv;
  400. unsigned short tmp_cram[0x40];
  401. unsigned short tmp_vsram[0x40];
  402. void *tmp_vram, *file;
  403. char *fname;
  404. fname = emu_GetSaveFName(1, 0, slot);
  405. if (!fname) return;
  406. tmp_vram = malloc(sizeof(Pico.vram));
  407. if (tmp_vram == NULL) return;
  408. memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));
  409. memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));
  410. memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));
  411. memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));
  412. if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
  413. file = gzopen(fname, "rb");
  414. emu_setSaveStateCbs(1);
  415. } else {
  416. file = fopen(fname, "rb");
  417. emu_setSaveStateCbs(0);
  418. }
  419. if (file) {
  420. if (PicoIn.AHW & 1) {
  421. PicoCdLoadStateGfx(file);
  422. } else {
  423. areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
  424. areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
  425. areaSeek(file, 0x2000, SEEK_CUR);
  426. areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
  427. areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
  428. areaSeek(file, 0x221a0, SEEK_SET);
  429. areaRead(&Pico.video, 1, sizeof(Pico.video), file);
  430. }
  431. areaClose(file);
  432. }
  433. emu_forcedFrame(POPT_EN_SOFTSCALE);
  434. menu_prepare_bg(1);
  435. memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));
  436. memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));
  437. memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));
  438. memcpy(&Pico.video, &tmp_pv, sizeof(Pico.video));
  439. free(tmp_vram);
  440. }
  441. static void draw_savestate_menu(int menu_sel, int is_loading)
  442. {
  443. int tl_x = 25, tl_y = 60, y, i;
  444. if (state_slot_flags & (1 << menu_sel))
  445. draw_savestate_bg(menu_sel);
  446. menu_draw_begin(1);
  447. text_out16(tl_x, 30, is_loading ? "Load state" : "Save state");
  448. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
  449. /* draw all 10 slots */
  450. y = tl_y;
  451. for (i = 0; i < 10; i++, y+=10)
  452. {
  453. text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
  454. }
  455. text_out16(tl_x, y, "back");
  456. menu_draw_end();
  457. }
  458. static int savestate_menu_loop(int is_loading)
  459. {
  460. static int menu_sel = 10;
  461. int menu_sel_max = 10;
  462. unsigned long inp = 0;
  463. state_check_slots();
  464. for(;;)
  465. {
  466. draw_savestate_menu(menu_sel, is_loading);
  467. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY|PBTN_STOP);
  468. if(inp & PBTN_UP ) {
  469. do {
  470. menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
  471. } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
  472. }
  473. if(inp & PBTN_DOWN) {
  474. do {
  475. menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
  476. } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
  477. }
  478. if(inp & PBTN_PLAY) { // save/load
  479. if (menu_sel < 10) {
  480. state_slot = menu_sel;
  481. PicoStateProgressCB = emu_stateCb; /* also suitable for menu */
  482. if (emu_SaveLoadGame(is_loading, 0)) {
  483. strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
  484. return 1;
  485. }
  486. return 0;
  487. } else return 1;
  488. }
  489. if(inp & PBTN_STOP) return 1;
  490. }
  491. }
  492. // -------------- key config --------------
  493. static char *action_binds(int player_idx, int action_mask)
  494. {
  495. static char strkeys[32*5];
  496. int i;
  497. strkeys[0] = 0;
  498. for (i = 0; i < 32; i++) // i is key index
  499. {
  500. if (currentConfig.KeyBinds[i] & action_mask)
  501. {
  502. if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
  503. if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, keyNames[i]); break; }
  504. else strcpy(strkeys, keyNames[i]);
  505. }
  506. }
  507. return strkeys;
  508. }
  509. static void unbind_action(int action)
  510. {
  511. int i;
  512. for (i = 0; i < 32; i++)
  513. currentConfig.KeyBinds[i] &= ~action;
  514. }
  515. static int count_bound_keys(int action, int pl_idx)
  516. {
  517. int i, keys = 0;
  518. for (i = 0; i < 32; i++)
  519. {
  520. if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
  521. if (currentConfig.KeyBinds[i] & action) keys++;
  522. }
  523. return keys;
  524. }
  525. static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx, int sel)
  526. {
  527. int x, y, tl_y = 30, i;
  528. menu_draw_begin(1);
  529. if (player_idx >= 0) {
  530. text_out16(80, 10, "Player %i controls", player_idx + 1);
  531. x = 80;
  532. } else {
  533. text_out16(80, 10, "Emulator controls");
  534. x = 40;
  535. }
  536. menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
  537. y = tl_y;
  538. for (i = 0; i < opt_cnt; i++, y+=10)
  539. text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
  540. text_out16(x, y, "Done");
  541. if (sel < opt_cnt) {
  542. text_out16(30, 195, "Press a button to bind/unbind");
  543. text_out16(30, 205, "Use HOME to clear");
  544. text_out16(30, 215, "To bind UP/DOWN, hold HOME");
  545. text_out16(30, 225, "Select \"Done\" to exit");
  546. } else {
  547. text_out16(30, 205, "Use Options -> Save cfg");
  548. text_out16(30, 215, "to save controls");
  549. text_out16(30, 225, "Press PLAY or STOP to exit");
  550. }
  551. menu_draw_end();
  552. }
  553. static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
  554. {
  555. int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
  556. unsigned long inp = 0;
  557. for (;;)
  558. {
  559. draw_key_config(opts, opt_cnt, player_idx, sel);
  560. inp = in_menu_wait(CONFIGURABLE_KEYS|PBTN_HOME);
  561. if (!(inp & PBTN_HOME)) {
  562. prev_select = 0;
  563. if(inp & PBTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
  564. if(inp & PBTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
  565. }
  566. if (sel >= opt_cnt) {
  567. if (inp & (PBTN_PLAY|PBTN_STOP)) break;
  568. else continue;
  569. }
  570. // if we are here, we want to bind/unbind something
  571. if ((inp & PBTN_HOME) && !prev_select)
  572. unbind_action(opts[sel].mask);
  573. prev_select = inp & PBTN_HOME;
  574. inp &= CONFIGURABLE_KEYS;
  575. inp &= ~PBTN_HOME;
  576. for (i = 0; i < 32; i++)
  577. if (inp & (1 << i)) {
  578. if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
  579. currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
  580. else currentConfig.KeyBinds[i] ^= opts[sel].mask;
  581. if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
  582. currentConfig.KeyBinds[i] &= ~(3 << 16);
  583. currentConfig.KeyBinds[i] |= player_idx << 16;
  584. }
  585. }
  586. }
  587. }
  588. menu_entry ctrlopt_entries[] =
  589. {
  590. { "Player 1", MB_NONE, MA_CTRL_PLAYER1, NULL, 0, 0, 0, 1, 0 },
  591. { "Player 2", MB_NONE, MA_CTRL_PLAYER2, NULL, 0, 0, 0, 1, 0 },
  592. { "Emulator controls", MB_NONE, MA_CTRL_EMU, NULL, 0, 0, 0, 1, 0 },
  593. { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &PicoIn.opt, 0x020, 0, 0, 1, 1 },
  594. { "Turbo rate", MB_RANGE, MA_CTRL_TURBO_RATE, &currentConfig.turbo_rate, 0, 1, 30, 1, 1 },
  595. { "Done", MB_NONE, MA_CTRL_DONE, NULL, 0, 0, 0, 1, 0 },
  596. };
  597. #define CTRLOPT_ENTRY_COUNT (sizeof(ctrlopt_entries) / sizeof(ctrlopt_entries[0]))
  598. const int ctrlopt_entry_count = CTRLOPT_ENTRY_COUNT;
  599. static void draw_kc_sel(int menu_sel)
  600. {
  601. int tl_x = 25+40, tl_y = 60, y;
  602. y = tl_y;
  603. menu_draw_begin(1);
  604. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
  605. me_draw(ctrlopt_entries, ctrlopt_entry_count, tl_x, tl_y, NULL, NULL);
  606. menu_draw_end();
  607. }
  608. // player2_flag, ?, ?, ?, ?, ?, ?, menu
  609. // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
  610. // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
  611. me_bind_action emuctrl_actions[] =
  612. {
  613. { "Load State ", 1<<28 },
  614. { "Save State ", 1<<27 },
  615. { "Prev Save Slot ", 1<<25 },
  616. { "Next Save Slot ", 1<<24 },
  617. { "Switch Renderer", 1<<26 },
  618. { "Volume Down ", 1<<30 },
  619. { "Volume Up ", 1<<29 },
  620. { NULL, 0 }
  621. };
  622. static void kc_sel_loop(void)
  623. {
  624. int menu_sel = 5, menu_sel_max = 5;
  625. unsigned long inp = 0;
  626. menu_id selected_id;
  627. while (1)
  628. {
  629. draw_kc_sel(menu_sel);
  630. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY|PBTN_STOP);
  631. selected_id = me_index2id(ctrlopt_entries, CTRLOPT_ENTRY_COUNT, menu_sel);
  632. if (inp & (PBTN_LEFT|PBTN_RIGHT)) // multi choise
  633. me_process(ctrlopt_entries, CTRLOPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0);
  634. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  635. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  636. if (inp & PBTN_PLAY) {
  637. int is_6button = PicoIn.opt & 0x020;
  638. switch (selected_id) {
  639. case MA_CTRL_PLAYER1: key_config_loop(me_ctrl_actions, is_6button ? 15 : 11, 0); return;
  640. case MA_CTRL_PLAYER2: key_config_loop(me_ctrl_actions, is_6button ? 15 : 11, 1); return;
  641. case MA_CTRL_EMU: key_config_loop(emuctrl_actions,
  642. sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]) - 1, -1); return;
  643. case MA_CTRL_DONE: if (!rom_loaded) emu_WriteConfig(0); return;
  644. default: return;
  645. }
  646. }
  647. if (inp & PBTN_STOP) return;
  648. }
  649. }
  650. // --------- sega/mega cd options ----------
  651. menu_entry cdopt_entries[] =
  652. {
  653. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1, 0 },
  654. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1, 0 },
  655. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1, 0 },
  656. { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, &currentConfig.EmuOpt, 0x0400, 0, 0, 1, 1 },
  657. { "CDDA audio", MB_ONOFF, MA_CDOPT_CDDA, &PicoIn.opt, 0x0800, 0, 0, 1, 1 },
  658. { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &PicoIn.opt, 0x0400, 0, 0, 1, 1 },
  659. { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1, 1 },
  660. { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, &PicoIn.opt, 0x8000, 0, 0, 1, 1 },
  661. { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&PicoIn.opt, 0x1000, 0, 0, 1, 1 },
  662. { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &PicoIn.opt, 0x2000, 0, 0, 1, 1 },
  663. { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1, 0 },
  664. };
  665. #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
  666. const int cdopt_entry_count = CDOPT_ENTRY_COUNT;
  667. struct bios_names_t
  668. {
  669. char us[32], eu[32], jp[32];
  670. };
  671. static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
  672. {
  673. struct bios_names_t *bios_names = param;
  674. char ra_buff[16];
  675. switch (entry->id)
  676. {
  677. case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
  678. case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
  679. case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
  680. case MA_CDOPT_READAHEAD:
  681. if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
  682. else strcpy(ra_buff, " OFF");
  683. text_out16(x, y, "ReadAhead buffer %s", ra_buff);
  684. break;
  685. default:break;
  686. }
  687. }
  688. static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
  689. {
  690. int tl_x = 25, tl_y = 60;
  691. menu_id selected_id;
  692. char ra_buff[16];
  693. if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
  694. else strcpy(ra_buff, " OFF");
  695. menu_draw_begin(1);
  696. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
  697. me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
  698. selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
  699. if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
  700. (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
  701. (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
  702. text_out16(tl_x, 210, "Press start to test selected BIOS");
  703. menu_draw_end();
  704. }
  705. static void cd_menu_loop_options(void)
  706. {
  707. static int menu_sel = 0;
  708. int menu_sel_max = 10;
  709. unsigned long inp = 0;
  710. struct bios_names_t bios_names;
  711. menu_id selected_id;
  712. char *bios, *p;
  713. if (emu_findBios(4, &bios)) { // US
  714. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
  715. if (*p == '/') p++;
  716. strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
  717. } else strcpy(bios_names.us, "NOT FOUND");
  718. if (emu_findBios(8, &bios)) { // EU
  719. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
  720. if (*p == '/') p++;
  721. strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
  722. } else strcpy(bios_names.eu, "NOT FOUND");
  723. if (emu_findBios(1, &bios)) { // JP
  724. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
  725. if (*p == '/') p++;
  726. strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
  727. } else strcpy(bios_names.jp, "NOT FOUND");
  728. for(;;)
  729. {
  730. draw_cd_menu_options(menu_sel, &bios_names);
  731. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_PLAY|PBTN_STOP|PBTN_REW);
  732. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  733. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  734. selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
  735. if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
  736. if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0) &&
  737. selected_id == MA_CDOPT_READAHEAD) {
  738. if (inp & PBTN_LEFT) {
  739. PicoCDBuffers >>= 1;
  740. if (PicoCDBuffers < 2) PicoCDBuffers = 0;
  741. } else {
  742. if (PicoCDBuffers < 2) PicoCDBuffers = 2;
  743. else PicoCDBuffers <<= 1;
  744. if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
  745. }
  746. }
  747. }
  748. if (inp & PBTN_PLAY) { // toggleable options
  749. if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
  750. selected_id == MA_CDOPT_DONE) {
  751. return;
  752. }
  753. switch (selected_id) { // BIOS testers
  754. case MA_CDOPT_TESTBIOS_USA:
  755. if (emu_findBios(4, &bios)) { // test US
  756. strcpy(romFileName, bios);
  757. engineState = PGS_ReloadRom;
  758. return;
  759. }
  760. break;
  761. case MA_CDOPT_TESTBIOS_EUR:
  762. if (emu_findBios(8, &bios)) { // test EU
  763. strcpy(romFileName, bios);
  764. engineState = PGS_ReloadRom;
  765. return;
  766. }
  767. break;
  768. case MA_CDOPT_TESTBIOS_JAP:
  769. if (emu_findBios(1, &bios)) { // test JP
  770. strcpy(romFileName, bios);
  771. engineState = PGS_ReloadRom;
  772. return;
  773. }
  774. break;
  775. default:
  776. break;
  777. }
  778. }
  779. if (inp & (PBTN_STOP|PBTN_REW)) return;
  780. }
  781. }
  782. // --------- advanced options ----------
  783. menu_entry opt2_entries[] =
  784. {
  785. { "Disable sprite limit", MB_ONOFF, MA_OPT2_NO_SPRITE_LIM, &PicoIn.opt, 0x40000, 0, 0, 1, 1 },
  786. { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &PicoIn.opt, 0x00004, 0, 0, 1, 1 },
  787. { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &PicoIn.opt, 0x00001, 0, 0, 1, 1 },
  788. { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496,&PicoIn.opt, 0x00002, 0, 0, 1, 1 },
  789. { "Emulate YM2413 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2413, &PicoIn.opt, 0x00020, 0, 0, 1, 1 },
  790. { "Double buffering", MB_ONOFF, MA_OPT2_DBLBUFF, &currentConfig.EmuOpt, 0x8000, 0, 0, 1, 1 },
  791. { "Wait for V-sync (slow)", MB_ONOFF, MA_OPT2_VSYNC, &currentConfig.EmuOpt, 0x2000, 0, 0, 1, 1 },
  792. { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, &currentConfig.EmuOpt, 0x0008, 0, 0, 1, 1 },
  793. { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, &currentConfig.EmuOpt, 0x0020, 0, 0, 1, 1 },
  794. { "SVP dynarec", MB_ONOFF, MA_OPT2_SVP_DYNAREC, &PicoIn.opt, 0x20000, 0, 0, 1, 1 },
  795. { "Disable idle loop patching",MB_ONOFF, MA_OPT2_NO_IDLE_LOOPS, &PicoIn.opt, 0x80000, 0, 0, 1, 1 },
  796. { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1, 0 },
  797. };
  798. #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
  799. const int opt2_entry_count = OPT2_ENTRY_COUNT;
  800. static void draw_amenu_options(int menu_sel)
  801. {
  802. int tl_x = 25, tl_y = 50;
  803. menu_draw_begin(1);
  804. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
  805. me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
  806. menu_draw_end();
  807. }
  808. static void amenu_loop_options(void)
  809. {
  810. static int menu_sel = 0;
  811. int menu_sel_max;
  812. unsigned long inp = 0;
  813. menu_id selected_id;
  814. menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
  815. for(;;)
  816. {
  817. draw_amenu_options(menu_sel);
  818. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_PLAY|PBTN_STOP|PBTN_REW);
  819. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  820. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  821. selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
  822. if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
  823. if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0) &&
  824. selected_id == MA_OPT2_GAMMA) {
  825. while ((inp = Framework_PollGetButtons()) & (PBTN_LEFT|PBTN_RIGHT)) {
  826. currentConfig.gamma += (inp & PBTN_LEFT) ? -1 : 1;
  827. if (currentConfig.gamma < 1) currentConfig.gamma = 1;
  828. if (currentConfig.gamma > 300) currentConfig.gamma = 300;
  829. draw_amenu_options(menu_sel);
  830. Sleep(18);
  831. }
  832. }
  833. }
  834. if (inp & PBTN_PLAY) { // toggleable options
  835. if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
  836. selected_id == MA_OPT2_DONE) {
  837. return;
  838. }
  839. }
  840. if (inp & (PBTN_STOP|PBTN_REW)) return;
  841. }
  842. }
  843. // -------------- options --------------
  844. menu_entry opt_entries[] =
  845. {
  846. { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1, 1 },
  847. { "Accurate sprites", MB_ONOFF, MA_OPT_ACC_SPRITES, &PicoIn.opt, 0x080, 0, 0, 0, 1 },
  848. { "Scanline mode (faster)", MB_ONOFF, MA_OPT_INTERLACED, &currentConfig.EmuOpt, 0x4000, 0, 0, 1, 1 },
  849. { "Scale low res mode", MB_ONOFF, MA_OPT_SCALING, &currentConfig.scaling, 0x0001, 0, 3, 1, 1 },
  850. { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, &currentConfig.EmuOpt, 0x0002, 0, 0, 1, 1 },
  851. { NULL, MB_RANGE, MA_OPT_FRAMESKIP, &currentConfig.Frameskip, 0, -1, 16, 1, 1 },
  852. { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, &currentConfig.EmuOpt, 0x0004, 0, 0, 1, 1 },
  853. { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1, 1 },
  854. { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1, 1 },
  855. { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, &currentConfig.EmuOpt, 0x0001, 0, 0, 1, 1 },
  856. { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1, 1 },
  857. { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1, 1 },
  858. { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1, 0 },
  859. { "[advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1, 0 },
  860. { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1, 0 },
  861. { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1, 0 },
  862. { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1, 0 },
  863. };
  864. #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
  865. const int opt_entry_count = OPT_ENTRY_COUNT;
  866. static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
  867. {
  868. char *str, str24[24];
  869. switch (entry->id)
  870. {
  871. case MA_OPT_RENDERER:
  872. if (PicoIn.opt&0x10)
  873. str = " 8bit fast";
  874. else if (currentConfig.EmuOpt&0x80)
  875. str = "16bit accurate";
  876. else
  877. str = " 8bit accurate";
  878. text_out16(x, y, "Renderer: %s", str);
  879. break;
  880. case MA_OPT_FRAMESKIP:
  881. if (currentConfig.Frameskip < 0)
  882. strcpy(str24, "Auto");
  883. else sprintf(str24, "%i", currentConfig.Frameskip);
  884. text_out16(x, y, "Frameskip %s", str24);
  885. break;
  886. case MA_OPT_SOUND_QUALITY:
  887. str = (PicoIn.opt&0x08)?"stereo":"mono";
  888. text_out16(x, y, "Sound Quality: %5iHz %s", PicoIn.sndRate, str);
  889. break;
  890. case MA_OPT_REGION:
  891. text_out16(x, y, "Region: %s", me_region_name(PicoIn.regionOverride, PicoIn.autoRgnOrder));
  892. break;
  893. case MA_OPT_CONFIRM_STATES:
  894. switch ((currentConfig.EmuOpt >> 9) & 5) {
  895. default: str = "OFF"; break;
  896. case 1: str = "writes"; break;
  897. case 4: str = "loads"; break;
  898. case 5: str = "both"; break;
  899. }
  900. text_out16(x, y, "Confirm savestate %s", str);
  901. break;
  902. case MA_OPT_SAVECFG:
  903. str24[0] = 0;
  904. if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
  905. text_out16(x, y, "Save cfg as default%s", str24);
  906. break;
  907. case MA_OPT_LOADCFG:
  908. text_out16(x, y, "Load cfg from profile %i", config_slot);
  909. break;
  910. default:
  911. lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
  912. break;
  913. }
  914. }
  915. static void draw_menu_options(int menu_sel)
  916. {
  917. int tl_x = 25, tl_y = 24;
  918. menu_draw_begin(1);
  919. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
  920. me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
  921. menu_draw_end();
  922. }
  923. static int sndrate_prevnext(int rate, int dir)
  924. {
  925. int i, rates[] = { 11025, 22050, 44100 };
  926. for (i = 0; i < 5; i++)
  927. if (rates[i] == rate) break;
  928. i += dir ? 1 : -1;
  929. if (i > 2) return dir ? 44100 : 22050;
  930. if (i < 0) return dir ? 22050 : 11025;
  931. return rates[i];
  932. }
  933. static void region_prevnext(int right)
  934. {
  935. // jp_ntsc=1, jp_pal=2, usa=4, eu=8
  936. static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
  937. int i;
  938. if (right) {
  939. if (!PicoIn.regionOverride) {
  940. for (i = 0; i < 6; i++)
  941. if (rgn_orders[i] == PicoIn.autoRgnOrder) break;
  942. if (i < 5) PicoIn.autoRgnOrder = rgn_orders[i+1];
  943. else PicoIn.regionOverride=1;
  944. }
  945. else PicoIn.regionOverride<<=1;
  946. if (PicoIn.regionOverride > 8) PicoIn.regionOverride = 8;
  947. } else {
  948. if (!PicoIn.regionOverride) {
  949. for (i = 0; i < 6; i++)
  950. if (rgn_orders[i] == PicoIn.autoRgnOrder) break;
  951. if (i > 0) PicoIn.autoRgnOrder = rgn_orders[i-1];
  952. }
  953. else PicoIn.regionOverride>>=1;
  954. }
  955. }
  956. static void menu_options_save(void)
  957. {
  958. if (PicoIn.regionOverride) {
  959. // force setting possibly changed..
  960. Pico.m.pal = (PicoIn.regionOverride == 2 || PicoIn.regionOverride == 8) ? 1 : 0;
  961. }
  962. if (!(PicoIn.opt & 0x20)) {
  963. // unbind XYZ MODE, just in case
  964. unbind_action(0xf00);
  965. }
  966. }
  967. static int menu_loop_options(void)
  968. {
  969. static int menu_sel = 0;
  970. int menu_sel_max, ret;
  971. unsigned long inp = 0;
  972. menu_id selected_id;
  973. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_loaded);
  974. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
  975. menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
  976. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  977. while (1)
  978. {
  979. draw_menu_options(menu_sel);
  980. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_PLAY|PBTN_STOP|PBTN_REW);
  981. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  982. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  983. selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
  984. if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
  985. if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0)) {
  986. switch (selected_id) {
  987. case MA_OPT_RENDERER:
  988. if (inp & PBTN_LEFT) {
  989. if ((PicoIn.opt&0x10) || !(currentConfig.EmuOpt &0x80)) {
  990. PicoIn.opt&= ~0x10;
  991. currentConfig.EmuOpt |= 0x80;
  992. }
  993. } else {
  994. if (!(PicoIn.opt&0x10) || (currentConfig.EmuOpt &0x80)) {
  995. PicoIn.opt|= 0x10;
  996. currentConfig.EmuOpt &= ~0x80;
  997. }
  998. }
  999. break;
  1000. case MA_OPT_SOUND_QUALITY:
  1001. if ((inp & PBTN_RIGHT) && PicoIn.sndRate == 44100 &&
  1002. !(PicoIn.opt&0x08))
  1003. {
  1004. PicoIn.sndRate = 11025;
  1005. PicoIn.opt |= 8;
  1006. } else if ((inp & PBTN_LEFT) && PicoIn.sndRate == 11025 &&
  1007. (PicoIn.opt&0x08) && !(PicoIn.AHW&1))
  1008. {
  1009. PicoIn.sndRate = 44100;
  1010. PicoIn.opt &= ~8;
  1011. } else
  1012. PicoIn.sndRate = sndrate_prevnext(PicoIn.sndRate, inp & PBTN_RIGHT);
  1013. break;
  1014. case MA_OPT_REGION:
  1015. region_prevnext(inp & PBTN_RIGHT);
  1016. break;
  1017. case MA_OPT_CONFIRM_STATES: {
  1018. int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
  1019. n += (inp & PBTN_LEFT) ? -1 : 1;
  1020. if (n < 0) n = 0; else if (n > 3) n = 3;
  1021. n |= n << 1; n &= ~2;
  1022. currentConfig.EmuOpt &= ~0xa00;
  1023. currentConfig.EmuOpt |= n << 9;
  1024. break;
  1025. }
  1026. case MA_OPT_SAVE_SLOT:
  1027. if (inp & PBTN_RIGHT) {
  1028. state_slot++; if (state_slot > 9) state_slot = 0;
  1029. } else {state_slot--; if (state_slot < 0) state_slot = 9;
  1030. }
  1031. break;
  1032. case MA_OPT_SAVECFG:
  1033. case MA_OPT_SAVECFG_GAME:
  1034. case MA_OPT_LOADCFG:
  1035. config_slot += (inp&PBTN_RIGHT) ? 1 : -1;
  1036. if (config_slot > 9) config_slot = 0;
  1037. if (config_slot < 0) config_slot = 9;
  1038. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
  1039. menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
  1040. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1041. break;
  1042. default:
  1043. //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. if (inp & PBTN_PLAY) {
  1049. if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
  1050. {
  1051. switch (selected_id)
  1052. {
  1053. case MA_OPT_SCD_OPTS:
  1054. cd_menu_loop_options();
  1055. if (engineState == PGS_ReloadRom)
  1056. return 0; // test BIOS
  1057. break;
  1058. case MA_OPT_ADV_OPTS:
  1059. amenu_loop_options();
  1060. break;
  1061. case MA_OPT_SAVECFG: // done (update and write)
  1062. menu_options_save();
  1063. if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
  1064. else strcpy(menuErrorMsg, "failed to write config");
  1065. return 1;
  1066. case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
  1067. menu_options_save();
  1068. if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
  1069. else strcpy(menuErrorMsg, "failed to write config");
  1070. return 1;
  1071. case MA_OPT_LOADCFG:
  1072. ret = emu_ReadConfig(1, 1);
  1073. if (!ret) ret = emu_ReadConfig(0, 1);
  1074. if (ret) strcpy(menuErrorMsg, "config loaded");
  1075. else strcpy(menuErrorMsg, "failed to load config");
  1076. return 1;
  1077. default:
  1078. //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. if(inp & (PBTN_STOP|PBTN_REW)) {
  1084. menu_options_save();
  1085. return 0; // done (update, no write)
  1086. }
  1087. }
  1088. }
  1089. // -------------- credits --------------
  1090. static void draw_menu_credits(void)
  1091. {
  1092. int tl_x = 15, tl_y = 56, y;
  1093. menu_draw_begin(1);
  1094. text_out16(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006-2008");
  1095. y = tl_y;
  1096. text_out16(tl_x, y, "Credits:");
  1097. text_out16(tl_x, (y+=10), "fDave: Cyclone 68000 core,");
  1098. text_out16(tl_x, (y+=10), " base code of PicoDrive");
  1099. text_out16(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");
  1100. text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
  1101. text_out16(tl_x, (y+=10), "Reesy: kgsdk wrapper, sound code");
  1102. text_out16(tl_x, (y+=10), "jens.l: gizmondo hardware");
  1103. text_out16(tl_x, (y+=10), "ketchupgun: skin design");
  1104. text_out16(tl_x, (y+=20), "special thanks (for docs, ideas)");
  1105. text_out16(tl_x, (y+=10), " Charles MacDonald, Haze,");
  1106. text_out16(tl_x, (y+=10), " Stephane Dallongeville,");
  1107. text_out16(tl_x, (y+=10), " Lordus, Exophase, Rokas,");
  1108. text_out16(tl_x, (y+=10), " Nemesis, Tasco Deluxe");
  1109. menu_draw_end();
  1110. }
  1111. // -------------- root menu --------------
  1112. menu_entry main_entries[] =
  1113. {
  1114. { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0, 0 },
  1115. { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0, 0 },
  1116. { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0, 0 },
  1117. { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0, 0 },
  1118. { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1, 0 },
  1119. { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1, 0 },
  1120. { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1, 0 },
  1121. { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1, 0 },
  1122. { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0, 0 },
  1123. { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1, 0 }
  1124. };
  1125. #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
  1126. static void draw_menu_root(int menu_sel)
  1127. {
  1128. const int tl_x = 70, tl_y = 70;
  1129. menu_draw_begin(1);
  1130. text_out16(tl_x, 20, "PicoDrive v" VERSION);
  1131. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
  1132. me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
  1133. // error
  1134. if (menuErrorMsg[0]) {
  1135. memset((char *)menu_screen + 321*224*2, 0, 321*16*2);
  1136. text_out16(5, 226, menuErrorMsg);
  1137. }
  1138. menu_draw_end();
  1139. }
  1140. static void menu_loop_root(void)
  1141. {
  1142. static int menu_sel = 0;
  1143. int ret, menu_sel_max;
  1144. unsigned long inp = 0;
  1145. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_loaded);
  1146. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_loaded);
  1147. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_loaded);
  1148. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_loaded);
  1149. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
  1150. menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
  1151. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1152. /* make sure action buttons are not pressed on entering menu */
  1153. draw_menu_root(menu_sel);
  1154. while (Framework_PollGetButtons() & (PBTN_PLAY|PBTN_STOP|PBTN_HOME)) Sleep(50);
  1155. for (;;)
  1156. {
  1157. draw_menu_root(menu_sel);
  1158. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY|PBTN_STOP|PBTN_HOME|PBTN_L|PBTN_R);
  1159. if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1160. if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1161. if((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R)) debug_menu_loop();
  1162. if( inp & (PBTN_HOME|PBTN_STOP)) {
  1163. if (rom_loaded) {
  1164. while (Framework_PollGetButtons() & (PBTN_HOME|PBTN_STOP)) Sleep(50); // wait until released
  1165. engineState = PGS_Running;
  1166. break;
  1167. }
  1168. }
  1169. if(inp & PBTN_PLAY) {
  1170. switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
  1171. {
  1172. case MA_MAIN_RESUME_GAME:
  1173. if (rom_loaded) {
  1174. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1175. engineState = PGS_Running;
  1176. return;
  1177. }
  1178. break;
  1179. case MA_MAIN_SAVE_STATE:
  1180. if (rom_loaded) {
  1181. if(savestate_menu_loop(0))
  1182. continue;
  1183. engineState = PGS_Running;
  1184. return;
  1185. }
  1186. break;
  1187. case MA_MAIN_LOAD_STATE:
  1188. if (rom_loaded) {
  1189. if(savestate_menu_loop(1))
  1190. continue;
  1191. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1192. engineState = PGS_Running;
  1193. return;
  1194. }
  1195. break;
  1196. case MA_MAIN_RESET_GAME:
  1197. if (rom_loaded) {
  1198. emu_ResetGame();
  1199. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1200. engineState = PGS_Running;
  1201. return;
  1202. }
  1203. break;
  1204. case MA_MAIN_LOAD_ROM:
  1205. {
  1206. char curr_path[MAX_PATH], *selfname;
  1207. FILE *tstf;
  1208. if ( (tstf = fopen(loadedRomFName, "rb")) )
  1209. {
  1210. fclose(tstf);
  1211. strcpy(curr_path, loadedRomFName);
  1212. }
  1213. else
  1214. getcwd(curr_path, MAX_PATH);
  1215. selfname = romsel_loop(curr_path);
  1216. if (selfname) {
  1217. lprintf("selected file: %s\n", selfname);
  1218. engineState = PGS_ReloadRom;
  1219. return;
  1220. }
  1221. break;
  1222. }
  1223. case MA_MAIN_OPTIONS:
  1224. ret = menu_loop_options();
  1225. if (ret == 1) continue; // status update
  1226. if (engineState == PGS_ReloadRom)
  1227. return; // BIOS test
  1228. break;
  1229. case MA_MAIN_CONTROLS:
  1230. kc_sel_loop();
  1231. break;
  1232. case MA_MAIN_CREDITS:
  1233. draw_menu_credits();
  1234. Sleep(500);
  1235. inp = in_menu_wait(PBTN_PLAY|PBTN_STOP);
  1236. break;
  1237. case MA_MAIN_EXIT:
  1238. engineState = PGS_Quit;
  1239. return;
  1240. case MA_MAIN_PATCHES:
  1241. if (rom_loaded && PicoPatches) {
  1242. patches_menu_loop();
  1243. PicoPatchApply();
  1244. strcpy(menuErrorMsg, "Patches applied");
  1245. continue;
  1246. }
  1247. break;
  1248. default:
  1249. lprintf("%s: something unknown selected\n", __FUNCTION__);
  1250. break;
  1251. }
  1252. }
  1253. menuErrorMsg[0] = 0; // clear error msg
  1254. }
  1255. }
  1256. // warning: alignment
  1257. void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
  1258. {
  1259. u32 *dest = dst;
  1260. const u32 *srce = src;
  1261. pixels /= 2;
  1262. if (darker)
  1263. {
  1264. while (pixels--)
  1265. {
  1266. u32 p = *srce++;
  1267. *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
  1268. }
  1269. }
  1270. else
  1271. {
  1272. while (pixels--)
  1273. {
  1274. u32 p = *srce++;
  1275. *dest++ = (p&0xf79ef79e)>>1;
  1276. }
  1277. }
  1278. }
  1279. static void menu_prepare_bg(int use_game_bg)
  1280. {
  1281. if (use_game_bg)
  1282. {
  1283. // darken the active framebuffer
  1284. if (giz_screen == NULL)
  1285. giz_screen = fb_lock(1);
  1286. memset(bg_buffer, 0, 321*8*2);
  1287. menu_darken_bg(bg_buffer + 321*8*2, (char *)giz_screen + 321*8*2, 321*224, 1);
  1288. memset(bg_buffer + 321*232*2, 0, 321*8*2);
  1289. fb_unlock();
  1290. giz_screen = NULL;
  1291. }
  1292. else
  1293. {
  1294. int i;
  1295. // should really only happen once, on startup..
  1296. readpng(bg_buffer, "skin/background.png", READPNG_BG);
  1297. // adjust 320 width to 321
  1298. for (i = 239; i > 0; i--)
  1299. memmove(bg_buffer + 321*2*i, bg_buffer + 320*2*i, 320*2);
  1300. }
  1301. }
  1302. static void menu_gfx_prepare(void)
  1303. {
  1304. menu_prepare_bg(rom_loaded);
  1305. menu_draw_begin(1);
  1306. menu_draw_end();
  1307. }
  1308. void menu_loop(void)
  1309. {
  1310. menu_gfx_prepare();
  1311. menu_loop_root();
  1312. menuErrorMsg[0] = 0;
  1313. }
  1314. // --------- CD tray close menu ----------
  1315. static void draw_menu_tray(int menu_sel)
  1316. {
  1317. int tl_x = 70, tl_y = 90, y;
  1318. menu_draw_begin(1);
  1319. text_out16(tl_x, 20, "The unit is about to");
  1320. text_out16(tl_x, 30, "close the CD tray.");
  1321. y = tl_y;
  1322. text_out16(tl_x, y, "Load new CD image");
  1323. text_out16(tl_x, (y+=10), "Insert nothing");
  1324. // draw cursor
  1325. text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
  1326. // error
  1327. if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
  1328. menu_draw_end();
  1329. }
  1330. int menu_loop_tray(void)
  1331. {
  1332. int menu_sel = 0, menu_sel_max = 1;
  1333. unsigned long inp = 0;
  1334. char curr_path[MAX_PATH], *selfname;
  1335. FILE *tstf;
  1336. menu_gfx_prepare();
  1337. if ( (tstf = fopen(loadedRomFName, "rb")) )
  1338. {
  1339. fclose(tstf);
  1340. strcpy(curr_path, loadedRomFName);
  1341. }
  1342. else
  1343. {
  1344. getcwd(curr_path, MAX_PATH);
  1345. }
  1346. /* make sure action buttons are not pressed on entering menu */
  1347. draw_menu_tray(menu_sel);
  1348. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1349. for (;;)
  1350. {
  1351. draw_menu_tray(menu_sel);
  1352. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY);
  1353. if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1354. if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1355. if(inp & PBTN_PLAY) {
  1356. switch (menu_sel) {
  1357. case 0: // select image
  1358. selfname = romsel_loop(curr_path);
  1359. if (selfname) {
  1360. int ret = -1;
  1361. cd_img_type cd_type;
  1362. cd_type = emu_cdCheck(NULL, romFileName);
  1363. if (cd_type != CIT_NOT_CD)
  1364. ret = Insert_CD(romFileName, cd_type);
  1365. if (ret != 0) {
  1366. sprintf(menuErrorMsg, "Load failed, invalid CD image?");
  1367. lprintf("%s\n", menuErrorMsg);
  1368. continue;
  1369. }
  1370. engineState = PGS_RestartRun;
  1371. return 1;
  1372. }
  1373. break;
  1374. case 1: // insert nothing
  1375. engineState = PGS_RestartRun;
  1376. return 0;
  1377. }
  1378. }
  1379. menuErrorMsg[0] = 0; // clear error msg
  1380. }
  1381. }