emu.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2007-2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdarg.h>
  12. #ifdef __GP2X__
  13. #include <unistd.h>
  14. #endif
  15. #include "../libpicofe/posix.h"
  16. #include "../libpicofe/input.h"
  17. #include "../libpicofe/fonts.h"
  18. #include "../libpicofe/sndout.h"
  19. #include "../libpicofe/lprintf.h"
  20. #include "../libpicofe/plat.h"
  21. #include "emu.h"
  22. #include "input_pico.h"
  23. #include "menu_pico.h"
  24. #include "config_file.h"
  25. #include <pico/pico_int.h>
  26. #include <pico/patch.h>
  27. #ifdef USE_LIBRETRO_VFS
  28. #include "file_stream_transforms.h"
  29. #endif
  30. #if defined(__GNUC__) && __GNUC__ >= 7
  31. #pragma GCC diagnostic ignored "-Wformat-truncation"
  32. #endif
  33. #ifndef _WIN32
  34. #define PATH_SEP "/"
  35. #define PATH_SEP_C '/'
  36. #else
  37. #define PATH_SEP "\\"
  38. #define PATH_SEP_C '\\'
  39. #endif
  40. #define STATUS_MSG_TIMEOUT 2000
  41. void *g_screen_ptr;
  42. int g_screen_width = 320;
  43. int g_screen_height = 240;
  44. int g_screen_ppitch = 320; // pitch in pixels
  45. const char *PicoConfigFile = "config2.cfg";
  46. currentConfig_t currentConfig, defaultConfig;
  47. int state_slot = 0;
  48. int config_slot = 0, config_slot_current = 0;
  49. int pico_pen_x = 320/2, pico_pen_y = 240/2;
  50. int pico_inp_mode;
  51. int flip_after_sync;
  52. int engineState = PGS_Menu;
  53. static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
  54. /* tmp buff to reduce stack usage for plats with small stack */
  55. static char static_buff[512];
  56. const char *rom_fname_reload;
  57. char rom_fname_loaded[512];
  58. int reset_timing = 0;
  59. static unsigned int notice_msg_time; /* when started showing */
  60. static char noticeMsg[40];
  61. unsigned char *movie_data = NULL;
  62. static int movie_size = 0;
  63. /* don't use tolower() for easy old glibc binary compatibility */
  64. static void strlwr_(char *string)
  65. {
  66. char *p;
  67. for (p = string; *p; p++)
  68. if ('A' <= *p && *p <= 'Z')
  69. *p += 'a' - 'A';
  70. }
  71. static int try_rfn_cut(char *fname)
  72. {
  73. FILE *tmp;
  74. char *p;
  75. p = fname + strlen(fname) - 1;
  76. for (; p > fname; p--)
  77. if (*p == '.') break;
  78. *p = 0;
  79. if((tmp = fopen(fname, "rb"))) {
  80. fclose(tmp);
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. static void get_ext(const char *file, char *ext)
  86. {
  87. const char *p;
  88. p = file + strlen(file) - 4;
  89. if (p < file) p = file;
  90. strncpy(ext, p, 4);
  91. ext[4] = 0;
  92. strlwr_(ext);
  93. }
  94. static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)
  95. {
  96. int prefix_len = 0;
  97. const char *p;
  98. *dst = 0;
  99. if (prefix) {
  100. int len = plat_get_root_dir(dst, dstlen);
  101. strcpy(dst + len, prefix);
  102. prefix_len = len + strlen(prefix);
  103. }
  104. p = fname + strlen(fname) - 1;
  105. for (; p >= fname && *p != PATH_SEP_C; p--)
  106. ;
  107. p++;
  108. strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);
  109. dst[dstlen - 8] = 0;
  110. if (dst[strlen(dst) - 4] == '.')
  111. dst[strlen(dst) - 4] = 0;
  112. if (ext)
  113. strcat(dst, ext);
  114. }
  115. static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)
  116. {
  117. fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);
  118. }
  119. void emu_status_msg(const char *format, ...)
  120. {
  121. va_list vl;
  122. int ret;
  123. va_start(vl, format);
  124. ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);
  125. va_end(vl);
  126. /* be sure old text gets overwritten */
  127. for (; ret < 28; ret++)
  128. noticeMsg[ret] = ' ';
  129. noticeMsg[ret] = 0;
  130. notice_msg_time = plat_get_ticks_ms();
  131. }
  132. static const char * const biosfiles_us[] = {
  133. "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U"
  134. };
  135. static const char * const biosfiles_eu[] = {
  136. "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E"
  137. };
  138. static const char * const biosfiles_jp[] = {
  139. "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
  140. };
  141. static const char *find_bios(int *region, const char *cd_fname)
  142. {
  143. int i, count;
  144. const char * const *files;
  145. FILE *f = NULL;
  146. int ret;
  147. // we need to have config loaded at this point
  148. ret = emu_read_config(cd_fname, 0);
  149. if (!ret) emu_read_config(NULL, 0);
  150. if (PicoIn.regionOverride) {
  151. *region = PicoIn.regionOverride;
  152. lprintf("override region to %s\n", *region != 4 ?
  153. (*region == 8 ? "EU" : "JAP") : "USA");
  154. }
  155. if (*region == 4) { // US
  156. files = biosfiles_us;
  157. count = sizeof(biosfiles_us) / sizeof(char *);
  158. } else if (*region == 8) { // EU
  159. files = biosfiles_eu;
  160. count = sizeof(biosfiles_eu) / sizeof(char *);
  161. } else if (*region == 1 || *region == 2) {
  162. files = biosfiles_jp;
  163. count = sizeof(biosfiles_jp) / sizeof(char *);
  164. } else {
  165. return 0;
  166. }
  167. for (i = 0; i < count; i++)
  168. {
  169. emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);
  170. strcat(static_buff, ".bin");
  171. f = fopen(static_buff, "rb");
  172. if (f) break;
  173. static_buff[strlen(static_buff) - 4] = 0;
  174. strcat(static_buff, ".zip");
  175. f = fopen(static_buff, "rb");
  176. if (f) break;
  177. }
  178. if (f) {
  179. lprintf("using bios: %s\n", static_buff);
  180. fclose(f);
  181. return static_buff;
  182. } else {
  183. sprintf(static_buff, "no %s BIOS files found, read docs",
  184. *region != 4 ? (*region == 8 ? "EU" : "JAP") : "USA");
  185. menu_update_msg(static_buff);
  186. return NULL;
  187. }
  188. }
  189. /* check if the name begins with BIOS name */
  190. /*
  191. static int emu_isBios(const char *name)
  192. {
  193. int i;
  194. for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
  195. if (strstr(name, biosfiles_us[i]) != NULL) return 1;
  196. for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
  197. if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
  198. for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
  199. if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
  200. return 0;
  201. }
  202. */
  203. static int extract_text(char *dest, const unsigned char *src, int len, int swab)
  204. {
  205. char *p = dest;
  206. int i;
  207. if (swab) swab = 1;
  208. for (i = len - 1; i >= 0; i--)
  209. {
  210. if (src[i^swab] != ' ') break;
  211. }
  212. len = i + 1;
  213. for (i = 0; i < len; i++)
  214. {
  215. unsigned char s = src[i^swab];
  216. if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
  217. s != '[' && s != ']' && s != '\\')
  218. {
  219. *p++ = s;
  220. }
  221. else
  222. {
  223. sprintf(p, "\\%02x", s);
  224. p += 3;
  225. }
  226. }
  227. return p - dest;
  228. }
  229. static char *emu_make_rom_id(const char *fname)
  230. {
  231. static char id_string[3+0xe*3+0x3*3+0x30*3+3];
  232. int pos, swab = 1;
  233. if (PicoIn.AHW & PAHW_MCD) {
  234. strcpy(id_string, "CD|");
  235. swab = 0;
  236. }
  237. else if (PicoIn.AHW & PAHW_SMS)
  238. strcpy(id_string, "MS|");
  239. else strcpy(id_string, "MD|");
  240. pos = 3;
  241. if (!(PicoIn.AHW & PAHW_SMS)) {
  242. pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial
  243. id_string[pos] = '|'; pos++;
  244. pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region
  245. id_string[pos] = '|'; pos++;
  246. pos += extract_text(id_string + pos, media_id_header + 0x50, 0x30, swab); // overseas name
  247. id_string[pos] = 0;
  248. if (pos > 5)
  249. return id_string;
  250. pos = 3;
  251. }
  252. // can't find name in ROM, use filename
  253. fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);
  254. return id_string;
  255. }
  256. // buffer must be at least 150 byte long
  257. void emu_get_game_name(char *str150)
  258. {
  259. int ret, swab = (PicoIn.AHW & PAHW_MCD) ? 0 : 1;
  260. char *s, *d;
  261. ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name
  262. for (s = d = str150 + 1; s < str150+ret; s++)
  263. {
  264. if (*s == 0) break;
  265. if (*s != ' ' || d[-1] != ' ')
  266. *d++ = *s;
  267. }
  268. *d = 0;
  269. }
  270. static void system_announce(void)
  271. {
  272. const char *sys_name, *tv_standard, *extra = "";
  273. int fps;
  274. if (PicoIn.AHW & PAHW_SMS) {
  275. sys_name = "Master System";
  276. #ifdef NO_SMS
  277. extra = " [no support]";
  278. #endif
  279. } else if (PicoIn.AHW & PAHW_PICO) {
  280. sys_name = "Pico";
  281. } else if ((PicoIn.AHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) {
  282. sys_name = "32X + Mega CD";
  283. if ((Pico.m.hardware & 0xc0) == 0x80)
  284. sys_name = "32X + Sega CD";
  285. } else if (PicoIn.AHW & PAHW_MCD) {
  286. sys_name = "Mega CD";
  287. if ((Pico.m.hardware & 0xc0) == 0x80)
  288. sys_name = "Sega CD";
  289. } else if (PicoIn.AHW & PAHW_32X) {
  290. sys_name = "32X";
  291. } else {
  292. sys_name = "MegaDrive";
  293. if ((Pico.m.hardware & 0xc0) == 0x80)
  294. sys_name = "Genesis";
  295. }
  296. tv_standard = Pico.m.pal ? "PAL" : "NTSC";
  297. fps = Pico.m.pal ? 50 : 60;
  298. emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);
  299. }
  300. static void do_region_override(const char *media_fname)
  301. {
  302. // we only need to override region if config tells us so
  303. int ret = emu_read_config(media_fname, 0);
  304. if (!ret) emu_read_config(NULL, 0);
  305. }
  306. int emu_reload_rom(const char *rom_fname_in)
  307. {
  308. // use setting before rom config is loaded
  309. int autoload = g_autostateld_opt;
  310. char *rom_fname = NULL;
  311. char ext[5];
  312. enum media_type_e media_type;
  313. int menu_romload_started = 0;
  314. char carthw_path[512];
  315. int retval = 0;
  316. lprintf("emu_ReloadRom(%s)\n", rom_fname_in);
  317. rom_fname = strdup(rom_fname_in);
  318. if (rom_fname == NULL)
  319. return 0;
  320. get_ext(rom_fname, ext);
  321. // early cleanup
  322. PicoPatchUnload();
  323. if (movie_data) {
  324. free(movie_data);
  325. movie_data = 0;
  326. }
  327. if (!strcmp(ext, ".gmv"))
  328. {
  329. // check for both gmv and rom
  330. int dummy;
  331. FILE *movie_file = fopen(rom_fname, "rb");
  332. if (!movie_file) {
  333. menu_update_msg("Failed to open movie.");
  334. goto out;
  335. }
  336. fseek(movie_file, 0, SEEK_END);
  337. movie_size = ftell(movie_file);
  338. fseek(movie_file, 0, SEEK_SET);
  339. if (movie_size < 64+3) {
  340. menu_update_msg("Invalid GMV file.");
  341. fclose(movie_file);
  342. goto out;
  343. }
  344. movie_data = malloc(movie_size);
  345. if (movie_data == NULL) {
  346. menu_update_msg("low memory.");
  347. fclose(movie_file);
  348. goto out;
  349. }
  350. dummy = fread(movie_data, 1, movie_size, movie_file);
  351. fclose(movie_file);
  352. if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
  353. menu_update_msg("Invalid GMV file.");
  354. goto out;
  355. }
  356. dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
  357. if (!dummy) {
  358. menu_update_msg("Could't find a ROM for movie.");
  359. goto out;
  360. }
  361. get_ext(rom_fname, ext);
  362. lprintf("gmv loaded for %s\n", rom_fname);
  363. }
  364. else if (!strcmp(ext, ".pat"))
  365. {
  366. int dummy;
  367. PicoPatchLoad(rom_fname);
  368. dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
  369. if (!dummy) {
  370. menu_update_msg("Could't find a ROM to patch.");
  371. goto out;
  372. }
  373. get_ext(rom_fname, ext);
  374. }
  375. menu_romload_prepare(rom_fname); // also CD load
  376. menu_romload_started = 1;
  377. emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));
  378. media_type = PicoLoadMedia(rom_fname, carthw_path,
  379. find_bios, do_region_override);
  380. switch (media_type) {
  381. case PM_BAD_DETECT:
  382. menu_update_msg("Not a ROM/CD img selected.");
  383. goto out;
  384. case PM_BAD_CD:
  385. menu_update_msg("Invalid CD image");
  386. goto out;
  387. case PM_BAD_CD_NO_BIOS:
  388. // find_bios() prints a message
  389. goto out;
  390. case PM_ERROR:
  391. menu_update_msg("Load error");
  392. goto out;
  393. default:
  394. break;
  395. }
  396. // make quirks visible in UI
  397. if (PicoIn.quirks & PQUIRK_FORCE_6BTN)
  398. currentConfig.input_dev0 = PICO_INPUT_PAD_6BTN;
  399. menu_romload_end();
  400. menu_romload_started = 0;
  401. if (PicoPatches) {
  402. PicoPatchPrepare();
  403. PicoPatchApply();
  404. }
  405. // additional movie stuff
  406. if (movie_data)
  407. {
  408. enum input_device indev = (movie_data[0x14] == '6') ?
  409. PICO_INPUT_PAD_6BTN : PICO_INPUT_PAD_3BTN;
  410. PicoSetInputDevice(0, indev);
  411. PicoSetInputDevice(1, indev);
  412. PicoIn.opt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing
  413. if (movie_data[0xF] >= 'A') {
  414. if (movie_data[0x16] & 0x80) {
  415. PicoIn.regionOverride = 8;
  416. } else {
  417. PicoIn.regionOverride = 4;
  418. }
  419. PicoReset();
  420. // TODO: bits 6 & 5
  421. }
  422. movie_data[0x18+30] = 0;
  423. emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);
  424. }
  425. else
  426. {
  427. system_announce();
  428. PicoIn.opt &= ~POPT_DIS_VDP_FIFO;
  429. }
  430. strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);
  431. rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
  432. // load SRAM for this ROM
  433. if (currentConfig.EmuOpt & EOPT_EN_SRAM)
  434. emu_save_load_game(1, 1);
  435. // state autoload?
  436. if (autoload) {
  437. int time, newest = 0, newest_slot = -1;
  438. int slot;
  439. for (slot = 0; slot < 10; slot++) {
  440. if (emu_check_save_file(slot, &time)) {
  441. if (time > newest) {
  442. newest = time;
  443. newest_slot = slot;
  444. }
  445. }
  446. }
  447. if (newest_slot >= 0) {
  448. lprintf("autoload slot %d\n", newest_slot);
  449. state_slot = newest_slot;
  450. emu_save_load_game(1, 0);
  451. }
  452. else {
  453. lprintf("no save to autoload.\n");
  454. }
  455. }
  456. retval = 1;
  457. out:
  458. if (menu_romload_started)
  459. menu_romload_end();
  460. free(rom_fname);
  461. return retval;
  462. }
  463. int emu_swap_cd(const char *fname)
  464. {
  465. enum cd_img_type cd_type;
  466. int ret = -1;
  467. cd_type = PicoCdCheck(fname, NULL);
  468. if (cd_type != CIT_NOT_CD)
  469. ret = cdd_load(fname, cd_type);
  470. if (ret != 0) {
  471. menu_update_msg("Load failed, invalid CD image?");
  472. return 0;
  473. }
  474. strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);
  475. rom_fname_loaded[sizeof(rom_fname_loaded) - 1] = 0;
  476. return 1;
  477. }
  478. // <base dir><end>
  479. void emu_make_path(char *buff, const char *end, int size)
  480. {
  481. int pos, end_len;
  482. end_len = strlen(end);
  483. pos = plat_get_root_dir(buff, size);
  484. strncpy(buff + pos, end, size - pos);
  485. buff[size - 1] = 0;
  486. if (pos + end_len > size - 1)
  487. lprintf("Warning: path truncated: %s\n", buff);
  488. }
  489. static void make_config_cfg(char *cfg_buff_512)
  490. {
  491. emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);
  492. if (config_slot != 0)
  493. {
  494. char *p = strrchr(cfg_buff_512, '.');
  495. if (p == NULL)
  496. p = cfg_buff_512 + strlen(cfg_buff_512);
  497. sprintf(p, ".%i.cfg", config_slot);
  498. }
  499. cfg_buff_512[511] = 0;
  500. }
  501. void emu_prep_defconfig(void)
  502. {
  503. memset(&defaultConfig, 0, sizeof(defaultConfig));
  504. defaultConfig.EmuOpt = 0x9d | EOPT_EN_CD_LEDS;
  505. defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
  506. POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
  507. POPT_EN_DRC|POPT_ACC_SPRITES |
  508. POPT_EN_32X|POPT_EN_PWM;
  509. defaultConfig.s_PsndRate = 44100;
  510. defaultConfig.s_PicoRegion = 0; // auto
  511. defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
  512. defaultConfig.s_PicoCDBuffers = 0;
  513. defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;
  514. defaultConfig.Frameskip = -1; // auto
  515. defaultConfig.input_dev0 = PICO_INPUT_PAD_3BTN;
  516. defaultConfig.input_dev1 = PICO_INPUT_PAD_3BTN;
  517. defaultConfig.volume = 50;
  518. defaultConfig.gamma = 100;
  519. defaultConfig.scaling = 0;
  520. defaultConfig.turbo_rate = 15;
  521. defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000;
  522. defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000;
  523. defaultConfig.max_skip = 4;
  524. // platform specific overrides
  525. pemu_prep_defconfig();
  526. }
  527. void emu_set_defconfig(void)
  528. {
  529. memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));
  530. PicoIn.opt = currentConfig.s_PicoOpt;
  531. PicoIn.sndRate = currentConfig.s_PsndRate;
  532. PicoIn.regionOverride = currentConfig.s_PicoRegion;
  533. PicoIn.autoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
  534. }
  535. int emu_read_config(const char *rom_fname, int no_defaults)
  536. {
  537. char cfg[512];
  538. int ret;
  539. if (!no_defaults)
  540. emu_set_defconfig();
  541. if (rom_fname == NULL)
  542. {
  543. // global config
  544. make_config_cfg(cfg);
  545. ret = config_readsect(cfg, NULL);
  546. }
  547. else
  548. {
  549. char ext[16];
  550. int vol;
  551. if (config_slot != 0)
  552. snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
  553. else
  554. strcpy(ext, ".cfg");
  555. fname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext, rom_fname);
  556. // read user's config
  557. vol = currentConfig.volume;
  558. ret = config_readsect(cfg, NULL);
  559. currentConfig.volume = vol; // make vol global (bah)
  560. if (ret != 0)
  561. {
  562. // read global config, and apply game_def.cfg on top
  563. make_config_cfg(cfg);
  564. config_readsect(cfg, NULL);
  565. emu_make_path(cfg, "game_def.cfg", sizeof(cfg));
  566. ret = config_readsect(cfg, emu_make_rom_id(rom_fname));
  567. }
  568. }
  569. pemu_validate_config();
  570. PicoIn.overclockM68k = currentConfig.overclock_68k;
  571. // some sanity checks
  572. #ifdef PSP
  573. /* TODO: mv to plat_validate_config() */
  574. if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
  575. if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
  576. if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;
  577. #endif
  578. if (currentConfig.volume < 0 || currentConfig.volume > 99)
  579. currentConfig.volume = 50;
  580. if (ret == 0)
  581. config_slot_current = config_slot;
  582. return (ret == 0);
  583. }
  584. int emu_write_config(int is_game)
  585. {
  586. char cfg[512];
  587. int ret, write_lrom = 0;
  588. if (!is_game)
  589. {
  590. make_config_cfg(cfg);
  591. write_lrom = 1;
  592. } else {
  593. char ext[16];
  594. if (config_slot != 0)
  595. snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
  596. else
  597. strcpy(ext, ".cfg");
  598. romfname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext);
  599. }
  600. lprintf("emu_write_config: %s ", cfg);
  601. ret = config_write(cfg);
  602. if (write_lrom) config_writelrom(cfg);
  603. #ifdef __GP2X__
  604. sync();
  605. #endif
  606. lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
  607. if (ret == 0) config_slot_current = config_slot;
  608. return ret == 0;
  609. }
  610. /* always using built-in font */
  611. #define mk_text_out(name, type, val, topleft, step_x, step_y) \
  612. void name(int x, int y, const char *text) \
  613. { \
  614. int i, l, len = strlen(text); \
  615. type *screen = (type *)(topleft) + x * step_x + y * step_y; \
  616. \
  617. for (i = 0; i < len; i++, screen += 8 * step_x) \
  618. { \
  619. for (l = 0; l < 8; l++) \
  620. { \
  621. unsigned char fd = fontdata8x8[text[i] * 8 + l];\
  622. type *s = screen + l * step_y; \
  623. if (fd&0x80) s[step_x * 0] = val; \
  624. if (fd&0x40) s[step_x * 1] = val; \
  625. if (fd&0x20) s[step_x * 2] = val; \
  626. if (fd&0x10) s[step_x * 3] = val; \
  627. if (fd&0x08) s[step_x * 4] = val; \
  628. if (fd&0x04) s[step_x * 5] = val; \
  629. if (fd&0x02) s[step_x * 6] = val; \
  630. if (fd&0x01) s[step_x * 7] = val; \
  631. } \
  632. } \
  633. }
  634. mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_ppitch)
  635. mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_ppitch)
  636. mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,
  637. (char *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)
  638. mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,
  639. (short *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)
  640. #undef mk_text_out
  641. void emu_osd_text16(int x, int y, const char *text)
  642. {
  643. int len = strlen(text) * 8;
  644. int i, h;
  645. len++;
  646. if (x + len > g_screen_width)
  647. len = g_screen_width - x;
  648. for (h = 0; h < 8; h++) {
  649. unsigned short *p;
  650. p = (unsigned short *)g_screen_ptr
  651. + x + g_screen_ppitch * (y + h);
  652. for (i = len; i > 0; i--, p++)
  653. *p = (*p >> 2) & 0x39e7;
  654. }
  655. emu_text_out16(x, y, text);
  656. }
  657. static void update_movie(void)
  658. {
  659. int offs = Pico.m.frame_count*3 + 0x40;
  660. if (offs+3 > movie_size) {
  661. free(movie_data);
  662. movie_data = 0;
  663. emu_status_msg("END OF MOVIE.");
  664. lprintf("END OF MOVIE.\n");
  665. } else {
  666. // MXYZ SACB RLDU
  667. PicoIn.pad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
  668. if(!(movie_data[offs] & 0x10)) PicoIn.pad[0] |= 0x40; // C
  669. if(!(movie_data[offs] & 0x20)) PicoIn.pad[0] |= 0x10; // A
  670. if(!(movie_data[offs] & 0x40)) PicoIn.pad[0] |= 0x20; // B
  671. PicoIn.pad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
  672. if(!(movie_data[offs+1] & 0x10)) PicoIn.pad[1] |= 0x40; // C
  673. if(!(movie_data[offs+1] & 0x20)) PicoIn.pad[1] |= 0x10; // A
  674. if(!(movie_data[offs+1] & 0x40)) PicoIn.pad[1] |= 0x20; // B
  675. PicoIn.pad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
  676. if(!(movie_data[offs+2] & 0x01)) PicoIn.pad[0] |= 0x0400; // X
  677. if(!(movie_data[offs+2] & 0x04)) PicoIn.pad[0] |= 0x0100; // Z
  678. PicoIn.pad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
  679. if(!(movie_data[offs+2] & 0x10)) PicoIn.pad[1] |= 0x0400; // X
  680. if(!(movie_data[offs+2] & 0x40)) PicoIn.pad[1] |= 0x0100; // Z
  681. }
  682. }
  683. static int try_ropen_file(const char *fname, int *time)
  684. {
  685. struct stat st;
  686. FILE *f;
  687. f = fopen(fname, "rb");
  688. if (f) {
  689. if (time != NULL) {
  690. *time = 0;
  691. if (fstat(fileno(f), &st) == 0)
  692. *time = (int)st.st_mtime;
  693. }
  694. fclose(f);
  695. return 1;
  696. }
  697. return 0;
  698. }
  699. char *emu_get_save_fname(int load, int is_sram, int slot, int *time)
  700. {
  701. char *saveFname = static_buff;
  702. char ext[16];
  703. if (is_sram)
  704. {
  705. strcpy(ext, (PicoIn.AHW & PAHW_MCD) ? ".brm" : ".srm");
  706. romfname_ext(saveFname, sizeof(static_buff),
  707. (PicoIn.AHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);
  708. if (!load)
  709. return saveFname;
  710. if (try_ropen_file(saveFname, time))
  711. return saveFname;
  712. romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
  713. if (try_ropen_file(saveFname, time))
  714. return saveFname;
  715. }
  716. else
  717. {
  718. const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";
  719. const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";
  720. ext[0] = 0;
  721. if (slot > 0 && slot < 10)
  722. sprintf(ext, ".%i", slot);
  723. strcat(ext, ext_main);
  724. if (!load) {
  725. romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
  726. return saveFname;
  727. }
  728. else {
  729. romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
  730. if (try_ropen_file(saveFname, time))
  731. return saveFname;
  732. romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
  733. if (try_ropen_file(saveFname, time))
  734. return saveFname;
  735. // try the other ext
  736. ext[0] = 0;
  737. if (slot > 0 && slot < 10)
  738. sprintf(ext, ".%i", slot);
  739. strcat(ext, ext_othr);
  740. romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);
  741. if (try_ropen_file(saveFname, time))
  742. return saveFname;
  743. }
  744. }
  745. return NULL;
  746. }
  747. int emu_check_save_file(int slot, int *time)
  748. {
  749. return emu_get_save_fname(1, 0, slot, time) ? 1 : 0;
  750. }
  751. int emu_save_load_game(int load, int sram)
  752. {
  753. int ret = 0;
  754. char *saveFname;
  755. // make save filename
  756. saveFname = emu_get_save_fname(load, sram, state_slot, NULL);
  757. if (saveFname == NULL) {
  758. if (!sram)
  759. emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");
  760. return -1;
  761. }
  762. lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
  763. if (sram)
  764. {
  765. FILE *sramFile;
  766. int sram_size;
  767. unsigned char *sram_data;
  768. int truncate = 1;
  769. if (PicoIn.AHW & PAHW_MCD)
  770. {
  771. if (PicoIn.opt & POPT_EN_MCD_RAMCART) {
  772. sram_size = 0x12000;
  773. sram_data = Pico.sv.data;
  774. if (sram_data)
  775. memcpy(sram_data, Pico_mcd->bram, 0x2000);
  776. } else {
  777. sram_size = 0x2000;
  778. sram_data = Pico_mcd->bram;
  779. truncate = 0; // the .brm may contain RAM cart data after normal brm
  780. }
  781. } else {
  782. sram_size = Pico.sv.size;
  783. sram_data = Pico.sv.data;
  784. }
  785. if (sram_data == NULL)
  786. return 0; // cart saves forcefully disabled for this game
  787. if (load)
  788. {
  789. sramFile = fopen(saveFname, "rb");
  790. if (!sramFile)
  791. return -1;
  792. ret = fread(sram_data, 1, sram_size, sramFile);
  793. ret = ret > 0 ? 0 : -1;
  794. fclose(sramFile);
  795. if ((PicoIn.AHW & PAHW_MCD) && (PicoIn.opt&POPT_EN_MCD_RAMCART))
  796. memcpy(Pico_mcd->bram, sram_data, 0x2000);
  797. } else {
  798. // sram save needs some special processing
  799. // see if we have anything to save
  800. for (; sram_size > 0; sram_size--)
  801. if (sram_data[sram_size-1]) break;
  802. if (sram_size) {
  803. sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
  804. if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
  805. if (!sramFile) return -1;
  806. ret = fwrite(sram_data, 1, sram_size, sramFile);
  807. ret = (ret != sram_size) ? -1 : 0;
  808. fclose(sramFile);
  809. #ifdef __GP2X__
  810. sync();
  811. #endif
  812. }
  813. }
  814. return ret;
  815. }
  816. else
  817. {
  818. ret = PicoState(saveFname, !load);
  819. if (!ret) {
  820. #ifdef __GP2X__
  821. if (!load) sync();
  822. #endif
  823. emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
  824. } else {
  825. emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
  826. ret = -1;
  827. }
  828. return ret;
  829. }
  830. }
  831. void emu_set_fastforward(int set_on)
  832. {
  833. static void *set_PsndOut = NULL;
  834. static int set_Frameskip, set_EmuOpt, is_on = 0;
  835. if (set_on && !is_on) {
  836. set_PsndOut = PicoIn.sndOut;
  837. set_Frameskip = currentConfig.Frameskip;
  838. set_EmuOpt = currentConfig.EmuOpt;
  839. PicoIn.sndOut = NULL;
  840. currentConfig.Frameskip = 8;
  841. currentConfig.EmuOpt &= ~4;
  842. currentConfig.EmuOpt |= 0x40000;
  843. is_on = 1;
  844. emu_status_msg("FAST FORWARD");
  845. }
  846. else if (!set_on && is_on) {
  847. PicoIn.sndOut = set_PsndOut;
  848. currentConfig.Frameskip = set_Frameskip;
  849. currentConfig.EmuOpt = set_EmuOpt;
  850. PsndRerate(1);
  851. is_on = 0;
  852. // mainly to unbreak pcm
  853. if (PicoIn.AHW & PAHW_MCD)
  854. pcd_state_loaded();
  855. }
  856. }
  857. static void emu_tray_open(void)
  858. {
  859. engineState = PGS_TrayMenu;
  860. }
  861. static void emu_tray_close(void)
  862. {
  863. emu_status_msg("CD tray closed.");
  864. }
  865. void emu_32x_startup(void)
  866. {
  867. plat_video_toggle_renderer(0, 0); // HACK
  868. system_announce();
  869. }
  870. void emu_reset_game(void)
  871. {
  872. PicoReset();
  873. reset_timing = 1;
  874. }
  875. void run_events_pico(unsigned int events)
  876. {
  877. int lim_x;
  878. if (events & PEV_PICO_SWINP) {
  879. pico_inp_mode++;
  880. if (pico_inp_mode > 2)
  881. pico_inp_mode = 0;
  882. switch (pico_inp_mode) {
  883. case 2: emu_status_msg("Input: Pen on Pad"); break;
  884. case 1: emu_status_msg("Input: Pen on Storyware"); break;
  885. case 0: emu_status_msg("Input: Joystick");
  886. PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
  887. break;
  888. }
  889. }
  890. if (events & PEV_PICO_PPREV) {
  891. PicoPicohw.page--;
  892. if (PicoPicohw.page < 0)
  893. PicoPicohw.page = 0;
  894. emu_status_msg("Page %i", PicoPicohw.page);
  895. }
  896. if (events & PEV_PICO_PNEXT) {
  897. PicoPicohw.page++;
  898. if (PicoPicohw.page > 6)
  899. PicoPicohw.page = 6;
  900. emu_status_msg("Page %i", PicoPicohw.page);
  901. }
  902. if (pico_inp_mode == 0)
  903. return;
  904. /* handle other input modes */
  905. if (PicoIn.pad[0] & 1) pico_pen_y--;
  906. if (PicoIn.pad[0] & 2) pico_pen_y++;
  907. if (PicoIn.pad[0] & 4) pico_pen_x--;
  908. if (PicoIn.pad[0] & 8) pico_pen_x++;
  909. PicoIn.pad[0] &= ~0x0f; // release UDLR
  910. lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
  911. if (pico_pen_y < 8)
  912. pico_pen_y = 8;
  913. if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)
  914. pico_pen_y = 224 - PICO_PEN_ADJUST_Y;
  915. if (pico_pen_x < 0)
  916. pico_pen_x = 0;
  917. if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)
  918. pico_pen_x = lim_x - PICO_PEN_ADJUST_X;
  919. PicoPicohw.pen_pos[0] = pico_pen_x;
  920. if (!(Pico.video.reg[12] & 1))
  921. PicoPicohw.pen_pos[0] += pico_pen_x / 4;
  922. PicoPicohw.pen_pos[0] += 0x3c;
  923. PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
  924. }
  925. static void do_turbo(unsigned short *pad, int acts)
  926. {
  927. static int turbo_pad = 0;
  928. static unsigned char turbo_cnt[3] = { 0, 0, 0 };
  929. int inc = currentConfig.turbo_rate * 2;
  930. if (acts & 0x1000) {
  931. turbo_cnt[0] += inc;
  932. if (turbo_cnt[0] >= 60)
  933. turbo_pad ^= 0x10, turbo_cnt[0] = 0;
  934. }
  935. if (acts & 0x2000) {
  936. turbo_cnt[1] += inc;
  937. if (turbo_cnt[1] >= 60)
  938. turbo_pad ^= 0x20, turbo_cnt[1] = 0;
  939. }
  940. if (acts & 0x4000) {
  941. turbo_cnt[2] += inc;
  942. if (turbo_cnt[2] >= 60)
  943. turbo_pad ^= 0x40, turbo_cnt[2] = 0;
  944. }
  945. *pad |= turbo_pad & (acts >> 8);
  946. }
  947. static void run_events_ui(unsigned int which)
  948. {
  949. if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))
  950. {
  951. int do_it = 1;
  952. if ( emu_check_save_file(state_slot, NULL) &&
  953. (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||
  954. ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )
  955. {
  956. const char *nm;
  957. char tmp[64];
  958. int keys, len;
  959. strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE? " : "OVERWRITE SAVE? ");
  960. len = strlen(tmp);
  961. nm = in_get_key_name(-1, -PBTN_MOK);
  962. snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
  963. len = strlen(tmp);
  964. nm = in_get_key_name(-1, -PBTN_MBACK);
  965. snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
  966. plat_status_msg_busy_first(tmp);
  967. in_set_config_int(0, IN_CFG_BLOCKING, 1);
  968. while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))
  969. ;
  970. while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MOK | PBTN_MBACK)))
  971. ;
  972. if (keys & PBTN_MBACK)
  973. do_it = 0;
  974. while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))
  975. ;
  976. in_set_config_int(0, IN_CFG_BLOCKING, 0);
  977. plat_status_msg_clear();
  978. }
  979. if (do_it) {
  980. plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");
  981. PicoStateProgressCB = plat_status_msg_busy_next;
  982. emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);
  983. PicoStateProgressCB = NULL;
  984. }
  985. }
  986. if (which & PEV_SWITCH_RND)
  987. {
  988. plat_video_toggle_renderer(1, 0);
  989. }
  990. if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))
  991. {
  992. if (which & PEV_SSLOT_PREV) {
  993. state_slot -= 1;
  994. if (state_slot < 0)
  995. state_slot = 9;
  996. } else {
  997. state_slot += 1;
  998. if (state_slot > 9)
  999. state_slot = 0;
  1000. }
  1001. emu_status_msg("SAVE SLOT %i [%s]", state_slot,
  1002. emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");
  1003. }
  1004. if (which & PEV_RESET)
  1005. emu_reset_game();
  1006. if (which & PEV_MENU)
  1007. engineState = PGS_Menu;
  1008. }
  1009. void emu_update_input(void)
  1010. {
  1011. static int prev_events = 0;
  1012. int actions[IN_BINDTYPE_COUNT] = { 0, };
  1013. int pl_actions[2];
  1014. int events;
  1015. in_update(actions);
  1016. pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];
  1017. pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;
  1018. PicoIn.pad[0] = pl_actions[0] & 0xfff;
  1019. PicoIn.pad[1] = pl_actions[1] & 0xfff;
  1020. if (pl_actions[0] & 0x7000)
  1021. do_turbo(&PicoIn.pad[0], pl_actions[0]);
  1022. if (pl_actions[1] & 0x7000)
  1023. do_turbo(&PicoIn.pad[1], pl_actions[1]);
  1024. events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
  1025. // volume is treated in special way and triggered every frame
  1026. if (events & (PEV_VOL_DOWN|PEV_VOL_UP))
  1027. plat_update_volume(1, events & PEV_VOL_UP);
  1028. if ((events ^ prev_events) & PEV_FF) {
  1029. emu_set_fastforward(events & PEV_FF);
  1030. plat_update_volume(0, 0);
  1031. reset_timing = 1;
  1032. }
  1033. events &= ~prev_events;
  1034. if (PicoIn.AHW == PAHW_PICO)
  1035. run_events_pico(events);
  1036. if (events)
  1037. run_events_ui(events);
  1038. if (movie_data)
  1039. update_movie();
  1040. prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
  1041. }
  1042. static void mkdir_path(char *path_with_reserve, int pos, const char *name)
  1043. {
  1044. strcpy(path_with_reserve + pos, name);
  1045. if (plat_is_dir(path_with_reserve))
  1046. return;
  1047. if (mkdir(path_with_reserve, 0777) < 0)
  1048. lprintf("failed to create: %s\n", path_with_reserve);
  1049. }
  1050. void emu_cmn_forced_frame(int no_scale, int do_emu)
  1051. {
  1052. int po_old = PicoIn.opt;
  1053. int y;
  1054. for (y = 0; y < g_screen_height; y++)
  1055. memset32((short *)g_screen_ptr + g_screen_ppitch * y, 0,
  1056. g_screen_width * 2 / 4);
  1057. PicoIn.opt &= ~POPT_ALT_RENDERER;
  1058. PicoIn.opt |= POPT_ACC_SPRITES;
  1059. if (!no_scale && currentConfig.scaling)
  1060. PicoIn.opt |= POPT_EN_SOFTSCALE;
  1061. PicoDrawSetOutFormat(PDF_RGB555, 1);
  1062. Pico.m.dirtyPal = 1;
  1063. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  1064. if (do_emu)
  1065. PicoFrame();
  1066. else
  1067. PicoFrameDrawOnly();
  1068. PicoIn.opt = po_old;
  1069. }
  1070. void emu_init(void)
  1071. {
  1072. char path[512];
  1073. int pos;
  1074. #if 0
  1075. // FIXME: handle through menu, etc
  1076. FILE *f;
  1077. f = fopen("32X_M_BIOS.BIN", "rb");
  1078. p32x_bios_m = malloc(2048);
  1079. fread(p32x_bios_m, 1, 2048, f);
  1080. fclose(f);
  1081. f = fopen("32X_S_BIOS.BIN", "rb");
  1082. p32x_bios_s = malloc(1024);
  1083. fread(p32x_bios_s, 1, 1024, f);
  1084. fclose(f);
  1085. #endif
  1086. /* make dirs for saves */
  1087. pos = plat_get_root_dir(path, sizeof(path) - 4);
  1088. mkdir_path(path, pos, "mds");
  1089. mkdir_path(path, pos, "srm");
  1090. mkdir_path(path, pos, "brm");
  1091. mkdir_path(path, pos, "cfg");
  1092. pprof_init();
  1093. make_config_cfg(path);
  1094. config_readlrom(path);
  1095. PicoInit();
  1096. PicoIn.osdMessage = plat_status_msg_busy_next;
  1097. PicoIn.mcdTrayOpen = emu_tray_open;
  1098. PicoIn.mcdTrayClose = emu_tray_close;
  1099. sndout_init();
  1100. }
  1101. void emu_finish(void)
  1102. {
  1103. // save SRAM
  1104. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
  1105. emu_save_load_game(0, 1);
  1106. Pico.sv.changed = 0;
  1107. }
  1108. if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
  1109. char cfg[512];
  1110. make_config_cfg(cfg);
  1111. config_writelrom(cfg);
  1112. #ifdef __GP2X__
  1113. sync();
  1114. #endif
  1115. }
  1116. pprof_finish();
  1117. PicoExit();
  1118. sndout_exit();
  1119. }
  1120. static void snd_write_nonblocking(int len)
  1121. {
  1122. sndout_write_nb(PicoIn.sndOut, len);
  1123. }
  1124. void emu_sound_start(void)
  1125. {
  1126. PicoIn.sndOut = NULL;
  1127. if (currentConfig.EmuOpt & EOPT_EN_SOUND)
  1128. {
  1129. int is_stereo = (PicoIn.opt & POPT_EN_STEREO) ? 1 : 0;
  1130. PsndRerate(Pico.m.frame_count ? 1 : 0);
  1131. printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
  1132. PicoIn.sndRate, Pico.snd.len, is_stereo, Pico.m.pal);
  1133. sndout_start(PicoIn.sndRate, is_stereo);
  1134. PicoIn.writeSound = snd_write_nonblocking;
  1135. plat_update_volume(0, 0);
  1136. memset(sndBuffer, 0, sizeof(sndBuffer));
  1137. PicoIn.sndOut = sndBuffer;
  1138. }
  1139. }
  1140. void emu_sound_stop(void)
  1141. {
  1142. sndout_stop();
  1143. }
  1144. void emu_sound_wait(void)
  1145. {
  1146. sndout_wait();
  1147. }
  1148. static void emu_loop_prep(void)
  1149. {
  1150. static int pal_old = -1;
  1151. static int filter_old = -1;
  1152. if (currentConfig.CPUclock != plat_target_cpu_clock_get())
  1153. plat_target_cpu_clock_set(currentConfig.CPUclock);
  1154. if (Pico.m.pal != pal_old) {
  1155. plat_target_lcdrate_set(Pico.m.pal);
  1156. pal_old = Pico.m.pal;
  1157. }
  1158. if (currentConfig.filter != filter_old) {
  1159. plat_target_hwfilter_set(currentConfig.filter);
  1160. filter_old = currentConfig.filter;
  1161. }
  1162. plat_target_gamma_set(currentConfig.gamma, 0);
  1163. pemu_loop_prep();
  1164. }
  1165. /* our tick here is 1 us right now */
  1166. #define ms_to_ticks(x) (unsigned int)(x * 1000)
  1167. #define get_ticks() plat_get_ticks_us()
  1168. void emu_loop(void)
  1169. {
  1170. int frames_done, frames_shown; /* actual frames for fps counter */
  1171. int target_frametime_x3;
  1172. unsigned int timestamp_x3 = 0;
  1173. unsigned int timestamp_aim_x3 = 0;
  1174. unsigned int timestamp_fps_x3 = 0;
  1175. char *notice_msg = NULL;
  1176. char fpsbuff[24];
  1177. int fskip_cnt = 0;
  1178. fpsbuff[0] = 0;
  1179. PicoLoopPrepare();
  1180. plat_video_loop_prepare();
  1181. emu_loop_prep();
  1182. pemu_sound_start();
  1183. /* number of ticks per frame */
  1184. if (Pico.m.pal)
  1185. target_frametime_x3 = 3 * ms_to_ticks(1000) / 50;
  1186. else
  1187. target_frametime_x3 = 3 * ms_to_ticks(1000) / 60;
  1188. reset_timing = 1;
  1189. frames_done = frames_shown = 0;
  1190. /* loop with resync every 1 sec. */
  1191. while (engineState == PGS_Running)
  1192. {
  1193. int skip = 0;
  1194. int diff;
  1195. pprof_start(main);
  1196. if (reset_timing) {
  1197. reset_timing = 0;
  1198. plat_video_wait_vsync();
  1199. timestamp_aim_x3 = get_ticks() * 3;
  1200. timestamp_fps_x3 = timestamp_aim_x3;
  1201. fskip_cnt = 0;
  1202. }
  1203. else if (currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) {
  1204. timestamp_aim_x3 = get_ticks() * 3;
  1205. }
  1206. timestamp_x3 = get_ticks() * 3;
  1207. // show notice_msg message?
  1208. if (notice_msg_time != 0)
  1209. {
  1210. static int noticeMsgSum;
  1211. if (timestamp_x3 - ms_to_ticks(notice_msg_time) * 3
  1212. > ms_to_ticks(STATUS_MSG_TIMEOUT) * 3)
  1213. {
  1214. notice_msg_time = 0;
  1215. notice_msg = NULL;
  1216. plat_status_msg_clear();
  1217. }
  1218. else {
  1219. int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
  1220. if (sum != noticeMsgSum) {
  1221. plat_status_msg_clear();
  1222. noticeMsgSum = sum;
  1223. }
  1224. notice_msg = noticeMsg;
  1225. }
  1226. }
  1227. // second changed?
  1228. if (timestamp_x3 - timestamp_fps_x3 >= ms_to_ticks(1000) * 3)
  1229. {
  1230. #ifdef BENCHMARK
  1231. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  1232. if (++bench == 10) {
  1233. bench = 0;
  1234. bench_fps_s = bench_fps;
  1235. bf[bfp++ & 3] = bench_fps;
  1236. bench_fps = 0;
  1237. }
  1238. bench_fps += frames_shown;
  1239. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  1240. printf("%s\n", fpsbuff);
  1241. #else
  1242. if (currentConfig.EmuOpt & EOPT_SHOW_FPS)
  1243. snprintf(fpsbuff, 8, "%02i/%02i ", frames_shown, frames_done);
  1244. #endif
  1245. frames_shown = frames_done = 0;
  1246. timestamp_fps_x3 += ms_to_ticks(1000) * 3;
  1247. }
  1248. #ifdef PFRAMES
  1249. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  1250. #endif
  1251. diff = timestamp_aim_x3 - timestamp_x3;
  1252. if (currentConfig.Frameskip >= 0) // frameskip enabled (or 0)
  1253. {
  1254. if (fskip_cnt < currentConfig.Frameskip) {
  1255. fskip_cnt++;
  1256. skip = 1;
  1257. }
  1258. else {
  1259. fskip_cnt = 0;
  1260. }
  1261. }
  1262. else if (diff < -target_frametime_x3)
  1263. {
  1264. /* no time left for this frame - skip */
  1265. /* limit auto frameskip to max_skip */
  1266. if (fskip_cnt < currentConfig.max_skip) {
  1267. fskip_cnt++;
  1268. skip = 1;
  1269. }
  1270. else {
  1271. fskip_cnt = 0;
  1272. }
  1273. } else
  1274. fskip_cnt = 0;
  1275. // don't go in debt too much
  1276. while (diff < -target_frametime_x3 * 3) {
  1277. timestamp_aim_x3 += target_frametime_x3;
  1278. diff = timestamp_aim_x3 - timestamp_x3;
  1279. }
  1280. emu_update_input();
  1281. if (skip) {
  1282. int do_audio = diff > -target_frametime_x3 * 2;
  1283. PicoIn.skipFrame = do_audio ? 1 : 2;
  1284. PicoFrame();
  1285. PicoIn.skipFrame = 0;
  1286. }
  1287. else {
  1288. PicoFrame();
  1289. pemu_finalize_frame(fpsbuff, notice_msg);
  1290. frames_shown++;
  1291. }
  1292. frames_done++;
  1293. timestamp_aim_x3 += target_frametime_x3;
  1294. if (!skip && !flip_after_sync)
  1295. plat_video_flip();
  1296. /* frame limiter */
  1297. if (!skip && !reset_timing
  1298. && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
  1299. {
  1300. unsigned int timestamp = get_ticks();
  1301. diff = timestamp_aim_x3 - timestamp * 3;
  1302. // sleep or vsync if we are still too fast
  1303. if (diff > target_frametime_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) {
  1304. // we are too fast
  1305. plat_video_wait_vsync();
  1306. timestamp = get_ticks();
  1307. diff = timestamp * 3 - timestamp_aim_x3;
  1308. }
  1309. if (diff > target_frametime_x3) {
  1310. // still too fast
  1311. plat_wait_till_us(timestamp + (diff - target_frametime_x3) / 3);
  1312. }
  1313. }
  1314. if (!skip && flip_after_sync)
  1315. plat_video_flip();
  1316. pprof_end(main);
  1317. }
  1318. emu_set_fastforward(0);
  1319. // save SRAM
  1320. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
  1321. plat_status_msg_busy_first("Writing SRAM/BRAM...");
  1322. emu_save_load_game(0, 1);
  1323. Pico.sv.changed = 0;
  1324. }
  1325. pemu_loop_end();
  1326. emu_sound_stop();
  1327. }