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