#include <pspdisplay.h>
#include <psputils.h>
#include <pspgu.h>
+#include <pspaudio.h>
#include "psp.h"
#include "menu.h"
#define OSD_FPS_X 420
#endif
+// additional pspaudio imports, credits to crazyc
+int sceAudio_38553111(unsigned short samples, unsigned short freq, char unknown); // play with conversion?
+int sceAudio_5C37C0AE(void); // end play?
+int sceAudio_E0727056(int volume, void *buffer); // blocking output
+int sceAudioOutput2GetRestSample();
+
+
char romFileName[PATH_MAX];
unsigned char *PicoDraw2FB = (unsigned char *)VRAM_CACHED_STUFF + 8; // +8 to be able to skip border with 1 quadword..
int engineState;
int reset_timing = 0; // do we need this?
+static void sound_init(void);
+static void sound_deinit(void);
static void blit2(const char *fps, const char *notice);
static void clearArea(int full);
mkdir("brm", 0777);
mkdir("cfg", 0777);
+ sound_init();
+
PicoInit();
PicoMessage = emu_msg_cb;
PicoMCDopenTray = emu_msg_tray_open;
}
PicoExit();
+ sound_deinit();
}
void emu_setDefaultConfig(void)
sceGuSync(0,0);
}
-/*
-static void updateSound(int len)
+
+/* sound stuff */
+#define SOUND_DEF_BLOCK_SIZE 1024 // 1152 // 1024
+#define SOUND_BLOCK_COUNT 4
+
+static short __attribute__((aligned(4))) sndBuffer[SOUND_DEF_BLOCK_SIZE*SOUND_BLOCK_COUNT*2 + 44100/50*2];
+static short *snd_playptr = NULL;
+static int samples_made = 0, samples_done = 0, samples_block = SOUND_DEF_BLOCK_SIZE;
+static int sound_thread_exit = 0;
+static SceUID sound_sem = -1;
+
+static void writeSound(int len);
+
+static int sound_thread(SceSize args, void *argp)
{
+ short *endptr = &sndBuffer[SOUND_DEF_BLOCK_SIZE*SOUND_BLOCK_COUNT*2];
+ int ret;
+
+ lprintf("sound_thread: started, priority %i\n", sceKernelGetThreadCurrentPriority());
+
+ while (!sound_thread_exit)
+ {
+ if (samples_made - samples_done < samples_block) {
+ // wait for data...
+ //lprintf("sthr: wait... (%i/%i)\n", samples_done, samples_made);
+ ret = sceKernelWaitSema(sound_sem, 1, 0);
+ //lprintf("sthr: sceKernelWaitSema: %i\n", ret);
+ continue;
+ }
+
+ //lprintf("sthr: got data: %i\n", samples_made - samples_done);
+
+ ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
+
+ samples_done += samples_block;
+ snd_playptr += samples_block;
+ if (snd_playptr >= endptr)
+ snd_playptr = sndBuffer;
+ if (ret)
+ lprintf("sthr: outf: %i; pos %i/%i\n", ret, samples_done, samples_made);
+ }
+
+ lprintf("sthr: exit\n");
+ sceKernelExitDeleteThread(0);
+ return 0;
+}
+
+static void sound_init(void)
+{
+ SceUID thid;
+
+ sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
+ if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
+
+ sound_thread_exit = 0;
+ thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
+ if (thid >= 0)
+ {
+ sceKernelStartThread(thid, 0, 0);
+ }
+ else
+ lprintf("sceKernelCreateThread failed: %i\n", thid);
+}
+
+static void sound_prepare(void)
+{
+ static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
+ int ret, stereo;
+
+ samples_made = samples_done = 0;
+
+ if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
+ sound_rerate(Pico.m.frame_count ? 1 : 0);
+ }
+ stereo=(PicoOpt&8)>>3;
+ samples_block = SOUND_DEF_BLOCK_SIZE;
+ if (PsndRate < 44100) samples_block = SOUND_DEF_BLOCK_SIZE / 2;
+ if (PsndRate < 22050) samples_block = SOUND_DEF_BLOCK_SIZE / 4;
+
+ lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
+ PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
+
+ while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
+ sceAudio_5C37C0AE();
+ ret = sceAudio_38553111(samples_block/2, PsndRate, 2/*stereo ? 2 : 1*/);
+ lprintf("sceAudio_38553111() ret: %i\n", ret);
+ if (ret < 0) {
+ lprintf("sceAudio_38553111() failed: %i\n", ret);
+ sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
+ noticeMsgTime = sceKernelGetSystemTimeLow();
+ currentConfig.EmuOpt &= ~4;
+ } else {
+// int ret = sceAudioSetChannelDataLen(ret, PsndLen); // a try..
+// lprintf("sceAudioSetChannelDataLen: %i\n", ret);
+ PicoWriteSound = writeSound;
+ memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
+ snd_playptr = sndBuffer;
+ PsndOut = sndBuffer;
+ PsndRate_old = PsndRate;
+ PicoOpt_old = PicoOpt;
+ pal_old = Pico.m.pal;
+ }
+}
+
+static void sound_end(void)
+{
+ int ret;
+ while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
+ ret = sceAudio_5C37C0AE();
+ lprintf("sound_end: sceAudio_5C37C0AE ret %i\n", ret);
+}
+
+static void sound_deinit(void)
+{
+ sound_thread_exit = 1;
+ sceKernelSignalSema(sound_sem, 1);
+}
+
+static void writeSound(int len)
+{
+ int ret;
+ short *endptr = &sndBuffer[SOUND_DEF_BLOCK_SIZE*SOUND_BLOCK_COUNT*2];
if (PicoOpt&8) len<<=1;
- // TODO..
+ PsndOut += len;
+ if (PsndOut > endptr) {
+ memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
+ PsndOut = &sndBuffer[PsndOut - endptr];
+ }
+ else if (PsndOut == endptr)
+ PsndOut = sndBuffer; // happy case
+
+ // signal the snd thread
+ samples_made += len;
+ if (samples_made - samples_done >= samples_block) {
+ if (!Pico.m.scanline) lprintf("signal, %i/%i\n", samples_done, samples_made);
+ ret = sceKernelSignalSema(sound_sem, 1);
+ if (!Pico.m.scanline) lprintf("signal ret %i\n", ret);
+ }
}
-*/
+
static void SkipFrame(void)
{
vidResetMode();
memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*8/4); // borders
memset32((int *)VRAM_CACHED_STUFF + 512*232/4, 0xe0e0e0e0, 512*8/4);
+ memset32((int *)psp_screen + 512*264*2/4, 0, 512*8*2/4);
PicoDrawSetColorFormat(-1);
PicoScan = EmuScanSlow;
void emu_Loop(void)
{
- //static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
char fpsbuff[24]; // fps count c string
unsigned int tval, tval_prev = 0, tval_thissec = 0; // timing
int frames_done = 0, frames_shown = 0, oldmodes = 0;
// prepare sound stuff
PsndOut = NULL;
-#if 0 // TODO
if (currentConfig.EmuOpt & 4)
{
- int ret, snd_excess_add, stereo;
- if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
- sound_rerate(Pico.m.frame_count ? 1 : 0);
- }
- stereo=(PicoOpt&8)>>3;
- snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
- snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
- lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
- PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
- ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
- if (ret != 0) {
- lprintf("FrameworkAudio_Init() failed: %i\n", ret);
- sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
- noticeMsgTime = sceKernelGetSystemTimeLow();
- currentConfig.EmuOpt &= ~4;
- } else {
- FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
- PicoWriteSound = updateSound;
- snd_cbuff = FrameworkAudio_56448Buffer();
- PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
- snd_all_samples = 0;
- PsndRate_old = PsndRate;
- PicoOpt_old = PicoOpt;
- pal_old = Pico.m.pal;
- }
+ sound_prepare();
}
-#endif
// loop?
while (engineState == PGS_Running)
if (PicoMCD & 1) PicoCDBufferFree();
-/*
+
if (PsndOut != NULL) {
- PsndOut = snd_cbuff = NULL;
- FrameworkAudio_Close();
+ PsndOut = NULL;
+ sound_end();
}
-*/
+
// save SRAM
if ((currentConfig.EmuOpt & 1) && SRam.changed) {
emu_msg_cb("Writing SRAM/BRAM..");
emu_SaveLoadGame(0, 1);
SRam.changed = 0;
}
+
+ // draw a frame for bg..
+ emu_forcedFrame();
}
#define menu_screen psp_screen
static void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
-static void menu_prepare_bg(int use_game_bg, int use_back_buff);
+static void menu_prepare_bg(int use_game_bg);
static unsigned int inp_prev = 0;
}
emu_forcedFrame();
- menu_prepare_bg(1, 1);
+ menu_prepare_bg(1);
restore_oldstate(oldstate);
}
void *bgdata = is_32col ? bgdatac32_start : bgdatac40_start;
unsigned long insize = is_32col ? bgdatac32_size : bgdatac40_size, outsize = 65856;
int ret;
- lprintf("%p %p %i %i (n %p)\n", bgdatac32_start, bgdatac40_start, bgdatac32_size, bgdatac40_size, &engineState);
ret = uncompress((Bytef *)bg_buffer, &outsize, bgdata, insize);
if (ret == 0)
{
memset32(psp_screen, 0, 512*272*2/4);
emu_forcedFrame();
- menu_prepare_bg(1, 1);
+ menu_prepare_bg(1);
if (oldstate) restore_oldstate(oldstate);
}
menu_entry opt_entries[] =
{
{ NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1 },
- { "Scale low res mode", MB_ONOFF, MA_OPT_SCALING, ¤tConfig.scaling, 0x0001, 0, 3, 1 },
{ "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, ¤tConfig.PicoOpt, 0x0040, 0, 0, 1 },
{ "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, ¤tConfig.PicoOpt, 0x0080, 0, 0, 1 },
{ "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, ¤tConfig.EmuOpt, 0x0002, 0, 0, 1 },
}
}
-static void menu_prepare_bg(int use_game_bg, int use_back_buff)
+static void menu_prepare_bg(int use_game_bg)
{
if (use_game_bg)
{
// darken the active framebuffer
unsigned short *dst = bg_buffer;
- unsigned short *src = use_back_buff ? psp_screen : psp_video_get_active_fb();
+ unsigned short *src = psp_screen;
int i;
for (i = 272; i > 0; i--, dst += 480, src += 512)
menu_darken_bg(dst, src, 480, 1);
static void menu_gfx_prepare(void)
{
- menu_prepare_bg(rom_data != NULL, 0);
+ menu_prepare_bg(rom_data != NULL);
menu_draw_begin();
menu_draw_end();