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