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