emu.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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 "configfile_fk.h"
  23. #include "input_pico.h"
  24. #include "menu_pico.h"
  25. #include "config_file.h"
  26. #include <pico/pico_int.h>
  27. #include <pico/patch.h>
  28. #ifdef USE_LIBRETRO_VFS
  29. #include "file_stream_transforms.h"
  30. #endif
  31. #if defined(__GNUC__) && __GNUC__ >= 7
  32. #pragma GCC diagnostic ignored "-Wformat-truncation"
  33. #endif
  34. #ifndef _WIN32
  35. #define PATH_SEP "/"
  36. #define PATH_SEP_C '/'
  37. #else
  38. #define PATH_SEP "\\"
  39. #define PATH_SEP_C '\\'
  40. #endif
  41. #define STATUS_MSG_TIMEOUT 2000
  42. void *g_screen_ptr;
  43. int g_screen_width = 320;
  44. int g_screen_height = 240;
  45. int g_screen_ppitch = 320; // pitch in pixels
  46. const char *PicoConfigFile = "config2.cfg";
  47. currentConfig_t currentConfig, defaultConfig;
  48. int state_slot = 0;
  49. int config_slot = 0, config_slot_current = 0;
  50. int pico_pen_x = 320/2, pico_pen_y = 240/2;
  51. int pico_inp_mode;
  52. int flip_after_sync;
  53. int engineState = PGS_Menu;
  54. int show_fps_bypass = 0;
  55. int need_screen_cleared = 0;
  56. static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
  57. /* tmp buff to reduce stack usage for plats with small stack */
  58. static char static_buff[1024];
  59. const char *rom_fname_reload;
  60. char rom_fname_loaded[1024];
  61. int reset_timing = 0;
  62. static unsigned int notice_msg_time; /* when started showing */
  63. static char noticeMsg[40];
  64. unsigned char *movie_data = NULL;
  65. static int movie_size = 0;
  66. /* don't use tolower() for easy old glibc binary compatibility */
  67. static void strlwr_(char *string)
  68. {
  69. char *p;
  70. for (p = string; *p; p++)
  71. if ('A' <= *p && *p <= 'Z')
  72. *p += 'a' - 'A';
  73. }
  74. static int try_rfn_cut(char *fname)
  75. {
  76. FILE *tmp;
  77. char *p;
  78. p = fname + strlen(fname) - 1;
  79. for (; p > fname; p--)
  80. if (*p == '.') break;
  81. *p = 0;
  82. if((tmp = fopen(fname, "rb"))) {
  83. fclose(tmp);
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. static void get_ext(const char *file, char *ext)
  89. {
  90. const char *p;
  91. p = file + strlen(file) - 4;
  92. if (p < file) p = file;
  93. strncpy(ext, p, 4);
  94. ext[4] = 0;
  95. strlwr_(ext);
  96. }
  97. static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)
  98. {
  99. int prefix_len = 0;
  100. const char *p;
  101. *dst = 0;
  102. if (prefix) {
  103. /*int len = plat_get_root_dir(dst, dstlen);
  104. strcpy(dst + len, prefix);
  105. prefix_len = len + strlen(prefix);*/
  106. /* Saves are in ROM folder */
  107. prefix_len = strlen(mRomPath)+1;
  108. sprintf(dst, "%s/", mRomPath);
  109. }
  110. p = fname + strlen(fname) - 1;
  111. for (; p >= fname && *p != PATH_SEP_C; p--)
  112. ;
  113. p++;
  114. strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);
  115. dst[dstlen - 8] = 0;
  116. if ((p = strrchr(dst, '.')) != NULL)
  117. dst[p-dst] = 0;
  118. if (ext)
  119. strcat(dst, ext);
  120. }
  121. static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)
  122. {
  123. fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);
  124. }
  125. void emu_status_msg(const char *format, ...)
  126. {
  127. va_list vl;
  128. int ret;
  129. va_start(vl, format);
  130. ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);
  131. va_end(vl);
  132. /* be sure old text gets overwritten */
  133. for (; ret < 28; ret++)
  134. noticeMsg[ret] = ' ';
  135. noticeMsg[ret] = 0;
  136. notice_msg_time = plat_get_ticks_ms();
  137. }
  138. static const char * const biosfiles_us[] = {
  139. "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U"
  140. };
  141. static const char * const biosfiles_eu[] = {
  142. "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E"
  143. };
  144. static const char * const biosfiles_jp[] = {
  145. "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
  146. };
  147. static const char *find_bios(int *region, const char *cd_fname)
  148. {
  149. int i, count;
  150. const char * const *files;
  151. FILE *f = NULL;
  152. int ret;
  153. // we need to have config loaded at this point
  154. ret = emu_read_config(cd_fname, 0);
  155. if (!ret) emu_read_config(NULL, 0);
  156. if (PicoIn.regionOverride) {
  157. *region = PicoIn.regionOverride;
  158. lprintf("override region to %s\n", *region != 4 ?
  159. (*region == 8 ? "EU" : "JAP") : "USA");
  160. }
  161. if (*region == 4) { // US
  162. files = biosfiles_us;
  163. count = sizeof(biosfiles_us) / sizeof(char *);
  164. } else if (*region == 8) { // EU
  165. files = biosfiles_eu;
  166. count = sizeof(biosfiles_eu) / sizeof(char *);
  167. } else if (*region == 1 || *region == 2) {
  168. files = biosfiles_jp;
  169. count = sizeof(biosfiles_jp) / sizeof(char *);
  170. } else {
  171. return 0;
  172. }
  173. /** Check Bios files in home directory */
  174. for (i = 0; i < count; i++)
  175. {
  176. emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);
  177. //printf("bios name static_buff: %s\n", static_buff);
  178. strcat(static_buff, ".bin");
  179. f = fopen(static_buff, "rb");
  180. if (f) break;
  181. static_buff[strlen(static_buff) - 4] = 0;
  182. strcat(static_buff, ".zip");
  183. f = fopen(static_buff, "rb");
  184. if (f) break;
  185. }
  186. /** Check Bios files in <rom path>/bios */
  187. if(!f){
  188. for (i = 0; i < count; i++)
  189. {
  190. sprintf(static_buff, "%s/bios/%s", mRomPath, files[i]);
  191. printf("static_buff: %s\n", static_buff);
  192. strcat(static_buff, ".bin");
  193. f = fopen(static_buff, "rb");
  194. if (f) break;
  195. static_buff[strlen(static_buff) - 4] = 0;
  196. strcat(static_buff, ".zip");
  197. f = fopen(static_buff, "rb");
  198. if (f) break;
  199. }
  200. }
  201. /** Check Bios files in rom path */
  202. if(!f){
  203. for (i = 0; i < count; i++)
  204. {
  205. sprintf(static_buff, "%s/%s", mRomPath, files[i]);
  206. printf("static_buff: %s\n", static_buff);
  207. strcat(static_buff, ".bin");
  208. f = fopen(static_buff, "rb");
  209. if (f) break;
  210. static_buff[strlen(static_buff) - 4] = 0;
  211. strcat(static_buff, ".zip");
  212. f = fopen(static_buff, "rb");
  213. if (f) break;
  214. }
  215. }
  216. if (f) {
  217. lprintf("using bios: %s\n", static_buff);
  218. fclose(f);
  219. return static_buff;
  220. } else {
  221. char* country;
  222. char** bios_names;
  223. if(*region == 4){ //USA
  224. country = "USA";
  225. bios_names = biosfiles_us;
  226. }
  227. else if(*region == 8){ //EU
  228. country = "EU";
  229. bios_names = biosfiles_eu;
  230. }
  231. else{ //JAP
  232. country = "JAP";
  233. bios_names = biosfiles_jp;
  234. }
  235. sprintf(static_buff, "no %s BIOS files found, read docs", country);
  236. menu_update_msg(static_buff);
  237. /** Set notif for BIOS */
  238. char shell_cmd[400];
  239. sprintf(shell_cmd, "%s 0 \" %s BIOS FILE MISSING^^Connect your FunKey S to ^your computer and copy the^BIOS file in the folder:^ Sega Genesis/bios/^^The file can be called:^ - %s.bin^ - %s.bin^ - %s.bin^ - %s.bin^^For more instructions:^www.funkey-project.com^^Press any button to exit...\"",
  240. SHELL_CMD_NOTIF_SET, country, bios_names[0], bios_names[1], bios_names[2], bios_names[3]);
  241. FILE *fp = popen(shell_cmd, "r");
  242. if (fp == NULL) {
  243. printf("In %s, Failed to run command %s\n", __func__, shell_cmd);
  244. } else {
  245. pclose(fp);
  246. }
  247. /** Wait for key press */
  248. SDL_Event event;
  249. while(event.type != SDL_KEYUP && event.type != SDL_QUIT){
  250. while (SDL_PollEvent(&event))
  251. SDL_Delay(60);
  252. }
  253. /** Clear notif for BIOS */
  254. fp = popen(SHELL_CMD_NOTIF_CLEAR, "r");
  255. if (fp == NULL) {
  256. printf("In %s, Failed to run command %s\n", __func__, SHELL_CMD_NOTIF_CLEAR);
  257. } else {
  258. pclose(fp);
  259. }
  260. /** Force clean exit */
  261. //engineState = PGS_Quit;
  262. emu_finish();
  263. plat_finish();
  264. plat_target_finish();
  265. exit(0);
  266. return NULL;
  267. }
  268. }
  269. /* check if the name begins with BIOS name */
  270. /*
  271. static int emu_isBios(const char *name)
  272. {
  273. int i;
  274. for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
  275. if (strstr(name, biosfiles_us[i]) != NULL) return 1;
  276. for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
  277. if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
  278. for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
  279. if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
  280. return 0;
  281. }
  282. */
  283. static int extract_text(char *dest, const unsigned char *src, int len, int swab)
  284. {
  285. char *p = dest;
  286. int i;
  287. if (swab) swab = 1;
  288. for (i = len - 1; i >= 0; i--)
  289. {
  290. if (src[i^swab] != ' ') break;
  291. }
  292. len = i + 1;
  293. for (i = 0; i < len; i++)
  294. {
  295. unsigned char s = src[i^swab];
  296. if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
  297. s != '[' && s != ']' && s != '\\')
  298. {
  299. *p++ = s;
  300. }
  301. else
  302. {
  303. sprintf(p, "\\%02x", s);
  304. p += 3;
  305. }
  306. }
  307. return p - dest;
  308. }
  309. static char *emu_make_rom_id(const char *fname)
  310. {
  311. static char id_string[3+0xe*3+0x3*3+0x30*3+3];
  312. int pos, swab = 1;
  313. if (PicoIn.AHW & PAHW_MCD) {
  314. strcpy(id_string, "CD|");
  315. swab = 0;
  316. }
  317. else if (PicoIn.AHW & PAHW_SMS)
  318. strcpy(id_string, "MS|");
  319. else strcpy(id_string, "MD|");
  320. pos = 3;
  321. if (!(PicoIn.AHW & PAHW_SMS)) {
  322. pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial
  323. id_string[pos] = '|'; pos++;
  324. pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region
  325. id_string[pos] = '|'; pos++;
  326. pos += extract_text(id_string + pos, media_id_header + 0x50, 0x30, swab); // overseas name
  327. id_string[pos] = 0;
  328. if (pos > 5)
  329. return id_string;
  330. pos = 3;
  331. }
  332. // can't find name in ROM, use filename
  333. fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);
  334. return id_string;
  335. }
  336. // buffer must be at least 150 byte long
  337. void emu_get_game_name(char *str150)
  338. {
  339. int ret, swab = (PicoIn.AHW & PAHW_MCD) ? 0 : 1;
  340. char *s, *d;
  341. ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name
  342. for (s = d = str150 + 1; s < str150+ret; s++)
  343. {
  344. if (*s == 0) break;
  345. if (*s != ' ' || d[-1] != ' ')
  346. *d++ = *s;
  347. }
  348. *d = 0;
  349. }
  350. static void system_announce(void)
  351. {
  352. const char *sys_name, *tv_standard, *extra = "";
  353. int fps;
  354. if (PicoIn.AHW & PAHW_SMS) {
  355. sys_name = "Master System";
  356. if (Pico.m.hardware & 0x1)
  357. sys_name = "Game Gear";
  358. else if (Pico.m.hardware & 0x4)
  359. sys_name = "Mark III";
  360. #ifdef NO_SMS
  361. extra = " [no support]";
  362. #endif
  363. } else if (PicoIn.AHW & PAHW_PICO) {
  364. sys_name = "Pico";
  365. } else if ((PicoIn.AHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) {
  366. sys_name = "32X + Mega CD";
  367. if ((Pico.m.hardware & 0xc0) == 0x80)
  368. sys_name = "32X + Sega CD";
  369. } else if (PicoIn.AHW & PAHW_MCD) {
  370. sys_name = "Mega CD";
  371. if ((Pico.m.hardware & 0xc0) == 0x80)
  372. sys_name = "Sega CD";
  373. } else if (PicoIn.AHW & PAHW_32X) {
  374. sys_name = "32X";
  375. } else {
  376. sys_name = "MegaDrive";
  377. if ((Pico.m.hardware & 0xc0) == 0x80)
  378. sys_name = "Genesis";
  379. }
  380. tv_standard = Pico.m.pal ? "PAL" : "NTSC";
  381. fps = Pico.m.pal ? 50 : 60;
  382. //emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);
  383. printf("\nSystem Announce: %s, %s / %dFPS%s\n", sys_name, tv_standard, fps, extra);
  384. printf("PicoIn.AHW = %d, Pico.m.hardware=%d\n", PicoIn.AHW, Pico.m.hardware);
  385. }
  386. static void do_region_override(const char *media_fname)
  387. {
  388. // we only need to override region if config tells us so
  389. int ret = emu_read_config(media_fname, 0);
  390. if (!ret) emu_read_config(NULL, 0);
  391. }
  392. int emu_reload_rom(const char *rom_fname_in)
  393. {
  394. // use setting before rom config is loaded
  395. int autoload = g_autostateld_opt;
  396. char *rom_fname = NULL;
  397. char ext[5];
  398. enum media_type_e media_type;
  399. int menu_romload_started = 0;
  400. char carthw_path[512];
  401. int retval = 0;
  402. lprintf("emu_ReloadRom(%s)\n", rom_fname_in);
  403. rom_fname = strdup(rom_fname_in);
  404. if (rom_fname == NULL)
  405. return 0;
  406. get_ext(rom_fname, ext);
  407. // early cleanup
  408. PicoPatchUnload();
  409. if (movie_data) {
  410. free(movie_data);
  411. movie_data = 0;
  412. }
  413. if (!strcmp(ext, ".gmv"))
  414. {
  415. // check for both gmv and rom
  416. int dummy;
  417. FILE *movie_file = fopen(rom_fname, "rb");
  418. if (!movie_file) {
  419. menu_update_msg("Failed to open movie.");
  420. goto out;
  421. }
  422. fseek(movie_file, 0, SEEK_END);
  423. movie_size = ftell(movie_file);
  424. fseek(movie_file, 0, SEEK_SET);
  425. if (movie_size < 64+3) {
  426. menu_update_msg("Invalid GMV file.");
  427. fclose(movie_file);
  428. goto out;
  429. }
  430. movie_data = malloc(movie_size);
  431. if (movie_data == NULL) {
  432. menu_update_msg("low memory.");
  433. fclose(movie_file);
  434. goto out;
  435. }
  436. dummy = fread(movie_data, 1, movie_size, movie_file);
  437. fclose(movie_file);
  438. if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
  439. menu_update_msg("Invalid GMV file.");
  440. goto out;
  441. }
  442. dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
  443. if (!dummy) {
  444. menu_update_msg("Could't find a ROM for movie.");
  445. goto out;
  446. }
  447. get_ext(rom_fname, ext);
  448. lprintf("gmv loaded for %s\n", rom_fname);
  449. }
  450. else if (!strcmp(ext, ".pat"))
  451. {
  452. int dummy;
  453. PicoPatchLoad(rom_fname);
  454. dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
  455. if (!dummy) {
  456. menu_update_msg("Could't find a ROM to patch.");
  457. goto out;
  458. }
  459. get_ext(rom_fname, ext);
  460. }
  461. menu_romload_prepare(rom_fname); // also CD load
  462. menu_romload_started = 1;
  463. emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));
  464. media_type = PicoLoadMedia(rom_fname, carthw_path,
  465. find_bios, do_region_override);
  466. switch (media_type) {
  467. case PM_BAD_DETECT:
  468. menu_update_msg("Not a ROM/CD img selected.");
  469. goto out;
  470. case PM_BAD_CD:
  471. menu_update_msg("Invalid CD image");
  472. goto out;
  473. case PM_BAD_CD_NO_BIOS:
  474. // find_bios() prints a message
  475. goto out;
  476. case PM_ERROR:
  477. menu_update_msg("Load error");
  478. goto out;
  479. default:
  480. break;
  481. }
  482. // make quirks visible in UI
  483. if (PicoIn.quirks & PQUIRK_FORCE_6BTN)
  484. currentConfig.input_dev0 = PICO_INPUT_PAD_6BTN;
  485. menu_romload_end();
  486. menu_romload_started = 0;
  487. if (PicoPatches) {
  488. PicoPatchPrepare();
  489. PicoPatchApply();
  490. }
  491. // additional movie stuff
  492. if (movie_data)
  493. {
  494. enum input_device indev = (movie_data[0x14] == '6') ?
  495. PICO_INPUT_PAD_6BTN : PICO_INPUT_PAD_3BTN;
  496. PicoSetInputDevice(0, indev);
  497. PicoSetInputDevice(1, indev);
  498. PicoIn.opt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing
  499. if (movie_data[0xF] >= 'A') {
  500. if (movie_data[0x16] & 0x80) {
  501. PicoIn.regionOverride = 8;
  502. } else {
  503. PicoIn.regionOverride = 4;
  504. }
  505. PicoReset();
  506. // TODO: bits 6 & 5
  507. }
  508. movie_data[0x18+30] = 0;
  509. emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);
  510. }
  511. else
  512. {
  513. system_announce();
  514. PicoIn.opt &= ~POPT_DIS_VDP_FIFO;
  515. }
  516. /* Set input map */
  517. if (PicoIn.AHW & PAHW_SMS) {
  518. printf("plat set sms input\n");
  519. plat_set_sms_input();
  520. }
  521. else{
  522. plat_set_genesis_input();
  523. }
  524. strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);
  525. rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
  526. // load SRAM for this ROM
  527. if (currentConfig.EmuOpt & EOPT_EN_SRAM)
  528. emu_save_load_game(1, 1);
  529. // state autoload?
  530. if (autoload) {
  531. int time, newest = 0, newest_slot = -1;
  532. int slot;
  533. for (slot = 0; slot < 10; slot++) {
  534. if (emu_check_save_file(slot, &time)) {
  535. if (time > newest) {
  536. newest = time;
  537. newest_slot = slot;
  538. }
  539. }
  540. }
  541. if (newest_slot >= 0) {
  542. lprintf("autoload slot %d\n", newest_slot);
  543. state_slot = newest_slot;
  544. emu_save_load_game(1, 0);
  545. }
  546. else {
  547. lprintf("no save to autoload.\n");
  548. }
  549. }
  550. retval = 1;
  551. out:
  552. if (menu_romload_started)
  553. menu_romload_end();
  554. free(rom_fname);
  555. return retval;
  556. }
  557. int emu_swap_cd(const char *fname)
  558. {
  559. enum cd_track_type cd_type;
  560. int ret = -1;
  561. cd_type = PicoCdCheck(fname, NULL);
  562. if (cd_type != CT_UNKNOWN)
  563. ret = cdd_load(fname, cd_type);
  564. if (ret != 0) {
  565. menu_update_msg("Load failed, invalid CD image?");
  566. return 0;
  567. }
  568. strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);
  569. rom_fname_loaded[sizeof(rom_fname_loaded) - 1] = 0;
  570. return 1;
  571. }
  572. // <base dir><end>
  573. void emu_make_path(char *buff, const char *end, int size)
  574. {
  575. int pos, end_len;
  576. end_len = strlen(end);
  577. pos = plat_get_root_dir(buff, size);
  578. strncpy(buff + pos, end, size - pos);
  579. buff[size - 1] = 0;
  580. if (pos + end_len > size - 1)
  581. lprintf("Warning: path truncated: %s\n", buff);
  582. }
  583. static void make_config_cfg(char *cfg_buff_512)
  584. {
  585. emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);
  586. if (config_slot != 0)
  587. {
  588. char *p = strrchr(cfg_buff_512, '.');
  589. if (p == NULL)
  590. p = cfg_buff_512 + strlen(cfg_buff_512);
  591. sprintf(p, ".%i.cfg", config_slot);
  592. }
  593. cfg_buff_512[511] = 0;
  594. }
  595. void emu_prep_defconfig(void)
  596. {
  597. memset(&defaultConfig, 0, sizeof(defaultConfig));
  598. defaultConfig.EmuOpt = EOPT_EN_SRAM | EOPT_EN_SOUND | EOPT_16BPP |
  599. EOPT_EN_CD_LEDS | EOPT_GZIP_SAVES | 0x10/*?*/;
  600. defaultConfig.s_PicoOpt = POPT_EN_YM2413|POPT_EN_GG_LCD |
  601. POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
  602. POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
  603. POPT_EN_DRC|POPT_ACC_SPRITES |
  604. POPT_EN_32X|POPT_EN_PWM;
  605. defaultConfig.s_PsndRate = 44100;
  606. defaultConfig.s_PicoRegion = 0; // auto
  607. defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
  608. defaultConfig.s_hwSelect = PHWS_AUTO;
  609. defaultConfig.s_PicoCDBuffers = 0;
  610. defaultConfig.s_PicoSndFilterAlpha = 0x10000 * 60 / 100;
  611. defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;
  612. defaultConfig.Frameskip = -1; // auto
  613. defaultConfig.input_dev0 = PICO_INPUT_PAD_3BTN;
  614. defaultConfig.input_dev1 = PICO_INPUT_PAD_3BTN;
  615. defaultConfig.volume = 50;
  616. //defaultConfig.volume = 99;
  617. defaultConfig.gamma = 100;
  618. defaultConfig.scaling = 0;
  619. defaultConfig.turbo_rate = 15;
  620. defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000;
  621. defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000;
  622. defaultConfig.max_skip = 4;
  623. // platform specific overrides
  624. pemu_prep_defconfig();
  625. }
  626. void emu_set_defconfig(void)
  627. {
  628. memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));
  629. PicoIn.opt = currentConfig.s_PicoOpt;
  630. PicoIn.sndRate = currentConfig.s_PsndRate;
  631. PicoIn.regionOverride = currentConfig.s_PicoRegion;
  632. PicoIn.autoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
  633. PicoIn.hwSelect = currentConfig.s_hwSelect;
  634. PicoIn.sndFilterAlpha = currentConfig.s_PicoSndFilterAlpha;
  635. }
  636. int emu_read_config(const char *rom_fname, int no_defaults)
  637. {
  638. char cfg[512];
  639. int ret;
  640. if (!no_defaults)
  641. emu_set_defconfig();
  642. if (rom_fname == NULL)
  643. {
  644. // global config
  645. make_config_cfg(cfg);
  646. ret = config_readsect(cfg, NULL);
  647. }
  648. else
  649. {
  650. char ext[16];
  651. int vol;
  652. if (config_slot != 0)
  653. snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
  654. else
  655. strcpy(ext, ".cfg");
  656. fname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext, rom_fname); // gamepath/gamename.cfg
  657. // read user's config
  658. vol = currentConfig.volume;
  659. ret = config_readsect(cfg, NULL);
  660. currentConfig.volume = vol; // make vol global (bah)
  661. if (ret != 0)
  662. {
  663. // read global config, and apply game_def.cfg on top
  664. make_config_cfg(cfg); // $HOME/.picodrive/config2.cfg
  665. config_readsect(cfg, NULL);
  666. emu_make_path(cfg, "game_def.cfg", sizeof(cfg)); // $HOME/.picodrive/game_def.cfg
  667. ret = config_readsect(cfg, emu_make_rom_id(rom_fname));
  668. }
  669. }
  670. pemu_validate_config();
  671. PicoIn.overclockM68k = currentConfig.overclock_68k;
  672. // some sanity checks
  673. if (currentConfig.volume < 0 || currentConfig.volume > 99)
  674. currentConfig.volume = 50;
  675. if (ret == 0)
  676. config_slot_current = config_slot;
  677. return (ret == 0);
  678. }
  679. int emu_write_config(int is_game)
  680. {
  681. char cfg[512];
  682. int ret, write_lrom = 0;
  683. if (!is_game)
  684. {
  685. make_config_cfg(cfg);
  686. write_lrom = 1;
  687. } else {
  688. char ext[16];
  689. if (config_slot != 0)
  690. snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
  691. else
  692. strcpy(ext, ".cfg");
  693. romfname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext);
  694. }
  695. lprintf("emu_write_config: %s ", cfg);
  696. ret = config_write(cfg);
  697. if (write_lrom) config_writelrom(cfg);
  698. #ifdef __GP2X__
  699. sync();
  700. #endif
  701. lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
  702. if (ret == 0) config_slot_current = config_slot;
  703. return ret == 0;
  704. }
  705. /* always using built-in font */
  706. #define mk_text_out(name, type, val, topleft, step_x, step_y) \
  707. void name(int x, int y, const char *text) \
  708. { \
  709. int i, l, len = strlen(text); \
  710. type *screen = (type *)(topleft) + x * step_x + y * step_y; \
  711. \
  712. for (i = 0; i < len; i++, screen += 8 * step_x) \
  713. { \
  714. for (l = 0; l < 8; l++) \
  715. { \
  716. unsigned char fd = fontdata8x8[text[i] * 8 + l];\
  717. type *s = screen + l * step_y; \
  718. if (fd&0x80) s[step_x * 0] = val; \
  719. if (fd&0x40) s[step_x * 1] = val; \
  720. if (fd&0x20) s[step_x * 2] = val; \
  721. if (fd&0x10) s[step_x * 3] = val; \
  722. if (fd&0x08) s[step_x * 4] = val; \
  723. if (fd&0x04) s[step_x * 5] = val; \
  724. if (fd&0x02) s[step_x * 6] = val; \
  725. if (fd&0x01) s[step_x * 7] = val; \
  726. } \
  727. } \
  728. }
  729. mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_ppitch)
  730. mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_ppitch)
  731. mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,
  732. (char *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)
  733. mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,
  734. (short *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)
  735. #undef mk_text_out
  736. void emu_osd_text16(int x, int y, const char *text)
  737. {
  738. int len = strlen(text) * 8;
  739. int i, h;
  740. len++;
  741. if (x + len > g_screen_width)
  742. len = g_screen_width - x;
  743. for (h = 0; h < 8; h++) {
  744. unsigned short *p;
  745. p = (unsigned short *)g_screen_ptr
  746. + x + g_screen_ppitch * (y + h);
  747. for (i = len; i > 0; i--, p++)
  748. *p = (*p >> 2) & 0x39e7;
  749. }
  750. emu_text_out16(x, y, text);
  751. }
  752. static void update_movie(void)
  753. {
  754. int offs = Pico.m.frame_count*3 + 0x40;
  755. if (offs+3 > movie_size) {
  756. free(movie_data);
  757. movie_data = 0;
  758. emu_status_msg("END OF MOVIE.");
  759. lprintf("END OF MOVIE.\n");
  760. } else {
  761. // MXYZ SACB RLDU
  762. PicoIn.pad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
  763. if(!(movie_data[offs] & 0x10)) PicoIn.pad[0] |= 0x40; // C
  764. if(!(movie_data[offs] & 0x20)) PicoIn.pad[0] |= 0x10; // A
  765. if(!(movie_data[offs] & 0x40)) PicoIn.pad[0] |= 0x20; // B
  766. PicoIn.pad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
  767. if(!(movie_data[offs+1] & 0x10)) PicoIn.pad[1] |= 0x40; // C
  768. if(!(movie_data[offs+1] & 0x20)) PicoIn.pad[1] |= 0x10; // A
  769. if(!(movie_data[offs+1] & 0x40)) PicoIn.pad[1] |= 0x20; // B
  770. PicoIn.pad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
  771. if(!(movie_data[offs+2] & 0x01)) PicoIn.pad[0] |= 0x0400; // X
  772. if(!(movie_data[offs+2] & 0x04)) PicoIn.pad[0] |= 0x0100; // Z
  773. PicoIn.pad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
  774. if(!(movie_data[offs+2] & 0x10)) PicoIn.pad[1] |= 0x0400; // X
  775. if(!(movie_data[offs+2] & 0x40)) PicoIn.pad[1] |= 0x0100; // Z
  776. }
  777. }
  778. static int try_ropen_file(const char *fname, int *time)
  779. {
  780. struct stat st;
  781. FILE *f;
  782. f = fopen(fname, "rb");
  783. if (f) {
  784. if (time != NULL) {
  785. *time = 0;
  786. if (fstat(fileno(f), &st) == 0)
  787. *time = (int)st.st_mtime;
  788. }
  789. fclose(f);
  790. return 1;
  791. }
  792. return 0;
  793. }
  794. char *emu_get_save_fname(int load, int is_sram, int slot, int *time)
  795. {
  796. char *saveFname = static_buff;
  797. char ext[16];
  798. if (is_sram)
  799. {
  800. strcpy(ext, (PicoIn.AHW & PAHW_MCD) ? ".brm" : ".srm");
  801. romfname_ext(saveFname, sizeof(static_buff),
  802. (PicoIn.AHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);
  803. if (!load)
  804. return saveFname;
  805. if (try_ropen_file(saveFname, time))
  806. return saveFname;
  807. romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
  808. if (try_ropen_file(saveFname, time))
  809. return saveFname;
  810. }
  811. else
  812. {
  813. const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";
  814. const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";
  815. ext[0] = 0;
  816. if (slot > 0 && slot < 10)
  817. sprintf(ext, ".%i", slot);
  818. strcat(ext, ext_main);
  819. if (!load) {
  820. romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
  821. return saveFname;
  822. }
  823. else {
  824. romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
  825. if (try_ropen_file(saveFname, time))
  826. return saveFname;
  827. romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
  828. if (try_ropen_file(saveFname, time))
  829. return saveFname;
  830. // try the other ext
  831. ext[0] = 0;
  832. if (slot > 0 && slot < 10)
  833. sprintf(ext, ".%i", slot);
  834. strcat(ext, ext_othr);
  835. romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);
  836. if (try_ropen_file(saveFname, time))
  837. return saveFname;
  838. }
  839. }
  840. return NULL;
  841. }
  842. int emu_check_save_file(int slot, int *time)
  843. {
  844. return emu_get_save_fname(1, 0, slot, time) ? 1 : 0;
  845. }
  846. int emu_save_load_game_from_file(int load, char *saveFname){
  847. int ret = PicoState(saveFname, !load);
  848. if (!ret) {
  849. //emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
  850. } else {
  851. //emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
  852. ret = -1;
  853. }
  854. return ret;
  855. }
  856. int emu_save_load_game(int load, int sram)
  857. {
  858. int ret = 0;
  859. char *saveFname;
  860. // make save filename
  861. saveFname = emu_get_save_fname(load, sram, state_slot, NULL);
  862. if (saveFname == NULL) {
  863. /*if (!sram)
  864. emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");*/
  865. return -1;
  866. }
  867. lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
  868. if (sram)
  869. {
  870. FILE *sramFile;
  871. int sram_size;
  872. unsigned char *sram_data;
  873. int truncate = 1;
  874. if (PicoIn.AHW & PAHW_MCD)
  875. {
  876. if (PicoIn.opt & POPT_EN_MCD_RAMCART) {
  877. sram_size = 0x12000;
  878. sram_data = Pico.sv.data;
  879. if (sram_data)
  880. memcpy(sram_data, Pico_mcd->bram, 0x2000);
  881. } else {
  882. sram_size = 0x2000;
  883. sram_data = Pico_mcd->bram;
  884. truncate = 0; // the .brm may contain RAM cart data after normal brm
  885. }
  886. } else {
  887. sram_size = Pico.sv.size;
  888. sram_data = Pico.sv.data;
  889. }
  890. if (sram_data == NULL)
  891. return 0; // cart saves forcefully disabled for this game
  892. if (load)
  893. {
  894. sramFile = fopen(saveFname, "rb");
  895. if (!sramFile)
  896. return -1;
  897. ret = fread(sram_data, 1, sram_size, sramFile);
  898. ret = ret > 0 ? 0 : -1;
  899. fclose(sramFile);
  900. if ((PicoIn.AHW & PAHW_MCD) && (PicoIn.opt&POPT_EN_MCD_RAMCART))
  901. memcpy(Pico_mcd->bram, sram_data, 0x2000);
  902. } else {
  903. // sram save needs some special processing
  904. // see if we have anything to save
  905. for (; sram_size > 0; sram_size--)
  906. if (sram_data[sram_size-1]) break;
  907. if (sram_size) {
  908. sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
  909. if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
  910. if (!sramFile) return -1;
  911. ret = fwrite(sram_data, 1, sram_size, sramFile);
  912. ret = (ret != sram_size) ? -1 : 0;
  913. fclose(sramFile);
  914. #ifdef __GP2X__
  915. sync();
  916. #endif
  917. }
  918. }
  919. return ret;
  920. }
  921. else
  922. {
  923. ret = PicoState(saveFname, !load);
  924. if (!ret) {
  925. #ifdef __GP2X__
  926. if (!load) sync();
  927. #endif
  928. //emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
  929. } else {
  930. //emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
  931. ret = -1;
  932. }
  933. return ret;
  934. }
  935. }
  936. void emu_set_fastforward(int set_on)
  937. {
  938. static void *set_PsndOut = NULL;
  939. static int set_Frameskip, set_EmuOpt, is_on = 0;
  940. if (set_on && !is_on) {
  941. set_PsndOut = PicoIn.sndOut;
  942. set_Frameskip = currentConfig.Frameskip;
  943. set_EmuOpt = currentConfig.EmuOpt;
  944. PicoIn.sndOut = NULL;
  945. currentConfig.Frameskip = 8;
  946. currentConfig.EmuOpt &= ~4;
  947. currentConfig.EmuOpt |= EOPT_NO_FRMLIMIT;
  948. is_on = 1;
  949. emu_status_msg("FAST FORWARD");
  950. }
  951. else if (!set_on && is_on) {
  952. PicoIn.sndOut = set_PsndOut;
  953. currentConfig.Frameskip = set_Frameskip;
  954. currentConfig.EmuOpt = set_EmuOpt;
  955. PsndRerate(1);
  956. is_on = 0;
  957. // mainly to unbreak pcm
  958. if (PicoIn.AHW & PAHW_MCD)
  959. pcd_state_loaded();
  960. }
  961. }
  962. static void emu_tray_open(void)
  963. {
  964. engineState = PGS_TrayMenu;
  965. }
  966. static void emu_tray_close(void)
  967. {
  968. emu_status_msg("CD tray closed.");
  969. }
  970. void emu_32x_startup(void)
  971. {
  972. plat_video_toggle_renderer(0, 0); // HACK
  973. system_announce();
  974. }
  975. void emu_reset_game(void)
  976. {
  977. PicoReset();
  978. reset_timing = 1;
  979. }
  980. void run_events_pico(unsigned int events)
  981. {
  982. int lim_x;
  983. if (events & PEV_PICO_SWINP) {
  984. pico_inp_mode++;
  985. if (pico_inp_mode > 2)
  986. pico_inp_mode = 0;
  987. switch (pico_inp_mode) {
  988. case 2: emu_status_msg("Input: Pen on Pad"); break;
  989. case 1: emu_status_msg("Input: Pen on Storyware"); break;
  990. case 0: emu_status_msg("Input: Joystick");
  991. PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
  992. break;
  993. }
  994. }
  995. if (events & PEV_PICO_PPREV) {
  996. PicoPicohw.page--;
  997. if (PicoPicohw.page < 0)
  998. PicoPicohw.page = 0;
  999. emu_status_msg("Page %i", PicoPicohw.page);
  1000. }
  1001. if (events & PEV_PICO_PNEXT) {
  1002. PicoPicohw.page++;
  1003. if (PicoPicohw.page > 6)
  1004. PicoPicohw.page = 6;
  1005. emu_status_msg("Page %i", PicoPicohw.page);
  1006. }
  1007. if (pico_inp_mode == 0)
  1008. return;
  1009. /* handle other input modes */
  1010. if (PicoIn.pad[0] & 1) pico_pen_y--;
  1011. if (PicoIn.pad[0] & 2) pico_pen_y++;
  1012. if (PicoIn.pad[0] & 4) pico_pen_x--;
  1013. if (PicoIn.pad[0] & 8) pico_pen_x++;
  1014. PicoIn.pad[0] &= ~0x0f; // release UDLR
  1015. lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
  1016. if (pico_pen_y < 8)
  1017. pico_pen_y = 8;
  1018. if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)
  1019. pico_pen_y = 224 - PICO_PEN_ADJUST_Y;
  1020. if (pico_pen_x < 0)
  1021. pico_pen_x = 0;
  1022. if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)
  1023. pico_pen_x = lim_x - PICO_PEN_ADJUST_X;
  1024. PicoPicohw.pen_pos[0] = pico_pen_x;
  1025. if (!(Pico.video.reg[12] & 1))
  1026. PicoPicohw.pen_pos[0] += pico_pen_x / 4;
  1027. PicoPicohw.pen_pos[0] += 0x3c;
  1028. PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
  1029. }
  1030. static void do_turbo(unsigned short *pad, int acts)
  1031. {
  1032. static int turbo_pad = 0;
  1033. static unsigned char turbo_cnt[3] = { 0, 0, 0 };
  1034. int inc = currentConfig.turbo_rate * 2;
  1035. if (acts & 0x1000) {
  1036. turbo_cnt[0] += inc;
  1037. if (turbo_cnt[0] >= 60)
  1038. turbo_pad ^= 0x10, turbo_cnt[0] = 0;
  1039. }
  1040. if (acts & 0x2000) {
  1041. turbo_cnt[1] += inc;
  1042. if (turbo_cnt[1] >= 60)
  1043. turbo_pad ^= 0x20, turbo_cnt[1] = 0;
  1044. }
  1045. if (acts & 0x4000) {
  1046. turbo_cnt[2] += inc;
  1047. if (turbo_cnt[2] >= 60)
  1048. turbo_pad ^= 0x40, turbo_cnt[2] = 0;
  1049. }
  1050. *pad |= turbo_pad & (acts >> 8);
  1051. }
  1052. static void run_events_ui(unsigned int which)
  1053. {
  1054. char shell_cmd[100];
  1055. FILE *fp;
  1056. //emu_action_old = emu_action;
  1057. //printf("New event: %d\n", which);
  1058. if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))
  1059. {
  1060. int do_it = 1;
  1061. if ( emu_check_save_file(state_slot, NULL) &&
  1062. (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||
  1063. ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )
  1064. {
  1065. const char *nm;
  1066. char tmp[64];
  1067. int keys, len;
  1068. strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE? " : "OVERWRITE SAVE? ");
  1069. len = strlen(tmp);
  1070. nm = in_get_key_name(-1, -PBTN_MOK);
  1071. snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
  1072. len = strlen(tmp);
  1073. nm = in_get_key_name(-1, -PBTN_MBACK);
  1074. snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
  1075. plat_status_msg_busy_first(tmp);
  1076. in_set_config_int(0, IN_CFG_BLOCKING, 1);
  1077. while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))
  1078. ;
  1079. while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MOK | PBTN_MBACK)))
  1080. ;
  1081. if (keys & PBTN_MBACK)
  1082. do_it = 0;
  1083. while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))
  1084. ;
  1085. in_set_config_int(0, IN_CFG_BLOCKING, 0);
  1086. plat_status_msg_clear();
  1087. }
  1088. if (do_it) {
  1089. plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");
  1090. PicoStateProgressCB = plat_status_msg_busy_next;
  1091. emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);
  1092. PicoStateProgressCB = NULL;
  1093. }
  1094. }
  1095. if (which & PEV_SWITCH_RND)
  1096. {
  1097. plat_video_toggle_renderer(1, 0);
  1098. }
  1099. if (which & PEV_VOL_DOWN)
  1100. {
  1101. printf("PEV_VOL_DOWN\r\n");
  1102. /// ----- Compute new value -----
  1103. volume_percentage = (volume_percentage < STEP_CHANGE_VOLUME)?
  1104. 0:(volume_percentage-STEP_CHANGE_VOLUME);
  1105. /// ----- HUD msg ------
  1106. char txt[100];
  1107. sprintf(txt, "VOLUME %d%%", volume_percentage);
  1108. plat_status_msg_busy_first(txt);
  1109. /// ----- Shell cmd ----
  1110. sprintf(shell_cmd, "%s %d", SHELL_CMD_VOLUME_SET, volume_percentage);
  1111. fp = popen(shell_cmd, "r");
  1112. if (fp == NULL) {
  1113. printf("Failed to run command %s\n", shell_cmd);
  1114. } else {
  1115. pclose(fp);
  1116. }
  1117. }
  1118. if (which & PEV_VOL_UP)
  1119. {
  1120. printf("PEV_VOL_UP\r\n");
  1121. /// ----- Compute new value -----
  1122. volume_percentage = (volume_percentage > 100 - STEP_CHANGE_VOLUME)?
  1123. 100:(volume_percentage+STEP_CHANGE_VOLUME);
  1124. /// ----- HUD msg ------
  1125. char txt[100];
  1126. sprintf(txt, "VOLUME %d%%", volume_percentage);
  1127. plat_status_msg_busy_first(txt);
  1128. /// ----- Shell cmd ----
  1129. sprintf(shell_cmd, "%s %d", SHELL_CMD_VOLUME_SET, volume_percentage);
  1130. fp = popen(shell_cmd, "r");
  1131. if (fp == NULL) {
  1132. printf("Failed to run command %s\n", shell_cmd);
  1133. } else {
  1134. pclose(fp);
  1135. }
  1136. }
  1137. if (which & PEV_BRIGHT_UP)
  1138. {
  1139. printf("PEV_BRIGHT_UP\r\n");
  1140. /// ----- Compute new value -----
  1141. brightness_percentage = (brightness_percentage > 100 - STEP_CHANGE_BRIGHTNESS)?
  1142. 100:(brightness_percentage+STEP_CHANGE_BRIGHTNESS);
  1143. /// ----- HUD msg ------
  1144. char txt[100];
  1145. sprintf(txt, "BRIGHTNESS %d%%", brightness_percentage);
  1146. plat_status_msg_busy_first(txt);
  1147. /// ----- Shell cmd ----
  1148. sprintf(shell_cmd, "%s %d", SHELL_CMD_BRIGHTNESS_SET, brightness_percentage);
  1149. fp = popen(shell_cmd, "r");
  1150. if (fp == NULL) {
  1151. printf("Failed to run command %s\n", shell_cmd);
  1152. } else {
  1153. pclose(fp);
  1154. }
  1155. }
  1156. if (which & PEV_BRIGHT_DOWN)
  1157. {
  1158. printf("PEV_BRIGHT_DOWN\r\n");
  1159. /// ----- Compute new value -----
  1160. brightness_percentage = (brightness_percentage < STEP_CHANGE_BRIGHTNESS)?
  1161. 0:(brightness_percentage-STEP_CHANGE_BRIGHTNESS);
  1162. /// ----- HUD msg ------
  1163. char txt[100];
  1164. sprintf(txt, "BRIGHTNESS %d%%", brightness_percentage);
  1165. plat_status_msg_busy_first(txt);
  1166. /// ----- Shell cmd ----
  1167. sprintf(shell_cmd, "%s %d", SHELL_CMD_BRIGHTNESS_SET, brightness_percentage);
  1168. fp = popen(shell_cmd, "r");
  1169. if (fp == NULL) {
  1170. printf("Failed to run command %s\n", shell_cmd);
  1171. } else {
  1172. pclose(fp);
  1173. }
  1174. }
  1175. if (which & PEV_AR_FACT_UP)
  1176. {
  1177. printf("PEV_AR_FACT_UP\r\n");
  1178. /// ----- Compute new value -----
  1179. if(aspect_ratio == ASPECT_RATIOS_TYPE_MANUAL){
  1180. aspect_ratio_factor_percent = (aspect_ratio_factor_percent+aspect_ratio_factor_step<100)?
  1181. aspect_ratio_factor_percent+aspect_ratio_factor_step:100;
  1182. need_screen_cleared = 1;
  1183. }
  1184. else{
  1185. aspect_ratio = ASPECT_RATIOS_TYPE_MANUAL;
  1186. }
  1187. aspect_ratio = ASPECT_RATIOS_TYPE_MANUAL;
  1188. /// ----- HUD msg ------
  1189. /*char txt[100];
  1190. sprintf(txt, " DISPLAY MODE: ZOOMED - %d%%", aspect_ratio_factor_percent);
  1191. plat_status_msg_busy_first(txt);*/
  1192. sprintf(shell_cmd, "%s %d \" DISPLAY MODE: ZOOMED %d%%%%\"",
  1193. SHELL_CMD_NOTIF_SET, NOTIF_SECONDS_DISP, aspect_ratio_factor_percent);
  1194. fp = popen(shell_cmd, "r");
  1195. if (fp == NULL) {
  1196. printf("Failed to run command %s\n", shell_cmd);
  1197. } else {
  1198. pclose(fp);
  1199. }
  1200. // Save config file
  1201. configfile_save(cfg_file_rom);
  1202. }
  1203. if (which & PEV_AR_FACT_DOWN)
  1204. {
  1205. printf("PEV_AR_FACT_DOWN\r\n");
  1206. /// ----- Compute new value -----
  1207. if(aspect_ratio == ASPECT_RATIOS_TYPE_MANUAL){
  1208. aspect_ratio_factor_percent = (aspect_ratio_factor_percent>aspect_ratio_factor_step)?
  1209. aspect_ratio_factor_percent-aspect_ratio_factor_step:0;
  1210. need_screen_cleared = 1;
  1211. }
  1212. else{
  1213. aspect_ratio = ASPECT_RATIOS_TYPE_MANUAL;
  1214. }
  1215. aspect_ratio = ASPECT_RATIOS_TYPE_MANUAL;
  1216. /// ----- HUD msg ------
  1217. /*char txt[100];
  1218. sprintf(txt, " DISPLAY MODE: ZOOMED - %d%%", aspect_ratio_factor_percent);
  1219. plat_status_msg_busy_first(txt);*/
  1220. sprintf(shell_cmd, "%s %d \" DISPLAY MODE: ZOOMED %d%%%%\"",
  1221. SHELL_CMD_NOTIF_SET, NOTIF_SECONDS_DISP, aspect_ratio_factor_percent);
  1222. fp = popen(shell_cmd, "r");
  1223. if (fp == NULL) {
  1224. printf("Failed to run command %s\n", shell_cmd);
  1225. } else {
  1226. pclose(fp);
  1227. }
  1228. // Save config file
  1229. configfile_save(cfg_file_rom);
  1230. }
  1231. if (which & PEV_DISPMODE)
  1232. {
  1233. printf("PEV_DISPMODE\r\n");
  1234. /// ----- Compute new value -----
  1235. aspect_ratio = (aspect_ratio+1)%NB_ASPECT_RATIOS_TYPES;
  1236. /// ----- HUD msg ------
  1237. //char txt[100];
  1238. if(aspect_ratio == ASPECT_RATIOS_TYPE_MANUAL){
  1239. //sprintf(txt, " DISPLAY MODE: ZOOMED - %d%%", aspect_ratio_factor_percent);
  1240. sprintf(shell_cmd, "%s %d \" DISPLAY MODE: ZOOMED %d%%%%\"",
  1241. SHELL_CMD_NOTIF_SET, NOTIF_SECONDS_DISP, aspect_ratio_factor_percent);
  1242. }
  1243. else{
  1244. //sprintf(txt, "DISPLAY MODE: %s", aspect_ratio_name[aspect_ratio]);
  1245. sprintf(shell_cmd, "%s %d \" DISPLAY MODE: %s\"",
  1246. SHELL_CMD_NOTIF_SET, NOTIF_SECONDS_DISP, aspect_ratio_name[aspect_ratio]);
  1247. }
  1248. //plat_status_msg_busy_first(txt);
  1249. fp = popen(shell_cmd, "r");
  1250. if (fp == NULL) {
  1251. printf("Failed to run command %s\n", shell_cmd);
  1252. } else {
  1253. pclose(fp);
  1254. }
  1255. // Save config file
  1256. configfile_save(cfg_file_rom);
  1257. }
  1258. if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))
  1259. {
  1260. if (which & PEV_SSLOT_PREV) {
  1261. state_slot -= 1;
  1262. if (state_slot < 0)
  1263. state_slot = 9;
  1264. } else {
  1265. state_slot += 1;
  1266. if (state_slot > 9)
  1267. state_slot = 0;
  1268. }
  1269. emu_status_msg("SAVE SLOT %i [%s]", state_slot,
  1270. emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");
  1271. }
  1272. if (which & PEV_RESET)
  1273. emu_reset_game();
  1274. if (which & PEV_MENU)
  1275. engineState = PGS_Menu;
  1276. }
  1277. void emu_update_input(void)
  1278. {
  1279. static int prev_events = 0;
  1280. int actions[IN_BINDTYPE_COUNT] = { 0, };
  1281. int pl_actions[2];
  1282. int events;
  1283. in_update(actions);
  1284. pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];
  1285. pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;
  1286. PicoIn.pad[0] = pl_actions[0] & 0xfff;
  1287. PicoIn.pad[1] = pl_actions[1] & 0xfff;
  1288. if (pl_actions[0] & 0x7000)
  1289. do_turbo(&PicoIn.pad[0], pl_actions[0]);
  1290. if (pl_actions[1] & 0x7000)
  1291. do_turbo(&PicoIn.pad[1], pl_actions[1]);
  1292. events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
  1293. // volume is treated in special way and triggered every frame
  1294. if (events & (PEV_VOL_DOWN|PEV_VOL_UP))
  1295. plat_update_volume(1, events & PEV_VOL_UP);
  1296. if ((events ^ prev_events) & PEV_FF) {
  1297. emu_set_fastforward(events & PEV_FF);
  1298. plat_update_volume(0, 0);
  1299. reset_timing = 1;
  1300. }
  1301. events &= ~prev_events;
  1302. /* SMS */
  1303. if (PicoIn.AHW == PAHW_PICO)
  1304. run_events_pico(events);
  1305. if (events)
  1306. run_events_ui(events);
  1307. if (movie_data)
  1308. update_movie();
  1309. prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
  1310. }
  1311. static void mkdir_path(char *path_with_reserve, int pos, const char *name)
  1312. {
  1313. strcpy(path_with_reserve + pos, name);
  1314. if (plat_is_dir(path_with_reserve))
  1315. return;
  1316. if (mkdir(path_with_reserve, 0777) < 0)
  1317. lprintf("failed to create: %s\n", path_with_reserve);
  1318. }
  1319. void emu_cmn_forced_frame(int no_scale, int do_emu, void *buf)
  1320. {
  1321. int po_old = PicoIn.opt;
  1322. int y;
  1323. for (y = 0; y < g_screen_height; y++)
  1324. memset32((short *)g_screen_ptr + g_screen_ppitch * y, 0,
  1325. g_screen_width * 2 / 4);
  1326. PicoIn.opt &= ~(POPT_ALT_RENDERER|POPT_EN_SOFTSCALE);
  1327. PicoIn.opt |= POPT_ACC_SPRITES;
  1328. if (!no_scale && currentConfig.scaling)
  1329. PicoIn.opt |= POPT_EN_SOFTSCALE;
  1330. PicoDrawSetOutFormat(PDF_RGB555, 1);
  1331. PicoDrawSetOutBuf(buf, g_screen_ppitch * 2);
  1332. Pico.m.dirtyPal = 1;
  1333. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  1334. if (do_emu)
  1335. PicoFrame();
  1336. else
  1337. PicoFrameDrawOnly();
  1338. PicoIn.opt = po_old;
  1339. }
  1340. int emu_is_segaCD(){
  1341. return (PicoIn.AHW & PAHW_MCD);
  1342. }
  1343. /* Quick save and turn off the console */
  1344. void quick_save_and_poweroff()
  1345. {
  1346. FILE *fp;
  1347. printf("Save Instant Play file\n");
  1348. /* Send command to cancel any previously scheduled powerdown */
  1349. fp = popen(SHELL_CMD_POWERDOWN_HANDLE, "r");
  1350. if (fp == NULL)
  1351. {
  1352. /* Countdown is still ticking, so better do nothing
  1353. than start writing and get interrupted!
  1354. */
  1355. printf("Failed to cancel scheduled shutdown\n");
  1356. exit(0);
  1357. }
  1358. pclose(fp);
  1359. /* Save */
  1360. emu_save_load_game_from_file(0, quick_save_file);
  1361. /* Perform Instant Play save and shutdown */
  1362. execlp(SHELL_CMD_INSTANT_PLAY, SHELL_CMD_INSTANT_PLAY,
  1363. "save", prog_name, "-loadStateFile", quick_save_file, mRomName, NULL);
  1364. /* Should not be reached */
  1365. printf("Failed to perform Instant Play save and shutdown\n");
  1366. /* Exit Emulator */
  1367. exit(0);
  1368. }
  1369. void emu_init(void)
  1370. {
  1371. char path[512];
  1372. int pos;
  1373. #if 0
  1374. // FIXME: handle through menu, etc
  1375. FILE *f;
  1376. f = fopen("32X_M_BIOS.BIN", "rb");
  1377. p32x_bios_m = malloc(2048);
  1378. fread(p32x_bios_m, 1, 2048, f);
  1379. fclose(f);
  1380. f = fopen("32X_S_BIOS.BIN", "rb");
  1381. p32x_bios_s = malloc(1024);
  1382. fread(p32x_bios_s, 1, 1024, f);
  1383. fclose(f);
  1384. #endif
  1385. /* make dirs for saves */
  1386. pos = plat_get_root_dir(path, sizeof(path) - 4);
  1387. mkdir_path(path, pos, "mds");
  1388. mkdir_path(path, pos, "srm");
  1389. mkdir_path(path, pos, "brm");
  1390. mkdir_path(path, pos, "cfg");
  1391. pprof_init();
  1392. make_config_cfg(path);
  1393. config_readlrom(path);
  1394. PicoInit();
  1395. PicoIn.osdMessage = plat_status_msg_busy_next;
  1396. PicoIn.mcdTrayOpen = emu_tray_open;
  1397. PicoIn.mcdTrayClose = emu_tray_close;
  1398. sndout_init();
  1399. }
  1400. void emu_finish(void)
  1401. {
  1402. // save SRAM
  1403. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
  1404. emu_save_load_game(0, 1);
  1405. Pico.sv.changed = 0;
  1406. }
  1407. if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
  1408. char cfg[512];
  1409. make_config_cfg(cfg);
  1410. config_writelrom(cfg);
  1411. #ifdef __GP2X__
  1412. sync();
  1413. #endif
  1414. }
  1415. pprof_finish();
  1416. PicoExit();
  1417. sndout_exit();
  1418. }
  1419. static void snd_write_nonblocking(int len)
  1420. {
  1421. sndout_write_nb(PicoIn.sndOut, len);
  1422. }
  1423. void emu_sound_start(void)
  1424. {
  1425. PicoIn.sndOut = NULL;
  1426. if (currentConfig.EmuOpt & EOPT_EN_SOUND)
  1427. {
  1428. int is_stereo = (PicoIn.opt & POPT_EN_STEREO) ? 1 : 0;
  1429. /// Hard Bypass Stereo to mono
  1430. //is_stereo = 0;
  1431. PsndRerate(Pico.m.frame_count ? 1 : 0);
  1432. printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
  1433. PicoIn.sndRate, Pico.snd.len, is_stereo, Pico.m.pal);
  1434. sndout_start(PicoIn.sndRate, is_stereo);
  1435. PicoIn.writeSound = snd_write_nonblocking;
  1436. plat_update_volume(0, 0);
  1437. memset(sndBuffer, 0, sizeof(sndBuffer));
  1438. PicoIn.sndOut = sndBuffer;
  1439. }
  1440. }
  1441. void emu_sound_stop(void)
  1442. {
  1443. sndout_stop();
  1444. }
  1445. void emu_sound_wait(void)
  1446. {
  1447. sndout_wait();
  1448. }
  1449. static void emu_loop_prep(void)
  1450. {
  1451. static int pal_old = -1;
  1452. static int filter_old = -1;
  1453. if (currentConfig.CPUclock != plat_target_cpu_clock_get())
  1454. plat_target_cpu_clock_set(currentConfig.CPUclock);
  1455. if (Pico.m.pal != pal_old) {
  1456. plat_target_lcdrate_set(Pico.m.pal);
  1457. pal_old = Pico.m.pal;
  1458. }
  1459. if (currentConfig.filter != filter_old) {
  1460. plat_target_hwfilter_set(currentConfig.filter);
  1461. filter_old = currentConfig.filter;
  1462. }
  1463. plat_target_gamma_set(currentConfig.gamma, 0);
  1464. pemu_loop_prep();
  1465. }
  1466. /* our tick here is 1 us right now */
  1467. #define ms_to_ticks(x) (unsigned int)(x * 1000)
  1468. #define get_ticks() plat_get_ticks_us()
  1469. #define vsync_delay_x3 3*ms_to_ticks(1)
  1470. void emu_loop(void)
  1471. {
  1472. int frames_done, frames_shown; /* actual frames for fps counter */
  1473. int frame_nb = 0;
  1474. int target_frametime_x3;
  1475. unsigned int timestamp_x3 = 0;
  1476. unsigned int timestamp_aim_x3 = 0;
  1477. unsigned int timestamp_fps_x3 = 0;
  1478. char *notice_msg = NULL;
  1479. char fpsbuff[24];
  1480. int fskip_cnt = 0;
  1481. fpsbuff[0] = 0;
  1482. PicoLoopPrepare();
  1483. plat_video_loop_prepare();
  1484. emu_loop_prep();
  1485. pemu_sound_start();
  1486. /* number of ticks per frame */
  1487. if (Pico.m.pal)
  1488. target_frametime_x3 = 3 * ms_to_ticks(1000) / 50;
  1489. else
  1490. target_frametime_x3 = 3 * ms_to_ticks(1000) / 60;
  1491. reset_timing = 1;
  1492. frames_done = frames_shown = 0;
  1493. /* loop with resync every 1 sec. */
  1494. while (engineState == PGS_Running)
  1495. {
  1496. int skip = 0;
  1497. int diff;
  1498. pprof_start(main);
  1499. if (reset_timing) {
  1500. reset_timing = 0;
  1501. plat_video_wait_vsync();
  1502. timestamp_aim_x3 = get_ticks() * 3;
  1503. timestamp_fps_x3 = timestamp_aim_x3;
  1504. fskip_cnt = 0;
  1505. }
  1506. else if (currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) {
  1507. timestamp_aim_x3 = get_ticks() * 3;
  1508. }
  1509. timestamp_x3 = get_ticks() * 3;
  1510. // show notice_msg message?
  1511. if (notice_msg_time != 0)
  1512. {
  1513. static int noticeMsgSum;
  1514. if (timestamp_x3 - ms_to_ticks(notice_msg_time) * 3
  1515. > ms_to_ticks(STATUS_MSG_TIMEOUT) * 3)
  1516. {
  1517. notice_msg_time = 0;
  1518. notice_msg = NULL;
  1519. plat_status_msg_clear();
  1520. }
  1521. else {
  1522. int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
  1523. if (sum != noticeMsgSum) {
  1524. plat_status_msg_clear();
  1525. noticeMsgSum = sum;
  1526. }
  1527. notice_msg = noticeMsg;
  1528. }
  1529. }
  1530. // second changed?
  1531. if (timestamp_x3 - timestamp_fps_x3 >= ms_to_ticks(1000) * 3)
  1532. {
  1533. #ifdef BENCHMARK
  1534. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  1535. if (++bench == 10) {
  1536. bench = 0;
  1537. bench_fps_s = bench_fps;
  1538. bf[bfp++ & 3] = bench_fps;
  1539. bench_fps = 0;
  1540. }
  1541. bench_fps += frames_shown;
  1542. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  1543. printf("%s\n", fpsbuff);
  1544. #else
  1545. if (currentConfig.EmuOpt & EOPT_SHOW_FPS || show_fps_bypass){
  1546. printf("%02i/%02i \n", frames_shown, frames_done);
  1547. snprintf(fpsbuff, 8, "%02i/%02i ", frames_shown, frames_done);
  1548. }
  1549. #endif
  1550. frames_shown = frames_done = 0;
  1551. timestamp_fps_x3 += ms_to_ticks(1000) * 3;
  1552. }
  1553. #ifdef PFRAMES
  1554. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  1555. #endif
  1556. diff = timestamp_aim_x3 - timestamp_x3;
  1557. if (currentConfig.Frameskip >= 0) // frameskip enabled (or 0)
  1558. {
  1559. if (fskip_cnt < currentConfig.Frameskip) {
  1560. fskip_cnt++;
  1561. skip = 1;
  1562. }
  1563. else {
  1564. fskip_cnt = 0;
  1565. }
  1566. }
  1567. else if (diff < -target_frametime_x3)
  1568. {
  1569. /* no time left for this frame - skip */
  1570. /* limit auto frameskip to max_skip */
  1571. if (fskip_cnt < currentConfig.max_skip) {
  1572. fskip_cnt++;
  1573. skip = 1;
  1574. }
  1575. else {
  1576. fskip_cnt = 0;
  1577. }
  1578. } else
  1579. fskip_cnt = 0;
  1580. // don't go in debt too much
  1581. while (diff < -target_frametime_x3 * 3) {
  1582. timestamp_aim_x3 += target_frametime_x3;
  1583. diff = timestamp_aim_x3 - timestamp_x3;
  1584. }
  1585. /* Quick save and poweroff */
  1586. if(mQuickSaveAndPoweroff){
  1587. quick_save_and_poweroff();
  1588. mQuickSaveAndPoweroff = 0;
  1589. }
  1590. emu_update_input();
  1591. if (skip) {
  1592. int do_audio = diff > -target_frametime_x3 * 2;
  1593. PicoIn.skipFrame = do_audio ? 1 : 2;
  1594. PicoFrame();
  1595. PicoIn.skipFrame = 0;
  1596. }
  1597. else {
  1598. PicoFrame();
  1599. pemu_finalize_frame(fpsbuff, notice_msg);
  1600. frames_shown++;
  1601. }
  1602. frames_done++;
  1603. timestamp_aim_x3 += target_frametime_x3;
  1604. if (!skip && !flip_after_sync)
  1605. plat_video_flip();
  1606. /* frame limiter */
  1607. if (!skip && !reset_timing
  1608. && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
  1609. {
  1610. unsigned int timestamp = get_ticks();
  1611. diff = timestamp_aim_x3 - timestamp * 3;
  1612. // sleep or vsync if we are still too fast
  1613. if (diff > target_frametime_x3 + vsync_delay_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) {
  1614. // we are too fast
  1615. plat_video_wait_vsync();
  1616. timestamp = get_ticks();
  1617. diff = timestamp * 3 - timestamp_aim_x3;
  1618. }
  1619. if (diff > target_frametime_x3 + vsync_delay_x3) {
  1620. // still too fast
  1621. plat_wait_till_us(timestamp + (diff - target_frametime_x3) / 3);
  1622. }
  1623. }
  1624. if (!skip && flip_after_sync)
  1625. plat_video_flip();
  1626. /* FOR SEGA CD */
  1627. /* Leave some time for the bios to load before starting a quick load at boot */
  1628. /* (Sega CD doesn't quick load at boot otherwise) */
  1629. /* Should have a much cleaner implementation thant this fix */
  1630. if((PicoIn.AHW & PAHW_MCD) && frame_nb >= 120){
  1631. /* Load slot */
  1632. if(load_state_slot != -1){
  1633. printf("LOADING FROM SLOT (SEGA CD) %d...\n", load_state_slot+1);
  1634. char fname[1024];
  1635. emu_save_load_game(1, 0);
  1636. printf("LOADED FROM SLOT (SEGA CD) %d\n", load_state_slot+1);
  1637. load_state_slot = -1;
  1638. }
  1639. if(need_quick_load != -1){
  1640. load_state_file = quick_save_file;
  1641. need_quick_load = -1;
  1642. }
  1643. /* Load file */
  1644. if(load_state_file != NULL){
  1645. printf("LOADING FROM FILE (SEGA CD) %s...\n", load_state_file);
  1646. emu_save_load_game_from_file(1, load_state_file);
  1647. printf("LOADED FROM SLOT (SEGA CD) %s\n", load_state_file);
  1648. load_state_file = NULL;
  1649. }
  1650. }
  1651. frame_nb++;
  1652. pprof_end(main);
  1653. }
  1654. emu_set_fastforward(0);
  1655. // save SRAM
  1656. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
  1657. plat_status_msg_busy_first("Writing SRAM/BRAM...");
  1658. emu_save_load_game(0, 1);
  1659. Pico.sv.changed = 0;
  1660. }
  1661. pemu_loop_end();
  1662. emu_sound_stop();
  1663. }