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