libretro: empty srm prevention hack
[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         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         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 /*
126 void log1(void *p1, void *p2)
127 {
128         lprintf("%p %p %p\n", p1, p2, DrawLineDest);
129 }
130 */
131
132 static 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
149 static short localPal[0x100];
150
151 static void blit(const char *fps, const char *notice)
152 {
153         int emu_opt = currentConfig.EmuOpt;
154
155         if (PicoOpt&0x10)
156         {
157                 int lines_flags = 224;
158                 // 8bit fast renderer
159                 if (Pico.m.dirtyPal) {
160                         Pico.m.dirtyPal = 0;
161                         vidConvCpyRGB565(localPal, Pico.cram, 0x40);
162                 }
163                 // a hack for VR
164                 if (PicoAHW & PAHW_SVP)
165                         memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
166                 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
167                 if (currentConfig.EmuOpt&0x4000)
168                         lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
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;
174                 // 8bit accurate renderer
175                 if (Pico.m.dirtyPal) {
176                         Pico.m.dirtyPal = 0;
177                         vidConvCpyRGB565(localPal, Pico.cram, 0x40);
178                         if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
179                                 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
180                                 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
181                                 memcpy32((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2/4);
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                 }
193                 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
194                 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
195                 if (currentConfig.EmuOpt&0x4000)
196                         lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
197                 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
198         }
199
200         if (notice || (emu_opt & 2)) {
201                 int h = 232;
202                 if (notice)      osd_text(4, h, notice);
203                 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
204         }
205
206         if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
207                 cd_leds();
208 }
209
210 // clears whole screen or just the notice area (in all buffers)
211 static void clearArea(int full)
212 {
213         if (giz_screen == NULL)
214                 giz_screen = fb_lock(1);
215         if (full) memset32(giz_screen, 0, 320*240*2/4);
216         else      memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
217
218         if (currentConfig.EmuOpt&0x8000) {
219                 fb_unlock();
220                 giz_screen = fb_lock(0);
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         }
224 }
225
226 static void vidResetMode(void)
227 {
228         giz_screen = fb_lock(1);
229
230         if (PicoOpt&0x10) {
231         } else if (currentConfig.EmuOpt&0x80) {
232                 PicoDrawSetOutFormat(PDF_RGB555, 0);
233                 PicoDrawSetCallbacks(EmuScanBegin16, NULL);
234         } else {
235                 PicoDrawSetOutFormat(PDF_NONE, 0);
236                 PicoDrawSetCallbacks(EmuScanBegin8, NULL);
237         }
238         if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
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;
246
247         memset32(giz_screen, 0, 321*240*2/4);
248         if (currentConfig.EmuOpt&0x8000) {
249                 fb_unlock();
250                 giz_screen = fb_lock(0);
251                 memset32(giz_screen, 0, 321*240*2/4);
252         }
253         fb_unlock();
254         giz_screen = NULL;
255 }
256
257 /*
258 #include <stdarg.h>
259 static void stdbg(const char *fmt, ...)
260 {
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 }
271 */
272
273 static void updateSound(int len)
274 {
275         snd_all_samples += len / 2;
276         PsndOut += len / 2;
277         if (PsndOut - snd_cbuff >= snd_cbuf_samples)
278         {
279                 //if (PsndOut - snd_cbuff != snd_cbuf_samples)
280                 //      stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
281                 PsndOut = snd_cbuff;
282         }
283 }
284
285
286 static void SkipFrame(void)
287 {
288         PicoSkipFrame=1;
289         PicoFrame();
290         PicoSkipFrame=0;
291 }
292
293 /* forced frame to front buffer */
294 void pemu_forced_frame(int no_scale, int do_emu)
295 {
296         int po_old = PicoOpt;
297         int eo_old = currentConfig.EmuOpt;
298
299         PicoOpt &= ~0x10;
300         PicoOpt |= POPT_ACC_SPRITES;
301         if (!no_scale)
302                 PicoOpt |= POPT_EN_SOFTSCALE;
303         currentConfig.EmuOpt |= 0x80;
304
305         if (giz_screen == NULL)
306                 giz_screen = fb_lock(1);
307
308         PicoDrawSetOutFormat(PDF_RGB555, 0);
309         PicoDrawSetCallbacks(EmuScanBegin16, NULL);
310         Pico.m.dirtyPal = 1;
311         PicoFrameDrawOnly();
312
313         fb_unlock();
314         giz_screen = NULL;
315
316         PicoOpt = po_old;
317         currentConfig.EmuOpt = eo_old;
318 }
319
320
321 static void RunEvents(unsigned int which)
322 {
323         if (which & 0x1800) // save or load (but not both)
324         {
325                 int do_it = 1;
326
327                 if (PsndOut != NULL)
328                         FrameworkAudio_SetPause(1);
329                 if (giz_screen == NULL)
330                         giz_screen = fb_lock(1);
331                 if ( emu_check_save_file(state_slot) &&
332                                 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
333                                  (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
334                 {
335                         int keys;
336                         blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
337                         while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) )
338                                 Sleep(50);
339                         if (keys & PBTN_STOP) do_it = 0;
340                         while(  ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release
341                                 Sleep(50);
342                         clearArea(0);
343                 }
344
345                 if (do_it)
346                 {
347                         osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
348                         PicoStateProgressCB = emu_stateCb;
349                         emu_save_load_game((which & 0x1000) >> 12, 0);
350                         PicoStateProgressCB = NULL;
351                         Sleep(0);
352                 }
353
354                 if (PsndOut != NULL)
355                         FrameworkAudio_SetPause(0);
356                 reset_timing = 1;
357         }
358         if (which & 0x0400) // switch renderer
359         {
360                 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |=  0x80; }
361                 else              { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
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         }
375         if (which & 0x0300)
376         {
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                 }
384                 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
385                 noticeMsgTime = GetTickCount();
386         }
387 }
388
389 static void updateKeys(void)
390 {
391         unsigned int keys, allActions[2] = { 0, 0 }, events;
392         static unsigned int prevEvents = 0;
393         int i;
394
395         /* FIXME: port to input fw, merge with emu.c:emu_update_input() */
396         keys = Framework_PollGetButtons();
397         if (keys & PBTN_HOME)
398                 engineState = PGS_Menu;
399
400         keys &= CONFIGURABLE_KEYS;
401
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]);
407
408         events = (allActions[0] | allActions[1]) >> 16;
409
410         // volume is treated in special way and triggered every frame
411         if ((events & 0x6000) && PsndOut != NULL)
412         {
413                 int vol = currentConfig.volume;
414                 if (events & 0x2000) {
415                         if (vol < 100) vol++;
416                 } else {
417                         if (vol >   0) vol--;
418                 }
419                 FrameworkAudio_SetVolume(vol, vol);
420                 sprintf(noticeMsg, "VOL: %02i ", vol);
421                 noticeMsgTime = GetTickCount();
422                 currentConfig.volume = vol;
423         }
424
425         events &= ~prevEvents;
426         if (events) RunEvents(events);
427         if (movie_data) emu_updateMovie();
428
429         prevEvents = (allActions[0] | allActions[1]) >> 16;
430 }
431
432 void plat_debug_cat(char *str)
433 {
434 }
435
436 static void simpleWait(DWORD until)
437 {
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);
448 }
449
450 void pemu_loop(void)
451 {
452         static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
453         char fpsbuff[24]; // fps count c string
454         DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
455         int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
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;
469
470         // pal/ntsc might have changed, reset related stuff
471         target_fps = Pico.m.pal ? 50 : 60;
472         target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
473         reset_timing = 1;
474
475         // prepare CD buffer
476         if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
477
478         // prepare sound stuff
479         PsndOut = NULL;
480         if (currentConfig.EmuOpt & 4)
481         {
482                 int ret, snd_excess_add, stereo;
483                 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
484                         PsndRerate(Pico.m.frame_count ? 1 : 0);
485                 }
486                 stereo=(PicoOpt&8)>>3;
487                 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
488                 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
489                 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
490                         PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
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                 }
507         }
508
509         // loop?
510         while (engineState == PGS_Running)
511         {
512                 int modes;
513
514                 tval = GetTickCount();
515                 if (reset_timing || tval < tval_prev) {
516                         //stdbg("timing reset");
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) {
539                         oldmodes = modes;
540                         clearArea(1);
541                 }
542
543                 // second passed?
544                 if (tval - tval_thissec >= sec_ms)
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
560                         //tval_thissec += 1000;
561                         tval_thissec += sec_ms;
562
563                         if (PsndOut != NULL)
564                         {
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;
568                                 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
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;
586                                 target_frametime += adj;
587                                 sec_ms = (target_frametime * target_fps) >> 8;
588                                 //stdbg("%i %i %i", audio_skew, adj, sec_ms);
589                                 frames_done = frames_shown = 0;
590                         }
591                         else if (currentConfig.Frameskip < 0) {
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;
595                         } else {
596                                 frames_done = frames_shown = 0;
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();
609                                 SkipFrame(); frames_done++;
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();
635                                 SkipFrame(); frames_done++;
636                                 continue;
637                         }
638                 }
639
640                 updateKeys();
641
642                 if (currentConfig.EmuOpt&0x80)
643                         /* be sure correct framebuffer is locked */
644                         giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
645
646                 PicoFrame();
647
648                 if (giz_screen == NULL)
649                         giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
650
651                 blit(fpsbuff, notice);
652
653                 if (giz_screen != NULL) {
654                         fb_unlock();
655                         giz_screen = NULL;
656                 }
657
658                 if (currentConfig.EmuOpt&0x2000)
659                         Framework2D_WaitVSync();
660
661                 if (currentConfig.EmuOpt&0x8000)
662                         fb_flip();
663
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
684         if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
685
686         if (PsndOut != NULL) {
687                 PsndOut = snd_cbuff = NULL;
688                 FrameworkAudio_Close();
689         }
690
691         // save SRAM
692         if ((currentConfig.EmuOpt & 1) && SRam.changed) {
693                 emu_stateCb("Writing SRAM/BRAM..");
694                 emu_save_load_game(0, 1);
695                 SRam.changed = 0;
696         }
697 }
698