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