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