3 * Copyright (C) 2006 Exophase <exophase@gmail.com>
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.
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.
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
24 //PSP_MODULE_INFO("gpSP", 0x1000, 0, 6);
25 //PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
27 void vblank_interrupt_handler(u32 sub, u32 *parg);
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;
39 //u32 breakpoint_value = 0;
41 frameskip_type current_frameskip_type = auto_frameskip;
42 u32 global_cycles_per_instruction = 1;
46 u32 frameskip_value = 2;
48 u64 frame_count_initial_timestamp = 0;
49 u64 last_frame_interval_timestamp;
50 u32 gp2x_fps_debug = 0;
56 u32 frameskip_value = 4;
58 u32 skip_next_frame = 0;
60 u32 frameskip_counter = 0;
65 u32 execute_cycles = 960;
66 s32 video_count = 960;
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;
85 u32 synchronize_flag = 1;
87 u32 update_backup_flag = 1;
89 u32 clock_speed = 200;
91 u32 clock_speed = 333;
95 void trigger_ext_event();
97 #define check_count(count_var) \
98 if(count_var < execute_cycles) \
99 execute_cycles = count_var; \
101 #define check_timer(timer_number) \
102 if(timer[timer_number].status == TIMER_PRESCALE) \
103 check_count(timer[timer_number].count); \
105 #define update_timer(timer_number) \
106 if(timer[timer_number].status != TIMER_INACTIVE) \
108 if(timer[timer_number].status != TIMER_CASCADE) \
110 timer[timer_number].count -= execute_cycles; \
111 io_registers[REG_TM##timer_number##D] = \
112 -(timer[timer_number].count >> timer[timer_number].prescale); \
115 if(timer[timer_number].count <= 0) \
117 if(timer[timer_number].irq == TIMER_TRIGGER_IRQ) \
118 irq_raised |= IRQ_TIMER##timer_number; \
120 if((timer_number != 3) && \
121 (timer[timer_number + 1].status == TIMER_CASCADE)) \
123 timer[timer_number + 1].count--; \
124 io_registers[REG_TM0D + (timer_number + 1) * 2] = \
125 -(timer[timer_number + 1].count); \
128 if(timer_number < 2) \
130 if(timer[timer_number].direct_sound_channels & 0x01) \
131 sound_timer(timer[timer_number].frequency_step, 0); \
133 if(timer[timer_number].direct_sound_channels & 0x02) \
134 sound_timer(timer[timer_number].frequency_step, 1); \
137 timer[timer_number].count += \
138 (timer[timer_number].reload << timer[timer_number].prescale); \
142 u8 *file_ext[] = { ".gba", ".bin", ".zip", NULL };
145 void ChangeWorkingDirectory(char *exe)
148 char *s = strrchr(exe, '/');
164 for(i = 0; i < 4; i++)
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;
173 timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH;
174 timer[1].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
179 execute_cycles = 960;
182 flush_translation_cache_rom();
183 flush_translation_cache_ram();
184 flush_translation_cache_bios();
187 int main(int argc, char *argv[])
193 u8 load_filename[512];
194 u8 bios_filename[512];
197 sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0,
198 vblank_interrupt_handler, NULL);
199 sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
200 #elif !defined(GP2X_BUILD)
201 freopen("CON", "wb", stdout);
204 extern char *cpu_mode_names[];
206 init_gamepak_buffer();
208 // Copy the directory path of the executable into main_path
211 // ChangeWorkingDirectory will null out the filename out of the path
212 ChangeWorkingDirectory(argv[0]);
215 getcwd(main_path, 512);
218 gamepak_filename[0] = 0;
225 // Overclocking GP2X and MMU patch goes here
232 sprintf(bios_filename, "%s/%s", main_path, "gba_bios.bin");
233 if(load_bios(bios_filename) == -1)
235 if(load_bios("gba_bios.bin") == -1)
238 gui_action_type gui_action = CURSOR_NONE;
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. ");
253 debug_screen_update();
255 while(gui_action == CURSOR_NONE)
257 gui_action = get_gui_input();
266 if(bios_rom[0] != 0x18)
268 gui_action_type gui_action = CURSOR_NONE;
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. ");
279 debug_screen_update();
281 while(gui_action == CURSOR_NONE)
283 gui_action = get_gui_input();
295 video_resolution_large();
299 if(load_gamepak(argv[1]) == -1)
302 printf("Failed to load gamepak %s, exiting.\n", load_filename);
307 set_gba_resolution(screen_scale);
308 video_resolution_small();
315 if(load_file(file_ext, load_filename) == -1)
321 if(load_gamepak(load_filename) == -1)
324 printf("Failed to load gamepak %s, exiting.\n", load_filename);
329 set_gba_resolution(screen_scale);
330 video_resolution_small();
339 // We'll never actually return from here.
342 execute_arm_translate(execute_cycles);
346 get_ticks_us(&frame_count_initial_timestamp);
349 /* u8 current_savestate_filename[512];
350 get_savestate_filename_noshot(savestate_slot,
351 current_savestate_filename);
352 load_state(current_savestate_filename); */
358 current_debug_state = COUNTDOWN_BREAKPOINT;
359 breakpoint_value = strtol(argv[2], NULL, 16);
364 execute_arm_translate(execute_cycles);
365 execute_arm(execute_cycles);
370 void print_memory_stats(u32 *counter, u32 *region_stats, char *stats_str)
372 u32 other_region_counter = region_stats[0x1] + region_stats[0xE] +
374 u32 rom_region_counter = region_stats[0x8] + region_stats[0x9] +
375 region_stats[0xA] + region_stats[0xB] + region_stats[0xC] +
377 u32 _counter = *counter;
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 /
383 region_stats[0x2] * 100.0 / _counter, region_stats[0x6] * 100.0 /
386 printf("oam: %f%%\tpalette: %f%%\trom: %f%%\tother: %f%%\n",
387 region_stats[0x7] * 100.0 / _counter, region_stats[0x5] * 100.0 /
389 rom_region_counter * 100.0 / _counter, other_region_counter * 100.0 /
393 memset(region_stats, 0, sizeof(u32) * 16);
396 u32 event_cycles = 0;
397 const u32 event_cycles_trigger = 60 * 5;
400 void trigger_ext_event()
402 static u32 event_number = 0;
403 static u64 benchmark_ticks[16];
405 u8 current_savestate_filename[512];
411 get_ticks_us(&new_ticks);
412 benchmark_ticks[event_number - 1] =
413 new_ticks - benchmark_ticks[event_number - 1];
416 current_frameskip_type = no_frameskip;
418 synchronize_flag = 0;
420 get_savestate_filename_noshot(savestate_slot,
421 current_savestate_filename);
422 load_state(current_savestate_filename);
427 // Full benchmark, run normally
436 // No video benchmark
437 // Set frameskip really high + manual
438 current_frameskip_type = manual_frameskip;
439 frameskip_value = 1000000;
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;
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;
460 char *print_strings[] =
473 benchmark_ticks[6] = benchmark_ticks[0] - benchmark_ticks[2];
474 benchmark_ticks[5] = benchmark_ticks[0] - benchmark_ticks[4] -
476 benchmark_ticks[7] = benchmark_ticks[0] - benchmark_ticks[1];
478 printf("Benchmark results (%d frames): \n", event_cycles_trigger);
479 for(i = 0; i < 8; i++)
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)));
493 get_ticks_us(benchmark_ticks + event_number);
498 static u32 frames_drawn = 60;
502 irq_type irq_raised = IRQ_NONE;
506 cpu_ticks += execute_cycles;
508 reg[CHANGED_PC_STATUS] = 0;
513 update_gbc_sound(cpu_ticks);
514 gbc_sound_update = 0;
522 video_count -= execute_cycles;
526 u32 vcount = io_registers[REG_VCOUNT];
527 u32 dispstat = io_registers[REG_DISPSTAT];
529 if((dispstat & 0x02) == 0)
531 // Transition from hrefresh to hblank
532 video_count += (272);
535 if((dispstat & 0x01) == 0)
542 io_registers[REG_BLDCNT] = 0;
545 // If in visible area also fire HDMA
546 for(i = 0; i < 4; i++)
548 if(dma[i].start_type == DMA_START_HBLANK)
549 dma_transfer(dma + i);
554 irq_raised |= IRQ_HBLANK;
558 // Transition from hblank to next line
566 // Transition from vrefresh to vblank
572 irq_raised |= IRQ_VBLANK;
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;
584 for(i = 0; i < 4; i++)
586 if(dma[i].start_type == DMA_START_VBLANK)
587 dma_transfer(dma + i);
594 // Transition from vblank to next screen
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;
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;
613 update_gbc_sound(cpu_ticks);
617 char print_buffer[32];
618 sprintf(print_buffer, "%d (%d)", fps, frames_drawn);
619 print_string(print_buffer, 0xFFFF, 0x000, 0, 0);
626 if(update_backup_flag)
632 if(event_cycles == event_cycles_trigger)
641 if(vcount == (dispstat >> 8))
647 irq_raised |= IRQ_VCOUNT;
655 io_registers[REG_VCOUNT] = vcount;
657 io_registers[REG_DISPSTAT] = dispstat;
661 raise_interrupt(irq_raised);
663 execute_cycles = video_count;
669 } while(reg[CPU_HALT_STATE] != CPU_ACTIVE);
671 return execute_cycles;
674 u64 last_screen_timestamp = 0;
675 u32 frame_speed = 15000;
680 u32 real_frame_count = 0;
681 u32 virtual_frame_count = 0;
682 u32 num_skipped_frames = 0;
684 void vblank_interrupt_handler(u32 sub, u32 *parg)
691 char char_buffer[64];
692 u64 new_ticks, time_delta;
693 s32 used_frameskip = frameskip_value;
695 if(!synchronize_flag)
697 print_string("--FF--", 0xFFFF, 0x000, 0, 0);
699 virtual_frame_count = real_frame_count - 1;
704 virtual_frame_count++;
706 if(real_frame_count >= virtual_frame_count)
708 if((real_frame_count > virtual_frame_count) &&
709 (current_frameskip_type == auto_frameskip) &&
710 (num_skipped_frames < frameskip_value))
713 num_skipped_frames++;
717 virtual_frame_count = real_frame_count;
718 num_skipped_frames = 0;
721 // Here so that the home button return will eventually work.
722 // If it's not running fullspeed anyway this won't really hurt
730 sceDisplayWaitVblankStart();
733 if(current_frameskip_type == manual_frameskip)
735 frameskip_counter = (frameskip_counter + 1) %
736 (used_frameskip + 1);
739 if(frameskip_counter != (rand() % (used_frameskip + 1)))
744 if(frameskip_counter)
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); */
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);
766 u32 real_frame_count = 0;
767 u32 virtual_frame_count = 0;
768 u32 num_skipped_frames = 0;
769 u32 interval_skipped_frames;
772 const u32 frame_interval = 60;
779 get_ticks_us(&new_ticks);
780 time_delta = new_ticks - last_screen_timestamp;
781 last_screen_timestamp = new_ticks;
784 virtual_frame_count++;
786 real_frame_count = ((new_ticks -
787 frame_count_initial_timestamp) * 3) / 50000;
789 if(real_frame_count >= virtual_frame_count)
791 if((real_frame_count > virtual_frame_count) &&
792 (current_frameskip_type == auto_frameskip) &&
793 (num_skipped_frames < frameskip_value))
796 num_skipped_frames++;
800 virtual_frame_count = real_frame_count;
801 num_skipped_frames = 0;
807 if(frames == frame_interval)
810 u32 new_frames_drawn;
812 time_delta = new_ticks - last_frame_interval_timestamp;
813 new_fps = (u64)((u64)1000000 * (u64)frame_interval) / time_delta;
815 (frame_interval - interval_skipped_frames) * (60 / frame_interval);
817 // Left open for rolling averages
819 frames_drawn = new_frames_drawn;
821 last_frame_interval_timestamp = new_ticks;
822 interval_skipped_frames = 0;
826 if(current_frameskip_type == manual_frameskip)
828 frameskip_counter = (frameskip_counter + 1) %
829 (frameskip_value + 1);
832 if(frameskip_counter != (rand() % (frameskip_value + 1)))
837 if(frameskip_counter)
842 interval_skipped_frames += skip_next_frame;
844 if(!synchronize_flag)
845 print_string("--FF--", 0xFFFF, 0x000, 0, 0);
853 u32 ticks_needed_total = 0;
854 float us_needed = 0.0;
856 const u32 frame_interval = 60;
862 char char_buffer[64];
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;
871 if((time_delta < frame_speed) && synchronize_flag)
873 delay_us(frame_speed - time_delta);
878 if(frames == frame_interval)
880 us_needed = (float)ticks_needed_total / frame_interval;
881 ticks_needed_total = 0;
885 if(current_frameskip_type == manual_frameskip)
887 frameskip_counter = (frameskip_counter + 1) %
888 (frameskip_value + 1);
891 if(frameskip_counter != (rand() % (frameskip_value + 1)))
896 if(frameskip_counter)
901 if(synchronize_flag == 0)
902 print_string("--FF--", 0xFFFF, 0x000, 0, 0);
904 sprintf(char_buffer, "gpSP: %.1fms %.1ffps", us_needed / 1000.0,
905 1000000.0 / us_needed);
906 SDL_WM_SetCaption(char_buffer, "gpSP");
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);
920 if(!update_backup_flag)
921 update_backup_force();
925 #ifdef REGISTER_USAGE_ANALYZE
926 print_register_usage();
952 u32 file_length(u8 *filename, s32 dummy)
955 sceIoGetstat(filename, &stats);
956 return stats.st_size;
959 void delay_us(u32 us_count)
961 sceKernelDelayThread(us_count);
964 void get_ticks_us(u64 *tick_return)
967 sceRtcGetCurrentTick(&ticks);
969 *tick_return = (ticks * 1000000) / sceRtcGetTickResolution();
974 u32 file_length(u8 *dummy, FILE *fp)
978 fseek(fp, 0, SEEK_END);
980 fseek(fp, 0, SEEK_SET);
987 void delay_us(u32 us_count)
989 SDL_Delay(us_count / 1000);
992 void get_ticks_us(u64 *ticks_return)
994 *ticks_return = (SDL_GetTicks() * 1000);
999 void delay_us(u32 us_count)
1002 SDL_Delay(us_count / 1000);
1005 void get_ticks_us(u64 *ticks_return)
1007 struct timeval current_time;
1008 gettimeofday(¤t_time, NULL);
1011 (u64)current_time.tv_sec * 1000000 + current_time.tv_usec;
1018 void change_ext(u8 *src, u8 *buffer, u8 *extension)
1021 strcpy(buffer, src);
1022 dot_position = strrchr(buffer, '.');
1025 strcpy(dot_position, extension);
1028 #define main_savestate_builder(type) \
1029 void main_##type##_savestate(file_tag_type savestate_file) \
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); \
1037 main_savestate_builder(read);
1038 main_savestate_builder(write_mem);
1041 void printout(void *str, u32 val)