portable documentation
[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"
e5f426aa 13#include "emu.h"
ea8c405f 14#include "menu.h"
15#include "giz.h"
16#include "asm_utils.h"
e5f426aa 17
ea8c405f 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];
e5f426aa 28char romFileName[MAX_PATH];
29int engineState;
e5f426aa 30
ea8c405f 31unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
e5f426aa 32int reset_timing = 0;
ea8c405f 33
fa283c9a 34static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
ea8c405f 35static DWORD noticeMsgTime = 0;
fa283c9a 36static short *snd_cbuff = NULL;
37static int snd_cbuf_samples = 0, snd_all_samples = 0;
ea8c405f 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{
9839d126 68 if (giz_screen == NULL)
69 giz_screen = Framework2D_LockBuffer();
70
ea8c405f 71 clearArea(0);
72 blit("", str);
9839d126 73
74 Framework2D_UnlockBuffer();
75 giz_screen = NULL;
da42200b 76
77 Sleep(0); /* yield the CPU, the system may need it */
ea8c405f 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.
2ec14aec 90 mkdir("mds", 0777);
91 mkdir("srm", 0777);
92 mkdir("brm", 0777);
93 mkdir("cfg", 0777);
ea8c405f 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;
9839d126 130 currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend
fa283c9a 131 currentConfig.PicoOpt = 0x07 | 0xc00; // | cd_pcm, cd_cdda
ea8c405f 132 currentConfig.PsndRate = 22050;
133 currentConfig.PicoRegion = 0; // auto
134 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
fa283c9a 135 currentConfig.Frameskip = -1; // auto
ea8c405f 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;
2ec14aec 144 currentConfig.KeyBinds[ 4] = 1<<7;
a8869ad1 145 currentConfig.KeyBinds[13] = 1<<26; // switch rend
ea8c405f 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
fa283c9a 150 currentConfig.PicoCDBuffers = 0;
ea8c405f 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
a8869ad1 160 if ((currentConfig.EmuOpt&0x4000) && (num&1) == (Pico.m.frame_count&1))
161 return 1; // skip next line
162
ea8c405f 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;
2ec14aec 170 HighCol = gfx_buffer + 328*(num+1);
ea8c405f 171
172 return 0;
173}
174
175static void osd_text(int x, int y, const char *text)
176{
499a0be3 177 int len = strlen(text) * 8 / 2;
178 int *p, h;
ea8c405f 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
499a0be3 182 memset32(p, 0, len);
ea8c405f 183 }
184 emu_textOut16(x, y, text);
185}
186
499a0be3 187/*
188void log1(void *p1, void *p2)
189{
190 lprintf("%p %p %p\n", p1, p2, DrawLineDest);
191}
192*/
ea8c405f 193
9839d126 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];
ea8c405f 212
213static void blit(const char *fps, const char *notice)
214{
215 int emu_opt = currentConfig.EmuOpt;
216
9839d126 217 if (PicoOpt&0x10)
218 {
219 int lines_flags = 224;
ea8c405f 220 // 8bit fast renderer
221 if (Pico.m.dirtyPal) {
222 Pico.m.dirtyPal = 0;
223 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
224 }
a8869ad1 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;
9839d126 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;
ea8c405f 233 // 8bit accurate renderer
234 if (Pico.m.dirtyPal) {
235 Pico.m.dirtyPal = 0;
236 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
2ec14aec 237 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
ea8c405f 238 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
9839d126 239 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
2ec14aec 240 blockcpy(localPal+0xc0, localPal+0x40, 0x40*2);
ea8c405f 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 }
9839d126 252 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
a8869ad1 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;
9839d126 256 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
ea8c405f 257 }
258
259 if (notice || (emu_opt & 2)) {
260 int h = 232;
261 if (notice) osd_text(4, h, notice);
9839d126 262 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
ea8c405f 263 }
ea8c405f 264
9839d126 265 if ((emu_opt & 0x400) && (PicoMCD & 1))
266 cd_leds();
ea8c405f 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);
499a0be3 275 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
ea8c405f 276}
277
278static void vidResetMode(void)
279{
499a0be3 280 giz_screen = Framework2D_LockBuffer();
281
ea8c405f 282 if (PicoOpt&0x10) {
283 } else if (currentConfig.EmuOpt&0x80) {
284 PicoDrawSetColorFormat(1);
285 PicoScan = EmuScan16;
286 } else {
499a0be3 287 PicoDrawSetColorFormat(-1);
ea8c405f 288 PicoScan = EmuScan8;
289 }
499a0be3 290 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
ea8c405f 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;
499a0be3 298
299 memset32(giz_screen, 0, 321*240*2/4);
ea8c405f 300 Framework2D_UnlockBuffer();
301 giz_screen = NULL;
302}
303
da42200b 304/*
fa283c9a 305#include <stdarg.h>
306static void stdbg(const char *fmt, ...)
ea8c405f 307{
fa283c9a 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}
da42200b 318*/
fa283c9a 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 {
da42200b 328 //if (PsndOut - snd_cbuff != snd_cbuf_samples)
329 // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
fa283c9a 330 PsndOut = snd_cbuff;
331 }
332}
333
334
335static void SkipFrame(void)
336{
337 PicoSkipFrame=1;
ea8c405f 338 PicoFrame();
339 PicoSkipFrame=0;
340}
341
342void emu_forcedFrame(void)
343{
fa283c9a 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;
ea8c405f 365}
366
fa283c9a 367
9839d126 368static void RunEvents(unsigned int which)
369{
da42200b 370 if (which & 0x1800) // save or load (but not both)
371 {
9839d126 372 int do_it = 1;
da42200b 373
374 if (PsndOut != NULL)
375 FrameworkAudio_SetPause(1);
376 if (giz_screen == NULL)
377 giz_screen = Framework2D_LockBuffer();
9839d126 378 if ( emu_checkSaveFile(state_slot) &&
a8869ad1 379 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
380 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
381 {
9839d126 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;
da42200b 387 while( ((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) ) // wait for release
388 Sleep(50);
9839d126 389 clearArea(0);
390 }
da42200b 391
392 if (do_it)
393 {
9839d126 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;
da42200b 398 Sleep(0);
9839d126 399 }
400
da42200b 401 if (PsndOut != NULL)
402 FrameworkAudio_SetPause(0);
9839d126 403 reset_timing = 1;
404 }
da42200b 405 if (which & 0x0400) // switch renderer
406 {
a8869ad1 407 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
408 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
9839d126 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 }
da42200b 422 if (which & 0x0300)
423 {
9839d126 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
ea8c405f 436static void updateKeys(void)
437{
2ec14aec 438 unsigned int keys, allActions[2] = { 0, 0 }, events;
439 static unsigned int prevEvents = 0;
440 int i;
441
442 keys = Framework_PollGetButtons();
fa283c9a 443 if (keys & BTN_HOME)
2ec14aec 444 engineState = PGS_Menu;
2ec14aec 445
446 keys &= CONFIGURABLE_KEYS;
447
448 for (i = 0; i < 32; i++)
449 {
fa283c9a 450 if (keys & (1 << i))
451 {
2ec14aec 452 int pl, acts = currentConfig.KeyBinds[i];
453 if (!acts) continue;
454 pl = (acts >> 16) & 1;
fa283c9a 455 if (combo_keys & (1 << i))
456 {
2ec14aec 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;
fa283c9a 469 } else {
2ec14aec 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
fa283c9a 481 if ((events & 0x6000) && PsndOut != NULL)
482 {
2ec14aec 483 int vol = currentConfig.volume;
484 if (events & 0x2000) {
485 if (vol < 100) vol++;
486 } else {
487 if (vol > 0) vol--;
488 }
fa283c9a 489 FrameworkAudio_SetVolume(vol, vol);
2ec14aec 490 sprintf(noticeMsg, "VOL: %02i", vol);
491 noticeMsgTime = GetTickCount();
492 currentConfig.volume = vol;
493 }
494
495 events &= ~prevEvents;
9839d126 496 if (events) RunEvents(events);
2ec14aec 497 if (movie_data) emu_updateMovie();
498
499 prevEvents = (allActions[0] | allActions[1]) >> 16;
ea8c405f 500}
501
fa283c9a 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
ea8c405f 531static void simpleWait(DWORD until)
532{
9839d126 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);
ea8c405f 543}
544
545void emu_Loop(void)
546{
fa283c9a 547 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
ea8c405f 548 char fpsbuff[24]; // fps count c string
549 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
fa283c9a 550 int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
ea8c405f 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;
fa283c9a 564 find_combos();
ea8c405f 565
566 // pal/ntsc might have changed, reset related stuff
567 target_fps = Pico.m.pal ? 50 : 60;
fa283c9a 568 target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
ea8c405f 569 reset_timing = 1;
570
fa283c9a 571 // prepare CD buffer
572 if (PicoMCD & 1) PicoCDBufferInit();
573
ea8c405f 574 // prepare sound stuff
fa283c9a 575 PsndOut = NULL;
576 if (currentConfig.EmuOpt & 4)
577 {
da42200b 578 int ret, snd_excess_add, stereo;
ea8c405f 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 }
da42200b 582 stereo=(PicoOpt&8)>>3;
ea8c405f 583 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
fa283c9a 584 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
ea8c405f 585 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
f3d1de29 586 PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
fa283c9a 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 }
ea8c405f 603 }
604
ea8c405f 605 // loop?
606 while (engineState == PGS_Running)
607 {
608 int modes;
609
610 tval = GetTickCount();
611 if (reset_timing || tval < tval_prev) {
da42200b 612 //stdbg("timing reset");
ea8c405f 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) {
ea8c405f 635 oldmodes = modes;
636 clearArea(1);
637 }
638
639 // second passed?
fa283c9a 640 if (tval - tval_thissec >= sec_ms)
ea8c405f 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
fa283c9a 656 //tval_thissec += 1000;
657 tval_thissec += sec_ms;
ea8c405f 658
fa283c9a 659 if (PsndOut != NULL)
660 {
f3d1de29 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;
fa283c9a 664 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
f3d1de29 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;
fa283c9a 682 target_frametime += adj;
683 sec_ms = (target_frametime * target_fps) >> 8;
da42200b 684 //stdbg("%i %i %i", audio_skew, adj, sec_ms);
ea8c405f 685 frames_done = frames_shown = 0;
fa283c9a 686 }
687 else if (currentConfig.Frameskip < 0) {
ea8c405f 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;
fa283c9a 691 } else {
692 frames_done = frames_shown = 0;
ea8c405f 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();
fa283c9a 705 SkipFrame(); frames_done++;
ea8c405f 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();
fa283c9a 731 SkipFrame(); frames_done++;
ea8c405f 732 continue;
733 }
734 }
735
736 updateKeys();
737
499a0be3 738 if (giz_screen == NULL && (currentConfig.EmuOpt&0x80))
ea8c405f 739 giz_screen = Framework2D_LockBuffer();
499a0be3 740 if (!(PicoOpt&0x10))
741 PicoScan((unsigned) -1, NULL);
ea8c405f 742
743 PicoFrame();
499a0be3 744
a8869ad1 745 if (currentConfig.EmuOpt&0x2000)
746 Framework2D_WaitVSync();
747
499a0be3 748 if (giz_screen == NULL)
749 giz_screen = Framework2D_LockBuffer();
750
ea8c405f 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
fa283c9a 780 if (PsndOut != NULL) {
781 PsndOut = snd_cbuff = NULL;
782 FrameworkAudio_Close();
783 }
784
ea8c405f 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}
e5f426aa 799