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