e5f426aa |
1 | #include <windows.h> |
ea8c405f |
2 | #include <string.h> |
e5f426aa |
3 | |
ea8c405f |
4 | #include "kgsdk/Framework2D.h" |
5 | #include "kgsdk/FrameworkAudio.h" |
6 | #include "../common/emu.h" |
7 | #include "../common/lprintf.h" |
8 | #include "../common/arm_utils.h" |
e5f426aa |
9 | #include "emu.h" |
ea8c405f |
10 | #include "menu.h" |
11 | #include "giz.h" |
12 | #include "asm_utils.h" |
e5f426aa |
13 | |
ea8c405f |
14 | #include <Pico/PicoInt.h> |
15 | |
16 | #ifdef BENCHMARK |
17 | #define OSD_FPS_X 220 |
18 | #else |
19 | #define OSD_FPS_X 260 |
20 | #endif |
21 | |
22 | // main 300K gfx-related buffer. Used by menu and renderers. |
23 | unsigned char gfx_buffer[321*240*2*2]; |
e5f426aa |
24 | char romFileName[MAX_PATH]; |
25 | int engineState; |
e5f426aa |
26 | |
ea8c405f |
27 | unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) ) |
e5f426aa |
28 | int reset_timing = 0; |
ea8c405f |
29 | |
30 | static DWORD noticeMsgTime = 0; |
31 | static int osd_fps_x; |
32 | |
33 | |
34 | static void blit(const char *fps, const char *notice); |
35 | static void clearArea(int full); |
36 | |
37 | void emu_noticeMsgUpdated(void) |
38 | { |
39 | noticeMsgTime = GetTickCount(); |
40 | } |
41 | |
42 | void emu_getMainDir(char *dst, int len) |
43 | { |
44 | if (len > 0) *dst = 0; |
45 | } |
46 | |
47 | static void emu_msg_cb(const char *msg) |
48 | { |
49 | if (giz_screen == NULL) |
50 | giz_screen = Framework2D_LockBuffer(); |
51 | |
52 | memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4); |
53 | emu_textOut16(4, 232, msg); |
54 | noticeMsgTime = GetTickCount() - 2000; |
55 | |
56 | /* assumption: emu_msg_cb gets called only when something slow is about to happen */ |
57 | reset_timing = 1; |
58 | } |
59 | |
60 | static void emu_state_cb(const char *str) |
61 | { |
62 | clearArea(0); |
63 | blit("", str); |
64 | } |
65 | |
66 | static void emu_msg_tray_open(void) |
67 | { |
68 | strcpy(noticeMsg, "CD tray opened"); |
69 | noticeMsgTime = GetTickCount(); |
70 | } |
71 | |
72 | |
73 | void emu_Init(void) |
74 | { |
75 | // make dirs for saves, cfgs, etc. |
76 | CreateDirectory(L"mds", NULL); |
77 | CreateDirectory(L"srm", NULL); |
78 | CreateDirectory(L"brm", NULL); |
79 | CreateDirectory(L"cfg", NULL); |
80 | |
81 | PicoInit(); |
82 | PicoMessage = emu_msg_cb; |
83 | PicoMCDopenTray = emu_msg_tray_open; |
84 | PicoMCDcloseTray = menu_loop_tray; |
85 | } |
86 | |
87 | void emu_Deinit(void) |
88 | { |
89 | // save SRAM |
90 | if((currentConfig.EmuOpt & 1) && SRam.changed) { |
91 | emu_SaveLoadGame(0, 1); |
92 | SRam.changed = 0; |
93 | } |
94 | |
95 | if (!(currentConfig.EmuOpt & 0x20)) { |
96 | FILE *f = fopen(PicoConfigFile, "r+b"); |
97 | if (!f) emu_WriteConfig(0); |
98 | else { |
99 | // if we already have config, reload it, except last ROM |
100 | fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET); |
101 | fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f); |
102 | fseek(f, 0, SEEK_SET); |
103 | fwrite(¤tConfig, 1, sizeof(currentConfig), f); |
104 | fflush(f); |
105 | fclose(f); |
106 | } |
107 | } |
108 | |
109 | PicoExit(); |
110 | } |
111 | |
112 | void emu_setDefaultConfig(void) |
113 | { |
114 | memset(¤tConfig, 0, sizeof(currentConfig)); |
115 | currentConfig.lastRomFile[0] = 0; |
116 | currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds |
117 | currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda |
118 | currentConfig.PsndRate = 22050; |
119 | currentConfig.PicoRegion = 0; // auto |
120 | currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP |
121 | currentConfig.Frameskip = -1; // auto |
122 | currentConfig.volume = 50; |
123 | currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU |
124 | currentConfig.KeyBinds[ 3] = 1<<1; |
125 | currentConfig.KeyBinds[ 0] = 1<<2; |
126 | currentConfig.KeyBinds[ 1] = 1<<3; |
127 | currentConfig.KeyBinds[ 5] = 1<<4; |
128 | currentConfig.KeyBinds[ 6] = 1<<5; |
129 | currentConfig.KeyBinds[ 7] = 1<<6; |
130 | currentConfig.KeyBinds[ 8] = 1<<7; |
131 | currentConfig.KeyBinds[ 4] = 1<<26; // switch rend |
132 | currentConfig.KeyBinds[ 8] = 1<<27; // save state |
133 | currentConfig.KeyBinds[ 9] = 1<<28; // load state |
134 | currentConfig.KeyBinds[12] = 1<<29; // vol up |
135 | currentConfig.KeyBinds[11] = 1<<30; // vol down |
136 | currentConfig.PicoCDBuffers = 64; |
137 | currentConfig.scaling = 0; |
138 | } |
139 | |
140 | |
141 | static int EmuScan16(unsigned int num, void *sdata) |
142 | { |
143 | if (!(Pico.video.reg[1]&8)) num += 8; |
144 | DrawLineDest = (unsigned short *) giz_screen + 321*(num+1); |
145 | |
146 | return 0; |
147 | } |
148 | |
149 | static int EmuScan8(unsigned int num, void *sdata) |
150 | { |
151 | // draw like the fast renderer |
152 | if (!(Pico.video.reg[1]&8)) num += 8; |
153 | HighCol = gfx_buffer + 328*8 + 328*(num+1); |
154 | |
155 | return 0; |
156 | } |
157 | |
158 | static void osd_text(int x, int y, const char *text) |
159 | { |
160 | int len = strlen(text) * 8; |
161 | int *p, i, h; |
162 | len = (len+1) >> 1; |
163 | for (h = 0; h < 8; h++) { |
164 | p = (int *) ((unsigned short *) giz_screen+x+321*(y+h)); |
165 | p = (int *) ((int)p & ~3); // align |
166 | for (i = len; i; i--, p++) *p = 0; |
167 | } |
168 | emu_textOut16(x, y, text); |
169 | } |
170 | |
171 | |
172 | short localPal[0x100]; |
173 | static void (*vidCpy8to16)(void *dest, void *src, short *pal, int lines) = NULL; |
174 | |
175 | static void blit(const char *fps, const char *notice) |
176 | { |
177 | int emu_opt = currentConfig.EmuOpt; |
178 | |
179 | if (PicoOpt&0x10) { |
180 | // 8bit fast renderer |
181 | if (Pico.m.dirtyPal) { |
182 | Pico.m.dirtyPal = 0; |
183 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
184 | } |
185 | vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224); |
186 | } else if (!(emu_opt&0x80)) { |
187 | // 8bit accurate renderer |
188 | if (Pico.m.dirtyPal) { |
189 | Pico.m.dirtyPal = 0; |
190 | vidConvCpyRGB565(localPal, Pico.cram, 0x40); |
191 | if(Pico.video.reg[0xC]&8) { // shadow/hilight mode |
192 | //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40); |
193 | //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO |
194 | blockcpy(localPal+0xc0, localPal+0x40, 0x40*4); |
195 | localPal[0xc0] = 0x0600; |
196 | localPal[0xd0] = 0xc000; |
197 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
198 | localPal[0xf0] = 0xffff; |
199 | } |
200 | /* no support |
201 | else if (rendstatus & 0x20) { // mid-frame palette changes |
202 | vidConvCpyRGB565(localPal+0x40, HighPal, 0x40); |
203 | vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40); |
204 | } */ |
205 | } |
206 | // TODO... |
207 | vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224); |
208 | } |
209 | |
210 | if (notice || (emu_opt & 2)) { |
211 | int h = 232; |
212 | if (notice) osd_text(4, h, notice); |
213 | if (emu_opt & 2) osd_text(osd_fps_x, h, fps); |
214 | } |
215 | // if ((emu_opt & 0x400) && (PicoMCD & 1)) |
216 | // cd_leds(); |
217 | |
218 | //gp2x_video_wait_vsync(); |
219 | |
220 | if (!(PicoOpt&0x10)) { |
221 | if (Pico.video.reg[1] & 8) { |
222 | if (currentConfig.EmuOpt&0x80) |
223 | DrawLineDest = (unsigned short *) giz_screen; |
224 | else |
225 | HighCol = gfx_buffer; |
226 | } else { |
227 | if (currentConfig.EmuOpt&0x80) |
228 | DrawLineDest = (unsigned short *) giz_screen + 320*8; |
229 | else |
230 | HighCol = gfx_buffer + 328*8; |
231 | } |
232 | } |
233 | } |
234 | |
235 | // clears whole screen or just the notice area (in all buffers) |
236 | static void clearArea(int full) |
237 | { |
238 | if (giz_screen == NULL) |
239 | giz_screen = Framework2D_LockBuffer(); |
240 | if (full) memset32(giz_screen, 0, 320*240*2/4); |
241 | else memset32((int *)((char *)giz_screen + 320*232*2), 0, 320*8*2/4); |
242 | } |
243 | |
244 | static void vidResetMode(void) |
245 | { |
246 | void *screen; |
247 | if (PicoOpt&0x10) { |
248 | } else if (currentConfig.EmuOpt&0x80) { |
249 | PicoDrawSetColorFormat(1); |
250 | PicoScan = EmuScan16; |
251 | } else { |
252 | PicoDrawSetColorFormat(0); |
253 | PicoScan = EmuScan8; |
254 | } |
255 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) { |
256 | // setup pal for 8-bit modes |
257 | localPal[0xc0] = 0x0600; |
258 | localPal[0xd0] = 0xc000; |
259 | localPal[0xe0] = 0x0000; // reserved pixels for OSD |
260 | localPal[0xf0] = 0xffff; |
261 | } |
262 | Pico.m.dirtyPal = 1; |
263 | screen = Framework2D_LockBuffer(); |
264 | memset32(screen, 0, 320*240*2/4); |
265 | Framework2D_UnlockBuffer(); |
266 | giz_screen = NULL; |
267 | } |
268 | |
269 | |
270 | static void SkipFrame(int do_audio) |
271 | { |
272 | PicoSkipFrame=do_audio ? 1 : 2; |
273 | PicoFrame(); |
274 | PicoSkipFrame=0; |
275 | } |
276 | |
277 | void emu_forcedFrame(void) |
278 | { |
279 | // TODO |
280 | } |
281 | |
282 | static void updateKeys(void) |
283 | { |
284 | } |
285 | |
286 | static void simpleWait(DWORD until) |
287 | { |
288 | } |
289 | |
290 | void emu_Loop(void) |
291 | { |
292 | //static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0; |
293 | char fpsbuff[24]; // fps count c string |
294 | DWORD tval, tval_prev = 0, tval_thissec = 0; // timing |
295 | int frames_done = 0, frames_shown = 0, oldmodes = 0; |
296 | int target_fps, target_frametime, lim_time, tval_diff, i; |
297 | char *notice = NULL; |
298 | |
299 | lprintf("entered emu_Loop()\n"); |
300 | |
301 | fpsbuff[0] = 0; |
302 | |
303 | // make sure we are in correct mode |
304 | vidResetMode(); |
305 | if (currentConfig.scaling) PicoOpt|=0x4000; |
306 | else PicoOpt&=~0x4000; |
307 | Pico.m.dirtyPal = 1; |
308 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc; |
309 | //find_combos(); // TODO |
310 | |
311 | // pal/ntsc might have changed, reset related stuff |
312 | target_fps = Pico.m.pal ? 50 : 60; |
313 | target_frametime = (1000<<8)/target_fps; |
314 | reset_timing = 1; |
315 | |
316 | // prepare sound stuff |
317 | /* if (currentConfig.EmuOpt & 4) { |
318 | int snd_excess_add; |
319 | if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) { |
320 | sound_rerate(Pico.m.frame_count ? 1 : 0); |
321 | } |
322 | snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps; |
323 | lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", |
324 | PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal); |
325 | gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3); |
326 | gp2x_sound_volume(currentConfig.volume, currentConfig.volume); |
327 | PicoWriteSound = updateSound; |
328 | memset(sndBuffer, 0, sizeof(sndBuffer)); |
329 | PsndOut = sndBuffer; |
330 | PsndRate_old = PsndRate; |
331 | PsndLen_real = PsndLen; |
332 | PicoOpt_old = PicoOpt; |
333 | pal_old = Pico.m.pal; |
334 | } else*/ { |
335 | PsndOut = 0; |
336 | } |
337 | |
338 | // prepare CD buffer |
339 | if (PicoMCD & 1) PicoCDBufferInit(); |
340 | |
341 | // loop? |
342 | while (engineState == PGS_Running) |
343 | { |
344 | int modes; |
345 | |
346 | tval = GetTickCount(); |
347 | if (reset_timing || tval < tval_prev) { |
348 | reset_timing = 0; |
349 | tval_thissec = tval; |
350 | frames_shown = frames_done = 0; |
351 | } |
352 | |
353 | // show notice message? |
354 | if (noticeMsgTime) { |
355 | static int noticeMsgSum; |
356 | if (tval - noticeMsgTime > 2000) { // > 2.0 sec |
357 | noticeMsgTime = 0; |
358 | clearArea(0); |
359 | notice = 0; |
360 | } else { |
361 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2]; |
362 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; } |
363 | notice = noticeMsg; |
364 | } |
365 | } |
366 | |
367 | // check for mode changes |
368 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8); |
369 | if (modes != oldmodes) { |
370 | osd_fps_x = OSD_FPS_X; |
371 | //if (modes & 4) |
372 | vidCpy8to16 = vidCpy8to16_40; |
373 | //else |
374 | // vidCpy8to16 = vidCpy8to16_32col; |
375 | oldmodes = modes; |
376 | clearArea(1); |
377 | } |
378 | |
379 | // second passed? |
380 | if (tval - tval_thissec >= 1000) |
381 | { |
382 | #ifdef BENCHMARK |
383 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4]; |
384 | if(++bench == 10) { |
385 | bench = 0; |
386 | bench_fps_s = bench_fps; |
387 | bf[bfp++ & 3] = bench_fps; |
388 | bench_fps = 0; |
389 | } |
390 | bench_fps += frames_shown; |
391 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2); |
392 | #else |
393 | if(currentConfig.EmuOpt & 2) |
394 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done); |
395 | #endif |
396 | tval_thissec = tval; |
397 | |
398 | if (PsndOut == 0 && currentConfig.Frameskip >= 0) { |
399 | frames_done = frames_shown = 0; |
400 | } else { |
401 | // it is quite common for this implementation to leave 1 fame unfinished |
402 | // when second changes, but we don't want buffer to starve. |
403 | if (PsndOut && frames_done < target_fps && frames_done > target_fps-5) { |
404 | updateKeys(); |
405 | SkipFrame(1); frames_done++; |
406 | } |
407 | |
408 | frames_done -= target_fps; if (frames_done < 0) frames_done = 0; |
409 | frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0; |
410 | if (frames_shown > frames_done) frames_shown = frames_done; |
411 | } |
412 | } |
413 | #ifdef PFRAMES |
414 | sprintf(fpsbuff, "%i", Pico.m.frame_count); |
415 | #endif |
416 | |
417 | tval_prev = tval; |
418 | lim_time = (frames_done+1) * target_frametime; |
419 | if (currentConfig.Frameskip >= 0) // frameskip enabled |
420 | { |
421 | for (i = 0; i < currentConfig.Frameskip; i++) { |
422 | updateKeys(); |
423 | SkipFrame(1); frames_done++; |
424 | if (PsndOut) { // do framelimitting if sound is enabled |
425 | int tval_diff; |
426 | tval = GetTickCount(); |
427 | tval_diff = (int)(tval - tval_thissec) << 8; |
428 | if (tval_diff < lim_time) // we are too fast |
429 | simpleWait(tval + ((lim_time - tval_diff)>>8)); |
430 | } |
431 | lim_time += target_frametime; |
432 | } |
433 | } |
434 | else // auto frameskip |
435 | { |
436 | int tval_diff; |
437 | tval = GetTickCount(); |
438 | tval_diff = (int)(tval - tval_thissec) << 8; |
439 | if (tval_diff > lim_time) |
440 | { |
441 | // no time left for this frame - skip |
442 | if (tval_diff - lim_time >= (300<<8)) { |
443 | /* something caused a slowdown for us (disk access? cache flush?) |
444 | * try to recover by resetting timing... */ |
445 | reset_timing = 1; |
446 | continue; |
447 | } |
448 | updateKeys(); |
449 | SkipFrame(tval_diff < lim_time+target_frametime*2); frames_done++; |
450 | continue; |
451 | } |
452 | } |
453 | |
454 | updateKeys(); |
455 | |
456 | if (giz_screen == NULL) |
457 | giz_screen = Framework2D_LockBuffer(); |
458 | |
459 | PicoFrame(); |
460 | blit(fpsbuff, notice); |
461 | |
462 | if (giz_screen != NULL) { |
463 | Framework2D_UnlockBuffer(); |
464 | giz_screen = NULL; |
465 | } |
466 | |
467 | // check time |
468 | tval = GetTickCount(); |
469 | tval_diff = (int)(tval - tval_thissec) << 8; |
470 | |
471 | if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection |
472 | reset_timing = 1; |
473 | else if (PsndOut != NULL || currentConfig.Frameskip < 0) |
474 | { |
475 | // sleep if we are still too fast |
476 | if (tval_diff < lim_time) |
477 | { |
478 | // we are too fast |
479 | simpleWait(tval + ((lim_time - tval_diff) >> 8)); |
480 | } |
481 | } |
482 | |
483 | frames_done++; frames_shown++; |
484 | } |
485 | |
486 | |
487 | if (PicoMCD & 1) PicoCDBufferFree(); |
488 | |
489 | // save SRAM |
490 | if ((currentConfig.EmuOpt & 1) && SRam.changed) { |
491 | emu_state_cb("Writing SRAM/BRAM.."); |
492 | emu_SaveLoadGame(0, 1); |
493 | SRam.changed = 0; |
494 | } |
495 | } |
496 | |
497 | |
498 | void emu_ResetGame(void) |
499 | { |
500 | PicoReset(0); |
501 | reset_timing = 1; |
502 | } |
e5f426aa |
503 | |