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