| | 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/arm_utils.h" |
| | 12 | #include "../common/config.h" |
| | 13 | #include "../libpicofe/lprintf.h" |
| | 14 | #include "emu.h" |
| | 15 | #include "menu.h" |
| | 16 | #include "giz.h" |
| | 17 | #include "asm_utils.h" |
| | 18 | |
| | 19 | #include <pico/pico_int.h> |
| | 20 | |
| | 21 | #ifdef BENCHMARK |
| | 22 | #define OSD_FPS_X 220 |
| | 23 | #else |
| | 24 | #define OSD_FPS_X 260 |
| | 25 | #endif |
| | 26 | |
| | 27 | // main 300K gfx-related buffer. Used by menu and renderers. |
| | 28 | unsigned char gfx_buffer[321*240*2*2]; |
| | 29 | |
| | 30 | static short *snd_cbuff = NULL; |
| | 31 | static int snd_cbuf_samples = 0, snd_all_samples = 0; |
| | 32 | |
| | 33 | |
| | 34 | static void blit(const char *fps, const char *notice); |
| | 35 | static void clearArea(int full); |
| | 36 | |
| | 37 | int plat_get_root_dir(char *dst, int len) |
| | 38 | { |
| | 39 | if (len > 0) *dst = 0; |
| | 40 | |
| | 41 | return 0; |
| | 42 | } |
| | 43 | |
| | 44 | static void emu_msg_cb(const char *msg) |
| | 45 | { |
| | 46 | if (giz_screen != NULL) fb_unlock(); |
| | 47 | giz_screen = fb_lock(1); |
| | 48 | |
| | 49 | memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
| | 50 | emu_text_out16(4, 232, msg); |
| | 51 | noticeMsgTime = GetTickCount() - 2000; |
| | 52 | |
| | 53 | /* assumption: emu_msg_cb gets called only when something slow is about to happen */ |
| | 54 | reset_timing = 1; |
| | 55 | |
| | 56 | fb_unlock(); |
| | 57 | giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1); |
| | 58 | } |
| | 59 | |
| | 60 | void emu_stateCb(const char *str) |
| | 61 | { |
| | 62 | if (giz_screen != NULL) fb_unlock(); |
| | 63 | giz_screen = fb_lock(1); |
| | 64 | |
| | 65 | clearArea(0); |
| | 66 | blit("", str); |
| | 67 | |
| | 68 | fb_unlock(); |
| | 69 | giz_screen = NULL; |
| | 70 | |
| | 71 | Sleep(0); /* yield the CPU, the system may need it */ |
| | 72 | } |
| | 73 | |
| | 74 | void pemu_prep_defconfig(void) |
| | 75 | { |
| | 76 | defaultConfig.s_PsndRate = 22050; |
| | 77 | defaultConfig.s_PicoCDBuffers = 0; |
| | 78 | defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU |
| | 79 | defaultConfig.KeyBinds[ 3] = 1<<1; |
| | 80 | defaultConfig.KeyBinds[ 0] = 1<<2; |
| | 81 | defaultConfig.KeyBinds[ 1] = 1<<3; |
| | 82 | defaultConfig.KeyBinds[ 5] = 1<<4; |
| | 83 | defaultConfig.KeyBinds[ 6] = 1<<5; |
| | 84 | defaultConfig.KeyBinds[ 7] = 1<<6; |
| | 85 | defaultConfig.KeyBinds[ 4] = 1<<7; |
| | 86 | defaultConfig.KeyBinds[13] = 1<<26; // switch rend |
| | 87 | defaultConfig.KeyBinds[ 8] = 1<<27; // save state |
| | 88 | defaultConfig.KeyBinds[ 9] = 1<<28; // load state |
| | 89 | defaultConfig.KeyBinds[12] = 1<<29; // vol up |
| | 90 | defaultConfig.KeyBinds[11] = 1<<30; // vol down |
| | 91 | defaultConfig.scaling = 0; |
| | 92 | } |
| | 93 | |
| | 94 | |
| | 95 | static int EmuScanBegin16(unsigned int num) |
| | 96 | { |
| | 97 | Pico.est.DrawLineDest = (unsigned short *) giz_screen + 321 * num; |
| | 98 | |
| | 99 | if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1)) |
| | 100 | return 1; // skip next line |
| | 101 | |
| | 102 | return 0; |
| | 103 | } |
| | 104 | |
| | 105 | static int EmuScanBegin8(unsigned int num) |
| | 106 | { |
| | 107 | // draw like the fast renderer |
| | 108 | Pico.est.HighCol = gfx_buffer + 328 * num; |
| | 109 | |
| | 110 | return 0; |
| | 111 | } |
| | 112 | |
| | 113 | static void osd_text(int x, int y, const char *text) |
| | 114 | { |
| | 115 | int len = strlen(text) * 8 / 2; |
| | 116 | int *p, h; |
| | 117 | for (h = 0; h < 8; h++) { |
| | 118 | p = (int *) ((unsigned short *) giz_screen+x+321*(y+h)); |
| | 119 | p = (int *) ((int)p & ~3); // align |
| | 120 | memset32(p, 0, len); |
| | 121 | } |
| | 122 | emu_text_out16(x, y, text); |
| | 123 | } |
| | 124 | |
| | 125 | static void cd_leds(void) |
| | 126 | { |
| | 127 | static int old_reg = 0; |
| | 128 | unsigned int col_g, col_r, *p; |
| | 129 | |
| | 130 | if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change |
| | 131 | old_reg = Pico_mcd->s68k_regs[0]; |
| | 132 | |
| | 133 | p = (unsigned int *)((short *)giz_screen + 321*2+4+2); |
| | 134 | col_g = (old_reg & 2) ? 0x06000600 : 0; |
| | 135 | col_r = (old_reg & 1) ? 0xc000c000 : 0; |
| | 136 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1; |
| | 137 | *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2; |
| | 138 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; |
| | 139 | } |
| | 140 | |
| | 141 | |
| | 142 | static short localPal[0x100]; |
| | 143 | |
| | 144 | static void blit(const char *fps, const char *notice) |
| | 145 | { |
| | 146 | int emu_opt = currentConfig.EmuOpt; |
| | 147 | |
| | 148 | if (PicoIn.opt&0x10) |
| | 149 | { |
| | 150 | int lines_flags = 224; |
| | 151 | // 8bit fast renderer |
| | 152 | if (Pico.m.dirtyPal) { |
| | 153 | Pico.m.dirtyPal = 0; |
| | 154 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
| | 155 | } |
| | 156 | // a hack for VR |
| | 157 | if (PicoIn.AHW & PAHW_SVP) |
| | 158 | memset((int *)(Pico.est.Draw2FB+328*8+328*223), 0xe0e0e0e0, 328*4); |
| | 159 | if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000; |
| | 160 | if (currentConfig.EmuOpt&0x4000) |
| | 161 | lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000; |
| | 162 | vidCpy8to16((unsigned short *)giz_screen+321*8, Pico.est.Draw2FB+328*8, localPal, lines_flags); |
| | 163 | } |
| | 164 | else if (!(emu_opt&0x80)) |
| | 165 | { |
| | 166 | int lines_flags; |
| | 167 | // 8bit accurate renderer |
| | 168 | if (Pico.m.dirtyPal) { |
| | 169 | if (Pico.m.dirtyPal == 2) |
| | 170 | Pico.m.dirtyPal = 0; |
| | 171 | /* no support |
| | 172 | switch (Pico.est.SonicPalCount) { |
| | 173 | case 3: vidConvCpyRGB565(localPal+0xc0, Pico.est.SonicPal+0xc0, 0x40); |
| | 174 | case 2: vidConvCpyRGB565(localPal+0x80, Pico.est.SonicPal+0x80, 0x40); |
| | 175 | case 1: vidConvCpyRGB565(localPal+0x40, Pico.est.SonicPal+0x40, 0x40); |
| | 176 | default://vidConvCpyRGB565(localPal, Pico.est.SonicPal, 0x40); |
| | 177 | } */ |
| | 178 | vidConvCpyRGB565(localPal, Pico.est.SonicPal, 0x40); |
| | 179 | if (Pico.video.reg[0xC]&8) { // shadow/hilight mode |
| | 180 | //vidConvCpyRGB32sh(localPal+0x40, Pico.est.SonicPal, 0x40); |
| | 181 | //vidConvCpyRGB32hi(localPal+0x80, Pico.est.SonicPal, 0x40); // TODO? |
| | 182 | memcpy((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2); |
| | 183 | localPal[0xc0] = 0x0600; |
| | 184 | localPal[0xd0] = 0xc000; |
| | 185 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
| | 186 | localPal[0xf0] = 0xffff; |
| | 187 | } |
| | 188 | } |
| | 189 | lines_flags = (Pico.video.reg[1]&8) ? 240 : 224; |
| | 190 | if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000; |
| | 191 | if (currentConfig.EmuOpt&0x4000) |
| | 192 | lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000; |
| | 193 | vidCpy8to16((unsigned short *)giz_screen+321*8, Pico.est.Draw2FB+328*8, localPal, lines_flags); |
| | 194 | } |
| | 195 | |
| | 196 | if (notice || (emu_opt & 2)) { |
| | 197 | int h = 232; |
| | 198 | if (notice) osd_text(4, h, notice); |
| | 199 | if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps); |
| | 200 | } |
| | 201 | |
| | 202 | if ((emu_opt & 0x400) && (PicoIn.AHW & PAHW_MCD)) |
| | 203 | cd_leds(); |
| | 204 | } |
| | 205 | |
| | 206 | // clears whole screen or just the notice area (in all buffers) |
| | 207 | static void clearArea(int full) |
| | 208 | { |
| | 209 | if (giz_screen == NULL) |
| | 210 | giz_screen = fb_lock(1); |
| | 211 | if (full) memset32(giz_screen, 0, 320*240*2/4); |
| | 212 | else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
| | 213 | |
| | 214 | if (currentConfig.EmuOpt&0x8000) { |
| | 215 | fb_unlock(); |
| | 216 | giz_screen = fb_lock(0); |
| | 217 | if (full) memset32(giz_screen, 0, 320*240*2/4); |
| | 218 | else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
| | 219 | } |
| | 220 | } |
| | 221 | |
| | 222 | static void vidResetMode(void) |
| | 223 | { |
| | 224 | giz_screen = fb_lock(1); |
| | 225 | |
| | 226 | if (PicoIn.opt&0x10) { |
| | 227 | } else if (currentConfig.EmuOpt&0x80) { |
| | 228 | PicoDrawSetOutFormat(PDF_RGB555, 0); |
| | 229 | PicoDrawSetCallbacks(EmuScanBegin16, NULL); |
| | 230 | } else { |
| | 231 | PicoDrawSetOutFormat(PDF_NONE, 0); |
| | 232 | PicoDrawSetCallbacks(EmuScanBegin8, NULL); |
| | 233 | } |
| | 234 | if ((PicoIn.opt&0x10) || !(currentConfig.EmuOpt&0x80)) { |
| | 235 | // setup pal for 8-bit modes |
| | 236 | localPal[0xc0] = 0x0600; |
| | 237 | localPal[0xd0] = 0xc000; |
| | 238 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
| | 239 | localPal[0xf0] = 0xffff; |
| | 240 | } |
| | 241 | Pico.m.dirtyPal = 1; |
| | 242 | |
| | 243 | memset32(giz_screen, 0, 321*240*2/4); |
| | 244 | if (currentConfig.EmuOpt&0x8000) { |
| | 245 | fb_unlock(); |
| | 246 | giz_screen = fb_lock(0); |
| | 247 | memset32(giz_screen, 0, 321*240*2/4); |
| | 248 | } |
| | 249 | fb_unlock(); |
| | 250 | giz_screen = NULL; |
| | 251 | } |
| | 252 | |
| | 253 | /* |
| | 254 | #include <stdarg.h> |
| | 255 | static void stdbg(const char *fmt, ...) |
| | 256 | { |
| | 257 | static int cnt = 0; |
| | 258 | va_list vl; |
| | 259 | |
| | 260 | sprintf(noticeMsg, "%x ", cnt++); |
| | 261 | va_start(vl, fmt); |
| | 262 | vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl); |
| | 263 | va_end(vl); |
| | 264 | |
| | 265 | noticeMsgTime = GetTickCount(); |
| | 266 | } |
| | 267 | */ |
| | 268 | |
| | 269 | static void updateSound(int len) |
| | 270 | { |
| | 271 | snd_all_samples += len / 2; |
| | 272 | PicoIn.sndOut += len / 2; |
| | 273 | if (PicoIn.sndOut - snd_cbuff >= snd_cbuf_samples) |
| | 274 | { |
| | 275 | //if (PicoIn.sndOut - snd_cbuff != snd_cbuf_samples) |
| | 276 | // stdbg("snd diff is %i, not %i", PicoIn.sndOut - snd_cbuff, snd_cbuf_samples); |
| | 277 | PicoIn.sndOut = snd_cbuff; |
| | 278 | } |
| | 279 | } |
| | 280 | |
| | 281 | |
| | 282 | static void SkipFrame(void) |
| | 283 | { |
| | 284 | PicoIn.skipFrame=1; |
| | 285 | PicoFrame(); |
| | 286 | PicoIn.skipFrame=0; |
| | 287 | } |
| | 288 | |
| | 289 | /* forced frame to front buffer */ |
| | 290 | void pemu_forced_frame(int no_scale, int do_emu) |
| | 291 | { |
| | 292 | int po_old = PicoIn.opt; |
| | 293 | int eo_old = currentConfig.EmuOpt; |
| | 294 | |
| | 295 | PicoIn.opt &= ~0x10; |
| | 296 | PicoIn.opt |= POPT_ACC_SPRITES; |
| | 297 | if (!no_scale && currentConfig.scaling) |
| | 298 | PicoIn.opt |= POPT_EN_SOFTSCALE; |
| | 299 | currentConfig.EmuOpt |= 0x80; |
| | 300 | |
| | 301 | if (giz_screen == NULL) |
| | 302 | giz_screen = fb_lock(1); |
| | 303 | |
| | 304 | PicoDrawSetOutFormat(PDF_RGB555, 0); |
| | 305 | PicoDrawSetCallbacks(EmuScanBegin16, NULL); |
| | 306 | Pico.m.dirtyPal = 1; |
| | 307 | PicoFrameDrawOnly(); |
| | 308 | |
| | 309 | fb_unlock(); |
| | 310 | giz_screen = NULL; |
| | 311 | |
| | 312 | PicoIn.opt = po_old; |
| | 313 | currentConfig.EmuOpt = eo_old; |
| | 314 | } |
| | 315 | |
| | 316 | |
| | 317 | static void RunEvents(unsigned int which) |
| | 318 | { |
| | 319 | if (which & 0x1800) // save or load (but not both) |
| | 320 | { |
| | 321 | int do_it = 1; |
| | 322 | |
| | 323 | if (PicoIn.sndOut != NULL) |
| | 324 | FrameworkAudio_SetPause(1); |
| | 325 | if (giz_screen == NULL) |
| | 326 | giz_screen = fb_lock(1); |
| | 327 | if ( emu_check_save_file(state_slot) && |
| | 328 | (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load |
| | 329 | (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save |
| | 330 | { |
| | 331 | int keys; |
| | 332 | blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)"); |
| | 333 | while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) |
| | 334 | Sleep(50); |
| | 335 | if (keys & PBTN_STOP) do_it = 0; |
| | 336 | while( ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release |
| | 337 | Sleep(50); |
| | 338 | clearArea(0); |
| | 339 | } |
| | 340 | |
| | 341 | if (do_it) |
| | 342 | { |
| | 343 | osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME"); |
| | 344 | PicoStateProgressCB = emu_stateCb; |
| | 345 | emu_save_load_game((which & 0x1000) >> 12, 0); |
| | 346 | PicoStateProgressCB = NULL; |
| | 347 | Sleep(0); |
| | 348 | } |
| | 349 | |
| | 350 | if (PicoIn.sndOut != NULL) |
| | 351 | FrameworkAudio_SetPause(0); |
| | 352 | reset_timing = 1; |
| | 353 | } |
| | 354 | if (which & 0x0400) // switch renderer |
| | 355 | { |
| | 356 | if (PicoIn.opt&0x10) { PicoIn.opt&=~0x10; currentConfig.EmuOpt |= 0x80; } |
| | 357 | else { PicoIn.opt|= 0x10; currentConfig.EmuOpt &= ~0x80; } |
| | 358 | |
| | 359 | vidResetMode(); |
| | 360 | |
| | 361 | if (PicoIn.opt&0x10) { |
| | 362 | strcpy(noticeMsg, " 8bit fast renderer"); |
| | 363 | } else if (currentConfig.EmuOpt&0x80) { |
| | 364 | strcpy(noticeMsg, "16bit accurate renderer"); |
| | 365 | } else { |
| | 366 | strcpy(noticeMsg, " 8bit accurate renderer"); |
| | 367 | } |
| | 368 | |
| | 369 | noticeMsgTime = GetTickCount(); |
| | 370 | } |
| | 371 | if (which & 0x0300) |
| | 372 | { |
| | 373 | if(which&0x0200) { |
| | 374 | state_slot -= 1; |
| | 375 | if(state_slot < 0) state_slot = 9; |
| | 376 | } else { |
| | 377 | state_slot += 1; |
| | 378 | if(state_slot > 9) state_slot = 0; |
| | 379 | } |
| | 380 | sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE"); |
| | 381 | noticeMsgTime = GetTickCount(); |
| | 382 | } |
| | 383 | } |
| | 384 | |
| | 385 | static void updateKeys(void) |
| | 386 | { |
| | 387 | unsigned int keys, allActions[2] = { 0, 0 }, events; |
| | 388 | static unsigned int prevEvents = 0; |
| | 389 | int i; |
| | 390 | |
| | 391 | /* FIXME: port to input fw, merge with emu.c:emu_update_input() */ |
| | 392 | keys = Framework_PollGetButtons(); |
| | 393 | if (keys & PBTN_HOME) |
| | 394 | engineState = PGS_Menu; |
| | 395 | |
| | 396 | keys &= CONFIGURABLE_KEYS; |
| | 397 | |
| | 398 | PicoIn.pad[0] = allActions[0] & 0xfff; |
| | 399 | PicoIn.pad[1] = allActions[1] & 0xfff; |
| | 400 | |
| | 401 | if (allActions[0] & 0x7000) emu_DoTurbo(&PicoIn.pad[0], allActions[0]); |
| | 402 | if (allActions[1] & 0x7000) emu_DoTurbo(&PicoIn.pad[1], allActions[1]); |
| | 403 | |
| | 404 | events = (allActions[0] | allActions[1]) >> 16; |
| | 405 | |
| | 406 | // volume is treated in special way and triggered every frame |
| | 407 | if ((events & 0x6000) && PicoIn.sndOut != NULL) |
| | 408 | { |
| | 409 | int vol = currentConfig.volume; |
| | 410 | if (events & 0x2000) { |
| | 411 | if (vol < 100) vol++; |
| | 412 | } else { |
| | 413 | if (vol > 0) vol--; |
| | 414 | } |
| | 415 | FrameworkAudio_SetVolume(vol, vol); |
| | 416 | sprintf(noticeMsg, "VOL: %02i ", vol); |
| | 417 | noticeMsgTime = GetTickCount(); |
| | 418 | currentConfig.volume = vol; |
| | 419 | } |
| | 420 | |
| | 421 | events &= ~prevEvents; |
| | 422 | if (events) RunEvents(events); |
| | 423 | if (movie_data) emu_updateMovie(); |
| | 424 | |
| | 425 | prevEvents = (allActions[0] | allActions[1]) >> 16; |
| | 426 | } |
| | 427 | |
| | 428 | void plat_debug_cat(char *str) |
| | 429 | { |
| | 430 | } |
| | 431 | |
| | 432 | static void simpleWait(DWORD until) |
| | 433 | { |
| | 434 | DWORD tval; |
| | 435 | int diff; |
| | 436 | |
| | 437 | tval = GetTickCount(); |
| | 438 | diff = (int)until - (int)tval; |
| | 439 | if (diff >= 2) |
| | 440 | Sleep(diff - 1); |
| | 441 | |
| | 442 | while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection |
| | 443 | spend_cycles(1024*2); |
| | 444 | } |
| | 445 | |
| | 446 | void pemu_loop(void) |
| | 447 | { |
| | 448 | static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0; |
| | 449 | char fpsbuff[24]; // fps count c string |
| | 450 | DWORD tval, tval_prev = 0, tval_thissec = 0; // timing |
| | 451 | int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000; |
| | 452 | int target_fps, target_frametime, lim_time, tval_diff, i; |
| | 453 | char *notice = NULL; |
| | 454 | |
| | 455 | lprintf("entered emu_Loop()\n"); |
| | 456 | |
| | 457 | fpsbuff[0] = 0; |
| | 458 | |
| | 459 | // make sure we are in correct mode |
| | 460 | vidResetMode(); |
| | 461 | if (currentConfig.scaling) PicoIn.opt|=0x4000; |
| | 462 | else PicoIn.opt&=~0x4000; |
| | 463 | Pico.m.dirtyPal = 1; |
| | 464 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc; |
| | 465 | |
| | 466 | // pal/ntsc might have changed, reset related stuff |
| | 467 | target_fps = Pico.m.pal ? 50 : 60; |
| | 468 | target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1; |
| | 469 | reset_timing = 1; |
| | 470 | |
| | 471 | // prepare CD buffer |
| | 472 | if (PicoIn.AHW & PAHW_MCD) PicoCDBufferInit(); |
| | 473 | |
| | 474 | // prepare sound stuff |
| | 475 | PicoIn.sndOut = NULL; |
| | 476 | if (currentConfig.EmuOpt & 4) |
| | 477 | { |
| | 478 | int ret, snd_excess_add, stereo; |
| | 479 | if (PicoIn.sndRate != PsndRate_old || (PicoIn.opt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) { |
| | 480 | PsndRerate(Pico.m.frame_count ? 1 : 0); |
| | 481 | } |
| | 482 | stereo=(PicoIn.opt&8)>>3; |
| | 483 | snd_excess_add = ((PicoIn.sndRate - Pico.snd.len*target_fps)<<16) / target_fps; |
| | 484 | snd_cbuf_samples = (PicoIn.sndRate<<stereo) * 16 / target_fps; |
| | 485 | lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", |
| | 486 | PicoIn.sndRate, Pico.snd.len, snd_excess_add, stereo, Pico.m.pal); |
| | 487 | ret = FrameworkAudio_Init(PicoIn.sndRate, snd_cbuf_samples, stereo); |
| | 488 | if (ret != 0) { |
| | 489 | lprintf("FrameworkAudio_Init() failed: %i\n", ret); |
| | 490 | sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret); |
| | 491 | noticeMsgTime = GetTickCount(); |
| | 492 | currentConfig.EmuOpt &= ~4; |
| | 493 | } else { |
| | 494 | FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume); |
| | 495 | PicoIn.writeSound = updateSound; |
| | 496 | snd_cbuff = FrameworkAudio_56448Buffer(); |
| | 497 | PicoIn.sndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle |
| | 498 | snd_all_samples = 0; |
| | 499 | PsndRate_old = PicoIn.sndRate; |
| | 500 | PicoOpt_old = PicoIn.opt; |
| | 501 | pal_old = Pico.m.pal; |
| | 502 | } |
| | 503 | } |
| | 504 | |
| | 505 | // loop? |
| | 506 | while (engineState == PGS_Running) |
| | 507 | { |
| | 508 | int modes; |
| | 509 | |
| | 510 | tval = GetTickCount(); |
| | 511 | if (reset_timing || tval < tval_prev) { |
| | 512 | //stdbg("timing reset"); |
| | 513 | reset_timing = 0; |
| | 514 | tval_thissec = tval; |
| | 515 | frames_shown = frames_done = 0; |
| | 516 | } |
| | 517 | |
| | 518 | // show notice message? |
| | 519 | if (noticeMsgTime) { |
| | 520 | static int noticeMsgSum; |
| | 521 | if (tval - noticeMsgTime > 2000) { // > 2.0 sec |
| | 522 | noticeMsgTime = 0; |
| | 523 | clearArea(0); |
| | 524 | notice = 0; |
| | 525 | } else { |
| | 526 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2]; |
| | 527 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; } |
| | 528 | notice = noticeMsg; |
| | 529 | } |
| | 530 | } |
| | 531 | |
| | 532 | // check for mode changes |
| | 533 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8); |
| | 534 | if (modes != oldmodes) { |
| | 535 | oldmodes = modes; |
| | 536 | clearArea(1); |
| | 537 | } |
| | 538 | |
| | 539 | // second passed? |
| | 540 | if (tval - tval_thissec >= sec_ms) |
| | 541 | { |
| | 542 | #ifdef BENCHMARK |
| | 543 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4]; |
| | 544 | if(++bench == 10) { |
| | 545 | bench = 0; |
| | 546 | bench_fps_s = bench_fps; |
| | 547 | bf[bfp++ & 3] = bench_fps; |
| | 548 | bench_fps = 0; |
| | 549 | } |
| | 550 | bench_fps += frames_shown; |
| | 551 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2); |
| | 552 | #else |
| | 553 | if(currentConfig.EmuOpt & 2) |
| | 554 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done); |
| | 555 | #endif |
| | 556 | //tval_thissec += 1000; |
| | 557 | tval_thissec += sec_ms; |
| | 558 | |
| | 559 | if (PicoIn.sndOut != NULL) |
| | 560 | { |
| | 561 | /* some code which tries to sync things to audio clock, the dirty way */ |
| | 562 | static int audio_skew_prev = 0; |
| | 563 | int audio_skew, adj, co = 9, shift = 7; |
| | 564 | audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos(); |
| | 565 | if (PicoIn.sndRate == 22050) co = 10; |
| | 566 | if (PicoIn.sndRate > 22050) co = 11; |
| | 567 | if (PicoIn.opt&8) shift++; |
| | 568 | if (audio_skew < 0) { |
| | 569 | adj = -((-audio_skew) >> shift); |
| | 570 | if (audio_skew > -(6<<co)) adj>>=1; |
| | 571 | if (audio_skew > -(4<<co)) adj>>=1; |
| | 572 | if (audio_skew > -(2<<co)) adj>>=1; |
| | 573 | if (audio_skew > audio_skew_prev) adj>>=2; // going up already |
| | 574 | } else { |
| | 575 | adj = audio_skew >> shift; |
| | 576 | if (audio_skew < (6<<co)) adj>>=1; |
| | 577 | if (audio_skew < (4<<co)) adj>>=1; |
| | 578 | if (audio_skew < (2<<co)) adj>>=1; |
| | 579 | if (audio_skew < audio_skew_prev) adj>>=2; |
| | 580 | } |
| | 581 | audio_skew_prev = audio_skew; |
| | 582 | target_frametime += adj; |
| | 583 | sec_ms = (target_frametime * target_fps) >> 8; |
| | 584 | //stdbg("%i %i %i", audio_skew, adj, sec_ms); |
| | 585 | frames_done = frames_shown = 0; |
| | 586 | } |
| | 587 | else if (currentConfig.Frameskip < 0) { |
| | 588 | frames_done -= target_fps; if (frames_done < 0) frames_done = 0; |
| | 589 | frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0; |
| | 590 | if (frames_shown > frames_done) frames_shown = frames_done; |
| | 591 | } else { |
| | 592 | frames_done = frames_shown = 0; |
| | 593 | } |
| | 594 | } |
| | 595 | #ifdef PFRAMES |
| | 596 | sprintf(fpsbuff, "%i", Pico.m.frame_count); |
| | 597 | #endif |
| | 598 | |
| | 599 | tval_prev = tval; |
| | 600 | lim_time = (frames_done+1) * target_frametime; |
| | 601 | if (currentConfig.Frameskip >= 0) // frameskip enabled |
| | 602 | { |
| | 603 | for (i = 0; i < currentConfig.Frameskip; i++) { |
| | 604 | updateKeys(); |
| | 605 | SkipFrame(); frames_done++; |
| | 606 | if (PicoIn.sndOut) { // do framelimitting if sound is enabled |
| | 607 | int tval_diff; |
| | 608 | tval = GetTickCount(); |
| | 609 | tval_diff = (int)(tval - tval_thissec) << 8; |
| | 610 | if (tval_diff < lim_time) // we are too fast |
| | 611 | simpleWait(tval + ((lim_time - tval_diff)>>8)); |
| | 612 | } |
| | 613 | lim_time += target_frametime; |
| | 614 | } |
| | 615 | } |
| | 616 | else // auto frameskip |
| | 617 | { |
| | 618 | int tval_diff; |
| | 619 | tval = GetTickCount(); |
| | 620 | tval_diff = (int)(tval - tval_thissec) << 8; |
| | 621 | if (tval_diff > lim_time) |
| | 622 | { |
| | 623 | // no time left for this frame - skip |
| | 624 | if (tval_diff - lim_time >= (300<<8)) { |
| | 625 | /* something caused a slowdown for us (disk access? cache flush?) |
| | 626 | * try to recover by resetting timing... */ |
| | 627 | reset_timing = 1; |
| | 628 | continue; |
| | 629 | } |
| | 630 | updateKeys(); |
| | 631 | SkipFrame(); frames_done++; |
| | 632 | continue; |
| | 633 | } |
| | 634 | } |
| | 635 | |
| | 636 | updateKeys(); |
| | 637 | |
| | 638 | if (currentConfig.EmuOpt&0x80) |
| | 639 | /* be sure correct framebuffer is locked */ |
| | 640 | giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1); |
| | 641 | |
| | 642 | PicoFrame(); |
| | 643 | |
| | 644 | if (giz_screen == NULL) |
| | 645 | giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1); |
| | 646 | |
| | 647 | blit(fpsbuff, notice); |
| | 648 | |
| | 649 | if (giz_screen != NULL) { |
| | 650 | fb_unlock(); |
| | 651 | giz_screen = NULL; |
| | 652 | } |
| | 653 | |
| | 654 | if (currentConfig.EmuOpt&0x2000) |
| | 655 | Framework2D_WaitVSync(); |
| | 656 | |
| | 657 | if (currentConfig.EmuOpt&0x8000) |
| | 658 | fb_flip(); |
| | 659 | |
| | 660 | // check time |
| | 661 | tval = GetTickCount(); |
| | 662 | tval_diff = (int)(tval - tval_thissec) << 8; |
| | 663 | |
| | 664 | if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection |
| | 665 | reset_timing = 1; |
| | 666 | else if (PicoIn.sndOut != NULL || currentConfig.Frameskip < 0) |
| | 667 | { |
| | 668 | // sleep if we are still too fast |
| | 669 | if (tval_diff < lim_time) |
| | 670 | { |
| | 671 | // we are too fast |
| | 672 | simpleWait(tval + ((lim_time - tval_diff) >> 8)); |
| | 673 | } |
| | 674 | } |
| | 675 | |
| | 676 | frames_done++; frames_shown++; |
| | 677 | } |
| | 678 | |
| | 679 | |
| | 680 | if (PicoIn.AHW & PAHW_MCD) PicoCDBufferFree(); |
| | 681 | |
| | 682 | if (PicoIn.sndOut != NULL) { |
| | 683 | PicoIn.sndOut = snd_cbuff = NULL; |
| | 684 | FrameworkAudio_Close(); |
| | 685 | } |
| | 686 | |
| | 687 | // save SRAM |
| | 688 | if ((currentConfig.EmuOpt & 1) && SRam.changed) { |
| | 689 | emu_stateCb("Writing SRAM/BRAM.."); |
| | 690 | emu_save_load_game(0, 1); |
| | 691 | SRam.changed = 0; |
| | 692 | } |
| | 693 | } |
| | 694 | |