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 "cpuctrl.h"
\r
26 #include <Pico/PicoInt.h>
\r
27 #include <Pico/Patch.h>
\r
28 #include <Pico/sound/mix.h>
\r
29 #include <zlib/zlib.h>
\r
34 #define OSD_FPS_X 220
\r
36 #define OSD_FPS_X 260
\r
41 int select_exits = 0;
\r
43 char romFileName[PATH_MAX];
\r
45 extern int crashed_940;
\r
47 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
48 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
49 static int osd_fps_x;
\r
50 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
\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 find_combos(void)
\r
104 // find out which keys and actions are combos
\r
105 combo_keys = combo_acts = 0;
\r
106 for (act = 0; act < 32; act++)
\r
108 int keyc = 0, keyc2 = 0;
\r
109 if (act == 16 || act == 17) continue; // player2 flag
\r
112 for (u = 0; u < 32; u++)
\r
113 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
117 for (u = 0; u < 32; u++)
\r
118 if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1
\r
119 (currentConfig.KeyBinds[u] & (1 << act))) keyc++;
\r
120 for (u = 0; u < 32; u++)
\r
121 if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2
\r
122 (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;
\r
123 if (keyc2 > keyc) keyc = keyc2;
\r
127 // loop again and mark those keys and actions as combo
\r
128 for (u = 0; u < 32; u++)
\r
130 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
131 combo_keys |= 1 << u;
\r
132 combo_acts |= 1 << act;
\r
138 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);
\r
142 static void scaling_update(void)
\r
144 PicoOpt &= ~0x4100;
\r
145 switch (currentConfig.scaling) {
\r
146 default: break; // off
\r
148 case 2: PicoOpt |= 0x0100; break; // hw hor+vert
\r
149 case 3: PicoOpt |= 0x4000; break; // sw hor
\r
154 void emu_Deinit(void)
\r
157 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
158 emu_SaveLoadGame(0, 1);
\r
162 if (!(currentConfig.EmuOpt & 0x20)) {
\r
163 FILE *f = fopen(PicoConfigFile, "r+b");
\r
164 if (!f) emu_WriteConfig(0);
\r
166 // if we already have config, reload it, except last ROM
\r
167 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
\r
168 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
\r
169 fseek(f, 0, SEEK_SET);
\r
170 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
184 if (gp2x_old_gamma != 100)
\r
188 void emu_setDefaultConfig(void)
\r
190 memset(¤tConfig, 0, sizeof(currentConfig));
\r
191 currentConfig.lastRomFile[0] = 0;
\r
192 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
\r
193 currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda
\r
194 currentConfig.PsndRate = 22050; // 44100;
\r
195 currentConfig.PicoRegion = 0; // auto
\r
196 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
197 currentConfig.Frameskip = -1; // auto
\r
198 currentConfig.CPUclock = 200;
\r
199 currentConfig.volume = 50;
\r
200 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
201 currentConfig.KeyBinds[ 4] = 1<<1;
\r
202 currentConfig.KeyBinds[ 2] = 1<<2;
\r
203 currentConfig.KeyBinds[ 6] = 1<<3;
\r
204 currentConfig.KeyBinds[14] = 1<<4;
\r
205 currentConfig.KeyBinds[13] = 1<<5;
\r
206 currentConfig.KeyBinds[12] = 1<<6;
\r
207 currentConfig.KeyBinds[ 8] = 1<<7;
\r
208 currentConfig.KeyBinds[15] = 1<<26; // switch rend
\r
209 currentConfig.KeyBinds[10] = 1<<27; // save state
\r
210 currentConfig.KeyBinds[11] = 1<<28; // load state
\r
211 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
212 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
213 currentConfig.gamma = 100;
\r
214 currentConfig.PicoCDBuffers = 64;
\r
215 currentConfig.scaling = 0;
\r
218 void osd_text(int x, int y, const char *text)
\r
220 int len = strlen(text)*8;
\r
222 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
224 x &= ~3; // align x
\r
225 len = (len+3) >> 2;
\r
226 for (h = 0; h < 8; h++) {
\r
227 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
228 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
230 emu_textOut8(x, y, text);
\r
233 x &= ~1; // align x
\r
234 len = (len+1) >> 1;
\r
235 for (h = 0; h < 8; h++) {
\r
236 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
237 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
239 emu_textOut16(x, y, text);
\r
243 static void cd_leds(void)
\r
247 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
\r
248 old_reg = Pico_mcd->s68k_regs[0];
\r
250 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
252 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
253 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
254 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
255 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
256 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
257 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
258 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
259 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
262 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
263 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
264 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
265 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
266 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
267 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
271 static int EmuScan16(unsigned int num, void *sdata)
\r
273 if (!(Pico.video.reg[1]&8)) num += 8;
\r
274 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);
\r
279 static int EmuScan8(unsigned int num, void *sdata)
\r
281 if (!(Pico.video.reg[1]&8)) num += 8;
\r
282 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);
\r
287 int localPal[0x100];
\r
288 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
290 static void blit(const char *fps, const char *notice)
\r
292 int emu_opt = currentConfig.EmuOpt;
\r
294 if (PicoOpt&0x10) {
\r
295 // 8bit fast renderer
\r
296 if (Pico.m.dirtyPal) {
\r
297 Pico.m.dirtyPal = 0;
\r
298 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
299 // feed new palette to our device
\r
300 gp2x_video_setpalette(localPal, 0x40);
\r
302 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
303 } else if (!(emu_opt&0x80)) {
\r
304 // 8bit accurate renderer
\r
305 if (Pico.m.dirtyPal) {
\r
306 Pico.m.dirtyPal = 0;
\r
307 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
308 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
309 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
310 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
311 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
\r
312 localPal[0xc0] = 0x0000c000;
\r
313 localPal[0xd0] = 0x00c00000;
\r
314 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
315 localPal[0xf0] = 0x00ffffff;
\r
316 gp2x_video_setpalette(localPal, 0x100);
\r
317 } else if (rendstatus & 0x20) { // mid-frame palette changes
\r
318 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
319 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
320 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
321 gp2x_video_setpalette(localPal, 0xc0);
\r
323 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
324 gp2x_video_setpalette(localPal, 0x40);
\r
329 if (notice || (emu_opt & 2)) {
\r
331 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
\r
332 if (notice) osd_text(4, h, notice);
\r
334 osd_text(osd_fps_x, h, fps);
\r
336 if ((emu_opt & 0x400) && (PicoMCD & 1))
\r
339 //gp2x_video_wait_vsync();
\r
342 if (!(PicoOpt&0x10)) {
\r
343 if (!(Pico.video.reg[1]&8)) {
\r
344 if (currentConfig.EmuOpt&0x80) {
\r
345 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
347 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
350 DrawLineDest = gp2x_screen;
\r
356 // clears whole screen or just the notice area (in all buffers)
\r
357 static void clearArea(int full)
\r
359 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
361 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
362 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
364 // 16bit accurate renderer
\r
365 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
366 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
371 static void vidResetMode(void)
\r
373 if (PicoOpt&0x10) {
\r
374 gp2x_video_changemode(8);
\r
375 } else if (currentConfig.EmuOpt&0x80) {
\r
376 gp2x_video_changemode(16);
\r
377 PicoDrawSetColorFormat(1);
\r
378 PicoScan = EmuScan16;
\r
381 gp2x_video_changemode(8);
\r
382 PicoDrawSetColorFormat(2);
\r
383 PicoScan = EmuScan8;
\r
386 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
387 // setup pal for 8-bit modes
\r
388 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
389 localPal[0xd0] = 0x00c00000;
\r
390 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
391 localPal[0xf0] = 0x00ffffff;
\r
392 gp2x_video_setpalette(localPal, 0x100);
\r
393 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
396 Pico.m.dirtyPal = 1;
\r
398 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
399 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
400 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
404 static void emu_msg_cb(const char *msg)
\r
406 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
408 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
409 osd_text(4, 232, msg);
\r
410 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
\r
412 // 16bit accurate renderer
\r
413 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
414 osd_text(4, 232, msg);
\r
415 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
\r
417 gettimeofday(¬iceMsgTime, 0);
\r
418 noticeMsgTime.tv_sec -= 2;
\r
420 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
424 static void emu_state_cb(const char *str)
\r
430 static void emu_msg_tray_open(void)
\r
432 strcpy(noticeMsg, "CD tray opened");
\r
433 gettimeofday(¬iceMsgTime, 0);
\r
436 static void update_volume(int has_changed, int is_up)
\r
438 static int prev_frame = 0, wait_frames = 0;
\r
439 int vol = currentConfig.volume;
\r
443 if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
\r
447 if (vol < 99) vol++;
\r
449 if (vol > 0) vol--;
\r
452 gp2x_sound_volume(vol, vol);
\r
453 currentConfig.volume = vol;
\r
455 sprintf(noticeMsg, "VOL: %02i", vol);
\r
456 gettimeofday(¬iceMsgTime, 0);
\r
457 prev_frame = Pico.m.frame_count;
\r
460 // set the right mixer func
\r
461 if (!(PicoOpt&8)) return; // just use defaults for mono
\r
463 PsndMix_32_to_16l = mix_32_to_16l_stereo;
\r
465 mix_32_to_16l_level = 5 - vol;
\r
466 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
\r
470 static void change_fast_forward(int set_on)
\r
472 static void *set_PsndOut = NULL;
\r
473 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
475 if (set_on && !is_on) {
\r
476 set_PsndOut = PsndOut;
\r
477 set_Frameskip = currentConfig.Frameskip;
\r
478 set_EmuOpt = currentConfig.EmuOpt;
\r
480 currentConfig.Frameskip = 8;
\r
481 currentConfig.EmuOpt &= ~4;
\r
484 else if (!set_on && is_on) {
\r
485 PsndOut = set_PsndOut;
\r
486 currentConfig.Frameskip = set_Frameskip;
\r
487 currentConfig.EmuOpt = set_EmuOpt;
\r
489 update_volume(0, 0);
\r
495 static void RunEvents(unsigned int which)
\r
497 if (which & 0x1800) // save or load (but not both)
\r
500 if ( emu_checkSaveFile(state_slot) &&
\r
501 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
502 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
\r
503 unsigned long keys;
\r
504 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
\r
505 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
507 if (keys & GP2X_X) do_it = 0;
\r
511 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
512 PicoStateProgressCB = emu_state_cb;
\r
513 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
\r
514 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
515 PicoStateProgressCB = NULL;
\r
520 if (which & 0x0400) // switch renderer
\r
522 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
523 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
524 else currentConfig.EmuOpt &= ~0x80;
\r
528 if (PicoOpt&0x10) {
\r
529 strcpy(noticeMsg, " 8bit fast renderer");
\r
530 } else if (currentConfig.EmuOpt&0x80) {
\r
531 strcpy(noticeMsg, "16bit accurate renderer");
\r
533 strcpy(noticeMsg, " 8bit accurate renderer");
\r
536 gettimeofday(¬iceMsgTime, 0);
\r
538 if (which & 0x0300)
\r
542 if(state_slot < 0) state_slot = 9;
\r
545 if(state_slot > 9) state_slot = 0;
\r
547 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
\r
548 gettimeofday(¬iceMsgTime, 0);
\r
550 if (which & 0x0080) {
\r
551 engineState = PGS_Menu;
\r
556 static void updateKeys(void)
\r
558 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
559 static unsigned long prevEvents = 0;
\r
562 keys = gp2x_joystick_read(0);
\r
563 if (keys & GP2X_SELECT) {
\r
564 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
565 // wait until select is released, so menu would not resume game
\r
566 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
569 keys &= CONFIGURABLE_KEYS;
\r
571 for (i = 0; i < 32; i++)
\r
573 if (keys & (1 << i)) {
\r
574 int pl, acts = currentConfig.KeyBinds[i];
\r
575 if (!acts) continue;
\r
576 pl = (acts >> 16) & 1;
\r
577 if (combo_keys & (1 << i)) {
\r
578 int u = i+1, acts_c = acts & combo_acts;
\r
579 // let's try to find the other one
\r
581 for (; u < 32; u++)
\r
582 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
\r
583 allActions[pl] |= acts_c;
\r
584 keys &= ~((1 << i) | (1 << u));
\r
587 // add non-combo actions if combo ones were not found
\r
588 if (!acts_c || u == 32)
\r
589 allActions[pl] |= acts & ~combo_acts;
\r
591 allActions[pl] |= acts;
\r
597 if (num_of_joys > 0)
\r
599 gp2x_usbjoy_update();
\r
600 for (joy = 0; joy < num_of_joys; joy++) {
\r
601 int keys = gp2x_usbjoy_check2(joy);
\r
602 for (i = 0; i < 32; i++) {
\r
603 if (keys & (1 << i)) {
\r
604 int acts = currentConfig.JoyBinds[joy][i];
\r
605 int pl = (acts >> 16) & 1;
\r
606 allActions[pl] |= acts;
\r
612 PicoPad[0] = (unsigned short) allActions[0];
\r
613 PicoPad[1] = (unsigned short) allActions[1];
\r
615 events = (allActions[0] | allActions[1]) >> 16;
\r
617 // volume is treated in special way and triggered every frame
\r
618 if (events & 0x6000)
\r
619 update_volume(1, events & 0x2000);
\r
621 if ((events ^ prevEvents) & 0x40)
\r
622 change_fast_forward(events & 0x40);
\r
624 events &= ~prevEvents;
\r
625 if (events) RunEvents(events);
\r
626 if (movie_data) emu_updateMovie();
\r
628 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
632 static void updateSound(int len)
\r
634 if (PicoOpt&8) len<<=1;
\r
636 /* avoid writing audio when lagging behind to prevent audio lag */
\r
637 if (PicoSkipFrame != 2)
\r
638 gp2x_sound_write(PsndOut, len<<1);
\r
642 static void SkipFrame(int do_audio)
\r
644 PicoSkipFrame=do_audio ? 1 : 2;
\r
650 void emu_forcedFrame(void)
\r
652 int po_old = PicoOpt;
\r
653 int eo_old = currentConfig.EmuOpt;
\r
655 PicoOpt &= ~0x0010;
\r
656 PicoOpt |= 0x4080; // soft_scale | acc_sprites
\r
657 currentConfig.EmuOpt |= 0x80;
\r
660 PicoDrawSetColorFormat(1);
\r
661 PicoScan = EmuScan16;
\r
663 Pico.m.dirtyPal = 1;
\r
664 PicoFrameDrawOnly();
\r
667 if (!(Pico.video.reg[12]&1)) {
\r
668 vidCpyM2 = vidCpyM2_32col;
\r
670 } else vidCpyM2 = vidCpyM2_40col;
\r
672 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
673 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
674 gp2x_video_setpalette(localPal, 0x40);
\r
677 currentConfig.EmuOpt = eo_old;
\r
680 static void simpleWait(int thissec, int lim_time)
\r
682 struct timeval tval;
\r
684 spend_cycles(1024);
\r
685 gettimeofday(&tval, 0);
\r
686 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
688 while(tval.tv_usec < lim_time)
\r
690 spend_cycles(1024);
\r
691 gettimeofday(&tval, 0);
\r
692 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
697 void emu_Loop(void)
\r
699 static int gp2x_old_clock = 200;
\r
700 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;
\r
701 char fpsbuff[24]; // fps count c string
\r
702 struct timeval tval; // timing
\r
703 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;
\r
704 int target_fps, target_frametime, lim_time, vsync_offset, i;
\r
707 printf("entered emu_Loop()\n");
\r
709 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
710 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
711 set_FCLK(currentConfig.CPUclock);
\r
712 gp2x_old_clock = currentConfig.CPUclock;
\r
716 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
\r
717 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
718 gp2x_old_gamma = currentConfig.gamma;
\r
719 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
722 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
\r
723 if (currentConfig.EmuOpt&0x2000)
\r
724 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
\r
725 else unset_LCD_custom_rate();
\r
728 EmuOpt_old = currentConfig.EmuOpt;
\r
731 // make sure we are in correct mode
\r
734 Pico.m.dirtyPal = 1;
\r
735 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
738 // pal/ntsc might have changed, reset related stuff
\r
739 target_fps = Pico.m.pal ? 50 : 60;
\r
740 target_frametime = 1000000/target_fps;
\r
743 // prepare sound stuff
\r
744 if (currentConfig.EmuOpt & 4)
\r
746 int snd_excess_add;
\r
747 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
\r
748 ((PicoOpt&0x200) && crashed_940)) {
\r
749 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
751 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
752 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
753 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
754 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
755 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
756 PicoWriteSound = updateSound;
\r
757 update_volume(0, 0);
\r
758 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
759 PsndOut = sndBuffer;
\r
760 PsndRate_old = PsndRate;
\r
761 PicoOpt_old = PicoOpt;
\r
762 pal_old = Pico.m.pal;
\r
767 // prepare CD buffer
\r
768 if (PicoMCD & 1) PicoCDBufferInit();
\r
770 // calc vsync offset to sync timing code with vsync
\r
771 if (currentConfig.EmuOpt&0x2000) {
\r
772 gettimeofday(&tval, 0);
\r
773 gp2x_video_wait_vsync();
\r
774 gettimeofday(&tval, 0);
\r
775 vsync_offset = tval.tv_usec;
\r
776 while (vsync_offset >= target_frametime)
\r
777 vsync_offset -= target_frametime;
\r
778 if (!vsync_offset) vsync_offset++;
\r
779 printf("vsync_offset: %i\n", vsync_offset);
\r
784 while (engineState == PGS_Running)
\r
788 gettimeofday(&tval, 0);
\r
789 if (reset_timing) {
\r
791 thissec = tval.tv_sec;
\r
792 frames_shown = frames_done = tval.tv_usec/target_frametime;
\r
795 // show notice message?
\r
796 if (noticeMsgTime.tv_sec)
\r
798 static int noticeMsgSum;
\r
799 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
800 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
804 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
805 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
806 notice = noticeMsg;
\r
810 // check for mode changes
\r
811 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
812 if (modes != oldmodes)
\r
815 osd_fps_x = OSD_FPS_X;
\r
817 vidCpyM2 = vidCpyM2_40col;
\r
819 if (PicoOpt & 0x100) {
\r
820 vidCpyM2 = vidCpyM2_32col_nobord;
\r
822 osd_fps_x = OSD_FPS_X - 64;
\r
824 vidCpyM2 = vidCpyM2_32col;
\r
827 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
828 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
829 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
835 if (thissec != tval.tv_sec)
\r
838 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
839 if (++bench == 10) {
\r
841 bench_fps_s = bench_fps;
\r
842 bf[bfp++ & 3] = bench_fps;
\r
845 bench_fps += frames_shown;
\r
846 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
848 if (currentConfig.EmuOpt & 2)
\r
849 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
850 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
852 thissec = tval.tv_sec;
\r
854 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
855 frames_done = frames_shown = 0;
\r
857 // it is quite common for this implementation to leave 1 fame unfinished
\r
858 // when second changes, but we don't want buffer to starve.
\r
859 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
861 SkipFrame(1); frames_done++;
\r
864 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
865 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
866 if (frames_shown > frames_done) frames_shown = frames_done;
\r
870 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
873 lim_time = (frames_done+1) * target_frametime + vsync_offset;
\r
874 if(currentConfig.Frameskip >= 0) { // frameskip enabled
\r
875 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
877 SkipFrame(1); frames_done++;
\r
878 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
\r
879 gettimeofday(&tval, 0);
\r
880 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
881 if(tval.tv_usec < lim_time) { // we are too fast
\r
882 simpleWait(thissec, lim_time);
\r
885 lim_time += target_frametime;
\r
887 } else if(tval.tv_usec > lim_time) { // auto frameskip
\r
888 // no time left for this frame - skip
\r
889 if (tval.tv_usec - lim_time >= 300000) {
\r
890 /* something caused a slowdown for us (disk access? cache flush?)
\r
891 * try to recover by resetting timing... */
\r
896 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;
\r
904 if (Pico.m.frame_count == 31563) {
\r
906 f = fopen("ram_p.bin", "wb");
\r
907 if (!f) { printf("!f\n"); exit(1); }
\r
908 fwrite(Pico.ram, 1, 0x10000, f);
\r
916 #define BYTE unsigned char
\r
917 #define WORD unsigned short
\r
920 BYTE IDLength; /* 00h Size of Image ID field */
\r
921 BYTE ColorMapType; /* 01h Color map type */
\r
922 BYTE ImageType; /* 02h Image type code */
\r
923 WORD CMapStart; /* 03h Color map origin */
\r
924 WORD CMapLength; /* 05h Color map length */
\r
925 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
926 WORD XOffset; /* 08h X origin of image */
\r
927 WORD YOffset; /* 0Ah Y origin of image */
\r
928 WORD Width; /* 0Ch Width of image */
\r
929 WORD Height; /* 0Eh Height of image */
\r
930 BYTE PixelDepth; /* 10h Image pixel size */
\r
931 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
932 } __attribute__((packed)) TGAHEAD;
\r
933 static unsigned short oldscr[320*240];
\r
934 FILE *f; char name[128]; int i;
\r
936 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
937 TGAHEAD.ImageType = 2;
\r
938 TGAHEAD.Width = 320;
\r
939 TGAHEAD.Height = 240;
\r
940 TGAHEAD.PixelDepth = 16;
\r
941 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
943 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
945 for (i = 0; i < 320*240; i++)
\r
946 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
949 for (i = 0; i < 320*240; i++)
\r
950 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
951 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
952 f = fopen(name, "wb");
\r
953 if (!f) { printf("!f\n"); exit(1); }
\r
954 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
955 fwrite(oldscr, 1, 320*240*2, f);
\r
962 gettimeofday(&tval, 0);
\r
963 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
965 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
\r
967 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
969 // sleep or vsync if we are still too fast
\r
970 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
971 if (!reset_timing && tval.tv_usec < lim_time)
\r
974 if (vsync_offset) {
\r
975 if (lim_time - tval.tv_usec > target_frametime/2)
\r
976 simpleWait(thissec, lim_time - target_frametime/4);
\r
977 gp2x_video_wait_vsync();
\r
979 simpleWait(thissec, lim_time);
\r
984 blit(fpsbuff, notice);
\r
986 frames_done++; frames_shown++;
\r
989 change_fast_forward(0);
\r
991 if (PicoMCD & 1) PicoCDBufferFree();
\r
994 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
995 emu_state_cb("Writing SRAM/BRAM..");
\r
996 emu_SaveLoadGame(0, 1);
\r
1000 // if in 8bit mode, generate 16bit image for menu background
\r
1001 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
1002 emu_forcedFrame();
\r
1006 void emu_ResetGame(void)
\r