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