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