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