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