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