| 1 | #include <windows.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include <sys/stat.h> // mkdir |
| 5 | #include <sys/types.h> |
| 6 | |
| 7 | #include "kgsdk/Framework.h" |
| 8 | #include "kgsdk/Framework2D.h" |
| 9 | #include "kgsdk/FrameworkAudio.h" |
| 10 | #include "../common/emu.h" |
| 11 | #include "../common/lprintf.h" |
| 12 | #include "../common/arm_utils.h" |
| 13 | #include "emu.h" |
| 14 | #include "menu.h" |
| 15 | #include "giz.h" |
| 16 | #include "asm_utils.h" |
| 17 | |
| 18 | #include <Pico/PicoInt.h> |
| 19 | |
| 20 | #ifdef BENCHMARK |
| 21 | #define OSD_FPS_X 220 |
| 22 | #else |
| 23 | #define OSD_FPS_X 260 |
| 24 | #endif |
| 25 | |
| 26 | // main 300K gfx-related buffer. Used by menu and renderers. |
| 27 | unsigned char gfx_buffer[321*240*2*2]; |
| 28 | char romFileName[MAX_PATH]; |
| 29 | int engineState; |
| 30 | |
| 31 | unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) ) |
| 32 | int reset_timing = 0; |
| 33 | |
| 34 | static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos |
| 35 | static DWORD noticeMsgTime = 0; |
| 36 | static short *snd_cbuff = NULL; |
| 37 | static int snd_cbuf_samples = 0, snd_all_samples = 0; |
| 38 | |
| 39 | |
| 40 | static void blit(const char *fps, const char *notice); |
| 41 | static void clearArea(int full); |
| 42 | |
| 43 | void emu_noticeMsgUpdated(void) |
| 44 | { |
| 45 | noticeMsgTime = GetTickCount(); |
| 46 | } |
| 47 | |
| 48 | void emu_getMainDir(char *dst, int len) |
| 49 | { |
| 50 | if (len > 0) *dst = 0; |
| 51 | } |
| 52 | |
| 53 | static void emu_msg_cb(const char *msg) |
| 54 | { |
| 55 | if (giz_screen != NULL) Framework2D_UnlockBuffer(); |
| 56 | giz_screen = Framework2D_LockBuffer(1); |
| 57 | |
| 58 | memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
| 59 | emu_textOut16(4, 232, msg); |
| 60 | noticeMsgTime = GetTickCount() - 2000; |
| 61 | |
| 62 | /* assumption: emu_msg_cb gets called only when something slow is about to happen */ |
| 63 | reset_timing = 1; |
| 64 | |
| 65 | Framework2D_UnlockBuffer(); |
| 66 | giz_screen = Framework2D_LockBuffer((currentConfig.EmuOpt&0x8000) ? 0 : 1); |
| 67 | } |
| 68 | |
| 69 | void emu_stateCb(const char *str) |
| 70 | { |
| 71 | if (giz_screen != NULL) Framework2D_UnlockBuffer(); |
| 72 | giz_screen = Framework2D_LockBuffer(1); |
| 73 | |
| 74 | clearArea(0); |
| 75 | blit("", str); |
| 76 | |
| 77 | Framework2D_UnlockBuffer(); |
| 78 | giz_screen = NULL; |
| 79 | |
| 80 | Sleep(0); /* yield the CPU, the system may need it */ |
| 81 | } |
| 82 | |
| 83 | static void emu_msg_tray_open(void) |
| 84 | { |
| 85 | strcpy(noticeMsg, "CD tray opened"); |
| 86 | noticeMsgTime = GetTickCount(); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void emu_Init(void) |
| 91 | { |
| 92 | // make dirs for saves, cfgs, etc. |
| 93 | mkdir("mds", 0777); |
| 94 | mkdir("srm", 0777); |
| 95 | mkdir("brm", 0777); |
| 96 | mkdir("cfg", 0777); |
| 97 | |
| 98 | PicoInit(); |
| 99 | PicoMessage = emu_msg_cb; |
| 100 | PicoMCDopenTray = emu_msg_tray_open; |
| 101 | PicoMCDcloseTray = menu_loop_tray; |
| 102 | } |
| 103 | |
| 104 | void emu_Deinit(void) |
| 105 | { |
| 106 | // save SRAM |
| 107 | if ((currentConfig.EmuOpt & 1) && SRam.changed) { |
| 108 | emu_SaveLoadGame(0, 1); |
| 109 | SRam.changed = 0; |
| 110 | } |
| 111 | |
| 112 | if (!(currentConfig.EmuOpt & 0x20)) { |
| 113 | FILE *f = fopen(PicoConfigFile, "r+b"); |
| 114 | if (!f) emu_WriteConfig(0); |
| 115 | else { |
| 116 | // if we already have config, reload it, except last ROM |
| 117 | fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET); |
| 118 | fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f); |
| 119 | fseek(f, 0, SEEK_SET); |
| 120 | fwrite(¤tConfig, 1, sizeof(currentConfig), f); |
| 121 | fflush(f); |
| 122 | fclose(f); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | PicoExit(); |
| 127 | } |
| 128 | |
| 129 | void emu_setDefaultConfig(void) |
| 130 | { |
| 131 | memset(¤tConfig, 0, sizeof(currentConfig)); |
| 132 | currentConfig.lastRomFile[0] = 0; |
| 133 | currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend |
| 134 | currentConfig.PicoOpt = 0x07 | 0xc00; // | cd_pcm, cd_cdda |
| 135 | currentConfig.PsndRate = 22050; |
| 136 | currentConfig.PicoRegion = 0; // auto |
| 137 | currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP |
| 138 | currentConfig.Frameskip = -1; // auto |
| 139 | currentConfig.volume = 50; |
| 140 | currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU |
| 141 | currentConfig.KeyBinds[ 3] = 1<<1; |
| 142 | currentConfig.KeyBinds[ 0] = 1<<2; |
| 143 | currentConfig.KeyBinds[ 1] = 1<<3; |
| 144 | currentConfig.KeyBinds[ 5] = 1<<4; |
| 145 | currentConfig.KeyBinds[ 6] = 1<<5; |
| 146 | currentConfig.KeyBinds[ 7] = 1<<6; |
| 147 | currentConfig.KeyBinds[ 4] = 1<<7; |
| 148 | currentConfig.KeyBinds[13] = 1<<26; // switch rend |
| 149 | currentConfig.KeyBinds[ 8] = 1<<27; // save state |
| 150 | currentConfig.KeyBinds[ 9] = 1<<28; // load state |
| 151 | currentConfig.KeyBinds[12] = 1<<29; // vol up |
| 152 | currentConfig.KeyBinds[11] = 1<<30; // vol down |
| 153 | currentConfig.PicoCDBuffers = 0; |
| 154 | currentConfig.scaling = 0; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static int EmuScan16(unsigned int num, void *sdata) |
| 159 | { |
| 160 | if (!(Pico.video.reg[1]&8)) num += 8; |
| 161 | DrawLineDest = (unsigned short *) giz_screen + 321*(num+1); |
| 162 | |
| 163 | if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1)) |
| 164 | return 1; // skip next line |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int EmuScan8(unsigned int num, void *sdata) |
| 170 | { |
| 171 | // draw like the fast renderer |
| 172 | if (!(Pico.video.reg[1]&8)) num += 8; |
| 173 | HighCol = gfx_buffer + 328*(num+1); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void osd_text(int x, int y, const char *text) |
| 179 | { |
| 180 | int len = strlen(text) * 8 / 2; |
| 181 | int *p, h; |
| 182 | for (h = 0; h < 8; h++) { |
| 183 | p = (int *) ((unsigned short *) giz_screen+x+321*(y+h)); |
| 184 | p = (int *) ((int)p & ~3); // align |
| 185 | memset32(p, 0, len); |
| 186 | } |
| 187 | emu_textOut16(x, y, text); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | void log1(void *p1, void *p2) |
| 192 | { |
| 193 | lprintf("%p %p %p\n", p1, p2, DrawLineDest); |
| 194 | } |
| 195 | */ |
| 196 | |
| 197 | static void cd_leds(void) |
| 198 | { |
| 199 | static int old_reg = 0; |
| 200 | unsigned int col_g, col_r, *p; |
| 201 | |
| 202 | if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change |
| 203 | old_reg = Pico_mcd->s68k_regs[0]; |
| 204 | |
| 205 | p = (unsigned int *)((short *)giz_screen + 321*2+4+2); |
| 206 | col_g = (old_reg & 2) ? 0x06000600 : 0; |
| 207 | col_r = (old_reg & 1) ? 0xc000c000 : 0; |
| 208 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1; |
| 209 | *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2; |
| 210 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | static short localPal[0x100]; |
| 215 | |
| 216 | static void blit(const char *fps, const char *notice) |
| 217 | { |
| 218 | int emu_opt = currentConfig.EmuOpt; |
| 219 | |
| 220 | if (PicoOpt&0x10) |
| 221 | { |
| 222 | int lines_flags = 224; |
| 223 | // 8bit fast renderer |
| 224 | if (Pico.m.dirtyPal) { |
| 225 | Pico.m.dirtyPal = 0; |
| 226 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
| 227 | } |
| 228 | if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000; |
| 229 | if (currentConfig.EmuOpt&0x4000) |
| 230 | lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000; |
| 231 | vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags); |
| 232 | } |
| 233 | else if (!(emu_opt&0x80)) |
| 234 | { |
| 235 | int lines_flags; |
| 236 | // 8bit accurate renderer |
| 237 | if (Pico.m.dirtyPal) { |
| 238 | Pico.m.dirtyPal = 0; |
| 239 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
| 240 | if (Pico.video.reg[0xC]&8) { // shadow/hilight mode |
| 241 | //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40); |
| 242 | //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO? |
| 243 | blockcpy(localPal+0xc0, localPal+0x40, 0x40*2); |
| 244 | localPal[0xc0] = 0x0600; |
| 245 | localPal[0xd0] = 0xc000; |
| 246 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
| 247 | localPal[0xf0] = 0xffff; |
| 248 | } |
| 249 | /* no support |
| 250 | else if (rendstatus & 0x20) { // mid-frame palette changes |
| 251 | vidConvCpyRGB565(localPal+0x40, HighPal, 0x40); |
| 252 | vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40); |
| 253 | } */ |
| 254 | } |
| 255 | lines_flags = (Pico.video.reg[1]&8) ? 240 : 224; |
| 256 | if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000; |
| 257 | if (currentConfig.EmuOpt&0x4000) |
| 258 | lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000; |
| 259 | vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags); |
| 260 | } |
| 261 | |
| 262 | if (notice || (emu_opt & 2)) { |
| 263 | int h = 232; |
| 264 | if (notice) osd_text(4, h, notice); |
| 265 | if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps); |
| 266 | } |
| 267 | |
| 268 | if ((emu_opt & 0x400) && (PicoMCD & 1)) |
| 269 | cd_leds(); |
| 270 | } |
| 271 | |
| 272 | // clears whole screen or just the notice area (in all buffers) |
| 273 | static void clearArea(int full) |
| 274 | { |
| 275 | if (giz_screen == NULL) |
| 276 | giz_screen = Framework2D_LockBuffer(1); |
| 277 | if (full) memset32(giz_screen, 0, 320*240*2/4); |
| 278 | else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
| 279 | |
| 280 | if (currentConfig.EmuOpt&0x8000) { |
| 281 | Framework2D_UnlockBuffer(); |
| 282 | giz_screen = Framework2D_LockBuffer(0); |
| 283 | if (full) memset32(giz_screen, 0, 320*240*2/4); |
| 284 | else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static void vidResetMode(void) |
| 289 | { |
| 290 | giz_screen = Framework2D_LockBuffer(1); |
| 291 | |
| 292 | if (PicoOpt&0x10) { |
| 293 | } else if (currentConfig.EmuOpt&0x80) { |
| 294 | PicoDrawSetColorFormat(1); |
| 295 | PicoScan = EmuScan16; |
| 296 | } else { |
| 297 | PicoDrawSetColorFormat(-1); |
| 298 | PicoScan = EmuScan8; |
| 299 | } |
| 300 | if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) { |
| 301 | // setup pal for 8-bit modes |
| 302 | localPal[0xc0] = 0x0600; |
| 303 | localPal[0xd0] = 0xc000; |
| 304 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
| 305 | localPal[0xf0] = 0xffff; |
| 306 | } |
| 307 | Pico.m.dirtyPal = 1; |
| 308 | |
| 309 | memset32(giz_screen, 0, 321*240*2/4); |
| 310 | if (currentConfig.EmuOpt&0x8000) { |
| 311 | Framework2D_UnlockBuffer(); |
| 312 | giz_screen = Framework2D_LockBuffer(0); |
| 313 | memset32(giz_screen, 0, 321*240*2/4); |
| 314 | } |
| 315 | Framework2D_UnlockBuffer(); |
| 316 | giz_screen = NULL; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | #include <stdarg.h> |
| 321 | static void stdbg(const char *fmt, ...) |
| 322 | { |
| 323 | static int cnt = 0; |
| 324 | va_list vl; |
| 325 | |
| 326 | sprintf(noticeMsg, "%x ", cnt++); |
| 327 | va_start(vl, fmt); |
| 328 | vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl); |
| 329 | va_end(vl); |
| 330 | |
| 331 | noticeMsgTime = GetTickCount(); |
| 332 | } |
| 333 | */ |
| 334 | |
| 335 | static void updateSound(int len) |
| 336 | { |
| 337 | if (PicoOpt&8) len<<=1; |
| 338 | |
| 339 | snd_all_samples += len; |
| 340 | PsndOut += len; |
| 341 | if (PsndOut - snd_cbuff >= snd_cbuf_samples) |
| 342 | { |
| 343 | //if (PsndOut - snd_cbuff != snd_cbuf_samples) |
| 344 | // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples); |
| 345 | PsndOut = snd_cbuff; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | static void SkipFrame(void) |
| 351 | { |
| 352 | PicoSkipFrame=1; |
| 353 | PicoFrame(); |
| 354 | PicoSkipFrame=0; |
| 355 | } |
| 356 | |
| 357 | /* forced frame to front buffer */ |
| 358 | void emu_forcedFrame(void) |
| 359 | { |
| 360 | int po_old = PicoOpt; |
| 361 | int eo_old = currentConfig.EmuOpt; |
| 362 | |
| 363 | PicoOpt &= ~0x0010; |
| 364 | PicoOpt |= 0x4080; // soft_scale | acc_sprites |
| 365 | currentConfig.EmuOpt |= 0x80; |
| 366 | |
| 367 | if (giz_screen == NULL) |
| 368 | giz_screen = Framework2D_LockBuffer(1); |
| 369 | |
| 370 | PicoDrawSetColorFormat(1); |
| 371 | PicoScan = EmuScan16; |
| 372 | PicoScan((unsigned) -1, NULL); |
| 373 | Pico.m.dirtyPal = 1; |
| 374 | PicoFrameDrawOnly(); |
| 375 | |
| 376 | Framework2D_UnlockBuffer(); |
| 377 | giz_screen = NULL; |
| 378 | |
| 379 | PicoOpt = po_old; |
| 380 | currentConfig.EmuOpt = eo_old; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | static void RunEvents(unsigned int which) |
| 385 | { |
| 386 | if (which & 0x1800) // save or load (but not both) |
| 387 | { |
| 388 | int do_it = 1; |
| 389 | |
| 390 | if (PsndOut != NULL) |
| 391 | FrameworkAudio_SetPause(1); |
| 392 | if (giz_screen == NULL) |
| 393 | giz_screen = Framework2D_LockBuffer(1); |
| 394 | if ( emu_checkSaveFile(state_slot) && |
| 395 | (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load |
| 396 | (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save |
| 397 | { |
| 398 | int keys; |
| 399 | blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)"); |
| 400 | while( !((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) ) |
| 401 | Sleep(50); |
| 402 | if (keys & BTN_STOP) do_it = 0; |
| 403 | while( ((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) ) // wait for release |
| 404 | Sleep(50); |
| 405 | clearArea(0); |
| 406 | } |
| 407 | |
| 408 | if (do_it) |
| 409 | { |
| 410 | osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME"); |
| 411 | PicoStateProgressCB = emu_stateCb; |
| 412 | emu_SaveLoadGame((which & 0x1000) >> 12, 0); |
| 413 | PicoStateProgressCB = NULL; |
| 414 | Sleep(0); |
| 415 | } |
| 416 | |
| 417 | if (PsndOut != NULL) |
| 418 | FrameworkAudio_SetPause(0); |
| 419 | reset_timing = 1; |
| 420 | } |
| 421 | if (which & 0x0400) // switch renderer |
| 422 | { |
| 423 | if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; } |
| 424 | else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; } |
| 425 | |
| 426 | vidResetMode(); |
| 427 | |
| 428 | if (PicoOpt&0x10) { |
| 429 | strcpy(noticeMsg, " 8bit fast renderer"); |
| 430 | } else if (currentConfig.EmuOpt&0x80) { |
| 431 | strcpy(noticeMsg, "16bit accurate renderer"); |
| 432 | } else { |
| 433 | strcpy(noticeMsg, " 8bit accurate renderer"); |
| 434 | } |
| 435 | |
| 436 | noticeMsgTime = GetTickCount(); |
| 437 | } |
| 438 | if (which & 0x0300) |
| 439 | { |
| 440 | if(which&0x0200) { |
| 441 | state_slot -= 1; |
| 442 | if(state_slot < 0) state_slot = 9; |
| 443 | } else { |
| 444 | state_slot += 1; |
| 445 | if(state_slot > 9) state_slot = 0; |
| 446 | } |
| 447 | sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE"); |
| 448 | noticeMsgTime = GetTickCount(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static void updateKeys(void) |
| 453 | { |
| 454 | unsigned int keys, allActions[2] = { 0, 0 }, events; |
| 455 | static unsigned int prevEvents = 0; |
| 456 | int i; |
| 457 | |
| 458 | keys = Framework_PollGetButtons(); |
| 459 | if (keys & BTN_HOME) |
| 460 | engineState = PGS_Menu; |
| 461 | |
| 462 | keys &= CONFIGURABLE_KEYS; |
| 463 | |
| 464 | for (i = 0; i < 32; i++) |
| 465 | { |
| 466 | if (keys & (1 << i)) |
| 467 | { |
| 468 | int pl, acts = currentConfig.KeyBinds[i]; |
| 469 | if (!acts) continue; |
| 470 | pl = (acts >> 16) & 1; |
| 471 | if (combo_keys & (1 << i)) |
| 472 | { |
| 473 | int u = i+1, acts_c = acts & combo_acts; |
| 474 | // let's try to find the other one |
| 475 | if (acts_c) |
| 476 | for (; u < 32; u++) |
| 477 | if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) { |
| 478 | allActions[pl] |= acts_c; |
| 479 | keys &= ~((1 << i) | (1 << u)); |
| 480 | break; |
| 481 | } |
| 482 | // add non-combo actions if combo ones were not found |
| 483 | if (!acts_c || u == 32) |
| 484 | allActions[pl] |= acts & ~combo_acts; |
| 485 | } else { |
| 486 | allActions[pl] |= acts; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | PicoPad[0] = (unsigned short) allActions[0]; |
| 492 | PicoPad[1] = (unsigned short) allActions[1]; |
| 493 | |
| 494 | events = (allActions[0] | allActions[1]) >> 16; |
| 495 | |
| 496 | // volume is treated in special way and triggered every frame |
| 497 | if ((events & 0x6000) && PsndOut != NULL) |
| 498 | { |
| 499 | int vol = currentConfig.volume; |
| 500 | if (events & 0x2000) { |
| 501 | if (vol < 100) vol++; |
| 502 | } else { |
| 503 | if (vol > 0) vol--; |
| 504 | } |
| 505 | FrameworkAudio_SetVolume(vol, vol); |
| 506 | sprintf(noticeMsg, "VOL: %02i ", vol); |
| 507 | noticeMsgTime = GetTickCount(); |
| 508 | currentConfig.volume = vol; |
| 509 | } |
| 510 | |
| 511 | events &= ~prevEvents; |
| 512 | if (events) RunEvents(events); |
| 513 | if (movie_data) emu_updateMovie(); |
| 514 | |
| 515 | prevEvents = (allActions[0] | allActions[1]) >> 16; |
| 516 | } |
| 517 | |
| 518 | static void find_combos(void) |
| 519 | { |
| 520 | int act, u; |
| 521 | |
| 522 | // find out which keys and actions are combos |
| 523 | combo_keys = combo_acts = 0; |
| 524 | for (act = 0; act < 32; act++) |
| 525 | { |
| 526 | int keyc = 0, keyc2 = 0; |
| 527 | if (act == 16 || act == 17) continue; // player2 flag |
| 528 | if (act > 17) |
| 529 | { |
| 530 | for (u = 0; u < 32; u++) |
| 531 | if (currentConfig.KeyBinds[u] & (1 << act)) keyc++; |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | for (u = 0; u < 32; u++) |
| 536 | if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1 |
| 537 | (currentConfig.KeyBinds[u] & (1 << act))) keyc++; |
| 538 | for (u = 0; u < 32; u++) |
| 539 | if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2 |
| 540 | (currentConfig.KeyBinds[u] & (1 << act))) keyc2++; |
| 541 | if (keyc2 > keyc) keyc = keyc2; |
| 542 | } |
| 543 | if (keyc > 1) |
| 544 | { |
| 545 | // loop again and mark those keys and actions as combo |
| 546 | for (u = 0; u < 32; u++) |
| 547 | { |
| 548 | if (currentConfig.KeyBinds[u] & (1 << act)) { |
| 549 | combo_keys |= 1 << u; |
| 550 | combo_acts |= 1 << act; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | |
| 558 | static void simpleWait(DWORD until) |
| 559 | { |
| 560 | DWORD tval; |
| 561 | int diff; |
| 562 | |
| 563 | tval = GetTickCount(); |
| 564 | diff = (int)until - (int)tval; |
| 565 | if (diff >= 2) |
| 566 | Sleep(diff - 1); |
| 567 | |
| 568 | while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection |
| 569 | spend_cycles(1024*2); |
| 570 | } |
| 571 | |
| 572 | void emu_Loop(void) |
| 573 | { |
| 574 | static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0; |
| 575 | char fpsbuff[24]; // fps count c string |
| 576 | DWORD tval, tval_prev = 0, tval_thissec = 0; // timing |
| 577 | int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000; |
| 578 | int target_fps, target_frametime, lim_time, tval_diff, i; |
| 579 | char *notice = NULL; |
| 580 | |
| 581 | lprintf("entered emu_Loop()\n"); |
| 582 | |
| 583 | fpsbuff[0] = 0; |
| 584 | |
| 585 | // make sure we are in correct mode |
| 586 | vidResetMode(); |
| 587 | if (currentConfig.scaling) PicoOpt|=0x4000; |
| 588 | else PicoOpt&=~0x4000; |
| 589 | Pico.m.dirtyPal = 1; |
| 590 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc; |
| 591 | find_combos(); |
| 592 | |
| 593 | // pal/ntsc might have changed, reset related stuff |
| 594 | target_fps = Pico.m.pal ? 50 : 60; |
| 595 | target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1; |
| 596 | reset_timing = 1; |
| 597 | |
| 598 | // prepare CD buffer |
| 599 | if (PicoMCD & 1) PicoCDBufferInit(); |
| 600 | |
| 601 | // prepare sound stuff |
| 602 | PsndOut = NULL; |
| 603 | if (currentConfig.EmuOpt & 4) |
| 604 | { |
| 605 | int ret, snd_excess_add, stereo; |
| 606 | if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) { |
| 607 | PsndRerate(Pico.m.frame_count ? 1 : 0); |
| 608 | } |
| 609 | stereo=(PicoOpt&8)>>3; |
| 610 | snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps; |
| 611 | snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps; |
| 612 | lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", |
| 613 | PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal); |
| 614 | ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo); |
| 615 | if (ret != 0) { |
| 616 | lprintf("FrameworkAudio_Init() failed: %i\n", ret); |
| 617 | sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret); |
| 618 | noticeMsgTime = GetTickCount(); |
| 619 | currentConfig.EmuOpt &= ~4; |
| 620 | } else { |
| 621 | FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume); |
| 622 | PicoWriteSound = updateSound; |
| 623 | snd_cbuff = FrameworkAudio_56448Buffer(); |
| 624 | PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle |
| 625 | snd_all_samples = 0; |
| 626 | PsndRate_old = PsndRate; |
| 627 | PicoOpt_old = PicoOpt; |
| 628 | pal_old = Pico.m.pal; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // loop? |
| 633 | while (engineState == PGS_Running) |
| 634 | { |
| 635 | int modes; |
| 636 | |
| 637 | tval = GetTickCount(); |
| 638 | if (reset_timing || tval < tval_prev) { |
| 639 | //stdbg("timing reset"); |
| 640 | reset_timing = 0; |
| 641 | tval_thissec = tval; |
| 642 | frames_shown = frames_done = 0; |
| 643 | } |
| 644 | |
| 645 | // show notice message? |
| 646 | if (noticeMsgTime) { |
| 647 | static int noticeMsgSum; |
| 648 | if (tval - noticeMsgTime > 2000) { // > 2.0 sec |
| 649 | noticeMsgTime = 0; |
| 650 | clearArea(0); |
| 651 | notice = 0; |
| 652 | } else { |
| 653 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2]; |
| 654 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; } |
| 655 | notice = noticeMsg; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // check for mode changes |
| 660 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8); |
| 661 | if (modes != oldmodes) { |
| 662 | oldmodes = modes; |
| 663 | clearArea(1); |
| 664 | } |
| 665 | |
| 666 | // second passed? |
| 667 | if (tval - tval_thissec >= sec_ms) |
| 668 | { |
| 669 | #ifdef BENCHMARK |
| 670 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4]; |
| 671 | if(++bench == 10) { |
| 672 | bench = 0; |
| 673 | bench_fps_s = bench_fps; |
| 674 | bf[bfp++ & 3] = bench_fps; |
| 675 | bench_fps = 0; |
| 676 | } |
| 677 | bench_fps += frames_shown; |
| 678 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2); |
| 679 | #else |
| 680 | if(currentConfig.EmuOpt & 2) |
| 681 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done); |
| 682 | #endif |
| 683 | //tval_thissec += 1000; |
| 684 | tval_thissec += sec_ms; |
| 685 | |
| 686 | if (PsndOut != NULL) |
| 687 | { |
| 688 | /* some code which tries to sync things to audio clock, the dirty way */ |
| 689 | static int audio_skew_prev = 0; |
| 690 | int audio_skew, adj, co = 9, shift = 7; |
| 691 | audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos(); |
| 692 | if (PsndRate == 22050) co = 10; |
| 693 | if (PsndRate > 22050) co = 11; |
| 694 | if (PicoOpt&8) shift++; |
| 695 | if (audio_skew < 0) { |
| 696 | adj = -((-audio_skew) >> shift); |
| 697 | if (audio_skew > -(6<<co)) adj>>=1; |
| 698 | if (audio_skew > -(4<<co)) adj>>=1; |
| 699 | if (audio_skew > -(2<<co)) adj>>=1; |
| 700 | if (audio_skew > audio_skew_prev) adj>>=2; // going up already |
| 701 | } else { |
| 702 | adj = audio_skew >> shift; |
| 703 | if (audio_skew < (6<<co)) adj>>=1; |
| 704 | if (audio_skew < (4<<co)) adj>>=1; |
| 705 | if (audio_skew < (2<<co)) adj>>=1; |
| 706 | if (audio_skew < audio_skew_prev) adj>>=2; |
| 707 | } |
| 708 | audio_skew_prev = audio_skew; |
| 709 | target_frametime += adj; |
| 710 | sec_ms = (target_frametime * target_fps) >> 8; |
| 711 | //stdbg("%i %i %i", audio_skew, adj, sec_ms); |
| 712 | frames_done = frames_shown = 0; |
| 713 | } |
| 714 | else if (currentConfig.Frameskip < 0) { |
| 715 | frames_done -= target_fps; if (frames_done < 0) frames_done = 0; |
| 716 | frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0; |
| 717 | if (frames_shown > frames_done) frames_shown = frames_done; |
| 718 | } else { |
| 719 | frames_done = frames_shown = 0; |
| 720 | } |
| 721 | } |
| 722 | #ifdef PFRAMES |
| 723 | sprintf(fpsbuff, "%i", Pico.m.frame_count); |
| 724 | #endif |
| 725 | |
| 726 | tval_prev = tval; |
| 727 | lim_time = (frames_done+1) * target_frametime; |
| 728 | if (currentConfig.Frameskip >= 0) // frameskip enabled |
| 729 | { |
| 730 | for (i = 0; i < currentConfig.Frameskip; i++) { |
| 731 | updateKeys(); |
| 732 | SkipFrame(); frames_done++; |
| 733 | if (PsndOut) { // do framelimitting if sound is enabled |
| 734 | int tval_diff; |
| 735 | tval = GetTickCount(); |
| 736 | tval_diff = (int)(tval - tval_thissec) << 8; |
| 737 | if (tval_diff < lim_time) // we are too fast |
| 738 | simpleWait(tval + ((lim_time - tval_diff)>>8)); |
| 739 | } |
| 740 | lim_time += target_frametime; |
| 741 | } |
| 742 | } |
| 743 | else // auto frameskip |
| 744 | { |
| 745 | int tval_diff; |
| 746 | tval = GetTickCount(); |
| 747 | tval_diff = (int)(tval - tval_thissec) << 8; |
| 748 | if (tval_diff > lim_time) |
| 749 | { |
| 750 | // no time left for this frame - skip |
| 751 | if (tval_diff - lim_time >= (300<<8)) { |
| 752 | /* something caused a slowdown for us (disk access? cache flush?) |
| 753 | * try to recover by resetting timing... */ |
| 754 | reset_timing = 1; |
| 755 | continue; |
| 756 | } |
| 757 | updateKeys(); |
| 758 | SkipFrame(); frames_done++; |
| 759 | continue; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | updateKeys(); |
| 764 | |
| 765 | if (currentConfig.EmuOpt&0x80) |
| 766 | /* be sure correct framebuffer is locked */ |
| 767 | giz_screen = Framework2D_LockBuffer((currentConfig.EmuOpt&0x8000) ? 0 : 1); |
| 768 | |
| 769 | if (!(PicoOpt&0x10)) |
| 770 | PicoScan((unsigned) -1, NULL); |
| 771 | |
| 772 | PicoFrame(); |
| 773 | |
| 774 | if (giz_screen == NULL) |
| 775 | giz_screen = Framework2D_LockBuffer((currentConfig.EmuOpt&0x8000) ? 0 : 1); |
| 776 | |
| 777 | blit(fpsbuff, notice); |
| 778 | |
| 779 | if (giz_screen != NULL) { |
| 780 | Framework2D_UnlockBuffer(); |
| 781 | giz_screen = NULL; |
| 782 | } |
| 783 | |
| 784 | if (currentConfig.EmuOpt&0x2000) |
| 785 | Framework2D_WaitVSync(); |
| 786 | |
| 787 | if (currentConfig.EmuOpt&0x8000) |
| 788 | Framework2D_Flip(); |
| 789 | |
| 790 | // check time |
| 791 | tval = GetTickCount(); |
| 792 | tval_diff = (int)(tval - tval_thissec) << 8; |
| 793 | |
| 794 | if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection |
| 795 | reset_timing = 1; |
| 796 | else if (PsndOut != NULL || currentConfig.Frameskip < 0) |
| 797 | { |
| 798 | // sleep if we are still too fast |
| 799 | if (tval_diff < lim_time) |
| 800 | { |
| 801 | // we are too fast |
| 802 | simpleWait(tval + ((lim_time - tval_diff) >> 8)); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | frames_done++; frames_shown++; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | if (PicoMCD & 1) PicoCDBufferFree(); |
| 811 | |
| 812 | if (PsndOut != NULL) { |
| 813 | PsndOut = snd_cbuff = NULL; |
| 814 | FrameworkAudio_Close(); |
| 815 | } |
| 816 | |
| 817 | // save SRAM |
| 818 | if ((currentConfig.EmuOpt & 1) && SRam.changed) { |
| 819 | emu_stateCb("Writing SRAM/BRAM.."); |
| 820 | emu_SaveLoadGame(0, 1); |
| 821 | SRam.changed = 0; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | |
| 826 | void emu_ResetGame(void) |
| 827 | { |
| 828 | PicoReset(0); |
| 829 | reset_timing = 1; |
| 830 | } |
| 831 | |