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