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