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