emu.c 36 KB

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