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