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. |
27 | unsigned char gfx_buffer[321*240*2*2]; |
e5f426aa |
28 | char romFileName[MAX_PATH]; |
29 | int engineState; |
e5f426aa |
30 | |
ea8c405f |
31 | unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) ) |
e5f426aa |
32 | int reset_timing = 0; |
ea8c405f |
33 | |
34 | static DWORD noticeMsgTime = 0; |
ea8c405f |
35 | |
36 | |
37 | static void blit(const char *fps, const char *notice); |
38 | static void clearArea(int full); |
39 | |
40 | void emu_noticeMsgUpdated(void) |
41 | { |
42 | noticeMsgTime = GetTickCount(); |
43 | } |
44 | |
45 | void emu_getMainDir(char *dst, int len) |
46 | { |
47 | if (len > 0) *dst = 0; |
48 | } |
49 | |
50 | static void emu_msg_cb(const char *msg) |
51 | { |
52 | if (giz_screen == NULL) |
53 | giz_screen = Framework2D_LockBuffer(); |
54 | |
55 | memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
56 | emu_textOut16(4, 232, msg); |
57 | noticeMsgTime = GetTickCount() - 2000; |
58 | |
59 | /* assumption: emu_msg_cb gets called only when something slow is about to happen */ |
60 | reset_timing = 1; |
61 | } |
62 | |
63 | static void emu_state_cb(const char *str) |
64 | { |
9839d126 |
65 | if (giz_screen == NULL) |
66 | giz_screen = Framework2D_LockBuffer(); |
67 | |
ea8c405f |
68 | clearArea(0); |
69 | blit("", str); |
9839d126 |
70 | |
71 | Framework2D_UnlockBuffer(); |
72 | giz_screen = NULL; |
ea8c405f |
73 | } |
74 | |
75 | static void emu_msg_tray_open(void) |
76 | { |
77 | strcpy(noticeMsg, "CD tray opened"); |
78 | noticeMsgTime = GetTickCount(); |
79 | } |
80 | |
81 | |
82 | void emu_Init(void) |
83 | { |
84 | // make dirs for saves, cfgs, etc. |
2ec14aec |
85 | mkdir("mds", 0777); |
86 | mkdir("srm", 0777); |
87 | mkdir("brm", 0777); |
88 | mkdir("cfg", 0777); |
ea8c405f |
89 | |
90 | PicoInit(); |
91 | PicoMessage = emu_msg_cb; |
92 | PicoMCDopenTray = emu_msg_tray_open; |
93 | PicoMCDcloseTray = menu_loop_tray; |
94 | } |
95 | |
96 | void emu_Deinit(void) |
97 | { |
98 | // save SRAM |
99 | if((currentConfig.EmuOpt & 1) && SRam.changed) { |
100 | emu_SaveLoadGame(0, 1); |
101 | SRam.changed = 0; |
102 | } |
103 | |
104 | if (!(currentConfig.EmuOpt & 0x20)) { |
105 | FILE *f = fopen(PicoConfigFile, "r+b"); |
106 | if (!f) emu_WriteConfig(0); |
107 | else { |
108 | // if we already have config, reload it, except last ROM |
109 | fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET); |
110 | fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f); |
111 | fseek(f, 0, SEEK_SET); |
112 | fwrite(¤tConfig, 1, sizeof(currentConfig), f); |
113 | fflush(f); |
114 | fclose(f); |
115 | } |
116 | } |
117 | |
118 | PicoExit(); |
119 | } |
120 | |
121 | void emu_setDefaultConfig(void) |
122 | { |
123 | memset(¤tConfig, 0, sizeof(currentConfig)); |
124 | currentConfig.lastRomFile[0] = 0; |
9839d126 |
125 | currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend |
ea8c405f |
126 | currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda |
127 | currentConfig.PsndRate = 22050; |
128 | currentConfig.PicoRegion = 0; // auto |
129 | currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP |
fd34fdd0 |
130 | currentConfig.Frameskip = 0;//-1; // auto |
ea8c405f |
131 | currentConfig.volume = 50; |
132 | currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU |
133 | currentConfig.KeyBinds[ 3] = 1<<1; |
134 | currentConfig.KeyBinds[ 0] = 1<<2; |
135 | currentConfig.KeyBinds[ 1] = 1<<3; |
136 | currentConfig.KeyBinds[ 5] = 1<<4; |
137 | currentConfig.KeyBinds[ 6] = 1<<5; |
138 | currentConfig.KeyBinds[ 7] = 1<<6; |
2ec14aec |
139 | currentConfig.KeyBinds[ 4] = 1<<7; |
9839d126 |
140 | currentConfig.KeyBinds[13] = 1<<26; // switch rend |
ea8c405f |
141 | currentConfig.KeyBinds[ 8] = 1<<27; // save state |
142 | currentConfig.KeyBinds[ 9] = 1<<28; // load state |
143 | currentConfig.KeyBinds[12] = 1<<29; // vol up |
144 | currentConfig.KeyBinds[11] = 1<<30; // vol down |
145 | currentConfig.PicoCDBuffers = 64; |
146 | currentConfig.scaling = 0; |
147 | } |
148 | |
149 | |
150 | static int EmuScan16(unsigned int num, void *sdata) |
151 | { |
152 | if (!(Pico.video.reg[1]&8)) num += 8; |
153 | DrawLineDest = (unsigned short *) giz_screen + 321*(num+1); |
154 | |
155 | return 0; |
156 | } |
157 | |
158 | static int EmuScan8(unsigned int num, void *sdata) |
159 | { |
160 | // draw like the fast renderer |
161 | if (!(Pico.video.reg[1]&8)) num += 8; |
2ec14aec |
162 | HighCol = gfx_buffer + 328*(num+1); |
ea8c405f |
163 | |
164 | return 0; |
165 | } |
166 | |
167 | static void osd_text(int x, int y, const char *text) |
168 | { |
499a0be3 |
169 | int len = strlen(text) * 8 / 2; |
170 | int *p, h; |
ea8c405f |
171 | for (h = 0; h < 8; h++) { |
172 | p = (int *) ((unsigned short *) giz_screen+x+321*(y+h)); |
173 | p = (int *) ((int)p & ~3); // align |
499a0be3 |
174 | memset32(p, 0, len); |
ea8c405f |
175 | } |
176 | emu_textOut16(x, y, text); |
177 | } |
178 | |
499a0be3 |
179 | /* |
180 | void log1(void *p1, void *p2) |
181 | { |
182 | lprintf("%p %p %p\n", p1, p2, DrawLineDest); |
183 | } |
184 | */ |
ea8c405f |
185 | |
9839d126 |
186 | static void cd_leds(void) |
187 | { |
188 | static int old_reg = 0; |
189 | unsigned int col_g, col_r, *p; |
190 | |
191 | if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change |
192 | old_reg = Pico_mcd->s68k_regs[0]; |
193 | |
194 | p = (unsigned int *)((short *)giz_screen + 321*2+4+2); |
195 | col_g = (old_reg & 2) ? 0x06000600 : 0; |
196 | col_r = (old_reg & 1) ? 0xc000c000 : 0; |
197 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1; |
198 | *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2; |
199 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; |
200 | } |
201 | |
202 | |
203 | static short localPal[0x100]; |
ea8c405f |
204 | |
205 | static void blit(const char *fps, const char *notice) |
206 | { |
207 | int emu_opt = currentConfig.EmuOpt; |
208 | |
9839d126 |
209 | if (PicoOpt&0x10) |
210 | { |
211 | int lines_flags = 224; |
ea8c405f |
212 | // 8bit fast renderer |
213 | if (Pico.m.dirtyPal) { |
214 | Pico.m.dirtyPal = 0; |
215 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
216 | } |
9839d126 |
217 | if (!(Pico.video.reg[12]&1)) lines_flags|=0x100; |
218 | vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags); |
219 | } |
220 | else if (!(emu_opt&0x80)) |
221 | { |
222 | int lines_flags; |
ea8c405f |
223 | // 8bit accurate renderer |
224 | if (Pico.m.dirtyPal) { |
225 | Pico.m.dirtyPal = 0; |
226 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
2ec14aec |
227 | if (Pico.video.reg[0xC]&8) { // shadow/hilight mode |
ea8c405f |
228 | //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40); |
9839d126 |
229 | //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO? |
2ec14aec |
230 | blockcpy(localPal+0xc0, localPal+0x40, 0x40*2); |
ea8c405f |
231 | localPal[0xc0] = 0x0600; |
232 | localPal[0xd0] = 0xc000; |
233 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
234 | localPal[0xf0] = 0xffff; |
235 | } |
236 | /* no support |
237 | else if (rendstatus & 0x20) { // mid-frame palette changes |
238 | vidConvCpyRGB565(localPal+0x40, HighPal, 0x40); |
239 | vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40); |
240 | } */ |
241 | } |
9839d126 |
242 | lines_flags = (Pico.video.reg[1]&8) ? 240 : 224; |
243 | if (!(Pico.video.reg[12]&1)) lines_flags|=0x100; |
244 | vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags); |
ea8c405f |
245 | } |
246 | |
247 | if (notice || (emu_opt & 2)) { |
248 | int h = 232; |
249 | if (notice) osd_text(4, h, notice); |
9839d126 |
250 | if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps); |
ea8c405f |
251 | } |
ea8c405f |
252 | |
9839d126 |
253 | if ((emu_opt & 0x400) && (PicoMCD & 1)) |
254 | cd_leds(); |
ea8c405f |
255 | } |
256 | |
257 | // clears whole screen or just the notice area (in all buffers) |
258 | static void clearArea(int full) |
259 | { |
260 | if (giz_screen == NULL) |
261 | giz_screen = Framework2D_LockBuffer(); |
262 | if (full) memset32(giz_screen, 0, 320*240*2/4); |
499a0be3 |
263 | else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
ea8c405f |
264 | } |
265 | |
266 | static void vidResetMode(void) |
267 | { |
499a0be3 |
268 | giz_screen = Framework2D_LockBuffer(); |
269 | |
ea8c405f |
270 | if (PicoOpt&0x10) { |
271 | } else if (currentConfig.EmuOpt&0x80) { |
272 | PicoDrawSetColorFormat(1); |
273 | PicoScan = EmuScan16; |
274 | } else { |
499a0be3 |
275 | PicoDrawSetColorFormat(-1); |
ea8c405f |
276 | PicoScan = EmuScan8; |
277 | } |
499a0be3 |
278 | if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) { |
ea8c405f |
279 | // setup pal for 8-bit modes |
280 | localPal[0xc0] = 0x0600; |
281 | localPal[0xd0] = 0xc000; |
282 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
283 | localPal[0xf0] = 0xffff; |
284 | } |
285 | Pico.m.dirtyPal = 1; |
499a0be3 |
286 | |
287 | memset32(giz_screen, 0, 321*240*2/4); |
ea8c405f |
288 | Framework2D_UnlockBuffer(); |
289 | giz_screen = NULL; |
290 | } |
291 | |
292 | |
293 | static void SkipFrame(int do_audio) |
294 | { |
295 | PicoSkipFrame=do_audio ? 1 : 2; |
296 | PicoFrame(); |
297 | PicoSkipFrame=0; |
298 | } |
299 | |
300 | void emu_forcedFrame(void) |
301 | { |
302 | // TODO |
303 | } |
304 | |
9839d126 |
305 | static void RunEvents(unsigned int which) |
306 | { |
307 | if (which & 0x1800) { // save or load (but not both) |
308 | int do_it = 1; |
309 | if ( emu_checkSaveFile(state_slot) && |
310 | (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load |
311 | (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save |
312 | int keys; |
313 | blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)"); |
314 | while( !((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) ) |
315 | Sleep(50); |
316 | if (keys & BTN_STOP) do_it = 0; |
317 | clearArea(0); |
318 | } |
319 | if (do_it) { |
320 | osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME"); |
321 | PicoStateProgressCB = emu_state_cb; |
322 | emu_SaveLoadGame((which & 0x1000) >> 12, 0); |
323 | PicoStateProgressCB = NULL; |
324 | } |
325 | |
326 | reset_timing = 1; |
327 | } |
328 | if (which & 0x0400) { // switch renderer |
329 | if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; } |
330 | else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10; |
331 | else currentConfig.EmuOpt &= ~0x80; |
332 | |
333 | vidResetMode(); |
334 | |
335 | if (PicoOpt&0x10) { |
336 | strcpy(noticeMsg, " 8bit fast renderer"); |
337 | } else if (currentConfig.EmuOpt&0x80) { |
338 | strcpy(noticeMsg, "16bit accurate renderer"); |
339 | } else { |
340 | strcpy(noticeMsg, " 8bit accurate renderer"); |
341 | } |
342 | |
343 | noticeMsgTime = GetTickCount(); |
344 | } |
345 | if (which & 0x0300) { |
346 | if(which&0x0200) { |
347 | state_slot -= 1; |
348 | if(state_slot < 0) state_slot = 9; |
349 | } else { |
350 | state_slot += 1; |
351 | if(state_slot > 9) state_slot = 0; |
352 | } |
353 | sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE"); |
354 | noticeMsgTime = GetTickCount(); |
355 | } |
356 | } |
357 | |
ea8c405f |
358 | static void updateKeys(void) |
359 | { |
2ec14aec |
360 | unsigned int keys, allActions[2] = { 0, 0 }, events; |
361 | static unsigned int prevEvents = 0; |
362 | int i; |
363 | |
364 | keys = Framework_PollGetButtons(); |
365 | if (keys & BTN_HOME) { |
366 | engineState = PGS_Menu; |
367 | // wait until select is released, so menu would not resume game |
368 | while (Framework_PollGetButtons() & BTN_HOME) Sleep(50); |
369 | } |
370 | |
371 | keys &= CONFIGURABLE_KEYS; |
372 | |
373 | for (i = 0; i < 32; i++) |
374 | { |
375 | if (keys & (1 << i)) { |
376 | int pl, acts = currentConfig.KeyBinds[i]; |
377 | if (!acts) continue; |
378 | pl = (acts >> 16) & 1; |
379 | /* TODO if (combo_keys & (1 << i)) { |
380 | int u = i+1, acts_c = acts & combo_acts; |
381 | // let's try to find the other one |
382 | if (acts_c) |
383 | for (; u < 32; u++) |
384 | if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) { |
385 | allActions[pl] |= acts_c; |
386 | keys &= ~((1 << i) | (1 << u)); |
387 | break; |
388 | } |
389 | // add non-combo actions if combo ones were not found |
390 | if (!acts_c || u == 32) |
391 | allActions[pl] |= acts & ~combo_acts; |
392 | } else */ { |
393 | allActions[pl] |= acts; |
394 | } |
395 | } |
396 | } |
397 | |
398 | PicoPad[0] = (unsigned short) allActions[0]; |
399 | PicoPad[1] = (unsigned short) allActions[1]; |
400 | |
401 | events = (allActions[0] | allActions[1]) >> 16; |
402 | |
403 | // volume is treated in special way and triggered every frame |
404 | if (events & 0x6000) { |
405 | int vol = currentConfig.volume; |
406 | if (events & 0x2000) { |
407 | if (vol < 100) vol++; |
408 | } else { |
409 | if (vol > 0) vol--; |
410 | } |
411 | //gp2x_sound_volume(vol, vol); |
412 | sprintf(noticeMsg, "VOL: %02i", vol); |
413 | noticeMsgTime = GetTickCount(); |
414 | currentConfig.volume = vol; |
415 | } |
416 | |
417 | events &= ~prevEvents; |
9839d126 |
418 | if (events) RunEvents(events); |
2ec14aec |
419 | if (movie_data) emu_updateMovie(); |
420 | |
421 | prevEvents = (allActions[0] | allActions[1]) >> 16; |
ea8c405f |
422 | } |
423 | |
424 | static void simpleWait(DWORD until) |
425 | { |
9839d126 |
426 | DWORD tval; |
427 | int diff; |
428 | |
429 | tval = GetTickCount(); |
430 | diff = (int)until - (int)tval; |
431 | if (diff >= 2) |
432 | Sleep(diff - 1); |
433 | |
434 | while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection |
435 | spend_cycles(1024*2); |
ea8c405f |
436 | } |
437 | |
438 | void emu_Loop(void) |
439 | { |
440 | //static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0; |
441 | char fpsbuff[24]; // fps count c string |
442 | DWORD tval, tval_prev = 0, tval_thissec = 0; // timing |
443 | int frames_done = 0, frames_shown = 0, oldmodes = 0; |
444 | int target_fps, target_frametime, lim_time, tval_diff, i; |
445 | char *notice = NULL; |
446 | |
447 | lprintf("entered emu_Loop()\n"); |
448 | |
449 | fpsbuff[0] = 0; |
450 | |
451 | // make sure we are in correct mode |
452 | vidResetMode(); |
453 | if (currentConfig.scaling) PicoOpt|=0x4000; |
454 | else PicoOpt&=~0x4000; |
455 | Pico.m.dirtyPal = 1; |
456 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc; |
457 | //find_combos(); // TODO |
458 | |
459 | // pal/ntsc might have changed, reset related stuff |
460 | target_fps = Pico.m.pal ? 50 : 60; |
461 | target_frametime = (1000<<8)/target_fps; |
462 | reset_timing = 1; |
463 | |
464 | // prepare sound stuff |
465 | /* if (currentConfig.EmuOpt & 4) { |
466 | int snd_excess_add; |
467 | if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) { |
468 | sound_rerate(Pico.m.frame_count ? 1 : 0); |
469 | } |
470 | snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps; |
471 | lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", |
472 | PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal); |
473 | gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3); |
474 | gp2x_sound_volume(currentConfig.volume, currentConfig.volume); |
475 | PicoWriteSound = updateSound; |
476 | memset(sndBuffer, 0, sizeof(sndBuffer)); |
477 | PsndOut = sndBuffer; |
478 | PsndRate_old = PsndRate; |
479 | PsndLen_real = PsndLen; |
480 | PicoOpt_old = PicoOpt; |
481 | pal_old = Pico.m.pal; |
482 | } else*/ { |
483 | PsndOut = 0; |
484 | } |
485 | |
486 | // prepare CD buffer |
487 | if (PicoMCD & 1) PicoCDBufferInit(); |
488 | |
489 | // loop? |
490 | while (engineState == PGS_Running) |
491 | { |
492 | int modes; |
493 | |
494 | tval = GetTickCount(); |
495 | if (reset_timing || tval < tval_prev) { |
496 | reset_timing = 0; |
497 | tval_thissec = tval; |
498 | frames_shown = frames_done = 0; |
499 | } |
500 | |
501 | // show notice message? |
502 | if (noticeMsgTime) { |
503 | static int noticeMsgSum; |
504 | if (tval - noticeMsgTime > 2000) { // > 2.0 sec |
505 | noticeMsgTime = 0; |
506 | clearArea(0); |
507 | notice = 0; |
508 | } else { |
509 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2]; |
510 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; } |
511 | notice = noticeMsg; |
512 | } |
513 | } |
514 | |
515 | // check for mode changes |
516 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8); |
517 | if (modes != oldmodes) { |
ea8c405f |
518 | oldmodes = modes; |
519 | clearArea(1); |
520 | } |
521 | |
522 | // second passed? |
523 | if (tval - tval_thissec >= 1000) |
524 | { |
525 | #ifdef BENCHMARK |
526 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4]; |
527 | if(++bench == 10) { |
528 | bench = 0; |
529 | bench_fps_s = bench_fps; |
530 | bf[bfp++ & 3] = bench_fps; |
531 | bench_fps = 0; |
532 | } |
533 | bench_fps += frames_shown; |
534 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2); |
535 | #else |
536 | if(currentConfig.EmuOpt & 2) |
537 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done); |
538 | #endif |
539 | tval_thissec = tval; |
540 | |
541 | if (PsndOut == 0 && currentConfig.Frameskip >= 0) { |
542 | frames_done = frames_shown = 0; |
543 | } else { |
544 | // it is quite common for this implementation to leave 1 fame unfinished |
545 | // when second changes, but we don't want buffer to starve. |
546 | if (PsndOut && frames_done < target_fps && frames_done > target_fps-5) { |
547 | updateKeys(); |
548 | SkipFrame(1); frames_done++; |
549 | } |
550 | |
551 | frames_done -= target_fps; if (frames_done < 0) frames_done = 0; |
552 | frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0; |
553 | if (frames_shown > frames_done) frames_shown = frames_done; |
554 | } |
555 | } |
556 | #ifdef PFRAMES |
557 | sprintf(fpsbuff, "%i", Pico.m.frame_count); |
558 | #endif |
559 | |
560 | tval_prev = tval; |
561 | lim_time = (frames_done+1) * target_frametime; |
562 | if (currentConfig.Frameskip >= 0) // frameskip enabled |
563 | { |
564 | for (i = 0; i < currentConfig.Frameskip; i++) { |
565 | updateKeys(); |
566 | SkipFrame(1); frames_done++; |
567 | if (PsndOut) { // do framelimitting if sound is enabled |
568 | int tval_diff; |
569 | tval = GetTickCount(); |
570 | tval_diff = (int)(tval - tval_thissec) << 8; |
571 | if (tval_diff < lim_time) // we are too fast |
572 | simpleWait(tval + ((lim_time - tval_diff)>>8)); |
573 | } |
574 | lim_time += target_frametime; |
575 | } |
576 | } |
577 | else // auto frameskip |
578 | { |
579 | int tval_diff; |
580 | tval = GetTickCount(); |
581 | tval_diff = (int)(tval - tval_thissec) << 8; |
582 | if (tval_diff > lim_time) |
583 | { |
584 | // no time left for this frame - skip |
585 | if (tval_diff - lim_time >= (300<<8)) { |
586 | /* something caused a slowdown for us (disk access? cache flush?) |
587 | * try to recover by resetting timing... */ |
588 | reset_timing = 1; |
589 | continue; |
590 | } |
591 | updateKeys(); |
592 | SkipFrame(tval_diff < lim_time+target_frametime*2); frames_done++; |
593 | continue; |
594 | } |
595 | } |
596 | |
597 | updateKeys(); |
598 | |
499a0be3 |
599 | if (giz_screen == NULL && (currentConfig.EmuOpt&0x80)) |
ea8c405f |
600 | giz_screen = Framework2D_LockBuffer(); |
499a0be3 |
601 | if (!(PicoOpt&0x10)) |
602 | PicoScan((unsigned) -1, NULL); |
ea8c405f |
603 | |
604 | PicoFrame(); |
499a0be3 |
605 | |
606 | if (giz_screen == NULL) |
607 | giz_screen = Framework2D_LockBuffer(); |
608 | |
ea8c405f |
609 | blit(fpsbuff, notice); |
610 | |
611 | if (giz_screen != NULL) { |
612 | Framework2D_UnlockBuffer(); |
613 | giz_screen = NULL; |
614 | } |
615 | |
616 | // check time |
617 | tval = GetTickCount(); |
618 | tval_diff = (int)(tval - tval_thissec) << 8; |
619 | |
620 | if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection |
621 | reset_timing = 1; |
622 | else if (PsndOut != NULL || currentConfig.Frameskip < 0) |
623 | { |
624 | // sleep if we are still too fast |
625 | if (tval_diff < lim_time) |
626 | { |
627 | // we are too fast |
628 | simpleWait(tval + ((lim_time - tval_diff) >> 8)); |
629 | } |
630 | } |
631 | |
632 | frames_done++; frames_shown++; |
633 | } |
634 | |
635 | |
636 | if (PicoMCD & 1) PicoCDBufferFree(); |
637 | |
638 | // save SRAM |
639 | if ((currentConfig.EmuOpt & 1) && SRam.changed) { |
640 | emu_state_cb("Writing SRAM/BRAM.."); |
641 | emu_SaveLoadGame(0, 1); |
642 | SRam.changed = 0; |
643 | } |
644 | } |
645 | |
646 | |
647 | void emu_ResetGame(void) |
648 | { |
649 | PicoReset(0); |
650 | reset_timing = 1; |
651 | } |
e5f426aa |
652 | |