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