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