wav support, better mp3 length handling using .cue
[libpicofe.git] / gp2x / emu.c
... / ...
CommitLineData
1// (c) Copyright 2006-2007 notaz, All rights reserved.\r
2// Free for non-commercial use.\r
3\r
4// For commercial use, separate licencing terms must be obtained.\r
5\r
6#include <stdio.h>\r
7#include <stdlib.h>\r
8#include <sys/time.h>\r
9#include <sys/stat.h>\r
10#include <sys/types.h>\r
11#include <linux/limits.h>\r
12#include <ctype.h>\r
13#include <unistd.h>\r
14\r
15#include <stdarg.h>\r
16\r
17#include "emu.h"\r
18#include "gp2x.h"\r
19#include "usbjoy.h"\r
20#include "menu.h"\r
21#include "../common/arm_utils.h"\r
22#include "../common/fonts.h"\r
23#include "../common/emu.h"\r
24#include "../common/config.h"\r
25#include "cpuctrl.h"\r
26\r
27#include <Pico/PicoInt.h>\r
28#include <Pico/Patch.h>\r
29#include <Pico/sound/mix.h>\r
30#include <zlib/zlib.h>\r
31\r
32//#define PFRAMES\r
33\r
34#ifdef BENCHMARK\r
35#define OSD_FPS_X 220\r
36#else\r
37#define OSD_FPS_X 260\r
38#endif\r
39\r
40\r
41int engineState;\r
42int select_exits = 0;\r
43\r
44char romFileName[PATH_MAX];\r
45\r
46extern int crashed_940;\r
47\r
48static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
49static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r
50static int osd_fps_x;\r
51static int gp2x_old_gamma = 100;\r
52char noticeMsg[64]; // notice msg to draw\r
53unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer\r
54int reset_timing = 0;\r
55\r
56static void emu_msg_cb(const char *msg);\r
57static void emu_msg_tray_open(void);\r
58\r
59\r
60void emu_noticeMsgUpdated(void)\r
61{\r
62 gettimeofday(&noticeMsgTime, 0);\r
63}\r
64\r
65void emu_getMainDir(char *dst, int len)\r
66{\r
67 extern char **g_argv;\r
68 int j;\r
69\r
70 strncpy(dst, g_argv[0], len);\r
71 len -= 32; // reserve\r
72 if (len < 0) len = 0;\r
73 dst[len] = 0;\r
74 for (j = strlen(dst); j > 0; j--)\r
75 if (dst[j] == '/') { dst[j+1] = 0; break; }\r
76}\r
77\r
78void emu_Init(void)\r
79{\r
80 // make temp buffer for alt renderer\r
81 PicoDraw2FB = malloc((8+320)*(8+240+8));\r
82 if (!PicoDraw2FB)\r
83 {\r
84 printf("PicoDraw2FB == 0\n");\r
85 }\r
86\r
87 // make dirs for saves, cfgs, etc.\r
88 mkdir("mds", 0777);\r
89 mkdir("srm", 0777);\r
90 mkdir("brm", 0777);\r
91 mkdir("cfg", 0777);\r
92\r
93 PicoInit();\r
94 PicoMessage = emu_msg_cb;\r
95 PicoMCDopenTray = emu_msg_tray_open;\r
96 PicoMCDcloseTray = menu_loop_tray;\r
97}\r
98\r
99\r
100static void scaling_update(void)\r
101{\r
102 PicoOpt &= ~0x4100;\r
103 switch (currentConfig.scaling) {\r
104 default: break; // off\r
105 case 1: // hw hor\r
106 case 2: PicoOpt |= 0x0100; break; // hw hor+vert\r
107 case 3: PicoOpt |= 0x4000; break; // sw hor\r
108 }\r
109}\r
110\r
111\r
112void emu_Deinit(void)\r
113{\r
114 // save SRAM\r
115 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
116 emu_SaveLoadGame(0, 1);\r
117 SRam.changed = 0;\r
118 }\r
119\r
120 if (!(currentConfig.EmuOpt & 0x20)) {\r
121 config_writelrom(PicoConfigFile);\r
122#ifndef NO_SYNC\r
123 sync();\r
124#endif\r
125 }\r
126\r
127 free(PicoDraw2FB);\r
128\r
129 PicoExit();\r
130\r
131 // restore gamma\r
132 if (gp2x_old_gamma != 100)\r
133 set_gamma(100, 0);\r
134}\r
135\r
136void emu_prepareDefaultConfig(void)\r
137{\r
138 memset(&defaultConfig, 0, sizeof(defaultConfig));\r
139 defaultConfig.EmuOpt = 0x1d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r
140 defaultConfig.s_PicoOpt = 0x0f | 0x20e00; // | <- use_940, cd_pcm, cd_cdda, svp drc\r
141 defaultConfig.s_PsndRate = 44100;\r
142 defaultConfig.s_PicoRegion = 0; // auto\r
143 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
144 defaultConfig.s_PicoCDBuffers = 0;\r
145 defaultConfig.Frameskip = -1; // auto\r
146 defaultConfig.CPUclock = 200;\r
147 defaultConfig.volume = 50;\r
148 defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
149 defaultConfig.KeyBinds[ 4] = 1<<1;\r
150 defaultConfig.KeyBinds[ 2] = 1<<2;\r
151 defaultConfig.KeyBinds[ 6] = 1<<3;\r
152 defaultConfig.KeyBinds[14] = 1<<4;\r
153 defaultConfig.KeyBinds[13] = 1<<5;\r
154 defaultConfig.KeyBinds[12] = 1<<6;\r
155 defaultConfig.KeyBinds[ 8] = 1<<7;\r
156 defaultConfig.KeyBinds[15] = 1<<26; // switch rend\r
157 defaultConfig.KeyBinds[10] = 1<<27; // save state\r
158 defaultConfig.KeyBinds[11] = 1<<28; // load state\r
159 defaultConfig.KeyBinds[23] = 1<<29; // vol up\r
160 defaultConfig.KeyBinds[22] = 1<<30; // vol down\r
161 defaultConfig.gamma = 100;\r
162 defaultConfig.scaling = 0;\r
163}\r
164\r
165void emu_setDefaultConfig(void)\r
166{\r
167 memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));\r
168 PicoOpt = currentConfig.s_PicoOpt;\r
169 PsndRate = currentConfig.s_PsndRate;\r
170 PicoRegionOverride = currentConfig.s_PicoRegion;\r
171 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
172 PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r
173}\r
174\r
175void osd_text(int x, int y, const char *text)\r
176{\r
177 int len = strlen(text)*8;\r
178\r
179 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
180 int *p, i, h;\r
181 x &= ~3; // align x\r
182 len = (len+3) >> 2;\r
183 for (h = 0; h < 8; h++) {\r
184 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r
185 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
186 }\r
187 emu_textOut8(x, y, text);\r
188 } else {\r
189 int *p, i, h;\r
190 x &= ~1; // align x\r
191 len = (len+1) >> 1;\r
192 for (h = 0; h < 8; h++) {\r
193 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));\r
194 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r
195 }\r
196 emu_textOut16(x, y, text);\r
197 }\r
198}\r
199\r
200static void cd_leds(void)\r
201{\r
202// static\r
203 int old_reg;\r
204// if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r
205 old_reg = Pico_mcd->s68k_regs[0];\r
206\r
207 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
208 // 8-bit modes\r
209 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
210 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
211 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =\r
212 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =\r
213 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;\r
214 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =\r
215 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =\r
216 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;\r
217 } else {\r
218 // 16-bit modes\r
219 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);\r
220 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r
221 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r
222 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
223 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
224 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r
225 }\r
226}\r
227\r
228static int EmuScanBegin16(unsigned int num)\r
229{\r
230 if (!(Pico.video.reg[1]&8)) num += 8;\r
231 DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;\r
232\r
233 return 0;\r
234}\r
235\r
236static int EmuScanBegin8(unsigned int num)\r
237{\r
238 if (!(Pico.video.reg[1]&8)) num += 8;\r
239 DrawLineDest = (unsigned char *) gp2x_screen + 320 * num;\r
240\r
241 return 0;\r
242}\r
243\r
244int localPal[0x100];\r
245static void (*vidCpyM2)(void *dest, void *src) = NULL;\r
246\r
247static void blit(const char *fps, const char *notice)\r
248{\r
249 int emu_opt = currentConfig.EmuOpt;\r
250\r
251 if (PicoOpt&0x10)\r
252 {\r
253 // 8bit fast renderer\r
254 if (Pico.m.dirtyPal) {\r
255 Pico.m.dirtyPal = 0;\r
256 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
257 // feed new palette to our device\r
258 gp2x_video_setpalette(localPal, 0x40);\r
259 }\r
260 // a hack for VR\r
261 if (PicoRead16Hook == PicoSVPRead16)\r
262 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
263 // do actual copy\r
264 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
265 }\r
266 else if (!(emu_opt&0x80))\r
267 {\r
268 // 8bit accurate renderer\r
269 if (Pico.m.dirtyPal)\r
270 {\r
271 Pico.m.dirtyPal = 0;\r
272 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\r
273 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
274 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
275 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r
276 memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r
277 localPal[0xc0] = 0x0000c000;\r
278 localPal[0xd0] = 0x00c00000;\r
279 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
280 localPal[0xf0] = 0x00ffffff;\r
281 gp2x_video_setpalette(localPal, 0x100);\r
282 } else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
283 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
284 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
285 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
286 gp2x_video_setpalette(localPal, 0xc0);\r
287 } else {\r
288 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
289 gp2x_video_setpalette(localPal, 0x40);\r
290 }\r
291 }\r
292 }\r
293\r
294 if (notice || (emu_opt & 2)) {\r
295 int h = 232;\r
296 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r
297 if (notice) osd_text(4, h, notice);\r
298 if (emu_opt & 2)\r
299 osd_text(osd_fps_x, h, fps);\r
300 }\r
301 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
302 cd_leds();\r
303\r
304 //gp2x_video_wait_vsync();\r
305 gp2x_video_flip();\r
306\r
307 if (!(PicoOpt&0x10)) {\r
308 if (!(Pico.video.reg[1]&8)) {\r
309 if (currentConfig.EmuOpt&0x80) {\r
310 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;\r
311 } else {\r
312 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;\r
313 }\r
314 } else {\r
315 DrawLineDest = gp2x_screen;\r
316 }\r
317 }\r
318}\r
319\r
320\r
321// clears whole screen or just the notice area (in all buffers)\r
322static void clearArea(int full)\r
323{\r
324 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
325 // 8-bit renderers\r
326 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
327 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
328 } else {\r
329 // 16bit accurate renderer\r
330 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r
331 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
332 }\r
333}\r
334\r
335\r
336static void vidResetMode(void)\r
337{\r
338 if (PicoOpt&0x10) {\r
339 gp2x_video_changemode(8);\r
340 } else if (currentConfig.EmuOpt&0x80) {\r
341 gp2x_video_changemode(16);\r
342 PicoDrawSetColorFormat(1);\r
343 PicoScanBegin = EmuScanBegin16;\r
344 } else {\r
345 gp2x_video_changemode(8);\r
346 PicoDrawSetColorFormat(2);\r
347 PicoScanBegin = EmuScanBegin8;\r
348 }\r
349 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
350 // setup pal for 8-bit modes\r
351 localPal[0xc0] = 0x0000c000; // MCD LEDs\r
352 localPal[0xd0] = 0x00c00000;\r
353 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
354 localPal[0xf0] = 0x00ffffff;\r
355 gp2x_video_setpalette(localPal, 0x100);\r
356 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
357 gp2x_video_flip();\r
358 }\r
359 Pico.m.dirtyPal = 1;\r
360 // reset scaling\r
361 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r
362 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r
363 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
364}\r
365\r
366\r
367static void emu_msg_cb(const char *msg)\r
368{\r
369 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
370 // 8-bit renderers\r
371 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
372 osd_text(4, 232, msg);\r
373 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);\r
374 } else {\r
375 // 16bit accurate renderer\r
376 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
377 osd_text(4, 232, msg);\r
378 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);\r
379 }\r
380 gettimeofday(&noticeMsgTime, 0);\r
381 noticeMsgTime.tv_sec -= 2;\r
382\r
383 /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r
384 reset_timing = 1;\r
385}\r
386\r
387static void emu_state_cb(const char *str)\r
388{\r
389 clearArea(0);\r
390 blit("", str);\r
391}\r
392\r
393static void emu_msg_tray_open(void)\r
394{\r
395 strcpy(noticeMsg, "CD tray opened");\r
396 gettimeofday(&noticeMsgTime, 0);\r
397}\r
398\r
399static void update_volume(int has_changed, int is_up)\r
400{\r
401 static int prev_frame = 0, wait_frames = 0;\r
402 int vol = currentConfig.volume;\r
403\r
404 if (has_changed)\r
405 {\r
406 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
407 wait_frames++;\r
408 else {\r
409 if (is_up) {\r
410 if (vol < 99) vol++;\r
411 } else {\r
412 if (vol > 0) vol--;\r
413 }\r
414 wait_frames = 0;\r
415 gp2x_sound_volume(vol, vol);\r
416 currentConfig.volume = vol;\r
417 }\r
418 sprintf(noticeMsg, "VOL: %02i", vol);\r
419 gettimeofday(&noticeMsgTime, 0);\r
420 prev_frame = Pico.m.frame_count;\r
421 }\r
422\r
423 // set the right mixer func\r
424 if (!(PicoOpt&8)) return; // just use defaults for mono\r
425 if (vol >= 5)\r
426 PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
427 else {\r
428 mix_32_to_16l_level = 5 - vol;\r
429 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
430 }\r
431}\r
432\r
433static void change_fast_forward(int set_on)\r
434{\r
435 static void *set_PsndOut = NULL;\r
436 static int set_Frameskip, set_EmuOpt, is_on = 0;\r
437\r
438 if (set_on && !is_on) {\r
439 set_PsndOut = PsndOut;\r
440 set_Frameskip = currentConfig.Frameskip;\r
441 set_EmuOpt = currentConfig.EmuOpt;\r
442 PsndOut = NULL;\r
443 currentConfig.Frameskip = 8;\r
444 currentConfig.EmuOpt &= ~4;\r
445 is_on = 1;\r
446 }\r
447 else if (!set_on && is_on) {\r
448 PsndOut = set_PsndOut;\r
449 currentConfig.Frameskip = set_Frameskip;\r
450 currentConfig.EmuOpt = set_EmuOpt;\r
451 PsndRerate(1);\r
452 update_volume(0, 0);\r
453 reset_timing = 1;\r
454 is_on = 0;\r
455 }\r
456}\r
457\r
458static void RunEvents(unsigned int which)\r
459{\r
460 if (which & 0x1800) // save or load (but not both)\r
461 {\r
462 int do_it = 1;\r
463 if ( emu_checkSaveFile(state_slot) &&\r
464 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load\r
465 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r
466 unsigned long keys;\r
467 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r
468 while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
469 usleep(50*1024);\r
470 if (keys & GP2X_X) do_it = 0;\r
471 while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release\r
472 usleep(50*1024);\r
473 clearArea(0);\r
474 }\r
475 if (do_it) {\r
476 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
477 PicoStateProgressCB = emu_state_cb;\r
478 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r
479 emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
480 PicoStateProgressCB = NULL;\r
481 }\r
482\r
483 reset_timing = 1;\r
484 }\r
485 if (which & 0x0400) // switch renderer\r
486 {\r
487 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
488 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
489 else currentConfig.EmuOpt &= ~0x80;\r
490\r
491 vidResetMode();\r
492\r
493 if (PicoOpt&0x10) {\r
494 strcpy(noticeMsg, " 8bit fast renderer");\r
495 } else if (currentConfig.EmuOpt&0x80) {\r
496 strcpy(noticeMsg, "16bit accurate renderer");\r
497 } else {\r
498 strcpy(noticeMsg, " 8bit accurate renderer");\r
499 }\r
500\r
501 gettimeofday(&noticeMsgTime, 0);\r
502 }\r
503 if (which & 0x0300)\r
504 {\r
505 if(which&0x0200) {\r
506 state_slot -= 1;\r
507 if(state_slot < 0) state_slot = 9;\r
508 } else {\r
509 state_slot += 1;\r
510 if(state_slot > 9) state_slot = 0;\r
511 }\r
512 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
513 gettimeofday(&noticeMsgTime, 0);\r
514 }\r
515 if (which & 0x0080) {\r
516 engineState = PGS_Menu;\r
517 }\r
518}\r
519\r
520\r
521static void updateKeys(void)\r
522{\r
523 unsigned long keys, allActions[2] = { 0, 0 }, events;\r
524 static unsigned long prevEvents = 0;\r
525 int joy, i;\r
526\r
527 keys = gp2x_joystick_read(0);\r
528 if (keys & GP2X_SELECT) {\r
529 engineState = select_exits ? PGS_Quit : PGS_Menu;\r
530 // wait until select is released, so menu would not resume game\r
531 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
532 }\r
533\r
534 keys &= CONFIGURABLE_KEYS;\r
535\r
536 for (i = 0; i < 32; i++)\r
537 {\r
538 if (keys & (1 << i))\r
539 {\r
540 int pl, acts = currentConfig.KeyBinds[i];\r
541 if (!acts) continue;\r
542 pl = (acts >> 16) & 1;\r
543 if (kb_combo_keys & (1 << i))\r
544 {\r
545 int u = i+1, acts_c = acts & kb_combo_acts;\r
546 // let's try to find the other one\r
547 if (acts_c) {\r
548 for (; u < 32; u++)\r
549 if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
550 allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r
551 keys &= ~((1 << i) | (1 << u));\r
552 break;\r
553 }\r
554 }\r
555 // add non-combo actions if combo ones were not found\r
556 if (!acts_c || u == 32)\r
557 allActions[pl] |= acts & ~kb_combo_acts;\r
558 } else {\r
559 allActions[pl] |= acts;\r
560 }\r
561 }\r
562 }\r
563\r
564 // add joy inputs\r
565 if (num_of_joys > 0)\r
566 {\r
567 gp2x_usbjoy_update();\r
568 for (joy = 0; joy < num_of_joys; joy++) {\r
569 int keys = gp2x_usbjoy_check2(joy);\r
570 for (i = 0; i < 32; i++) {\r
571 if (keys & (1 << i)) {\r
572 int acts = currentConfig.JoyBinds[joy][i];\r
573 int pl = (acts >> 16) & 1;\r
574 allActions[pl] |= acts;\r
575 }\r
576 }\r
577 }\r
578 }\r
579\r
580 PicoPad[0] = (unsigned short) allActions[0];\r
581 PicoPad[1] = (unsigned short) allActions[1];\r
582\r
583 events = (allActions[0] | allActions[1]) >> 16;\r
584\r
585 // volume is treated in special way and triggered every frame\r
586 if (events & 0x6000)\r
587 update_volume(1, events & 0x2000);\r
588\r
589 if ((events ^ prevEvents) & 0x40)\r
590 change_fast_forward(events & 0x40);\r
591\r
592 events &= ~prevEvents;\r
593 if (events) RunEvents(events);\r
594 if (movie_data) emu_updateMovie();\r
595\r
596 prevEvents = (allActions[0] | allActions[1]) >> 16;\r
597}\r
598\r
599\r
600static void updateSound(int len)\r
601{\r
602 if (PicoOpt&8) len<<=1;\r
603\r
604 /* avoid writing audio when lagging behind to prevent audio lag */\r
605 if (PicoSkipFrame != 2)\r
606 gp2x_sound_write(PsndOut, len<<1);\r
607}\r
608\r
609\r
610static void SkipFrame(int do_audio)\r
611{\r
612 PicoSkipFrame=do_audio ? 1 : 2;\r
613 PicoFrame();\r
614 PicoSkipFrame=0;\r
615}\r
616\r
617\r
618void emu_forcedFrame(void)\r
619{\r
620 int po_old = PicoOpt;\r
621 int eo_old = currentConfig.EmuOpt;\r
622\r
623 PicoOpt &= ~0x0010;\r
624 PicoOpt |= 0x4080; // soft_scale | acc_sprites\r
625 currentConfig.EmuOpt |= 0x80;\r
626\r
627 //vidResetMode();\r
628 PicoDrawSetColorFormat(1);\r
629 PicoScanBegin = EmuScanBegin16;\r
630 Pico.m.dirtyPal = 1;\r
631 PicoFrameDrawOnly();\r
632\r
633/*\r
634 if (!(Pico.video.reg[12]&1)) {\r
635 vidCpyM2 = vidCpyM2_32col;\r
636 clearArea(1);\r
637 } else vidCpyM2 = vidCpyM2_40col;\r
638\r
639 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
640 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
641 gp2x_video_setpalette(localPal, 0x40);\r
642*/\r
643 PicoOpt = po_old;\r
644 currentConfig.EmuOpt = eo_old;\r
645}\r
646\r
647static void simpleWait(int thissec, int lim_time)\r
648{\r
649 struct timeval tval;\r
650\r
651 spend_cycles(1024);\r
652 gettimeofday(&tval, 0);\r
653 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
654\r
655 while (tval.tv_usec < lim_time)\r
656 {\r
657 spend_cycles(1024);\r
658 gettimeofday(&tval, 0);\r
659 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
660 }\r
661}\r
662\r
663\r
664#if 0\r
665static void tga_dump(void)\r
666{\r
667#define BYTE unsigned char\r
668#define WORD unsigned short\r
669 struct\r
670 {\r
671 BYTE IDLength; /* 00h Size of Image ID field */\r
672 BYTE ColorMapType; /* 01h Color map type */\r
673 BYTE ImageType; /* 02h Image type code */\r
674 WORD CMapStart; /* 03h Color map origin */\r
675 WORD CMapLength; /* 05h Color map length */\r
676 BYTE CMapDepth; /* 07h Depth of color map entries */\r
677 WORD XOffset; /* 08h X origin of image */\r
678 WORD YOffset; /* 0Ah Y origin of image */\r
679 WORD Width; /* 0Ch Width of image */\r
680 WORD Height; /* 0Eh Height of image */\r
681 BYTE PixelDepth; /* 10h Image pixel size */\r
682 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
683 } __attribute__((packed)) TGAHEAD;\r
684 static unsigned short oldscr[320*240];\r
685 FILE *f; char name[128]; int i;\r
686\r
687 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
688 TGAHEAD.ImageType = 2;\r
689 TGAHEAD.Width = 320;\r
690 TGAHEAD.Height = 240;\r
691 TGAHEAD.PixelDepth = 16;\r
692 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
693\r
694#define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
695\r
696 for (i = 0; i < 320*240; i++)\r
697 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
698 if (i < 320*240)\r
699 {\r
700 for (i = 0; i < 320*240; i++)\r
701 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
702 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
703 f = fopen(name, "wb");\r
704 if (!f) { printf("!f\n"); exit(1); }\r
705 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
706 fwrite(oldscr, 1, 320*240*2, f);\r
707 fclose(f);\r
708 }\r
709}\r
710#endif\r
711\r
712\r
713void emu_Loop(void)\r
714{\r
715 static int gp2x_old_clock = 200;\r
716 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;\r
717 char fpsbuff[24]; // fps count c string\r
718 struct timeval tval; // timing\r
719 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
720 int frames_done, frames_shown, thissec; // actual frames\r
721 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
722 char *notice = 0;\r
723\r
724 printf("entered emu_Loop()\n");\r
725\r
726 if (gp2x_old_clock != currentConfig.CPUclock) {\r
727 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
728 set_FCLK(currentConfig.CPUclock);\r
729 gp2x_old_clock = currentConfig.CPUclock;\r
730 printf(" done\n");\r
731 }\r
732\r
733 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
734 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
735 gp2x_old_gamma = currentConfig.gamma;\r
736 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
737 }\r
738\r
739 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
740 if (currentConfig.EmuOpt&0x2000)\r
741 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
742 else unset_LCD_custom_rate();\r
743 }\r
744\r
745 EmuOpt_old = currentConfig.EmuOpt;\r
746 fpsbuff[0] = 0;\r
747\r
748 // make sure we are in correct mode\r
749 vidResetMode();\r
750 scaling_update();\r
751 Pico.m.dirtyPal = 1;\r
752 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
753 emu_findKeyBindCombos();\r
754\r
755 // pal/ntsc might have changed, reset related stuff\r
756 target_fps = Pico.m.pal ? 50 : 60;\r
757 target_frametime = 1000000/target_fps;\r
758 reset_timing = 1;\r
759\r
760 // prepare sound stuff\r
761 if (currentConfig.EmuOpt & 4)\r
762 {\r
763 int snd_excess_add;\r
764 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
765 ((PicoOpt&0x200) && crashed_940)) {\r
766 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
767 }\r
768 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
769 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
770 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
771 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
772 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
773 PicoWriteSound = updateSound;\r
774 update_volume(0, 0);\r
775 memset(sndBuffer, 0, sizeof(sndBuffer));\r
776 PsndOut = sndBuffer;\r
777 PsndRate_old = PsndRate;\r
778 PicoOpt_old = PicoOpt;\r
779 pal_old = Pico.m.pal;\r
780 } else {\r
781 PsndOut = NULL;\r
782 }\r
783\r
784 // prepare CD buffer\r
785 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
786\r
787 // calc vsync offset to sync timing code with vsync\r
788 if (currentConfig.EmuOpt&0x2000) {\r
789 gettimeofday(&tval, 0);\r
790 gp2x_video_wait_vsync();\r
791 gettimeofday(&tval, 0);\r
792 vsync_offset = tval.tv_usec;\r
793 while (vsync_offset >= target_frametime)\r
794 vsync_offset -= target_frametime;\r
795 if (!vsync_offset) vsync_offset++;\r
796 printf("vsync_offset: %i\n", vsync_offset);\r
797 } else\r
798 vsync_offset = 0;\r
799\r
800 frames_done = frames_shown = thissec =\r
801 pframes_done = pframes_shown = pthissec = 0;\r
802\r
803 // loop\r
804 while (engineState == PGS_Running)\r
805 {\r
806 int modes;\r
807\r
808 gettimeofday(&tval, 0);\r
809 if (reset_timing) {\r
810 reset_timing = 0;\r
811 pthissec = tval.tv_sec;\r
812 pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
813 }\r
814\r
815 // show notice message?\r
816 if (noticeMsgTime.tv_sec)\r
817 {\r
818 static int noticeMsgSum;\r
819 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
820 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
821 clearArea(0);\r
822 notice = 0;\r
823 } else {\r
824 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
825 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
826 notice = noticeMsg;\r
827 }\r
828 }\r
829\r
830 // check for mode changes\r
831 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
832 if (modes != oldmodes)\r
833 {\r
834 int scalex = 320;\r
835 osd_fps_x = OSD_FPS_X;\r
836 if (modes & 4) {\r
837 vidCpyM2 = vidCpyM2_40col;\r
838 } else {\r
839 if (PicoOpt & 0x100) {\r
840 vidCpyM2 = vidCpyM2_32col_nobord;\r
841 scalex = 256;\r
842 osd_fps_x = OSD_FPS_X - 64;\r
843 } else {\r
844 vidCpyM2 = vidCpyM2_32col;\r
845 }\r
846 }\r
847 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
848 gp2x_video_RGB_setscaling(8, scalex, 224);\r
849 else gp2x_video_RGB_setscaling(0, scalex, 240);\r
850 oldmodes = modes;\r
851 clearArea(1);\r
852 }\r
853\r
854 // second changed?\r
855 if (thissec != tval.tv_sec)\r
856 {\r
857#ifdef BENCHMARK\r
858 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
859 if (++bench == 10) {\r
860 bench = 0;\r
861 bench_fps_s = bench_fps;\r
862 bf[bfp++ & 3] = bench_fps;\r
863 bench_fps = 0;\r
864 }\r
865 bench_fps += frames_shown;\r
866 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
867#else\r
868 if (currentConfig.EmuOpt & 2) {\r
869 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
870 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
871 }\r
872#endif\r
873 frames_shown = frames_done = 0;\r
874 thissec = tval.tv_sec;\r
875 }\r
876#ifdef PFRAMES\r
877 sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
878#endif\r
879\r
880 if (pthissec != tval.tv_sec)\r
881 {\r
882 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
883 pframes_done = pframes_shown = 0;\r
884 } else {\r
885 // it is quite common for this implementation to leave 1 fame unfinished\r
886 // when second changes, but we don't want buffer to starve.\r
887 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
888 updateKeys();\r
889 SkipFrame(1); pframes_done++;\r
890 }\r
891\r
892 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;\r
893 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
894 if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
895 }\r
896 pthissec = tval.tv_sec;\r
897 }\r
898\r
899 lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
900 if (currentConfig.Frameskip >= 0) // frameskip enabled\r
901 {\r
902 for(i = 0; i < currentConfig.Frameskip; i++) {\r
903 updateKeys();\r
904 SkipFrame(1); pframes_done++; frames_done++;\r
905 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
906 gettimeofday(&tval, 0);\r
907 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
908 if (tval.tv_usec < lim_time) { // we are too fast\r
909 simpleWait(pthissec, lim_time);\r
910 }\r
911 }\r
912 lim_time += target_frametime;\r
913 }\r
914 }\r
915 else if (tval.tv_usec > lim_time) // auto frameskip\r
916 {\r
917 // no time left for this frame - skip\r
918 if (tval.tv_usec - lim_time >= 300000) {\r
919 /* something caused a slowdown for us (disk access? cache flush?)\r
920 * try to recover by resetting timing... */\r
921 reset_timing = 1;\r
922 continue;\r
923 }\r
924 updateKeys();\r
925 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
926 continue;\r
927 }\r
928\r
929 updateKeys();\r
930 PicoFrame();\r
931\r
932 // check time\r
933 gettimeofday(&tval, 0);\r
934 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
935\r
936 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
937 reset_timing = 1;\r
938 else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
939 {\r
940 // sleep or vsync if we are still too fast\r
941 // usleep sleeps for ~20ms minimum, so it is not a solution here\r
942 if (!reset_timing && tval.tv_usec < lim_time)\r
943 {\r
944 // we are too fast\r
945 if (vsync_offset) {\r
946 if (lim_time - tval.tv_usec > target_frametime/2)\r
947 simpleWait(pthissec, lim_time - target_frametime/4);\r
948 gp2x_video_wait_vsync();\r
949 } else {\r
950 simpleWait(pthissec, lim_time);\r
951 }\r
952 }\r
953 }\r
954\r
955 blit(fpsbuff, notice);\r
956\r
957 pframes_done++; pframes_shown++;\r
958 frames_done++; frames_shown++;\r
959 }\r
960\r
961 change_fast_forward(0);\r
962\r
963 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
964\r
965 // save SRAM\r
966 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
967 emu_state_cb("Writing SRAM/BRAM..");\r
968 emu_SaveLoadGame(0, 1);\r
969 SRam.changed = 0;\r
970 }\r
971\r
972 // if in 8bit mode, generate 16bit image for menu background\r
973 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
974 emu_forcedFrame();\r
975}\r
976\r
977\r
978void emu_ResetGame(void)\r
979{\r
980 PicoReset();\r
981 reset_timing = 1;\r
982}\r
983\r