+ // setup GU
+ sceGuSync(0,0); // sync with prev
+ sceGuStart(GU_DIRECT, guCmdList);
+
+ sceGuClutMode(GU_PSM_5650,0,0xff,0);
+ sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
+ sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
+ if (currentConfig.scaling)
+ sceGuTexFilter(GU_LINEAR, GU_LINEAR);
+ else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
+ sceGuTexScale(1.0f,1.0f);
+ sceGuTexOffset(0.0f,0.0f);
+
+ sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
+
+ // slow rend.
+ PicoDrawSetColorFormat(-1);
+ PicoScan = EmuScanSlow;
+
+ localPal[0xe0] = 0;
+ Pico.m.dirtyPal = 1;
+ blit_16bit_mode = dynamic_palette = 0;
+
+ sceGuFinish();
+ set_scaling_params();
+ sceGuSync(0,0);
+}
+
+
+/* sound stuff */
+#define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
+#define SOUND_BLOCK_SIZE_PAL (1764*2)
+#define SOUND_BLOCK_COUNT 8
+
+static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
+static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
+static int samples_made = 0, samples_done = 0, samples_block = 0;
+static int sound_thread_exit = 0;
+static SceUID sound_sem = -1;
+
+static void writeSound(int len);
+
+static int sound_thread(SceSize args, void *argp)
+{
+ int ret = 0;
+
+ lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
+
+ while (!sound_thread_exit)
+ {
+ if (samples_made - samples_done < samples_block) {
+ // wait for data (use at least 2 blocks)
+ //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
+ while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
+ ret = sceKernelWaitSema(sound_sem, 1, 0);
+ if (ret < 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 >= sndBuffer_endptr)
+ snd_playptr = sndBuffer;
+ // 1.5 kernel returns 0, newer ones return # of samples queued
+ if (ret < 0)
+ lprintf("sthr: sceAudio_E0727056: %08x; pos %i/%i\n", ret, samples_done, samples_made);
+
+ // shouln't happen, but just in case
+ if (samples_made - samples_done >= samples_block*3) {
+ lprintf("sthr: block skip (%i)\n", samples_made - samples_done);
+ samples_done += samples_block; // skip
+ snd_playptr += samples_block;
+ }
+
+ }
+
+ lprintf("sthr: exit\n");
+ sceKernelExitDeleteThread(0);
+ return 0;
+}
+
+static void sound_init(void)
+{
+ SceUID thid;
+ int ret;
+
+ sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
+ if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
+
+ samples_made = samples_done = 0;
+ samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
+ sound_thread_exit = 0;
+ thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
+ if (thid >= 0)
+ {
+ ret = sceKernelStartThread(thid, 0, 0);
+ if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
+ }
+ 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) {
+ PsndRerate(Pico.m.frame_count ? 1 : 0);
+ }
+ stereo=(PicoOpt&8)>>3;
+
+ samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
+ if (PsndRate <= 22050) samples_block /= 2;
+ sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
+
+ 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); // seems to not need that stupid 64byte alignment
+ if (ret < 0) {
+ lprintf("sceAudio_38553111() failed: %i\n", ret);
+ sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
+ noticeMsgTime = sceKernelGetSystemTimeLow();
+ currentConfig.EmuOpt &= ~4;