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