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