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