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