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