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 int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
35 static DWORD noticeMsgTime = 0;
36 static short *snd_cbuff = NULL;
37 static int snd_cbuf_samples = 0, snd_all_samples = 0;
40 static void blit(const char *fps, const char *notice);
41 static void clearArea(int full);
43 void emu_noticeMsgUpdated(void)
45 noticeMsgTime = GetTickCount();
48 void emu_getMainDir(char *dst, int len)
50 if (len > 0) *dst = 0;
53 static void emu_msg_cb(const char *msg)
55 if (giz_screen == NULL)
56 giz_screen = Framework2D_LockBuffer();
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 static void emu_state_cb(const char *str)
68 if (giz_screen == NULL)
69 giz_screen = Framework2D_LockBuffer();
74 Framework2D_UnlockBuffer();
78 static void emu_msg_tray_open(void)
80 strcpy(noticeMsg, "CD tray opened");
81 noticeMsgTime = GetTickCount();
87 // make dirs for saves, cfgs, etc.
94 PicoMessage = emu_msg_cb;
95 PicoMCDopenTray = emu_msg_tray_open;
96 PicoMCDcloseTray = menu_loop_tray;
102 if((currentConfig.EmuOpt & 1) && SRam.changed) {
103 emu_SaveLoadGame(0, 1);
107 if (!(currentConfig.EmuOpt & 0x20)) {
108 FILE *f = fopen(PicoConfigFile, "r+b");
109 if (!f) emu_WriteConfig(0);
111 // if we already have config, reload it, except last ROM
112 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
113 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
114 fseek(f, 0, SEEK_SET);
115 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
124 void emu_setDefaultConfig(void)
126 memset(¤tConfig, 0, sizeof(currentConfig));
127 currentConfig.lastRomFile[0] = 0;
128 currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend
129 currentConfig.PicoOpt = 0x07 | 0xc00; // | cd_pcm, cd_cdda
130 currentConfig.PsndRate = 22050;
131 currentConfig.PicoRegion = 0; // auto
132 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
133 currentConfig.Frameskip = -1; // auto
134 currentConfig.volume = 50;
135 currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
136 currentConfig.KeyBinds[ 3] = 1<<1;
137 currentConfig.KeyBinds[ 0] = 1<<2;
138 currentConfig.KeyBinds[ 1] = 1<<3;
139 currentConfig.KeyBinds[ 5] = 1<<4;
140 currentConfig.KeyBinds[ 6] = 1<<5;
141 currentConfig.KeyBinds[ 7] = 1<<6;
142 currentConfig.KeyBinds[ 4] = 1<<7;
143 currentConfig.KeyBinds[ 8] = 1<<27; // save state
144 currentConfig.KeyBinds[ 9] = 1<<28; // load state
145 currentConfig.KeyBinds[12] = 1<<29; // vol up
146 currentConfig.KeyBinds[11] = 1<<30; // vol down
147 currentConfig.PicoCDBuffers = 0;
148 currentConfig.scaling = 0;
152 static int EmuScan16(unsigned int num, void *sdata)
154 if (!(Pico.video.reg[1]&8)) num += 8;
155 DrawLineDest = (unsigned short *) giz_screen + 321*(num+1);
160 static int EmuScan8(unsigned int num, void *sdata)
162 // draw like the fast renderer
163 if (!(Pico.video.reg[1]&8)) num += 8;
164 HighCol = gfx_buffer + 328*(num+1);
169 static void osd_text(int x, int y, const char *text)
171 int len = strlen(text) * 8 / 2;
173 for (h = 0; h < 8; h++) {
174 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
175 p = (int *) ((int)p & ~3); // align
178 emu_textOut16(x, y, text);
182 void log1(void *p1, void *p2)
184 lprintf("%p %p %p\n", p1, p2, DrawLineDest);
188 static void cd_leds(void)
190 static int old_reg = 0;
191 unsigned int col_g, col_r, *p;
193 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
194 old_reg = Pico_mcd->s68k_regs[0];
196 p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
197 col_g = (old_reg & 2) ? 0x06000600 : 0;
198 col_r = (old_reg & 1) ? 0xc000c000 : 0;
199 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
200 *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
201 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
205 static short localPal[0x100];
207 static void blit(const char *fps, const char *notice)
209 int emu_opt = currentConfig.EmuOpt;
213 int lines_flags = 224;
214 // 8bit fast renderer
215 if (Pico.m.dirtyPal) {
217 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
219 if (!(Pico.video.reg[12]&1)) lines_flags|=0x100;
220 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
222 else if (!(emu_opt&0x80))
225 // 8bit accurate renderer
226 if (Pico.m.dirtyPal) {
228 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
229 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
230 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
231 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
232 blockcpy(localPal+0xc0, localPal+0x40, 0x40*2);
233 localPal[0xc0] = 0x0600;
234 localPal[0xd0] = 0xc000;
235 localPal[0xe0] = 0x0000; // reserved pixels for OSD
236 localPal[0xf0] = 0xffff;
239 else if (rendstatus & 0x20) { // mid-frame palette changes
240 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
241 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
244 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
245 if (!(Pico.video.reg[12]&1)) lines_flags|=0x100;
246 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
249 if (notice || (emu_opt & 2)) {
251 if (notice) osd_text(4, h, notice);
252 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
255 if ((emu_opt & 0x400) && (PicoMCD & 1))
259 // clears whole screen or just the notice area (in all buffers)
260 static void clearArea(int full)
262 if (giz_screen == NULL)
263 giz_screen = Framework2D_LockBuffer();
264 if (full) memset32(giz_screen, 0, 320*240*2/4);
265 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
268 static void vidResetMode(void)
270 giz_screen = Framework2D_LockBuffer();
273 } else if (currentConfig.EmuOpt&0x80) {
274 PicoDrawSetColorFormat(1);
275 PicoScan = EmuScan16;
277 PicoDrawSetColorFormat(-1);
280 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
281 // setup pal for 8-bit modes
282 localPal[0xc0] = 0x0600;
283 localPal[0xd0] = 0xc000;
284 localPal[0xe0] = 0x0000; // reserved pixels for OSD
285 localPal[0xf0] = 0xffff;
289 memset32(giz_screen, 0, 321*240*2/4);
290 Framework2D_UnlockBuffer();
296 static void stdbg(const char *fmt, ...)
301 sprintf(noticeMsg, "%x ", cnt++);
303 vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
306 noticeMsgTime = GetTickCount();
310 static void updateSound(int len)
312 if (PicoOpt&8) len<<=1;
314 snd_all_samples += len;
316 if (PsndOut - snd_cbuff >= snd_cbuf_samples)
318 if (PsndOut - snd_cbuff != snd_cbuf_samples)
319 stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
325 static void SkipFrame(void)
332 void emu_forcedFrame(void)
334 int po_old = PicoOpt;
335 int eo_old = currentConfig.EmuOpt;
338 PicoOpt |= 0x4080; // soft_scale | acc_sprites
339 currentConfig.EmuOpt |= 0x80;
341 if (giz_screen == NULL)
342 giz_screen = Framework2D_LockBuffer();
344 PicoDrawSetColorFormat(1);
345 PicoScan = EmuScan16;
346 PicoScan((unsigned) -1, NULL);
350 Framework2D_UnlockBuffer();
354 currentConfig.EmuOpt = eo_old;
358 static void RunEvents(unsigned int which)
360 if (which & 0x1800) { // save or load (but not both)
362 if ( emu_checkSaveFile(state_slot) &&
363 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
364 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
366 blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
367 while( !((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) )
369 if (keys & BTN_STOP) do_it = 0;
373 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
374 PicoStateProgressCB = emu_state_cb;
375 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
376 PicoStateProgressCB = NULL;
381 if (which & 0x0400) { // switch renderer
382 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
383 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
384 else currentConfig.EmuOpt &= ~0x80;
389 strcpy(noticeMsg, " 8bit fast renderer");
390 } else if (currentConfig.EmuOpt&0x80) {
391 strcpy(noticeMsg, "16bit accurate renderer");
393 strcpy(noticeMsg, " 8bit accurate renderer");
396 noticeMsgTime = GetTickCount();
398 if (which & 0x0300) {
401 if(state_slot < 0) state_slot = 9;
404 if(state_slot > 9) state_slot = 0;
406 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
407 noticeMsgTime = GetTickCount();
411 static void updateKeys(void)
413 unsigned int keys, allActions[2] = { 0, 0 }, events;
414 static unsigned int prevEvents = 0;
417 keys = Framework_PollGetButtons();
419 engineState = PGS_Menu;
421 keys &= CONFIGURABLE_KEYS;
423 for (i = 0; i < 32; i++)
427 int pl, acts = currentConfig.KeyBinds[i];
429 pl = (acts >> 16) & 1;
430 if (combo_keys & (1 << i))
432 int u = i+1, acts_c = acts & combo_acts;
433 // let's try to find the other one
436 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
437 allActions[pl] |= acts_c;
438 keys &= ~((1 << i) | (1 << u));
441 // add non-combo actions if combo ones were not found
442 if (!acts_c || u == 32)
443 allActions[pl] |= acts & ~combo_acts;
445 allActions[pl] |= acts;
450 PicoPad[0] = (unsigned short) allActions[0];
451 PicoPad[1] = (unsigned short) allActions[1];
453 events = (allActions[0] | allActions[1]) >> 16;
455 // volume is treated in special way and triggered every frame
456 if ((events & 0x6000) && PsndOut != NULL)
458 int vol = currentConfig.volume;
459 if (events & 0x2000) {
460 if (vol < 100) vol++;
464 FrameworkAudio_SetVolume(vol, vol);
465 sprintf(noticeMsg, "VOL: %02i", vol);
466 noticeMsgTime = GetTickCount();
467 currentConfig.volume = vol;
470 events &= ~prevEvents;
471 if (events) RunEvents(events);
472 if (movie_data) emu_updateMovie();
474 prevEvents = (allActions[0] | allActions[1]) >> 16;
477 static void find_combos(void)
481 // find out which keys and actions are combos
482 combo_keys = combo_acts = 0;
483 for (act = 0; act < 32; act++)
486 if (act == 16 || act == 17) continue; // player2 flag
487 for (u = 0; u < 32; u++)
489 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
493 // loop again and mark those keys and actions as combo
494 for (u = 0; u < 32; u++)
496 if (currentConfig.KeyBinds[u] & (1 << act)) {
497 combo_keys |= 1 << u;
498 combo_acts |= 1 << act;
506 static void simpleWait(DWORD until)
511 tval = GetTickCount();
512 diff = (int)until - (int)tval;
516 while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
517 spend_cycles(1024*2);
522 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
523 char fpsbuff[24]; // fps count c string
524 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
525 int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
526 int target_fps, target_frametime, lim_time, tval_diff, i;
529 lprintf("entered emu_Loop()\n");
533 // make sure we are in correct mode
535 if (currentConfig.scaling) PicoOpt|=0x4000;
536 else PicoOpt&=~0x4000;
538 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
541 // pal/ntsc might have changed, reset related stuff
542 target_fps = Pico.m.pal ? 50 : 60;
543 target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
547 if (PicoMCD & 1) PicoCDBufferInit();
549 // prepare sound stuff
551 if (currentConfig.EmuOpt & 4)
553 int ret, snd_excess_add, stereo=(PicoOpt&8)>>3;
554 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
555 sound_rerate(Pico.m.frame_count ? 1 : 0);
557 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
558 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
559 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
560 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
561 ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
563 lprintf("FrameworkAudio_Init() failed: %i\n", ret);
564 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
565 noticeMsgTime = GetTickCount();
566 currentConfig.EmuOpt &= ~4;
568 FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
569 PicoWriteSound = updateSound;
570 snd_cbuff = FrameworkAudio_56448Buffer();
571 PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
573 PsndRate_old = PsndRate;
574 PicoOpt_old = PicoOpt;
575 pal_old = Pico.m.pal;
580 while (engineState == PGS_Running)
584 tval = GetTickCount();
585 if (reset_timing || tval < tval_prev) {
586 stdbg("timing reset");
589 frames_shown = frames_done = 0;
592 // show notice message?
594 static int noticeMsgSum;
595 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
600 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
601 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
606 // check for mode changes
607 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
608 if (modes != oldmodes) {
614 if (tval - tval_thissec >= sec_ms)
617 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
620 bench_fps_s = bench_fps;
621 bf[bfp++ & 3] = bench_fps;
624 bench_fps += frames_shown;
625 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
627 if(currentConfig.EmuOpt & 2)
628 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
630 //tval_thissec += 1000;
631 tval_thissec += sec_ms;
636 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
638 adj = -((-audio_skew) >> 10);
639 else adj = audio_skew >> 10;
640 target_frametime += adj;
641 sec_ms = (target_frametime * target_fps) >> 8;
642 stdbg("%i %i %i", audio_skew, adj, sec_ms);
643 frames_done = frames_shown = 0;
645 else if (currentConfig.Frameskip < 0) {
646 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
647 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
648 if (frames_shown > frames_done) frames_shown = frames_done;
650 frames_done = frames_shown = 0;
654 sprintf(fpsbuff, "%i", Pico.m.frame_count);
658 lim_time = (frames_done+1) * target_frametime;
659 if (currentConfig.Frameskip >= 0) // frameskip enabled
661 for (i = 0; i < currentConfig.Frameskip; i++) {
663 SkipFrame(); frames_done++;
664 if (PsndOut) { // do framelimitting if sound is enabled
666 tval = GetTickCount();
667 tval_diff = (int)(tval - tval_thissec) << 8;
668 if (tval_diff < lim_time) // we are too fast
669 simpleWait(tval + ((lim_time - tval_diff)>>8));
671 lim_time += target_frametime;
674 else // auto frameskip
677 tval = GetTickCount();
678 tval_diff = (int)(tval - tval_thissec) << 8;
679 if (tval_diff > lim_time)
681 // no time left for this frame - skip
682 if (tval_diff - lim_time >= (300<<8)) {
683 /* something caused a slowdown for us (disk access? cache flush?)
684 * try to recover by resetting timing... */
689 SkipFrame(); frames_done++;
696 if (giz_screen == NULL && (currentConfig.EmuOpt&0x80))
697 giz_screen = Framework2D_LockBuffer();
699 PicoScan((unsigned) -1, NULL);
703 if (giz_screen == NULL)
704 giz_screen = Framework2D_LockBuffer();
706 blit(fpsbuff, notice);
708 if (giz_screen != NULL) {
709 Framework2D_UnlockBuffer();
714 tval = GetTickCount();
715 tval_diff = (int)(tval - tval_thissec) << 8;
717 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
719 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
721 // sleep if we are still too fast
722 if (tval_diff < lim_time)
725 simpleWait(tval + ((lim_time - tval_diff) >> 8));
729 frames_done++; frames_shown++;
733 if (PicoMCD & 1) PicoCDBufferFree();
735 if (PsndOut != NULL) {
736 PsndOut = snd_cbuff = NULL;
737 FrameworkAudio_Close();
741 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
742 emu_state_cb("Writing SRAM/BRAM..");
743 emu_SaveLoadGame(0, 1);
749 void emu_ResetGame(void)