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