4 #include <sys/stat.h> // mkdir
7 #include "kgsdk/Framework.h"
8 #include "kgsdk/Framework2D.h"
9 #include "kgsdk/FrameworkAudio.h"
10 #include "../common/emu.h"
11 #include "../common/lprintf.h"
12 #include "../common/arm_utils.h"
16 #include "asm_utils.h"
18 #include <Pico/PicoInt.h>
26 // main 300K gfx-related buffer. Used by menu and renderers.
27 unsigned char gfx_buffer[321*240*2*2];
28 char romFileName[MAX_PATH];
31 unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
34 static DWORD noticeMsgTime = 0;
38 static void blit(const char *fps, const char *notice);
39 static void clearArea(int full);
41 void emu_noticeMsgUpdated(void)
43 noticeMsgTime = GetTickCount();
46 void emu_getMainDir(char *dst, int len)
48 if (len > 0) *dst = 0;
51 static void emu_msg_cb(const char *msg)
53 if (giz_screen == NULL)
54 giz_screen = Framework2D_LockBuffer();
56 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
57 emu_textOut16(4, 232, msg);
58 noticeMsgTime = GetTickCount() - 2000;
60 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
64 static void emu_state_cb(const char *str)
70 static void emu_msg_tray_open(void)
72 strcpy(noticeMsg, "CD tray opened");
73 noticeMsgTime = GetTickCount();
79 // make dirs for saves, cfgs, etc.
86 PicoMessage = emu_msg_cb;
87 PicoMCDopenTray = emu_msg_tray_open;
88 PicoMCDcloseTray = menu_loop_tray;
94 if((currentConfig.EmuOpt & 1) && SRam.changed) {
95 emu_SaveLoadGame(0, 1);
99 if (!(currentConfig.EmuOpt & 0x20)) {
100 FILE *f = fopen(PicoConfigFile, "r+b");
101 if (!f) emu_WriteConfig(0);
103 // if we already have config, reload it, except last ROM
104 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
105 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
106 fseek(f, 0, SEEK_SET);
107 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
116 void emu_setDefaultConfig(void)
118 memset(¤tConfig, 0, sizeof(currentConfig));
119 currentConfig.lastRomFile[0] = 0;
120 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
121 currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda
122 currentConfig.PsndRate = 22050;
123 currentConfig.PicoRegion = 0; // auto
124 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
125 currentConfig.Frameskip = 0;//-1; // auto
126 currentConfig.volume = 50;
127 currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
128 currentConfig.KeyBinds[ 3] = 1<<1;
129 currentConfig.KeyBinds[ 0] = 1<<2;
130 currentConfig.KeyBinds[ 1] = 1<<3;
131 currentConfig.KeyBinds[ 5] = 1<<4;
132 currentConfig.KeyBinds[ 6] = 1<<5;
133 currentConfig.KeyBinds[ 7] = 1<<6;
134 currentConfig.KeyBinds[ 4] = 1<<7;
135 currentConfig.KeyBinds[ 8] = 1<<27; // save state
136 currentConfig.KeyBinds[ 9] = 1<<28; // load state
137 currentConfig.KeyBinds[12] = 1<<29; // vol up
138 currentConfig.KeyBinds[11] = 1<<30; // vol down
139 currentConfig.PicoCDBuffers = 64;
140 currentConfig.scaling = 0;
144 static int EmuScan16(unsigned int num, void *sdata)
146 if (!(Pico.video.reg[1]&8)) num += 8;
147 DrawLineDest = (unsigned short *) giz_screen + 321*(num+1);
152 static int EmuScan8(unsigned int num, void *sdata)
154 // draw like the fast renderer
155 if (!(Pico.video.reg[1]&8)) num += 8;
156 HighCol = gfx_buffer + 328*(num+1);
161 static void osd_text(int x, int y, const char *text)
163 int len = strlen(text) * 8;
166 for (h = 0; h < 8; h++) {
167 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
168 p = (int *) ((int)p & ~3); // align
169 for (i = len; i; i--, p++) *p = 0;
171 emu_textOut16(x, y, text);
175 short localPal[0x100];
176 static void (*vidCpy8to16)(void *dest, void *src, short *pal, int lines) = NULL;
178 static void blit(const char *fps, const char *notice)
180 int emu_opt = currentConfig.EmuOpt;
183 // 8bit fast renderer
184 if (Pico.m.dirtyPal) {
186 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
188 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
189 } else if (!(emu_opt&0x80)) {
190 // 8bit accurate renderer
191 if (Pico.m.dirtyPal) {
193 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
194 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
195 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
196 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO
197 blockcpy(localPal+0xc0, localPal+0x40, 0x40*2);
198 localPal[0xc0] = 0x0600;
199 localPal[0xd0] = 0xc000;
200 localPal[0xe0] = 0x0000; // reserved pixels for OSD
201 localPal[0xf0] = 0xffff;
204 else if (rendstatus & 0x20) { // mid-frame palette changes
205 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
206 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
210 //lprintf("vidCpy8to16 %p %p\n", (unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8);
211 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
212 //lprintf("after vidCpy8to16\n");
215 if (notice || (emu_opt & 2)) {
217 if (notice) osd_text(4, h, notice);
218 if (emu_opt & 2) osd_text(osd_fps_x, h, fps);
220 // if ((emu_opt & 0x400) && (PicoMCD & 1))
223 //gp2x_video_wait_vsync();
225 if (!(PicoOpt&0x10)) {
226 if (Pico.video.reg[1] & 8) {
227 if (currentConfig.EmuOpt&0x80)
228 DrawLineDest = (unsigned short *) giz_screen;
230 HighCol = gfx_buffer;
232 if (currentConfig.EmuOpt&0x80)
233 DrawLineDest = (unsigned short *) giz_screen + 320*8;
235 HighCol = gfx_buffer + 328*8;
240 // clears whole screen or just the notice area (in all buffers)
241 static void clearArea(int full)
243 if (giz_screen == NULL)
244 giz_screen = Framework2D_LockBuffer();
245 if (full) memset32(giz_screen, 0, 320*240*2/4);
246 else memset32((int *)((char *)giz_screen + 320*232*2), 0, 320*8*2/4);
249 static void vidResetMode(void)
253 } else if (currentConfig.EmuOpt&0x80) {
254 PicoDrawSetColorFormat(1);
255 PicoScan = EmuScan16;
257 PicoDrawSetColorFormat(0);
260 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
261 // setup pal for 8-bit modes
262 localPal[0xc0] = 0x0600;
263 localPal[0xd0] = 0xc000;
264 localPal[0xe0] = 0x0000; // reserved pixels for OSD
265 localPal[0xf0] = 0xffff;
268 screen = Framework2D_LockBuffer();
269 memset32(screen, 0, 320*240*2/4);
270 Framework2D_UnlockBuffer();
275 static void SkipFrame(int do_audio)
277 PicoSkipFrame=do_audio ? 1 : 2;
282 void emu_forcedFrame(void)
287 static void updateKeys(void)
289 unsigned int keys, allActions[2] = { 0, 0 }, events;
290 static unsigned int prevEvents = 0;
293 keys = Framework_PollGetButtons();
294 if (keys & BTN_HOME) {
295 engineState = PGS_Menu;
296 // wait until select is released, so menu would not resume game
297 while (Framework_PollGetButtons() & BTN_HOME) Sleep(50);
300 keys &= CONFIGURABLE_KEYS;
302 for (i = 0; i < 32; i++)
304 if (keys & (1 << i)) {
305 int pl, acts = currentConfig.KeyBinds[i];
307 pl = (acts >> 16) & 1;
308 /* TODO if (combo_keys & (1 << i)) {
309 int u = i+1, acts_c = acts & combo_acts;
310 // let's try to find the other one
313 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
314 allActions[pl] |= acts_c;
315 keys &= ~((1 << i) | (1 << u));
318 // add non-combo actions if combo ones were not found
319 if (!acts_c || u == 32)
320 allActions[pl] |= acts & ~combo_acts;
322 allActions[pl] |= acts;
327 PicoPad[0] = (unsigned short) allActions[0];
328 PicoPad[1] = (unsigned short) allActions[1];
330 events = (allActions[0] | allActions[1]) >> 16;
332 // volume is treated in special way and triggered every frame
333 if (events & 0x6000) {
334 int vol = currentConfig.volume;
335 if (events & 0x2000) {
336 if (vol < 100) vol++;
340 //gp2x_sound_volume(vol, vol);
341 sprintf(noticeMsg, "VOL: %02i", vol);
342 noticeMsgTime = GetTickCount();
343 currentConfig.volume = vol;
346 events &= ~prevEvents;
347 //if (events) RunEvents(events); // TODO
348 if (movie_data) emu_updateMovie();
350 prevEvents = (allActions[0] | allActions[1]) >> 16;
353 static void simpleWait(DWORD until)
359 //static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
360 char fpsbuff[24]; // fps count c string
361 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
362 int frames_done = 0, frames_shown = 0, oldmodes = 0;
363 int target_fps, target_frametime, lim_time, tval_diff, i;
366 lprintf("entered emu_Loop()\n");
370 // make sure we are in correct mode
372 if (currentConfig.scaling) PicoOpt|=0x4000;
373 else PicoOpt&=~0x4000;
375 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
376 //find_combos(); // TODO
378 // pal/ntsc might have changed, reset related stuff
379 target_fps = Pico.m.pal ? 50 : 60;
380 target_frametime = (1000<<8)/target_fps;
383 // prepare sound stuff
384 /* if (currentConfig.EmuOpt & 4) {
386 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
387 sound_rerate(Pico.m.frame_count ? 1 : 0);
389 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
390 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
391 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
392 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
393 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
394 PicoWriteSound = updateSound;
395 memset(sndBuffer, 0, sizeof(sndBuffer));
397 PsndRate_old = PsndRate;
398 PsndLen_real = PsndLen;
399 PicoOpt_old = PicoOpt;
400 pal_old = Pico.m.pal;
406 if (PicoMCD & 1) PicoCDBufferInit();
409 while (engineState == PGS_Running)
413 tval = GetTickCount();
414 if (reset_timing || tval < tval_prev) {
417 frames_shown = frames_done = 0;
420 // show notice message?
422 static int noticeMsgSum;
423 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
428 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
429 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
434 // check for mode changes
435 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
436 if (modes != oldmodes) {
437 osd_fps_x = OSD_FPS_X;
439 vidCpy8to16 = vidCpy8to16_40;
441 // vidCpy8to16 = vidCpy8to16_32col;
447 if (tval - tval_thissec >= 1000)
450 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
453 bench_fps_s = bench_fps;
454 bf[bfp++ & 3] = bench_fps;
457 bench_fps += frames_shown;
458 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
460 if(currentConfig.EmuOpt & 2)
461 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
465 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
466 frames_done = frames_shown = 0;
468 // it is quite common for this implementation to leave 1 fame unfinished
469 // when second changes, but we don't want buffer to starve.
470 if (PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
472 SkipFrame(1); frames_done++;
475 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
476 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
477 if (frames_shown > frames_done) frames_shown = frames_done;
481 sprintf(fpsbuff, "%i", Pico.m.frame_count);
485 lim_time = (frames_done+1) * target_frametime;
486 if (currentConfig.Frameskip >= 0) // frameskip enabled
488 for (i = 0; i < currentConfig.Frameskip; i++) {
490 SkipFrame(1); frames_done++;
491 if (PsndOut) { // do framelimitting if sound is enabled
493 tval = GetTickCount();
494 tval_diff = (int)(tval - tval_thissec) << 8;
495 if (tval_diff < lim_time) // we are too fast
496 simpleWait(tval + ((lim_time - tval_diff)>>8));
498 lim_time += target_frametime;
501 else // auto frameskip
504 tval = GetTickCount();
505 tval_diff = (int)(tval - tval_thissec) << 8;
506 if (tval_diff > lim_time)
508 // no time left for this frame - skip
509 if (tval_diff - lim_time >= (300<<8)) {
510 /* something caused a slowdown for us (disk access? cache flush?)
511 * try to recover by resetting timing... */
516 SkipFrame(tval_diff < lim_time+target_frametime*2); frames_done++;
523 if (giz_screen == NULL)
524 giz_screen = Framework2D_LockBuffer();
527 blit(fpsbuff, notice);
529 if (giz_screen != NULL) {
530 Framework2D_UnlockBuffer();
533 //lprintf("after unlock\n");
536 tval = GetTickCount();
537 tval_diff = (int)(tval - tval_thissec) << 8;
539 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
541 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
543 // sleep if we are still too fast
544 if (tval_diff < lim_time)
547 simpleWait(tval + ((lim_time - tval_diff) >> 8));
551 frames_done++; frames_shown++;
555 if (PicoMCD & 1) PicoCDBufferFree();
558 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
559 emu_state_cb("Writing SRAM/BRAM..");
560 emu_SaveLoadGame(0, 1);
566 void emu_ResetGame(void)