3 #include <sys/syslimits.h> // PATH_MAX
5 #include <pspthreadman.h>
6 #include <pspdisplay.h>
11 #include "../common/emu.h"
12 #include "../common/lprintf.h"
13 #include "../../Pico/PicoInt.h"
24 // 088000-0ae000 texture0
25 // 0ae000-0d4000 texture1
27 char romFileName[PATH_MAX];
28 static unsigned char picoD2FB[(8+320)*(8+240+8)];
29 unsigned char *PicoDraw2FB = picoD2FB; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
32 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
33 static unsigned int noticeMsgTime = 0;
34 int reset_timing = 0; // do we need this?
37 static void blit(const char *fps, const char *notice);
38 static void clearArea(int full);
40 void emu_noticeMsgUpdated(void)
42 noticeMsgTime = sceKernelGetSystemTimeLow();
45 void emu_getMainDir(char *dst, int len)
47 if (len > 0) *dst = 0;
50 static void emu_msg_cb(const char *msg)
52 void *fb = psp_video_get_active_fb();
54 memset32((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
55 emu_textOut16(4, 264, msg);
56 noticeMsgTime = sceKernelGetSystemTimeLow() - 2000000;
58 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
62 void emu_stateCb(const char *str)
68 static void emu_msg_tray_open(void)
70 strcpy(noticeMsg, "CD tray opened");
71 noticeMsgTime = sceKernelGetSystemTimeLow();
77 // make dirs for saves, cfgs, etc.
84 PicoMessage = emu_msg_cb;
85 PicoMCDopenTray = emu_msg_tray_open;
86 PicoMCDcloseTray = menu_loop_tray;
92 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
93 emu_SaveLoadGame(0, 1);
97 if (!(currentConfig.EmuOpt & 0x20)) {
98 FILE *f = fopen(PicoConfigFile, "r+b");
99 if (!f) emu_WriteConfig(0);
101 // if we already have config, reload it, except last ROM
102 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
103 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
104 fseek(f, 0, SEEK_SET);
105 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
114 void emu_setDefaultConfig(void)
116 memset(¤tConfig, 0, sizeof(currentConfig));
117 currentConfig.lastRomFile[0] = 0;
118 currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend
119 currentConfig.PicoOpt = 0x07 | 0xc00; // | cd_pcm, cd_cdda
120 currentConfig.PsndRate = 22050;
121 currentConfig.PicoRegion = 0; // auto
122 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
123 currentConfig.Frameskip = -1; // auto
124 currentConfig.volume = 50;
125 currentConfig.CPUclock = 222;
126 currentConfig.KeyBinds[ 4] = 1<<0; // SACB RLDU
127 currentConfig.KeyBinds[ 6] = 1<<1;
128 currentConfig.KeyBinds[ 7] = 1<<2;
129 currentConfig.KeyBinds[ 5] = 1<<3;
130 currentConfig.KeyBinds[14] = 1<<4;
131 currentConfig.KeyBinds[13] = 1<<5;
132 currentConfig.KeyBinds[15] = 1<<6;
133 currentConfig.KeyBinds[ 3] = 1<<7;
134 currentConfig.KeyBinds[23] = 1<<26; // switch rend
135 currentConfig.KeyBinds[ 8] = 1<<27; // save state
136 currentConfig.KeyBinds[ 9] = 1<<28; // load state
137 currentConfig.PicoCDBuffers = 0;
138 currentConfig.scaling = 0;
142 static int EmuScan16(unsigned int num, void *sdata)
144 if (!(Pico.video.reg[1]&8)) num += 8;
145 DrawLineDest = (unsigned short *) psp_screen + 512*(num+1);
150 static int EmuScan8(unsigned int num, void *sdata)
152 // draw like the fast renderer
154 //if (!(Pico.video.reg[1]&8)) num += 8;
155 //HighCol = gfx_buffer + 328*(num+1);
160 static void osd_text(int x, const char *text)
162 int len = strlen(text) * 8 / 2;
164 for (h = 0; h < 8; h++) {
165 p = (int *) ((unsigned short *) psp_screen+x+512*(264+h));
166 p = (int *) ((int)p & ~3); // align
169 emu_textOut16(x, 264, text);
173 static void cd_leds(void)
175 static int old_reg = 0;
176 unsigned int col_g, col_r, *p;
178 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
179 old_reg = Pico_mcd->s68k_regs[0];
181 p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
182 col_g = (old_reg & 2) ? 0x06000600 : 0;
183 col_r = (old_reg & 1) ? 0xc000c000 : 0;
184 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
185 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
186 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
190 static short localPal[0x100];
192 static void blit(const char *fps, const char *notice)
194 int emu_opt = currentConfig.EmuOpt;
199 int lines_flags = 224;
200 // 8bit fast renderer
201 if (Pico.m.dirtyPal) {
203 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
205 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
206 if (currentConfig.EmuOpt&0x4000)
207 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
208 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
210 else if (!(emu_opt&0x80))
213 // 8bit accurate renderer
214 if (Pico.m.dirtyPal) {
216 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
217 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
218 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
219 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
220 blockcpy(localPal+0xc0, localPal+0x40, 0x40*2);
221 localPal[0xc0] = 0x0600;
222 localPal[0xd0] = 0xc000;
223 localPal[0xe0] = 0x0000; // reserved pixels for OSD
224 localPal[0xf0] = 0xffff;
227 else if (rendstatus & 0x20) { // mid-frame palette changes
228 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
229 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
232 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
233 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
234 if (currentConfig.EmuOpt&0x4000)
235 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
236 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
240 if (notice || (emu_opt & 2)) {
241 if (notice) osd_text(4, notice);
242 if (emu_opt & 2) osd_text(OSD_FPS_X, fps);
245 if ((emu_opt & 0x400) && (PicoMCD & 1))
251 // clears whole screen or just the notice area (in all buffers)
252 static void clearArea(int full)
255 memset32(psp_screen, 0, 512*272*2/4);
257 memset32(psp_screen, 0, 512*272*2/4);
259 void *fb = psp_video_get_active_fb();
260 memset32((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
261 memset32((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
265 static void vidResetMode(void)
268 } else if (currentConfig.EmuOpt&0x80) {
269 PicoDrawSetColorFormat(1);
270 PicoScan = EmuScan16;
272 PicoDrawSetColorFormat(-1);
275 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
276 // setup pal for 8-bit modes
277 localPal[0xc0] = 0x0600;
278 localPal[0xd0] = 0xc000;
279 localPal[0xe0] = 0x0000; // reserved pixels for OSD
280 localPal[0xf0] = 0xffff;
287 static void updateSound(int len)
289 if (PicoOpt&8) len<<=1;
295 static void SkipFrame(void)
302 void emu_forcedFrame(void)
304 int po_old = PicoOpt;
305 int eo_old = currentConfig.EmuOpt;
308 PicoOpt |= 0x4080; // soft_scale | acc_sprites
309 currentConfig.EmuOpt |= 0x80;
311 PicoDrawSetColorFormat(1);
312 PicoScan = EmuScan16;
313 PicoScan((unsigned) -1, NULL);
318 currentConfig.EmuOpt = eo_old;
322 static void RunEvents(unsigned int which)
324 if (which & 0x1800) // save or load (but not both)
328 if ( emu_checkSaveFile(state_slot) &&
329 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
330 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
333 blit("", (which & 0x1000) ? "LOAD STATE? (X=yes, O=no)" : "OVERWRITE SAVE? (X=yes, O=no)");
334 while( !((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) )
336 if (keys & BTN_CIRCLE) do_it = 0;
337 while( ((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) ) // wait for release
344 osd_text(4, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
345 PicoStateProgressCB = emu_stateCb;
346 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
347 PicoStateProgressCB = NULL;
353 if (which & 0x0400) // switch renderer
355 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
356 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
361 strcpy(noticeMsg, " 8bit fast renderer");
362 } else if (currentConfig.EmuOpt&0x80) {
363 strcpy(noticeMsg, "16bit accurate renderer");
365 strcpy(noticeMsg, " 8bit accurate renderer");
368 noticeMsgTime = sceKernelGetSystemTimeLow();
374 if(state_slot < 0) state_slot = 9;
377 if(state_slot > 9) state_slot = 0;
379 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
380 noticeMsgTime = sceKernelGetSystemTimeLow();
384 static void updateKeys(void)
386 unsigned int keys, allActions[2] = { 0, 0 }, events;
387 static unsigned int prevEvents = 0;
390 keys = psp_pad_read(0);
391 if (keys & PSP_CTRL_HOME)
392 sceDisplayWaitVblankStart();
394 if (keys & BTN_SELECT)
395 engineState = PGS_Menu;
397 keys &= CONFIGURABLE_KEYS;
399 for (i = 0; i < 32; i++)
403 int pl, acts = currentConfig.KeyBinds[i];
405 pl = (acts >> 16) & 1;
406 if (combo_keys & (1 << i))
408 int u = i+1, acts_c = acts & combo_acts;
409 // let's try to find the other one
412 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
413 allActions[pl] |= acts_c;
414 keys &= ~((1 << i) | (1 << u));
417 // add non-combo actions if combo ones were not found
418 if (!acts_c || u == 32)
419 allActions[pl] |= acts & ~combo_acts;
421 allActions[pl] |= acts;
426 PicoPad[0] = (unsigned short) allActions[0];
427 PicoPad[1] = (unsigned short) allActions[1];
429 events = (allActions[0] | allActions[1]) >> 16;
431 // volume is treated in special way and triggered every frame
432 if ((events & 0x6000) && PsndOut != NULL)
434 int vol = currentConfig.volume;
435 if (events & 0x2000) {
436 if (vol < 100) vol++;
440 // FrameworkAudio_SetVolume(vol, vol); // TODO
441 sprintf(noticeMsg, "VOL: %02i ", vol);
442 noticeMsgTime = sceKernelGetSystemTimeLow();
443 currentConfig.volume = vol;
446 events &= ~prevEvents;
447 if (events) RunEvents(events);
448 if (movie_data) emu_updateMovie();
450 prevEvents = (allActions[0] | allActions[1]) >> 16;
453 static void find_combos(void)
457 // find out which keys and actions are combos
458 combo_keys = combo_acts = 0;
459 for (act = 0; act < 32; act++)
462 if (act == 16 || act == 17) continue; // player2 flag
463 for (u = 0; u < 32; u++)
465 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
469 // loop again and mark those keys and actions as combo
470 for (u = 0; u < 32; u++)
472 if (currentConfig.KeyBinds[u] & (1 << act)) {
473 combo_keys |= 1 << u;
474 combo_acts |= 1 << act;
482 static void simpleWait(unsigned int until)
487 tval = sceKernelGetSystemTimeLow();
488 diff = (int)until - (int)tval;
489 if (diff >= 512 && diff < 100*1024)
490 sceKernelDelayThread(diff);
495 //static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
496 char fpsbuff[24]; // fps count c string
497 unsigned int tval, tval_prev = 0, tval_thissec = 0; // timing
498 int frames_done = 0, frames_shown = 0, oldmodes = 0;
499 int target_fps, target_frametime, lim_time, tval_diff, i;
502 lprintf("entered emu_Loop()\n");
506 if (currentConfig.CPUclock != psp_get_cpu_clock()) {
507 lprintf("setting cpu clock to %iMHz... ", currentConfig.CPUclock);
508 i = psp_set_cpu_clock(currentConfig.CPUclock);
509 lprintf(i ? "failed\n" : "done\n");
510 currentConfig.CPUclock = psp_get_cpu_clock();
513 // make sure we are in correct mode
516 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
519 // pal/ntsc might have changed, reset related stuff
520 target_fps = Pico.m.pal ? 50 : 60;
521 target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
525 if (PicoMCD & 1) PicoCDBufferInit();
527 // prepare sound stuff
530 if (currentConfig.EmuOpt & 4)
532 int ret, snd_excess_add, stereo;
533 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
534 sound_rerate(Pico.m.frame_count ? 1 : 0);
536 stereo=(PicoOpt&8)>>3;
537 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
538 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
539 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
540 PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
541 ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
543 lprintf("FrameworkAudio_Init() failed: %i\n", ret);
544 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
545 noticeMsgTime = sceKernelGetSystemTimeLow();
546 currentConfig.EmuOpt &= ~4;
548 FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
549 PicoWriteSound = updateSound;
550 snd_cbuff = FrameworkAudio_56448Buffer();
551 PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
553 PsndRate_old = PsndRate;
554 PicoOpt_old = PicoOpt;
555 pal_old = Pico.m.pal;
561 while (engineState == PGS_Running)
565 tval = sceKernelGetSystemTimeLow();
566 if (reset_timing || tval < tval_prev) {
567 //stdbg("timing reset");
570 frames_shown = frames_done = 0;
573 // show notice message?
575 static int noticeMsgSum;
576 if (tval - noticeMsgTime > 2000000) { // > 2.0 sec
581 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
582 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
587 // check for mode changes
588 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
589 if (modes != oldmodes) {
595 if (tval - tval_thissec >= 1000000)
598 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
601 bench_fps_s = bench_fps;
602 bf[bfp++ & 3] = bench_fps;
605 bench_fps += frames_shown;
606 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
608 if(currentConfig.EmuOpt & 2)
609 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
611 tval_thissec += 1000000;
613 if (currentConfig.Frameskip < 0) {
614 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
615 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
616 if (frames_shown > frames_done) frames_shown = frames_done;
618 frames_done = frames_shown = 0;
622 sprintf(fpsbuff, "%i", Pico.m.frame_count);
626 lim_time = (frames_done+1) * target_frametime;
627 if (currentConfig.Frameskip >= 0) // frameskip enabled
629 for (i = 0; i < currentConfig.Frameskip; i++) {
631 SkipFrame(); frames_done++;
632 if (PsndOut) { // do framelimitting if sound is enabled
634 tval = sceKernelGetSystemTimeLow();
635 tval_diff = (int)(tval - tval_thissec) << 8;
636 if (tval_diff < lim_time) // we are too fast
637 simpleWait(tval + ((lim_time - tval_diff)>>8));
639 lim_time += target_frametime;
642 else // auto frameskip
645 tval = sceKernelGetSystemTimeLow();
646 tval_diff = (int)(tval - tval_thissec) << 8;
647 if (tval_diff > lim_time)
649 // no time left for this frame - skip
650 if (tval_diff - lim_time >= (300000<<8)) {
651 /* something caused a slowdown for us (disk access? cache flush?)
652 * try to recover by resetting timing... */
657 SkipFrame(); frames_done++;
665 PicoScan((unsigned) -1, NULL);
669 blit(fpsbuff, notice);
672 tval = sceKernelGetSystemTimeLow();
673 tval_diff = (int)(tval - tval_thissec) << 8;
675 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300000<<8)) // slowdown detection
677 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
679 // sleep if we are still too fast
680 if (tval_diff < lim_time)
683 simpleWait(tval + ((lim_time - tval_diff) >> 8));
687 frames_done++; frames_shown++;
691 if (PicoMCD & 1) PicoCDBufferFree();
693 if (PsndOut != NULL) {
694 PsndOut = snd_cbuff = NULL;
695 FrameworkAudio_Close();
699 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
700 emu_stateCb("Writing SRAM/BRAM..");
701 emu_SaveLoadGame(0, 1);
707 void emu_ResetGame(void)