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