work on 'vblank on line start' problem, var changes, mask defines
[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 clearArea(0);\r
472 }\r
473 if (do_it) {\r
474 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
475 PicoStateProgressCB = emu_state_cb;\r
476 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r
477 emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
478 PicoStateProgressCB = NULL;\r
479 }\r
480\r
481 reset_timing = 1;\r
482 }\r
483 if (which & 0x0400) // switch renderer\r
484 {\r
485 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
486 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
487 else currentConfig.EmuOpt &= ~0x80;\r
488\r
489 vidResetMode();\r
490\r
491 if (PicoOpt&0x10) {\r
492 strcpy(noticeMsg, " 8bit fast renderer");\r
493 } else if (currentConfig.EmuOpt&0x80) {\r
494 strcpy(noticeMsg, "16bit accurate renderer");\r
495 } else {\r
496 strcpy(noticeMsg, " 8bit accurate renderer");\r
497 }\r
498\r
499 gettimeofday(&noticeMsgTime, 0);\r
500 }\r
501 if (which & 0x0300)\r
502 {\r
503 if(which&0x0200) {\r
504 state_slot -= 1;\r
505 if(state_slot < 0) state_slot = 9;\r
506 } else {\r
507 state_slot += 1;\r
508 if(state_slot > 9) state_slot = 0;\r
509 }\r
510 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
511 gettimeofday(&noticeMsgTime, 0);\r
512 }\r
513 if (which & 0x0080) {\r
514 engineState = PGS_Menu;\r
515 }\r
516}\r
517\r
518\r
519static void updateKeys(void)\r
520{\r
521 unsigned long keys, allActions[2] = { 0, 0 }, events;\r
522 static unsigned long prevEvents = 0;\r
523 int joy, i;\r
524\r
525 keys = gp2x_joystick_read(0);\r
526 if (keys & GP2X_SELECT) {\r
527 engineState = select_exits ? PGS_Quit : PGS_Menu;\r
528 // wait until select is released, so menu would not resume game\r
529 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
530 }\r
531\r
532 keys &= CONFIGURABLE_KEYS;\r
533\r
534 for (i = 0; i < 32; i++)\r
535 {\r
536 if (keys & (1 << i))\r
537 {\r
538 int pl, acts = currentConfig.KeyBinds[i];\r
539 if (!acts) continue;\r
540 pl = (acts >> 16) & 1;\r
541 if (kb_combo_keys & (1 << i))\r
542 {\r
543 int u = i+1, acts_c = acts & kb_combo_acts;\r
544 // let's try to find the other one\r
545 if (acts_c) {\r
546 for (; u < 32; u++)\r
547 if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
548 allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r
549 keys &= ~((1 << i) | (1 << u));\r
550 break;\r
551 }\r
552 }\r
553 // add non-combo actions if combo ones were not found\r
554 if (!acts_c || u == 32)\r
555 allActions[pl] |= acts & ~kb_combo_acts;\r
556 } else {\r
557 allActions[pl] |= acts;\r
558 }\r
559 }\r
560 }\r
561\r
562 // add joy inputs\r
563 if (num_of_joys > 0)\r
564 {\r
565 gp2x_usbjoy_update();\r
566 for (joy = 0; joy < num_of_joys; joy++) {\r
567 int keys = gp2x_usbjoy_check2(joy);\r
568 for (i = 0; i < 32; i++) {\r
569 if (keys & (1 << i)) {\r
570 int acts = currentConfig.JoyBinds[joy][i];\r
571 int pl = (acts >> 16) & 1;\r
572 allActions[pl] |= acts;\r
573 }\r
574 }\r
575 }\r
576 }\r
577\r
578 PicoPad[0] = (unsigned short) allActions[0];\r
579 PicoPad[1] = (unsigned short) allActions[1];\r
580\r
581 events = (allActions[0] | allActions[1]) >> 16;\r
582\r
583 // volume is treated in special way and triggered every frame\r
584 if (events & 0x6000)\r
585 update_volume(1, events & 0x2000);\r
586\r
587 if ((events ^ prevEvents) & 0x40)\r
588 change_fast_forward(events & 0x40);\r
589\r
590 events &= ~prevEvents;\r
591 if (events) RunEvents(events);\r
592 if (movie_data) emu_updateMovie();\r
593\r
594 prevEvents = (allActions[0] | allActions[1]) >> 16;\r
595}\r
596\r
597\r
598static void updateSound(int len)\r
599{\r
600 if (PicoOpt&8) len<<=1;\r
601\r
602 /* avoid writing audio when lagging behind to prevent audio lag */\r
603 if (PicoSkipFrame != 2)\r
604 gp2x_sound_write(PsndOut, len<<1);\r
605}\r
606\r
607\r
608static void SkipFrame(int do_audio)\r
609{\r
610 PicoSkipFrame=do_audio ? 1 : 2;\r
611 PicoFrame();\r
612 PicoSkipFrame=0;\r
613}\r
614\r
615\r
616void emu_forcedFrame(void)\r
617{\r
618 int po_old = PicoOpt;\r
619 int eo_old = currentConfig.EmuOpt;\r
620\r
621 PicoOpt &= ~0x0010;\r
622 PicoOpt |= 0x4080; // soft_scale | acc_sprites\r
623 currentConfig.EmuOpt |= 0x80;\r
624\r
625 //vidResetMode();\r
626 PicoDrawSetColorFormat(1);\r
627 PicoScanBegin = EmuScanBegin16;\r
628 Pico.m.dirtyPal = 1;\r
629 PicoFrameDrawOnly();\r
630\r
631/*\r
632 if (!(Pico.video.reg[12]&1)) {\r
633 vidCpyM2 = vidCpyM2_32col;\r
634 clearArea(1);\r
635 } else vidCpyM2 = vidCpyM2_40col;\r
636\r
637 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
638 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
639 gp2x_video_setpalette(localPal, 0x40);\r
640*/\r
641 PicoOpt = po_old;\r
642 currentConfig.EmuOpt = eo_old;\r
643}\r
644\r
645static void simpleWait(int thissec, int lim_time)\r
646{\r
647 struct timeval tval;\r
648\r
649 spend_cycles(1024);\r
650 gettimeofday(&tval, 0);\r
651 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
652\r
653 while (tval.tv_usec < lim_time)\r
654 {\r
655 spend_cycles(1024);\r
656 gettimeofday(&tval, 0);\r
657 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
658 }\r
659}\r
660\r
661\r
662#if 0\r
663static void tga_dump(void)\r
664{\r
665#define BYTE unsigned char\r
666#define WORD unsigned short\r
667 struct\r
668 {\r
669 BYTE IDLength; /* 00h Size of Image ID field */\r
670 BYTE ColorMapType; /* 01h Color map type */\r
671 BYTE ImageType; /* 02h Image type code */\r
672 WORD CMapStart; /* 03h Color map origin */\r
673 WORD CMapLength; /* 05h Color map length */\r
674 BYTE CMapDepth; /* 07h Depth of color map entries */\r
675 WORD XOffset; /* 08h X origin of image */\r
676 WORD YOffset; /* 0Ah Y origin of image */\r
677 WORD Width; /* 0Ch Width of image */\r
678 WORD Height; /* 0Eh Height of image */\r
679 BYTE PixelDepth; /* 10h Image pixel size */\r
680 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
681 } __attribute__((packed)) TGAHEAD;\r
682 static unsigned short oldscr[320*240];\r
683 FILE *f; char name[128]; int i;\r
684\r
685 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
686 TGAHEAD.ImageType = 2;\r
687 TGAHEAD.Width = 320;\r
688 TGAHEAD.Height = 240;\r
689 TGAHEAD.PixelDepth = 16;\r
690 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
691\r
692#define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
693\r
694 for (i = 0; i < 320*240; i++)\r
695 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
696 if (i < 320*240)\r
697 {\r
698 for (i = 0; i < 320*240; i++)\r
699 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
700 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
701 f = fopen(name, "wb");\r
702 if (!f) { printf("!f\n"); exit(1); }\r
703 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
704 fwrite(oldscr, 1, 320*240*2, f);\r
705 fclose(f);\r
706 }\r
707}\r
708#endif\r
709\r
710\r
711void emu_Loop(void)\r
712{\r
713 static int gp2x_old_clock = 200;\r
714 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;\r
715 char fpsbuff[24]; // fps count c string\r
716 struct timeval tval; // timing\r
717 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
718 int frames_done, frames_shown, thissec; // actual frames\r
719 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
720 char *notice = 0;\r
721\r
722 printf("entered emu_Loop()\n");\r
723\r
724 if (gp2x_old_clock != currentConfig.CPUclock) {\r
725 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
726 set_FCLK(currentConfig.CPUclock);\r
727 gp2x_old_clock = currentConfig.CPUclock;\r
728 printf(" done\n");\r
729 }\r
730\r
731 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
732 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
733 gp2x_old_gamma = currentConfig.gamma;\r
734 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
735 }\r
736\r
737 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
738 if (currentConfig.EmuOpt&0x2000)\r
739 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
740 else unset_LCD_custom_rate();\r
741 }\r
742\r
743 EmuOpt_old = currentConfig.EmuOpt;\r
744 fpsbuff[0] = 0;\r
745\r
746 // make sure we are in correct mode\r
747 vidResetMode();\r
748 scaling_update();\r
749 Pico.m.dirtyPal = 1;\r
750 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
751 emu_findKeyBindCombos();\r
752\r
753 // pal/ntsc might have changed, reset related stuff\r
754 target_fps = Pico.m.pal ? 50 : 60;\r
755 target_frametime = 1000000/target_fps;\r
756 reset_timing = 1;\r
757\r
758 // prepare sound stuff\r
759 if (currentConfig.EmuOpt & 4)\r
760 {\r
761 int snd_excess_add;\r
762 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
763 ((PicoOpt&0x200) && crashed_940)) {\r
764 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
765 }\r
766 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
767 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
768 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
769 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
770 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
771 PicoWriteSound = updateSound;\r
772 update_volume(0, 0);\r
773 memset(sndBuffer, 0, sizeof(sndBuffer));\r
774 PsndOut = sndBuffer;\r
775 PsndRate_old = PsndRate;\r
776 PicoOpt_old = PicoOpt;\r
777 pal_old = Pico.m.pal;\r
778 } else {\r
779 PsndOut = NULL;\r
780 }\r
781\r
782 // prepare CD buffer\r
783 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
784\r
785 // calc vsync offset to sync timing code with vsync\r
786 if (currentConfig.EmuOpt&0x2000) {\r
787 gettimeofday(&tval, 0);\r
788 gp2x_video_wait_vsync();\r
789 gettimeofday(&tval, 0);\r
790 vsync_offset = tval.tv_usec;\r
791 while (vsync_offset >= target_frametime)\r
792 vsync_offset -= target_frametime;\r
793 if (!vsync_offset) vsync_offset++;\r
794 printf("vsync_offset: %i\n", vsync_offset);\r
795 } else\r
796 vsync_offset = 0;\r
797\r
798 frames_done = frames_shown = thissec =\r
799 pframes_done = pframes_shown = pthissec = 0;\r
800\r
801 // loop\r
802 while (engineState == PGS_Running)\r
803 {\r
804 int modes;\r
805\r
806 gettimeofday(&tval, 0);\r
807 if (reset_timing) {\r
808 reset_timing = 0;\r
809 pthissec = tval.tv_sec;\r
810 pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
811 }\r
812\r
813 // show notice message?\r
814 if (noticeMsgTime.tv_sec)\r
815 {\r
816 static int noticeMsgSum;\r
817 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
818 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
819 clearArea(0);\r
820 notice = 0;\r
821 } else {\r
822 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
823 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
824 notice = noticeMsg;\r
825 }\r
826 }\r
827\r
828 // check for mode changes\r
829 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
830 if (modes != oldmodes)\r
831 {\r
832 int scalex = 320;\r
833 osd_fps_x = OSD_FPS_X;\r
834 if (modes & 4) {\r
835 vidCpyM2 = vidCpyM2_40col;\r
836 } else {\r
837 if (PicoOpt & 0x100) {\r
838 vidCpyM2 = vidCpyM2_32col_nobord;\r
839 scalex = 256;\r
840 osd_fps_x = OSD_FPS_X - 64;\r
841 } else {\r
842 vidCpyM2 = vidCpyM2_32col;\r
843 }\r
844 }\r
845 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
846 gp2x_video_RGB_setscaling(8, scalex, 224);\r
847 else gp2x_video_RGB_setscaling(0, scalex, 240);\r
848 oldmodes = modes;\r
849 clearArea(1);\r
850 }\r
851\r
852 // second changed?\r
853 if (thissec != tval.tv_sec)\r
854 {\r
855#ifdef BENCHMARK\r
856 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
857 if (++bench == 10) {\r
858 bench = 0;\r
859 bench_fps_s = bench_fps;\r
860 bf[bfp++ & 3] = bench_fps;\r
861 bench_fps = 0;\r
862 }\r
863 bench_fps += frames_shown;\r
864 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
865#else\r
866 if (currentConfig.EmuOpt & 2) {\r
867 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
868 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
869 }\r
870#endif\r
871 frames_shown = frames_done = 0;\r
872 thissec = tval.tv_sec;\r
873 }\r
874#ifdef PFRAMES\r
875 sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
876#endif\r
877\r
878 if (pthissec != tval.tv_sec)\r
879 {\r
880 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
881 pframes_done = pframes_shown = 0;\r
882 } else {\r
883 // it is quite common for this implementation to leave 1 fame unfinished\r
884 // when second changes, but we don't want buffer to starve.\r
885 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
886 updateKeys();\r
887 SkipFrame(1); pframes_done++;\r
888 }\r
889\r
890 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;\r
891 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
892 if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
893 }\r
894 pthissec = tval.tv_sec;\r
895 }\r
896\r
897 lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
898 if (currentConfig.Frameskip >= 0) // frameskip enabled\r
899 {\r
900 for(i = 0; i < currentConfig.Frameskip; i++) {\r
901 updateKeys();\r
902 SkipFrame(1); pframes_done++; frames_done++;\r
903 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
904 gettimeofday(&tval, 0);\r
905 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
906 if (tval.tv_usec < lim_time) { // we are too fast\r
907 simpleWait(pthissec, lim_time);\r
908 }\r
909 }\r
910 lim_time += target_frametime;\r
911 }\r
912 }\r
913 else if (tval.tv_usec > lim_time) // auto frameskip\r
914 {\r
915 // no time left for this frame - skip\r
916 if (tval.tv_usec - lim_time >= 300000) {\r
917 /* something caused a slowdown for us (disk access? cache flush?)\r
918 * try to recover by resetting timing... */\r
919 reset_timing = 1;\r
920 continue;\r
921 }\r
922 updateKeys();\r
923 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
924 continue;\r
925 }\r
926\r
927 updateKeys();\r
928 PicoFrame();\r
929\r
930 // check time\r
931 gettimeofday(&tval, 0);\r
932 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
933\r
934 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
935 reset_timing = 1;\r
936 else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
937 {\r
938 // sleep or vsync if we are still too fast\r
939 // usleep sleeps for ~20ms minimum, so it is not a solution here\r
940 if (!reset_timing && tval.tv_usec < lim_time)\r
941 {\r
942 // we are too fast\r
943 if (vsync_offset) {\r
944 if (lim_time - tval.tv_usec > target_frametime/2)\r
945 simpleWait(pthissec, lim_time - target_frametime/4);\r
946 gp2x_video_wait_vsync();\r
947 } else {\r
948 simpleWait(pthissec, lim_time);\r
949 }\r
950 }\r
951 }\r
952\r
953 blit(fpsbuff, notice);\r
954\r
955 pframes_done++; pframes_shown++;\r
956 frames_done++; frames_shown++;\r
957 }\r
958\r
959 change_fast_forward(0);\r
960\r
961 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
962\r
963 // save SRAM\r
964 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
965 emu_state_cb("Writing SRAM/BRAM..");\r
966 emu_SaveLoadGame(0, 1);\r
967 SRam.changed = 0;\r
968 }\r
969\r
970 // if in 8bit mode, generate 16bit image for menu background\r
971 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
972 emu_forcedFrame();\r
973}\r
974\r
975\r
976void emu_ResetGame(void)\r
977{\r
978 PicoReset();\r
979 reset_timing = 1;\r
980}\r
981\r