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 static void emu_msg_cb(const char *msg);
\r
57 static void emu_msg_tray_open(void);
\r
60 void emu_noticeMsgUpdated(void)
\r
62 gettimeofday(¬iceMsgTime, 0);
\r
65 void emu_getMainDir(char *dst, int len)
\r
67 extern char **g_argv;
\r
70 strncpy(dst, g_argv[0], len);
\r
71 len -= 32; // reserve
\r
72 if (len < 0) len = 0;
\r
74 for (j = strlen(dst); j > 0; j--)
\r
75 if (dst[j] == '/') { dst[j+1] = 0; break; }
\r
80 // make temp buffer for alt renderer
\r
81 PicoDraw2FB = malloc((8+320)*(8+240+8));
\r
84 printf("PicoDraw2FB == 0\n");
\r
87 // make dirs for saves, cfgs, etc.
\r
94 PicoMessage = emu_msg_cb;
\r
95 PicoMCDopenTray = emu_msg_tray_open;
\r
96 PicoMCDcloseTray = menu_loop_tray;
\r
100 static void scaling_update(void)
\r
102 PicoOpt &= ~0x4100;
\r
103 switch (currentConfig.scaling) {
\r
104 default: break; // off
\r
106 case 2: PicoOpt |= 0x0100; break; // hw hor+vert
\r
107 case 3: PicoOpt |= 0x4000; break; // sw hor
\r
112 void emu_Deinit(void)
\r
115 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
116 emu_SaveLoadGame(0, 1);
\r
120 if (!(currentConfig.EmuOpt & 0x20)) {
\r
121 config_writelrom(PicoConfigFile);
\r
132 if (gp2x_old_gamma != 100)
\r
136 void emu_prepareDefaultConfig(void)
\r
138 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
139 defaultConfig.EmuOpt = 0x1d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds
\r
140 defaultConfig.s_PicoOpt = 0x0f | 0x20e00; // | <- use_940, cd_pcm, cd_cdda, svp drc
\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 = 200;
\r
147 defaultConfig.volume = 50;
\r
148 defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
149 defaultConfig.KeyBinds[ 4] = 1<<1;
\r
150 defaultConfig.KeyBinds[ 2] = 1<<2;
\r
151 defaultConfig.KeyBinds[ 6] = 1<<3;
\r
152 defaultConfig.KeyBinds[14] = 1<<4;
\r
153 defaultConfig.KeyBinds[13] = 1<<5;
\r
154 defaultConfig.KeyBinds[12] = 1<<6;
\r
155 defaultConfig.KeyBinds[ 8] = 1<<7;
\r
156 defaultConfig.KeyBinds[15] = 1<<26; // switch rend
\r
157 defaultConfig.KeyBinds[10] = 1<<27; // save state
\r
158 defaultConfig.KeyBinds[11] = 1<<28; // load state
\r
159 defaultConfig.KeyBinds[23] = 1<<29; // vol up
\r
160 defaultConfig.KeyBinds[22] = 1<<30; // vol down
\r
161 defaultConfig.gamma = 100;
\r
162 defaultConfig.scaling = 0;
\r
165 void emu_setDefaultConfig(void)
\r
167 memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));
\r
168 PicoOpt = currentConfig.s_PicoOpt;
\r
169 PsndRate = currentConfig.s_PsndRate;
\r
170 PicoRegionOverride = currentConfig.s_PicoRegion;
\r
171 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
\r
172 PicoCDBuffers = currentConfig.s_PicoCDBuffers;
\r
175 void osd_text(int x, int y, const char *text)
\r
177 int len = strlen(text)*8;
\r
179 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
181 x &= ~3; // align x
\r
182 len = (len+3) >> 2;
\r
183 for (h = 0; h < 8; h++) {
\r
184 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
185 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
187 emu_textOut8(x, y, text);
\r
190 x &= ~1; // align x
\r
191 len = (len+1) >> 1;
\r
192 for (h = 0; h < 8; h++) {
\r
193 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
194 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
196 emu_textOut16(x, y, text);
\r
200 static void cd_leds(void)
\r
204 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
\r
205 old_reg = Pico_mcd->s68k_regs[0];
\r
207 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
209 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
210 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
211 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
212 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
213 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
214 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
215 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
216 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
219 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
220 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
221 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
222 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
223 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
224 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
228 static int EmuScanBegin16(unsigned int num)
\r
230 if (!(Pico.video.reg[1]&8)) num += 8;
\r
231 DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;
\r
236 static int EmuScanBegin8(unsigned int num)
\r
238 if (!(Pico.video.reg[1]&8)) num += 8;
\r
239 DrawLineDest = (unsigned char *) gp2x_screen + 320 * num;
\r
244 int localPal[0x100];
\r
245 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
247 static void blit(const char *fps, const char *notice)
\r
249 int emu_opt = currentConfig.EmuOpt;
\r
253 // 8bit fast renderer
\r
254 if (Pico.m.dirtyPal) {
\r
255 Pico.m.dirtyPal = 0;
\r
256 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
257 // feed new palette to our device
\r
258 gp2x_video_setpalette(localPal, 0x40);
\r
261 if (PicoRead16Hook == PicoSVPRead16)
\r
262 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
\r
264 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
266 else if (!(emu_opt&0x80))
\r
268 // 8bit accurate renderer
\r
269 if (Pico.m.dirtyPal)
\r
271 Pico.m.dirtyPal = 0;
\r
272 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
273 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
274 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
275 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
276 memcpy32(localPal+0xc0, localPal+0x40, 0x40);
\r
277 localPal[0xc0] = 0x0000c000;
\r
278 localPal[0xd0] = 0x00c00000;
\r
279 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
280 localPal[0xf0] = 0x00ffffff;
\r
281 gp2x_video_setpalette(localPal, 0x100);
\r
282 } else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
\r
283 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
284 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
285 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
286 gp2x_video_setpalette(localPal, 0xc0);
\r
288 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
289 gp2x_video_setpalette(localPal, 0x40);
\r
294 if (notice || (emu_opt & 2)) {
\r
296 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
\r
297 if (notice) osd_text(4, h, notice);
\r
299 osd_text(osd_fps_x, h, fps);
\r
301 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
\r
304 //gp2x_video_wait_vsync();
\r
307 if (!(PicoOpt&0x10)) {
\r
308 if (!(Pico.video.reg[1]&8)) {
\r
309 if (currentConfig.EmuOpt&0x80) {
\r
310 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
312 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
315 DrawLineDest = gp2x_screen;
\r
321 // clears whole screen or just the notice area (in all buffers)
\r
322 static void clearArea(int full)
\r
324 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
326 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
327 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
329 // 16bit accurate renderer
\r
330 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
331 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
336 static void vidResetMode(void)
\r
338 if (PicoOpt&0x10) {
\r
339 gp2x_video_changemode(8);
\r
340 } else if (currentConfig.EmuOpt&0x80) {
\r
341 gp2x_video_changemode(16);
\r
342 PicoDrawSetColorFormat(1);
\r
343 PicoScanBegin = EmuScanBegin16;
\r
345 gp2x_video_changemode(8);
\r
346 PicoDrawSetColorFormat(2);
\r
347 PicoScanBegin = EmuScanBegin8;
\r
349 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
350 // setup pal for 8-bit modes
\r
351 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
352 localPal[0xd0] = 0x00c00000;
\r
353 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
354 localPal[0xf0] = 0x00ffffff;
\r
355 gp2x_video_setpalette(localPal, 0x100);
\r
356 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
359 Pico.m.dirtyPal = 1;
\r
361 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
362 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
363 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
367 static void emu_msg_cb(const char *msg)
\r
369 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
371 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
372 osd_text(4, 232, msg);
\r
373 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
\r
375 // 16bit accurate renderer
\r
376 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
377 osd_text(4, 232, msg);
\r
378 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
\r
380 gettimeofday(¬iceMsgTime, 0);
\r
381 noticeMsgTime.tv_sec -= 2;
\r
383 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
387 static void emu_state_cb(const char *str)
\r
393 static void emu_msg_tray_open(void)
\r
395 strcpy(noticeMsg, "CD tray opened");
\r
396 gettimeofday(¬iceMsgTime, 0);
\r
399 static void update_volume(int has_changed, int is_up)
\r
401 static int prev_frame = 0, wait_frames = 0;
\r
402 int vol = currentConfig.volume;
\r
406 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
\r
410 if (vol < 99) vol++;
\r
412 if (vol > 0) vol--;
\r
415 gp2x_sound_volume(vol, vol);
\r
416 currentConfig.volume = vol;
\r
418 sprintf(noticeMsg, "VOL: %02i", vol);
\r
419 gettimeofday(¬iceMsgTime, 0);
\r
420 prev_frame = Pico.m.frame_count;
\r
423 // set the right mixer func
\r
424 if (!(PicoOpt&8)) return; // just use defaults for mono
\r
426 PsndMix_32_to_16l = mix_32_to_16l_stereo;
\r
428 mix_32_to_16l_level = 5 - vol;
\r
429 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
\r
433 static void change_fast_forward(int set_on)
\r
435 static void *set_PsndOut = NULL;
\r
436 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
438 if (set_on && !is_on) {
\r
439 set_PsndOut = PsndOut;
\r
440 set_Frameskip = currentConfig.Frameskip;
\r
441 set_EmuOpt = currentConfig.EmuOpt;
\r
443 currentConfig.Frameskip = 8;
\r
444 currentConfig.EmuOpt &= ~4;
\r
447 else if (!set_on && is_on) {
\r
448 PsndOut = set_PsndOut;
\r
449 currentConfig.Frameskip = set_Frameskip;
\r
450 currentConfig.EmuOpt = set_EmuOpt;
\r
452 update_volume(0, 0);
\r
458 static void RunEvents(unsigned int which)
\r
460 if (which & 0x1800) // save or load (but not both)
\r
463 if ( emu_checkSaveFile(state_slot) &&
\r
464 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
465 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
\r
466 unsigned long keys;
\r
467 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
\r
468 while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
470 if (keys & GP2X_X) do_it = 0;
\r
471 while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release
\r
476 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
477 PicoStateProgressCB = emu_state_cb;
\r
478 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
\r
479 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
480 PicoStateProgressCB = NULL;
\r
485 if (which & 0x0400) // switch renderer
\r
487 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
488 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
489 else currentConfig.EmuOpt &= ~0x80;
\r
493 if (PicoOpt&0x10) {
\r
494 strcpy(noticeMsg, " 8bit fast renderer");
\r
495 } else if (currentConfig.EmuOpt&0x80) {
\r
496 strcpy(noticeMsg, "16bit accurate renderer");
\r
498 strcpy(noticeMsg, " 8bit accurate renderer");
\r
501 gettimeofday(¬iceMsgTime, 0);
\r
503 if (which & 0x0300)
\r
507 if(state_slot < 0) state_slot = 9;
\r
510 if(state_slot > 9) state_slot = 0;
\r
512 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
\r
513 gettimeofday(¬iceMsgTime, 0);
\r
515 if (which & 0x0080) {
\r
516 engineState = PGS_Menu;
\r
521 static void updateKeys(void)
\r
523 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
524 static unsigned long prevEvents = 0;
\r
527 keys = gp2x_joystick_read(0);
\r
528 if (keys & GP2X_SELECT) {
\r
529 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
530 // wait until select is released, so menu would not resume game
\r
531 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
534 keys &= CONFIGURABLE_KEYS;
\r
536 for (i = 0; i < 32; i++)
\r
538 if (keys & (1 << i))
\r
540 int pl, acts = currentConfig.KeyBinds[i];
\r
541 if (!acts) continue;
\r
542 pl = (acts >> 16) & 1;
\r
543 if (kb_combo_keys & (1 << i))
\r
545 int u = i+1, acts_c = acts & kb_combo_acts;
\r
546 // let's try to find the other one
\r
548 for (; u < 32; u++)
\r
549 if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {
\r
550 allActions[pl] |= acts_c & currentConfig.KeyBinds[u];
\r
551 keys &= ~((1 << i) | (1 << u));
\r
555 // add non-combo actions if combo ones were not found
\r
556 if (!acts_c || u == 32)
\r
557 allActions[pl] |= acts & ~kb_combo_acts;
\r
559 allActions[pl] |= acts;
\r
565 if (num_of_joys > 0)
\r
567 gp2x_usbjoy_update();
\r
568 for (joy = 0; joy < num_of_joys; joy++) {
\r
569 int keys = gp2x_usbjoy_check2(joy);
\r
570 for (i = 0; i < 32; i++) {
\r
571 if (keys & (1 << i)) {
\r
572 int acts = currentConfig.JoyBinds[joy][i];
\r
573 int pl = (acts >> 16) & 1;
\r
574 allActions[pl] |= acts;
\r
580 PicoPad[0] = (unsigned short) allActions[0];
\r
581 PicoPad[1] = (unsigned short) allActions[1];
\r
583 events = (allActions[0] | allActions[1]) >> 16;
\r
585 // volume is treated in special way and triggered every frame
\r
586 if (events & 0x6000)
\r
587 update_volume(1, events & 0x2000);
\r
589 if ((events ^ prevEvents) & 0x40)
\r
590 change_fast_forward(events & 0x40);
\r
592 events &= ~prevEvents;
\r
593 if (events) RunEvents(events);
\r
594 if (movie_data) emu_updateMovie();
\r
596 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
600 static void updateSound(int len)
\r
602 if (PicoOpt&8) len<<=1;
\r
604 /* avoid writing audio when lagging behind to prevent audio lag */
\r
605 if (PicoSkipFrame != 2)
\r
606 gp2x_sound_write(PsndOut, len<<1);
\r
610 static void SkipFrame(int do_audio)
\r
612 PicoSkipFrame=do_audio ? 1 : 2;
\r
618 void emu_forcedFrame(void)
\r
620 int po_old = PicoOpt;
\r
621 int eo_old = currentConfig.EmuOpt;
\r
623 PicoOpt &= ~0x0010;
\r
624 PicoOpt |= 0x4080; // soft_scale | acc_sprites
\r
625 currentConfig.EmuOpt |= 0x80;
\r
628 PicoDrawSetColorFormat(1);
\r
629 PicoScanBegin = EmuScanBegin16;
\r
630 Pico.m.dirtyPal = 1;
\r
631 PicoFrameDrawOnly();
\r
634 if (!(Pico.video.reg[12]&1)) {
\r
635 vidCpyM2 = vidCpyM2_32col;
\r
637 } else vidCpyM2 = vidCpyM2_40col;
\r
639 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
640 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
641 gp2x_video_setpalette(localPal, 0x40);
\r
644 currentConfig.EmuOpt = eo_old;
\r
647 static void simpleWait(int thissec, int lim_time)
\r
649 struct timeval tval;
\r
651 spend_cycles(1024);
\r
652 gettimeofday(&tval, 0);
\r
653 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
655 while (tval.tv_usec < lim_time)
\r
657 spend_cycles(1024);
\r
658 gettimeofday(&tval, 0);
\r
659 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
665 static void tga_dump(void)
\r
667 #define BYTE unsigned char
\r
668 #define WORD unsigned short
\r
671 BYTE IDLength; /* 00h Size of Image ID field */
\r
672 BYTE ColorMapType; /* 01h Color map type */
\r
673 BYTE ImageType; /* 02h Image type code */
\r
674 WORD CMapStart; /* 03h Color map origin */
\r
675 WORD CMapLength; /* 05h Color map length */
\r
676 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
677 WORD XOffset; /* 08h X origin of image */
\r
678 WORD YOffset; /* 0Ah Y origin of image */
\r
679 WORD Width; /* 0Ch Width of image */
\r
680 WORD Height; /* 0Eh Height of image */
\r
681 BYTE PixelDepth; /* 10h Image pixel size */
\r
682 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
683 } __attribute__((packed)) TGAHEAD;
\r
684 static unsigned short oldscr[320*240];
\r
685 FILE *f; char name[128]; int i;
\r
687 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
688 TGAHEAD.ImageType = 2;
\r
689 TGAHEAD.Width = 320;
\r
690 TGAHEAD.Height = 240;
\r
691 TGAHEAD.PixelDepth = 16;
\r
692 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
694 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
696 for (i = 0; i < 320*240; i++)
\r
697 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
700 for (i = 0; i < 320*240; i++)
\r
701 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
702 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
703 f = fopen(name, "wb");
\r
704 if (!f) { printf("!f\n"); exit(1); }
\r
705 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
706 fwrite(oldscr, 1, 320*240*2, f);
\r
713 void emu_Loop(void)
\r
715 static int gp2x_old_clock = 200;
\r
716 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;
\r
717 char fpsbuff[24]; // fps count c string
\r
718 struct timeval tval; // timing
\r
719 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
\r
720 int frames_done, frames_shown, thissec; // actual frames
\r
721 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
\r
724 printf("entered emu_Loop()\n");
\r
726 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
727 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
728 set_FCLK(currentConfig.CPUclock);
\r
729 gp2x_old_clock = currentConfig.CPUclock;
\r
733 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
\r
734 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
735 gp2x_old_gamma = currentConfig.gamma;
\r
736 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
739 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
\r
740 if (currentConfig.EmuOpt&0x2000)
\r
741 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
\r
742 else unset_LCD_custom_rate();
\r
745 EmuOpt_old = currentConfig.EmuOpt;
\r
748 // make sure we are in correct mode
\r
751 Pico.m.dirtyPal = 1;
\r
752 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
753 emu_findKeyBindCombos();
\r
755 // pal/ntsc might have changed, reset related stuff
\r
756 target_fps = Pico.m.pal ? 50 : 60;
\r
757 target_frametime = 1000000/target_fps;
\r
760 // prepare sound stuff
\r
761 if (currentConfig.EmuOpt & 4)
\r
763 int snd_excess_add;
\r
764 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
\r
765 ((PicoOpt&0x200) && crashed_940)) {
\r
766 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
768 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
769 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
770 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
771 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
772 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
773 PicoWriteSound = updateSound;
\r
774 update_volume(0, 0);
\r
775 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
776 PsndOut = sndBuffer;
\r
777 PsndRate_old = PsndRate;
\r
778 PicoOpt_old = PicoOpt;
\r
779 pal_old = Pico.m.pal;
\r
784 // prepare CD buffer
\r
785 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
\r
787 // calc vsync offset to sync timing code with vsync
\r
788 if (currentConfig.EmuOpt&0x2000) {
\r
789 gettimeofday(&tval, 0);
\r
790 gp2x_video_wait_vsync();
\r
791 gettimeofday(&tval, 0);
\r
792 vsync_offset = tval.tv_usec;
\r
793 while (vsync_offset >= target_frametime)
\r
794 vsync_offset -= target_frametime;
\r
795 if (!vsync_offset) vsync_offset++;
\r
796 printf("vsync_offset: %i\n", vsync_offset);
\r
800 frames_done = frames_shown = thissec =
\r
801 pframes_done = pframes_shown = pthissec = 0;
\r
804 while (engineState == PGS_Running)
\r
808 gettimeofday(&tval, 0);
\r
809 if (reset_timing) {
\r
811 pthissec = tval.tv_sec;
\r
812 pframes_shown = pframes_done = tval.tv_usec/target_frametime;
\r
815 // show notice message?
\r
816 if (noticeMsgTime.tv_sec)
\r
818 static int noticeMsgSum;
\r
819 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
820 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
824 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
825 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
826 notice = noticeMsg;
\r
830 // check for mode changes
\r
831 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
832 if (modes != oldmodes)
\r
835 osd_fps_x = OSD_FPS_X;
\r
837 vidCpyM2 = vidCpyM2_40col;
\r
839 if (PicoOpt & 0x100) {
\r
840 vidCpyM2 = vidCpyM2_32col_nobord;
\r
842 osd_fps_x = OSD_FPS_X - 64;
\r
844 vidCpyM2 = vidCpyM2_32col;
\r
847 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
848 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
849 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
855 if (thissec != tval.tv_sec)
\r
858 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
859 if (++bench == 10) {
\r
861 bench_fps_s = bench_fps;
\r
862 bf[bfp++ & 3] = bench_fps;
\r
865 bench_fps += frames_shown;
\r
866 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
868 if (currentConfig.EmuOpt & 2) {
\r
869 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
870 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
873 frames_shown = frames_done = 0;
\r
874 thissec = tval.tv_sec;
\r
877 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
880 if (pthissec != tval.tv_sec)
\r
882 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
883 pframes_done = pframes_shown = 0;
\r
885 // it is quite common for this implementation to leave 1 fame unfinished
\r
886 // when second changes, but we don't want buffer to starve.
\r
887 if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
\r
889 SkipFrame(1); pframes_done++;
\r
892 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
\r
893 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
\r
894 if (pframes_shown > pframes_done) pframes_shown = pframes_done;
\r
896 pthissec = tval.tv_sec;
\r
899 lim_time = (pframes_done+1) * target_frametime + vsync_offset;
\r
900 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
902 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
904 SkipFrame(1); pframes_done++; frames_done++;
\r
905 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
\r
906 gettimeofday(&tval, 0);
\r
907 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
908 if (tval.tv_usec < lim_time) { // we are too fast
\r
909 simpleWait(pthissec, lim_time);
\r
912 lim_time += target_frametime;
\r
915 else if (tval.tv_usec > lim_time) // auto frameskip
\r
917 // no time left for this frame - skip
\r
918 if (tval.tv_usec - lim_time >= 300000) {
\r
919 /* something caused a slowdown for us (disk access? cache flush?)
\r
920 * try to recover by resetting timing... */
\r
925 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
\r
933 gettimeofday(&tval, 0);
\r
934 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
936 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
\r
938 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
940 // sleep or vsync if we are still too fast
\r
941 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
942 if (!reset_timing && tval.tv_usec < lim_time)
\r
945 if (vsync_offset) {
\r
946 if (lim_time - tval.tv_usec > target_frametime/2)
\r
947 simpleWait(pthissec, lim_time - target_frametime/4);
\r
948 gp2x_video_wait_vsync();
\r
950 simpleWait(pthissec, lim_time);
\r
955 blit(fpsbuff, notice);
\r
957 pframes_done++; pframes_shown++;
\r
958 frames_done++; frames_shown++;
\r
961 change_fast_forward(0);
\r
963 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
\r
966 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
967 emu_state_cb("Writing SRAM/BRAM..");
\r
968 emu_SaveLoadGame(0, 1);
\r
972 // if in 8bit mode, generate 16bit image for menu background
\r
973 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
978 void emu_ResetGame(void)
\r