emu.c 49 KB

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