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