4 #include "kgsdk/Framework2D.h"
5 #include "kgsdk/FrameworkAudio.h"
6 #include "../common/emu.h"
7 #include "../common/lprintf.h"
8 #include "../common/arm_utils.h"
12 #include "asm_utils.h"
14 #include <Pico/PicoInt.h>
22 // main 300K gfx-related buffer. Used by menu and renderers.
23 unsigned char gfx_buffer[321*240*2*2];
24 char romFileName[MAX_PATH];
27 unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
30 static DWORD noticeMsgTime = 0;
34 static void blit(const char *fps, const char *notice);
35 static void clearArea(int full);
37 void emu_noticeMsgUpdated(void)
39 noticeMsgTime = GetTickCount();
42 void emu_getMainDir(char *dst, int len)
44 if (len > 0) *dst = 0;
47 static void emu_msg_cb(const char *msg)
49 if (giz_screen == NULL)
50 giz_screen = Framework2D_LockBuffer();
52 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
53 emu_textOut16(4, 232, msg);
54 noticeMsgTime = GetTickCount() - 2000;
56 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
60 static void emu_state_cb(const char *str)
66 static void emu_msg_tray_open(void)
68 strcpy(noticeMsg, "CD tray opened");
69 noticeMsgTime = GetTickCount();
75 // make dirs for saves, cfgs, etc.
76 CreateDirectory(L"mds", NULL);
77 CreateDirectory(L"srm", NULL);
78 CreateDirectory(L"brm", NULL);
79 CreateDirectory(L"cfg", NULL);
82 PicoMessage = emu_msg_cb;
83 PicoMCDopenTray = emu_msg_tray_open;
84 PicoMCDcloseTray = menu_loop_tray;
90 if((currentConfig.EmuOpt & 1) && SRam.changed) {
91 emu_SaveLoadGame(0, 1);
95 if (!(currentConfig.EmuOpt & 0x20)) {
96 FILE *f = fopen(PicoConfigFile, "r+b");
97 if (!f) emu_WriteConfig(0);
99 // if we already have config, reload it, except last ROM
100 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
101 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
102 fseek(f, 0, SEEK_SET);
103 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
112 void emu_setDefaultConfig(void)
114 memset(¤tConfig, 0, sizeof(currentConfig));
115 currentConfig.lastRomFile[0] = 0;
116 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
117 currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda
118 currentConfig.PsndRate = 22050;
119 currentConfig.PicoRegion = 0; // auto
120 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
121 currentConfig.Frameskip = 0;//-1; // auto
122 currentConfig.volume = 50;
123 currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
124 currentConfig.KeyBinds[ 3] = 1<<1;
125 currentConfig.KeyBinds[ 0] = 1<<2;
126 currentConfig.KeyBinds[ 1] = 1<<3;
127 currentConfig.KeyBinds[ 5] = 1<<4;
128 currentConfig.KeyBinds[ 6] = 1<<5;
129 currentConfig.KeyBinds[ 7] = 1<<6;
130 currentConfig.KeyBinds[ 8] = 1<<7;
131 currentConfig.KeyBinds[ 4] = 1<<26; // switch rend
132 currentConfig.KeyBinds[ 8] = 1<<27; // save state
133 currentConfig.KeyBinds[ 9] = 1<<28; // load state
134 currentConfig.KeyBinds[12] = 1<<29; // vol up
135 currentConfig.KeyBinds[11] = 1<<30; // vol down
136 currentConfig.PicoCDBuffers = 64;
137 currentConfig.scaling = 0;
141 static int EmuScan16(unsigned int num, void *sdata)
143 if (!(Pico.video.reg[1]&8)) num += 8;
144 DrawLineDest = (unsigned short *) giz_screen + 321*(num+1);
149 static int EmuScan8(unsigned int num, void *sdata)
151 // draw like the fast renderer
152 if (!(Pico.video.reg[1]&8)) num += 8;
153 HighCol = gfx_buffer + 328*8 + 328*(num+1);
158 static void osd_text(int x, int y, const char *text)
160 int len = strlen(text) * 8;
163 for (h = 0; h < 8; h++) {
164 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
165 p = (int *) ((int)p & ~3); // align
166 for (i = len; i; i--, p++) *p = 0;
168 emu_textOut16(x, y, text);
172 short localPal[0x100];
173 static void (*vidCpy8to16)(void *dest, void *src, short *pal, int lines) = NULL;
176 static void vidCpy8to16_(void *dest, void *src, short *pal, int lines)
178 vidCpy8to16(dest, src, pal, lines);
181 static void blit(const char *fps, const char *notice)
183 int emu_opt = currentConfig.EmuOpt;
186 // 8bit fast renderer
187 if (Pico.m.dirtyPal) {
189 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
191 vidCpy8to16_((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
192 } else if (!(emu_opt&0x80)) {
193 // 8bit accurate renderer
194 if (Pico.m.dirtyPal) {
196 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
197 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
198 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
199 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO
200 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
201 localPal[0xc0] = 0x0600;
202 localPal[0xd0] = 0xc000;
203 localPal[0xe0] = 0x0000; // reserved pixels for OSD
204 localPal[0xf0] = 0xffff;
207 else if (rendstatus & 0x20) { // mid-frame palette changes
208 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
209 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
213 //lprintf("vidCpy8to16 %p %p\n", (unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8);
214 vidCpy8to16_((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
215 //lprintf("after vidCpy8to16\n");
218 if (notice || (emu_opt & 2)) {
220 if (notice) osd_text(4, h, notice);
221 if (emu_opt & 2) osd_text(osd_fps_x, h, fps);
223 // if ((emu_opt & 0x400) && (PicoMCD & 1))
226 //gp2x_video_wait_vsync();
228 if (!(PicoOpt&0x10)) {
229 if (Pico.video.reg[1] & 8) {
230 if (currentConfig.EmuOpt&0x80)
231 DrawLineDest = (unsigned short *) giz_screen;
233 HighCol = gfx_buffer;
235 if (currentConfig.EmuOpt&0x80)
236 DrawLineDest = (unsigned short *) giz_screen + 320*8;
238 HighCol = gfx_buffer + 328*8;
243 // clears whole screen or just the notice area (in all buffers)
244 static void clearArea(int full)
246 if (giz_screen == NULL)
247 giz_screen = Framework2D_LockBuffer();
248 if (full) memset32(giz_screen, 0, 320*240*2/4);
249 else memset32((int *)((char *)giz_screen + 320*232*2), 0, 320*8*2/4);
252 static void vidResetMode(void)
256 } else if (currentConfig.EmuOpt&0x80) {
257 PicoDrawSetColorFormat(1);
258 PicoScan = EmuScan16;
260 PicoDrawSetColorFormat(0);
263 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
264 // setup pal for 8-bit modes
265 localPal[0xc0] = 0x0600;
266 localPal[0xd0] = 0xc000;
267 localPal[0xe0] = 0x0000; // reserved pixels for OSD
268 localPal[0xf0] = 0xffff;
271 screen = Framework2D_LockBuffer();
272 memset32(screen, 0, 320*240*2/4);
273 Framework2D_UnlockBuffer();
278 static void SkipFrame(int do_audio)
280 PicoSkipFrame=do_audio ? 1 : 2;
285 void emu_forcedFrame(void)
290 static void updateKeys(void)
294 static void simpleWait(DWORD until)
300 //static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
301 char fpsbuff[24]; // fps count c string
302 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
303 int frames_done = 0, frames_shown = 0, oldmodes = 0;
304 int target_fps, target_frametime, lim_time, tval_diff, i;
307 lprintf("entered emu_Loop()\n");
311 // make sure we are in correct mode
313 if (currentConfig.scaling) PicoOpt|=0x4000;
314 else PicoOpt&=~0x4000;
316 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
317 //find_combos(); // TODO
319 // pal/ntsc might have changed, reset related stuff
320 target_fps = Pico.m.pal ? 50 : 60;
321 target_frametime = (1000<<8)/target_fps;
324 // prepare sound stuff
325 /* if (currentConfig.EmuOpt & 4) {
327 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
328 sound_rerate(Pico.m.frame_count ? 1 : 0);
330 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
331 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
332 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
333 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
334 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
335 PicoWriteSound = updateSound;
336 memset(sndBuffer, 0, sizeof(sndBuffer));
338 PsndRate_old = PsndRate;
339 PsndLen_real = PsndLen;
340 PicoOpt_old = PicoOpt;
341 pal_old = Pico.m.pal;
347 if (PicoMCD & 1) PicoCDBufferInit();
350 while (engineState == PGS_Running)
354 tval = GetTickCount();
355 if (reset_timing || tval < tval_prev) {
358 frames_shown = frames_done = 0;
361 // show notice message?
363 static int noticeMsgSum;
364 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
369 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
370 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
375 // check for mode changes
376 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
377 if (modes != oldmodes) {
378 osd_fps_x = OSD_FPS_X;
380 vidCpy8to16 = vidCpy8to16_40;
382 // vidCpy8to16 = vidCpy8to16_32col;
388 if (tval - tval_thissec >= 1000)
391 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
394 bench_fps_s = bench_fps;
395 bf[bfp++ & 3] = bench_fps;
398 bench_fps += frames_shown;
399 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
401 if(currentConfig.EmuOpt & 2)
402 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
406 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
407 frames_done = frames_shown = 0;
409 // it is quite common for this implementation to leave 1 fame unfinished
410 // when second changes, but we don't want buffer to starve.
411 if (PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
413 SkipFrame(1); frames_done++;
416 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
417 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
418 if (frames_shown > frames_done) frames_shown = frames_done;
422 sprintf(fpsbuff, "%i", Pico.m.frame_count);
426 lim_time = (frames_done+1) * target_frametime;
427 if (currentConfig.Frameskip >= 0) // frameskip enabled
429 for (i = 0; i < currentConfig.Frameskip; i++) {
431 SkipFrame(1); frames_done++;
432 if (PsndOut) { // do framelimitting if sound is enabled
434 tval = GetTickCount();
435 tval_diff = (int)(tval - tval_thissec) << 8;
436 if (tval_diff < lim_time) // we are too fast
437 simpleWait(tval + ((lim_time - tval_diff)>>8));
439 lim_time += target_frametime;
442 else // auto frameskip
445 tval = GetTickCount();
446 tval_diff = (int)(tval - tval_thissec) << 8;
447 if (tval_diff > lim_time)
449 // no time left for this frame - skip
450 if (tval_diff - lim_time >= (300<<8)) {
451 /* something caused a slowdown for us (disk access? cache flush?)
452 * try to recover by resetting timing... */
457 SkipFrame(tval_diff < lim_time+target_frametime*2); frames_done++;
464 if (giz_screen == NULL)
465 giz_screen = Framework2D_LockBuffer();
468 blit(fpsbuff, notice);
470 if (giz_screen != NULL) {
471 Framework2D_UnlockBuffer();
474 //lprintf("after unlock\n");
477 tval = GetTickCount();
478 tval_diff = (int)(tval - tval_thissec) << 8;
480 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
482 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
484 // sleep if we are still too fast
485 if (tval_diff < lim_time)
488 simpleWait(tval + ((lim_time - tval_diff) >> 8));
492 frames_done++; frames_shown++;
496 if (PicoMCD & 1) PicoCDBufferFree();
499 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
500 emu_state_cb("Writing SRAM/BRAM..");
501 emu_SaveLoadGame(0, 1);
507 void emu_ResetGame(void)