1 // (c) Copyright 2006-2009 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\r
8 #include <sys/time.h>
\r
11 #include "../common/arm_utils.h"
\r
12 #include "../common/fonts.h"
\r
13 #include "../common/emu.h"
\r
14 #include "../common/menu.h"
\r
15 #include "../common/config.h"
\r
16 #include "../common/input.h"
\r
17 #include "../linux/sndout_oss.h"
\r
18 #include "asm_utils.h"
\r
20 #include <pico/pico_int.h>
\r
21 #include <pico/patch.h>
\r
22 #include <pico/sound/mix.h>
\r
23 #include <zlib/zlib.h>
\r
27 //#define USE_320_SCREEN 1
\r
30 #define OSD_FPS_X (800-200)
\r
32 #define OSD_FPS_X (800-120)
\r
36 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
37 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
38 static int osd_fps_x;
\r
39 unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
\r
41 #define PICO_PEN_ADJUST_X 4
\r
42 #define PICO_PEN_ADJUST_Y 2
\r
43 static int pico_pen_x = 0, pico_pen_y = 240/2;
\r
46 void plat_status_msg(const char *format, ...)
\r
50 va_start(vl, format);
\r
51 vsnprintf(noticeMsg, sizeof(noticeMsg), fmt, vl);
\r
54 gettimeofday(¬iceMsgTime, 0);
\r
57 int plat_get_root_dir(char *dst, int len)
\r
59 extern char **g_argv;
\r
62 strncpy(dst, g_argv[0], len);
\r
63 len -= 32; // reserve
\r
64 if (len < 0) len = 0;
\r
66 for (j = strlen(dst); j > 0; j--)
\r
67 if (dst[j] == '/') { dst[j+1] = 0; break; }
\r
72 void pemu_prep_defconfig(void)
\r
74 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
75 defaultConfig.EmuOpt = 0x8f | 0x00600; // | <- confirm_save, cd_leds
\r
76 defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC;
\r
77 defaultConfig.s_PicoOpt |= POPT_ACC_SPRITES|POPT_EN_MCD_GFX;
\r
78 defaultConfig.EmuOpt &= ~8; // no save gzip
\r
79 defaultConfig.s_PsndRate = 44100;
\r
80 defaultConfig.s_PicoRegion = 0;
\r
81 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
82 defaultConfig.s_PicoCDBuffers = 0;
\r
83 defaultConfig.Frameskip = 0;
\r
84 defaultConfig.CPUclock = 200;
\r
85 defaultConfig.volume = 50;
\r
86 defaultConfig.scaling = 0;
\r
87 defaultConfig.turbo_rate = 15;
\r
90 static void textOut16(int x, int y, const char *text)
\r
92 int i,l,len=strlen(text);
\r
93 unsigned int *screen = (unsigned int *)((unsigned short *)g_screen_ptr + (x&~1) + y*g_screen_width);
\r
95 for (i = 0; i < len; i++)
\r
99 unsigned char fd = fontdata8x8[((text[i])*8)+l/2];
\r
100 unsigned int *d = &screen[l*g_screen_width/2];
\r
101 if (fd&0x80) d[0]=0xffffffff;
\r
102 if (fd&0x40) d[1]=0xffffffff;
\r
103 if (fd&0x20) d[2]=0xffffffff;
\r
104 if (fd&0x10) d[3]=0xffffffff;
\r
105 if (fd&0x08) d[4]=0xffffffff;
\r
106 if (fd&0x04) d[5]=0xffffffff;
\r
107 if (fd&0x02) d[6]=0xffffffff;
\r
108 if (fd&0x01) d[7]=0xffffffff;
\r
109 l++; d = &screen[l*g_screen_width/2];
\r
110 if (fd&0x80) d[0]=0xffffffff;
\r
111 if (fd&0x40) d[1]=0xffffffff;
\r
112 if (fd&0x20) d[2]=0xffffffff;
\r
113 if (fd&0x10) d[3]=0xffffffff;
\r
114 if (fd&0x08) d[4]=0xffffffff;
\r
115 if (fd&0x04) d[5]=0xffffffff;
\r
116 if (fd&0x02) d[6]=0xffffffff;
\r
117 if (fd&0x01) d[7]=0xffffffff;
\r
125 static void osd_text(int x, int y, const char *text)
\r
127 int len = strlen(text)*8;
\r
129 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
131 x &= ~3; // align x
\r
132 len = (len+3) >> 2;
\r
133 for (h = 0; h < 8; h++) {
\r
134 p = (int *) ((unsigned char *) g_screen_ptr+x+g_screen_width*(y+h));
\r
135 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
137 emu_text_out8(x, y, text);
\r
140 x &= ~1; // align x
\r
142 for (h = 0; h < 16; h++) {
\r
143 p = (int *) ((unsigned short *) g_screen_ptr+x+g_screen_width*(y+h));
\r
144 for (i = len; i; i--, p++) *p = 0;//(*p>>2)&0x39e7;
\r
146 text_out16(x, y, text);
\r
150 static void draw_cd_leds(void)
\r
154 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
\r
155 old_reg = Pico_mcd->s68k_regs[0];
\r
157 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
159 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
160 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
161 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =
\r
162 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =
\r
163 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;
\r
164 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =
\r
165 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =
\r
166 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;
\r
169 unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);
\r
170 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
171 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
172 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
173 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
174 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
178 static void draw_pico_ptr(void)
\r
180 unsigned short *p = (unsigned short *)g_screen_ptr;
\r
182 // only if pen enabled and for 16bit modes
\r
183 if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
\r
185 if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
\r
188 p += g_screen_width * (pico_pen_y + PICO_PEN_ADJUST_Y);
\r
189 p += pico_pen_x + PICO_PEN_ADJUST_X;
\r
197 #ifdef USE_320_SCREEN
\r
199 static int EmuScanBegin16(unsigned int num)
\r
201 if (!(Pico.video.reg[1]&8)) num += 8;
\r
202 DrawLineDest = (unsigned short *)g_screen_ptr + num*800 + 800/2 - 320/2;
\r
203 //int w = (Pico.video.reg[12]&1) ? 320 : 256;
\r
204 //DrawLineDest = (unsigned short *)g_screen_ptr + num*w;
\r
209 #else // USE_320_SCREEN
\r
211 static int EmuScanEnd16(unsigned int num)
\r
213 unsigned char *ps=HighCol+8;
\r
214 unsigned short *pd;
\r
215 unsigned short *pal=HighPal;
\r
216 int sh = Pico.video.reg[0xC]&8;
\r
217 int len, mask = 0xff;
\r
219 if (!(Pico.video.reg[1]&8)) num += 8;
\r
220 pd=(unsigned short *)g_screen_ptr + num*800*2 + 800/2 - 320*2/2;
\r
222 if (Pico.m.dirtyPal)
\r
223 PicoDoHighPal555(sh);
\r
225 if (Pico.video.reg[12]&1) {
\r
232 if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))
\r
233 mask=0x3f; // messed sprites, upper bits are priority stuff
\r
236 clut_line(pd, ps, pal, (mask<<16) | len);
\r
238 for (; len > 0; len--)
\r
240 unsigned int p = pal[*ps++ & mask];
\r
242 *(unsigned int *)pd = p;
\r
243 *(unsigned int *)(&pd[800]) = p;
\r
251 #endif // USE_320_SCREEN
\r
253 int localPal[0x100];
\r
254 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
256 static void blit(const char *fps, const char *notice)
\r
258 int emu_opt = currentConfig.EmuOpt;
\r
262 // 8bit fast renderer
\r
263 if (Pico.m.dirtyPal) {
\r
264 Pico.m.dirtyPal = 0;
\r
265 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
266 // feed new palette to our device
\r
267 // gp2x_video_setpalette(localPal, 0x40);
\r
270 if (PicoRead16Hook == PicoSVPRead16)
\r
271 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
\r
273 vidCpyM2((unsigned char *)g_screen_ptr+g_screen_width*8, PicoDraw2FB+328*8);
\r
275 else if (!(emu_opt&0x80))
\r
277 // 8bit accurate renderer
\r
278 if (Pico.m.dirtyPal)
\r
281 Pico.m.dirtyPal = 0;
\r
282 if (Pico.video.reg[0xC]&8) // shadow/hilight mode
\r
284 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
285 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
286 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
287 memcpy32(localPal+0xc0, localPal+0x40, 0x40);
\r
290 else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
\r
291 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
292 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
293 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
297 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
298 memcpy32(localPal+0x80, localPal, 0x40);
\r
300 if (pallen > 0xc0) {
\r
301 localPal[0xc0] = 0x0000c000;
\r
302 localPal[0xd0] = 0x00c00000;
\r
303 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
304 localPal[0xf0] = 0x00ffffff;
\r
306 // gp2x_video_setpalette(localPal, pallen);
\r
310 if (notice || (emu_opt & 2)) {
\r
311 int h = g_screen_height-16;
\r
312 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 16;
\r
313 if (notice) osd_text(4, h, notice);
\r
315 osd_text(osd_fps_x, h, fps);
\r
317 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
\r
319 if (PicoAHW & PAHW_PICO)
\r
322 //gp2x_video_wait_vsync();
\r
323 // gp2x_video_flip();
\r
327 // clears whole screen or just the notice area (in all buffers)
\r
328 static void clearArea(int full)
\r
330 if (full) memset(g_screen_ptr, 0, g_screen_width*g_screen_height*2);
\r
331 else memset((short *)g_screen_ptr + g_screen_width * (g_screen_height - 16), 0,
\r
332 g_screen_width * 16 * 2);
\r
336 static void vidResetMode(void)
\r
339 if (PicoOpt&0x10) {
\r
340 gp2x_video_changemode(8);
\r
341 } else if (currentConfig.EmuOpt&0x80) {
\r
342 gp2x_video_changemode(16);
\r
343 #ifdef USE_320_SCREEN
\r
344 PicoDrawSetColorFormat(1);
\r
345 PicoScanBegin = EmuScanBegin16;
\r
347 PicoDrawSetColorFormat(-1);
\r
348 PicoScanEnd = EmuScanEnd16;
\r
351 gp2x_video_changemode(8);
\r
352 PicoDrawSetColorFormat(2);
\r
353 PicoScanBegin = EmuScanBegin8;
\r
355 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
356 // setup pal for 8-bit modes
\r
357 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
358 localPal[0xd0] = 0x00c00000;
\r
359 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
360 localPal[0xf0] = 0x00ffffff;
\r
361 gp2x_video_setpalette(localPal, 0x100);
\r
362 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
365 Pico.m.dirtyPal = 1;
\r
367 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
368 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
369 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
371 #ifdef USE_320_SCREEN
\r
372 PicoDrawSetColorFormat(1);
\r
373 PicoScanBegin = EmuScanBegin16;
\r
375 PicoDrawSetColorFormat(-1);
\r
376 PicoScanEnd = EmuScanEnd16;
\r
382 static void update_volume(int has_changed, int is_up)
\r
384 static int prev_frame = 0, wait_frames = 0;
\r
385 int vol = currentConfig.volume;
\r
389 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
\r
393 if (vol < 99) vol++;
\r
395 if (vol > 0) vol--;
\r
398 sndout_oss_setvol(vol, vol);
\r
399 currentConfig.volume = vol;
\r
401 sprintf(noticeMsg, "VOL: %02i", vol);
\r
402 gettimeofday(¬iceMsgTime, 0);
\r
403 prev_frame = Pico.m.frame_count;
\r
406 // set the right mixer func
\r
407 if (!(PicoOpt&8)) return; // just use defaults for mono
\r
409 PsndMix_32_to_16l = mix_32_to_16l_stereo;
\r
411 mix_32_to_16l_level = 5 - vol;
\r
412 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
\r
417 static void updateSound(int len)
\r
419 if (PicoOpt&8) len<<=1;
\r
421 /* avoid writing audio when lagging behind to prevent audio lag */
\r
422 if (PicoSkipFrame != 2)
\r
423 sndout_oss_write(PsndOut, len<<1);
\r
427 static void SkipFrame(int do_audio)
\r
429 PicoSkipFrame=do_audio ? 1 : 2;
\r
435 void pemu_forced_frame(int opts)
\r
437 int po_old = PicoOpt;
\r
438 int eo_old = currentConfig.EmuOpt;
\r
441 PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
\r
442 currentConfig.EmuOpt |= 0x80;
\r
445 #ifdef USE_320_SCREEN
\r
446 PicoDrawSetColorFormat(1);
\r
447 PicoScanBegin = EmuScanBegin16;
\r
449 PicoDrawSetColorFormat(-1);
\r
450 PicoScanEnd = EmuScanEnd16;
\r
452 Pico.m.dirtyPal = 1;
\r
453 PicoFrameDrawOnly();
\r
456 if (!(Pico.video.reg[12]&1)) {
\r
457 vidCpyM2 = vidCpyM2_32col;
\r
459 } else vidCpyM2 = vidCpyM2_40col;
\r
461 vidCpyM2((unsigned char *)g_screen_ptr+g_screen_width*8, PicoDraw2FB+328*8);
\r
462 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
463 gp2x_video_setpalette(localPal, 0x40);
\r
466 currentConfig.EmuOpt = eo_old;
\r
469 void plat_debug_cat(char *str)
\r
473 static void simpleWait(int thissec, int lim_time)
\r
475 struct timeval tval;
\r
477 spend_cycles(1024);
\r
478 gettimeofday(&tval, 0);
\r
479 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
481 while (tval.tv_usec < lim_time)
\r
483 spend_cycles(1024);
\r
484 gettimeofday(&tval, 0);
\r
485 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
489 void pemu_sound_start(void)
\r
491 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
\r
492 int target_fps = Pico.m.pal ? 50 : 60;
\r
496 if (currentConfig.EmuOpt & 4)
\r
498 int snd_excess_add;
\r
499 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old)
\r
500 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
502 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
503 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
504 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
505 sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);
\r
506 sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
\r
507 PicoWriteSound = updateSound;
\r
508 update_volume(0, 0);
\r
509 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
510 PsndOut = sndBuffer;
\r
511 PsndRate_old = PsndRate;
\r
512 PicoOpt_old = PicoOpt;
\r
513 pal_old = Pico.m.pal;
\r
517 void pemu_sound_stop(void)
\r
521 void pemu_sound_wait(void)
\r
523 // don't need to do anything, writes will block by themselves
\r
526 void pemu_loop(void)
\r
528 char fpsbuff[24]; // fps count c string
\r
529 struct timeval tval; // timing
\r
530 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
\r
531 int frames_done, frames_shown, thissec; // actual frames
\r
532 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
\r
535 printf("entered emu_Loop()\n");
\r
539 // make sure we are in correct mode
\r
541 Pico.m.dirtyPal = 1;
\r
542 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
544 // pal/ntsc might have changed, reset related stuff
\r
545 target_fps = Pico.m.pal ? 50 : 60;
\r
546 target_frametime = 1000000/target_fps;
\r
549 pemu_sound_start();
\r
551 // prepare CD buffer
\r
552 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
\r
554 // calc vsync offset to sync timing code with vsync
\r
555 if (currentConfig.EmuOpt&0x2000) {
\r
556 gettimeofday(&tval, 0);
\r
557 //gp2x_video_wait_vsync();
\r
558 gettimeofday(&tval, 0);
\r
559 vsync_offset = tval.tv_usec;
\r
560 while (vsync_offset >= target_frametime)
\r
561 vsync_offset -= target_frametime;
\r
562 if (!vsync_offset) vsync_offset++;
\r
563 printf("vsync_offset: %i\n", vsync_offset);
\r
567 frames_done = frames_shown = thissec =
\r
568 pframes_done = pframes_shown = pthissec = 0;
\r
571 while (engineState == PGS_Running)
\r
575 gettimeofday(&tval, 0);
\r
576 if (reset_timing) {
\r
578 pthissec = tval.tv_sec;
\r
579 pframes_shown = pframes_done = tval.tv_usec/target_frametime;
\r
582 // show notice message?
\r
583 if (noticeMsgTime.tv_sec)
\r
585 static int noticeMsgSum;
\r
586 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
587 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
591 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
592 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
593 notice = noticeMsg;
\r
597 // check for mode changes
\r
598 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
599 if (modes != oldmodes)
\r
601 int scalex = g_screen_width;
\r
602 osd_fps_x = OSD_FPS_X;
\r
604 vidCpyM2 = vidCpyM2_40col;
\r
606 if (PicoOpt & 0x100) {
\r
607 vidCpyM2 = vidCpyM2_32col_nobord;
\r
609 osd_fps_x = OSD_FPS_X - 64;
\r
611 vidCpyM2 = vidCpyM2_32col;
\r
614 //if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
615 // gp2x_video_RGB_setscaling(8, scalex, 224);
\r
616 // else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
622 if (thissec != tval.tv_sec)
\r
625 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
626 if (++bench == 4) {
\r
628 bench_fps_s = bench_fps / 4;
\r
629 bf[bfp++ & 3] = bench_fps / 4;
\r
632 bench_fps += frames_shown;
\r
633 sprintf(fpsbuff, "%3i/%3i/%3i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
634 printf("%s\n", fpsbuff);
\r
636 if (currentConfig.EmuOpt & 2) {
\r
637 sprintf(fpsbuff, "%3i/%3i", frames_shown, frames_done);
\r
638 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
641 frames_shown = frames_done = 0;
\r
642 thissec = tval.tv_sec;
\r
645 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
648 if (pthissec != tval.tv_sec)
\r
650 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
651 pframes_done = pframes_shown = 0;
\r
653 // it is quite common for this implementation to leave 1 fame unfinished
\r
654 // when second changes, but we don't want buffer to starve.
\r
655 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
\r
656 emu_update_input();
\r
657 SkipFrame(1); pframes_done++;
\r
660 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
\r
661 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
\r
662 if (pframes_shown > pframes_done) pframes_shown = pframes_done;
\r
664 pthissec = tval.tv_sec;
\r
667 lim_time = (pframes_done+1) * target_frametime + vsync_offset;
\r
668 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
670 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
671 emu_update_input();
\r
672 SkipFrame(1); pframes_done++; frames_done++;
\r
673 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
\r
674 gettimeofday(&tval, 0);
\r
675 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
676 if (tval.tv_usec < lim_time) { // we are too fast
\r
677 simpleWait(pthissec, lim_time);
\r
680 lim_time += target_frametime;
\r
683 else if (tval.tv_usec > lim_time) // auto frameskip
\r
685 // no time left for this frame - skip
\r
686 if (tval.tv_usec - lim_time >= 300000) {
\r
687 /* something caused a slowdown for us (disk access? cache flush?)
\r
688 * try to recover by resetting timing... */
\r
692 emu_update_input();
\r
693 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
\r
697 emu_update_input();
\r
701 gettimeofday(&tval, 0);
\r
702 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
704 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
\r
707 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
709 // sleep or vsync if we are still too fast
\r
710 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
711 if (!reset_timing && tval.tv_usec < lim_time)
\r
714 if (vsync_offset) {
\r
715 if (lim_time - tval.tv_usec > target_frametime/2)
\r
716 simpleWait(pthissec, lim_time - target_frametime/4);
\r
717 // gp2x_video_wait_vsync();
\r
719 simpleWait(pthissec, lim_time);
\r
724 blit(fpsbuff, notice);
\r
726 pframes_done++; pframes_shown++;
\r
727 frames_done++; frames_shown++;
\r
730 emu_set_fastforward(0);
\r
732 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
\r
735 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
736 /* FIXME: plat_status_msg_busy_first */
\r
737 emu_state_cb("Writing SRAM/BRAM..");
\r
738 emu_save_load_game(0, 1);
\r
742 // if in 8bit mode, generate 16bit image for menu background
\r
743 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
744 pemu_forced_frame(POPT_EN_SOFTSCALE);
\r