emu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // (c) Copyright 2006-2009 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 <stdarg.h>
  8. #include "../common/arm_utils.h"
  9. #include "../common/fonts.h"
  10. #include "../common/emu.h"
  11. #include "../common/menu.h"
  12. #include "../common/config.h"
  13. #include "../common/input.h"
  14. #include "../linux/sndout_oss.h"
  15. #include "asm_utils.h"
  16. #include <pico/pico_int.h>
  17. #include <pico/patch.h>
  18. #include <pico/sound/mix.h>
  19. #include <zlib/zlib.h>
  20. //#define PFRAMES
  21. #define BENCHMARK
  22. //#define USE_320_SCREEN 1
  23. #ifdef BENCHMARK
  24. #define OSD_FPS_X (800-200)
  25. #else
  26. #define OSD_FPS_X (800-120)
  27. #endif
  28. static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
  29. static int osd_fps_x;
  30. unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
  31. #define PICO_PEN_ADJUST_X 4
  32. #define PICO_PEN_ADJUST_Y 2
  33. static int pico_pen_x = 0, pico_pen_y = 240/2;
  34. int plat_get_root_dir(char *dst, int len)
  35. {
  36. extern char **g_argv;
  37. int j;
  38. strncpy(dst, g_argv[0], len);
  39. len -= 32; // reserve
  40. if (len < 0) len = 0;
  41. dst[len] = 0;
  42. for (j = strlen(dst); j > 0; j--)
  43. if (dst[j] == '/') { dst[j+1] = 0; break; }
  44. return j + 1;
  45. }
  46. void pemu_prep_defconfig(void)
  47. {
  48. memset(&defaultConfig, 0, sizeof(defaultConfig));
  49. defaultConfig.EmuOpt = 0x8f | 0x00600; // | <- confirm_save, cd_leds
  50. defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC;
  51. defaultConfig.s_PicoOpt |= POPT_ACC_SPRITES|POPT_EN_MCD_GFX;
  52. defaultConfig.EmuOpt &= ~8; // no save gzip
  53. defaultConfig.s_PsndRate = 44100;
  54. defaultConfig.s_PicoRegion = 0;
  55. defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
  56. defaultConfig.s_PicoCDBuffers = 0;
  57. defaultConfig.Frameskip = 0;
  58. defaultConfig.CPUclock = 200;
  59. defaultConfig.volume = 50;
  60. defaultConfig.scaling = 0;
  61. defaultConfig.turbo_rate = 15;
  62. }
  63. static void textOut16(int x, int y, const char *text)
  64. {
  65. int i,l,len=strlen(text);
  66. unsigned int *screen = (unsigned int *)((unsigned short *)g_screen_ptr + (x&~1) + y*g_screen_width);
  67. for (i = 0; i < len; i++)
  68. {
  69. for (l=0;l<16;)
  70. {
  71. unsigned char fd = fontdata8x8[((text[i])*8)+l/2];
  72. unsigned int *d = &screen[l*g_screen_width/2];
  73. if (fd&0x80) d[0]=0xffffffff;
  74. if (fd&0x40) d[1]=0xffffffff;
  75. if (fd&0x20) d[2]=0xffffffff;
  76. if (fd&0x10) d[3]=0xffffffff;
  77. if (fd&0x08) d[4]=0xffffffff;
  78. if (fd&0x04) d[5]=0xffffffff;
  79. if (fd&0x02) d[6]=0xffffffff;
  80. if (fd&0x01) d[7]=0xffffffff;
  81. l++; d = &screen[l*g_screen_width/2];
  82. if (fd&0x80) d[0]=0xffffffff;
  83. if (fd&0x40) d[1]=0xffffffff;
  84. if (fd&0x20) d[2]=0xffffffff;
  85. if (fd&0x10) d[3]=0xffffffff;
  86. if (fd&0x08) d[4]=0xffffffff;
  87. if (fd&0x04) d[5]=0xffffffff;
  88. if (fd&0x02) d[6]=0xffffffff;
  89. if (fd&0x01) d[7]=0xffffffff;
  90. l++;
  91. }
  92. screen += 8;
  93. }
  94. }
  95. static void osd_text(int x, int y, const char *text)
  96. {
  97. int len = strlen(text)*8;
  98. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  99. int *p, i, h;
  100. x &= ~3; // align x
  101. len = (len+3) >> 2;
  102. for (h = 0; h < 8; h++) {
  103. p = (int *) ((unsigned char *) g_screen_ptr+x+g_screen_width*(y+h));
  104. for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
  105. }
  106. emu_text_out8(x, y, text);
  107. } else {
  108. int *p, i, h;
  109. x &= ~1; // align x
  110. len++;
  111. for (h = 0; h < 16; h++) {
  112. p = (int *) ((unsigned short *) g_screen_ptr+x+g_screen_width*(y+h));
  113. for (i = len; i; i--, p++) *p = 0;//(*p>>2)&0x39e7;
  114. }
  115. text_out16(x, y, text);
  116. }
  117. }
  118. static void draw_cd_leds(void)
  119. {
  120. // static
  121. int old_reg;
  122. // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
  123. old_reg = Pico_mcd->s68k_regs[0];
  124. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  125. // 8-bit modes
  126. unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
  127. unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
  128. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =
  129. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =
  130. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;
  131. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =
  132. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =
  133. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;
  134. } else {
  135. // 16-bit modes
  136. unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);
  137. unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
  138. unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
  139. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
  140. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
  141. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  142. }
  143. }
  144. static void draw_pico_ptr(void)
  145. {
  146. unsigned short *p = (unsigned short *)g_screen_ptr;
  147. // only if pen enabled and for 16bit modes
  148. if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
  149. if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
  150. p += 32;
  151. p += g_screen_width * (pico_pen_y + PICO_PEN_ADJUST_Y);
  152. p += pico_pen_x + PICO_PEN_ADJUST_X;
  153. p[0] ^= 0xffff;
  154. p[319] ^= 0xffff;
  155. p[320] ^= 0xffff;
  156. p[321] ^= 0xffff;
  157. p[640] ^= 0xffff;
  158. }
  159. #ifdef USE_320_SCREEN
  160. static int EmuScanBegin16(unsigned int num)
  161. {
  162. DrawLineDest = (unsigned short *)g_screen_ptr + num*800 + 800/2 - 320/2;
  163. //int w = (Pico.video.reg[12]&1) ? 320 : 256;
  164. //DrawLineDest = (unsigned short *)g_screen_ptr + num*w;
  165. return 0;
  166. }
  167. #else // USE_320_SCREEN
  168. static int EmuScanEnd16(unsigned int num)
  169. {
  170. unsigned char *ps=HighCol+8;
  171. unsigned short *pd;
  172. unsigned short *pal=HighPal;
  173. int sh = Pico.video.reg[0xC]&8;
  174. int len, mask = 0xff;
  175. pd=(unsigned short *)g_screen_ptr + num*800*2 + 800/2 - 320*2/2;
  176. if (Pico.m.dirtyPal)
  177. PicoDoHighPal555(sh);
  178. if (Pico.video.reg[12]&1) {
  179. len = 320;
  180. } else {
  181. pd += 32*2;
  182. len = 256;
  183. }
  184. if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))
  185. mask=0x3f; // messed sprites, upper bits are priority stuff
  186. #if 1
  187. clut_line(pd, ps, pal, (mask<<16) | len);
  188. #else
  189. for (; len > 0; len--)
  190. {
  191. unsigned int p = pal[*ps++ & mask];
  192. p |= p << 16;
  193. *(unsigned int *)pd = p;
  194. *(unsigned int *)(&pd[800]) = p;
  195. pd += 2;
  196. }
  197. #endif
  198. return 0;
  199. }
  200. #endif // USE_320_SCREEN
  201. int localPal[0x100];
  202. static void (*vidCpyM2)(void *dest, void *src) = NULL;
  203. static void blit(const char *fps, const char *notice)
  204. {
  205. int emu_opt = currentConfig.EmuOpt;
  206. if (PicoOpt&0x10)
  207. {
  208. // 8bit fast renderer
  209. if (Pico.m.dirtyPal) {
  210. Pico.m.dirtyPal = 0;
  211. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  212. // feed new palette to our device
  213. // gp2x_video_setpalette(localPal, 0x40);
  214. }
  215. // a hack for VR
  216. if (PicoAHW & PAHW_SVP)
  217. memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
  218. // do actual copy
  219. vidCpyM2((unsigned char *)g_screen_ptr+g_screen_width*8, PicoDraw2FB+328*8);
  220. }
  221. else if (!(emu_opt&0x80))
  222. {
  223. // 8bit accurate renderer
  224. if (Pico.m.dirtyPal)
  225. {
  226. int pallen = 0xc0;
  227. Pico.m.dirtyPal = 0;
  228. if (Pico.video.reg[0xC]&8) // shadow/hilight mode
  229. {
  230. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  231. vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
  232. vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
  233. memcpy32(localPal+0xc0, localPal+0x40, 0x40);
  234. pallen = 0x100;
  235. }
  236. else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
  237. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  238. vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
  239. vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
  240. pallen = 0xc0;
  241. }
  242. else {
  243. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  244. memcpy32(localPal+0x80, localPal, 0x40);
  245. }
  246. if (pallen > 0xc0) {
  247. localPal[0xc0] = 0x0000c000;
  248. localPal[0xd0] = 0x00c00000;
  249. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  250. localPal[0xf0] = 0x00ffffff;
  251. }
  252. // gp2x_video_setpalette(localPal, pallen);
  253. }
  254. }
  255. if (notice || (emu_opt & 2)) {
  256. int h = g_screen_height-16;
  257. if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 16;
  258. if (notice) osd_text(4, h, notice);
  259. if (emu_opt & 2)
  260. osd_text(osd_fps_x, h, fps);
  261. }
  262. if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
  263. draw_cd_leds();
  264. if (PicoAHW & PAHW_PICO)
  265. draw_pico_ptr();
  266. //gp2x_video_wait_vsync();
  267. // gp2x_video_flip();
  268. }
  269. // clears whole screen or just the notice area (in all buffers)
  270. static void clearArea(int full)
  271. {
  272. if (full) memset(g_screen_ptr, 0, g_screen_width*g_screen_height*2);
  273. else memset((short *)g_screen_ptr + g_screen_width * (g_screen_height - 16), 0,
  274. g_screen_width * 16 * 2);
  275. }
  276. static void vidResetMode(void)
  277. {
  278. #if 0
  279. if (PicoOpt&0x10) {
  280. gp2x_video_changemode(8);
  281. } else if (currentConfig.EmuOpt&0x80) {
  282. gp2x_video_changemode(16);
  283. #ifdef USE_320_SCREEN
  284. PicoDrawSetColorFormat(1);
  285. PicoScanBegin = EmuScanBegin16;
  286. #else
  287. PicoDrawSetColorFormat(-1);
  288. PicoScanEnd = EmuScanEnd16;
  289. #endif
  290. } else {
  291. gp2x_video_changemode(8);
  292. PicoDrawSetColorFormat(2);
  293. PicoScanBegin = EmuScanBegin8;
  294. }
  295. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  296. // setup pal for 8-bit modes
  297. localPal[0xc0] = 0x0000c000; // MCD LEDs
  298. localPal[0xd0] = 0x00c00000;
  299. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  300. localPal[0xf0] = 0x00ffffff;
  301. gp2x_video_setpalette(localPal, 0x100);
  302. gp2x_memset_all_buffers(0, 0xe0, 320*240);
  303. gp2x_video_flip();
  304. }
  305. Pico.m.dirtyPal = 1;
  306. // reset scaling
  307. if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
  308. gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
  309. else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
  310. #else
  311. #ifdef USE_320_SCREEN
  312. PicoDrawSetColorFormat(1);
  313. PicoScanBegin = EmuScanBegin16;
  314. #else
  315. PicoDrawSetColorFormat(-1);
  316. PicoScanEnd = EmuScanEnd16;
  317. #endif
  318. #endif
  319. }
  320. static void update_volume(int has_changed, int is_up)
  321. {
  322. static int prev_frame = 0, wait_frames = 0;
  323. int vol = currentConfig.volume;
  324. if (has_changed)
  325. {
  326. if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
  327. wait_frames++;
  328. else {
  329. if (is_up) {
  330. if (vol < 99) vol++;
  331. } else {
  332. if (vol > 0) vol--;
  333. }
  334. wait_frames = 0;
  335. sndout_oss_setvol(vol, vol);
  336. currentConfig.volume = vol;
  337. }
  338. sprintf(noticeMsg, "VOL: %02i", vol);
  339. gettimeofday(&noticeMsgTime, 0);
  340. prev_frame = Pico.m.frame_count;
  341. }
  342. // set the right mixer func
  343. if (!(PicoOpt&8)) return; // just use defaults for mono
  344. if (vol >= 5)
  345. PsndMix_32_to_16l = mix_32_to_16l_stereo;
  346. else {
  347. mix_32_to_16l_level = 5 - vol;
  348. PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
  349. }
  350. }
  351. static void updateSound(int len)
  352. {
  353. if (PicoOpt&8) len<<=1;
  354. /* avoid writing audio when lagging behind to prevent audio lag */
  355. if (PicoSkipFrame != 2)
  356. sndout_oss_write(PsndOut, len<<1);
  357. }
  358. static void SkipFrame(int do_audio)
  359. {
  360. PicoSkipFrame=do_audio ? 1 : 2;
  361. PicoFrame();
  362. PicoSkipFrame=0;
  363. }
  364. void pemu_forced_frame(int opts)
  365. {
  366. int po_old = PicoOpt;
  367. int eo_old = currentConfig.EmuOpt;
  368. PicoOpt &= ~0x10;
  369. PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
  370. currentConfig.EmuOpt |= 0x80;
  371. //vidResetMode();
  372. #ifdef USE_320_SCREEN
  373. PicoDrawSetColorFormat(1);
  374. PicoScanBegin = EmuScanBegin16;
  375. #else
  376. PicoDrawSetColorFormat(-1);
  377. PicoScanEnd = EmuScanEnd16;
  378. #endif
  379. Pico.m.dirtyPal = 1;
  380. PicoFrameDrawOnly();
  381. /*
  382. if (!(Pico.video.reg[12]&1)) {
  383. vidCpyM2 = vidCpyM2_32col;
  384. clearArea(1);
  385. } else vidCpyM2 = vidCpyM2_40col;
  386. vidCpyM2((unsigned char *)g_screen_ptr+g_screen_width*8, PicoDraw2FB+328*8);
  387. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  388. gp2x_video_setpalette(localPal, 0x40);
  389. */
  390. PicoOpt = po_old;
  391. currentConfig.EmuOpt = eo_old;
  392. }
  393. void plat_debug_cat(char *str)
  394. {
  395. }
  396. static void simpleWait(int thissec, int lim_time)
  397. {
  398. struct timeval tval;
  399. spend_cycles(1024);
  400. gettimeofday(&tval, 0);
  401. if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
  402. while (tval.tv_usec < lim_time)
  403. {
  404. spend_cycles(1024);
  405. gettimeofday(&tval, 0);
  406. if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
  407. }
  408. }
  409. void pemu_sound_start(void)
  410. {
  411. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  412. int target_fps = Pico.m.pal ? 50 : 60;
  413. PsndOut = NULL;
  414. if (currentConfig.EmuOpt & 4)
  415. {
  416. int snd_excess_add;
  417. if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old)
  418. PsndRerate(Pico.m.frame_count ? 1 : 0);
  419. snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
  420. printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
  421. PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
  422. sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);
  423. sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
  424. PicoWriteSound = updateSound;
  425. update_volume(0, 0);
  426. memset(sndBuffer, 0, sizeof(sndBuffer));
  427. PsndOut = sndBuffer;
  428. PsndRate_old = PsndRate;
  429. PicoOpt_old = PicoOpt;
  430. pal_old = Pico.m.pal;
  431. }
  432. }
  433. void pemu_sound_stop(void)
  434. {
  435. }
  436. void pemu_sound_wait(void)
  437. {
  438. // don't need to do anything, writes will block by themselves
  439. }
  440. void pemu_loop(void)
  441. {
  442. char fpsbuff[24]; // fps count c string
  443. struct timeval tval; // timing
  444. int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
  445. int frames_done, frames_shown, thissec; // actual frames
  446. int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
  447. char *notice = 0;
  448. printf("entered emu_Loop()\n");
  449. fpsbuff[0] = 0;
  450. // make sure we are in correct mode
  451. vidResetMode();
  452. Pico.m.dirtyPal = 1;
  453. oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
  454. // pal/ntsc might have changed, reset related stuff
  455. target_fps = Pico.m.pal ? 50 : 60;
  456. target_frametime = 1000000/target_fps;
  457. reset_timing = 1;
  458. pemu_sound_start();
  459. // prepare CD buffer
  460. if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
  461. // calc vsync offset to sync timing code with vsync
  462. if (currentConfig.EmuOpt&0x2000) {
  463. gettimeofday(&tval, 0);
  464. //gp2x_video_wait_vsync();
  465. gettimeofday(&tval, 0);
  466. vsync_offset = tval.tv_usec;
  467. while (vsync_offset >= target_frametime)
  468. vsync_offset -= target_frametime;
  469. if (!vsync_offset) vsync_offset++;
  470. printf("vsync_offset: %i\n", vsync_offset);
  471. } else
  472. vsync_offset = 0;
  473. frames_done = frames_shown = thissec =
  474. pframes_done = pframes_shown = pthissec = 0;
  475. // loop
  476. while (engineState == PGS_Running)
  477. {
  478. int modes;
  479. gettimeofday(&tval, 0);
  480. if (reset_timing) {
  481. reset_timing = 0;
  482. pthissec = tval.tv_sec;
  483. pframes_shown = pframes_done = tval.tv_usec/target_frametime;
  484. }
  485. // show notice message?
  486. if (noticeMsgTime.tv_sec)
  487. {
  488. static int noticeMsgSum;
  489. if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
  490. noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
  491. clearArea(0);
  492. notice = 0;
  493. } else {
  494. int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
  495. if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
  496. notice = noticeMsg;
  497. }
  498. }
  499. // check for mode changes
  500. modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
  501. if (modes != oldmodes)
  502. {
  503. int scalex = g_screen_width;
  504. osd_fps_x = OSD_FPS_X;
  505. if (modes & 4) {
  506. vidCpyM2 = vidCpyM2_40col;
  507. } else {
  508. if (PicoOpt & 0x100) {
  509. vidCpyM2 = vidCpyM2_32col_nobord;
  510. scalex = 256;
  511. osd_fps_x = OSD_FPS_X - 64;
  512. } else {
  513. vidCpyM2 = vidCpyM2_32col;
  514. }
  515. }
  516. //if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
  517. // gp2x_video_RGB_setscaling(8, scalex, 224);
  518. // else gp2x_video_RGB_setscaling(0, scalex, 240);
  519. oldmodes = modes;
  520. clearArea(1);
  521. }
  522. // second changed?
  523. if (thissec != tval.tv_sec)
  524. {
  525. #ifdef BENCHMARK
  526. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  527. if (++bench == 4) {
  528. bench = 0;
  529. bench_fps_s = bench_fps / 4;
  530. bf[bfp++ & 3] = bench_fps / 4;
  531. bench_fps = 0;
  532. }
  533. bench_fps += frames_shown;
  534. sprintf(fpsbuff, "%3i/%3i/%3i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  535. printf("%s\n", fpsbuff);
  536. #else
  537. if (currentConfig.EmuOpt & 2) {
  538. sprintf(fpsbuff, "%3i/%3i", frames_shown, frames_done);
  539. if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
  540. }
  541. #endif
  542. frames_shown = frames_done = 0;
  543. thissec = tval.tv_sec;
  544. }
  545. #ifdef PFRAMES
  546. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  547. #endif
  548. if (pthissec != tval.tv_sec)
  549. {
  550. if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
  551. pframes_done = pframes_shown = 0;
  552. } else {
  553. // it is quite common for this implementation to leave 1 fame unfinished
  554. // when second changes, but we don't want buffer to starve.
  555. if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
  556. emu_update_input();
  557. SkipFrame(1); pframes_done++;
  558. }
  559. pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
  560. pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
  561. if (pframes_shown > pframes_done) pframes_shown = pframes_done;
  562. }
  563. pthissec = tval.tv_sec;
  564. }
  565. lim_time = (pframes_done+1) * target_frametime + vsync_offset;
  566. if (currentConfig.Frameskip >= 0) // frameskip enabled
  567. {
  568. for(i = 0; i < currentConfig.Frameskip; i++) {
  569. emu_update_input();
  570. SkipFrame(1); pframes_done++; frames_done++;
  571. if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
  572. gettimeofday(&tval, 0);
  573. if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
  574. if (tval.tv_usec < lim_time) { // we are too fast
  575. simpleWait(pthissec, lim_time);
  576. }
  577. }
  578. lim_time += target_frametime;
  579. }
  580. }
  581. else if (tval.tv_usec > lim_time) // auto frameskip
  582. {
  583. // no time left for this frame - skip
  584. if (tval.tv_usec - lim_time >= 300000) {
  585. /* something caused a slowdown for us (disk access? cache flush?)
  586. * try to recover by resetting timing... */
  587. reset_timing = 1;
  588. continue;
  589. }
  590. emu_update_input();
  591. SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
  592. continue;
  593. }
  594. emu_update_input();
  595. PicoFrame();
  596. // check time
  597. gettimeofday(&tval, 0);
  598. if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
  599. if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
  600. reset_timing = 1;
  601. #if 1
  602. else if (PsndOut != NULL || currentConfig.Frameskip < 0)
  603. {
  604. // sleep or vsync if we are still too fast
  605. // usleep sleeps for ~20ms minimum, so it is not a solution here
  606. if (!reset_timing && tval.tv_usec < lim_time)
  607. {
  608. // we are too fast
  609. if (vsync_offset) {
  610. if (lim_time - tval.tv_usec > target_frametime/2)
  611. simpleWait(pthissec, lim_time - target_frametime/4);
  612. // gp2x_video_wait_vsync();
  613. } else {
  614. simpleWait(pthissec, lim_time);
  615. }
  616. }
  617. }
  618. #endif
  619. blit(fpsbuff, notice);
  620. pframes_done++; pframes_shown++;
  621. frames_done++; frames_shown++;
  622. }
  623. emu_set_fastforward(0);
  624. if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
  625. // save SRAM
  626. if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
  627. /* FIXME: plat_status_msg_busy_first */
  628. emu_state_cb("Writing SRAM/BRAM..");
  629. emu_save_load_game(0, 1);
  630. SRam.changed = 0;
  631. }
  632. // if in 8bit mode, generate 16bit image for menu background
  633. if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
  634. pemu_forced_frame(POPT_EN_SOFTSCALE);
  635. }