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