emu.c 44 KB

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