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"
13 #include "../common/config.h"
17 #include "asm_utils.h"
19 #include <pico/pico_int.h>
27 // main 300K gfx-related buffer. Used by menu and renderers.
28 unsigned char gfx_buffer[321*240*2*2];
30 unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
33 static DWORD noticeMsgTime = 0;
34 static short *snd_cbuff = NULL;
35 static int snd_cbuf_samples = 0, snd_all_samples = 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 int emu_getMainDir(char *dst, int len)
48 if (len > 0) *dst = 0;
53 static void emu_msg_cb(const char *msg)
55 if (giz_screen != NULL) fb_unlock();
56 giz_screen = fb_lock(1);
58 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
59 emu_textOut16(4, 232, msg);
60 noticeMsgTime = GetTickCount() - 2000;
62 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
66 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
69 void emu_stateCb(const char *str)
71 if (giz_screen != NULL) fb_unlock();
72 giz_screen = fb_lock(1);
80 Sleep(0); /* yield the CPU, the system may need it */
83 static void emu_msg_tray_open(void)
85 strcpy(noticeMsg, "CD tray opened");
86 noticeMsgTime = GetTickCount();
92 // make dirs for saves, cfgs, etc.
99 PicoMessage = emu_msg_cb;
100 PicoMCDopenTray = emu_msg_tray_open;
101 PicoMCDcloseTray = menu_loop_tray;
104 void emu_Deinit(void)
107 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
108 emu_SaveLoadGame(0, 1);
112 if (!(currentConfig.EmuOpt & 0x20))
113 config_writelrom(PicoConfigFile);
118 void emu_prepareDefaultConfig(void)
120 memset(&defaultConfig, 0, sizeof(defaultConfig));
121 defaultConfig.EmuOpt = 0x1d | 0x680; // | confirm_save, cd_leds, 16bit rend
122 defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
123 defaultConfig.s_PsndRate = 22050;
124 defaultConfig.s_PicoRegion = 0; // auto
125 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
126 defaultConfig.s_PicoCDBuffers = 0;
127 defaultConfig.Frameskip = -1; // auto
128 defaultConfig.volume = 50;
129 defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
130 defaultConfig.KeyBinds[ 3] = 1<<1;
131 defaultConfig.KeyBinds[ 0] = 1<<2;
132 defaultConfig.KeyBinds[ 1] = 1<<3;
133 defaultConfig.KeyBinds[ 5] = 1<<4;
134 defaultConfig.KeyBinds[ 6] = 1<<5;
135 defaultConfig.KeyBinds[ 7] = 1<<6;
136 defaultConfig.KeyBinds[ 4] = 1<<7;
137 defaultConfig.KeyBinds[13] = 1<<26; // switch rend
138 defaultConfig.KeyBinds[ 8] = 1<<27; // save state
139 defaultConfig.KeyBinds[ 9] = 1<<28; // load state
140 defaultConfig.KeyBinds[12] = 1<<29; // vol up
141 defaultConfig.KeyBinds[11] = 1<<30; // vol down
142 defaultConfig.scaling = 0;
143 defaultConfig.turbo_rate = 15;
147 static int EmuScanBegin16(unsigned int num)
149 if (!(Pico.video.reg[1]&8)) num += 8;
150 DrawLineDest = (unsigned short *) giz_screen + 321 * num;
152 if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1))
153 return 1; // skip next line
158 static int EmuScanBegin8(unsigned int num)
160 // draw like the fast renderer
161 if (!(Pico.video.reg[1]&8)) num += 8;
162 HighCol = gfx_buffer + 328 * num;
167 static void osd_text(int x, int y, const char *text)
169 int len = strlen(text) * 8 / 2;
171 for (h = 0; h < 8; h++) {
172 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
173 p = (int *) ((int)p & ~3); // align
176 emu_textOut16(x, y, text);
180 void log1(void *p1, void *p2)
182 lprintf("%p %p %p\n", p1, p2, DrawLineDest);
186 static void cd_leds(void)
188 static int old_reg = 0;
189 unsigned int col_g, col_r, *p;
191 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
192 old_reg = Pico_mcd->s68k_regs[0];
194 p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
195 col_g = (old_reg & 2) ? 0x06000600 : 0;
196 col_r = (old_reg & 1) ? 0xc000c000 : 0;
197 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
198 *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
199 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
203 static short localPal[0x100];
205 static void blit(const char *fps, const char *notice)
207 int emu_opt = currentConfig.EmuOpt;
211 int lines_flags = 224;
212 // 8bit fast renderer
213 if (Pico.m.dirtyPal) {
215 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
218 if (PicoRead16Hook == PicoSVPRead16)
219 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
220 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
221 if (currentConfig.EmuOpt&0x4000)
222 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
223 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
225 else if (!(emu_opt&0x80))
228 // 8bit accurate renderer
229 if (Pico.m.dirtyPal) {
231 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
232 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
233 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
234 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
235 memcpy32((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2/4);
236 localPal[0xc0] = 0x0600;
237 localPal[0xd0] = 0xc000;
238 localPal[0xe0] = 0x0000; // reserved pixels for OSD
239 localPal[0xf0] = 0xffff;
242 else if (rendstatus & 0x20) { // mid-frame palette changes
243 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
244 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
247 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
248 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
249 if (currentConfig.EmuOpt&0x4000)
250 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
251 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
254 if (notice || (emu_opt & 2)) {
256 if (notice) osd_text(4, h, notice);
257 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
260 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
264 // clears whole screen or just the notice area (in all buffers)
265 static void clearArea(int full)
267 if (giz_screen == NULL)
268 giz_screen = fb_lock(1);
269 if (full) memset32(giz_screen, 0, 320*240*2/4);
270 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
272 if (currentConfig.EmuOpt&0x8000) {
274 giz_screen = fb_lock(0);
275 if (full) memset32(giz_screen, 0, 320*240*2/4);
276 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
280 static void vidResetMode(void)
282 giz_screen = fb_lock(1);
285 } else if (currentConfig.EmuOpt&0x80) {
286 PicoDrawSetColorFormat(1);
287 PicoScanBegin = EmuScanBegin16;
289 PicoDrawSetColorFormat(-1);
290 PicoScanBegin = EmuScanBegin8;
292 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
293 // setup pal for 8-bit modes
294 localPal[0xc0] = 0x0600;
295 localPal[0xd0] = 0xc000;
296 localPal[0xe0] = 0x0000; // reserved pixels for OSD
297 localPal[0xf0] = 0xffff;
301 memset32(giz_screen, 0, 321*240*2/4);
302 if (currentConfig.EmuOpt&0x8000) {
304 giz_screen = fb_lock(0);
305 memset32(giz_screen, 0, 321*240*2/4);
313 static void stdbg(const char *fmt, ...)
318 sprintf(noticeMsg, "%x ", cnt++);
320 vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
323 noticeMsgTime = GetTickCount();
327 static void updateSound(int len)
329 if (PicoOpt&8) len<<=1;
331 snd_all_samples += len;
333 if (PsndOut - snd_cbuff >= snd_cbuf_samples)
335 //if (PsndOut - snd_cbuff != snd_cbuf_samples)
336 // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
342 static void SkipFrame(void)
349 /* forced frame to front buffer */
350 void emu_forcedFrame(int opts)
352 int po_old = PicoOpt;
353 int eo_old = currentConfig.EmuOpt;
356 PicoOpt |= opts|POPT_ACC_SPRITES;
357 currentConfig.EmuOpt |= 0x80;
359 if (giz_screen == NULL)
360 giz_screen = fb_lock(1);
362 PicoDrawSetColorFormat(1);
363 PicoScanBegin = EmuScanBegin16;
371 currentConfig.EmuOpt = eo_old;
375 static void RunEvents(unsigned int which)
377 if (which & 0x1800) // save or load (but not both)
382 FrameworkAudio_SetPause(1);
383 if (giz_screen == NULL)
384 giz_screen = fb_lock(1);
385 if ( emu_checkSaveFile(state_slot) &&
386 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
387 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
390 blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
391 while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) )
393 if (keys & PBTN_STOP) do_it = 0;
394 while( ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release
401 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
402 PicoStateProgressCB = emu_stateCb;
403 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
404 PicoStateProgressCB = NULL;
409 FrameworkAudio_SetPause(0);
412 if (which & 0x0400) // switch renderer
414 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
415 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
420 strcpy(noticeMsg, " 8bit fast renderer");
421 } else if (currentConfig.EmuOpt&0x80) {
422 strcpy(noticeMsg, "16bit accurate renderer");
424 strcpy(noticeMsg, " 8bit accurate renderer");
427 noticeMsgTime = GetTickCount();
433 if(state_slot < 0) state_slot = 9;
436 if(state_slot > 9) state_slot = 0;
438 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
439 noticeMsgTime = GetTickCount();
443 static void updateKeys(void)
445 unsigned int keys, allActions[2] = { 0, 0 }, events;
446 static unsigned int prevEvents = 0;
449 /* FIXME: port to input fw */
450 keys = Framework_PollGetButtons();
451 if (keys & PBTN_HOME)
452 engineState = PGS_Menu;
454 keys &= CONFIGURABLE_KEYS;
456 PicoPad[0] = allActions[0] & 0xfff;
457 PicoPad[1] = allActions[1] & 0xfff;
459 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
460 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
462 events = (allActions[0] | allActions[1]) >> 16;
464 // volume is treated in special way and triggered every frame
465 if ((events & 0x6000) && PsndOut != NULL)
467 int vol = currentConfig.volume;
468 if (events & 0x2000) {
469 if (vol < 100) vol++;
473 FrameworkAudio_SetVolume(vol, vol);
474 sprintf(noticeMsg, "VOL: %02i ", vol);
475 noticeMsgTime = GetTickCount();
476 currentConfig.volume = vol;
479 events &= ~prevEvents;
480 if (events) RunEvents(events);
481 if (movie_data) emu_updateMovie();
483 prevEvents = (allActions[0] | allActions[1]) >> 16;
486 void emu_platformDebugCat(char *str)
491 static void simpleWait(DWORD until)
496 tval = GetTickCount();
497 diff = (int)until - (int)tval;
501 while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
502 spend_cycles(1024*2);
507 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
508 char fpsbuff[24]; // fps count c string
509 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
510 int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
511 int target_fps, target_frametime, lim_time, tval_diff, i;
514 lprintf("entered emu_Loop()\n");
518 // make sure we are in correct mode
520 if (currentConfig.scaling) PicoOpt|=0x4000;
521 else PicoOpt&=~0x4000;
523 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
525 // pal/ntsc might have changed, reset related stuff
526 target_fps = Pico.m.pal ? 50 : 60;
527 target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
531 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
533 // prepare sound stuff
535 if (currentConfig.EmuOpt & 4)
537 int ret, snd_excess_add, stereo;
538 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
539 PsndRerate(Pico.m.frame_count ? 1 : 0);
541 stereo=(PicoOpt&8)>>3;
542 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
543 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
544 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
545 PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
546 ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
548 lprintf("FrameworkAudio_Init() failed: %i\n", ret);
549 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
550 noticeMsgTime = GetTickCount();
551 currentConfig.EmuOpt &= ~4;
553 FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
554 PicoWriteSound = updateSound;
555 snd_cbuff = FrameworkAudio_56448Buffer();
556 PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
558 PsndRate_old = PsndRate;
559 PicoOpt_old = PicoOpt;
560 pal_old = Pico.m.pal;
565 while (engineState == PGS_Running)
569 tval = GetTickCount();
570 if (reset_timing || tval < tval_prev) {
571 //stdbg("timing reset");
574 frames_shown = frames_done = 0;
577 // show notice message?
579 static int noticeMsgSum;
580 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
585 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
586 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
591 // check for mode changes
592 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
593 if (modes != oldmodes) {
599 if (tval - tval_thissec >= sec_ms)
602 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
605 bench_fps_s = bench_fps;
606 bf[bfp++ & 3] = bench_fps;
609 bench_fps += frames_shown;
610 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
612 if(currentConfig.EmuOpt & 2)
613 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
615 //tval_thissec += 1000;
616 tval_thissec += sec_ms;
620 /* some code which tries to sync things to audio clock, the dirty way */
621 static int audio_skew_prev = 0;
622 int audio_skew, adj, co = 9, shift = 7;
623 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
624 if (PsndRate == 22050) co = 10;
625 if (PsndRate > 22050) co = 11;
626 if (PicoOpt&8) shift++;
627 if (audio_skew < 0) {
628 adj = -((-audio_skew) >> shift);
629 if (audio_skew > -(6<<co)) adj>>=1;
630 if (audio_skew > -(4<<co)) adj>>=1;
631 if (audio_skew > -(2<<co)) adj>>=1;
632 if (audio_skew > audio_skew_prev) adj>>=2; // going up already
634 adj = audio_skew >> shift;
635 if (audio_skew < (6<<co)) adj>>=1;
636 if (audio_skew < (4<<co)) adj>>=1;
637 if (audio_skew < (2<<co)) adj>>=1;
638 if (audio_skew < audio_skew_prev) adj>>=2;
640 audio_skew_prev = audio_skew;
641 target_frametime += adj;
642 sec_ms = (target_frametime * target_fps) >> 8;
643 //stdbg("%i %i %i", audio_skew, adj, sec_ms);
644 frames_done = frames_shown = 0;
646 else if (currentConfig.Frameskip < 0) {
647 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
648 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
649 if (frames_shown > frames_done) frames_shown = frames_done;
651 frames_done = frames_shown = 0;
655 sprintf(fpsbuff, "%i", Pico.m.frame_count);
659 lim_time = (frames_done+1) * target_frametime;
660 if (currentConfig.Frameskip >= 0) // frameskip enabled
662 for (i = 0; i < currentConfig.Frameskip; i++) {
664 SkipFrame(); frames_done++;
665 if (PsndOut) { // do framelimitting if sound is enabled
667 tval = GetTickCount();
668 tval_diff = (int)(tval - tval_thissec) << 8;
669 if (tval_diff < lim_time) // we are too fast
670 simpleWait(tval + ((lim_time - tval_diff)>>8));
672 lim_time += target_frametime;
675 else // auto frameskip
678 tval = GetTickCount();
679 tval_diff = (int)(tval - tval_thissec) << 8;
680 if (tval_diff > lim_time)
682 // no time left for this frame - skip
683 if (tval_diff - lim_time >= (300<<8)) {
684 /* something caused a slowdown for us (disk access? cache flush?)
685 * try to recover by resetting timing... */
690 SkipFrame(); frames_done++;
697 if (currentConfig.EmuOpt&0x80)
698 /* be sure correct framebuffer is locked */
699 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
703 if (giz_screen == NULL)
704 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
706 blit(fpsbuff, notice);
708 if (giz_screen != NULL) {
713 if (currentConfig.EmuOpt&0x2000)
714 Framework2D_WaitVSync();
716 if (currentConfig.EmuOpt&0x8000)
720 tval = GetTickCount();
721 tval_diff = (int)(tval - tval_thissec) << 8;
723 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
725 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
727 // sleep if we are still too fast
728 if (tval_diff < lim_time)
731 simpleWait(tval + ((lim_time - tval_diff) >> 8));
735 frames_done++; frames_shown++;
739 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
741 if (PsndOut != NULL) {
742 PsndOut = snd_cbuff = NULL;
743 FrameworkAudio_Close();
747 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
748 emu_stateCb("Writing SRAM/BRAM..");
749 emu_SaveLoadGame(0, 1);
755 void emu_ResetGame(void)