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