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