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