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