1 // (c) Copyright 2006-2009 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
13 #include "plat_gp2x.h"
\r
15 #include "../common/plat.h"
\r
16 #include "../common/menu.h"
\r
17 #include "../common/arm_utils.h"
\r
18 #include "../common/fonts.h"
\r
19 #include "../common/emu.h"
\r
20 #include "../common/config.h"
\r
21 #include "../common/input.h"
\r
22 #include "../linux/sndout_oss.h"
\r
23 #include "version.h"
\r
25 #include <pico/pico_int.h>
\r
26 #include <pico/patch.h>
\r
27 #include <pico/sound/mix.h>
\r
28 #include <zlib/zlib.h>
\r
33 #define OSD_FPS_X 220
\r
35 #define OSD_FPS_X 260
\r
39 extern int crashed_940;
\r
41 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
42 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
43 static int osd_fps_x;
\r
44 static int gp2x_old_gamma = 100;
\r
45 unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
\r
46 int reset_timing = 0;
\r
48 #define PICO_PEN_ADJUST_X 4
\r
49 #define PICO_PEN_ADJUST_Y 2
\r
50 static int pico_pen_x = 320/2, pico_pen_y = 240/2;
\r
52 static void emu_msg_cb(const char *msg);
\r
53 static void emu_msg_tray_open(void);
\r
56 void emu_noticeMsgUpdated(void)
\r
58 gettimeofday(¬iceMsgTime, 0);
\r
61 int emu_getMainDir(char *dst, int len)
\r
63 extern char **g_argv;
\r
66 strncpy(dst, g_argv[0], len);
\r
67 len -= 32; // reserve
\r
68 if (len < 0) len = 0;
\r
70 for (j = strlen(dst); j > 0; j--)
\r
71 if (dst[j] == '/') { dst[j+1] = 0; break; }
\r
78 // make temp buffer for alt renderer
\r
79 PicoDraw2FB = malloc((8+320)*(8+240+8));
\r
82 printf("PicoDraw2FB == 0\n");
\r
85 // make dirs for saves, cfgs, etc.
\r
92 PicoMessage = emu_msg_cb;
\r
93 PicoMCDopenTray = emu_msg_tray_open;
\r
94 PicoMCDcloseTray = menu_loop_tray;
\r
98 static void scaling_update(void)
\r
100 PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);
\r
101 switch (currentConfig.scaling) {
\r
103 case EOPT_SCALE_HW_H:
\r
104 case EOPT_SCALE_HW_HV:
\r
105 PicoOpt |= POPT_DIS_32C_BORDER;
\r
107 case EOPT_SCALE_SW_H:
\r
108 PicoOpt |= POPT_EN_SOFTSCALE;
\r
114 void emu_Deinit(void)
\r
117 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
118 emu_SaveLoadGame(0, 1);
\r
122 if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG))
\r
130 if (gp2x_old_gamma != 100)
\r
131 set_lcd_gamma(100, 0);
\r
134 void emu_prepareDefaultConfig(void)
\r
138 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
139 defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS | 0x600; // | <- confirm_save, cd_leds
\r
140 defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
\r
141 defaultConfig.s_PsndRate = 44100;
\r
142 defaultConfig.s_PicoRegion = 0; // auto
\r
143 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
144 defaultConfig.s_PicoCDBuffers = 0;
\r
145 defaultConfig.Frameskip = -1; // auto
\r
146 defaultConfig.CPUclock = default_cpu_clock;
\r
147 defaultConfig.volume = 50;
\r
148 defaultConfig.gamma = 100;
\r
149 defaultConfig.scaling = 0;
\r
150 defaultConfig.turbo_rate = 15;
\r
152 soc = soc_detect();
\r
153 if (soc == SOCID_MMSP2)
\r
154 defaultConfig.s_PicoOpt |= POPT_EXT_FM;
\r
157 static void osd_text(int x, int y, const char *text)
\r
159 int len = strlen(text)*8;
\r
160 int *p, i, h, offs;
\r
162 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
163 len = (len+3) >> 2;
\r
164 for (h = 0; h < 8; h++) {
\r
165 offs = (x + g_screen_width * (y+h)) & ~3;
\r
166 p = (int *) ((char *)g_screen_ptr + offs);
\r
167 for (i = len; i; i--, p++)
\r
170 emu_textOut8(x, y, text);
\r
172 len = (len+1) >> 1;
\r
173 for (h = 0; h < 8; h++) {
\r
174 offs = (x + g_screen_width * (y+h)) & ~1;
\r
175 p = (int *) ((short *)g_screen_ptr + offs);
\r
176 for (i = len; i; i--, p++)
\r
177 *p = (*p >> 2) & 0x39e7;
\r
179 emu_textOut16(x, y, text);
\r
183 static void draw_cd_leds(void)
\r
187 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
\r
188 old_reg = Pico_mcd->s68k_regs[0];
\r
190 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
192 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
193 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
194 *(unsigned int *)((char *)g_screen_ptr + 320*2+ 4) =
\r
195 *(unsigned int *)((char *)g_screen_ptr + 320*3+ 4) =
\r
196 *(unsigned int *)((char *)g_screen_ptr + 320*4+ 4) = col_g;
\r
197 *(unsigned int *)((char *)g_screen_ptr + 320*2+12) =
\r
198 *(unsigned int *)((char *)g_screen_ptr + 320*3+12) =
\r
199 *(unsigned int *)((char *)g_screen_ptr + 320*4+12) = col_r;
\r
202 unsigned int *p = (unsigned int *)((short *)g_screen_ptr + 320*2+4);
\r
203 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
204 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
205 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
206 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
207 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
211 static void draw_pico_ptr(void)
\r
213 unsigned short *p = (unsigned short *)g_screen_ptr;
\r
215 // only if pen enabled and for 16bit modes
\r
216 if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
\r
218 if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
\r
221 p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);
\r
222 p += pico_pen_x + PICO_PEN_ADJUST_X;
\r
230 static int EmuScanBegin16(unsigned int num)
\r
232 if (!(Pico.video.reg[1]&8)) num += 8;
\r
233 DrawLineDest = (unsigned short *) g_screen_ptr + g_screen_width * num;
\r
238 static int EmuScanBegin8(unsigned int num)
\r
240 if (!(Pico.video.reg[1]&8)) num += 8;
\r
241 DrawLineDest = (unsigned char *) g_screen_ptr + g_screen_width * num;
\r
246 int localPal[0x100];
\r
247 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
249 static void blit(const char *fps, const char *notice)
\r
251 int emu_opt = currentConfig.EmuOpt;
\r
255 // 8bit fast renderer
\r
256 if (Pico.m.dirtyPal) {
\r
257 Pico.m.dirtyPal = 0;
\r
258 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
259 // feed new palette to our device
\r
260 gp2x_video_setpalette(localPal, 0x40);
\r
263 if (PicoRead16Hook == PicoSVPRead16)
\r
264 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
\r
266 vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);
\r
268 else if (!(emu_opt&0x80))
\r
270 // 8bit accurate renderer
\r
271 if (Pico.m.dirtyPal)
\r
274 Pico.m.dirtyPal = 0;
\r
275 if (Pico.video.reg[0xC]&8) // shadow/hilight mode
\r
277 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
278 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
279 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
280 memcpy32(localPal+0xc0, localPal+0x40, 0x40);
\r
283 else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
\r
284 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
285 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
286 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
289 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
290 memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess
\r
292 if (pallen > 0xc0) {
\r
293 localPal[0xc0] = 0x0000c000;
\r
294 localPal[0xd0] = 0x00c00000;
\r
295 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
296 localPal[0xf0] = 0x00ffffff;
\r
298 gp2x_video_setpalette(localPal, pallen);
\r
302 if (notice || (emu_opt & 2)) {
\r
304 if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))
\r
307 osd_text(4, h, notice);
\r
309 osd_text(osd_fps_x, h, fps);
\r
311 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
\r
313 if (PicoAHW & PAHW_PICO)
\r
316 //gp2x_video_wait_vsync();
\r
319 if (!(PicoOpt&0x10)) {
\r
320 if (!(Pico.video.reg[1]&8)) {
\r
321 if (currentConfig.EmuOpt&0x80) {
\r
322 DrawLineDest = (unsigned short *) g_screen_ptr + 320*8;
\r
324 DrawLineDest = (unsigned char *) g_screen_ptr + 320*8;
\r
327 DrawLineDest = g_screen_ptr;
\r
333 // clears whole screen or just the notice area (in all buffers)
\r
334 static void clearArea(int full)
\r
336 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
338 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
339 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
341 // 16bit accurate renderer
\r
342 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
343 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
348 static void vidResetMode(void)
\r
350 if (PicoOpt&0x10) {
\r
351 gp2x_video_changemode(8);
\r
352 } else if (currentConfig.EmuOpt&0x80) {
\r
353 gp2x_video_changemode(16);
\r
354 PicoDrawSetColorFormat(1);
\r
355 PicoScanBegin = EmuScanBegin16;
\r
357 gp2x_video_changemode(8);
\r
358 PicoDrawSetColorFormat(2);
\r
359 PicoScanBegin = EmuScanBegin8;
\r
361 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
362 // setup pal for 8-bit modes
\r
363 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
364 localPal[0xd0] = 0x00c00000;
\r
365 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
366 localPal[0xf0] = 0x00ffffff;
\r
367 gp2x_video_setpalette(localPal, 0x100);
\r
368 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
371 Pico.m.dirtyPal = 1;
\r
373 if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))
\r
374 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
375 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
379 static void emu_msg_cb(const char *msg)
\r
381 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
383 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
384 osd_text(4, 232, msg);
\r
385 gp2x_memcpy_all_buffers((char *)g_screen_ptr+320*232, 320*232, 320*8);
\r
387 // 16bit accurate renderer
\r
388 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
389 osd_text(4, 232, msg);
\r
390 gp2x_memcpy_all_buffers((char *)g_screen_ptr+320*232*2, 320*232*2, 320*8*2);
\r
392 gettimeofday(¬iceMsgTime, 0);
\r
393 noticeMsgTime.tv_sec -= 2;
\r
395 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
399 static void emu_state_cb(const char *str)
\r
405 static void emu_msg_tray_open(void)
\r
407 strcpy(noticeMsg, "CD tray opened");
\r
408 gettimeofday(¬iceMsgTime, 0);
\r
411 static void RunEventsPico(unsigned int events)
\r
413 int ret, px, py, lim_x;
\r
414 static int pdown_frames = 0;
\r
416 emu_RunEventsPico(events);
\r
418 if (pico_inp_mode == 0) return;
\r
421 ret = gp2x_touchpad_read(&px, &py);
\r
426 if (pdown_frames++ > 5)
\r
427 PicoPad[0] |= 0x20;
\r
431 if (!(Pico.video.reg[12]&1)) {
\r
433 if (pico_pen_x < 0) pico_pen_x = 0;
\r
434 if (pico_pen_x > 248) pico_pen_x = 248;
\r
436 if (pico_pen_y > 224) pico_pen_y = 224;
\r
442 // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
\r
445 if (PicoPad[0] & 1) pico_pen_y--;
\r
446 if (PicoPad[0] & 2) pico_pen_y++;
\r
447 if (PicoPad[0] & 4) pico_pen_x--;
\r
448 if (PicoPad[0] & 8) pico_pen_x++;
\r
449 PicoPad[0] &= ~0x0f; // release UDLR
\r
451 lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
\r
452 if (pico_pen_y < 8) pico_pen_y = 8;
\r
453 if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;
\r
454 if (pico_pen_x < 0) pico_pen_x = 0;
\r
455 if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;
\r
457 PicoPicohw.pen_pos[0] = pico_pen_x;
\r
458 if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
\r
459 PicoPicohw.pen_pos[0] += 0x3c;
\r
460 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
\r
463 static void update_volume(int has_changed, int is_up)
\r
465 static int prev_frame = 0, wait_frames = 0;
\r
466 int vol = currentConfig.volume;
\r
470 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
\r
474 if (vol < 99) vol++;
\r
476 if (vol > 0) vol--;
\r
479 sndout_oss_setvol(vol, vol);
\r
480 currentConfig.volume = vol;
\r
482 sprintf(noticeMsg, "VOL: %02i", vol);
\r
483 gettimeofday(¬iceMsgTime, 0);
\r
484 prev_frame = Pico.m.frame_count;
\r
487 // set the right mixer func
\r
488 if (!(PicoOpt&8)) return; // just use defaults for mono
\r
490 PsndMix_32_to_16l = mix_32_to_16l_stereo;
\r
492 mix_32_to_16l_level = 5 - vol;
\r
493 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
\r
497 static void RunEvents(unsigned int which)
\r
499 if (which & 0x1800) // save or load (but not both)
\r
502 if ( emu_checkSaveFile(state_slot) &&
\r
503 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
504 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
\r
510 strcpy(tmp, (which & 0x1000) ? "LOAD STATE? " : "OVERWRITE SAVE? ");
\r
512 nm = in_get_key_name(-1, -PBTN_MA3);
\r
513 snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
\r
515 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
516 snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
\r
520 in_set_blocking(1);
\r
521 while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK)); // wait for release
\r
522 while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) ); // .. press
\r
523 if (keys & PBTN_MBACK)
\r
525 while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK)); // .. release
\r
526 in_set_blocking(0);
\r
531 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
532 PicoStateProgressCB = emu_state_cb;
\r
533 gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);
\r
534 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
535 PicoStateProgressCB = NULL;
\r
540 if (which & 0x0400) // switch renderer
\r
542 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
543 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
544 else currentConfig.EmuOpt &= ~0x80;
\r
548 if (PicoOpt&0x10) {
\r
549 strcpy(noticeMsg, " 8bit fast renderer");
\r
550 } else if (currentConfig.EmuOpt&0x80) {
\r
551 strcpy(noticeMsg, "16bit accurate renderer");
\r
553 strcpy(noticeMsg, " 8bit accurate renderer");
\r
556 emu_noticeMsgUpdated();
\r
558 if (which & 0x0300)
\r
562 if(state_slot < 0) state_slot = 9;
\r
565 if(state_slot > 9) state_slot = 0;
\r
567 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
\r
568 emu_noticeMsgUpdated();
\r
570 if (which & 0x0080) {
\r
571 engineState = PGS_Menu;
\r
575 static void updateKeys(void)
\r
577 unsigned int allActions[2] = { 0, 0 }, events;
\r
578 static unsigned int prevEvents = 0;
\r
580 /* FIXME: player2 */
\r
581 allActions[0] = in_update();
\r
583 PicoPad[0] = allActions[0] & 0xfff;
\r
584 PicoPad[1] = allActions[1] & 0xfff;
\r
586 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
\r
587 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
\r
589 events = (allActions[0] | allActions[1]) >> 16;
\r
591 // volume is treated in special way and triggered every frame
\r
592 if (events & 0x6000)
\r
593 update_volume(1, events & 0x2000);
\r
595 if ((events ^ prevEvents) & 0x40) {
\r
596 emu_changeFastForward(events & 0x40);
\r
597 update_volume(0, 0);
\r
601 events &= ~prevEvents;
\r
603 if (PicoAHW == PAHW_PICO)
\r
604 RunEventsPico(events);
\r
605 if (events) RunEvents(events);
\r
606 if (movie_data) emu_updateMovie();
\r
608 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
612 static void updateSound(int len)
\r
614 if (PicoOpt&8) len<<=1;
\r
616 /* avoid writing audio when lagging behind to prevent audio lag */
\r
617 if (PicoSkipFrame != 2)
\r
618 sndout_oss_write(PsndOut, len<<1);
\r
621 void emu_startSound(void)
\r
623 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
\r
624 int target_fps = Pico.m.pal ? 50 : 60;
\r
628 // prepare sound stuff
\r
629 if (currentConfig.EmuOpt & 4)
\r
631 int snd_excess_add;
\r
632 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
\r
633 ((PicoOpt&0x200) && crashed_940)) {
\r
634 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
636 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
637 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
638 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
639 sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);
\r
640 sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
\r
641 PicoWriteSound = updateSound;
\r
642 update_volume(0, 0);
\r
643 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
644 PsndOut = sndBuffer;
\r
645 PsndRate_old = PsndRate;
\r
646 PicoOpt_old = PicoOpt;
\r
647 pal_old = Pico.m.pal;
\r
651 void emu_endSound(void)
\r
655 /* wait until we can write more sound */
\r
656 void emu_waitSound(void)
\r
658 // don't need to do anything, writes will block by themselves
\r
662 static void SkipFrame(int do_audio)
\r
664 PicoSkipFrame=do_audio ? 1 : 2;
\r
670 void emu_forcedFrame(int opts)
\r
672 int po_old = PicoOpt;
\r
673 int eo_old = currentConfig.EmuOpt;
\r
676 PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
\r
677 currentConfig.EmuOpt |= 0x80;
\r
680 PicoDrawSetColorFormat(1);
\r
681 PicoScanBegin = EmuScanBegin16;
\r
682 Pico.m.dirtyPal = 1;
\r
683 PicoFrameDrawOnly();
\r
686 if (!(Pico.video.reg[12]&1)) {
\r
687 vidCpyM2 = vidCpyM2_32col;
\r
689 } else vidCpyM2 = vidCpyM2_40col;
\r
691 vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);
\r
692 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
693 gp2x_video_setpalette(localPal, 0x40);
\r
696 currentConfig.EmuOpt = eo_old;
\r
699 void emu_platformDebugCat(char *str)
\r
704 static void simpleWait(int thissec, int lim_time)
\r
706 struct timeval tval;
\r
708 spend_cycles(1024);
\r
709 gettimeofday(&tval, 0);
\r
710 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
712 while (tval.tv_usec < lim_time)
\r
714 spend_cycles(1024);
\r
715 gettimeofday(&tval, 0);
\r
716 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
722 static void tga_dump(void)
\r
724 #define BYTE unsigned char
\r
725 #define WORD unsigned short
\r
728 BYTE IDLength; /* 00h Size of Image ID field */
\r
729 BYTE ColorMapType; /* 01h Color map type */
\r
730 BYTE ImageType; /* 02h Image type code */
\r
731 WORD CMapStart; /* 03h Color map origin */
\r
732 WORD CMapLength; /* 05h Color map length */
\r
733 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
734 WORD XOffset; /* 08h X origin of image */
\r
735 WORD YOffset; /* 0Ah Y origin of image */
\r
736 WORD Width; /* 0Ch Width of image */
\r
737 WORD Height; /* 0Eh Height of image */
\r
738 BYTE PixelDepth; /* 10h Image pixel size */
\r
739 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
740 } __attribute__((packed)) TGAHEAD;
\r
741 static unsigned short oldscr[320*240];
\r
742 FILE *f; char name[128]; int i;
\r
744 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
745 TGAHEAD.ImageType = 2;
\r
746 TGAHEAD.Width = 320;
\r
747 TGAHEAD.Height = 240;
\r
748 TGAHEAD.PixelDepth = 16;
\r
749 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
751 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
753 for (i = 0; i < 320*240; i++)
\r
754 if(oldscr[i] != CONV(((unsigned short *)g_screen_ptr)[i])) break;
\r
757 for (i = 0; i < 320*240; i++)
\r
758 oldscr[i] = CONV(((unsigned short *)g_screen_ptr)[i]);
\r
759 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
760 f = fopen(name, "wb");
\r
761 if (!f) { printf("!f\n"); exit(1); }
\r
762 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
763 fwrite(oldscr, 1, 320*240*2, f);
\r
770 void emu_Loop(void)
\r
772 static int gp2x_old_clock = -1, EmuOpt_old = 0;
\r
773 char fpsbuff[24]; // fps count c string
\r
774 struct timeval tval; // timing
\r
775 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
\r
776 int frames_done, frames_shown, thissec; // actual frames
\r
777 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
\r
780 printf("entered emu_Loop()\n");
\r
782 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {
\r
783 if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)
\r
786 unset_ram_timings();
\r
789 if (gp2x_old_clock < 0)
\r
790 gp2x_old_clock = default_cpu_clock;
\r
791 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
792 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
793 gp2x_set_cpuclk(currentConfig.CPUclock);
\r
794 gp2x_old_clock = currentConfig.CPUclock;
\r
798 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
\r
799 set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
800 gp2x_old_gamma = currentConfig.gamma;
\r
801 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
804 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_PSYNC) {
\r
805 if (currentConfig.EmuOpt & EOPT_PSYNC)
\r
806 set_lcd_custom_rate(Pico.m.pal);
\r
808 unset_lcd_custom_rate();
\r
811 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)
\r
812 gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);
\r
814 EmuOpt_old = currentConfig.EmuOpt;
\r
817 // make sure we are in correct mode
\r
820 Pico.m.dirtyPal = 1;
\r
821 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
823 // pal/ntsc might have changed, reset related stuff
\r
824 target_fps = Pico.m.pal ? 50 : 60;
\r
825 target_frametime = 1000000/target_fps;
\r
830 // prepare CD buffer
\r
831 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
\r
833 // calc vsync offset to sync timing code with vsync
\r
834 if (currentConfig.EmuOpt&0x2000) {
\r
835 gettimeofday(&tval, 0);
\r
836 gp2x_video_wait_vsync();
\r
837 gettimeofday(&tval, 0);
\r
838 vsync_offset = tval.tv_usec;
\r
839 while (vsync_offset >= target_frametime)
\r
840 vsync_offset -= target_frametime;
\r
841 if (!vsync_offset) vsync_offset++;
\r
842 printf("vsync_offset: %i\n", vsync_offset);
\r
846 frames_done = frames_shown = thissec =
\r
847 pframes_done = pframes_shown = pthissec = 0;
\r
850 while (engineState == PGS_Running)
\r
854 gettimeofday(&tval, 0);
\r
855 if (reset_timing) {
\r
857 pthissec = tval.tv_sec;
\r
858 pframes_shown = pframes_done = tval.tv_usec/target_frametime;
\r
861 // show notice message?
\r
862 if (noticeMsgTime.tv_sec)
\r
864 static int noticeMsgSum;
\r
865 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
866 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
870 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
871 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
872 notice = noticeMsg;
\r
876 // check for mode changes
\r
877 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
878 if (modes != oldmodes)
\r
881 osd_fps_x = OSD_FPS_X;
\r
883 vidCpyM2 = vidCpyM2_40col;
\r
885 if (PicoOpt & 0x100) {
\r
886 vidCpyM2 = vidCpyM2_32col_nobord;
\r
888 osd_fps_x = OSD_FPS_X - 64;
\r
890 vidCpyM2 = vidCpyM2_32col;
\r
893 /* want vertical scaling and game is not in 240 line mode */
\r
894 if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(modes&8))
\r
895 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
896 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
902 if (thissec != tval.tv_sec)
\r
905 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
906 if (++bench == 10) {
\r
908 bench_fps_s = bench_fps;
\r
909 bf[bfp++ & 3] = bench_fps;
\r
912 bench_fps += frames_shown;
\r
913 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
915 if (currentConfig.EmuOpt & 2) {
\r
916 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
917 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
920 frames_shown = frames_done = 0;
\r
921 thissec = tval.tv_sec;
\r
924 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
927 if (pthissec != tval.tv_sec)
\r
929 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
930 pframes_done = pframes_shown = 0;
\r
932 // it is quite common for this implementation to leave 1 fame unfinished
\r
933 // when second changes, but we don't want buffer to starve.
\r
934 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
\r
936 SkipFrame(1); pframes_done++;
\r
939 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
\r
940 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
\r
941 if (pframes_shown > pframes_done) pframes_shown = pframes_done;
\r
943 pthissec = tval.tv_sec;
\r
946 lim_time = (pframes_done+1) * target_frametime + vsync_offset;
\r
947 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
949 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
951 SkipFrame(1); pframes_done++; frames_done++;
\r
952 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
\r
953 gettimeofday(&tval, 0);
\r
954 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
955 if (tval.tv_usec < lim_time) { // we are too fast
\r
956 simpleWait(pthissec, lim_time);
\r
959 lim_time += target_frametime;
\r
962 else if (tval.tv_usec > lim_time) // auto frameskip
\r
964 // no time left for this frame - skip
\r
965 if (tval.tv_usec - lim_time >= 300000) {
\r
966 /* something caused a slowdown for us (disk access? cache flush?)
\r
967 * try to recover by resetting timing... */
\r
972 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
\r
980 gettimeofday(&tval, 0);
\r
981 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
983 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
\r
985 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
987 // sleep or vsync if we are still too fast
\r
988 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
989 if (!reset_timing && tval.tv_usec < lim_time)
\r
992 if (vsync_offset) {
\r
993 if (lim_time - tval.tv_usec > target_frametime/2)
\r
994 simpleWait(pthissec, lim_time - target_frametime/4);
\r
995 gp2x_video_wait_vsync();
\r
997 simpleWait(pthissec, lim_time);
\r
1002 blit(fpsbuff, notice);
\r
1004 pframes_done++; pframes_shown++;
\r
1005 frames_done++; frames_shown++;
\r
1008 emu_changeFastForward(0);
\r
1010 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
\r
1013 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1014 emu_state_cb("Writing SRAM/BRAM..");
\r
1015 emu_SaveLoadGame(0, 1);
\r
1019 // if in 8bit mode, generate 16bit image for menu background
\r
1020 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
1021 emu_forcedFrame(POPT_EN_SOFTSCALE);
\r
1025 void emu_ResetGame(void)
\r
1031 const char *plat_get_credits(void)
\r
1033 return "PicoDrive v" VERSION " (c) notaz, 2006-2009\n\n\n"
\r
1035 "fDave: Cyclone 68000 core,\n"
\r
1036 " base code of PicoDrive\n"
\r
1037 "Reesy & FluBBa: DrZ80 core\n"
\r
1038 "MAME devs: YM2612 and SN76496 cores\n"
\r
1039 "rlyeh and others: minimal SDK\n"
\r
1040 "Squidge: mmuhack\n"
\r
1041 "Dzz: ARM940 sample\n"
\r
1042 "GnoStiC / Puck2099: USB joy code\n"
\r
1043 "craigix: GP2X hardware\n"
\r
1044 "ketchupgun: skin design\n"
\r
1046 "special thanks (for docs, ideas):\n"
\r
1047 " Charles MacDonald, Haze,\n"
\r
1048 " Stephane Dallongeville,\n"
\r
1049 " Lordus, Exophase, Rokas,\n"
\r
1050 " Nemesis, Tasco Deluxe";
\r