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