1 // (c) Copyright 2007 notaz, All rights reserved.
2 // Free for non-commercial use.
4 // For commercial use, separate licencing terms must be obtained.
8 #include <sys/syslimits.h> // PATH_MAX
10 #include <pspthreadman.h>
11 #include <pspdisplay.h>
20 #include "../common/emu.h"
21 #include "../common/lprintf.h"
22 #include "../../Pico/PicoInt.h"
26 // additional pspaudio imports, credits to crazyc
27 int sceAudio_38553111(unsigned short samples, unsigned short freq, char unknown); // play with conversion?
28 int sceAudio_5C37C0AE(void); // end play?
29 int sceAudio_E0727056(int volume, void *buffer); // blocking output
30 int sceAudioOutput2GetRestSample();
33 char romFileName[PATH_MAX];
34 unsigned char *PicoDraw2FB = (unsigned char *)VRAM_CACHED_STUFF + 8; // +8 to be able to skip border with 1 quadword..
37 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
38 static unsigned int noticeMsgTime = 0;
39 int reset_timing = 0; // do we need this?
42 static void sound_init(void);
43 static void sound_deinit(void);
44 static void blit2(const char *fps, const char *notice, int lagging_behind);
45 static void clearArea(int full);
47 void emu_noticeMsgUpdated(void)
49 noticeMsgTime = sceKernelGetSystemTimeLow();
52 void emu_getMainDir(char *dst, int len)
54 if (len > 0) *dst = 0;
57 static void osd_text(int x, const char *text, int is_active, int clear_all)
59 unsigned short *screen = is_active ? psp_video_get_active_fb() : psp_screen;
60 int len = clear_all ? (480 / 2) : (strlen(text) * 8 / 2);
63 for (h = 0; h < 8; h++) {
64 p = (int *) (screen+x+512*(264+h));
65 p = (int *) ((int)p & ~3); // align
66 memset32_uncached(p, 0, len);
68 if (is_active) { tmp = psp_screen; psp_screen = screen; } // nasty pointer tricks
69 emu_textOut16(x, 264, text);
70 if (is_active) psp_screen = tmp;
73 void emu_msg_cb(const char *msg)
75 osd_text(4, msg, 1, 1);
76 noticeMsgTime = sceKernelGetSystemTimeLow() - 2000000;
78 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
82 static void emu_msg_tray_open(void)
84 strcpy(noticeMsg, "CD tray opened");
85 noticeMsgTime = sceKernelGetSystemTimeLow();
91 // make dirs for saves, cfgs, etc.
100 PicoMessage = emu_msg_cb;
101 PicoMCDopenTray = emu_msg_tray_open;
102 PicoMCDcloseTray = menu_loop_tray;
105 void emu_Deinit(void)
108 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
109 emu_SaveLoadGame(0, 1);
113 if (!(currentConfig.EmuOpt & 0x20)) {
114 FILE *f = fopen(PicoConfigFile, "r+b");
115 if (!f) emu_WriteConfig(0);
117 // if we already have config, reload it, except last ROM
118 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
119 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
120 fseek(f, 0, SEEK_SET);
121 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
131 void emu_setDefaultConfig(void)
133 memset(¤tConfig, 0, sizeof(currentConfig));
134 currentConfig.lastRomFile[0] = 0;
135 currentConfig.EmuOpt = 0x1d | 0x680; // | confirm_save, cd_leds, acc rend
136 currentConfig.PicoOpt = 0x0f | 0x1c00; // | gfx_cd, cd_pcm, cd_cdda
137 currentConfig.PsndRate = 22050;
138 currentConfig.PicoRegion = 0; // auto
139 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
140 currentConfig.Frameskip = -1; // auto
141 currentConfig.volume = 50;
142 currentConfig.CPUclock = 333;
143 currentConfig.KeyBinds[ 4] = 1<<0; // SACB RLDU
144 currentConfig.KeyBinds[ 6] = 1<<1;
145 currentConfig.KeyBinds[ 7] = 1<<2;
146 currentConfig.KeyBinds[ 5] = 1<<3;
147 currentConfig.KeyBinds[14] = 1<<4;
148 currentConfig.KeyBinds[13] = 1<<5;
149 currentConfig.KeyBinds[15] = 1<<6;
150 currentConfig.KeyBinds[ 3] = 1<<7;
151 currentConfig.KeyBinds[12] = 1<<26; // switch rnd
152 currentConfig.KeyBinds[ 8] = 1<<27; // save state
153 currentConfig.KeyBinds[ 9] = 1<<28; // load state
154 currentConfig.KeyBinds[28] = 1<<0; // num "buttons"
155 currentConfig.KeyBinds[30] = 1<<1;
156 currentConfig.KeyBinds[31] = 1<<2;
157 currentConfig.KeyBinds[29] = 1<<3;
158 currentConfig.PicoCDBuffers = 64;
159 currentConfig.scaling = 1; // bilinear filtering for psp
160 currentConfig.scale = 1.20; // fullscreen
161 currentConfig.hscale40 = 1.25;
162 currentConfig.hscale32 = 1.56;
166 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
174 static struct Vertex __attribute__((aligned(4))) g_vertices[2];
175 static unsigned short __attribute__((aligned(16))) localPal[0x100];
176 static int dynamic_palette = 0, need_pal_upload = 0, blit_16bit_mode = 0;
177 static int fbimg_offs = 0;
179 static void set_scaling_params(void)
181 int src_width, fbimg_width, fbimg_height, fbimg_xoffs, fbimg_yoffs, border_hack = 0;
182 g_vertices[0].x = g_vertices[0].y =
183 g_vertices[0].z = g_vertices[1].z = 0;
185 fbimg_height = (int)(240.0 * currentConfig.scale + 0.5);
186 if (Pico.video.reg[12] & 1) {
187 fbimg_width = (int)(320.0 * currentConfig.scale * currentConfig.hscale40 + 0.5);
190 fbimg_width = (int)(256.0 * currentConfig.scale * currentConfig.hscale32 + 0.5);
194 if (fbimg_width & 1) fbimg_width++; // make even
195 if (fbimg_height & 1) fbimg_height++;
197 if (fbimg_width >= 480) {
198 g_vertices[0].u = (fbimg_width-480)/2;
199 g_vertices[1].u = src_width - (fbimg_width-480)/2 - 1;
200 if (fbimg_width == 480) border_hack = 1;
205 g_vertices[1].u = src_width;
206 fbimg_xoffs = 240 - fbimg_width/2;
209 if (fbimg_height >= 272) {
210 g_vertices[0].v = (fbimg_height-272)/2;
211 g_vertices[1].v = 240 - (fbimg_height-272)/2;
216 g_vertices[1].v = 240;
217 fbimg_yoffs = 136 - fbimg_height/2;
220 g_vertices[1].x = fbimg_width;
221 g_vertices[1].y = fbimg_height;
222 if (fbimg_xoffs < 0) fbimg_xoffs = 0;
223 if (fbimg_yoffs < 0) fbimg_yoffs = 0;
230 fbimg_offs = (fbimg_yoffs*512 + fbimg_xoffs) * 2; // dst is always 16bit
233 lprintf("set_scaling_params:\n");
234 lprintf("offs: %i, %i\n", fbimg_xoffs, fbimg_yoffs);
235 lprintf("xy0, xy1: %i, %i; %i, %i\n", g_vertices[0].x, g_vertices[0].y, g_vertices[1].x, g_vertices[1].y);
236 lprintf("uv0, uv1: %i, %i; %i, %i\n", g_vertices[0].u, g_vertices[0].v, g_vertices[1].u, g_vertices[1].v);
240 static void do_pal_update(int allow_sh)
242 unsigned int *spal=(void *)Pico.cram;
243 unsigned int *dpal=(void *)localPal;
246 for (i = 0x3f/2; i >= 0; i--)
247 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
249 if (allow_sh && (Pico.video.reg[0xC]&8)) // shadow/hilight?
252 for (i = 0x3f/2; i >= 0; i--)
253 dpal[0x20|i] = dpal[0x60|i] = (dpal[i]>>1)&0x738e738e;
255 for (i = 0x3f; i >= 0; i--) {
256 int t=localPal[i]&0xe71c;t+=0x4208;
258 if (t&0x800) t|=0x700;
259 if (t&0x10000) t|=0xe000;
261 localPal[0x80|i]=(unsigned short)t;
269 static void do_slowmode_lines(int line_to)
271 int line = 0, line_len = (Pico.video.reg[12]&1) ? 320 : 256;
272 unsigned short *dst = (unsigned short *)VRAM_STUFF + 512*240/2;
273 unsigned char *src = (unsigned char *)VRAM_CACHED_STUFF + 16;
274 if (!(Pico.video.reg[1]&8)) { line = 8; dst += 512*8; src += 512*8; }
276 for (; line < line_to; line++, dst+=512, src+=512)
277 amips_clut(dst, src, localPal, line_len);
280 static void EmuScanPrepare(void)
282 HighCol = (unsigned char *)VRAM_CACHED_STUFF + 8;
283 if (!(Pico.video.reg[1]&8)) HighCol += 8*512;
290 static int EmuScanSlow(unsigned int num, void *sdata)
292 if (!(Pico.video.reg[1]&8)) num += 8;
294 if (Pico.m.dirtyPal) {
295 if (!dynamic_palette) {
296 do_slowmode_lines(num);
302 if (dynamic_palette) {
303 int line_len = (Pico.video.reg[12]&1) ? 320 : 256;
304 void *dst = (char *)VRAM_STUFF + 512*240 + 512*2*num;
305 amips_clut(dst, HighCol + 8, localPal, line_len);
307 HighCol = (unsigned char *)VRAM_CACHED_STUFF + (num+1)*512 + 8;
312 static void blitscreen_clut(void)
314 int offs = fbimg_offs;
315 offs += (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
317 sceGuSync(0,0); // sync with prev
318 sceGuStart(GU_DIRECT, guCmdList);
319 sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
323 if (!blit_16bit_mode) {
324 sceGuTexMode(GU_PSM_5650, 0, 0, 0);
325 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 512*240);
332 if (blit_16bit_mode) {
333 sceGuClutMode(GU_PSM_5650,0,0xff,0);
334 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
335 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
339 if ((PicoOpt&0x10) && Pico.m.dirtyPal)
342 sceKernelDcacheWritebackAll();
344 if (need_pal_upload) {
346 sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
351 if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
353 struct Vertex* vertices;
356 #define SLICE_WIDTH 32
357 for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
360 vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
361 memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
362 vertices[0].u = vertices[0].x = x;
363 vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
364 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
366 // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
370 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
376 static void cd_leds(void)
378 unsigned int reg, col_g, col_r, *p;
380 reg = Pico_mcd->s68k_regs[0];
382 p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
383 col_g = (reg & 2) ? 0x06000600 : 0;
384 col_r = (reg & 1) ? 0x00180018 : 0;
385 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
386 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
387 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
391 static void dbg_text(void)
396 sprintf(text, "sl: %i, 16b: %i", g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x, blit_16bit_mode);
397 len = strlen(text) * 8 / 2;
398 for (h = 0; h < 8; h++) {
399 p = (int *) ((unsigned short *) psp_screen+2+512*(256+h));
400 p = (int *) ((int)p & ~3); // align
401 memset32_uncached(p, 0, len);
403 emu_textOut16(2, 256, text);
407 /* called after rendering is done, but frame emulation is not finished */
414 // clear top and bottom trash
415 for (pd = PicoDraw2FB+8, i = 8; i > 0; i--, pd += 512)
416 memset32((int *)pd, 0xe0e0e0e0, 320/4);
417 for (pd = PicoDraw2FB+512*232+8, i = 8; i > 0; i--, pd += 512)
418 memset32((int *)pd, 0xe0e0e0e0, 320/4);
425 static void blit2(const char *fps, const char *notice, int lagging_behind)
427 int vsync = 0, emu_opt = currentConfig.EmuOpt;
429 if (notice || (emu_opt & 2)) {
430 if (notice) osd_text(4, notice, 0, 0);
431 if (emu_opt & 2) osd_text(OSD_FPS_X, fps, 0, 0);
436 if ((emu_opt & 0x400) && (PicoMCD & 1))
439 if (currentConfig.EmuOpt & 0x2000) { // want vsync
440 if (!(currentConfig.EmuOpt & 0x10000) || !lagging_behind) vsync = 1;
443 psp_video_flip(vsync);
446 // clears whole screen or just the notice area (in all buffers)
447 static void clearArea(int full)
450 memset32_uncached(psp_screen, 0, 512*272*2/4);
452 memset32_uncached(psp_screen, 0, 512*272*2/4);
453 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*240/4);
454 memset32((int *)VRAM_CACHED_STUFF+512*240/4, 0, 512*240*2/4);
456 void *fb = psp_video_get_active_fb();
457 memset32_uncached((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
458 memset32_uncached((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
462 static void vidResetMode(void)
465 sceGuSync(0,0); // sync with prev
466 sceGuStart(GU_DIRECT, guCmdList);
468 sceGuClutMode(GU_PSM_5650,0,0xff,0);
469 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
470 sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
471 if (currentConfig.scaling)
472 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
473 else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
474 sceGuTexScale(1.0f,1.0f);
475 sceGuTexOffset(0.0f,0.0f);
477 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
480 PicoDrawSetColorFormat(-1);
481 PicoScan = EmuScanSlow;
485 blit_16bit_mode = dynamic_palette = 0;
488 set_scaling_params();
494 #define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
495 #define SOUND_BLOCK_SIZE_PAL (1764*2)
496 #define SOUND_BLOCK_COUNT 8
498 static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
499 static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
500 static int samples_made = 0, samples_done = 0, samples_block = 0;
501 static int sound_thread_exit = 0;
502 static SceUID sound_sem = -1;
504 static void writeSound(int len);
506 static int sound_thread(SceSize args, void *argp)
510 lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
512 while (!sound_thread_exit)
514 if (samples_made - samples_done < samples_block) {
515 // wait for data (use at least 2 blocks)
516 //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
517 while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
518 ret = sceKernelWaitSema(sound_sem, 1, 0);
519 if (ret < 0) lprintf("sthr: sceKernelWaitSema: %i\n", ret);
523 // lprintf("sthr: got data: %i\n", samples_made - samples_done);
525 ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
527 samples_done += samples_block;
528 snd_playptr += samples_block;
529 if (snd_playptr >= sndBuffer_endptr)
530 snd_playptr = sndBuffer;
531 // 1.5 kernel returns 0, newer ones return # of samples queued
533 lprintf("sthr: sceAudio_E0727056: %08x; pos %i/%i\n", ret, samples_done, samples_made);
535 // shouln't happen, but just in case
536 if (samples_made - samples_done >= samples_block*3) {
537 lprintf("sthr: block skip (%i)\n", samples_made - samples_done);
538 samples_done += samples_block; // skip
539 snd_playptr += samples_block;
544 lprintf("sthr: exit\n");
545 sceKernelExitDeleteThread(0);
549 static void sound_init(void)
554 sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
555 if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
557 samples_made = samples_done = 0;
558 samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
559 sound_thread_exit = 0;
560 thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
563 ret = sceKernelStartThread(thid, 0, 0);
564 if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
567 lprintf("sceKernelCreateThread failed: %i\n", thid);
570 static void sound_prepare(void)
572 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
575 samples_made = samples_done = 0;
577 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
578 PsndRerate(Pico.m.frame_count ? 1 : 0);
580 stereo=(PicoOpt&8)>>3;
582 samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
583 if (PsndRate <= 22050) samples_block /= 2;
584 sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
586 lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
587 PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
589 // while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
590 // sceAudio_5C37C0AE();
591 ret = sceAudio_38553111(samples_block/2, PsndRate, 2); // seems to not need that stupid 64byte alignment
593 lprintf("sceAudio_38553111() failed: %i\n", ret);
594 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
595 noticeMsgTime = sceKernelGetSystemTimeLow();
596 currentConfig.EmuOpt &= ~4;
598 PicoWriteSound = writeSound;
599 memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
600 snd_playptr = sndBuffer_endptr - samples_block;
601 samples_made = samples_block; // send 1 empty block first..
603 PsndRate_old = PsndRate;
604 PicoOpt_old = PicoOpt;
605 pal_old = Pico.m.pal;
609 static void sound_end(void)
612 if (samples_done == 0)
614 // if no data is written between sceAudio_38553111 and sceAudio_5C37C0AE calls,
615 // we get a deadlock on next sceAudio_38553111 call
616 // so this is yet another workaround:
617 memset32((int *)(void *)sndBuffer, 0, samples_block*4/4);
618 samples_made = samples_block * 3;
619 sceKernelSignalSema(sound_sem, 1);
621 sceKernelDelayThread(100*1000);
622 samples_made = samples_done = 0;
623 for (i = 0; sceAudioOutput2GetRestSample() > 0 && i < 16; i++)
628 static void sound_deinit(void)
630 sound_thread_exit = 1;
631 sceKernelSignalSema(sound_sem, 1);
632 sceKernelDeleteSema(sound_sem);
636 static void writeSound(int len)
639 if (PicoOpt&8) len<<=1;
642 /*if (PsndOut > sndBuffer_endptr) {
643 memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
644 PsndOut = &sndBuffer[PsndOut - endptr];
648 if (PsndOut > sndBuffer_endptr) lprintf("snd oflow %i!\n", PsndOut - sndBuffer_endptr);
649 if (PsndOut >= sndBuffer_endptr)
652 // signal the snd thread
654 if (samples_made - samples_done > samples_block*2) {
655 // lprintf("signal, %i/%i\n", samples_done, samples_made);
656 ret = sceKernelSignalSema(sound_sem, 1);
657 //if (ret < 0) lprintf("snd signal ret %08x\n", ret);
662 static void SkipFrame(void)
669 void emu_forcedFrame(void)
671 int po_old = PicoOpt;
672 int eo_old = currentConfig.EmuOpt;
675 PicoOpt |= 0x4080; // soft_scale | acc_sprites
676 currentConfig.EmuOpt |= 0x80;
679 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*8/4); // borders
680 memset32((int *)VRAM_CACHED_STUFF + 512*232/4, 0xe0e0e0e0, 512*8/4);
681 memset32_uncached((int *)psp_screen + 512*264*2/4, 0, 512*8*2/4);
683 PicoDrawSetColorFormat(-1);
684 PicoScan = EmuScanSlow;
691 currentConfig.EmuOpt = eo_old;
695 static void RunEvents(unsigned int which)
697 if (which & 0x1800) // save or load (but not both)
701 if ( emu_checkSaveFile(state_slot) &&
702 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
703 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
707 blit2("", (which & 0x1000) ? "LOAD STATE? (X=yes, O=no)" : "OVERWRITE SAVE? (X=yes, O=no)", 0);
708 while( !((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) )
710 if (keys & BTN_CIRCLE) do_it = 0;
711 while( ((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) ) // wait for release
718 osd_text(4, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME", 1, 0);
719 PicoStateProgressCB = emu_msg_cb;
720 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
721 PicoStateProgressCB = NULL;
727 if (which & 0x0400) // switch renderer
729 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
730 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
735 strcpy(noticeMsg, "fast renderer");
736 else if (currentConfig.EmuOpt&0x80)
737 strcpy(noticeMsg, "accurate renderer");
739 noticeMsgTime = sceKernelGetSystemTimeLow();
745 if(state_slot < 0) state_slot = 9;
748 if(state_slot > 9) state_slot = 0;
750 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
751 noticeMsgTime = sceKernelGetSystemTimeLow();
755 static void updateKeys(void)
757 unsigned int keys, allActions[2] = { 0, 0 }, events;
758 static unsigned int prevEvents = 0;
761 keys = psp_pad_read(0);
762 if (keys & PSP_CTRL_HOME)
763 sceDisplayWaitVblankStart();
765 if (keys & BTN_SELECT)
766 engineState = PGS_Menu;
768 keys &= CONFIGURABLE_KEYS;
770 for (i = 0; i < 32; i++)
774 int pl, acts = currentConfig.KeyBinds[i];
776 pl = (acts >> 16) & 1;
777 if (combo_keys & (1 << i))
779 int u = i+1, acts_c = acts & combo_acts;
780 // let's try to find the other one
783 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
784 allActions[pl] |= acts_c;
785 keys &= ~((1 << i) | (1 << u));
788 // add non-combo actions if combo ones were not found
789 if (!acts_c || u == 32)
790 allActions[pl] |= acts & ~combo_acts;
792 allActions[pl] |= acts;
797 PicoPad[0] = (unsigned short) allActions[0];
798 PicoPad[1] = (unsigned short) allActions[1];
800 events = (allActions[0] | allActions[1]) >> 16;
802 // volume is treated in special way and triggered every frame
803 if ((events & 0x6000) && PsndOut != NULL)
805 int vol = currentConfig.volume;
806 if (events & 0x2000) {
807 if (vol < 100) vol++;
811 // FrameworkAudio_SetVolume(vol, vol); // TODO
812 sprintf(noticeMsg, "VOL: %02i ", vol);
813 noticeMsgTime = sceKernelGetSystemTimeLow();
814 currentConfig.volume = vol;
817 events &= ~prevEvents;
818 if (events) RunEvents(events);
819 if (movie_data) emu_updateMovie();
821 prevEvents = (allActions[0] | allActions[1]) >> 16;
824 static void find_combos(void)
828 // find out which keys and actions are combos
829 combo_keys = combo_acts = 0;
830 for (act = 0; act < 32; act++)
832 int keyc = 0, keyc2 = 0;
833 if (act == 16 || act == 17) continue; // player2 flag
836 for (u = 0; u < 28; u++) // 28 because nub can't produce combos
837 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
841 for (u = 0; u < 28; u++)
842 if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1
843 (currentConfig.KeyBinds[u] & (1 << act))) keyc++;
844 for (u = 0; u < 28; u++)
845 if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2
846 (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;
848 if (keyc > 1 || keyc2 > 1)
850 // loop again and mark those keys and actions as combo
851 for (u = 0; u < 28; u++)
853 if (currentConfig.KeyBinds[u] & (1 << act)) {
854 combo_keys |= 1 << u;
855 combo_acts |= 1 << act;
863 static void simpleWait(unsigned int until)
868 tval = sceKernelGetSystemTimeLow();
869 diff = (int)until - (int)tval;
870 if (diff >= 512 && diff < 100*1024)
871 sceKernelDelayThread(diff);
876 static int mp3_init_done = 0;
877 char fpsbuff[24]; // fps count c string
878 unsigned int tval, tval_prev = 0, tval_thissec = 0; // timing
879 int frames_done = 0, frames_shown = 0, oldmodes = 0;
880 int target_fps, target_frametime, lim_time, tval_diff, i;
883 lprintf("entered emu_Loop()\n");
887 if (currentConfig.CPUclock != psp_get_cpu_clock()) {
888 lprintf("setting cpu clock to %iMHz... ", currentConfig.CPUclock);
889 i = psp_set_cpu_clock(currentConfig.CPUclock);
890 lprintf(i ? "failed\n" : "done\n");
891 currentConfig.CPUclock = psp_get_cpu_clock();
894 // make sure we are in correct mode
898 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
901 // pal/ntsc might have changed, reset related stuff
902 target_fps = Pico.m.pal ? 50 : 60;
903 target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
910 if (!mp3_init_done) {
913 if (i) { engineState = PGS_Menu; return; }
917 // prepare sound stuff
919 if (currentConfig.EmuOpt & 4)
924 sceDisplayWaitVblankStart();
927 while (engineState == PGS_Running)
931 tval = sceKernelGetSystemTimeLow();
932 if (reset_timing || tval < tval_prev) {
933 //stdbg("timing reset");
936 frames_shown = frames_done = 0;
939 // show notice message?
941 static int noticeMsgSum;
942 if (tval - noticeMsgTime > 2000000) { // > 2.0 sec
947 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
948 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
953 // check for mode changes
954 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
955 if (modes != oldmodes) {
958 set_scaling_params();
962 if (tval - tval_thissec >= 1000000)
965 if (currentConfig.Frameskip < 0 && frames_done < target_fps) {
966 SkipFrame(); frames_done++;
969 if (currentConfig.EmuOpt & 2)
970 sprintf(fpsbuff, "%02i/%02i ", frames_shown, frames_done);
972 tval_thissec += 1000000;
974 if (currentConfig.Frameskip < 0) {
975 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
976 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
977 if (frames_shown > frames_done) frames_shown = frames_done;
979 frames_done = frames_shown = 0;
983 sprintf(fpsbuff, "%i", Pico.m.frame_count);
987 lim_time = (frames_done+1) * target_frametime;
988 if (currentConfig.Frameskip >= 0) // frameskip enabled
990 for (i = 0; i < currentConfig.Frameskip; i++) {
992 SkipFrame(); frames_done++;
993 if (PsndOut) { // do framelimitting if sound is enabled
995 tval = sceKernelGetSystemTimeLow();
996 tval_diff = (int)(tval - tval_thissec) << 8;
997 if (tval_diff < lim_time) // we are too fast
998 simpleWait(tval + ((lim_time - tval_diff)>>8));
1000 lim_time += target_frametime;
1003 else // auto frameskip
1006 tval = sceKernelGetSystemTimeLow();
1007 tval_diff = (int)(tval - tval_thissec) << 8;
1008 if (tval_diff > lim_time && (frames_done/16 < frames_shown))
1010 // no time left for this frame - skip
1011 if (tval_diff - lim_time >= (300000<<8)) {
1016 SkipFrame(); frames_done++;
1023 if (!(PicoOpt&0x10))
1031 tval = sceKernelGetSystemTimeLow();
1032 tval_diff = (int)(tval - tval_thissec) << 8;
1034 blit2(fpsbuff, notice, tval_diff > lim_time);
1036 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300000<<8)) // slowdown detection
1038 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
1040 // sleep if we are still too fast
1041 if (tval_diff < lim_time)
1044 simpleWait(tval + ((lim_time - tval_diff) >> 8));
1048 frames_done++; frames_shown++;
1052 if (PicoMCD & 1) PicoCDBufferFree();
1054 if (PsndOut != NULL) {
1060 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
1061 emu_msg_cb("Writing SRAM/BRAM..");
1062 emu_SaveLoadGame(0, 1);
1066 // clear fps counters and stuff
1067 memset32_uncached((int *)psp_video_get_active_fb() + 512*264*2/4, 0, 512*8*2/4);
1071 void emu_ResetGame(void)