Ecco splash sound fix?
[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
fcf94fcc 722void emu_platformDebugCat(char *str)\r
723{\r
724 // nothing\r
725}\r
726\r
cc68a136 727static void simpleWait(int thissec, int lim_time)\r
728{\r
729 struct timeval tval;\r
730\r
731 spend_cycles(1024);\r
732 gettimeofday(&tval, 0);\r
da310283 733 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
cc68a136 734\r
da310283 735 while (tval.tv_usec < lim_time)\r
cc68a136 736 {\r
737 spend_cycles(1024);\r
738 gettimeofday(&tval, 0);\r
da310283 739 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
cc68a136 740 }\r
741}\r
742\r
743\r
da310283 744#if 0\r
745static void tga_dump(void)\r
746{\r
747#define BYTE unsigned char\r
748#define WORD unsigned short\r
749 struct\r
750 {\r
751 BYTE IDLength; /* 00h Size of Image ID field */\r
752 BYTE ColorMapType; /* 01h Color map type */\r
753 BYTE ImageType; /* 02h Image type code */\r
754 WORD CMapStart; /* 03h Color map origin */\r
755 WORD CMapLength; /* 05h Color map length */\r
756 BYTE CMapDepth; /* 07h Depth of color map entries */\r
757 WORD XOffset; /* 08h X origin of image */\r
758 WORD YOffset; /* 0Ah Y origin of image */\r
759 WORD Width; /* 0Ch Width of image */\r
760 WORD Height; /* 0Eh Height of image */\r
761 BYTE PixelDepth; /* 10h Image pixel size */\r
762 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
763 } __attribute__((packed)) TGAHEAD;\r
764 static unsigned short oldscr[320*240];\r
765 FILE *f; char name[128]; int i;\r
766\r
767 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
768 TGAHEAD.ImageType = 2;\r
769 TGAHEAD.Width = 320;\r
770 TGAHEAD.Height = 240;\r
771 TGAHEAD.PixelDepth = 16;\r
772 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
773\r
774#define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
775\r
776 for (i = 0; i < 320*240; i++)\r
777 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
778 if (i < 320*240)\r
779 {\r
780 for (i = 0; i < 320*240; i++)\r
781 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
782 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
783 f = fopen(name, "wb");\r
784 if (!f) { printf("!f\n"); exit(1); }\r
785 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
786 fwrite(oldscr, 1, 320*240*2, f);\r
787 fclose(f);\r
788 }\r
789}\r
790#endif\r
791\r
792\r
cc68a136 793void emu_Loop(void)\r
794{\r
795 static int gp2x_old_clock = 200;\r
fa283c9a 796 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;\r
cc68a136 797 char fpsbuff[24]; // fps count c string\r
798 struct timeval tval; // timing\r
da310283 799 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
800 int frames_done, frames_shown, thissec; // actual frames\r
801 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
cc68a136 802 char *notice = 0;\r
803\r
804 printf("entered emu_Loop()\n");\r
805\r
806 if (gp2x_old_clock != currentConfig.CPUclock) {\r
807 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
808 set_FCLK(currentConfig.CPUclock);\r
809 gp2x_old_clock = currentConfig.CPUclock;\r
810 printf(" done\n");\r
811 }\r
812\r
82bc9cdd 813 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
814 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
cc68a136 815 gp2x_old_gamma = currentConfig.gamma;\r
82bc9cdd 816 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
cc68a136 817 }\r
818\r
aae35e84 819 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
820 if (currentConfig.EmuOpt&0x2000)\r
821 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
822 else unset_LCD_custom_rate();\r
823 }\r
824\r
825 EmuOpt_old = currentConfig.EmuOpt;\r
cc68a136 826 fpsbuff[0] = 0;\r
827\r
828 // make sure we are in correct mode\r
829 vidResetMode();\r
ea8c405f 830 scaling_update();\r
e11c5548 831 Pico.m.dirtyPal = 1;\r
cc68a136 832 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
bdec53c9 833 emu_findKeyBindCombos();\r
cc68a136 834\r
835 // pal/ntsc might have changed, reset related stuff\r
836 target_fps = Pico.m.pal ? 50 : 60;\r
837 target_frametime = 1000000/target_fps;\r
838 reset_timing = 1;\r
839\r
840 // prepare sound stuff\r
4a32f01f 841 if (currentConfig.EmuOpt & 4)\r
842 {\r
7a93adeb 843 int snd_excess_add;\r
1dceadae 844 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
845 ((PicoOpt&0x200) && crashed_940)) {\r
9d917eea 846 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
cc68a136 847 }\r
cc68a136 848 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
1dceadae 849 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
850 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
cc68a136 851 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
852 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
853 PicoWriteSound = updateSound;\r
4a32f01f 854 update_volume(0, 0);\r
7a93adeb 855 memset(sndBuffer, 0, sizeof(sndBuffer));\r
856 PsndOut = sndBuffer;\r
cc68a136 857 PsndRate_old = PsndRate;\r
cc68a136 858 PicoOpt_old = PicoOpt;\r
859 pal_old = Pico.m.pal;\r
860 } else {\r
4a32f01f 861 PsndOut = NULL;\r
cc68a136 862 }\r
863\r
0a051f55 864 // prepare CD buffer\r
602133e1 865 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
0a051f55 866\r
aae35e84 867 // calc vsync offset to sync timing code with vsync\r
868 if (currentConfig.EmuOpt&0x2000) {\r
869 gettimeofday(&tval, 0);\r
870 gp2x_video_wait_vsync();\r
871 gettimeofday(&tval, 0);\r
872 vsync_offset = tval.tv_usec;\r
873 while (vsync_offset >= target_frametime)\r
874 vsync_offset -= target_frametime;\r
875 if (!vsync_offset) vsync_offset++;\r
876 printf("vsync_offset: %i\n", vsync_offset);\r
877 } else\r
878 vsync_offset = 0;\r
879\r
da310283 880 frames_done = frames_shown = thissec =\r
881 pframes_done = pframes_shown = pthissec = 0;\r
882\r
883 // loop\r
cc68a136 884 while (engineState == PGS_Running)\r
885 {\r
886 int modes;\r
887\r
888 gettimeofday(&tval, 0);\r
4a32f01f 889 if (reset_timing) {\r
cc68a136 890 reset_timing = 0;\r
da310283 891 pthissec = tval.tv_sec;\r
892 pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
cc68a136 893 }\r
894\r
895 // show notice message?\r
4a32f01f 896 if (noticeMsgTime.tv_sec)\r
897 {\r
cc68a136 898 static int noticeMsgSum;\r
899 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
900 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
901 clearArea(0);\r
902 notice = 0;\r
903 } else {\r
904 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
905 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
906 notice = noticeMsg;\r
907 }\r
908 }\r
909\r
910 // check for mode changes\r
911 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
4a32f01f 912 if (modes != oldmodes)\r
913 {\r
cc68a136 914 int scalex = 320;\r
915 osd_fps_x = OSD_FPS_X;\r
916 if (modes & 4) {\r
917 vidCpyM2 = vidCpyM2_40col;\r
918 } else {\r
919 if (PicoOpt & 0x100) {\r
920 vidCpyM2 = vidCpyM2_32col_nobord;\r
921 scalex = 256;\r
922 osd_fps_x = OSD_FPS_X - 64;\r
923 } else {\r
924 vidCpyM2 = vidCpyM2_32col;\r
925 }\r
926 }\r
2433f409 927 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
928 gp2x_video_RGB_setscaling(8, scalex, 224);\r
929 else gp2x_video_RGB_setscaling(0, scalex, 240);\r
cc68a136 930 oldmodes = modes;\r
931 clearArea(1);\r
932 }\r
933\r
934 // second changed?\r
4a32f01f 935 if (thissec != tval.tv_sec)\r
936 {\r
cc68a136 937#ifdef BENCHMARK\r
938 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
4a32f01f 939 if (++bench == 10) {\r
cc68a136 940 bench = 0;\r
941 bench_fps_s = bench_fps;\r
942 bf[bfp++ & 3] = bench_fps;\r
943 bench_fps = 0;\r
944 }\r
945 bench_fps += frames_shown;\r
946 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
947#else\r
1b13dae0 948 if (currentConfig.EmuOpt & 2) {\r
cc68a136 949 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
1b13dae0 950 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
951 }\r
cc68a136 952#endif\r
da310283 953 frames_shown = frames_done = 0;\r
cc68a136 954 thissec = tval.tv_sec;\r
da310283 955 }\r
956#ifdef PFRAMES\r
957 sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
958#endif\r
cc68a136 959\r
da310283 960 if (pthissec != tval.tv_sec)\r
961 {\r
4a32f01f 962 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
da310283 963 pframes_done = pframes_shown = 0;\r
cc68a136 964 } else {\r
965 // it is quite common for this implementation to leave 1 fame unfinished\r
966 // when second changes, but we don't want buffer to starve.\r
da310283 967 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
cc68a136 968 updateKeys();\r
da310283 969 SkipFrame(1); pframes_done++;\r
cc68a136 970 }\r
971\r
da310283 972 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;\r
973 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
974 if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
cc68a136 975 }\r
da310283 976 pthissec = tval.tv_sec;\r
cc68a136 977 }\r
aae35e84 978\r
da310283 979 lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
980 if (currentConfig.Frameskip >= 0) // frameskip enabled\r
981 {\r
cc68a136 982 for(i = 0; i < currentConfig.Frameskip; i++) {\r
983 updateKeys();\r
da310283 984 SkipFrame(1); pframes_done++; frames_done++;\r
4a32f01f 985 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
cc68a136 986 gettimeofday(&tval, 0);\r
da310283 987 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
988 if (tval.tv_usec < lim_time) { // we are too fast\r
989 simpleWait(pthissec, lim_time);\r
cc68a136 990 }\r
991 }\r
992 lim_time += target_frametime;\r
993 }\r
da310283 994 }\r
995 else if (tval.tv_usec > lim_time) // auto frameskip\r
996 {\r
cc68a136 997 // no time left for this frame - skip\r
ea8c405f 998 if (tval.tv_usec - lim_time >= 300000) {\r
4ff2d527 999 /* something caused a slowdown for us (disk access? cache flush?)\r
1000 * try to recover by resetting timing... */\r
1001 reset_timing = 1;\r
1002 continue;\r
1003 }\r
cc68a136 1004 updateKeys();\r
da310283 1005 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
cc68a136 1006 continue;\r
1007 }\r
1008\r
1009 updateKeys();\r
1010 PicoFrame();\r
1011\r
1012 // check time\r
1013 gettimeofday(&tval, 0);\r
da310283 1014 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
cc68a136 1015\r
ea8c405f 1016 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
4ff2d527 1017 reset_timing = 1;\r
1018 else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
cc68a136 1019 {\r
aae35e84 1020 // sleep or vsync if we are still too fast\r
cc68a136 1021 // usleep sleeps for ~20ms minimum, so it is not a solution here\r
4a32f01f 1022 if (!reset_timing && tval.tv_usec < lim_time)\r
cc68a136 1023 {\r
1024 // we are too fast\r
aae35e84 1025 if (vsync_offset) {\r
1026 if (lim_time - tval.tv_usec > target_frametime/2)\r
da310283 1027 simpleWait(pthissec, lim_time - target_frametime/4);\r
aae35e84 1028 gp2x_video_wait_vsync();\r
1029 } else {\r
da310283 1030 simpleWait(pthissec, lim_time);\r
aae35e84 1031 }\r
cc68a136 1032 }\r
1033 }\r
1034\r
1035 blit(fpsbuff, notice);\r
1036\r
da310283 1037 pframes_done++; pframes_shown++;\r
1038 frames_done++; frames_shown++;\r
cc68a136 1039 }\r
1040\r
c060a9ab 1041 emu_changeFastForward(0);\r
0a051f55 1042\r
602133e1 1043 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
0a051f55 1044\r
cc68a136 1045 // save SRAM\r
1046 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
6cadc2da 1047 emu_state_cb("Writing SRAM/BRAM..");\r
cc68a136 1048 emu_SaveLoadGame(0, 1);\r
1049 SRam.changed = 0;\r
1050 }\r
e11c5548 1051\r
a12e0116 1052 // if in 8bit mode, generate 16bit image for menu background\r
1053 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
0fc0e241 1054 emu_forcedFrame(POPT_EN_SOFTSCALE);\r
cc68a136 1055}\r
1056\r
1057\r
1058void emu_ResetGame(void)\r
1059{\r
1cb1584b 1060 PicoReset();\r
cc68a136 1061 reset_timing = 1;\r
1062}\r
1063\r