3 #include <sys/syslimits.h> // PATH_MAX
5 #include <pspthreadman.h>
6 #include <pspdisplay.h>
14 #include "../common/emu.h"
15 #include "../common/lprintf.h"
16 #include "../../Pico/PicoInt.h"
20 // additional pspaudio imports, credits to crazyc
21 int sceAudio_38553111(unsigned short samples, unsigned short freq, char unknown); // play with conversion?
22 int sceAudio_5C37C0AE(void); // end play?
23 int sceAudio_E0727056(int volume, void *buffer); // blocking output
24 int sceAudioOutput2GetRestSample();
27 char romFileName[PATH_MAX];
28 unsigned char *PicoDraw2FB = (unsigned char *)VRAM_CACHED_STUFF + 8; // +8 to be able to skip border with 1 quadword..
31 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
32 static unsigned int noticeMsgTime = 0;
33 int reset_timing = 0; // do we need this?
36 static void sound_init(void);
37 static void sound_deinit(void);
38 static void blit2(const char *fps, const char *notice, int lagging_behind);
39 static void clearArea(int full);
41 void emu_noticeMsgUpdated(void)
43 noticeMsgTime = sceKernelGetSystemTimeLow();
46 void emu_getMainDir(char *dst, int len)
48 if (len > 0) *dst = 0;
51 static void osd_text(int x, const char *text, int is_active)
53 unsigned short *screen = is_active ? psp_video_get_active_fb() : psp_screen;
54 int len = strlen(text) * 8 / 2;
57 for (h = 0; h < 8; h++) {
58 p = (int *) (screen+x+512*(264+h));
59 p = (int *) ((int)p & ~3); // align
62 if (is_active) { tmp = psp_screen; psp_screen = screen; } // nasty pointer tricks
63 emu_textOut16(x, 264, text);
64 if (is_active) psp_screen = tmp;
67 void emu_msg_cb(const char *msg)
70 noticeMsgTime = sceKernelGetSystemTimeLow() - 2000000;
72 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
76 static void emu_msg_tray_open(void)
78 strcpy(noticeMsg, "CD tray opened");
79 noticeMsgTime = sceKernelGetSystemTimeLow();
85 // 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);
125 void emu_setDefaultConfig(void)
127 memset(¤tConfig, 0, sizeof(currentConfig));
128 currentConfig.lastRomFile[0] = 0;
129 currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend
130 currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda
131 currentConfig.PsndRate = 22050;
132 currentConfig.PicoRegion = 0; // auto
133 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
134 currentConfig.Frameskip = -1; // auto
135 currentConfig.volume = 50;
136 currentConfig.CPUclock = 333;
137 currentConfig.KeyBinds[ 4] = 1<<0; // SACB RLDU
138 currentConfig.KeyBinds[ 6] = 1<<1;
139 currentConfig.KeyBinds[ 7] = 1<<2;
140 currentConfig.KeyBinds[ 5] = 1<<3;
141 currentConfig.KeyBinds[14] = 1<<4;
142 currentConfig.KeyBinds[13] = 1<<5;
143 currentConfig.KeyBinds[15] = 1<<6;
144 currentConfig.KeyBinds[ 3] = 1<<7;
145 currentConfig.KeyBinds[12] = 1<<26; // switch rnd
146 currentConfig.KeyBinds[ 8] = 1<<27; // save state
147 currentConfig.KeyBinds[ 9] = 1<<28; // load state
148 currentConfig.KeyBinds[28] = 1<<0; // num "buttons"
149 currentConfig.KeyBinds[30] = 1<<1;
150 currentConfig.KeyBinds[31] = 1<<2;
151 currentConfig.KeyBinds[29] = 1<<3;
152 currentConfig.PicoCDBuffers = 0;
153 currentConfig.scaling = 1; // bilinear filtering for psp
154 currentConfig.scale = 1.20; // fullscreen
155 currentConfig.hscale40 = 1.25;
156 currentConfig.hscale32 = 1.56;
160 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
168 static struct Vertex __attribute__((aligned(4))) g_vertices[2];
169 static unsigned short __attribute__((aligned(16))) localPal[0x100];
170 static int dynamic_palette = 0, need_pal_upload = 0, blit_16bit_mode = 0;
171 static int fbimg_offs = 0;
173 static void set_scaling_params(void)
175 int src_width, fbimg_width, fbimg_height, fbimg_xoffs, fbimg_yoffs;
176 g_vertices[0].x = g_vertices[0].y =
177 g_vertices[0].z = g_vertices[1].z = 0;
179 fbimg_height = (int)(240.0 * currentConfig.scale + 0.5);
180 if (Pico.video.reg[12] & 1) {
181 fbimg_width = (int)(320.0 * currentConfig.scale * currentConfig.hscale40 + 0.5);
184 fbimg_width = (int)(256.0 * currentConfig.scale * currentConfig.hscale32 + 0.5);
188 if (fbimg_width >= 480) {
189 g_vertices[0].u = (fbimg_width-480)/2;
190 g_vertices[1].u = src_width - (fbimg_width-480)/2;
195 g_vertices[1].u = src_width;
196 fbimg_xoffs = 240 - fbimg_width/2;
199 if (fbimg_height >= 272) {
200 g_vertices[0].v = (fbimg_height-272)/2;
201 g_vertices[1].v = 240 - (fbimg_height-272)/2;
206 g_vertices[1].v = 240;
207 fbimg_yoffs = 136 - fbimg_height/2;
210 g_vertices[1].x = fbimg_width;
211 g_vertices[1].y = fbimg_height;
212 if (fbimg_xoffs < 0) fbimg_xoffs = 0;
213 if (fbimg_yoffs < 0) fbimg_yoffs = 0;
214 fbimg_offs = (fbimg_yoffs*512 + fbimg_xoffs) * 2; // dst is always 16bit
217 lprintf("set_scaling_params:\n");
218 lprintf("offs: %i, %i\n", fbimg_xoffs, fbimg_yoffs);
219 lprintf("xy0, xy1: %i, %i; %i, %i\n", g_vertices[0].x, g_vertices[0].y, g_vertices[1].x, g_vertices[1].y);
220 lprintf("uv0, uv1: %i, %i; %i, %i\n", g_vertices[0].u, g_vertices[0].v, g_vertices[1].u, g_vertices[1].v);
224 static void do_pal_update(int allow_sh)
226 unsigned int *spal=(void *)Pico.cram;
227 unsigned int *dpal=(void *)localPal;
230 for (i = 0x3f/2; i >= 0; i--)
231 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
233 if (allow_sh && (Pico.video.reg[0xC]&8)) // shadow/hilight?
236 for (i = 0x3f/2; i >= 0; i--)
237 dpal[0x20|i] = dpal[0x60|i] = (dpal[i]>>1)&0x738e738e;
239 for (i = 0x3f; i >= 0; i--) {
240 int t=localPal[i]&0xe71c;t+=0x4208;
242 if (t&0x800) t|=0x700;
243 if (t&0x10000) t|=0xe000;
245 localPal[0x80|i]=(unsigned short)t;
253 static void do_slowmode_lines(int line_to)
255 int line = 0, line_len = (Pico.video.reg[12]&1) ? 320 : 256;
256 unsigned short *dst = (unsigned short *)VRAM_STUFF + 512*240/2;
257 unsigned char *src = (unsigned char *)VRAM_CACHED_STUFF + 16;
258 if (!(Pico.video.reg[1]&8)) { line = 8; dst += 512*8; src += 512*8; }
260 for (; line < line_to; line++, dst+=512, src+=512)
261 amips_clut(dst, src, localPal, line_len);
264 static void EmuScanPrepare(void)
266 HighCol = (unsigned char *)VRAM_CACHED_STUFF + 8;
267 if (!(Pico.video.reg[1]&8)) HighCol += 8*512;
274 static int EmuScanSlow(unsigned int num, void *sdata)
276 if (!(Pico.video.reg[1]&8)) num += 8;
278 if (Pico.m.dirtyPal) {
279 if (!dynamic_palette) {
280 do_slowmode_lines(num);
286 if (dynamic_palette) {
287 int line_len = (Pico.video.reg[12]&1) ? 320 : 256;
288 void *dst = (char *)VRAM_STUFF + 512*240 + 512*2*num;
289 amips_clut(dst, HighCol + 8, localPal, line_len);
291 HighCol = (unsigned char *)VRAM_CACHED_STUFF + (num+1)*512 + 8;
296 static void blitscreen_clut(void)
298 int offs = fbimg_offs;
299 offs += (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
301 sceGuSync(0,0); // sync with prev
302 sceGuStart(GU_DIRECT, guCmdList);
303 sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
307 if (!blit_16bit_mode) {
308 sceGuTexMode(GU_PSM_5650, 0, 0, 0);
309 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 512*240);
316 if (blit_16bit_mode) {
317 sceGuClutMode(GU_PSM_5650,0,0xff,0);
318 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
319 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
323 if ((PicoOpt&0x10) && Pico.m.dirtyPal)
326 sceKernelDcacheWritebackAll();
328 if (need_pal_upload) {
330 sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
335 if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
337 struct Vertex* vertices;
340 #define SLICE_WIDTH 32
341 for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
344 vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
345 memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
346 vertices[0].u = vertices[0].x = x;
347 vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
348 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
350 // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
354 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
360 static void cd_leds(void)
362 unsigned int reg, col_g, col_r, *p;
364 reg = Pico_mcd->s68k_regs[0];
366 p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
367 col_g = (reg & 2) ? 0x06000600 : 0;
368 col_r = (reg & 1) ? 0x00180018 : 0;
369 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
370 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
371 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
375 static void dbg_text(void)
380 sprintf(text, "sl: %i, 16b: %i", g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x, blit_16bit_mode);
381 len = strlen(text) * 8 / 2;
382 for (h = 0; h < 8; h++) {
383 p = (int *) ((unsigned short *) psp_screen+2+512*(256+h));
384 p = (int *) ((int)p & ~3); // align
387 emu_textOut16(2, 256, text);
391 /* called after rendering is done, but frame emulation is not finished */
398 // clear top and bottom trash
399 for (pd = PicoDraw2FB+8, i = 8; i > 0; i--, pd += 512)
400 memset32((int *)pd, 0xe0e0e0e0, 320/4);
401 for (pd = PicoDraw2FB+512*232+8, i = 8; i > 0; i--, pd += 512)
402 memset32((int *)pd, 0xe0e0e0e0, 320/4);
409 static void blit2(const char *fps, const char *notice, int lagging_behind)
411 int vsync = 0, emu_opt = currentConfig.EmuOpt;
413 if (notice || (emu_opt & 2)) {
414 if (notice) osd_text(4, notice, 0);
415 if (emu_opt & 2) osd_text(OSD_FPS_X, fps, 0);
420 if ((emu_opt & 0x400) && (PicoMCD & 1))
423 if (currentConfig.EmuOpt & 0x2000) { // want vsync
424 if (!(currentConfig.EmuOpt & 0x10000) || !lagging_behind) vsync = 1;
427 psp_video_flip(vsync);
430 // clears whole screen or just the notice area (in all buffers)
431 static void clearArea(int full)
434 memset32(psp_screen, 0, 512*272*2/4);
436 memset32(psp_screen, 0, 512*272*2/4);
437 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*240/4);
438 memset32((int *)VRAM_CACHED_STUFF+512*240/4, 0, 512*240*2/4);
440 void *fb = psp_video_get_active_fb();
441 memset32((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
442 memset32((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
446 static void vidResetMode(void)
449 sceGuSync(0,0); // sync with prev
450 sceGuStart(GU_DIRECT, guCmdList);
452 sceGuClutMode(GU_PSM_5650,0,0xff,0);
453 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
454 sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
455 if (currentConfig.scaling)
456 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
457 else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
458 sceGuTexScale(1.0f,1.0f);
459 sceGuTexOffset(0.0f,0.0f);
461 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
464 PicoDrawSetColorFormat(-1);
465 PicoScan = EmuScanSlow;
469 blit_16bit_mode = dynamic_palette = 0;
472 set_scaling_params();
478 #define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
479 #define SOUND_BLOCK_SIZE_PAL (1764*2)
480 #define SOUND_BLOCK_COUNT 4
482 static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
483 static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
484 static int samples_made = 0, samples_done = 0, samples_block = 0;
485 static int sound_thread_exit = 0;
486 static SceUID sound_sem = -1;
488 static void writeSound(int len);
490 static int sound_thread(SceSize args, void *argp)
494 lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
496 while (!sound_thread_exit)
498 if (samples_made - samples_done < samples_block) {
499 // wait for data (use at least 2 blocks)
500 //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
501 while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
502 ret = sceKernelWaitSema(sound_sem, 1, 0);
503 //lprintf("sthr: sceKernelWaitSema: %i\n", ret);
507 //lprintf("sthr: got data: %i\n", samples_made - samples_done);
509 ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
511 samples_done += samples_block;
512 snd_playptr += samples_block;
513 if (snd_playptr >= sndBuffer_endptr)
514 snd_playptr = sndBuffer;
516 lprintf("sthr: outf: %i; pos %i/%i\n", ret, samples_done, samples_made);
518 // shouln't happen, but just in case
519 if (samples_made - samples_done >= samples_block*3) {
520 //lprintf("block skip (%i)\n", samples_made - samples_done);
521 samples_done += samples_block; // skip
522 snd_playptr += samples_block;
527 lprintf("sthr: exit\n");
528 sceKernelExitDeleteThread(0);
532 static void sound_init(void)
536 sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
537 if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
539 samples_made = samples_done = 0;
540 samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
541 sound_thread_exit = 0;
542 thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
545 sceKernelStartThread(thid, 0, 0);
548 lprintf("sceKernelCreateThread failed: %i\n", thid);
551 static void sound_prepare(void)
553 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
556 samples_made = samples_done = 0;
558 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
559 PsndRerate(Pico.m.frame_count ? 1 : 0);
561 stereo=(PicoOpt&8)>>3;
563 samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
564 if (PsndRate <= 22050) samples_block /= 2;
565 sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
567 lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
568 PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
570 while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
572 ret = sceAudio_38553111(samples_block/2, PsndRate, 2); // seems to not need that stupid 64byte alignment
574 lprintf("sceAudio_38553111() failed: %i\n", ret);
575 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
576 noticeMsgTime = sceKernelGetSystemTimeLow();
577 currentConfig.EmuOpt &= ~4;
579 PicoWriteSound = writeSound;
580 memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
581 snd_playptr = sndBuffer_endptr - samples_block;
582 samples_made = samples_block; // send 1 empty block first..
584 PsndRate_old = PsndRate;
585 PicoOpt_old = PicoOpt;
586 pal_old = Pico.m.pal;
590 static void sound_end(void)
592 samples_made = samples_done = 0;
593 while (sceAudioOutput2GetRestSample() > 0)
598 static void sound_deinit(void)
600 sound_thread_exit = 1;
601 sceKernelSignalSema(sound_sem, 1);
602 sceKernelDeleteSema(sound_sem);
606 static void writeSound(int len)
609 if (PicoOpt&8) len<<=1;
612 /*if (PsndOut > sndBuffer_endptr) {
613 memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
614 PsndOut = &sndBuffer[PsndOut - endptr];
618 if (PsndOut >= sndBuffer_endptr)
621 // signal the snd thread
623 if (samples_made - samples_done > samples_block*2) {
624 // lprintf("signal, %i/%i\n", samples_done, samples_made);
625 ret = sceKernelSignalSema(sound_sem, 1);
626 // lprintf("signal ret %i\n", ret);
631 static void SkipFrame(void)
638 void emu_forcedFrame(void)
640 int po_old = PicoOpt;
641 int eo_old = currentConfig.EmuOpt;
644 PicoOpt |= 0x4080; // soft_scale | acc_sprites
645 currentConfig.EmuOpt |= 0x80;
648 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*8/4); // borders
649 memset32((int *)VRAM_CACHED_STUFF + 512*232/4, 0xe0e0e0e0, 512*8/4);
650 memset32((int *)psp_screen + 512*264*2/4, 0, 512*8*2/4);
652 PicoDrawSetColorFormat(-1);
653 PicoScan = EmuScanSlow;
660 currentConfig.EmuOpt = eo_old;
664 static void RunEvents(unsigned int which)
666 if (which & 0x1800) // save or load (but not both)
670 if ( emu_checkSaveFile(state_slot) &&
671 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
672 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
676 blit2("", (which & 0x1000) ? "LOAD STATE? (X=yes, O=no)" : "OVERWRITE SAVE? (X=yes, O=no)", 0);
677 while( !((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) )
679 if (keys & BTN_CIRCLE) do_it = 0;
680 while( ((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) ) // wait for release
687 osd_text(4, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME", 1);
688 PicoStateProgressCB = emu_msg_cb;
689 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
690 PicoStateProgressCB = NULL;
696 if (which & 0x0400) // switch renderer
698 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
699 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
704 strcpy(noticeMsg, "fast renderer");
705 else if (currentConfig.EmuOpt&0x80)
706 strcpy(noticeMsg, "accurate renderer");
708 noticeMsgTime = sceKernelGetSystemTimeLow();
714 if(state_slot < 0) state_slot = 9;
717 if(state_slot > 9) state_slot = 0;
719 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
720 noticeMsgTime = sceKernelGetSystemTimeLow();
724 static void updateKeys(void)
726 unsigned int keys, allActions[2] = { 0, 0 }, events;
727 static unsigned int prevEvents = 0;
730 keys = psp_pad_read(0);
731 if (keys & PSP_CTRL_HOME)
732 sceDisplayWaitVblankStart();
734 if (keys & BTN_SELECT)
735 engineState = PGS_Menu;
737 keys &= CONFIGURABLE_KEYS;
739 for (i = 0; i < 32; i++)
743 int pl, acts = currentConfig.KeyBinds[i];
745 pl = (acts >> 16) & 1;
746 if (combo_keys & (1 << i))
748 int u = i+1, acts_c = acts & combo_acts;
749 // let's try to find the other one
752 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
753 allActions[pl] |= acts_c;
754 keys &= ~((1 << i) | (1 << u));
757 // add non-combo actions if combo ones were not found
758 if (!acts_c || u == 32)
759 allActions[pl] |= acts & ~combo_acts;
761 allActions[pl] |= acts;
766 PicoPad[0] = (unsigned short) allActions[0];
767 PicoPad[1] = (unsigned short) allActions[1];
769 events = (allActions[0] | allActions[1]) >> 16;
771 // volume is treated in special way and triggered every frame
772 if ((events & 0x6000) && PsndOut != NULL)
774 int vol = currentConfig.volume;
775 if (events & 0x2000) {
776 if (vol < 100) vol++;
780 // FrameworkAudio_SetVolume(vol, vol); // TODO
781 sprintf(noticeMsg, "VOL: %02i ", vol);
782 noticeMsgTime = sceKernelGetSystemTimeLow();
783 currentConfig.volume = vol;
786 events &= ~prevEvents;
787 if (events) RunEvents(events);
788 if (movie_data) emu_updateMovie();
790 prevEvents = (allActions[0] | allActions[1]) >> 16;
793 static void find_combos(void)
797 // find out which keys and actions are combos
798 combo_keys = combo_acts = 0;
799 for (act = 0; act < 32; act++)
801 int keyc = 0, keyc2 = 0;
802 if (act == 16 || act == 17) continue; // player2 flag
805 for (u = 0; u < 28; u++) // 28 because nub can't produce combos
806 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
810 for (u = 0; u < 28; u++)
811 if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1
812 (currentConfig.KeyBinds[u] & (1 << act))) keyc++;
813 for (u = 0; u < 28; u++)
814 if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2
815 (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;
817 if (keyc > 1 || keyc2 > 1)
819 // loop again and mark those keys and actions as combo
820 for (u = 0; u < 28; u++)
822 if (currentConfig.KeyBinds[u] & (1 << act)) {
823 combo_keys |= 1 << u;
824 combo_acts |= 1 << act;
832 static void simpleWait(unsigned int until)
837 tval = sceKernelGetSystemTimeLow();
838 diff = (int)until - (int)tval;
839 if (diff >= 512 && diff < 100*1024)
840 sceKernelDelayThread(diff);
845 char fpsbuff[24]; // fps count c string
846 unsigned int tval, tval_prev = 0, tval_thissec = 0; // timing
847 int frames_done = 0, frames_shown = 0, oldmodes = 0;
848 int target_fps, target_frametime, lim_time, tval_diff, i;
851 lprintf("entered emu_Loop()\n");
855 if (currentConfig.CPUclock != psp_get_cpu_clock()) {
856 lprintf("setting cpu clock to %iMHz... ", currentConfig.CPUclock);
857 i = psp_set_cpu_clock(currentConfig.CPUclock);
858 lprintf(i ? "failed\n" : "done\n");
859 currentConfig.CPUclock = psp_get_cpu_clock();
862 // make sure we are in correct mode
866 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
869 // pal/ntsc might have changed, reset related stuff
870 target_fps = Pico.m.pal ? 50 : 60;
871 target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
875 if (PicoMCD & 1) PicoCDBufferInit();
877 // prepare sound stuff
879 if (currentConfig.EmuOpt & 4)
884 sceDisplayWaitVblankStart();
887 while (engineState == PGS_Running)
891 tval = sceKernelGetSystemTimeLow();
892 if (reset_timing || tval < tval_prev) {
893 //stdbg("timing reset");
896 frames_shown = frames_done = 0;
899 // show notice message?
901 static int noticeMsgSum;
902 if (tval - noticeMsgTime > 2000000) { // > 2.0 sec
907 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
908 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
913 // check for mode changes
914 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
915 if (modes != oldmodes) {
918 set_scaling_params();
922 if (tval - tval_thissec >= 1000000)
925 if (currentConfig.Frameskip < 0 && frames_done < target_fps) {
926 SkipFrame(); frames_done++;
929 if (currentConfig.EmuOpt & 2)
930 sprintf(fpsbuff, "%02i/%02i ", frames_shown, frames_done);
932 tval_thissec += 1000000;
934 if (currentConfig.Frameskip < 0) {
935 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
936 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
937 if (frames_shown > frames_done) frames_shown = frames_done;
939 frames_done = frames_shown = 0;
943 sprintf(fpsbuff, "%i", Pico.m.frame_count);
947 lim_time = (frames_done+1) * target_frametime;
948 if (currentConfig.Frameskip >= 0) // frameskip enabled
950 for (i = 0; i < currentConfig.Frameskip; i++) {
952 SkipFrame(); frames_done++;
953 if (PsndOut) { // do framelimitting if sound is enabled
955 tval = sceKernelGetSystemTimeLow();
956 tval_diff = (int)(tval - tval_thissec) << 8;
957 if (tval_diff < lim_time) // we are too fast
958 simpleWait(tval + ((lim_time - tval_diff)>>8));
960 lim_time += target_frametime;
963 else // auto frameskip
966 tval = sceKernelGetSystemTimeLow();
967 tval_diff = (int)(tval - tval_thissec) << 8;
968 if (tval_diff > lim_time && (frames_done/16 < frames_shown))
970 // no time left for this frame - skip
971 if (tval_diff - lim_time >= (300000<<8)) {
976 SkipFrame(); frames_done++;
991 tval = sceKernelGetSystemTimeLow();
992 tval_diff = (int)(tval - tval_thissec) << 8;
994 blit2(fpsbuff, notice, tval_diff > lim_time);
996 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300000<<8)) // slowdown detection
998 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
1000 // sleep if we are still too fast
1001 if (tval_diff < lim_time)
1004 simpleWait(tval + ((lim_time - tval_diff) >> 8));
1008 frames_done++; frames_shown++;
1012 if (PicoMCD & 1) PicoCDBufferFree();
1014 if (PsndOut != NULL) {
1020 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
1021 emu_msg_cb("Writing SRAM/BRAM..");
1022 emu_SaveLoadGame(0, 1);
1026 // clear fps counters and stuff
1027 memset32((int *)psp_video_get_active_fb() + 512*264*2/4, 0, 512*8*2/4);
1031 void emu_ResetGame(void)