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