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