emu.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // (c) Copyright 2006-2007 notaz, All rights reserved.
  2. // Free for non-commercial use.
  3. // For commercial use, separate licencing terms must be obtained.
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <linux/limits.h>
  10. #include <ctype.h>
  11. #include <unistd.h>
  12. #include <stdarg.h>
  13. #include "emu.h"
  14. #include "gp2x.h"
  15. #include "usbjoy.h"
  16. #include "menu.h"
  17. #include "../common/arm_utils.h"
  18. #include "../common/fonts.h"
  19. #include "../common/emu.h"
  20. #include "../common/config.h"
  21. #include "cpuctrl.h"
  22. #include <Pico/PicoInt.h>
  23. #include <Pico/Patch.h>
  24. #include <Pico/sound/mix.h>
  25. #include <zlib/zlib.h>
  26. //#define PFRAMES
  27. #ifdef BENCHMARK
  28. #define OSD_FPS_X 220
  29. #else
  30. #define OSD_FPS_X 260
  31. #endif
  32. int engineState;
  33. int select_exits = 0;
  34. char romFileName[PATH_MAX];
  35. extern int crashed_940;
  36. static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
  37. static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
  38. static int osd_fps_x;
  39. static int gp2x_old_gamma = 100;
  40. unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
  41. int reset_timing = 0;
  42. #define PICO_PEN_ADJUST_X 4
  43. #define PICO_PEN_ADJUST_Y 2
  44. static int pico_pen_x = 320/2, pico_pen_y = 240/2;
  45. static void emu_msg_cb(const char *msg);
  46. static void emu_msg_tray_open(void);
  47. void emu_noticeMsgUpdated(void)
  48. {
  49. gettimeofday(&noticeMsgTime, 0);
  50. }
  51. int emu_getMainDir(char *dst, int len)
  52. {
  53. extern char **g_argv;
  54. int j;
  55. strncpy(dst, g_argv[0], len);
  56. len -= 32; // reserve
  57. if (len < 0) len = 0;
  58. dst[len] = 0;
  59. for (j = strlen(dst); j > 0; j--)
  60. if (dst[j] == '/') { dst[j+1] = 0; break; }
  61. return j + 1;
  62. }
  63. void emu_Init(void)
  64. {
  65. // make temp buffer for alt renderer
  66. PicoDraw2FB = malloc((8+320)*(8+240+8));
  67. if (!PicoDraw2FB)
  68. {
  69. printf("PicoDraw2FB == 0\n");
  70. }
  71. // make dirs for saves, cfgs, etc.
  72. mkdir("mds", 0777);
  73. mkdir("srm", 0777);
  74. mkdir("brm", 0777);
  75. mkdir("cfg", 0777);
  76. PicoInit();
  77. PicoMessage = emu_msg_cb;
  78. PicoMCDopenTray = emu_msg_tray_open;
  79. PicoMCDcloseTray = menu_loop_tray;
  80. }
  81. static void scaling_update(void)
  82. {
  83. PicoOpt &= ~0x4100;
  84. switch (currentConfig.scaling) {
  85. default: break; // off
  86. case 1: // hw hor
  87. case 2: PicoOpt |= 0x0100; break; // hw hor+vert
  88. case 3: PicoOpt |= 0x4000; break; // sw hor
  89. }
  90. }
  91. void emu_Deinit(void)
  92. {
  93. // save SRAM
  94. if((currentConfig.EmuOpt & 1) && SRam.changed) {
  95. emu_SaveLoadGame(0, 1);
  96. SRam.changed = 0;
  97. }
  98. if (!(currentConfig.EmuOpt & 0x20)) {
  99. config_writelrom(PicoConfigFile);
  100. #ifndef NO_SYNC
  101. sync();
  102. #endif
  103. }
  104. free(PicoDraw2FB);
  105. PicoExit();
  106. // restore gamma
  107. if (gp2x_old_gamma != 100)
  108. set_gamma(100, 0);
  109. }
  110. void emu_prepareDefaultConfig(void)
  111. {
  112. memset(&defaultConfig, 0, sizeof(defaultConfig));
  113. defaultConfig.EmuOpt = 0x9d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds
  114. defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
  115. defaultConfig.s_PsndRate = 44100;
  116. defaultConfig.s_PicoRegion = 0; // auto
  117. defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
  118. defaultConfig.s_PicoCDBuffers = 0;
  119. defaultConfig.Frameskip = -1; // auto
  120. defaultConfig.CPUclock = 200;
  121. defaultConfig.volume = 50;
  122. defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
  123. defaultConfig.KeyBinds[ 4] = 1<<1;
  124. defaultConfig.KeyBinds[ 2] = 1<<2;
  125. defaultConfig.KeyBinds[ 6] = 1<<3;
  126. defaultConfig.KeyBinds[14] = 1<<4;
  127. defaultConfig.KeyBinds[13] = 1<<5;
  128. defaultConfig.KeyBinds[12] = 1<<6;
  129. defaultConfig.KeyBinds[ 8] = 1<<7;
  130. defaultConfig.KeyBinds[15] = 1<<26; // switch rend
  131. defaultConfig.KeyBinds[10] = 1<<27; // save state
  132. defaultConfig.KeyBinds[11] = 1<<28; // load state
  133. defaultConfig.KeyBinds[23] = 1<<29; // vol up
  134. defaultConfig.KeyBinds[22] = 1<<30; // vol down
  135. defaultConfig.gamma = 100;
  136. defaultConfig.scaling = 0;
  137. defaultConfig.turbo_rate = 15;
  138. }
  139. void osd_text(int x, int y, const char *text)
  140. {
  141. int len = strlen(text)*8;
  142. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  143. int *p, i, h;
  144. x &= ~3; // align x
  145. len = (len+3) >> 2;
  146. for (h = 0; h < 8; h++) {
  147. p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
  148. for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
  149. }
  150. emu_textOut8(x, y, text);
  151. } else {
  152. int *p, i, h;
  153. x &= ~1; // align x
  154. len = (len+1) >> 1;
  155. for (h = 0; h < 8; h++) {
  156. p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
  157. for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
  158. }
  159. emu_textOut16(x, y, text);
  160. }
  161. }
  162. static void draw_cd_leds(void)
  163. {
  164. // static
  165. int old_reg;
  166. // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
  167. old_reg = Pico_mcd->s68k_regs[0];
  168. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  169. // 8-bit modes
  170. unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
  171. unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
  172. *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
  173. *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
  174. *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
  175. *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
  176. *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
  177. *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
  178. } else {
  179. // 16-bit modes
  180. unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
  181. unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
  182. unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
  183. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
  184. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
  185. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  186. }
  187. }
  188. static void draw_pico_ptr(void)
  189. {
  190. unsigned short *p = (unsigned short *)gp2x_screen;
  191. // only if pen enabled and for 16bit modes
  192. if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
  193. if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
  194. p += 32;
  195. p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);
  196. p += pico_pen_x + PICO_PEN_ADJUST_X;
  197. p[0] ^= 0xffff;
  198. p[319] ^= 0xffff;
  199. p[320] ^= 0xffff;
  200. p[321] ^= 0xffff;
  201. p[640] ^= 0xffff;
  202. }
  203. static int EmuScanBegin16(unsigned int num)
  204. {
  205. if (!(Pico.video.reg[1]&8)) num += 8;
  206. DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;
  207. return 0;
  208. }
  209. static int EmuScanBegin8(unsigned int num)
  210. {
  211. if (!(Pico.video.reg[1]&8)) num += 8;
  212. DrawLineDest = (unsigned char *) gp2x_screen + 320 * num;
  213. return 0;
  214. }
  215. int localPal[0x100];
  216. static void (*vidCpyM2)(void *dest, void *src) = NULL;
  217. static void blit(const char *fps, const char *notice)
  218. {
  219. int emu_opt = currentConfig.EmuOpt;
  220. if (PicoOpt&0x10)
  221. {
  222. // 8bit fast renderer
  223. if (Pico.m.dirtyPal) {
  224. Pico.m.dirtyPal = 0;
  225. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  226. // feed new palette to our device
  227. gp2x_video_setpalette(localPal, 0x40);
  228. }
  229. // a hack for VR
  230. if (PicoRead16Hook == PicoSVPRead16)
  231. memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
  232. // do actual copy
  233. vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
  234. }
  235. else if (!(emu_opt&0x80))
  236. {
  237. // 8bit accurate renderer
  238. if (Pico.m.dirtyPal)
  239. {
  240. int pallen = 0x40;
  241. Pico.m.dirtyPal = 0;
  242. if (Pico.video.reg[0xC]&8) // shadow/hilight mode
  243. {
  244. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  245. vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
  246. vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
  247. memcpy32(localPal+0xc0, localPal+0x40, 0x40);
  248. pallen = 0x100;
  249. }
  250. else if (rendstatus & PDRAW_ACC_SPRITES) {
  251. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  252. memcpy32(localPal+0x40, localPal, 0x40);
  253. memcpy32(localPal+0x80, localPal, 0x40);
  254. memcpy32(localPal+0xc0, localPal, 0x40);
  255. pallen = 0x100;
  256. }
  257. else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
  258. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  259. vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
  260. vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
  261. pallen = 0xc0;
  262. }
  263. else {
  264. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  265. }
  266. if (pallen > 0xc0) {
  267. localPal[0xc0] = 0x0000c000;
  268. localPal[0xd0] = 0x00c00000;
  269. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  270. localPal[0xf0] = 0x00ffffff;
  271. }
  272. gp2x_video_setpalette(localPal, pallen);
  273. }
  274. }
  275. if (notice || (emu_opt & 2)) {
  276. int h = 232;
  277. if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
  278. if (notice) osd_text(4, h, notice);
  279. if (emu_opt & 2)
  280. osd_text(osd_fps_x, h, fps);
  281. }
  282. if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
  283. draw_cd_leds();
  284. if (PicoAHW & PAHW_PICO)
  285. draw_pico_ptr();
  286. //gp2x_video_wait_vsync();
  287. gp2x_video_flip();
  288. if (!(PicoOpt&0x10)) {
  289. if (!(Pico.video.reg[1]&8)) {
  290. if (currentConfig.EmuOpt&0x80) {
  291. DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
  292. } else {
  293. DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
  294. }
  295. } else {
  296. DrawLineDest = gp2x_screen;
  297. }
  298. }
  299. }
  300. // clears whole screen or just the notice area (in all buffers)
  301. static void clearArea(int full)
  302. {
  303. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  304. // 8-bit renderers
  305. if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
  306. else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
  307. } else {
  308. // 16bit accurate renderer
  309. if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
  310. else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
  311. }
  312. }
  313. static void vidResetMode(void)
  314. {
  315. if (PicoOpt&0x10) {
  316. gp2x_video_changemode(8);
  317. } else if (currentConfig.EmuOpt&0x80) {
  318. gp2x_video_changemode(16);
  319. PicoDrawSetColorFormat(1);
  320. PicoScanBegin = EmuScanBegin16;
  321. } else {
  322. gp2x_video_changemode(8);
  323. PicoDrawSetColorFormat(2);
  324. PicoScanBegin = EmuScanBegin8;
  325. }
  326. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  327. // setup pal for 8-bit modes
  328. localPal[0xc0] = 0x0000c000; // MCD LEDs
  329. localPal[0xd0] = 0x00c00000;
  330. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  331. localPal[0xf0] = 0x00ffffff;
  332. gp2x_video_setpalette(localPal, 0x100);
  333. gp2x_memset_all_buffers(0, 0xe0, 320*240);
  334. gp2x_video_flip();
  335. }
  336. Pico.m.dirtyPal = 1;
  337. // reset scaling
  338. if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
  339. gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
  340. else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
  341. }
  342. static void emu_msg_cb(const char *msg)
  343. {
  344. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  345. // 8-bit renderers
  346. gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
  347. osd_text(4, 232, msg);
  348. gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
  349. } else {
  350. // 16bit accurate renderer
  351. gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
  352. osd_text(4, 232, msg);
  353. gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
  354. }
  355. gettimeofday(&noticeMsgTime, 0);
  356. noticeMsgTime.tv_sec -= 2;
  357. /* assumption: emu_msg_cb gets called only when something slow is about to happen */
  358. reset_timing = 1;
  359. }
  360. static void emu_state_cb(const char *str)
  361. {
  362. clearArea(0);
  363. blit("", str);
  364. }
  365. static void emu_msg_tray_open(void)
  366. {
  367. strcpy(noticeMsg, "CD tray opened");
  368. gettimeofday(&noticeMsgTime, 0);
  369. }
  370. static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)
  371. {
  372. int ret, px, py, lim_x;
  373. static int pdown_frames = 0;
  374. emu_RunEventsPico(events);
  375. if (pico_inp_mode == 0) return;
  376. // for F200
  377. ret = gp2x_touchpad_read(&px, &py);
  378. if (ret >= 0)
  379. {
  380. if (ret > 35000)
  381. {
  382. if (pdown_frames++ > 5)
  383. PicoPad[0] |= 0x20;
  384. pico_pen_x = px;
  385. pico_pen_y = py;
  386. if (!(Pico.video.reg[12]&1)) {
  387. pico_pen_x -= 32;
  388. if (pico_pen_x < 0) pico_pen_x = 0;
  389. if (pico_pen_x > 248) pico_pen_x = 248;
  390. }
  391. if (pico_pen_y > 224) pico_pen_y = 224;
  392. }
  393. else
  394. pdown_frames = 0;
  395. //if (ret == 0)
  396. // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
  397. }
  398. PicoPad[0] &= ~0x0f; // release UDLR
  399. if (gp2x_keys & GP2X_UP) pico_pen_y--;
  400. if (gp2x_keys & GP2X_DOWN) pico_pen_y++;
  401. if (gp2x_keys & GP2X_LEFT) pico_pen_x--;
  402. if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;
  403. lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
  404. if (pico_pen_y < 8) pico_pen_y = 8;
  405. if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;
  406. if (pico_pen_x < 0) pico_pen_x = 0;
  407. if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;
  408. PicoPicohw.pen_pos[0] = pico_pen_x;
  409. if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
  410. PicoPicohw.pen_pos[0] += 0x3c;
  411. PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
  412. }
  413. static void update_volume(int has_changed, int is_up)
  414. {
  415. static int prev_frame = 0, wait_frames = 0;
  416. int vol = currentConfig.volume;
  417. if (has_changed)
  418. {
  419. if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
  420. wait_frames++;
  421. else {
  422. if (is_up) {
  423. if (vol < 99) vol++;
  424. } else {
  425. if (vol > 0) vol--;
  426. }
  427. wait_frames = 0;
  428. gp2x_sound_volume(vol, vol);
  429. currentConfig.volume = vol;
  430. }
  431. sprintf(noticeMsg, "VOL: %02i", vol);
  432. gettimeofday(&noticeMsgTime, 0);
  433. prev_frame = Pico.m.frame_count;
  434. }
  435. // set the right mixer func
  436. if (!(PicoOpt&8)) return; // just use defaults for mono
  437. if (vol >= 5)
  438. PsndMix_32_to_16l = mix_32_to_16l_stereo;
  439. else {
  440. mix_32_to_16l_level = 5 - vol;
  441. PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
  442. }
  443. }
  444. static void RunEvents(unsigned int which)
  445. {
  446. if (which & 0x1800) // save or load (but not both)
  447. {
  448. int do_it = 1;
  449. if ( emu_checkSaveFile(state_slot) &&
  450. (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
  451. (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
  452. unsigned long keys;
  453. blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
  454. while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
  455. usleep(50*1024);
  456. if (keys & GP2X_X) do_it = 0;
  457. while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release
  458. usleep(50*1024);
  459. clearArea(0);
  460. }
  461. if (do_it) {
  462. osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
  463. PicoStateProgressCB = emu_state_cb;
  464. gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
  465. emu_SaveLoadGame((which & 0x1000) >> 12, 0);
  466. PicoStateProgressCB = NULL;
  467. }
  468. reset_timing = 1;
  469. }
  470. if (which & 0x0400) // switch renderer
  471. {
  472. if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
  473. else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
  474. else currentConfig.EmuOpt &= ~0x80;
  475. vidResetMode();
  476. if (PicoOpt&0x10) {
  477. strcpy(noticeMsg, " 8bit fast renderer");
  478. } else if (currentConfig.EmuOpt&0x80) {
  479. strcpy(noticeMsg, "16bit accurate renderer");
  480. } else {
  481. strcpy(noticeMsg, " 8bit accurate renderer");
  482. }
  483. gettimeofday(&noticeMsgTime, 0);
  484. }
  485. if (which & 0x0300)
  486. {
  487. if(which&0x0200) {
  488. state_slot -= 1;
  489. if(state_slot < 0) state_slot = 9;
  490. } else {
  491. state_slot += 1;
  492. if(state_slot > 9) state_slot = 0;
  493. }
  494. sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
  495. gettimeofday(&noticeMsgTime, 0);
  496. }
  497. if (which & 0x0080) {
  498. engineState = PGS_Menu;
  499. }
  500. }
  501. static void updateKeys(void)
  502. {
  503. unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;
  504. static unsigned int prevEvents = 0;
  505. int joy, i;
  506. keys = gp2x_joystick_read(0);
  507. if (keys & GP2X_SELECT) {
  508. engineState = select_exits ? PGS_Quit : PGS_Menu;
  509. // wait until select is released, so menu would not resume game
  510. while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
  511. }
  512. keys &= CONFIGURABLE_KEYS;
  513. keys2 = keys;
  514. for (i = 0; i < 32; i++)
  515. {
  516. if (keys2 & (1 << i))
  517. {
  518. int pl, acts = currentConfig.KeyBinds[i];
  519. if (!acts) continue;
  520. pl = (acts >> 16) & 1;
  521. if (kb_combo_keys & (1 << i))
  522. {
  523. int u = i+1, acts_c = acts & kb_combo_acts;
  524. // let's try to find the other one
  525. if (acts_c) {
  526. for (; u < 32; u++)
  527. if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {
  528. allActions[pl] |= acts_c & currentConfig.KeyBinds[u];
  529. keys2 &= ~((1 << i) | (1 << u));
  530. break;
  531. }
  532. }
  533. // add non-combo actions if combo ones were not found
  534. if (!acts_c || u == 32)
  535. allActions[pl] |= acts & ~kb_combo_acts;
  536. } else {
  537. allActions[pl] |= acts;
  538. }
  539. }
  540. }
  541. // add joy inputs
  542. if (num_of_joys > 0)
  543. {
  544. gp2x_usbjoy_update();
  545. for (joy = 0; joy < num_of_joys; joy++) {
  546. int btns = gp2x_usbjoy_check2(joy);
  547. for (i = 0; i < 32; i++) {
  548. if (btns & (1 << i)) {
  549. int acts = currentConfig.JoyBinds[joy][i];
  550. int pl = (acts >> 16) & 1;
  551. allActions[pl] |= acts;
  552. }
  553. }
  554. }
  555. }
  556. PicoPad[0] = allActions[0] & 0xfff;
  557. PicoPad[1] = allActions[1] & 0xfff;
  558. if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
  559. if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
  560. events = (allActions[0] | allActions[1]) >> 16;
  561. // volume is treated in special way and triggered every frame
  562. if (events & 0x6000)
  563. update_volume(1, events & 0x2000);
  564. if ((events ^ prevEvents) & 0x40) {
  565. emu_changeFastForward(events & 0x40);
  566. update_volume(0, 0);
  567. reset_timing = 1;
  568. }
  569. events &= ~prevEvents;
  570. if (PicoAHW == PAHW_PICO)
  571. RunEventsPico(events, keys);
  572. if (events) RunEvents(events);
  573. if (movie_data) emu_updateMovie();
  574. prevEvents = (allActions[0] | allActions[1]) >> 16;
  575. }
  576. static void updateSound(int len)
  577. {
  578. if (PicoOpt&8) len<<=1;
  579. /* avoid writing audio when lagging behind to prevent audio lag */
  580. if (PicoSkipFrame != 2)
  581. gp2x_sound_write(PsndOut, len<<1);
  582. }
  583. static void SkipFrame(int do_audio)
  584. {
  585. PicoSkipFrame=do_audio ? 1 : 2;
  586. PicoFrame();
  587. PicoSkipFrame=0;
  588. }
  589. void emu_forcedFrame(int opts)
  590. {
  591. int po_old = PicoOpt;
  592. int eo_old = currentConfig.EmuOpt;
  593. PicoOpt &= ~0x10;
  594. PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
  595. currentConfig.EmuOpt |= 0x80;
  596. //vidResetMode();
  597. PicoDrawSetColorFormat(1);
  598. PicoScanBegin = EmuScanBegin16;
  599. Pico.m.dirtyPal = 1;
  600. PicoFrameDrawOnly();
  601. /*
  602. if (!(Pico.video.reg[12]&1)) {
  603. vidCpyM2 = vidCpyM2_32col;
  604. clearArea(1);
  605. } else vidCpyM2 = vidCpyM2_40col;
  606. vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
  607. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  608. gp2x_video_setpalette(localPal, 0x40);
  609. */
  610. PicoOpt = po_old;
  611. currentConfig.EmuOpt = eo_old;
  612. }
  613. void emu_platformDebugCat(char *str)
  614. {
  615. // nothing
  616. }
  617. static void simpleWait(int thissec, int lim_time)
  618. {
  619. struct timeval tval;
  620. spend_cycles(1024);
  621. gettimeofday(&tval, 0);
  622. if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
  623. while (tval.tv_usec < lim_time)
  624. {
  625. spend_cycles(1024);
  626. gettimeofday(&tval, 0);
  627. if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
  628. }
  629. }
  630. #if 0
  631. static void tga_dump(void)
  632. {
  633. #define BYTE unsigned char
  634. #define WORD unsigned short
  635. struct
  636. {
  637. BYTE IDLength; /* 00h Size of Image ID field */
  638. BYTE ColorMapType; /* 01h Color map type */
  639. BYTE ImageType; /* 02h Image type code */
  640. WORD CMapStart; /* 03h Color map origin */
  641. WORD CMapLength; /* 05h Color map length */
  642. BYTE CMapDepth; /* 07h Depth of color map entries */
  643. WORD XOffset; /* 08h X origin of image */
  644. WORD YOffset; /* 0Ah Y origin of image */
  645. WORD Width; /* 0Ch Width of image */
  646. WORD Height; /* 0Eh Height of image */
  647. BYTE PixelDepth; /* 10h Image pixel size */
  648. BYTE ImageDescriptor; /* 11h Image descriptor byte */
  649. } __attribute__((packed)) TGAHEAD;
  650. static unsigned short oldscr[320*240];
  651. FILE *f; char name[128]; int i;
  652. memset(&TGAHEAD, 0, sizeof(TGAHEAD));
  653. TGAHEAD.ImageType = 2;
  654. TGAHEAD.Width = 320;
  655. TGAHEAD.Height = 240;
  656. TGAHEAD.PixelDepth = 16;
  657. TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
  658. #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
  659. for (i = 0; i < 320*240; i++)
  660. if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
  661. if (i < 320*240)
  662. {
  663. for (i = 0; i < 320*240; i++)
  664. oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
  665. sprintf(name, "%05i.tga", Pico.m.frame_count);
  666. f = fopen(name, "wb");
  667. if (!f) { printf("!f\n"); exit(1); }
  668. fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
  669. fwrite(oldscr, 1, 320*240*2, f);
  670. fclose(f);
  671. }
  672. }
  673. #endif
  674. void emu_Loop(void)
  675. {
  676. static int gp2x_old_clock = 200;
  677. static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;
  678. char fpsbuff[24]; // fps count c string
  679. struct timeval tval; // timing
  680. int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
  681. int frames_done, frames_shown, thissec; // actual frames
  682. int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
  683. char *notice = 0;
  684. printf("entered emu_Loop()\n");
  685. if (gp2x_old_clock != currentConfig.CPUclock) {
  686. printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
  687. set_FCLK(currentConfig.CPUclock);
  688. gp2x_old_clock = currentConfig.CPUclock;
  689. printf(" done\n");
  690. }
  691. if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
  692. set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
  693. gp2x_old_gamma = currentConfig.gamma;
  694. printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
  695. }
  696. if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
  697. if (currentConfig.EmuOpt&0x2000)
  698. set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
  699. else unset_LCD_custom_rate();
  700. }
  701. EmuOpt_old = currentConfig.EmuOpt;
  702. fpsbuff[0] = 0;
  703. // make sure we are in correct mode
  704. vidResetMode();
  705. scaling_update();
  706. Pico.m.dirtyPal = 1;
  707. oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
  708. emu_findKeyBindCombos();
  709. // pal/ntsc might have changed, reset related stuff
  710. target_fps = Pico.m.pal ? 50 : 60;
  711. target_frametime = 1000000/target_fps;
  712. reset_timing = 1;
  713. // prepare sound stuff
  714. if (currentConfig.EmuOpt & 4)
  715. {
  716. int snd_excess_add;
  717. if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
  718. ((PicoOpt&0x200) && crashed_940)) {
  719. PsndRerate(Pico.m.frame_count ? 1 : 0);
  720. }
  721. snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
  722. printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
  723. PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
  724. gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
  725. gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
  726. PicoWriteSound = updateSound;
  727. update_volume(0, 0);
  728. memset(sndBuffer, 0, sizeof(sndBuffer));
  729. PsndOut = sndBuffer;
  730. PsndRate_old = PsndRate;
  731. PicoOpt_old = PicoOpt;
  732. pal_old = Pico.m.pal;
  733. } else {
  734. PsndOut = NULL;
  735. }
  736. // prepare CD buffer
  737. if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
  738. // calc vsync offset to sync timing code with vsync
  739. if (currentConfig.EmuOpt&0x2000) {
  740. gettimeofday(&tval, 0);
  741. gp2x_video_wait_vsync();
  742. gettimeofday(&tval, 0);
  743. vsync_offset = tval.tv_usec;
  744. while (vsync_offset >= target_frametime)
  745. vsync_offset -= target_frametime;
  746. if (!vsync_offset) vsync_offset++;
  747. printf("vsync_offset: %i\n", vsync_offset);
  748. } else
  749. vsync_offset = 0;
  750. frames_done = frames_shown = thissec =
  751. pframes_done = pframes_shown = pthissec = 0;
  752. // loop
  753. while (engineState == PGS_Running)
  754. {
  755. int modes;
  756. gettimeofday(&tval, 0);
  757. if (reset_timing) {
  758. reset_timing = 0;
  759. pthissec = tval.tv_sec;
  760. pframes_shown = pframes_done = tval.tv_usec/target_frametime;
  761. }
  762. // show notice message?
  763. if (noticeMsgTime.tv_sec)
  764. {
  765. static int noticeMsgSum;
  766. if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
  767. noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
  768. clearArea(0);
  769. notice = 0;
  770. } else {
  771. int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
  772. if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
  773. notice = noticeMsg;
  774. }
  775. }
  776. // check for mode changes
  777. modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
  778. if (modes != oldmodes)
  779. {
  780. int scalex = 320;
  781. osd_fps_x = OSD_FPS_X;
  782. if (modes & 4) {
  783. vidCpyM2 = vidCpyM2_40col;
  784. } else {
  785. if (PicoOpt & 0x100) {
  786. vidCpyM2 = vidCpyM2_32col_nobord;
  787. scalex = 256;
  788. osd_fps_x = OSD_FPS_X - 64;
  789. } else {
  790. vidCpyM2 = vidCpyM2_32col;
  791. }
  792. }
  793. if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
  794. gp2x_video_RGB_setscaling(8, scalex, 224);
  795. else gp2x_video_RGB_setscaling(0, scalex, 240);
  796. oldmodes = modes;
  797. clearArea(1);
  798. }
  799. // second changed?
  800. if (thissec != tval.tv_sec)
  801. {
  802. #ifdef BENCHMARK
  803. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  804. if (++bench == 10) {
  805. bench = 0;
  806. bench_fps_s = bench_fps;
  807. bf[bfp++ & 3] = bench_fps;
  808. bench_fps = 0;
  809. }
  810. bench_fps += frames_shown;
  811. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  812. #else
  813. if (currentConfig.EmuOpt & 2) {
  814. sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
  815. if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
  816. }
  817. #endif
  818. frames_shown = frames_done = 0;
  819. thissec = tval.tv_sec;
  820. }
  821. #ifdef PFRAMES
  822. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  823. #endif
  824. if (pthissec != tval.tv_sec)
  825. {
  826. if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
  827. pframes_done = pframes_shown = 0;
  828. } else {
  829. // it is quite common for this implementation to leave 1 fame unfinished
  830. // when second changes, but we don't want buffer to starve.
  831. if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
  832. updateKeys();
  833. SkipFrame(1); pframes_done++;
  834. }
  835. pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
  836. pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
  837. if (pframes_shown > pframes_done) pframes_shown = pframes_done;
  838. }
  839. pthissec = tval.tv_sec;
  840. }
  841. lim_time = (pframes_done+1) * target_frametime + vsync_offset;
  842. if (currentConfig.Frameskip >= 0) // frameskip enabled
  843. {
  844. for(i = 0; i < currentConfig.Frameskip; i++) {
  845. updateKeys();
  846. SkipFrame(1); pframes_done++; frames_done++;
  847. if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
  848. gettimeofday(&tval, 0);
  849. if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
  850. if (tval.tv_usec < lim_time) { // we are too fast
  851. simpleWait(pthissec, lim_time);
  852. }
  853. }
  854. lim_time += target_frametime;
  855. }
  856. }
  857. else if (tval.tv_usec > lim_time) // auto frameskip
  858. {
  859. // no time left for this frame - skip
  860. if (tval.tv_usec - lim_time >= 300000) {
  861. /* something caused a slowdown for us (disk access? cache flush?)
  862. * try to recover by resetting timing... */
  863. reset_timing = 1;
  864. continue;
  865. }
  866. updateKeys();
  867. SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
  868. continue;
  869. }
  870. updateKeys();
  871. PicoFrame();
  872. // check time
  873. gettimeofday(&tval, 0);
  874. if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
  875. if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
  876. reset_timing = 1;
  877. else if (PsndOut != NULL || currentConfig.Frameskip < 0)
  878. {
  879. // sleep or vsync if we are still too fast
  880. // usleep sleeps for ~20ms minimum, so it is not a solution here
  881. if (!reset_timing && tval.tv_usec < lim_time)
  882. {
  883. // we are too fast
  884. if (vsync_offset) {
  885. if (lim_time - tval.tv_usec > target_frametime/2)
  886. simpleWait(pthissec, lim_time - target_frametime/4);
  887. gp2x_video_wait_vsync();
  888. } else {
  889. simpleWait(pthissec, lim_time);
  890. }
  891. }
  892. }
  893. blit(fpsbuff, notice);
  894. pframes_done++; pframes_shown++;
  895. frames_done++; frames_shown++;
  896. }
  897. emu_changeFastForward(0);
  898. if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
  899. // save SRAM
  900. if((currentConfig.EmuOpt & 1) && SRam.changed) {
  901. emu_state_cb("Writing SRAM/BRAM..");
  902. emu_SaveLoadGame(0, 1);
  903. SRam.changed = 0;
  904. }
  905. // if in 8bit mode, generate 16bit image for menu background
  906. if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
  907. emu_forcedFrame(POPT_EN_SOFTSCALE);
  908. }
  909. void emu_ResetGame(void)
  910. {
  911. PicoReset();
  912. reset_timing = 1;
  913. }