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