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