2823a4c8 |
1 | /* gameplaySP |
2 | * |
3 | * Copyright (C) 2006 Exophase <exophase@gmail.com> |
4 | * |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as |
7 | * published by the Free Software Foundation; either version 2 of |
8 | * the License, or (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | */ |
19 | |
20 | #include "common.h" |
21 | |
22 | #ifdef PSP_BUILD |
23 | |
24 | //PSP_MODULE_INFO("gpSP", 0x1000, 0, 6); |
25 | //PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER); |
26 | |
27 | void vblank_interrupt_handler(u32 sub, u32 *parg); |
28 | |
29 | #endif |
30 | |
31 | timer_type timer[4]; |
32 | |
33 | //debug_state current_debug_state = COUNTDOWN_BREAKPOINT; |
34 | //debug_state current_debug_state = PC_BREAKPOINT; |
35 | u32 breakpoint_value = 0x7c5000; |
36 | debug_state current_debug_state = RUN; |
37 | //debug_state current_debug_state = STEP_RUN; |
38 | |
39 | //u32 breakpoint_value = 0; |
40 | |
41 | frameskip_type current_frameskip_type = auto_frameskip; |
42 | u32 global_cycles_per_instruction = 1; |
43 | u32 random_skip = 0; |
ee0a3871 |
44 | u32 fps_debug = 0; |
2823a4c8 |
45 | |
2823a4c8 |
46 | u32 frameskip_value = 2; |
47 | |
2823a4c8 |
48 | u64 last_frame_interval_timestamp; |
2823a4c8 |
49 | |
2823a4c8 |
50 | u32 skip_next_frame = 0; |
51 | |
52 | u32 frameskip_counter = 0; |
53 | |
54 | u32 cpu_ticks = 0; |
55 | u32 frame_ticks = 0; |
56 | |
57 | u32 execute_cycles = 960; |
58 | s32 video_count = 960; |
59 | u32 ticks; |
60 | |
61 | u32 arm_frame = 0; |
62 | u32 thumb_frame = 0; |
63 | u32 last_frame = 0; |
64 | |
65 | u32 cycle_memory_access = 0; |
66 | u32 cycle_pc_relative_access = 0; |
67 | u32 cycle_sp_relative_access = 0; |
68 | u32 cycle_block_memory_access = 0; |
69 | u32 cycle_block_memory_sp_access = 0; |
70 | u32 cycle_block_memory_words = 0; |
71 | u32 cycle_dma16_words = 0; |
72 | u32 cycle_dma32_words = 0; |
73 | u32 flush_ram_count = 0; |
74 | u32 gbc_update_count = 0; |
75 | u32 oam_update_count = 0; |
76 | |
77 | u32 synchronize_flag = 1; |
78 | |
79 | u32 update_backup_flag = 1; |
90206450 |
80 | #ifdef GP2X_BUILD |
81 | u32 clock_speed = 200; |
82 | #else |
2823a4c8 |
83 | u32 clock_speed = 333; |
90206450 |
84 | #endif |
bbba3209 |
85 | char main_path[512]; |
2823a4c8 |
86 | |
87 | void trigger_ext_event(); |
88 | |
89 | #define check_count(count_var) \ |
90 | if(count_var < execute_cycles) \ |
91 | execute_cycles = count_var; \ |
92 | |
93 | #define check_timer(timer_number) \ |
94 | if(timer[timer_number].status == TIMER_PRESCALE) \ |
95 | check_count(timer[timer_number].count); \ |
96 | |
97 | #define update_timer(timer_number) \ |
98 | if(timer[timer_number].status != TIMER_INACTIVE) \ |
99 | { \ |
100 | if(timer[timer_number].status != TIMER_CASCADE) \ |
101 | { \ |
102 | timer[timer_number].count -= execute_cycles; \ |
103 | io_registers[REG_TM##timer_number##D] = \ |
104 | -(timer[timer_number].count >> timer[timer_number].prescale); \ |
105 | } \ |
106 | \ |
107 | if(timer[timer_number].count <= 0) \ |
108 | { \ |
109 | if(timer[timer_number].irq == TIMER_TRIGGER_IRQ) \ |
110 | irq_raised |= IRQ_TIMER##timer_number; \ |
111 | \ |
112 | if((timer_number != 3) && \ |
113 | (timer[timer_number + 1].status == TIMER_CASCADE)) \ |
114 | { \ |
115 | timer[timer_number + 1].count--; \ |
116 | io_registers[REG_TM0D + (timer_number + 1) * 2] = \ |
117 | -(timer[timer_number + 1].count); \ |
118 | } \ |
119 | \ |
120 | if(timer_number < 2) \ |
121 | { \ |
122 | if(timer[timer_number].direct_sound_channels & 0x01) \ |
123 | sound_timer(timer[timer_number].frequency_step, 0); \ |
124 | \ |
125 | if(timer[timer_number].direct_sound_channels & 0x02) \ |
126 | sound_timer(timer[timer_number].frequency_step, 1); \ |
127 | } \ |
128 | \ |
129 | timer[timer_number].count += \ |
130 | (timer[timer_number].reload << timer[timer_number].prescale); \ |
131 | } \ |
132 | } \ |
133 | |
bbba3209 |
134 | static const char *file_ext[] = { ".gba", ".bin", ".zip", NULL }; |
2823a4c8 |
135 | |
136 | #ifdef ARM_ARCH |
137 | void ChangeWorkingDirectory(char *exe) |
138 | { |
139 | #ifndef _WIN32_WCE |
140 | char *s = strrchr(exe, '/'); |
141 | if (s != NULL) { |
142 | *s = '\0'; |
143 | chdir(exe); |
144 | *s = '/'; |
145 | } |
146 | #endif |
147 | } |
148 | #endif |
149 | |
150 | void init_main() |
151 | { |
152 | u32 i; |
153 | |
154 | skip_next_frame = 0; |
155 | |
156 | for(i = 0; i < 4; i++) |
157 | { |
158 | dma[i].start_type = DMA_INACTIVE; |
159 | dma[i].direct_sound_channel = DMA_NO_DIRECT_SOUND; |
160 | timer[i].status = TIMER_INACTIVE; |
161 | timer[i].reload = 0x10000; |
162 | timer[i].stop_cpu_ticks = 0; |
163 | } |
164 | |
165 | timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH; |
166 | timer[1].direct_sound_channels = TIMER_DS_CHANNEL_NONE; |
167 | |
168 | cpu_ticks = 0; |
169 | frame_ticks = 0; |
170 | |
171 | execute_cycles = 960; |
172 | video_count = 960; |
173 | |
174 | flush_translation_cache_rom(); |
175 | flush_translation_cache_ram(); |
176 | flush_translation_cache_bios(); |
177 | } |
178 | |
179 | int main(int argc, char *argv[]) |
180 | { |
d0944fc9 |
181 | char bios_filename[512]; |
182 | int ret; |
183 | |
2823a4c8 |
184 | #ifdef PSP_BUILD |
185 | sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0, |
186 | vblank_interrupt_handler, NULL); |
187 | sceKernelEnableSubIntr(PSP_VBLANK_INT, 0); |
2823a4c8 |
188 | #endif |
189 | |
2823a4c8 |
190 | init_gamepak_buffer(); |
191 | |
192 | // Copy the directory path of the executable into main_path |
193 | |
194 | #ifdef ARM_ARCH |
195 | // ChangeWorkingDirectory will null out the filename out of the path |
196 | ChangeWorkingDirectory(argv[0]); |
197 | #endif |
198 | |
199 | getcwd(main_path, 512); |
2823a4c8 |
200 | |
201 | #ifdef PSP_BUILD |
202 | delay_us(2500000); |
203 | #endif |
204 | |
43c24b30 |
205 | #ifndef PC_BUILD |
206 | gpsp_plat_init(); |
2823a4c8 |
207 | #endif |
eb3668fc |
208 | load_config_file(); |
209 | |
210 | gamepak_filename[0] = 0; |
2823a4c8 |
211 | |
4cdfc0bc |
212 | init_video(); |
213 | |
d0944fc9 |
214 | sprintf(bios_filename, "%s" PATH_SEPARATOR "%s", main_path, "gba_bios.bin"); |
215 | ret = load_bios(bios_filename); |
216 | if (ret != 0) |
217 | ret = load_bios("gba_bios.bin"); |
218 | if (ret != 0) |
2823a4c8 |
219 | { |
220 | gui_action_type gui_action = CURSOR_NONE; |
221 | |
222 | debug_screen_start(); |
223 | debug_screen_printl("Sorry, but gpSP requires a Gameboy Advance BIOS "); |
224 | debug_screen_printl("image to run correctly. Make sure to get an "); |
225 | debug_screen_printl("authentic one, it'll be exactly 16384 bytes large "); |
226 | debug_screen_printl("and should have the following md5sum value: "); |
227 | debug_screen_printl(" "); |
228 | debug_screen_printl("a860e8c0b6d573d191e4ec7db1b1e4f6 "); |
229 | debug_screen_printl(" "); |
230 | debug_screen_printl("When you do get it name it gba_bios.bin and put it"); |
eb3668fc |
231 | #ifdef PND_BUILD |
232 | debug_screen_printl("in <CD card>/pandora/appdata/gpsp/ . "); |
233 | #else |
2823a4c8 |
234 | debug_screen_printl("in the same directory as gpSP. "); |
eb3668fc |
235 | #endif |
2823a4c8 |
236 | debug_screen_printl(" "); |
237 | debug_screen_printl("Press any button to exit. "); |
238 | |
239 | debug_screen_update(); |
240 | |
241 | while(gui_action == CURSOR_NONE) |
242 | { |
243 | gui_action = get_gui_input(); |
244 | delay_us(15000); |
245 | } |
246 | |
247 | debug_screen_end(); |
248 | |
249 | quit(); |
250 | } |
251 | |
252 | if(bios_rom[0] != 0x18) |
253 | { |
254 | gui_action_type gui_action = CURSOR_NONE; |
255 | |
256 | debug_screen_start(); |
257 | debug_screen_printl("You have an incorrect BIOS image. "); |
258 | debug_screen_printl("While many games will work fine, some will not. It"); |
259 | debug_screen_printl("is strongly recommended that you obtain the "); |
260 | debug_screen_printl("correct BIOS file. Do NOT report any bugs if you "); |
261 | debug_screen_printl("are seeing this message. "); |
262 | debug_screen_printl(" "); |
263 | debug_screen_printl("Press any button to resume, at your own risk. "); |
264 | |
265 | debug_screen_update(); |
266 | |
267 | while(gui_action == CURSOR_NONE) |
268 | { |
269 | gui_action = get_gui_input(); |
270 | delay_us(15000); |
271 | } |
272 | |
273 | debug_screen_end(); |
274 | } |
275 | |
276 | init_main(); |
277 | init_sound(); |
278 | |
279 | init_input(); |
280 | |
281 | video_resolution_large(); |
282 | |
283 | if(argc > 1) |
284 | { |
285 | if(load_gamepak(argv[1]) == -1) |
286 | { |
43c24b30 |
287 | #ifndef PSP_BUILD |
bbba3209 |
288 | printf("Failed to load gamepak %s, exiting.\n", argv[1]); |
2823a4c8 |
289 | #endif |
290 | exit(-1); |
291 | } |
292 | |
293 | set_gba_resolution(screen_scale); |
294 | video_resolution_small(); |
295 | |
296 | init_cpu(); |
297 | init_memory(); |
298 | } |
299 | else |
300 | { |
bbba3209 |
301 | char load_filename[512]; |
2823a4c8 |
302 | if(load_file(file_ext, load_filename) == -1) |
303 | { |
304 | menu(copy_screen()); |
305 | } |
306 | else |
307 | { |
308 | if(load_gamepak(load_filename) == -1) |
309 | { |
43c24b30 |
310 | #ifndef PSP_BUILD |
2823a4c8 |
311 | printf("Failed to load gamepak %s, exiting.\n", load_filename); |
312 | #endif |
313 | exit(-1); |
314 | } |
315 | |
1d02ca75 |
316 | set_clock_speed(); |
2823a4c8 |
317 | set_gba_resolution(screen_scale); |
318 | video_resolution_small(); |
319 | |
320 | init_cpu(); |
321 | init_memory(); |
322 | } |
323 | } |
324 | |
325 | last_frame = 0; |
326 | |
327 | // We'll never actually return from here. |
328 | |
329 | #ifdef PSP_BUILD |
330 | execute_arm_translate(execute_cycles); |
331 | #else |
332 | |
2823a4c8 |
333 | /* u8 current_savestate_filename[512]; |
334 | get_savestate_filename_noshot(savestate_slot, |
335 | current_savestate_filename); |
336 | load_state(current_savestate_filename); */ |
337 | |
90206450 |
338 | // debug_on(); |
2823a4c8 |
339 | |
340 | if(argc > 2) |
341 | { |
342 | current_debug_state = COUNTDOWN_BREAKPOINT; |
343 | breakpoint_value = strtol(argv[2], NULL, 16); |
344 | } |
345 | |
346 | trigger_ext_event(); |
347 | |
348 | execute_arm_translate(execute_cycles); |
349 | execute_arm(execute_cycles); |
350 | #endif |
351 | return 0; |
352 | } |
353 | |
354 | void print_memory_stats(u32 *counter, u32 *region_stats, char *stats_str) |
355 | { |
356 | u32 other_region_counter = region_stats[0x1] + region_stats[0xE] + |
357 | region_stats[0xF]; |
358 | u32 rom_region_counter = region_stats[0x8] + region_stats[0x9] + |
359 | region_stats[0xA] + region_stats[0xB] + region_stats[0xC] + |
360 | region_stats[0xD]; |
361 | u32 _counter = *counter; |
362 | |
363 | printf("memory access stats: %s (out of %d)\n", stats_str, _counter); |
364 | printf("bios: %f%%\tiwram: %f%%\tewram: %f%%\tvram: %f\n", |
365 | region_stats[0x0] * 100.0 / _counter, region_stats[0x3] * 100.0 / |
366 | _counter, |
367 | region_stats[0x2] * 100.0 / _counter, region_stats[0x6] * 100.0 / |
368 | _counter); |
369 | |
370 | printf("oam: %f%%\tpalette: %f%%\trom: %f%%\tother: %f%%\n", |
371 | region_stats[0x7] * 100.0 / _counter, region_stats[0x5] * 100.0 / |
372 | _counter, |
373 | rom_region_counter * 100.0 / _counter, other_region_counter * 100.0 / |
374 | _counter); |
375 | |
376 | *counter = 0; |
377 | memset(region_stats, 0, sizeof(u32) * 16); |
378 | } |
379 | |
380 | u32 event_cycles = 0; |
381 | const u32 event_cycles_trigger = 60 * 5; |
382 | u32 no_alpha = 0; |
383 | |
384 | void trigger_ext_event() |
385 | { |
386 | static u32 event_number = 0; |
387 | static u64 benchmark_ticks[16]; |
388 | u64 new_ticks; |
bbba3209 |
389 | char current_savestate_filename[512]; |
2823a4c8 |
390 | |
391 | return; |
392 | |
393 | if(event_number) |
394 | { |
395 | get_ticks_us(&new_ticks); |
396 | benchmark_ticks[event_number - 1] = |
397 | new_ticks - benchmark_ticks[event_number - 1]; |
398 | } |
399 | |
400 | current_frameskip_type = no_frameskip; |
401 | no_alpha = 0; |
402 | synchronize_flag = 0; |
403 | |
404 | get_savestate_filename_noshot(savestate_slot, |
405 | current_savestate_filename); |
406 | load_state(current_savestate_filename); |
407 | |
408 | switch(event_number) |
409 | { |
410 | case 0: |
411 | // Full benchmark, run normally |
412 | break; |
413 | |
414 | case 1: |
415 | // No alpha blending |
416 | no_alpha = 1; |
417 | break; |
418 | |
419 | case 2: |
420 | // No video benchmark |
421 | // Set frameskip really high + manual |
422 | current_frameskip_type = manual_frameskip; |
423 | frameskip_value = 1000000; |
424 | break; |
425 | |
426 | case 3: |
427 | // No CPU benchmark |
428 | // Put CPU in halt mode, put it in IRQ mode with interrupts off |
429 | reg[CPU_HALT_STATE] = CPU_HALT; |
430 | reg[REG_CPSR] = 0xD2; |
431 | break; |
432 | |
433 | case 4: |
434 | // No CPU or video benchmark |
435 | reg[CPU_HALT_STATE] = CPU_HALT; |
436 | reg[REG_CPSR] = 0xD2; |
437 | current_frameskip_type = manual_frameskip; |
438 | frameskip_value = 1000000; |
439 | break; |
440 | |
441 | case 5: |
442 | { |
443 | // Done |
444 | char *print_strings[] = |
445 | { |
446 | "Full test ", |
447 | "No blending ", |
448 | "No video ", |
449 | "No CPU ", |
450 | "No CPU/video", |
451 | "CPU speed ", |
452 | "Video speed ", |
453 | "Alpha cost " |
454 | }; |
455 | u32 i; |
456 | |
457 | benchmark_ticks[6] = benchmark_ticks[0] - benchmark_ticks[2]; |
458 | benchmark_ticks[5] = benchmark_ticks[0] - benchmark_ticks[4] - |
459 | benchmark_ticks[6]; |
460 | benchmark_ticks[7] = benchmark_ticks[0] - benchmark_ticks[1]; |
461 | |
462 | printf("Benchmark results (%d frames): \n", event_cycles_trigger); |
463 | for(i = 0; i < 8; i++) |
464 | { |
465 | printf(" %s: %d ms (%f ms per frame)\n", |
466 | print_strings[i], (u32)benchmark_ticks[i] / 1000, |
467 | (float)(benchmark_ticks[i] / (1000.0 * event_cycles_trigger))); |
468 | if(i == 4) |
469 | printf("\n"); |
470 | } |
471 | quit(); |
472 | } |
473 | } |
474 | |
475 | event_cycles = 0; |
476 | |
477 | get_ticks_us(benchmark_ticks + event_number); |
478 | event_number++; |
479 | } |
480 | |
42c81190 |
481 | static u32 fps = 60; |
482 | static u32 frames_drawn = 60; |
483 | |
2823a4c8 |
484 | u32 update_gba() |
485 | { |
486 | irq_type irq_raised = IRQ_NONE; |
487 | |
488 | do |
489 | { |
490 | cpu_ticks += execute_cycles; |
491 | |
492 | reg[CHANGED_PC_STATUS] = 0; |
493 | |
494 | if(gbc_sound_update) |
495 | { |
496 | gbc_update_count++; |
497 | update_gbc_sound(cpu_ticks); |
498 | gbc_sound_update = 0; |
499 | } |
500 | |
501 | update_timer(0); |
502 | update_timer(1); |
503 | update_timer(2); |
504 | update_timer(3); |
505 | |
506 | video_count -= execute_cycles; |
507 | |
508 | if(video_count <= 0) |
509 | { |
510 | u32 vcount = io_registers[REG_VCOUNT]; |
511 | u32 dispstat = io_registers[REG_DISPSTAT]; |
512 | |
513 | if((dispstat & 0x02) == 0) |
514 | { |
515 | // Transition from hrefresh to hblank |
516 | video_count += (272); |
517 | dispstat |= 0x02; |
518 | |
519 | if((dispstat & 0x01) == 0) |
520 | { |
521 | u32 i; |
522 | if(oam_update) |
523 | oam_update_count++; |
524 | |
525 | if(no_alpha) |
526 | io_registers[REG_BLDCNT] = 0; |
527 | update_scanline(); |
528 | |
529 | // If in visible area also fire HDMA |
530 | for(i = 0; i < 4; i++) |
531 | { |
532 | if(dma[i].start_type == DMA_START_HBLANK) |
533 | dma_transfer(dma + i); |
534 | } |
535 | } |
536 | |
537 | if(dispstat & 0x10) |
538 | irq_raised |= IRQ_HBLANK; |
539 | } |
540 | else |
541 | { |
542 | // Transition from hblank to next line |
543 | video_count += 960; |
544 | dispstat &= ~0x02; |
545 | |
546 | vcount++; |
547 | |
548 | if(vcount == 160) |
549 | { |
550 | // Transition from vrefresh to vblank |
551 | u32 i; |
552 | |
553 | dispstat |= 0x01; |
554 | if(dispstat & 0x8) |
555 | { |
556 | irq_raised |= IRQ_VBLANK; |
557 | } |
558 | |
559 | affine_reference_x[0] = |
560 | (s32)(address32(io_registers, 0x28) << 4) >> 4; |
561 | affine_reference_y[0] = |
562 | (s32)(address32(io_registers, 0x2C) << 4) >> 4; |
563 | affine_reference_x[1] = |
564 | (s32)(address32(io_registers, 0x38) << 4) >> 4; |
565 | affine_reference_y[1] = |
566 | (s32)(address32(io_registers, 0x3C) << 4) >> 4; |
567 | |
568 | for(i = 0; i < 4; i++) |
569 | { |
570 | if(dma[i].start_type == DMA_START_VBLANK) |
571 | dma_transfer(dma + i); |
572 | } |
573 | } |
574 | else |
575 | |
576 | if(vcount == 228) |
577 | { |
578 | // Transition from vblank to next screen |
579 | dispstat &= ~0x01; |
580 | frame_ticks++; |
581 | |
582 | #ifdef PC_BUILD |
d0944fc9 |
583 | /* printf("frame update (%x), %d instructions total, %d RAM flushes\n", |
2823a4c8 |
584 | reg[REG_PC], instruction_count - last_frame, flush_ram_count); |
585 | last_frame = instruction_count; |
d0944fc9 |
586 | */ |
2823a4c8 |
587 | /* printf("%d gbc audio updates\n", gbc_update_count); |
588 | printf("%d oam updates\n", oam_update_count); */ |
589 | gbc_update_count = 0; |
590 | oam_update_count = 0; |
591 | flush_ram_count = 0; |
592 | #endif |
593 | |
594 | if(update_input()) |
595 | continue; |
596 | |
597 | update_gbc_sound(cpu_ticks); |
42c81190 |
598 | |
ee0a3871 |
599 | if(fps_debug) |
42c81190 |
600 | { |
601 | char print_buffer[32]; |
43c24b30 |
602 | sprintf(print_buffer, "%2d (%2d)", fps, frames_drawn); |
42c81190 |
603 | print_string(print_buffer, 0xFFFF, 0x000, 0, 0); |
604 | } |
43c24b30 |
605 | if(!synchronize_flag) |
606 | print_string("-FF-", 0xFFFF, 0x000, 216, 0); |
2823a4c8 |
607 | |
608 | update_screen(); |
609 | |
42c81190 |
610 | synchronize(); |
611 | |
2823a4c8 |
612 | if(update_backup_flag) |
613 | update_backup(); |
614 | |
615 | process_cheats(); |
616 | |
617 | event_cycles++; |
618 | if(event_cycles == event_cycles_trigger) |
619 | { |
620 | trigger_ext_event(); |
621 | continue; |
622 | } |
623 | |
624 | vcount = 0; |
625 | } |
626 | |
627 | if(vcount == (dispstat >> 8)) |
628 | { |
629 | // vcount trigger |
630 | dispstat |= 0x04; |
631 | if(dispstat & 0x20) |
632 | { |
633 | irq_raised |= IRQ_VCOUNT; |
634 | } |
635 | } |
636 | else |
637 | { |
638 | dispstat &= ~0x04; |
639 | } |
640 | |
641 | io_registers[REG_VCOUNT] = vcount; |
642 | } |
643 | io_registers[REG_DISPSTAT] = dispstat; |
644 | } |
645 | |
646 | if(irq_raised) |
647 | raise_interrupt(irq_raised); |
648 | |
649 | execute_cycles = video_count; |
650 | |
651 | check_timer(0); |
652 | check_timer(1); |
653 | check_timer(2); |
654 | check_timer(3); |
655 | } while(reg[CPU_HALT_STATE] != CPU_ACTIVE); |
656 | |
657 | return execute_cycles; |
658 | } |
659 | |
2823a4c8 |
660 | #ifdef PSP_BUILD |
661 | |
662 | u32 real_frame_count = 0; |
663 | u32 virtual_frame_count = 0; |
664 | u32 num_skipped_frames = 0; |
665 | |
666 | void vblank_interrupt_handler(u32 sub, u32 *parg) |
667 | { |
668 | real_frame_count++; |
669 | } |
670 | |
671 | void synchronize() |
672 | { |
673 | char char_buffer[64]; |
674 | u64 new_ticks, time_delta; |
675 | s32 used_frameskip = frameskip_value; |
676 | |
677 | if(!synchronize_flag) |
678 | { |
2823a4c8 |
679 | used_frameskip = 4; |
680 | virtual_frame_count = real_frame_count - 1; |
681 | } |
682 | |
683 | skip_next_frame = 0; |
684 | |
685 | virtual_frame_count++; |
686 | |
687 | if(real_frame_count >= virtual_frame_count) |
688 | { |
689 | if((real_frame_count > virtual_frame_count) && |
690 | (current_frameskip_type == auto_frameskip) && |
691 | (num_skipped_frames < frameskip_value)) |
692 | { |
693 | skip_next_frame = 1; |
694 | num_skipped_frames++; |
695 | } |
696 | else |
697 | { |
698 | virtual_frame_count = real_frame_count; |
699 | num_skipped_frames = 0; |
700 | } |
701 | |
702 | // Here so that the home button return will eventually work. |
703 | // If it's not running fullspeed anyway this won't really hurt |
704 | // it much more. |
705 | |
706 | delay_us(1); |
707 | } |
708 | else |
709 | { |
710 | if(synchronize_flag) |
711 | sceDisplayWaitVblankStart(); |
712 | } |
713 | |
714 | if(current_frameskip_type == manual_frameskip) |
715 | { |
716 | frameskip_counter = (frameskip_counter + 1) % |
717 | (used_frameskip + 1); |
718 | if(random_skip) |
719 | { |
720 | if(frameskip_counter != (rand() % (used_frameskip + 1))) |
721 | skip_next_frame = 1; |
722 | } |
723 | else |
724 | { |
725 | if(frameskip_counter) |
726 | skip_next_frame = 1; |
727 | } |
728 | } |
729 | |
730 | /* sprintf(char_buffer, "%08d %08d %d %d %d\n", |
731 | real_frame_count, virtual_frame_count, num_skipped_frames, |
732 | real_frame_count - virtual_frame_count, skip_next_frame); |
733 | print_string(char_buffer, 0xFFFF, 0x0000, 0, 10); */ |
734 | |
735 | /* |
736 | sprintf(char_buffer, "%02d %02d %06d %07d", frameskip, (u32)ms_needed, |
737 | ram_translation_ptr - ram_translation_cache, rom_translation_ptr - |
738 | rom_translation_cache); |
739 | print_string(char_buffer, 0xFFFF, 0x0000, 0, 0); |
740 | */ |
741 | } |
742 | |
2455b6a3 |
743 | #else |
2823a4c8 |
744 | |
745 | u32 real_frame_count = 0; |
746 | u32 virtual_frame_count = 0; |
747 | u32 num_skipped_frames = 0; |
748 | u32 interval_skipped_frames; |
749 | u32 frames; |
750 | |
2823a4c8 |
751 | const u32 frame_interval = 60; |
752 | |
753 | void synchronize() |
754 | { |
755 | u64 new_ticks; |
756 | u64 time_delta; |
2823a4c8 |
757 | |
758 | get_ticks_us(&new_ticks); |
2823a4c8 |
759 | |
760 | skip_next_frame = 0; |
761 | virtual_frame_count++; |
762 | |
2455b6a3 |
763 | real_frame_count = (new_ticks * 3) / 50000; |
2823a4c8 |
764 | |
765 | if(real_frame_count >= virtual_frame_count) |
766 | { |
767 | if((real_frame_count > virtual_frame_count) && |
768 | (current_frameskip_type == auto_frameskip) && |
769 | (num_skipped_frames < frameskip_value)) |
770 | { |
771 | skip_next_frame = 1; |
772 | num_skipped_frames++; |
773 | } |
774 | else |
775 | { |
776 | virtual_frame_count = real_frame_count; |
777 | num_skipped_frames = 0; |
778 | } |
779 | } |
2455b6a3 |
780 | else if (synchronize_flag) |
781 | { |
782 | #if defined(PND_BUILD) |
783 | fb_wait_vsync(); |
784 | #elif !defined(GP2X_BUILD) // sleeping on GP2X is a bad idea |
785 | delay_us((u64)virtual_frame_count * 50000 / 3 - new_ticks + 2); |
786 | #endif |
787 | } |
2823a4c8 |
788 | |
789 | frames++; |
790 | |
791 | if(frames == frame_interval) |
792 | { |
793 | u32 new_fps; |
794 | u32 new_frames_drawn; |
795 | |
796 | time_delta = new_ticks - last_frame_interval_timestamp; |
797 | new_fps = (u64)((u64)1000000 * (u64)frame_interval) / time_delta; |
798 | new_frames_drawn = |
799 | (frame_interval - interval_skipped_frames) * (60 / frame_interval); |
800 | |
801 | // Left open for rolling averages |
802 | fps = new_fps; |
803 | frames_drawn = new_frames_drawn; |
804 | |
805 | last_frame_interval_timestamp = new_ticks; |
806 | interval_skipped_frames = 0; |
2823a4c8 |
807 | frames = 0; |
808 | } |
809 | |
810 | if(current_frameskip_type == manual_frameskip) |
811 | { |
812 | frameskip_counter = (frameskip_counter + 1) % |
813 | (frameskip_value + 1); |
814 | if(random_skip) |
815 | { |
816 | if(frameskip_counter != (rand() % (frameskip_value + 1))) |
817 | skip_next_frame = 1; |
818 | } |
819 | else |
820 | { |
821 | if(frameskip_counter) |
822 | skip_next_frame = 1; |
823 | } |
824 | } |
825 | |
826 | interval_skipped_frames += skip_next_frame; |
827 | |
2455b6a3 |
828 | #if !defined(GP2X_BUILD) && !defined(PND_BUILD) |
2823a4c8 |
829 | char char_buffer[64]; |
2455b6a3 |
830 | sprintf(char_buffer, "gpSP: %2d (%2d) fps", fps, frames_drawn); |
2823a4c8 |
831 | SDL_WM_SetCaption(char_buffer, "gpSP"); |
2455b6a3 |
832 | #endif |
2823a4c8 |
833 | } |
834 | |
835 | #endif |
836 | |
837 | void quit() |
838 | { |
839 | if(!update_backup_flag) |
840 | update_backup_force(); |
841 | |
842 | sound_exit(); |
843 | |
844 | #ifdef REGISTER_USAGE_ANALYZE |
845 | print_register_usage(); |
846 | #endif |
847 | |
848 | #ifdef PSP_BUILD |
849 | sceKernelExitGame(); |
850 | #else |
851 | SDL_Quit(); |
852 | |
43c24b30 |
853 | #ifndef PC_BUILD |
854 | gpsp_plat_quit(); |
2823a4c8 |
855 | #endif |
856 | |
857 | exit(0); |
858 | #endif |
859 | } |
860 | |
861 | void reset_gba() |
862 | { |
863 | init_main(); |
864 | init_memory(); |
865 | init_cpu(); |
866 | reset_sound(); |
867 | } |
868 | |
869 | #ifdef PSP_BUILD |
870 | |
bbba3209 |
871 | u32 file_length(char *filename, s32 dummy) |
2823a4c8 |
872 | { |
873 | SceIoStat stats; |
874 | sceIoGetstat(filename, &stats); |
875 | return stats.st_size; |
876 | } |
877 | |
878 | void delay_us(u32 us_count) |
879 | { |
880 | sceKernelDelayThread(us_count); |
881 | } |
882 | |
883 | void get_ticks_us(u64 *tick_return) |
884 | { |
885 | u64 ticks; |
886 | sceRtcGetCurrentTick(&ticks); |
887 | |
888 | *tick_return = (ticks * 1000000) / sceRtcGetTickResolution(); |
889 | } |
890 | |
891 | #else |
892 | |
bbba3209 |
893 | u32 file_length(char *dummy, FILE *fp) |
2823a4c8 |
894 | { |
895 | u32 length; |
896 | |
897 | fseek(fp, 0, SEEK_END); |
898 | length = ftell(fp); |
899 | fseek(fp, 0, SEEK_SET); |
900 | |
901 | return length; |
902 | } |
903 | |
904 | #ifdef PC_BUILD |
905 | |
906 | void delay_us(u32 us_count) |
907 | { |
908 | SDL_Delay(us_count / 1000); |
909 | } |
910 | |
911 | void get_ticks_us(u64 *ticks_return) |
912 | { |
43c24b30 |
913 | *ticks_return = (u64)SDL_GetTicks() * 1000; |
2823a4c8 |
914 | } |
915 | |
916 | #else |
917 | |
918 | void delay_us(u32 us_count) |
919 | { |
90206450 |
920 | //usleep(us_count); |
921 | SDL_Delay(us_count / 1000); |
2823a4c8 |
922 | } |
923 | |
924 | void get_ticks_us(u64 *ticks_return) |
925 | { |
926 | struct timeval current_time; |
927 | gettimeofday(¤t_time, NULL); |
928 | |
929 | *ticks_return = |
930 | (u64)current_time.tv_sec * 1000000 + current_time.tv_usec; |
931 | } |
932 | |
933 | #endif |
934 | |
935 | #endif |
936 | |
bbba3209 |
937 | void change_ext(const char *src, char *buffer, const char *extension) |
2823a4c8 |
938 | { |
bbba3209 |
939 | char *dot_position; |
2823a4c8 |
940 | strcpy(buffer, src); |
941 | dot_position = strrchr(buffer, '.'); |
942 | |
943 | if(dot_position) |
944 | strcpy(dot_position, extension); |
945 | } |
946 | |
d0944fc9 |
947 | // make path: <main_path>/<romname>.<ext> |
948 | void make_rpath(char *buff, size_t size, const char *ext) |
949 | { |
950 | char *p; |
951 | p = strrchr(gamepak_filename, PATH_SEPARATOR_CHAR); |
952 | if (p == NULL) |
953 | p = gamepak_filename; |
954 | |
955 | snprintf(buff, size, "%s/%s", main_path, p); |
956 | p = strrchr(buff, '.'); |
957 | if (p != NULL) |
958 | strcpy(p, ext); |
959 | } |
960 | |
2823a4c8 |
961 | #define main_savestate_builder(type) \ |
962 | void main_##type##_savestate(file_tag_type savestate_file) \ |
963 | { \ |
964 | file_##type##_variable(savestate_file, cpu_ticks); \ |
965 | file_##type##_variable(savestate_file, execute_cycles); \ |
966 | file_##type##_variable(savestate_file, video_count); \ |
967 | file_##type##_array(savestate_file, timer); \ |
968 | } \ |
969 | |
970 | main_savestate_builder(read); |
971 | main_savestate_builder(write_mem); |
972 | |
973 | |
974 | void printout(void *str, u32 val) |
975 | { |
976 | printf(str, val); |
977 | } |
1d02ca75 |
978 | |
979 | void set_clock_speed() |
980 | { |
981 | static u32 clock_speed_old = default_clock_speed; |
982 | if (clock_speed != clock_speed_old) |
983 | { |
984 | printf("about to set CPU clock to %iMHz\n", clock_speed); |
985 | #ifdef PSP_BUILD |
986 | scePowerSetClockFrequency(clock_speed, clock_speed, clock_speed / 2); |
987 | #elif defined(GP2X_BUILD) |
988 | set_FCLK(clock_speed); |
989 | #endif |
990 | clock_speed_old = clock_speed; |
991 | } |
992 | } |
993 | |