emu.c 37 KB

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