emu.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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 ((p = strrchr(dst, '.')) != NULL)
  111. dst[p-dst] = 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. PicoDrawSetOutBuf(g_screen_ptr, g_screen_ppitch * 2);
  1063. Pico.m.dirtyPal = 1;
  1064. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  1065. if (do_emu)
  1066. PicoFrame();
  1067. else
  1068. PicoFrameDrawOnly();
  1069. PicoIn.opt = po_old;
  1070. }
  1071. void emu_init(void)
  1072. {
  1073. char path[512];
  1074. int pos;
  1075. #if 0
  1076. // FIXME: handle through menu, etc
  1077. FILE *f;
  1078. f = fopen("32X_M_BIOS.BIN", "rb");
  1079. p32x_bios_m = malloc(2048);
  1080. fread(p32x_bios_m, 1, 2048, f);
  1081. fclose(f);
  1082. f = fopen("32X_S_BIOS.BIN", "rb");
  1083. p32x_bios_s = malloc(1024);
  1084. fread(p32x_bios_s, 1, 1024, f);
  1085. fclose(f);
  1086. #endif
  1087. /* make dirs for saves */
  1088. pos = plat_get_root_dir(path, sizeof(path) - 4);
  1089. mkdir_path(path, pos, "mds");
  1090. mkdir_path(path, pos, "srm");
  1091. mkdir_path(path, pos, "brm");
  1092. mkdir_path(path, pos, "cfg");
  1093. pprof_init();
  1094. make_config_cfg(path);
  1095. config_readlrom(path);
  1096. PicoInit();
  1097. PicoIn.osdMessage = plat_status_msg_busy_next;
  1098. PicoIn.mcdTrayOpen = emu_tray_open;
  1099. PicoIn.mcdTrayClose = emu_tray_close;
  1100. sndout_init();
  1101. }
  1102. void emu_finish(void)
  1103. {
  1104. // save SRAM
  1105. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
  1106. emu_save_load_game(0, 1);
  1107. Pico.sv.changed = 0;
  1108. }
  1109. if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
  1110. char cfg[512];
  1111. make_config_cfg(cfg);
  1112. config_writelrom(cfg);
  1113. #ifdef __GP2X__
  1114. sync();
  1115. #endif
  1116. }
  1117. pprof_finish();
  1118. PicoExit();
  1119. sndout_exit();
  1120. }
  1121. static void snd_write_nonblocking(int len)
  1122. {
  1123. sndout_write_nb(PicoIn.sndOut, len);
  1124. }
  1125. void emu_sound_start(void)
  1126. {
  1127. PicoIn.sndOut = NULL;
  1128. if (currentConfig.EmuOpt & EOPT_EN_SOUND)
  1129. {
  1130. int is_stereo = (PicoIn.opt & POPT_EN_STEREO) ? 1 : 0;
  1131. PsndRerate(Pico.m.frame_count ? 1 : 0);
  1132. printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
  1133. PicoIn.sndRate, Pico.snd.len, is_stereo, Pico.m.pal);
  1134. sndout_start(PicoIn.sndRate, is_stereo);
  1135. PicoIn.writeSound = snd_write_nonblocking;
  1136. plat_update_volume(0, 0);
  1137. memset(sndBuffer, 0, sizeof(sndBuffer));
  1138. PicoIn.sndOut = sndBuffer;
  1139. }
  1140. }
  1141. void emu_sound_stop(void)
  1142. {
  1143. sndout_stop();
  1144. }
  1145. void emu_sound_wait(void)
  1146. {
  1147. sndout_wait();
  1148. }
  1149. static void emu_loop_prep(void)
  1150. {
  1151. static int pal_old = -1;
  1152. static int filter_old = -1;
  1153. if (currentConfig.CPUclock != plat_target_cpu_clock_get())
  1154. plat_target_cpu_clock_set(currentConfig.CPUclock);
  1155. if (Pico.m.pal != pal_old) {
  1156. plat_target_lcdrate_set(Pico.m.pal);
  1157. pal_old = Pico.m.pal;
  1158. }
  1159. if (currentConfig.filter != filter_old) {
  1160. plat_target_hwfilter_set(currentConfig.filter);
  1161. filter_old = currentConfig.filter;
  1162. }
  1163. plat_target_gamma_set(currentConfig.gamma, 0);
  1164. pemu_loop_prep();
  1165. }
  1166. /* our tick here is 1 us right now */
  1167. #define ms_to_ticks(x) (unsigned int)(x * 1000)
  1168. #define get_ticks() plat_get_ticks_us()
  1169. void emu_loop(void)
  1170. {
  1171. int frames_done, frames_shown; /* actual frames for fps counter */
  1172. int target_frametime_x3;
  1173. unsigned int timestamp_x3 = 0;
  1174. unsigned int timestamp_aim_x3 = 0;
  1175. unsigned int timestamp_fps_x3 = 0;
  1176. char *notice_msg = NULL;
  1177. char fpsbuff[24];
  1178. int fskip_cnt = 0;
  1179. fpsbuff[0] = 0;
  1180. PicoLoopPrepare();
  1181. plat_video_loop_prepare();
  1182. emu_loop_prep();
  1183. pemu_sound_start();
  1184. /* number of ticks per frame */
  1185. if (Pico.m.pal)
  1186. target_frametime_x3 = 3 * ms_to_ticks(1000) / 50;
  1187. else
  1188. target_frametime_x3 = 3 * ms_to_ticks(1000) / 60;
  1189. reset_timing = 1;
  1190. frames_done = frames_shown = 0;
  1191. /* loop with resync every 1 sec. */
  1192. while (engineState == PGS_Running)
  1193. {
  1194. int skip = 0;
  1195. int diff;
  1196. pprof_start(main);
  1197. if (reset_timing) {
  1198. reset_timing = 0;
  1199. plat_video_wait_vsync();
  1200. timestamp_aim_x3 = get_ticks() * 3;
  1201. timestamp_fps_x3 = timestamp_aim_x3;
  1202. fskip_cnt = 0;
  1203. }
  1204. else if (currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) {
  1205. timestamp_aim_x3 = get_ticks() * 3;
  1206. }
  1207. timestamp_x3 = get_ticks() * 3;
  1208. // show notice_msg message?
  1209. if (notice_msg_time != 0)
  1210. {
  1211. static int noticeMsgSum;
  1212. if (timestamp_x3 - ms_to_ticks(notice_msg_time) * 3
  1213. > ms_to_ticks(STATUS_MSG_TIMEOUT) * 3)
  1214. {
  1215. notice_msg_time = 0;
  1216. notice_msg = NULL;
  1217. plat_status_msg_clear();
  1218. }
  1219. else {
  1220. int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
  1221. if (sum != noticeMsgSum) {
  1222. plat_status_msg_clear();
  1223. noticeMsgSum = sum;
  1224. }
  1225. notice_msg = noticeMsg;
  1226. }
  1227. }
  1228. // second changed?
  1229. if (timestamp_x3 - timestamp_fps_x3 >= ms_to_ticks(1000) * 3)
  1230. {
  1231. #ifdef BENCHMARK
  1232. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  1233. if (++bench == 10) {
  1234. bench = 0;
  1235. bench_fps_s = bench_fps;
  1236. bf[bfp++ & 3] = bench_fps;
  1237. bench_fps = 0;
  1238. }
  1239. bench_fps += frames_shown;
  1240. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  1241. printf("%s\n", fpsbuff);
  1242. #else
  1243. if (currentConfig.EmuOpt & EOPT_SHOW_FPS)
  1244. snprintf(fpsbuff, 8, "%02i/%02i ", frames_shown, frames_done);
  1245. #endif
  1246. frames_shown = frames_done = 0;
  1247. timestamp_fps_x3 += ms_to_ticks(1000) * 3;
  1248. }
  1249. #ifdef PFRAMES
  1250. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  1251. #endif
  1252. diff = timestamp_aim_x3 - timestamp_x3;
  1253. if (currentConfig.Frameskip >= 0) // frameskip enabled (or 0)
  1254. {
  1255. if (fskip_cnt < currentConfig.Frameskip) {
  1256. fskip_cnt++;
  1257. skip = 1;
  1258. }
  1259. else {
  1260. fskip_cnt = 0;
  1261. }
  1262. }
  1263. else if (diff < -target_frametime_x3)
  1264. {
  1265. /* no time left for this frame - skip */
  1266. /* limit auto frameskip to max_skip */
  1267. if (fskip_cnt < currentConfig.max_skip) {
  1268. fskip_cnt++;
  1269. skip = 1;
  1270. }
  1271. else {
  1272. fskip_cnt = 0;
  1273. }
  1274. } else
  1275. fskip_cnt = 0;
  1276. // don't go in debt too much
  1277. while (diff < -target_frametime_x3 * 3) {
  1278. timestamp_aim_x3 += target_frametime_x3;
  1279. diff = timestamp_aim_x3 - timestamp_x3;
  1280. }
  1281. emu_update_input();
  1282. if (skip) {
  1283. int do_audio = diff > -target_frametime_x3 * 2;
  1284. PicoIn.skipFrame = do_audio ? 1 : 2;
  1285. PicoFrame();
  1286. PicoIn.skipFrame = 0;
  1287. }
  1288. else {
  1289. PicoFrame();
  1290. pemu_finalize_frame(fpsbuff, notice_msg);
  1291. frames_shown++;
  1292. }
  1293. frames_done++;
  1294. timestamp_aim_x3 += target_frametime_x3;
  1295. if (!skip && !flip_after_sync)
  1296. plat_video_flip();
  1297. /* frame limiter */
  1298. if (!skip && !reset_timing
  1299. && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
  1300. {
  1301. unsigned int timestamp = get_ticks();
  1302. diff = timestamp_aim_x3 - timestamp * 3;
  1303. // sleep or vsync if we are still too fast
  1304. if (diff > target_frametime_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) {
  1305. // we are too fast
  1306. plat_video_wait_vsync();
  1307. timestamp = get_ticks();
  1308. diff = timestamp * 3 - timestamp_aim_x3;
  1309. }
  1310. if (diff > target_frametime_x3) {
  1311. // still too fast
  1312. plat_wait_till_us(timestamp + (diff - target_frametime_x3) / 3);
  1313. }
  1314. }
  1315. if (!skip && flip_after_sync)
  1316. plat_video_flip();
  1317. pprof_end(main);
  1318. }
  1319. emu_set_fastforward(0);
  1320. // save SRAM
  1321. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
  1322. plat_status_msg_busy_first("Writing SRAM/BRAM...");
  1323. emu_save_load_game(0, 1);
  1324. Pico.sv.changed = 0;
  1325. }
  1326. pemu_loop_end();
  1327. emu_sound_stop();
  1328. }