emu.c 49 KB

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