1 // (c) Copyright 2006-2008 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\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
17 #include "../gp2x/emu.h"
\r
18 #include "../gp2x/menu.h"
\r
19 #include "../gp2x/gp2x.h"
\r
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
25 #include "../common/input.h"
\r
26 #include "../linux/sndout_oss.h"
\r
27 #include "asm_utils.h"
\r
29 #include <pico/pico_int.h>
\r
30 #include <pico/patch.h>
\r
31 #include <pico/sound/mix.h>
\r
32 #include <zlib/zlib.h>
\r
36 //#define USE_320_SCREEN 1
\r
39 #define OSD_FPS_X (800-200)
\r
41 #define OSD_FPS_X (800-120)
\r
46 int select_exits = 0;
\r
48 char romFileName[PATH_MAX];
\r
50 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
51 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
52 static int osd_fps_x;
\r
53 unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
\r
54 int reset_timing = 0;
\r
56 #define PICO_PEN_ADJUST_X 4
\r
57 #define PICO_PEN_ADJUST_Y 2
\r
58 static int pico_pen_x = SCREEN_WIDTH/2, pico_pen_y = 240/2;
\r
60 static void emu_msg_cb(const char *msg);
\r
61 static void emu_msg_tray_open(void);
\r
64 void emu_noticeMsgUpdated(void)
\r
66 gettimeofday(¬iceMsgTime, 0);
\r
69 int emu_getMainDir(char *dst, int len)
\r
71 extern char **g_argv;
\r
74 strncpy(dst, g_argv[0], len);
\r
75 len -= 32; // reserve
\r
76 if (len < 0) len = 0;
\r
78 for (j = strlen(dst); j > 0; j--)
\r
79 if (dst[j] == '/') { dst[j+1] = 0; break; }
\r
86 // make temp buffer for alt renderer
\r
87 PicoDraw2FB = malloc((8+320)*(8+240+8));
\r
90 printf("PicoDraw2FB == 0\n");
\r
93 // make dirs for saves, cfgs, etc.
\r
100 PicoMessage = emu_msg_cb;
\r
101 PicoMCDopenTray = emu_msg_tray_open;
\r
102 PicoMCDcloseTray = menu_loop_tray;
\r
106 void emu_Deinit(void)
\r
109 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
110 emu_SaveLoadGame(0, 1);
\r
114 if (!(currentConfig.EmuOpt & 0x20)) {
\r
115 config_writelrom(PicoConfigFile);
\r
126 void emu_prepareDefaultConfig(void)
\r
128 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
129 defaultConfig.EmuOpt = 0x8f | 0x00600; // | <- confirm_save, cd_leds
\r
130 defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC;
\r
131 defaultConfig.s_PicoOpt |= POPT_ACC_SPRITES|POPT_EN_MCD_GFX;
\r
132 // defaultConfig.s_PicoOpt &= ~POPT_EN_SVP_DRC; // crashes :(
\r
133 defaultConfig.EmuOpt &= ~8; // no save gzip
\r
134 defaultConfig.s_PsndRate = 44100;
\r
135 defaultConfig.s_PicoRegion = 0;
\r
136 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
137 defaultConfig.s_PicoCDBuffers = 0;
\r
138 defaultConfig.Frameskip = 0;
\r
139 defaultConfig.CPUclock = 200;
\r
140 defaultConfig.volume = 50;
\r
141 defaultConfig.scaling = 0;
\r
142 defaultConfig.turbo_rate = 15;
\r
145 static void textOut16(int x, int y, const char *text)
\r
147 int i,l,len=strlen(text);
\r
148 unsigned int *screen = (unsigned int *)((unsigned short *)SCREEN_BUFFER + (x&~1) + y*SCREEN_WIDTH);
\r
150 for (i = 0; i < len; i++)
\r
154 unsigned char fd = fontdata8x8[((text[i])*8)+l/2];
\r
155 unsigned int *d = &screen[l*SCREEN_WIDTH/2];
\r
156 if (fd&0x80) d[0]=0xffffffff;
\r
157 if (fd&0x40) d[1]=0xffffffff;
\r
158 if (fd&0x20) d[2]=0xffffffff;
\r
159 if (fd&0x10) d[3]=0xffffffff;
\r
160 if (fd&0x08) d[4]=0xffffffff;
\r
161 if (fd&0x04) d[5]=0xffffffff;
\r
162 if (fd&0x02) d[6]=0xffffffff;
\r
163 if (fd&0x01) d[7]=0xffffffff;
\r
164 l++; d = &screen[l*SCREEN_WIDTH/2];
\r
165 if (fd&0x80) d[0]=0xffffffff;
\r
166 if (fd&0x40) d[1]=0xffffffff;
\r
167 if (fd&0x20) d[2]=0xffffffff;
\r
168 if (fd&0x10) d[3]=0xffffffff;
\r
169 if (fd&0x08) d[4]=0xffffffff;
\r
170 if (fd&0x04) d[5]=0xffffffff;
\r
171 if (fd&0x02) d[6]=0xffffffff;
\r
172 if (fd&0x01) d[7]=0xffffffff;
\r
180 void osd_text(int x, int y, const char *text)
\r
182 int len = strlen(text)*8;
\r
184 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
186 x &= ~3; // align x
\r
187 len = (len+3) >> 2;
\r
188 for (h = 0; h < 8; h++) {
\r
189 p = (int *) ((unsigned char *) gp2x_screen+x+SCREEN_WIDTH*(y+h));
\r
190 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
192 emu_textOut8(x, y, text);
\r
195 x &= ~1; // align x
\r
197 for (h = 0; h < 16; h++) {
\r
198 p = (int *) ((unsigned short *) gp2x_screen+x+SCREEN_WIDTH*(y+h));
\r
199 for (i = len; i; i--, p++) *p = 0;//(*p>>2)&0x39e7;
\r
201 textOut16(x, y, text);
\r
205 static void draw_cd_leds(void)
\r
209 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
\r
210 old_reg = Pico_mcd->s68k_regs[0];
\r
212 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
214 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
215 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
216 *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*2+ 4) =
\r
217 *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*3+ 4) =
\r
218 *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*4+ 4) = col_g;
\r
219 *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*2+12) =
\r
220 *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*3+12) =
\r
221 *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*4+12) = col_r;
\r
224 unsigned int *p = (unsigned int *)((short *)gp2x_screen + SCREEN_WIDTH*2+4);
\r
225 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
226 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
227 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += SCREEN_WIDTH/2 - 12/2;
\r
228 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += SCREEN_WIDTH/2 - 12/2;
\r
229 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
233 static void draw_pico_ptr(void)
\r
235 unsigned short *p = (unsigned short *)gp2x_screen;
\r
237 // only if pen enabled and for 16bit modes
\r
238 if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
\r
240 if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
\r
243 p += SCREEN_WIDTH * (pico_pen_y + PICO_PEN_ADJUST_Y);
\r
244 p += pico_pen_x + PICO_PEN_ADJUST_X;
\r
252 #ifdef USE_320_SCREEN
\r
254 static int EmuScanBegin16(unsigned int num)
\r
256 if (!(Pico.video.reg[1]&8)) num += 8;
\r
257 DrawLineDest = (unsigned short *)gp2x_screen + num*800 + 800/2 - 320/2;
\r
258 //int w = (Pico.video.reg[12]&1) ? 320 : 256;
\r
259 //DrawLineDest = (unsigned short *)gp2x_screen + num*w;
\r
264 #else // USE_320_SCREEN
\r
266 static int EmuScanEnd16(unsigned int num)
\r
268 unsigned char *ps=HighCol+8;
\r
269 unsigned short *pd;
\r
270 unsigned short *pal=HighPal;
\r
271 int sh = Pico.video.reg[0xC]&8;
\r
272 int len, mask = 0xff;
\r
274 if (!(Pico.video.reg[1]&8)) num += 8;
\r
275 pd=(unsigned short *)gp2x_screen + num*800*2 + 800/2 - 320*2/2;
\r
277 if (Pico.m.dirtyPal)
\r
278 PicoDoHighPal555(sh);
\r
280 if (Pico.video.reg[12]&1) {
\r
287 if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))
\r
288 mask=0x3f; // messed sprites, upper bits are priority stuff
\r
291 clut_line(pd, ps, pal, (mask<<16) | len);
\r
293 for (; len > 0; len--)
\r
295 unsigned int p = pal[*ps++ & mask];
\r
297 *(unsigned int *)pd = p;
\r
298 *(unsigned int *)(&pd[800]) = p;
\r
306 #endif // USE_320_SCREEN
\r
308 static int EmuScanBegin8(unsigned int num)
\r
310 if (!(Pico.video.reg[1]&8)) num += 8;
\r
311 DrawLineDest = (unsigned char *) gp2x_screen + SCREEN_WIDTH * num;
\r
316 int localPal[0x100];
\r
317 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
319 static void blit(const char *fps, const char *notice)
\r
321 int emu_opt = currentConfig.EmuOpt;
\r
325 // 8bit fast renderer
\r
326 if (Pico.m.dirtyPal) {
\r
327 Pico.m.dirtyPal = 0;
\r
328 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
329 // feed new palette to our device
\r
330 gp2x_video_setpalette(localPal, 0x40);
\r
333 if (PicoRead16Hook == PicoSVPRead16)
\r
334 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
\r
336 vidCpyM2((unsigned char *)gp2x_screen+SCREEN_WIDTH*8, PicoDraw2FB+328*8);
\r
338 else if (!(emu_opt&0x80))
\r
340 // 8bit accurate renderer
\r
341 if (Pico.m.dirtyPal)
\r
344 Pico.m.dirtyPal = 0;
\r
345 if (Pico.video.reg[0xC]&8) // shadow/hilight mode
\r
347 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
348 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
349 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
350 memcpy32(localPal+0xc0, localPal+0x40, 0x40);
\r
353 else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
\r
354 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
355 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
356 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
360 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
361 memcpy32(localPal+0x80, localPal, 0x40);
\r
363 if (pallen > 0xc0) {
\r
364 localPal[0xc0] = 0x0000c000;
\r
365 localPal[0xd0] = 0x00c00000;
\r
366 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
367 localPal[0xf0] = 0x00ffffff;
\r
369 gp2x_video_setpalette(localPal, pallen);
\r
373 if (notice || (emu_opt & 2)) {
\r
374 int h = SCREEN_HEIGHT-16;
\r
375 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 16;
\r
376 if (notice) osd_text(4, h, notice);
\r
378 osd_text(osd_fps_x, h, fps);
\r
380 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
\r
382 if (PicoAHW & PAHW_PICO)
\r
386 //memset((char *)gp2x_screen+800*471*2, (u++)>>1, 800*9*2);
\r
389 //gp2x_video_wait_vsync();
\r
394 // clears whole screen or just the notice area (in all buffers)
\r
395 static void clearArea(int full)
\r
397 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
399 if (full) gp2x_memset_all_buffers(0, 0xe0, SCREEN_WIDTH*240);
\r
400 else gp2x_memset_all_buffers(SCREEN_WIDTH*232, 0xe0, SCREEN_WIDTH*8);
\r
402 // 16bit accurate renderer
\r
403 if (full) gp2x_memset_all_buffers(0, 0, SCREEN_WIDTH*SCREEN_HEIGHT*2);
\r
404 else gp2x_memset_all_buffers(SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2, 0, SCREEN_WIDTH*16*2);
\r
409 static void vidResetMode(void)
\r
411 if (PicoOpt&0x10) {
\r
412 gp2x_video_changemode(8);
\r
413 } else if (currentConfig.EmuOpt&0x80) {
\r
414 gp2x_video_changemode(16);
\r
415 #ifdef USE_320_SCREEN
\r
416 PicoDrawSetColorFormat(1);
\r
417 PicoScanBegin = EmuScanBegin16;
\r
419 PicoDrawSetColorFormat(-1);
\r
420 PicoScanEnd = EmuScanEnd16;
\r
423 gp2x_video_changemode(8);
\r
424 PicoDrawSetColorFormat(2);
\r
425 PicoScanBegin = EmuScanBegin8;
\r
427 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
428 // setup pal for 8-bit modes
\r
429 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
430 localPal[0xd0] = 0x00c00000;
\r
431 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
432 localPal[0xf0] = 0x00ffffff;
\r
433 gp2x_video_setpalette(localPal, 0x100);
\r
434 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
437 Pico.m.dirtyPal = 1;
\r
439 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
440 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
441 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
445 static void emu_msg_cb(const char *msg)
\r
447 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
449 gp2x_memset_all_buffers(SCREEN_WIDTH*(SCREEN_HEIGHT-16), 0xe0, SCREEN_WIDTH*16);
\r
450 osd_text(4, SCREEN_HEIGHT-16, msg);
\r
451 gp2x_memcpy_all_buffers((char *)gp2x_screen+SCREEN_WIDTH*(SCREEN_HEIGHT-16),
\r
452 SCREEN_WIDTH*(SCREEN_HEIGHT-16), SCREEN_WIDTH*16);
\r
454 // 16bit accurate renderer
\r
455 gp2x_memset_all_buffers(SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2, 0, SCREEN_WIDTH*16*2);
\r
456 osd_text(4, SCREEN_HEIGHT-16, msg);
\r
457 gp2x_memcpy_all_buffers((char *)gp2x_screen+SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2,
\r
458 SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2, SCREEN_WIDTH*16*2);
\r
460 gettimeofday(¬iceMsgTime, 0);
\r
461 noticeMsgTime.tv_sec -= 2;
\r
463 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
467 static void emu_state_cb(const char *str)
\r
473 static void emu_msg_tray_open(void)
\r
475 strcpy(noticeMsg, "CD tray opened");
\r
476 gettimeofday(¬iceMsgTime, 0);
\r
480 static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)
\r
482 int ret, px, py, lim_x;
\r
483 static int pdown_frames = 0;
\r
485 emu_RunEventsPico(events);
\r
487 if (pico_inp_mode == 0) return;
\r
490 ret = gp2x_touchpad_read(&px, &py);
\r
495 if (pdown_frames++ > 5)
\r
496 PicoPad[0] |= 0x20;
\r
500 if (!(Pico.video.reg[12]&1)) {
\r
502 if (pico_pen_x < 0) pico_pen_x = 0;
\r
503 if (pico_pen_x > 248) pico_pen_x = 248;
\r
505 if (pico_pen_y > 224) pico_pen_y = 224;
\r
511 // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
\r
514 PicoPad[0] &= ~0x0f; // release UDLR
\r
515 if (gp2x_keys & GP2X_UP) pico_pen_y--;
\r
516 if (gp2x_keys & GP2X_DOWN) pico_pen_y++;
\r
517 if (gp2x_keys & GP2X_LEFT) pico_pen_x--;
\r
518 if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;
\r
520 lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
\r
521 if (pico_pen_y < 8) pico_pen_y = 8;
\r
522 if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;
\r
523 if (pico_pen_x < 0) pico_pen_x = 0;
\r
524 if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;
\r
526 PicoPicohw.pen_pos[0] = pico_pen_x;
\r
527 if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
\r
528 PicoPicohw.pen_pos[0] += 0x3c;
\r
529 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
\r
533 static void update_volume(int has_changed, int is_up)
\r
535 static int prev_frame = 0, wait_frames = 0;
\r
536 int vol = currentConfig.volume;
\r
540 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
\r
544 if (vol < 99) vol++;
\r
546 if (vol > 0) vol--;
\r
549 sndout_oss_setvol(vol, vol);
\r
550 currentConfig.volume = vol;
\r
552 sprintf(noticeMsg, "VOL: %02i", vol);
\r
553 gettimeofday(¬iceMsgTime, 0);
\r
554 prev_frame = Pico.m.frame_count;
\r
557 // set the right mixer func
\r
558 if (!(PicoOpt&8)) return; // just use defaults for mono
\r
560 PsndMix_32_to_16l = mix_32_to_16l_stereo;
\r
562 mix_32_to_16l_level = 5 - vol;
\r
563 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
\r
567 static void RunEvents(unsigned int which)
\r
569 if (which & 0x1800) // save or load (but not both)
\r
572 if ( emu_checkSaveFile(state_slot) &&
\r
573 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
574 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
\r
576 unsigned long keys;
\r
577 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
\r
578 while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
580 if (keys & GP2X_X) do_it = 0;
\r
581 while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release
\r
587 osd_text(4, SCREEN_HEIGHT-16, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
588 PicoStateProgressCB = emu_state_cb;
\r
589 gp2x_memcpy_all_buffers(gp2x_screen, 0, SCREEN_WIDTH*SCREEN_HEIGHT*2);
\r
590 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
591 PicoStateProgressCB = NULL;
\r
596 if (which & 0x0400) // switch renderer
\r
598 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
599 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
600 else currentConfig.EmuOpt &= ~0x80;
\r
604 if (PicoOpt&0x10) {
\r
605 strcpy(noticeMsg, " 8bit fast renderer");
\r
606 } else if (currentConfig.EmuOpt&0x80) {
\r
607 strcpy(noticeMsg, "16bit accurate renderer");
\r
609 strcpy(noticeMsg, " 8bit accurate renderer");
\r
612 gettimeofday(¬iceMsgTime, 0);
\r
614 if (which & 0x0300)
\r
618 if(state_slot < 0) state_slot = 9;
\r
621 if(state_slot > 9) state_slot = 0;
\r
623 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
\r
624 gettimeofday(¬iceMsgTime, 0);
\r
626 if (which & 0x0080) {
\r
627 engineState = PGS_Menu;
\r
631 static void updateKeys(void)
\r
633 unsigned int allActions[2] = { 0, 0 }, events;
\r
634 static unsigned int prevEvents = 0;
\r
636 /* FIXME: combos, player2 */
\r
637 allActions[0] = in_update();
\r
639 PicoPad[0] = allActions[0] & 0xfff;
\r
640 PicoPad[1] = allActions[1] & 0xfff;
\r
642 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
\r
643 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
\r
645 events = (allActions[0] | allActions[1]) >> 16;
\r
647 // volume is treated in special way and triggered every frame
\r
648 if (events & 0x6000)
\r
649 update_volume(1, events & 0x2000);
\r
651 if ((events ^ prevEvents) & 0x40) {
\r
652 emu_changeFastForward(events & 0x40);
\r
653 update_volume(0, 0);
\r
657 events &= ~prevEvents;
\r
660 if (PicoAHW == PAHW_PICO)
\r
661 RunEventsPico(events, keys);
\r
663 if (events) RunEvents(events);
\r
664 if (movie_data) emu_updateMovie();
\r
666 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
670 static void updateSound(int len)
\r
672 if (PicoOpt&8) len<<=1;
\r
674 /* avoid writing audio when lagging behind to prevent audio lag */
\r
675 if (PicoSkipFrame != 2)
\r
676 sndout_oss_write(PsndOut, len<<1);
\r
680 static void SkipFrame(int do_audio)
\r
682 PicoSkipFrame=do_audio ? 1 : 2;
\r
688 void emu_forcedFrame(int opts)
\r
690 int po_old = PicoOpt;
\r
691 int eo_old = currentConfig.EmuOpt;
\r
694 PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
\r
695 currentConfig.EmuOpt |= 0x80;
\r
698 #ifdef USE_320_SCREEN
\r
699 PicoDrawSetColorFormat(1);
\r
700 PicoScanBegin = EmuScanBegin16;
\r
702 PicoDrawSetColorFormat(-1);
\r
703 PicoScanEnd = EmuScanEnd16;
\r
705 Pico.m.dirtyPal = 1;
\r
706 PicoFrameDrawOnly();
\r
709 if (!(Pico.video.reg[12]&1)) {
\r
710 vidCpyM2 = vidCpyM2_32col;
\r
712 } else vidCpyM2 = vidCpyM2_40col;
\r
714 vidCpyM2((unsigned char *)gp2x_screen+SCREEN_WIDTH*8, PicoDraw2FB+328*8);
\r
715 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
716 gp2x_video_setpalette(localPal, 0x40);
\r
719 currentConfig.EmuOpt = eo_old;
\r
722 void emu_platformDebugCat(char *str)
\r
727 static void simpleWait(int thissec, int lim_time)
\r
729 struct timeval tval;
\r
731 spend_cycles(1024);
\r
732 gettimeofday(&tval, 0);
\r
733 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
735 while (tval.tv_usec < lim_time)
\r
737 spend_cycles(1024);
\r
738 gettimeofday(&tval, 0);
\r
739 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
743 void emu_startSound(void)
\r
745 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
\r
746 int target_fps = Pico.m.pal ? 50 : 60;
\r
750 if (currentConfig.EmuOpt & 4)
\r
752 int snd_excess_add;
\r
753 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old)
\r
754 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
756 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
757 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
758 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
759 sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);
\r
760 sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
\r
761 PicoWriteSound = updateSound;
\r
762 update_volume(0, 0);
\r
763 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
764 PsndOut = sndBuffer;
\r
765 PsndRate_old = PsndRate;
\r
766 PicoOpt_old = PicoOpt;
\r
767 pal_old = Pico.m.pal;
\r
771 void emu_endSound(void)
\r
775 /* wait until we can write more sound */
\r
776 void emu_waitSound(void)
\r
778 // don't need to do anything, writes will block by themselves
\r
781 void emu_Loop(void)
\r
783 char fpsbuff[24]; // fps count c string
\r
784 struct timeval tval; // timing
\r
785 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
\r
786 int frames_done, frames_shown, thissec; // actual frames
\r
787 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
\r
790 printf("entered emu_Loop()\n");
\r
794 // make sure we are in correct mode
\r
796 Pico.m.dirtyPal = 1;
\r
797 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
798 emu_findKeyBindCombos();
\r
800 // pal/ntsc might have changed, reset related stuff
\r
801 target_fps = Pico.m.pal ? 50 : 60;
\r
802 target_frametime = 1000000/target_fps;
\r
807 // prepare CD buffer
\r
808 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
\r
810 // calc vsync offset to sync timing code with vsync
\r
811 if (currentConfig.EmuOpt&0x2000) {
\r
812 gettimeofday(&tval, 0);
\r
813 gp2x_video_wait_vsync();
\r
814 gettimeofday(&tval, 0);
\r
815 vsync_offset = tval.tv_usec;
\r
816 while (vsync_offset >= target_frametime)
\r
817 vsync_offset -= target_frametime;
\r
818 if (!vsync_offset) vsync_offset++;
\r
819 printf("vsync_offset: %i\n", vsync_offset);
\r
823 frames_done = frames_shown = thissec =
\r
824 pframes_done = pframes_shown = pthissec = 0;
\r
827 while (engineState == PGS_Running)
\r
831 gettimeofday(&tval, 0);
\r
832 if (reset_timing) {
\r
834 pthissec = tval.tv_sec;
\r
835 pframes_shown = pframes_done = tval.tv_usec/target_frametime;
\r
838 // show notice message?
\r
839 if (noticeMsgTime.tv_sec)
\r
841 static int noticeMsgSum;
\r
842 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
843 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
847 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
848 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
849 notice = noticeMsg;
\r
853 // check for mode changes
\r
854 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
855 if (modes != oldmodes)
\r
857 int scalex = SCREEN_WIDTH;
\r
858 osd_fps_x = OSD_FPS_X;
\r
860 vidCpyM2 = vidCpyM2_40col;
\r
862 if (PicoOpt & 0x100) {
\r
863 vidCpyM2 = vidCpyM2_32col_nobord;
\r
865 osd_fps_x = OSD_FPS_X - 64;
\r
867 vidCpyM2 = vidCpyM2_32col;
\r
870 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
871 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
872 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
878 if (thissec != tval.tv_sec)
\r
881 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
882 if (++bench == 4) {
\r
884 bench_fps_s = bench_fps / 4;
\r
885 bf[bfp++ & 3] = bench_fps / 4;
\r
888 bench_fps += frames_shown;
\r
889 sprintf(fpsbuff, "%3i/%3i/%3i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
890 printf("%s\n", fpsbuff);
\r
892 if (currentConfig.EmuOpt & 2) {
\r
893 sprintf(fpsbuff, "%3i/%3i", frames_shown, frames_done);
\r
894 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
897 frames_shown = frames_done = 0;
\r
898 thissec = tval.tv_sec;
\r
901 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
904 if (pthissec != tval.tv_sec)
\r
906 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
907 pframes_done = pframes_shown = 0;
\r
909 // it is quite common for this implementation to leave 1 fame unfinished
\r
910 // when second changes, but we don't want buffer to starve.
\r
911 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
\r
913 SkipFrame(1); pframes_done++;
\r
916 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
\r
917 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
\r
918 if (pframes_shown > pframes_done) pframes_shown = pframes_done;
\r
920 pthissec = tval.tv_sec;
\r
923 lim_time = (pframes_done+1) * target_frametime + vsync_offset;
\r
924 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
926 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
928 SkipFrame(1); pframes_done++; frames_done++;
\r
929 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
\r
930 gettimeofday(&tval, 0);
\r
931 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
932 if (tval.tv_usec < lim_time) { // we are too fast
\r
933 simpleWait(pthissec, lim_time);
\r
936 lim_time += target_frametime;
\r
939 else if (tval.tv_usec > lim_time) // auto frameskip
\r
941 // no time left for this frame - skip
\r
942 if (tval.tv_usec - lim_time >= 300000) {
\r
943 /* something caused a slowdown for us (disk access? cache flush?)
\r
944 * try to recover by resetting timing... */
\r
949 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
\r
957 gettimeofday(&tval, 0);
\r
958 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
960 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
\r
963 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
965 // sleep or vsync if we are still too fast
\r
966 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
967 if (!reset_timing && tval.tv_usec < lim_time)
\r
970 if (vsync_offset) {
\r
971 if (lim_time - tval.tv_usec > target_frametime/2)
\r
972 simpleWait(pthissec, lim_time - target_frametime/4);
\r
973 gp2x_video_wait_vsync();
\r
975 simpleWait(pthissec, lim_time);
\r
980 blit(fpsbuff, notice);
\r
982 pframes_done++; pframes_shown++;
\r
983 frames_done++; frames_shown++;
\r
986 emu_changeFastForward(0);
\r
988 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
\r
991 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
992 emu_state_cb("Writing SRAM/BRAM..");
\r
993 emu_SaveLoadGame(0, 1);
\r
997 // if in 8bit mode, generate 16bit image for menu background
\r
998 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
999 emu_forcedFrame(POPT_EN_SOFTSCALE);
\r
1003 void emu_ResetGame(void)
\r