1 // (c) Copyright 2006-2007 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
21 #include "../common/arm_utils.h"
\r
22 #include "../common/fonts.h"
\r
23 #include "../common/emu.h"
\r
24 #include "../common/config.h"
\r
25 #include "cpuctrl.h"
\r
27 #include <Pico/PicoInt.h>
\r
28 #include <Pico/Patch.h>
\r
29 #include <Pico/sound/mix.h>
\r
30 #include <zlib/zlib.h>
\r
35 #define OSD_FPS_X 220
\r
37 #define OSD_FPS_X 260
\r
42 int select_exits = 0;
\r
44 char romFileName[PATH_MAX];
\r
46 extern int crashed_940;
\r
48 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
49 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
50 static int osd_fps_x;
\r
51 static int gp2x_old_gamma = 100;
\r
52 char noticeMsg[64]; // notice msg to draw
\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 = 320/2, pico_pen_y = 240/2, pico_inp_mode = 0;
\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 void 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
84 // make temp buffer for alt renderer
\r
85 PicoDraw2FB = malloc((8+320)*(8+240+8));
\r
88 printf("PicoDraw2FB == 0\n");
\r
91 // make dirs for saves, cfgs, etc.
\r
98 PicoMessage = emu_msg_cb;
\r
99 PicoMCDopenTray = emu_msg_tray_open;
\r
100 PicoMCDcloseTray = menu_loop_tray;
\r
104 static void scaling_update(void)
\r
106 PicoOpt &= ~0x4100;
\r
107 switch (currentConfig.scaling) {
\r
108 default: break; // off
\r
110 case 2: PicoOpt |= 0x0100; break; // hw hor+vert
\r
111 case 3: PicoOpt |= 0x4000; break; // sw hor
\r
116 void emu_Deinit(void)
\r
119 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
120 emu_SaveLoadGame(0, 1);
\r
124 if (!(currentConfig.EmuOpt & 0x20)) {
\r
125 config_writelrom(PicoConfigFile);
\r
136 if (gp2x_old_gamma != 100)
\r
140 void emu_prepareDefaultConfig(void)
\r
142 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
143 defaultConfig.EmuOpt = 0x1d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds
\r
144 defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
\r
145 defaultConfig.s_PsndRate = 44100;
\r
146 defaultConfig.s_PicoRegion = 0; // auto
\r
147 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
148 defaultConfig.s_PicoCDBuffers = 0;
\r
149 defaultConfig.Frameskip = -1; // auto
\r
150 defaultConfig.CPUclock = 200;
\r
151 defaultConfig.volume = 50;
\r
152 defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
153 defaultConfig.KeyBinds[ 4] = 1<<1;
\r
154 defaultConfig.KeyBinds[ 2] = 1<<2;
\r
155 defaultConfig.KeyBinds[ 6] = 1<<3;
\r
156 defaultConfig.KeyBinds[14] = 1<<4;
\r
157 defaultConfig.KeyBinds[13] = 1<<5;
\r
158 defaultConfig.KeyBinds[12] = 1<<6;
\r
159 defaultConfig.KeyBinds[ 8] = 1<<7;
\r
160 defaultConfig.KeyBinds[15] = 1<<26; // switch rend
\r
161 defaultConfig.KeyBinds[10] = 1<<27; // save state
\r
162 defaultConfig.KeyBinds[11] = 1<<28; // load state
\r
163 defaultConfig.KeyBinds[23] = 1<<29; // vol up
\r
164 defaultConfig.KeyBinds[22] = 1<<30; // vol down
\r
165 defaultConfig.gamma = 100;
\r
166 defaultConfig.scaling = 0;
\r
169 void emu_setDefaultConfig(void)
\r
171 memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));
\r
172 PicoOpt = currentConfig.s_PicoOpt;
\r
173 PsndRate = currentConfig.s_PsndRate;
\r
174 PicoRegionOverride = currentConfig.s_PicoRegion;
\r
175 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
\r
176 PicoCDBuffers = currentConfig.s_PicoCDBuffers;
\r
179 void osd_text(int x, int y, const char *text)
\r
181 int len = strlen(text)*8;
\r
183 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
185 x &= ~3; // align x
\r
186 len = (len+3) >> 2;
\r
187 for (h = 0; h < 8; h++) {
\r
188 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
189 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
191 emu_textOut8(x, y, text);
\r
194 x &= ~1; // align x
\r
195 len = (len+1) >> 1;
\r
196 for (h = 0; h < 8; h++) {
\r
197 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
198 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
200 emu_textOut16(x, y, text);
\r
204 static void draw_cd_leds(void)
\r
208 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
\r
209 old_reg = Pico_mcd->s68k_regs[0];
\r
211 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
213 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
214 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
215 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
216 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
217 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
218 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
219 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
220 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
223 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
224 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
225 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
226 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
227 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
228 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
232 static void draw_pico_ptr(void)
\r
234 unsigned short *p = (unsigned short *)gp2x_screen;
\r
236 // only if pen enabled and for 16bit modes
\r
237 if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
\r
239 if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
\r
242 p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);
\r
243 p += pico_pen_x + PICO_PEN_ADJUST_X;
\r
251 static int EmuScanBegin16(unsigned int num)
\r
253 if (!(Pico.video.reg[1]&8)) num += 8;
\r
254 DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;
\r
259 static int EmuScanBegin8(unsigned int num)
\r
261 if (!(Pico.video.reg[1]&8)) num += 8;
\r
262 DrawLineDest = (unsigned char *) gp2x_screen + 320 * num;
\r
267 int localPal[0x100];
\r
268 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
270 static void blit(const char *fps, const char *notice)
\r
272 int emu_opt = currentConfig.EmuOpt;
\r
276 // 8bit fast renderer
\r
277 if (Pico.m.dirtyPal) {
\r
278 Pico.m.dirtyPal = 0;
\r
279 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
280 // feed new palette to our device
\r
281 gp2x_video_setpalette(localPal, 0x40);
\r
284 if (PicoRead16Hook == PicoSVPRead16)
\r
285 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
\r
287 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
289 else if (!(emu_opt&0x80))
\r
291 // 8bit accurate renderer
\r
292 if (Pico.m.dirtyPal)
\r
295 Pico.m.dirtyPal = 0;
\r
296 if (Pico.video.reg[0xC]&8) // shadow/hilight mode
\r
298 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
299 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
300 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
301 memcpy32(localPal+0xc0, localPal+0x40, 0x40);
\r
304 else if (rendstatus & PDRAW_ACC_SPRITES) {
\r
305 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
306 memcpy32(localPal+0x40, localPal, 0x40);
\r
307 memcpy32(localPal+0x80, localPal, 0x40);
\r
308 memcpy32(localPal+0xc0, localPal, 0x40);
\r
311 else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
\r
312 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
313 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
314 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
318 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
320 if (pallen > 0xc0) {
\r
321 localPal[0xc0] = 0x0000c000;
\r
322 localPal[0xd0] = 0x00c00000;
\r
323 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
324 localPal[0xf0] = 0x00ffffff;
\r
326 gp2x_video_setpalette(localPal, pallen);
\r
330 if (notice || (emu_opt & 2)) {
\r
332 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
\r
333 if (notice) osd_text(4, h, notice);
\r
335 osd_text(osd_fps_x, h, fps);
\r
337 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
\r
339 if (PicoAHW & PAHW_PICO)
\r
342 //gp2x_video_wait_vsync();
\r
345 if (!(PicoOpt&0x10)) {
\r
346 if (!(Pico.video.reg[1]&8)) {
\r
347 if (currentConfig.EmuOpt&0x80) {
\r
348 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
350 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
353 DrawLineDest = gp2x_screen;
\r
359 // clears whole screen or just the notice area (in all buffers)
\r
360 static void clearArea(int full)
\r
362 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
364 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
365 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
367 // 16bit accurate renderer
\r
368 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
369 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
374 static void vidResetMode(void)
\r
376 if (PicoOpt&0x10) {
\r
377 gp2x_video_changemode(8);
\r
378 } else if (currentConfig.EmuOpt&0x80) {
\r
379 gp2x_video_changemode(16);
\r
380 PicoDrawSetColorFormat(1);
\r
381 PicoScanBegin = EmuScanBegin16;
\r
383 gp2x_video_changemode(8);
\r
384 PicoDrawSetColorFormat(2);
\r
385 PicoScanBegin = EmuScanBegin8;
\r
387 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
388 // setup pal for 8-bit modes
\r
389 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
390 localPal[0xd0] = 0x00c00000;
\r
391 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
392 localPal[0xf0] = 0x00ffffff;
\r
393 gp2x_video_setpalette(localPal, 0x100);
\r
394 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
397 Pico.m.dirtyPal = 1;
\r
399 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
400 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
401 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
405 static void emu_msg_cb(const char *msg)
\r
407 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
409 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
410 osd_text(4, 232, msg);
\r
411 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
\r
413 // 16bit accurate renderer
\r
414 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
415 osd_text(4, 232, msg);
\r
416 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
\r
418 gettimeofday(¬iceMsgTime, 0);
\r
419 noticeMsgTime.tv_sec -= 2;
\r
421 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
425 static void emu_state_cb(const char *str)
\r
431 static void emu_msg_tray_open(void)
\r
433 strcpy(noticeMsg, "CD tray opened");
\r
434 gettimeofday(¬iceMsgTime, 0);
\r
437 static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)
\r
439 if (events & (1 << 3)) {
\r
441 if (pico_inp_mode > 2) pico_inp_mode = 0;
\r
442 switch (pico_inp_mode) {
\r
443 case 2: strcpy(noticeMsg, "Input: Pen on Pad "); break;
\r
444 case 1: strcpy(noticeMsg, "Input: Pen on Storyware"); break;
\r
445 case 0: strcpy(noticeMsg, "Input: Joytick ");
\r
446 PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
\r
449 gettimeofday(¬iceMsgTime, 0);
\r
451 if (events & (1 << 4)) {
\r
453 if (PicoPicohw.page < 0) PicoPicohw.page = 0;
\r
454 sprintf(noticeMsg, "Page %i ", PicoPicohw.page);
\r
455 gettimeofday(¬iceMsgTime, 0);
\r
457 if (events & (1 << 5)) {
\r
459 if (PicoPicohw.page > 6) PicoPicohw.page = 6;
\r
460 sprintf(noticeMsg, "Page %i ", PicoPicohw.page);
\r
461 gettimeofday(¬iceMsgTime, 0);
\r
463 if (pico_inp_mode != 0) {
\r
464 PicoPad[0] &= ~0x0f; // release UDLR
\r
465 if (gp2x_keys & GP2X_UP) { pico_pen_y--; if (pico_pen_y < 0) pico_pen_y = 0; }
\r
466 if (gp2x_keys & GP2X_DOWN) { pico_pen_y++; if (pico_pen_y > 239-PICO_PEN_ADJUST_Y) pico_pen_y = 239-PICO_PEN_ADJUST_Y; }
\r
467 if (gp2x_keys & GP2X_LEFT) { pico_pen_x--; if (pico_pen_x < 0) pico_pen_x = 0; }
\r
468 if (gp2x_keys & GP2X_RIGHT) {
\r
469 int lim = (Pico.video.reg[12]&1) ? 319 : 255;
\r
471 if (pico_pen_x > lim-PICO_PEN_ADJUST_X)
\r
472 pico_pen_x = lim-PICO_PEN_ADJUST_X;
\r
474 PicoPicohw.pen_pos[0] = pico_pen_x;
\r
475 if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
\r
476 PicoPicohw.pen_pos[0] += 0x3c;
\r
477 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
\r
481 static void update_volume(int has_changed, int is_up)
\r
483 static int prev_frame = 0, wait_frames = 0;
\r
484 int vol = currentConfig.volume;
\r
488 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
\r
492 if (vol < 99) vol++;
\r
494 if (vol > 0) vol--;
\r
497 gp2x_sound_volume(vol, vol);
\r
498 currentConfig.volume = vol;
\r
500 sprintf(noticeMsg, "VOL: %02i", vol);
\r
501 gettimeofday(¬iceMsgTime, 0);
\r
502 prev_frame = Pico.m.frame_count;
\r
505 // set the right mixer func
\r
506 if (!(PicoOpt&8)) return; // just use defaults for mono
\r
508 PsndMix_32_to_16l = mix_32_to_16l_stereo;
\r
510 mix_32_to_16l_level = 5 - vol;
\r
511 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
\r
515 static void change_fast_forward(int set_on)
\r
517 static void *set_PsndOut = NULL;
\r
518 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
520 if (set_on && !is_on) {
\r
521 set_PsndOut = PsndOut;
\r
522 set_Frameskip = currentConfig.Frameskip;
\r
523 set_EmuOpt = currentConfig.EmuOpt;
\r
525 currentConfig.Frameskip = 8;
\r
526 currentConfig.EmuOpt &= ~4;
\r
529 else if (!set_on && is_on) {
\r
530 PsndOut = set_PsndOut;
\r
531 currentConfig.Frameskip = set_Frameskip;
\r
532 currentConfig.EmuOpt = set_EmuOpt;
\r
534 update_volume(0, 0);
\r
540 static void RunEvents(unsigned int which)
\r
542 if (which & 0x1800) // save or load (but not both)
\r
545 if ( emu_checkSaveFile(state_slot) &&
\r
546 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
547 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
\r
548 unsigned long keys;
\r
549 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
\r
550 while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
552 if (keys & GP2X_X) do_it = 0;
\r
553 while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release
\r
558 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
559 PicoStateProgressCB = emu_state_cb;
\r
560 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
\r
561 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
562 PicoStateProgressCB = NULL;
\r
567 if (which & 0x0400) // switch renderer
\r
569 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
570 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
571 else currentConfig.EmuOpt &= ~0x80;
\r
575 if (PicoOpt&0x10) {
\r
576 strcpy(noticeMsg, " 8bit fast renderer");
\r
577 } else if (currentConfig.EmuOpt&0x80) {
\r
578 strcpy(noticeMsg, "16bit accurate renderer");
\r
580 strcpy(noticeMsg, " 8bit accurate renderer");
\r
583 gettimeofday(¬iceMsgTime, 0);
\r
585 if (which & 0x0300)
\r
589 if(state_slot < 0) state_slot = 9;
\r
592 if(state_slot > 9) state_slot = 0;
\r
594 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
\r
595 gettimeofday(¬iceMsgTime, 0);
\r
597 if (which & 0x0080) {
\r
598 engineState = PGS_Menu;
\r
602 static void updateKeys(void)
\r
604 unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;
\r
605 static unsigned int prevEvents = 0;
\r
608 keys = gp2x_joystick_read(0);
\r
609 if (keys & GP2X_SELECT) {
\r
610 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
611 // wait until select is released, so menu would not resume game
\r
612 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
615 keys &= CONFIGURABLE_KEYS;
\r
618 for (i = 0; i < 32; i++)
\r
620 if (keys2 & (1 << i))
\r
622 int pl, acts = currentConfig.KeyBinds[i];
\r
623 if (!acts) continue;
\r
624 pl = (acts >> 16) & 1;
\r
625 if (kb_combo_keys & (1 << i))
\r
627 int u = i+1, acts_c = acts & kb_combo_acts;
\r
628 // let's try to find the other one
\r
630 for (; u < 32; u++)
\r
631 if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {
\r
632 allActions[pl] |= acts_c & currentConfig.KeyBinds[u];
\r
633 keys2 &= ~((1 << i) | (1 << u));
\r
637 // add non-combo actions if combo ones were not found
\r
638 if (!acts_c || u == 32)
\r
639 allActions[pl] |= acts & ~kb_combo_acts;
\r
641 allActions[pl] |= acts;
\r
647 if (num_of_joys > 0)
\r
649 gp2x_usbjoy_update();
\r
650 for (joy = 0; joy < num_of_joys; joy++) {
\r
651 int btns = gp2x_usbjoy_check2(joy);
\r
652 for (i = 0; i < 32; i++) {
\r
653 if (btns & (1 << i)) {
\r
654 int acts = currentConfig.JoyBinds[joy][i];
\r
655 int pl = (acts >> 16) & 1;
\r
656 allActions[pl] |= acts;
\r
662 PicoPad[0] = (unsigned short) allActions[0];
\r
663 PicoPad[1] = (unsigned short) allActions[1];
\r
665 events = (allActions[0] | allActions[1]) >> 16;
\r
667 // volume is treated in special way and triggered every frame
\r
668 if (events & 0x6000)
\r
669 update_volume(1, events & 0x2000);
\r
671 if ((events ^ prevEvents) & 0x40)
\r
672 change_fast_forward(events & 0x40);
\r
674 events &= ~prevEvents;
\r
676 if (PicoAHW == PAHW_PICO)
\r
677 RunEventsPico(events, keys);
\r
679 if (events) RunEvents(events);
\r
680 if (movie_data) emu_updateMovie();
\r
682 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
686 static void updateSound(int len)
\r
688 if (PicoOpt&8) len<<=1;
\r
690 /* avoid writing audio when lagging behind to prevent audio lag */
\r
691 if (PicoSkipFrame != 2)
\r
692 gp2x_sound_write(PsndOut, len<<1);
\r
696 static void SkipFrame(int do_audio)
\r
698 PicoSkipFrame=do_audio ? 1 : 2;
\r
704 void emu_forcedFrame(int opts)
\r
706 int po_old = PicoOpt;
\r
707 int eo_old = currentConfig.EmuOpt;
\r
710 PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
\r
711 currentConfig.EmuOpt |= 0x80;
\r
714 PicoDrawSetColorFormat(1);
\r
715 PicoScanBegin = EmuScanBegin16;
\r
716 Pico.m.dirtyPal = 1;
\r
717 PicoFrameDrawOnly();
\r
720 if (!(Pico.video.reg[12]&1)) {
\r
721 vidCpyM2 = vidCpyM2_32col;
\r
723 } else vidCpyM2 = vidCpyM2_40col;
\r
725 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
726 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
727 gp2x_video_setpalette(localPal, 0x40);
\r
730 currentConfig.EmuOpt = eo_old;
\r
733 static void simpleWait(int thissec, int lim_time)
\r
735 struct timeval tval;
\r
737 spend_cycles(1024);
\r
738 gettimeofday(&tval, 0);
\r
739 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
741 while (tval.tv_usec < lim_time)
\r
743 spend_cycles(1024);
\r
744 gettimeofday(&tval, 0);
\r
745 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
751 static void tga_dump(void)
\r
753 #define BYTE unsigned char
\r
754 #define WORD unsigned short
\r
757 BYTE IDLength; /* 00h Size of Image ID field */
\r
758 BYTE ColorMapType; /* 01h Color map type */
\r
759 BYTE ImageType; /* 02h Image type code */
\r
760 WORD CMapStart; /* 03h Color map origin */
\r
761 WORD CMapLength; /* 05h Color map length */
\r
762 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
763 WORD XOffset; /* 08h X origin of image */
\r
764 WORD YOffset; /* 0Ah Y origin of image */
\r
765 WORD Width; /* 0Ch Width of image */
\r
766 WORD Height; /* 0Eh Height of image */
\r
767 BYTE PixelDepth; /* 10h Image pixel size */
\r
768 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
769 } __attribute__((packed)) TGAHEAD;
\r
770 static unsigned short oldscr[320*240];
\r
771 FILE *f; char name[128]; int i;
\r
773 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
774 TGAHEAD.ImageType = 2;
\r
775 TGAHEAD.Width = 320;
\r
776 TGAHEAD.Height = 240;
\r
777 TGAHEAD.PixelDepth = 16;
\r
778 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
780 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
782 for (i = 0; i < 320*240; i++)
\r
783 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
786 for (i = 0; i < 320*240; i++)
\r
787 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
788 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
789 f = fopen(name, "wb");
\r
790 if (!f) { printf("!f\n"); exit(1); }
\r
791 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
792 fwrite(oldscr, 1, 320*240*2, f);
\r
799 void emu_Loop(void)
\r
801 static int gp2x_old_clock = 200;
\r
802 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;
\r
803 char fpsbuff[24]; // fps count c string
\r
804 struct timeval tval; // timing
\r
805 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
\r
806 int frames_done, frames_shown, thissec; // actual frames
\r
807 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
\r
810 printf("entered emu_Loop()\n");
\r
812 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
813 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
814 set_FCLK(currentConfig.CPUclock);
\r
815 gp2x_old_clock = currentConfig.CPUclock;
\r
819 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
\r
820 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
821 gp2x_old_gamma = currentConfig.gamma;
\r
822 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
825 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
\r
826 if (currentConfig.EmuOpt&0x2000)
\r
827 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
\r
828 else unset_LCD_custom_rate();
\r
831 EmuOpt_old = currentConfig.EmuOpt;
\r
834 // make sure we are in correct mode
\r
837 Pico.m.dirtyPal = 1;
\r
838 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
839 emu_findKeyBindCombos();
\r
841 // pal/ntsc might have changed, reset related stuff
\r
842 target_fps = Pico.m.pal ? 50 : 60;
\r
843 target_frametime = 1000000/target_fps;
\r
846 // prepare sound stuff
\r
847 if (currentConfig.EmuOpt & 4)
\r
849 int snd_excess_add;
\r
850 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
\r
851 ((PicoOpt&0x200) && crashed_940)) {
\r
852 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
854 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
855 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
856 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
857 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
858 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
859 PicoWriteSound = updateSound;
\r
860 update_volume(0, 0);
\r
861 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
862 PsndOut = sndBuffer;
\r
863 PsndRate_old = PsndRate;
\r
864 PicoOpt_old = PicoOpt;
\r
865 pal_old = Pico.m.pal;
\r
870 // prepare CD buffer
\r
871 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
\r
873 // calc vsync offset to sync timing code with vsync
\r
874 if (currentConfig.EmuOpt&0x2000) {
\r
875 gettimeofday(&tval, 0);
\r
876 gp2x_video_wait_vsync();
\r
877 gettimeofday(&tval, 0);
\r
878 vsync_offset = tval.tv_usec;
\r
879 while (vsync_offset >= target_frametime)
\r
880 vsync_offset -= target_frametime;
\r
881 if (!vsync_offset) vsync_offset++;
\r
882 printf("vsync_offset: %i\n", vsync_offset);
\r
886 frames_done = frames_shown = thissec =
\r
887 pframes_done = pframes_shown = pthissec = 0;
\r
890 while (engineState == PGS_Running)
\r
894 gettimeofday(&tval, 0);
\r
895 if (reset_timing) {
\r
897 pthissec = tval.tv_sec;
\r
898 pframes_shown = pframes_done = tval.tv_usec/target_frametime;
\r
901 // show notice message?
\r
902 if (noticeMsgTime.tv_sec)
\r
904 static int noticeMsgSum;
\r
905 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
906 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
910 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
911 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
912 notice = noticeMsg;
\r
916 // check for mode changes
\r
917 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
918 if (modes != oldmodes)
\r
921 osd_fps_x = OSD_FPS_X;
\r
923 vidCpyM2 = vidCpyM2_40col;
\r
925 if (PicoOpt & 0x100) {
\r
926 vidCpyM2 = vidCpyM2_32col_nobord;
\r
928 osd_fps_x = OSD_FPS_X - 64;
\r
930 vidCpyM2 = vidCpyM2_32col;
\r
933 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
934 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
935 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
941 if (thissec != tval.tv_sec)
\r
944 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
945 if (++bench == 10) {
\r
947 bench_fps_s = bench_fps;
\r
948 bf[bfp++ & 3] = bench_fps;
\r
951 bench_fps += frames_shown;
\r
952 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
954 if (currentConfig.EmuOpt & 2) {
\r
955 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
956 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
959 frames_shown = frames_done = 0;
\r
960 thissec = tval.tv_sec;
\r
963 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
966 if (pthissec != tval.tv_sec)
\r
968 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
969 pframes_done = pframes_shown = 0;
\r
971 // it is quite common for this implementation to leave 1 fame unfinished
\r
972 // when second changes, but we don't want buffer to starve.
\r
973 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
\r
975 SkipFrame(1); pframes_done++;
\r
978 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
\r
979 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
\r
980 if (pframes_shown > pframes_done) pframes_shown = pframes_done;
\r
982 pthissec = tval.tv_sec;
\r
985 lim_time = (pframes_done+1) * target_frametime + vsync_offset;
\r
986 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
988 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
990 SkipFrame(1); pframes_done++; frames_done++;
\r
991 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
\r
992 gettimeofday(&tval, 0);
\r
993 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
994 if (tval.tv_usec < lim_time) { // we are too fast
\r
995 simpleWait(pthissec, lim_time);
\r
998 lim_time += target_frametime;
\r
1001 else if (tval.tv_usec > lim_time) // auto frameskip
\r
1003 // no time left for this frame - skip
\r
1004 if (tval.tv_usec - lim_time >= 300000) {
\r
1005 /* something caused a slowdown for us (disk access? cache flush?)
\r
1006 * try to recover by resetting timing... */
\r
1011 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
\r
1019 gettimeofday(&tval, 0);
\r
1020 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1022 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
\r
1024 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
1026 // sleep or vsync if we are still too fast
\r
1027 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
1028 if (!reset_timing && tval.tv_usec < lim_time)
\r
1030 // we are too fast
\r
1031 if (vsync_offset) {
\r
1032 if (lim_time - tval.tv_usec > target_frametime/2)
\r
1033 simpleWait(pthissec, lim_time - target_frametime/4);
\r
1034 gp2x_video_wait_vsync();
\r
1036 simpleWait(pthissec, lim_time);
\r
1041 blit(fpsbuff, notice);
\r
1043 pframes_done++; pframes_shown++;
\r
1044 frames_done++; frames_shown++;
\r
1047 change_fast_forward(0);
\r
1049 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
\r
1052 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1053 emu_state_cb("Writing SRAM/BRAM..");
\r
1054 emu_SaveLoadGame(0, 1);
\r
1058 // if in 8bit mode, generate 16bit image for menu background
\r
1059 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
1060 emu_forcedFrame(POPT_EN_SOFTSCALE);
\r
1064 void emu_ResetGame(void)
\r