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