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