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