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