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