emu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h> // tolower
  7. #ifndef NO_SYNC
  8. #include <unistd.h>
  9. #endif
  10. #include "emu.h"
  11. #include "menu.h"
  12. #include "fonts.h"
  13. #include "lprintf.h"
  14. #include "config.h"
  15. #include <Pico/PicoInt.h>
  16. #include <Pico/Patch.h>
  17. #include <zlib/zlib.h>
  18. #if defined(__GP2X__)
  19. #include "../gp2x/gp2x.h"
  20. #define SCREEN_WIDTH 320
  21. #define SCREEN_BUFFER gp2x_screen
  22. #elif defined(__GIZ__)
  23. #include "../gizmondo/giz.h"
  24. #define SCREEN_WIDTH 321
  25. #define SCREEN_BUFFER giz_screen
  26. #elif defined(PSP)
  27. #include "../psp/psp.h"
  28. #define SCREEN_WIDTH 512
  29. #define SCREEN_BUFFER psp_screen
  30. #endif
  31. char *PicoConfigFile = "config.cfg";
  32. currentConfig_t currentConfig, defaultConfig;
  33. int rom_loaded = 0;
  34. char noticeMsg[64];
  35. int state_slot = 0;
  36. int config_slot = 0, config_slot_current = 0;
  37. char lastRomFile[512];
  38. int kb_combo_keys = 0, kb_combo_acts = 0; // keys and actions which need button combos
  39. unsigned char *movie_data = NULL;
  40. static int movie_size = 0;
  41. // provided by platform code:
  42. extern char romFileName[];
  43. extern void emu_noticeMsgUpdated(void);
  44. extern void emu_getMainDir(char *dst, int len);
  45. extern void emu_setDefaultConfig(void);
  46. extern void menu_romload_prepare(const char *rom_name);
  47. extern void menu_romload_end(void);
  48. // utilities
  49. static void strlwr_(char* string)
  50. {
  51. while ( (*string++ = (char)tolower(*string)) );
  52. }
  53. static int try_rfn_cut(void)
  54. {
  55. FILE *tmp;
  56. char *p;
  57. p = romFileName + strlen(romFileName) - 1;
  58. for (; p > romFileName; p--)
  59. if (*p == '.') break;
  60. *p = 0;
  61. if((tmp = fopen(romFileName, "rb"))) {
  62. fclose(tmp);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. static void get_ext(char *file, char *ext)
  68. {
  69. char *p;
  70. p = file + strlen(file) - 4;
  71. if (p < file) p = file;
  72. strncpy(ext, p, 4);
  73. ext[4] = 0;
  74. strlwr_(ext);
  75. }
  76. char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };
  77. char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };
  78. char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
  79. int emu_findBios(int region, char **bios_file)
  80. {
  81. static char bios_path[1024];
  82. int i, count;
  83. char **files;
  84. FILE *f = NULL;
  85. if (region == 4) { // US
  86. files = biosfiles_us;
  87. count = sizeof(biosfiles_us) / sizeof(char *);
  88. } else if (region == 8) { // EU
  89. files = biosfiles_eu;
  90. count = sizeof(biosfiles_eu) / sizeof(char *);
  91. } else if (region == 1 || region == 2) {
  92. files = biosfiles_jp;
  93. count = sizeof(biosfiles_jp) / sizeof(char *);
  94. } else {
  95. return 0;
  96. }
  97. for (i = 0; i < count; i++)
  98. {
  99. emu_getMainDir(bios_path, sizeof(bios_path));
  100. strcat(bios_path, files[i]);
  101. strcat(bios_path, ".bin");
  102. f = fopen(bios_path, "rb");
  103. if (f) break;
  104. bios_path[strlen(bios_path) - 4] = 0;
  105. strcat(bios_path, ".zip");
  106. f = fopen(bios_path, "rb");
  107. if (f) break;
  108. }
  109. if (f) {
  110. lprintf("using bios: %s\n", bios_path);
  111. fclose(f);
  112. if (bios_file) *bios_file = bios_path;
  113. return 1;
  114. } else {
  115. sprintf(menuErrorMsg, "no %s BIOS files found, read docs",
  116. region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
  117. lprintf("%s\n", menuErrorMsg);
  118. return 0;
  119. }
  120. }
  121. /* check if the name begins with BIOS name */
  122. /*
  123. static int emu_isBios(const char *name)
  124. {
  125. int i;
  126. for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
  127. if (strstr(name, biosfiles_us[i]) != NULL) return 1;
  128. for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
  129. if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
  130. for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
  131. if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
  132. return 0;
  133. }
  134. */
  135. static unsigned char id_header[0x100];
  136. /* checks if romFileName points to valid MegaCD image
  137. * if so, checks for suitable BIOS */
  138. int emu_cdCheck(int *pregion)
  139. {
  140. unsigned char buf[32];
  141. pm_file *cd_f;
  142. int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe
  143. cd_f = pm_open(romFileName);
  144. if (!cd_f) return 0; // let the upper level handle this
  145. if (pm_read(buf, 32, cd_f) != 32) {
  146. pm_close(cd_f);
  147. return 0;
  148. }
  149. if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)
  150. if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)
  151. if (type == 0) {
  152. pm_close(cd_f);
  153. return 0;
  154. }
  155. pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);
  156. pm_read(id_header, sizeof(id_header), cd_f);
  157. /* it seems we have a CD image here. Try to detect region now.. */
  158. pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
  159. pm_read(buf, 1, cd_f);
  160. pm_close(cd_f);
  161. if (buf[0] == 0x64) region = 8; // EU
  162. if (buf[0] == 0xa1) region = 1; // JAP
  163. lprintf("detected %s Sega/Mega CD image with %s region\n",
  164. type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
  165. if (pregion != NULL) *pregion = region;
  166. return type;
  167. }
  168. static int extract_text(char *dest, unsigned char *src, int len, int swab)
  169. {
  170. char *p = dest;
  171. int i;
  172. if (swab) swab = 1;
  173. for (i = len - 1; i >= 0; i--)
  174. {
  175. if (src[i^swab] != ' ') break;
  176. }
  177. len = i + 1;
  178. for (i = 0; i < len; i++)
  179. {
  180. unsigned char s = src[i^swab];
  181. if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
  182. s != '[' && s != ']' && s != '\\')
  183. {
  184. *p++ = s;
  185. }
  186. else
  187. {
  188. sprintf(p, "\\%02x", s);
  189. p += 3;
  190. }
  191. }
  192. return p - dest;
  193. }
  194. char *emu_makeRomId(void)
  195. {
  196. static char id_string[3+0x11+0x11+0x30+16];
  197. int pos, swab = 1;
  198. if (PicoAHW & PAHW_MCD) {
  199. strcpy(id_string, "CD|");
  200. swab = 0;
  201. }
  202. else strcpy(id_string, "MD|");
  203. pos = 3;
  204. pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial
  205. id_string[pos] = '|'; pos++;
  206. pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region
  207. id_string[pos] = '|'; pos++;
  208. pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name
  209. id_string[pos] = 0;
  210. return id_string;
  211. }
  212. int emu_ReloadRom(void)
  213. {
  214. unsigned int rom_size = 0;
  215. char *used_rom_name = romFileName;
  216. unsigned char *rom_data = NULL;
  217. char ext[5];
  218. pm_file *rom;
  219. int ret, cd_state, cd_region, cfg_loaded = 0;
  220. lprintf("emu_ReloadRom(%s)\n", romFileName);
  221. get_ext(romFileName, ext);
  222. // detect wrong extensions
  223. if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
  224. sprintf(menuErrorMsg, "Not a ROM selected.");
  225. return 0;
  226. }
  227. PicoPatchUnload();
  228. // check for movie file
  229. if (movie_data) {
  230. free(movie_data);
  231. movie_data = 0;
  232. }
  233. if (!strcmp(ext, ".gmv"))
  234. {
  235. // check for both gmv and rom
  236. int dummy;
  237. FILE *movie_file = fopen(romFileName, "rb");
  238. if(!movie_file) {
  239. sprintf(menuErrorMsg, "Failed to open movie.");
  240. return 0;
  241. }
  242. fseek(movie_file, 0, SEEK_END);
  243. movie_size = ftell(movie_file);
  244. fseek(movie_file, 0, SEEK_SET);
  245. if(movie_size < 64+3) {
  246. sprintf(menuErrorMsg, "Invalid GMV file.");
  247. fclose(movie_file);
  248. return 0;
  249. }
  250. movie_data = malloc(movie_size);
  251. if(movie_data == NULL) {
  252. sprintf(menuErrorMsg, "low memory.");
  253. fclose(movie_file);
  254. return 0;
  255. }
  256. fread(movie_data, 1, movie_size, movie_file);
  257. fclose(movie_file);
  258. if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
  259. sprintf(menuErrorMsg, "Invalid GMV file.");
  260. return 0;
  261. }
  262. dummy = try_rfn_cut() || try_rfn_cut();
  263. if (!dummy) {
  264. sprintf(menuErrorMsg, "Could't find a ROM for movie.");
  265. return 0;
  266. }
  267. get_ext(romFileName, ext);
  268. }
  269. else if (!strcmp(ext, ".pat")) {
  270. int dummy;
  271. PicoPatchLoad(romFileName);
  272. dummy = try_rfn_cut() || try_rfn_cut();
  273. if (!dummy) {
  274. sprintf(menuErrorMsg, "Could't find a ROM to patch.");
  275. return 0;
  276. }
  277. get_ext(romFileName, ext);
  278. }
  279. if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)
  280. Stop_CD();
  281. // check for MegaCD image
  282. cd_state = emu_cdCheck(&cd_region);
  283. if (cd_state > 0)
  284. {
  285. PicoAHW |= PAHW_MCD;
  286. // valid CD image, check for BIOS..
  287. // we need to have config loaded at this point
  288. ret = emu_ReadConfig(1, 1);
  289. if (!ret) emu_ReadConfig(0, 1);
  290. cfg_loaded = 1;
  291. if (PicoRegionOverride) {
  292. cd_region = PicoRegionOverride;
  293. lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");
  294. }
  295. if (!emu_findBios(cd_region, &used_rom_name)) {
  296. // bios_help() ?
  297. PicoAHW &= ~PAHW_MCD;
  298. return 0;
  299. }
  300. get_ext(used_rom_name, ext);
  301. }
  302. else
  303. {
  304. if (PicoAHW & PAHW_MCD) Stop_CD();
  305. PicoAHW &= ~PAHW_MCD;
  306. }
  307. rom = pm_open(used_rom_name);
  308. if (!rom) {
  309. sprintf(menuErrorMsg, "Failed to open rom.");
  310. return 0;
  311. }
  312. menu_romload_prepare(used_rom_name); // also CD load
  313. PicoCartUnload();
  314. rom_loaded = 0;
  315. if ( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
  316. sprintf(menuErrorMsg, "PicoCartLoad() failed.");
  317. lprintf("%s\n", menuErrorMsg);
  318. pm_close(rom);
  319. menu_romload_end();
  320. return 0;
  321. }
  322. pm_close(rom);
  323. // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
  324. if (rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
  325. ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
  326. if (rom_data) free(rom_data);
  327. sprintf(menuErrorMsg, "Not a ROM selected.");
  328. menu_romload_end();
  329. return 0;
  330. }
  331. // load config for this ROM (do this before insert to get correct region)
  332. if (!(PicoAHW & PAHW_MCD))
  333. memcpy(id_header, rom_data + 0x100, sizeof(id_header));
  334. if (!cfg_loaded) {
  335. ret = emu_ReadConfig(1, 1);
  336. if (!ret) emu_ReadConfig(0, 1);
  337. }
  338. lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
  339. if (PicoCartInsert(rom_data, rom_size)) {
  340. sprintf(menuErrorMsg, "Failed to load ROM.");
  341. menu_romload_end();
  342. return 0;
  343. }
  344. Pico.m.frame_count = 0;
  345. // insert CD if it was detected
  346. if (cd_state > 0) {
  347. ret = Insert_CD(romFileName, cd_state == 2);
  348. if (ret != 0) {
  349. sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
  350. lprintf("%s\n", menuErrorMsg);
  351. menu_romload_end();
  352. return 0;
  353. }
  354. }
  355. menu_romload_end();
  356. if (PicoPatches) {
  357. PicoPatchPrepare();
  358. PicoPatchApply();
  359. }
  360. // additional movie stuff
  361. if (movie_data) {
  362. if(movie_data[0x14] == '6')
  363. PicoOpt |= POPT_6BTN_PAD; // 6 button pad
  364. else PicoOpt &= ~POPT_6BTN_PAD;
  365. PicoOpt |= POPT_DIS_VDP_FIFO|POPT_ACC_TIMING; // accurate timing, no VDP fifo timing
  366. if(movie_data[0xF] >= 'A') {
  367. if(movie_data[0x16] & 0x80) {
  368. PicoRegionOverride = 8;
  369. } else {
  370. PicoRegionOverride = 4;
  371. }
  372. PicoReset();
  373. // TODO: bits 6 & 5
  374. }
  375. movie_data[0x18+30] = 0;
  376. sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
  377. }
  378. else
  379. {
  380. PicoOpt &= ~POPT_DIS_VDP_FIFO;
  381. if(Pico.m.pal) {
  382. strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
  383. } else {
  384. strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
  385. }
  386. }
  387. emu_noticeMsgUpdated();
  388. // load SRAM for this ROM
  389. if (currentConfig.EmuOpt & 1)
  390. emu_SaveLoadGame(1, 1);
  391. strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1);
  392. lastRomFile[sizeof(lastRomFile)-1] = 0;
  393. rom_loaded = 1;
  394. return 1;
  395. }
  396. static void romfname_ext(char *dst, const char *prefix, const char *ext)
  397. {
  398. char *p;
  399. int prefix_len = 0;
  400. // make save filename
  401. for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
  402. *dst = 0;
  403. if (prefix) {
  404. strcpy(dst, prefix);
  405. prefix_len = strlen(prefix);
  406. }
  407. strncpy(dst + prefix_len, p, 511-prefix_len);
  408. dst[511-8] = 0;
  409. if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
  410. if (ext) strcat(dst, ext);
  411. }
  412. static void make_config_cfg(char *cfg)
  413. {
  414. strncpy(cfg, PicoConfigFile, 511);
  415. if (config_slot != 0)
  416. {
  417. char *p = strrchr(cfg, '.');
  418. if (p == NULL) p = cfg + strlen(cfg);
  419. sprintf(p, ".%i.cfg", config_slot);
  420. }
  421. cfg[511] = 0;
  422. }
  423. int emu_ReadConfig(int game, int no_defaults)
  424. {
  425. char cfg[512];
  426. FILE *f;
  427. int ret;
  428. if (!game)
  429. {
  430. if (!no_defaults)
  431. emu_setDefaultConfig();
  432. make_config_cfg(cfg);
  433. ret = config_readsect(cfg, NULL);
  434. }
  435. else
  436. {
  437. char *sect = emu_makeRomId();
  438. // try new .cfg way
  439. if (config_slot != 0)
  440. sprintf(cfg, "game.%i.cfg", config_slot);
  441. else strcpy(cfg, "game.cfg");
  442. ret = -1;
  443. if (config_havesect(cfg, sect))
  444. {
  445. // read user's config
  446. int vol = currentConfig.volume;
  447. emu_setDefaultConfig();
  448. ret = config_readsect(cfg, sect);
  449. currentConfig.volume = vol; // make vol global (bah)
  450. }
  451. else
  452. {
  453. // read global config, and apply game_def.cfg on top
  454. make_config_cfg(cfg);
  455. config_readsect(cfg, NULL);
  456. ret = config_readsect("game_def.cfg", sect);
  457. }
  458. if (ret != 0)
  459. {
  460. // fall back to old game specific cfg
  461. char extbuf[16];
  462. if (config_slot != 0)
  463. sprintf(extbuf, ".%i.pbcfg", config_slot);
  464. else strcpy(extbuf, ".pbcfg");
  465. romfname_ext(cfg, "cfg/", extbuf);
  466. f = fopen(cfg, "rb");
  467. if (!f) {
  468. romfname_ext(cfg, NULL, ".pbcfg");
  469. f = fopen(cfg, "rb");
  470. }
  471. if (f) {
  472. int bread;
  473. fseek(f, 512, SEEK_SET); // skip unused lrom buffer
  474. bread = fread(&currentConfig, 1, sizeof(currentConfig), f);
  475. lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)");
  476. fclose(f);
  477. ret = 0;
  478. }
  479. if (ret == 0) {
  480. PicoOpt = currentConfig.s_PicoOpt;
  481. PsndRate = currentConfig.s_PsndRate;
  482. PicoRegionOverride = currentConfig.s_PicoRegion;
  483. PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
  484. // PicoCDBuffers = currentConfig.s_PicoCDBuffers; // ignore in this case
  485. }
  486. }
  487. else
  488. {
  489. lprintf("loaded cfg from sect \"%s\"\n", sect);
  490. }
  491. }
  492. // some sanity checks
  493. if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
  494. #ifdef PSP
  495. if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
  496. if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;
  497. #else
  498. if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
  499. #endif
  500. if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;
  501. #ifdef __GP2X__
  502. // if volume keys are unbound, bind them to volume control
  503. if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
  504. currentConfig.KeyBinds[23] = 1<<29; // vol up
  505. currentConfig.KeyBinds[22] = 1<<30; // vol down
  506. }
  507. #endif
  508. if (ret == 0) config_slot_current = config_slot;
  509. return (ret == 0);
  510. }
  511. int emu_WriteConfig(int is_game)
  512. {
  513. char cfg[512], *game_sect = NULL;
  514. int ret, write_lrom = 0;
  515. if (!is_game)
  516. {
  517. strncpy(cfg, PicoConfigFile, 511);
  518. if (config_slot != 0)
  519. {
  520. char *p = strrchr(cfg, '.');
  521. if (p == NULL) p = cfg + strlen(cfg);
  522. sprintf(p, ".%i.cfg", config_slot);
  523. }
  524. cfg[511] = 0;
  525. write_lrom = 1;
  526. } else {
  527. if (config_slot != 0)
  528. sprintf(cfg, "game.%i.cfg", config_slot);
  529. else strcpy(cfg, "game.cfg");
  530. game_sect = emu_makeRomId();
  531. lprintf("emu_WriteConfig: sect \"%s\"\n", game_sect);
  532. }
  533. lprintf("emu_WriteConfig: %s ", cfg);
  534. ret = config_writesect(cfg, game_sect);
  535. if (write_lrom) config_writelrom(cfg);
  536. #ifndef NO_SYNC
  537. sync();
  538. #endif
  539. lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
  540. if (ret == 0) config_slot_current = config_slot;
  541. return ret == 0;
  542. }
  543. void emu_textOut8(int x, int y, const char *text)
  544. {
  545. int i,l,len=strlen(text);
  546. unsigned char *screen = (unsigned char *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;
  547. /* always using built-in font */
  548. for (i = 0; i < len; i++)
  549. {
  550. for (l=0;l<8;l++)
  551. {
  552. unsigned char fd = fontdata8x8[((text[i])*8)+l];
  553. if (fd&0x80) screen[l*SCREEN_WIDTH+0]=0xf0;
  554. if (fd&0x40) screen[l*SCREEN_WIDTH+1]=0xf0;
  555. if (fd&0x20) screen[l*SCREEN_WIDTH+2]=0xf0;
  556. if (fd&0x10) screen[l*SCREEN_WIDTH+3]=0xf0;
  557. if (fd&0x08) screen[l*SCREEN_WIDTH+4]=0xf0;
  558. if (fd&0x04) screen[l*SCREEN_WIDTH+5]=0xf0;
  559. if (fd&0x02) screen[l*SCREEN_WIDTH+6]=0xf0;
  560. if (fd&0x01) screen[l*SCREEN_WIDTH+7]=0xf0;
  561. }
  562. screen += 8;
  563. }
  564. }
  565. void emu_textOut16(int x, int y, const char *text)
  566. {
  567. int i,l,len=strlen(text);
  568. unsigned short *screen = (unsigned short *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;
  569. for (i = 0; i < len; i++)
  570. {
  571. for (l=0;l<8;l++)
  572. {
  573. unsigned char fd = fontdata8x8[((text[i])*8)+l];
  574. if(fd&0x80) screen[l*SCREEN_WIDTH+0]=0xffff;
  575. if(fd&0x40) screen[l*SCREEN_WIDTH+1]=0xffff;
  576. if(fd&0x20) screen[l*SCREEN_WIDTH+2]=0xffff;
  577. if(fd&0x10) screen[l*SCREEN_WIDTH+3]=0xffff;
  578. if(fd&0x08) screen[l*SCREEN_WIDTH+4]=0xffff;
  579. if(fd&0x04) screen[l*SCREEN_WIDTH+5]=0xffff;
  580. if(fd&0x02) screen[l*SCREEN_WIDTH+6]=0xffff;
  581. if(fd&0x01) screen[l*SCREEN_WIDTH+7]=0xffff;
  582. }
  583. screen += 8;
  584. }
  585. }
  586. #ifdef PSP
  587. #define MAX_COMBO_KEY 23
  588. #else
  589. #define MAX_COMBO_KEY 31
  590. #endif
  591. void emu_findKeyBindCombos(void)
  592. {
  593. int act, u;
  594. // find out which keys and actions are combos
  595. kb_combo_keys = kb_combo_acts = 0;
  596. for (act = 0; act < 32; act++)
  597. {
  598. int keyc = 0, keyc2 = 0;
  599. if (act == 16 || act == 17) continue; // player2 flag
  600. if (act > 17)
  601. {
  602. for (u = 0; u <= MAX_COMBO_KEY; u++)
  603. if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
  604. }
  605. else
  606. {
  607. for (u = 0; u <= MAX_COMBO_KEY; u++)
  608. if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1
  609. (currentConfig.KeyBinds[u] & (1 << act))) keyc++;
  610. for (u = 0; u <= MAX_COMBO_KEY; u++)
  611. if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2
  612. (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;
  613. if (keyc2 > keyc) keyc = keyc2;
  614. }
  615. if (keyc > 1)
  616. {
  617. // loop again and mark those keys and actions as combo
  618. for (u = 0; u <= MAX_COMBO_KEY; u++)
  619. {
  620. if (currentConfig.KeyBinds[u] & (1 << act)) {
  621. kb_combo_keys |= 1 << u;
  622. kb_combo_acts |= 1 << act;
  623. }
  624. }
  625. }
  626. }
  627. // printf("combo keys/acts: %08x %08x\n", kb_combo_keys, kb_combo_acts);
  628. }
  629. void emu_updateMovie(void)
  630. {
  631. int offs = Pico.m.frame_count*3 + 0x40;
  632. if (offs+3 > movie_size) {
  633. free(movie_data);
  634. movie_data = 0;
  635. strcpy(noticeMsg, "END OF MOVIE.");
  636. lprintf("END OF MOVIE.\n");
  637. emu_noticeMsgUpdated();
  638. } else {
  639. // MXYZ SACB RLDU
  640. PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
  641. if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
  642. if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
  643. if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
  644. PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
  645. if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
  646. if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
  647. if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
  648. PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
  649. if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
  650. if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
  651. PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
  652. if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
  653. if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
  654. }
  655. }
  656. static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
  657. {
  658. return gzread(file, p, _n);
  659. }
  660. static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
  661. {
  662. return gzwrite(file, p, _n);
  663. }
  664. static int try_ropen_file(const char *fname)
  665. {
  666. FILE *f;
  667. f = fopen(fname, "rb");
  668. if (f) {
  669. fclose(f);
  670. return 1;
  671. }
  672. return 0;
  673. }
  674. char *emu_GetSaveFName(int load, int is_sram, int slot)
  675. {
  676. static char saveFname[512];
  677. char ext[16];
  678. if (is_sram)
  679. {
  680. romfname_ext(saveFname, (PicoAHW&1) ? "brm/" : "srm/", (PicoAHW&1) ? ".brm" : ".srm");
  681. if (load) {
  682. if (try_ropen_file(saveFname)) return saveFname;
  683. // try in current dir..
  684. romfname_ext(saveFname, NULL, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");
  685. if (try_ropen_file(saveFname)) return saveFname;
  686. return NULL; // give up
  687. }
  688. }
  689. else
  690. {
  691. ext[0] = 0;
  692. if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
  693. strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");
  694. romfname_ext(saveFname, "mds/", ext);
  695. if (load) {
  696. if (try_ropen_file(saveFname)) return saveFname;
  697. romfname_ext(saveFname, NULL, ext);
  698. if (try_ropen_file(saveFname)) return saveFname;
  699. if (currentConfig.EmuOpt & 8) {
  700. ext[0] = 0;
  701. if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
  702. strcat(ext, ".mds");
  703. romfname_ext(saveFname, "mds/", ext);
  704. if (try_ropen_file(saveFname)) return saveFname;
  705. romfname_ext(saveFname, NULL, ext);
  706. if (try_ropen_file(saveFname)) return saveFname;
  707. }
  708. return NULL;
  709. }
  710. }
  711. return saveFname;
  712. }
  713. int emu_checkSaveFile(int slot)
  714. {
  715. return emu_GetSaveFName(1, 0, slot) ? 1 : 0;
  716. }
  717. void emu_setSaveStateCbs(int gz)
  718. {
  719. if (gz) {
  720. areaRead = gzRead2;
  721. areaWrite = gzWrite2;
  722. areaEof = (areaeof *) gzeof;
  723. areaSeek = (areaseek *) gzseek;
  724. areaClose = (areaclose *) gzclose;
  725. } else {
  726. areaRead = (arearw *) fread;
  727. areaWrite = (arearw *) fwrite;
  728. areaEof = (areaeof *) feof;
  729. areaSeek = (areaseek *) fseek;
  730. areaClose = (areaclose *) fclose;
  731. }
  732. }
  733. int emu_SaveLoadGame(int load, int sram)
  734. {
  735. int ret = 0;
  736. char *saveFname;
  737. // make save filename
  738. saveFname = emu_GetSaveFName(load, sram, state_slot);
  739. if (saveFname == NULL) {
  740. if (!sram) {
  741. strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");
  742. emu_noticeMsgUpdated();
  743. }
  744. return -1;
  745. }
  746. lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
  747. if (sram)
  748. {
  749. FILE *sramFile;
  750. int sram_size;
  751. unsigned char *sram_data;
  752. int truncate = 1;
  753. if (PicoAHW & PAHW_MCD)
  754. {
  755. if (PicoOpt&POPT_EN_MCD_RAMCART) {
  756. sram_size = 0x12000;
  757. sram_data = SRam.data;
  758. if (sram_data)
  759. memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
  760. } else {
  761. sram_size = 0x2000;
  762. sram_data = Pico_mcd->bram;
  763. truncate = 0; // the .brm may contain RAM cart data after normal brm
  764. }
  765. } else {
  766. sram_size = SRam.end-SRam.start+1;
  767. if(Pico.m.sram_reg & 4) sram_size=0x2000;
  768. sram_data = SRam.data;
  769. }
  770. if (!sram_data) return 0; // SRam forcefully disabled for this game
  771. if (load)
  772. {
  773. sramFile = fopen(saveFname, "rb");
  774. if(!sramFile) return -1;
  775. fread(sram_data, 1, sram_size, sramFile);
  776. fclose(sramFile);
  777. if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))
  778. memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
  779. } else {
  780. // sram save needs some special processing
  781. // see if we have anything to save
  782. for (; sram_size > 0; sram_size--)
  783. if (sram_data[sram_size-1]) break;
  784. if (sram_size) {
  785. sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
  786. if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
  787. if (!sramFile) return -1;
  788. ret = fwrite(sram_data, 1, sram_size, sramFile);
  789. ret = (ret != sram_size) ? -1 : 0;
  790. fclose(sramFile);
  791. #ifndef NO_SYNC
  792. sync();
  793. #endif
  794. }
  795. }
  796. return ret;
  797. }
  798. else
  799. {
  800. void *PmovFile = NULL;
  801. if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)
  802. {
  803. if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
  804. emu_setSaveStateCbs(1);
  805. if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
  806. }
  807. }
  808. else
  809. {
  810. if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
  811. emu_setSaveStateCbs(0);
  812. }
  813. }
  814. if(PmovFile) {
  815. ret = PmovState(load ? 6 : 5, PmovFile);
  816. areaClose(PmovFile);
  817. PmovFile = 0;
  818. if (load) Pico.m.dirtyPal=1;
  819. #ifndef NO_SYNC
  820. else sync();
  821. #endif
  822. }
  823. else ret = -1;
  824. if (!ret)
  825. strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
  826. else
  827. {
  828. strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
  829. ret = -1;
  830. }
  831. emu_noticeMsgUpdated();
  832. return ret;
  833. }
  834. }