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