supporting caanoo, line doublers, refactoring
[libpicofe.git] / psp / emu.c
CommitLineData
63b796ca 1// (c) Copyright 2007 notaz, All rights reserved.
2// Free for non-commercial use.
3
4// For commercial use, separate licencing terms must be obtained.
5
2951214e 6#include <sys/stat.h>
7#include <sys/types.h>
8#include <sys/syslimits.h> // PATH_MAX
9
703e4c7b 10#include <pspthreadman.h>
2b90fc61 11#include <pspdisplay.h>
db298784 12#include <psputils.h>
13#include <pspgu.h>
0953046b 14#include <pspaudio.h>
703e4c7b 15
16#include "psp.h"
17#include "menu.h"
18#include "emu.h"
5ecedd0c 19#include "mp3.h"
93c0d147 20#include "asm_utils.h"
703e4c7b 21#include "../common/emu.h"
960a8e27 22#include "../common/config.h"
703e4c7b 23#include "../common/lprintf.h"
f11bad75 24#include <pico/pico_int.h>
25#include <pico/cd/cue.h>
2951214e 26
f3f1615e 27#define OSD_FPS_X 432
703e4c7b 28
0953046b 29// additional pspaudio imports, credits to crazyc
30int sceAudio_38553111(unsigned short samples, unsigned short freq, char unknown); // play with conversion?
31int sceAudio_5C37C0AE(void); // end play?
32int sceAudio_E0727056(int volume, void *buffer); // blocking output
33int sceAudioOutput2GetRestSample();
34
35
db298784 36unsigned char *PicoDraw2FB = (unsigned char *)VRAM_CACHED_STUFF + 8; // +8 to be able to skip border with 1 quadword..
049a6b3e 37int engineStateSuspend;
2951214e 38
8a091e48 39#define PICO_PEN_ADJUST_X 4
40#define PICO_PEN_ADJUST_Y 2
41static int pico_pen_x = 320/2, pico_pen_y = 240/2;
703e4c7b 42
0953046b 43static void sound_init(void);
44static void sound_deinit(void);
4b8f4f3c 45static void blit2(const char *fps, const char *notice, int lagging_behind);
703e4c7b 46static void clearArea(int full);
2951214e 47
93c18cb4 48int plat_get_root_dir(char *dst, int len)
2951214e 49{
703e4c7b 50 if (len > 0) *dst = 0;
36f6fd5a 51 return 0;
2951214e 52}
53
60a10527 54static void osd_text(int x, const char *text, int is_active, int clear_all)
2951214e 55{
6f748c47 56 unsigned short *screen = is_active ? psp_video_get_active_fb() : psp_screen;
60a10527 57 int len = clear_all ? (480 / 2) : (strlen(text) * 8 / 2);
6f748c47 58 int *p, h;
59 void *tmp;
60 for (h = 0; h < 8; h++) {
61 p = (int *) (screen+x+512*(264+h));
62 p = (int *) ((int)p & ~3); // align
5ecedd0c 63 memset32_uncached(p, 0, len);
6f748c47 64 }
65 if (is_active) { tmp = psp_screen; psp_screen = screen; } // nasty pointer tricks
b7911801 66 emu_text_out16(x, 264, text);
6f748c47 67 if (is_active) psp_screen = tmp;
68}
703e4c7b 69
6f748c47 70void emu_msg_cb(const char *msg)
71{
60a10527 72 osd_text(4, msg, 1, 1);
703e4c7b 73 noticeMsgTime = sceKernelGetSystemTimeLow() - 2000000;
74
75 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
76 reset_timing = 1;
2951214e 77}
78
93c18cb4 79/* FIXME: move to plat */
2951214e 80void emu_Init(void)
81{
0953046b 82 sound_init();
2951214e 83}
84
85void emu_Deinit(void)
86{
0953046b 87 sound_deinit();
2951214e 88}
89
93c18cb4 90void pemu_prep_defconfig(void)
960a8e27 91{
960a8e27 92 defaultConfig.s_PsndRate = 22050;
960a8e27 93 defaultConfig.s_PicoCDBuffers = 64;
960a8e27 94 defaultConfig.CPUclock = 333;
95 defaultConfig.KeyBinds[ 4] = 1<<0; // SACB RLDU
96 defaultConfig.KeyBinds[ 6] = 1<<1;
97 defaultConfig.KeyBinds[ 7] = 1<<2;
98 defaultConfig.KeyBinds[ 5] = 1<<3;
99 defaultConfig.KeyBinds[14] = 1<<4;
100 defaultConfig.KeyBinds[13] = 1<<5;
101 defaultConfig.KeyBinds[15] = 1<<6;
102 defaultConfig.KeyBinds[ 3] = 1<<7;
103 defaultConfig.KeyBinds[12] = 1<<26; // switch rnd
104 defaultConfig.KeyBinds[ 8] = 1<<27; // save state
105 defaultConfig.KeyBinds[ 9] = 1<<28; // load state
106 defaultConfig.KeyBinds[28] = 1<<0; // num "buttons"
107 defaultConfig.KeyBinds[30] = 1<<1;
108 defaultConfig.KeyBinds[31] = 1<<2;
109 defaultConfig.KeyBinds[29] = 1<<3;
110 defaultConfig.scaling = 1; // bilinear filtering for psp
111 defaultConfig.scale = 1.20; // fullscreen
112 defaultConfig.hscale40 = 1.25;
113 defaultConfig.hscale32 = 1.56;
114}
115
703e4c7b 116
6f748c47 117extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
b13a934c 118extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
119
8a091e48 120static void (*amips_clut_f)(unsigned short *dst, unsigned char *src, unsigned short *pal, int count) = NULL;
db298784 121
122struct Vertex
123{
124 short u,v;
125 short x,y,z;
126};
127
6f748c47 128static struct Vertex __attribute__((aligned(4))) g_vertices[2];
129static unsigned short __attribute__((aligned(16))) localPal[0x100];
130static int dynamic_palette = 0, need_pal_upload = 0, blit_16bit_mode = 0;
131static int fbimg_offs = 0;
132
133static void set_scaling_params(void)
db298784 134{
5ecedd0c 135 int src_width, fbimg_width, fbimg_height, fbimg_xoffs, fbimg_yoffs, border_hack = 0;
6f748c47 136 g_vertices[0].x = g_vertices[0].y =
137 g_vertices[0].z = g_vertices[1].z = 0;
138
139 fbimg_height = (int)(240.0 * currentConfig.scale + 0.5);
140 if (Pico.video.reg[12] & 1) {
141 fbimg_width = (int)(320.0 * currentConfig.scale * currentConfig.hscale40 + 0.5);
142 src_width = 320;
143 } else {
144 fbimg_width = (int)(256.0 * currentConfig.scale * currentConfig.hscale32 + 0.5);
145 src_width = 256;
146 }
db298784 147
5ecedd0c 148 if (fbimg_width & 1) fbimg_width++; // make even
149 if (fbimg_height & 1) fbimg_height++;
150
6f748c47 151 if (fbimg_width >= 480) {
152 g_vertices[0].u = (fbimg_width-480)/2;
5ecedd0c 153 g_vertices[1].u = src_width - (fbimg_width-480)/2 - 1;
6f748c47 154 fbimg_width = 480;
155 fbimg_xoffs = 0;
156 } else {
157 g_vertices[0].u = 0;
158 g_vertices[1].u = src_width;
159 fbimg_xoffs = 240 - fbimg_width/2;
160 }
c6334564 161 if (fbimg_width > 320 && fbimg_width <= 480) border_hack = 1;
6f748c47 162
163 if (fbimg_height >= 272) {
164 g_vertices[0].v = (fbimg_height-272)/2;
165 g_vertices[1].v = 240 - (fbimg_height-272)/2;
166 fbimg_height = 272;
167 fbimg_yoffs = 0;
168 } else {
169 g_vertices[0].v = 0;
170 g_vertices[1].v = 240;
171 fbimg_yoffs = 136 - fbimg_height/2;
172 }
173
174 g_vertices[1].x = fbimg_width;
175 g_vertices[1].y = fbimg_height;
176 if (fbimg_xoffs < 0) fbimg_xoffs = 0;
177 if (fbimg_yoffs < 0) fbimg_yoffs = 0;
5ecedd0c 178 if (border_hack) {
179 g_vertices[0].u++;
180 g_vertices[0].x++;
181 g_vertices[1].u--;
182 g_vertices[1].x--;
183 }
6f748c47 184 fbimg_offs = (fbimg_yoffs*512 + fbimg_xoffs) * 2; // dst is always 16bit
185
a52e1f62 186 /*
6f748c47 187 lprintf("set_scaling_params:\n");
188 lprintf("offs: %i, %i\n", fbimg_xoffs, fbimg_yoffs);
189 lprintf("xy0, xy1: %i, %i; %i, %i\n", g_vertices[0].x, g_vertices[0].y, g_vertices[1].x, g_vertices[1].y);
190 lprintf("uv0, uv1: %i, %i; %i, %i\n", g_vertices[0].u, g_vertices[0].v, g_vertices[1].u, g_vertices[1].v);
a52e1f62 191 */
db298784 192}
193
b13a934c 194static void do_pal_update(int allow_sh, int allow_as)
703e4c7b 195{
6f748c47 196 unsigned int *dpal=(void *)localPal;
197 int i;
db298784 198
93c0d147 199 //for (i = 0x3f/2; i >= 0; i--)
200 // dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
960a8e27 201 do_pal_convert(localPal, Pico.cram, currentConfig.gamma, currentConfig.gamma2);
db298784 202
b13a934c 203 Pico.m.dirtyPal = 0;
204 need_pal_upload = 1;
205
a52e1f62 206 if (allow_sh && (Pico.video.reg[0xC]&8)) // shadow/hilight?
6f748c47 207 {
208 // shadowed pixels
db298784 209 for (i = 0x3f/2; i >= 0; i--)
93c0d147 210 dpal[0x20|i] = dpal[0x60|i] = (dpal[i]>>1)&0x7bcf7bcf;
6f748c47 211 // hilighted pixels
212 for (i = 0x3f; i >= 0; i--) {
93c0d147 213 int t=localPal[i]&0xf79e;t+=0x4208;
214 if (t&0x20) t|=0x1e;
215 if (t&0x800) t|=0x780;
216 if (t&0x10000) t|=0xf000;
217 t&=0xf79e;
6f748c47 218 localPal[0x80|i]=(unsigned short)t;
219 }
220 localPal[0xe0] = 0;
8a091e48 221 localPal[0xf0] = 0x001f;
db298784 222 }
61f66fe0 223 else if (allow_as && (rendstatus & PDRAW_SPR_LO_ON_HI))
b13a934c 224 {
7443ecd9 225 memcpy32((int *)dpal+0x80/2, (void *)localPal, 0x40*2/4);
b13a934c 226 }
6f748c47 227}
db298784 228
6f748c47 229static void do_slowmode_lines(int line_to)
230{
231 int line = 0, line_len = (Pico.video.reg[12]&1) ? 320 : 256;
232 unsigned short *dst = (unsigned short *)VRAM_STUFF + 512*240/2;
233 unsigned char *src = (unsigned char *)VRAM_CACHED_STUFF + 16;
234 if (!(Pico.video.reg[1]&8)) { line = 8; dst += 512*8; src += 512*8; }
db298784 235
6f748c47 236 for (; line < line_to; line++, dst+=512, src+=512)
b13a934c 237 amips_clut_f(dst, src, localPal, line_len);
6f748c47 238}
db298784 239
6f748c47 240static void EmuScanPrepare(void)
241{
242 HighCol = (unsigned char *)VRAM_CACHED_STUFF + 8;
243 if (!(Pico.video.reg[1]&8)) HighCol += 8*512;
db298784 244
366747cc 245 if (dynamic_palette > 0)
246 dynamic_palette--;
7443ecd9 247
6f748c47 248 if (Pico.m.dirtyPal)
b13a934c 249 do_pal_update(1, 1);
61f66fe0 250 if ((rendstatus & PDRAW_SPR_LO_ON_HI) && !(Pico.video.reg[0xC]&8))
b13a934c 251 amips_clut_f = amips_clut_6bit;
252 else amips_clut_f = amips_clut;
6f748c47 253}
254
960a8e27 255static int EmuScanSlowBegin(unsigned int num)
256{
960a8e27 257 if (!dynamic_palette)
258 HighCol = (unsigned char *)VRAM_CACHED_STUFF + num * 512 + 8;
259
260 return 0;
261}
262
263static int EmuScanSlowEnd(unsigned int num)
6f748c47 264{
6f748c47 265 if (Pico.m.dirtyPal) {
266 if (!dynamic_palette) {
267 do_slowmode_lines(num);
366747cc 268 dynamic_palette = 3; // last for 2 more frames
6f748c47 269 }
7443ecd9 270 do_pal_update(1, 1);
6f748c47 271 }
272
273 if (dynamic_palette) {
274 int line_len = (Pico.video.reg[12]&1) ? 320 : 256;
275 void *dst = (char *)VRAM_STUFF + 512*240 + 512*2*num;
b13a934c 276 amips_clut_f(dst, HighCol + 8, localPal, line_len);
960a8e27 277 }
db298784 278
703e4c7b 279 return 0;
280}
281
6f748c47 282static void blitscreen_clut(void)
db298784 283{
6f748c47 284 int offs = fbimg_offs;
285 offs += (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
db298784 286
db298784 287 sceGuSync(0,0); // sync with prev
288 sceGuStart(GU_DIRECT, guCmdList);
6f748c47 289 sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
db298784 290
6f748c47 291 if (dynamic_palette)
292 {
8a091e48 293 if (!blit_16bit_mode) { // the current mode is not 16bit
6f748c47 294 sceGuTexMode(GU_PSM_5650, 0, 0, 0);
295 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 512*240);
db298784 296
6f748c47 297 blit_16bit_mode = 1;
298 }
db298784 299 }
6f748c47 300 else
db298784 301 {
6f748c47 302 if (blit_16bit_mode) {
303 sceGuClutMode(GU_PSM_5650,0,0xff,0);
304 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
305 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
306 blit_16bit_mode = 0;
307 }
db298784 308
6f748c47 309 if ((PicoOpt&0x10) && Pico.m.dirtyPal)
b13a934c 310 do_pal_update(0, 0);
a52e1f62 311
312 sceKernelDcacheWritebackAll();
db298784 313
6f748c47 314 if (need_pal_upload) {
315 need_pal_upload = 0;
316 sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
317 }
318 }
703e4c7b 319
6f748c47 320#if 1
321 if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
322 {
323 struct Vertex* vertices;
324 int x;
703e4c7b 325
6f748c47 326 #define SLICE_WIDTH 32
327 for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
328 {
329 // render sprite
330 vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
331 memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
332 vertices[0].u = vertices[0].x = x;
333 vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
334 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
335 }
336 // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
703e4c7b 337 }
6f748c47 338 else
339#endif
340 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
341
342 sceGuFinish();
703e4c7b 343}
344
345
346static void cd_leds(void)
347{
16e89bed 348 unsigned int reg, col_g, col_r, *p;
703e4c7b 349
16e89bed 350 reg = Pico_mcd->s68k_regs[0];
703e4c7b 351
352 p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
16e89bed 353 col_g = (reg & 2) ? 0x06000600 : 0;
354 col_r = (reg & 1) ? 0x00180018 : 0;
703e4c7b 355 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
356 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
357 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
358}
359
8a091e48 360static void draw_pico_ptr(void)
361{
362 unsigned char *p = (unsigned char *)VRAM_STUFF + 16;
363
364 // only if pen enabled and for 8bit mode
365 if (pico_inp_mode == 0 || blit_16bit_mode) return;
366
367 p += 512 * (pico_pen_y + PICO_PEN_ADJUST_Y);
368 p += pico_pen_x + PICO_PEN_ADJUST_X;
369 p[ -1] = 0xe0; p[ 0] = 0xf0; p[ 1] = 0xe0;
370 p[ 511] = 0xf0; p[ 512] = 0xf0; p[ 513] = 0xf0;
371 p[1023] = 0xe0; p[1024] = 0xf0; p[1025] = 0xe0;
372}
373
374
5ecedd0c 375#if 0
6f748c47 376static void dbg_text(void)
703e4c7b 377{
6f748c47 378 int *p, h, len;
379 char text[128];
703e4c7b 380
6f748c47 381 sprintf(text, "sl: %i, 16b: %i", g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x, blit_16bit_mode);
382 len = strlen(text) * 8 / 2;
383 for (h = 0; h < 8; h++) {
384 p = (int *) ((unsigned short *) psp_screen+2+512*(256+h));
385 p = (int *) ((int)p & ~3); // align
5ecedd0c 386 memset32_uncached(p, 0, len);
703e4c7b 387 }
b7911801 388 emu_text_out16(2, 256, text);
6f748c47 389}
5ecedd0c 390#endif
6f748c47 391
392/* called after rendering is done, but frame emulation is not finished */
393void blit1(void)
394{
395 if (PicoOpt&0x10)
703e4c7b 396 {
6f748c47 397 int i;
398 unsigned char *pd;
399 // clear top and bottom trash
400 for (pd = PicoDraw2FB+8, i = 8; i > 0; i--, pd += 512)
401 memset32((int *)pd, 0xe0e0e0e0, 320/4);
402 for (pd = PicoDraw2FB+512*232+8, i = 8; i > 0; i--, pd += 512)
403 memset32((int *)pd, 0xe0e0e0e0, 320/4);
703e4c7b 404 }
6f748c47 405
8a091e48 406 if (PicoAHW & PAHW_PICO)
407 draw_pico_ptr();
408
6f748c47 409 blitscreen_clut();
410}
411
412
4b8f4f3c 413static void blit2(const char *fps, const char *notice, int lagging_behind)
6f748c47 414{
4b8f4f3c 415 int vsync = 0, emu_opt = currentConfig.EmuOpt;
703e4c7b 416
417 if (notice || (emu_opt & 2)) {
60a10527 418 if (notice) osd_text(4, notice, 0, 0);
419 if (emu_opt & 2) osd_text(OSD_FPS_X, fps, 0, 0);
703e4c7b 420 }
421
5ecedd0c 422 //dbg_text();
6f748c47 423
dd5fd477 424 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
703e4c7b 425 cd_leds();
426
4b8f4f3c 427 if (currentConfig.EmuOpt & 0x2000) { // want vsync
428 if (!(currentConfig.EmuOpt & 0x10000) || !lagging_behind) vsync = 1;
429 }
430
431 psp_video_flip(vsync);
703e4c7b 432}
433
434// clears whole screen or just the notice area (in all buffers)
435static void clearArea(int full)
436{
437 if (full) {
5ecedd0c 438 memset32_uncached(psp_screen, 0, 512*272*2/4);
703e4c7b 439 psp_video_flip(0);
5ecedd0c 440 memset32_uncached(psp_screen, 0, 512*272*2/4);
6f748c47 441 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*240/4);
442 memset32((int *)VRAM_CACHED_STUFF+512*240/4, 0, 512*240*2/4);
703e4c7b 443 } else {
444 void *fb = psp_video_get_active_fb();
5ecedd0c 445 memset32_uncached((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
446 memset32_uncached((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
703e4c7b 447 }
448}
449
450static void vidResetMode(void)
451{
db298784 452 // setup GU
453 sceGuSync(0,0); // sync with prev
454 sceGuStart(GU_DIRECT, guCmdList);
455
456 sceGuClutMode(GU_PSM_5650,0,0xff,0);
db298784 457 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
458 sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
6f748c47 459 if (currentConfig.scaling)
460 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
461 else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
db298784 462 sceGuTexScale(1.0f,1.0f);
463 sceGuTexOffset(0.0f,0.0f);
db298784 464
6f748c47 465 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
db298784 466
6f748c47 467 // slow rend.
662e622b 468 PicoDrawSetOutFormat(PDF_NONE, 0);
b6072c17 469 PicoDrawSetCallbacks(EmuScanSlowBegin, EmuScanSlowEnd);
db298784 470
6f748c47 471 localPal[0xe0] = 0;
8a091e48 472 localPal[0xf0] = 0x001f;
703e4c7b 473 Pico.m.dirtyPal = 1;
6f748c47 474 blit_16bit_mode = dynamic_palette = 0;
703e4c7b 475
db298784 476 sceGuFinish();
6f748c47 477 set_scaling_params();
db298784 478 sceGuSync(0,0);
703e4c7b 479}
6f748c47 480
93c18cb4 481void plat_debug_cat(char *str)
7443ecd9 482{
483 strcat(str, blit_16bit_mode ? "soft clut\n" : "hard clut\n");
484}
485
0953046b 486
487/* sound stuff */
b9342840 488#define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
489#define SOUND_BLOCK_SIZE_PAL (1764*2)
a6df06b7 490#define SOUND_BLOCK_COUNT 8
0953046b 491
2aea862d 492static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
493static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
494static int samples_made = 0, samples_done = 0, samples_block = 0;
0953046b 495static int sound_thread_exit = 0;
496static SceUID sound_sem = -1;
497
498static void writeSound(int len);
499
500static int sound_thread(SceSize args, void *argp)
703e4c7b 501{
a6df06b7 502 int ret = 0;
0953046b 503
ae1bf35c 504 lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
0953046b 505
506 while (!sound_thread_exit)
507 {
508 if (samples_made - samples_done < samples_block) {
b9342840 509 // wait for data (use at least 2 blocks)
fe9e3b25 510 //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
b9342840 511 while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
512 ret = sceKernelWaitSema(sound_sem, 1, 0);
a6df06b7 513 if (ret < 0) lprintf("sthr: sceKernelWaitSema: %i\n", ret);
0953046b 514 continue;
515 }
516
a6df06b7 517 // lprintf("sthr: got data: %i\n", samples_made - samples_done);
0953046b 518
519 ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
520
521 samples_done += samples_block;
522 snd_playptr += samples_block;
2aea862d 523 if (snd_playptr >= sndBuffer_endptr)
0953046b 524 snd_playptr = sndBuffer;
fe9e3b25 525 // 1.5 kernel returns 0, newer ones return # of samples queued
526 if (ret < 0)
527 lprintf("sthr: sceAudio_E0727056: %08x; pos %i/%i\n", ret, samples_done, samples_made);
b9342840 528
529 // shouln't happen, but just in case
530 if (samples_made - samples_done >= samples_block*3) {
6d741b32 531 //lprintf("sthr: block skip (%i)\n", samples_made - samples_done);
b9342840 532 samples_done += samples_block; // skip
533 snd_playptr += samples_block;
534 }
535
0953046b 536 }
537
538 lprintf("sthr: exit\n");
539 sceKernelExitDeleteThread(0);
540 return 0;
541}
542
543static void sound_init(void)
544{
545 SceUID thid;
fe9e3b25 546 int ret;
0953046b 547
548 sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
549 if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
550
ae1bf35c 551 samples_made = samples_done = 0;
552 samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
0953046b 553 sound_thread_exit = 0;
554 thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
555 if (thid >= 0)
556 {
fe9e3b25 557 ret = sceKernelStartThread(thid, 0, 0);
558 if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
0953046b 559 }
560 else
561 lprintf("sceKernelCreateThread failed: %i\n", thid);
562}
563
93c18cb4 564void pemu_sound_start(void)
0953046b 565{
566 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
567 int ret, stereo;
568
569 samples_made = samples_done = 0;
570
571 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
c4f2f371 572 PsndRerate(Pico.m.frame_count ? 1 : 0);
0953046b 573 }
574 stereo=(PicoOpt&8)>>3;
2aea862d 575
576 samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
b9342840 577 if (PsndRate <= 22050) samples_block /= 2;
2aea862d 578 sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
0953046b 579
580 lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
581 PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
582
60a10527 583 // while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
584 // sceAudio_5C37C0AE();
2aea862d 585 ret = sceAudio_38553111(samples_block/2, PsndRate, 2); // seems to not need that stupid 64byte alignment
0953046b 586 if (ret < 0) {
587 lprintf("sceAudio_38553111() failed: %i\n", ret);
b5bfb864 588 emu_status_msg("sound init failed (%i), snd disabled", ret);
388947f3 589 currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
0953046b 590 } else {
0953046b 591 PicoWriteSound = writeSound;
592 memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
2aea862d 593 snd_playptr = sndBuffer_endptr - samples_block;
594 samples_made = samples_block; // send 1 empty block first..
0953046b 595 PsndOut = sndBuffer;
596 PsndRate_old = PsndRate;
597 PicoOpt_old = PicoOpt;
598 pal_old = Pico.m.pal;
599 }
600}
601
93c18cb4 602void pemu_sound_stop(void)
0953046b 603{
60a10527 604 int i;
605 if (samples_done == 0)
606 {
607 // if no data is written between sceAudio_38553111 and sceAudio_5C37C0AE calls,
608 // we get a deadlock on next sceAudio_38553111 call
609 // so this is yet another workaround:
610 memset32((int *)(void *)sndBuffer, 0, samples_block*4/4);
611 samples_made = samples_block * 3;
612 sceKernelSignalSema(sound_sem, 1);
613 }
614 sceKernelDelayThread(100*1000);
a52e1f62 615 samples_made = samples_done = 0;
60a10527 616 for (i = 0; sceAudioOutput2GetRestSample() > 0 && i < 16; i++)
2aea862d 617 psp_msleep(100);
618 sceAudio_5C37C0AE();
0953046b 619}
620
725d7f6c 621/* wait until we can write more sound */
93c18cb4 622void pemu_sound_wait(void)
725d7f6c 623{
624 // TODO: test this
625 while (!sound_thread_exit && samples_made - samples_done > samples_block * 4)
626 psp_msleep(10);
627}
628
0953046b 629static void sound_deinit(void)
630{
631 sound_thread_exit = 1;
632 sceKernelSignalSema(sound_sem, 1);
a52e1f62 633 sceKernelDeleteSema(sound_sem);
634 sound_sem = -1;
0953046b 635}
636
637static void writeSound(int len)
638{
639 int ret;
703e4c7b 640 if (PicoOpt&8) len<<=1;
641
0953046b 642 PsndOut += len;
2aea862d 643 /*if (PsndOut > sndBuffer_endptr) {
0953046b 644 memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
645 PsndOut = &sndBuffer[PsndOut - endptr];
2aea862d 646 lprintf("mov\n");
0953046b 647 }
2aea862d 648 else*/
a6df06b7 649 if (PsndOut > sndBuffer_endptr) lprintf("snd oflow %i!\n", PsndOut - sndBuffer_endptr);
2aea862d 650 if (PsndOut >= sndBuffer_endptr)
651 PsndOut = sndBuffer;
0953046b 652
653 // signal the snd thread
654 samples_made += len;
b9342840 655 if (samples_made - samples_done > samples_block*2) {
2aea862d 656 // lprintf("signal, %i/%i\n", samples_done, samples_made);
0953046b 657 ret = sceKernelSignalSema(sound_sem, 1);
a6df06b7 658 //if (ret < 0) lprintf("snd signal ret %08x\n", ret);
0953046b 659 }
703e4c7b 660}
0953046b 661
703e4c7b 662
663static void SkipFrame(void)
664{
665 PicoSkipFrame=1;
666 PicoFrame();
667 PicoSkipFrame=0;
668}
669
209a7eff 670void pemu_forced_frame(int opts, int no_scale)
703e4c7b 671{
672 int po_old = PicoOpt;
673 int eo_old = currentConfig.EmuOpt;
674
84e21f25 675 PicoOpt &= ~0x10;
8a091e48 676 PicoOpt |= opts|POPT_ACC_SPRITES;
703e4c7b 677 currentConfig.EmuOpt |= 0x80;
678
6f748c47 679 vidResetMode();
680 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*8/4); // borders
681 memset32((int *)VRAM_CACHED_STUFF + 512*232/4, 0xe0e0e0e0, 512*8/4);
5ecedd0c 682 memset32_uncached((int *)psp_screen + 512*264*2/4, 0, 512*8*2/4);
6f748c47 683
662e622b 684 PicoDrawSetOutFormat(PDF_NONE, 0);
b6072c17 685 PicoDrawSetCallbacks(EmuScanSlowBegin, EmuScanSlowEnd);
db298784 686 EmuScanPrepare();
703e4c7b 687 PicoFrameDrawOnly();
6f748c47 688 blit1();
689 sceGuSync(0,0);
703e4c7b 690
691 PicoOpt = po_old;
692 currentConfig.EmuOpt = eo_old;
693}
694
695
8a091e48 696static void RunEventsPico(unsigned int events, unsigned int keys)
697{
698 emu_RunEventsPico(events);
699
700 if (pico_inp_mode != 0)
701 {
702 PicoPad[0] &= ~0x0f; // release UDLR
b3972d82 703 if (keys & PBTN_UP) { pico_pen_y--; if (pico_pen_y < 8) pico_pen_y = 8; }
704 if (keys & PBTN_DOWN) { pico_pen_y++; if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y; }
705 if (keys & PBTN_LEFT) { pico_pen_x--; if (pico_pen_x < 0) pico_pen_x = 0; }
706 if (keys & PBTN_RIGHT) {
8a091e48 707 int lim = (Pico.video.reg[12]&1) ? 319 : 255;
708 pico_pen_x++;
709 if (pico_pen_x > lim-PICO_PEN_ADJUST_X)
710 pico_pen_x = lim-PICO_PEN_ADJUST_X;
711 }
712 PicoPicohw.pen_pos[0] = pico_pen_x;
713 if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
714 PicoPicohw.pen_pos[0] += 0x3c;
715 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
716 }
717}
718
703e4c7b 719static void RunEvents(unsigned int which)
720{
721 if (which & 0x1800) // save or load (but not both)
722 {
723 int do_it = 1;
724
ee0f881e 725 if ( emu_check_save_file(state_slot) &&
703e4c7b 726 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
727 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
728 {
729 int keys;
4b8f4f3c 730 sceGuSync(0,0);
731 blit2("", (which & 0x1000) ? "LOAD STATE? (X=yes, O=no)" : "OVERWRITE SAVE? (X=yes, O=no)", 0);
b3972d82 732 while( !((keys = psp_pad_read(1)) & (PBTN_X|PBTN_CIRCLE)) )
703e4c7b 733 psp_msleep(50);
b3972d82 734 if (keys & PBTN_CIRCLE) do_it = 0;
735 while( ((keys = psp_pad_read(1)) & (PBTN_X|PBTN_CIRCLE)) ) // wait for release
703e4c7b 736 psp_msleep(50);
737 clearArea(0);
738 }
739
740 if (do_it)
741 {
60a10527 742 osd_text(4, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME", 1, 0);
6f748c47 743 PicoStateProgressCB = emu_msg_cb;
ee0f881e 744 emu_save_load_game((which & 0x1000) >> 12, 0);
703e4c7b 745 PicoStateProgressCB = NULL;
746 psp_msleep(0);
747 }
748
749 reset_timing = 1;
750 }
751 if (which & 0x0400) // switch renderer
752 {
753 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
754 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
755
756 vidResetMode();
757
388947f3 758 if (PicoOpt & POPT_ALT_RENDERER)
b5bfb864 759 emu_status_msg("fast renderer");
ae1bf35c 760 else if (currentConfig.EmuOpt&0x80)
b5bfb864 761 emu_status_msg("accurate renderer");
703e4c7b 762 }
763 if (which & 0x0300)
764 {
765 if(which&0x0200) {
766 state_slot -= 1;
767 if(state_slot < 0) state_slot = 9;
768 } else {
769 state_slot += 1;
770 if(state_slot > 9) state_slot = 0;
771 }
b5bfb864 772 emu_status_msg("SAVE SLOT %i [%s]", state_slot,
ee0f881e 773 emu_check_save_file(state_slot) ? "USED" : "FREE");
703e4c7b 774 }
775}
776
777static void updateKeys(void)
778{
779 unsigned int keys, allActions[2] = { 0, 0 }, events;
780 static unsigned int prevEvents = 0;
781 int i;
782
388947f3 783 /* FIXME: port to input fw, merge with emu.c:emu_update_input() */
703e4c7b 784 keys = psp_pad_read(0);
2b90fc61 785 if (keys & PSP_CTRL_HOME)
786 sceDisplayWaitVblankStart();
787
b3972d82 788 if (keys & PBTN_SELECT)
703e4c7b 789 engineState = PGS_Menu;
790
791 keys &= CONFIGURABLE_KEYS;
792
6589c840 793 PicoPad[0] = allActions[0] & 0xfff;
794 PicoPad[1] = allActions[1] & 0xfff;
795
796 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
797 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
703e4c7b 798
799 events = (allActions[0] | allActions[1]) >> 16;
800
8a091e48 801 if ((events ^ prevEvents) & 0x40) {
ee0f881e 802 emu_set_fastforward(events & 0x40);
8a091e48 803 reset_timing = 1;
804 }
805
703e4c7b 806 events &= ~prevEvents;
8a091e48 807
808 if (PicoAHW == PAHW_PICO)
809 RunEventsPico(events, keys);
703e4c7b 810 if (events) RunEvents(events);
811 if (movie_data) emu_updateMovie();
812
813 prevEvents = (allActions[0] | allActions[1]) >> 16;
814}
815
703e4c7b 816
817static void simpleWait(unsigned int until)
818{
819 unsigned int tval;
820 int diff;
821
822 tval = sceKernelGetSystemTimeLow();
823 diff = (int)until - (int)tval;
824 if (diff >= 512 && diff < 100*1024)
825 sceKernelDelayThread(diff);
826}
827
93c18cb4 828void pemu_loop(void)
703e4c7b 829{
fe9e3b25 830 static int mp3_init_done = 0;
703e4c7b 831 char fpsbuff[24]; // fps count c string
960a8e27 832 unsigned int tval, tval_thissec = 0; // timing
833 int target_fps, target_frametime, lim_time, tval_diff, i, oldmodes = 0;
834 int pframes_done, pframes_shown; // "period" frames, used for sync
835 int frames_done, frames_shown, tval_fpsc = 0; // actual frames
703e4c7b 836 char *notice = NULL;
837
838 lprintf("entered emu_Loop()\n");
839
840 fpsbuff[0] = 0;
841
2b90fc61 842 if (currentConfig.CPUclock != psp_get_cpu_clock()) {
843 lprintf("setting cpu clock to %iMHz... ", currentConfig.CPUclock);
844 i = psp_set_cpu_clock(currentConfig.CPUclock);
845 lprintf(i ? "failed\n" : "done\n");
846 currentConfig.CPUclock = psp_get_cpu_clock();
847 }
848
703e4c7b 849 // make sure we are in correct mode
850 vidResetMode();
6f748c47 851 clearArea(1);
703e4c7b 852 Pico.m.dirtyPal = 1;
853 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
703e4c7b 854
855 // pal/ntsc might have changed, reset related stuff
856 target_fps = Pico.m.pal ? 50 : 60;
857 target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
858 reset_timing = 1;
859
dd5fd477 860 if (PicoAHW & PAHW_MCD) {
fe9e3b25 861 // prepare CD buffer
862 PicoCDBufferInit();
863 // mp3...
864 if (!mp3_init_done) {
865 i = mp3_init();
866 mp3_init_done = 1;
867 if (i) { engineState = PGS_Menu; return; }
868 }
869 }
703e4c7b 870
871 // prepare sound stuff
872 PsndOut = NULL;
93c18cb4 873 if (currentConfig.EmuOpt & EOPT_EN_SOUND)
703e4c7b 874 {
93c18cb4 875 pemu_sound_start();
703e4c7b 876 }
703e4c7b 877
c93fb19e 878 sceDisplayWaitVblankStart();
960a8e27 879 pframes_shown = pframes_done =
880 frames_shown = frames_done = 0;
881
882 tval_fpsc = sceKernelGetSystemTimeLow();
c93fb19e 883
703e4c7b 884 // loop?
885 while (engineState == PGS_Running)
886 {
887 int modes;
888
889 tval = sceKernelGetSystemTimeLow();
960a8e27 890 if (reset_timing || tval < tval_fpsc) {
703e4c7b 891 //stdbg("timing reset");
892 reset_timing = 0;
893 tval_thissec = tval;
960a8e27 894 pframes_shown = pframes_done = 0;
703e4c7b 895 }
896
897 // show notice message?
898 if (noticeMsgTime) {
899 static int noticeMsgSum;
900 if (tval - noticeMsgTime > 2000000) { // > 2.0 sec
901 noticeMsgTime = 0;
902 clearArea(0);
903 notice = 0;
904 } else {
905 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
906 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
907 notice = noticeMsg;
908 }
909 }
910
911 // check for mode changes
912 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
913 if (modes != oldmodes) {
914 oldmodes = modes;
915 clearArea(1);
6f748c47 916 set_scaling_params();
703e4c7b 917 }
918
919 // second passed?
960a8e27 920 if (tval - tval_fpsc >= 1000000)
921 {
922 if (currentConfig.EmuOpt & 2)
923 sprintf(fpsbuff, "%02i/%02i ", frames_shown, frames_done);
924 frames_done = frames_shown = 0;
925 tval_fpsc += 1000000;
926 }
927
703e4c7b 928 if (tval - tval_thissec >= 1000000)
929 {
2aea862d 930 // missing 1 frame?
960a8e27 931 if (currentConfig.Frameskip < 0 && pframes_done < target_fps) {
932 SkipFrame(); pframes_done++; frames_done++;
703e4c7b 933 }
2aea862d 934
703e4c7b 935 tval_thissec += 1000000;
936
937 if (currentConfig.Frameskip < 0) {
960a8e27 938 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
939 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
940 if (pframes_shown > pframes_done) pframes_shown = pframes_done;
703e4c7b 941 } else {
960a8e27 942 pframes_done = pframes_shown = 0;
703e4c7b 943 }
944 }
945#ifdef PFRAMES
946 sprintf(fpsbuff, "%i", Pico.m.frame_count);
947#endif
948
960a8e27 949 lim_time = (pframes_done+1) * target_frametime;
703e4c7b 950 if (currentConfig.Frameskip >= 0) // frameskip enabled
951 {
952 for (i = 0; i < currentConfig.Frameskip; i++) {
953 updateKeys();
960a8e27 954 SkipFrame(); pframes_done++; frames_done++;
c6334564 955 if (!(currentConfig.EmuOpt&0x40000)) { // do framelimitting if needed
703e4c7b 956 int tval_diff;
957 tval = sceKernelGetSystemTimeLow();
958 tval_diff = (int)(tval - tval_thissec) << 8;
959 if (tval_diff < lim_time) // we are too fast
960 simpleWait(tval + ((lim_time - tval_diff)>>8));
961 }
962 lim_time += target_frametime;
963 }
964 }
965 else // auto frameskip
966 {
967 int tval_diff;
968 tval = sceKernelGetSystemTimeLow();
969 tval_diff = (int)(tval - tval_thissec) << 8;
960a8e27 970 if (tval_diff > lim_time && (pframes_done/16 < pframes_shown))
703e4c7b 971 {
972 // no time left for this frame - skip
973 if (tval_diff - lim_time >= (300000<<8)) {
703e4c7b 974 reset_timing = 1;
975 continue;
976 }
977 updateKeys();
960a8e27 978 SkipFrame(); pframes_done++; frames_done++;
703e4c7b 979 continue;
980 }
981 }
982
983 updateKeys();
984
985 if (!(PicoOpt&0x10))
db298784 986 EmuScanPrepare();
703e4c7b 987
988 PicoFrame();
989
4b8f4f3c 990 sceGuSync(0,0);
703e4c7b 991
992 // check time
993 tval = sceKernelGetSystemTimeLow();
994 tval_diff = (int)(tval - tval_thissec) << 8;
995
4b8f4f3c 996 blit2(fpsbuff, notice, tval_diff > lim_time);
997
960a8e27 998 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300000<<8)) { // slowdown detection
703e4c7b 999 reset_timing = 1;
960a8e27 1000 }
c6334564 1001 else if (!(currentConfig.EmuOpt&0x40000) || currentConfig.Frameskip < 0)
703e4c7b 1002 {
1003 // sleep if we are still too fast
1004 if (tval_diff < lim_time)
1005 {
1006 // we are too fast
1007 simpleWait(tval + ((lim_time - tval_diff) >> 8));
1008 }
1009 }
1010
960a8e27 1011 pframes_done++; pframes_shown++;
1012 frames_done++; frames_shown++;
703e4c7b 1013 }
1014
1015
ee0f881e 1016 emu_set_fastforward(0);
8a091e48 1017
dd5fd477 1018 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
0953046b 1019
703e4c7b 1020 if (PsndOut != NULL) {
93c18cb4 1021 pemu_sound_stop();
0953046b 1022 PsndOut = NULL;
703e4c7b 1023 }
0953046b 1024
703e4c7b 1025 // save SRAM
1026 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
6f748c47 1027 emu_msg_cb("Writing SRAM/BRAM..");
ee0f881e 1028 emu_save_load_game(0, 1);
703e4c7b 1029 SRam.changed = 0;
1030 }
0953046b 1031
a52e1f62 1032 // clear fps counters and stuff
5ecedd0c 1033 memset32_uncached((int *)psp_video_get_active_fb() + 512*264*2/4, 0, 512*8*2/4);
703e4c7b 1034}
1035
677b5dd8 1036void emu_HandleResume(void)
1037{
dd5fd477 1038 if (!(PicoAHW & PAHW_MCD)) return;
677b5dd8 1039
dfa8d77a 1040 // reopen first CD track
677b5dd8 1041 if (Pico_mcd->TOC.Tracks[0].F != NULL)
1042 {
049a6b3e 1043 char *fname = rom_fname_reload;
1044 int len = strlen(rom_fname_reload);
dfa8d77a 1045 cue_data_t *cue_data = NULL;
1046
1047 if (len > 4 && strcasecmp(fname + len - 4, ".cue") == 0)
1048 {
049a6b3e 1049 cue_data = cue_parse(rom_fname_reload);
dfa8d77a 1050 if (cue_data != NULL)
1051 fname = cue_data->tracks[1].fname;
1052 }
1053
1054 lprintf("emu_HandleResume: reopen %s\n", fname);
677b5dd8 1055 pm_close(Pico_mcd->TOC.Tracks[0].F);
dfa8d77a 1056 Pico_mcd->TOC.Tracks[0].F = pm_open(fname);
677b5dd8 1057 lprintf("reopen %s\n", Pico_mcd->TOC.Tracks[0].F != NULL ? "ok" : "failed");
dfa8d77a 1058
1059 if (cue_data != NULL) cue_destroy(cue_data);
677b5dd8 1060 }
1061
1062 mp3_reopen_file();
dfa8d77a 1063
1064 if (!(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
1065 cdda_start_play();
677b5dd8 1066}
1067