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