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