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