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