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