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 last_frame_interval_timestamp;
50 u32 skip_next_frame = 0;
52 u32 frameskip_counter = 0;
57 u32 execute_cycles = 960;
58 s32 video_count = 960;
65 u32 cycle_memory_access = 0;
66 u32 cycle_pc_relative_access = 0;
67 u32 cycle_sp_relative_access = 0;
68 u32 cycle_block_memory_access = 0;
69 u32 cycle_block_memory_sp_access = 0;
70 u32 cycle_block_memory_words = 0;
71 u32 cycle_dma16_words = 0;
72 u32 cycle_dma32_words = 0;
73 u32 flush_ram_count = 0;
74 u32 gbc_update_count = 0;
75 u32 oam_update_count = 0;
77 u32 synchronize_flag = 1;
79 u32 update_backup_flag = 1;
81 u32 clock_speed = 200;
83 u32 clock_speed = 333;
87 void trigger_ext_event();
89 #define check_count(count_var) \
90 if(count_var < execute_cycles) \
91 execute_cycles = count_var; \
93 #define check_timer(timer_number) \
94 if(timer[timer_number].status == TIMER_PRESCALE) \
95 check_count(timer[timer_number].count); \
97 #define update_timer(timer_number) \
98 if(timer[timer_number].status != TIMER_INACTIVE) \
100 if(timer[timer_number].status != TIMER_CASCADE) \
102 timer[timer_number].count -= execute_cycles; \
103 io_registers[REG_TM##timer_number##D] = \
104 -(timer[timer_number].count >> timer[timer_number].prescale); \
107 if(timer[timer_number].count <= 0) \
109 if(timer[timer_number].irq == TIMER_TRIGGER_IRQ) \
110 irq_raised |= IRQ_TIMER##timer_number; \
112 if((timer_number != 3) && \
113 (timer[timer_number + 1].status == TIMER_CASCADE)) \
115 timer[timer_number + 1].count--; \
116 io_registers[REG_TM0D + (timer_number + 1) * 2] = \
117 -(timer[timer_number + 1].count); \
120 if(timer_number < 2) \
122 if(timer[timer_number].direct_sound_channels & 0x01) \
123 sound_timer(timer[timer_number].frequency_step, 0); \
125 if(timer[timer_number].direct_sound_channels & 0x02) \
126 sound_timer(timer[timer_number].frequency_step, 1); \
129 timer[timer_number].count += \
130 (timer[timer_number].reload << timer[timer_number].prescale); \
134 static const char *file_ext[] = { ".gba", ".bin", ".zip", NULL };
137 static void ChangeWorkingDirectory(char *exe)
140 char *s = strrchr(exe, '/');
149 static void switch_to_romdir(void)
154 file_open(romdir_file, "romdir.txt", read);
156 if(file_check_valid(romdir_file))
158 r = file_read(romdir_file, buff, sizeof(buff) - 1);
162 while (r > 0 && isspace(buff[r-1]))
166 file_close(romdir_file);
170 static void save_romdir(void)
174 snprintf(buff, sizeof(buff), "%s" PATH_SEPARATOR "romdir.txt", main_path);
175 file_open(romdir_file, buff, write);
177 if(file_check_valid(romdir_file))
179 if (getcwd(buff, sizeof(buff)))
181 file_write(romdir_file, buff, strlen(buff));
183 file_close(romdir_file);
187 void ChangeWorkingDirectory(char *exe) {}
188 static void switch_to_romdir(void) {}
189 static void save_romdir(void) {}
198 for(i = 0; i < 4; i++)
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;
207 timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH;
208 timer[1].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
213 execute_cycles = 960;
216 flush_translation_cache_rom();
217 flush_translation_cache_ram();
218 flush_translation_cache_bios();
221 int main(int argc, char *argv[])
223 char bios_filename[512];
227 sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0,
228 vblank_interrupt_handler, NULL);
229 sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
232 init_gamepak_buffer();
234 // Copy the directory path of the executable into main_path
236 // ChangeWorkingDirectory will null out the filename out of the path
237 ChangeWorkingDirectory(argv[0]);
239 getcwd(main_path, 512);
250 gamepak_filename[0] = 0;
254 sprintf(bios_filename, "%s" PATH_SEPARATOR "%s", main_path, "gba_bios.bin");
255 ret = load_bios(bios_filename);
257 ret = load_bios("gba_bios.bin");
260 gui_action_type gui_action = CURSOR_NONE;
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");
272 debug_screen_printl("in <SD card>/pandora/appdata/gpsp/ . ");
274 debug_screen_printl("in the same directory as gpSP. ");
276 debug_screen_printl(" ");
277 debug_screen_printl("Press any button to exit. ");
279 debug_screen_update();
281 while(gui_action == CURSOR_NONE)
283 gui_action = get_gui_input();
292 if(bios_rom[0] != 0x18)
294 gui_action_type gui_action = CURSOR_NONE;
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. ");
305 debug_screen_update();
307 while(gui_action == CURSOR_NONE)
309 gui_action = get_gui_input();
321 video_resolution_large();
325 if(load_gamepak(argv[1]) == -1)
328 printf("Failed to load gamepak %s, exiting.\n", argv[1]);
333 set_gba_resolution(screen_scale);
334 video_resolution_small();
341 char load_filename[512];
343 if(load_file(file_ext, load_filename) == -1)
349 if(load_gamepak(load_filename) == -1)
352 printf("Failed to load gamepak %s, exiting.\n", load_filename);
358 set_gba_resolution(screen_scale);
359 video_resolution_small();
368 // We'll never actually return from here.
371 execute_arm_translate(execute_cycles);
374 /* u8 current_savestate_filename[512];
375 get_savestate_filename_noshot(savestate_slot,
376 current_savestate_filename);
377 load_state(current_savestate_filename); */
383 current_debug_state = COUNTDOWN_BREAKPOINT;
384 breakpoint_value = strtol(argv[2], NULL, 16);
389 execute_arm_translate(execute_cycles);
390 execute_arm(execute_cycles);
395 void print_memory_stats(u32 *counter, u32 *region_stats, char *stats_str)
397 u32 other_region_counter = region_stats[0x1] + region_stats[0xE] +
399 u32 rom_region_counter = region_stats[0x8] + region_stats[0x9] +
400 region_stats[0xA] + region_stats[0xB] + region_stats[0xC] +
402 u32 _counter = *counter;
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 /
408 region_stats[0x2] * 100.0 / _counter, region_stats[0x6] * 100.0 /
411 printf("oam: %f%%\tpalette: %f%%\trom: %f%%\tother: %f%%\n",
412 region_stats[0x7] * 100.0 / _counter, region_stats[0x5] * 100.0 /
414 rom_region_counter * 100.0 / _counter, other_region_counter * 100.0 /
418 memset(region_stats, 0, sizeof(u32) * 16);
421 u32 event_cycles = 0;
422 const u32 event_cycles_trigger = 60 * 5;
425 void trigger_ext_event()
427 static u32 event_number = 0;
428 static u64 benchmark_ticks[16];
430 char current_savestate_filename[512];
436 get_ticks_us(&new_ticks);
437 benchmark_ticks[event_number - 1] =
438 new_ticks - benchmark_ticks[event_number - 1];
441 current_frameskip_type = no_frameskip;
443 synchronize_flag = 0;
445 get_savestate_filename_noshot(savestate_slot,
446 current_savestate_filename);
447 load_state(current_savestate_filename);
452 // Full benchmark, run normally
461 // No video benchmark
462 // Set frameskip really high + manual
463 current_frameskip_type = manual_frameskip;
464 frameskip_value = 1000000;
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;
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;
485 char *print_strings[] =
498 benchmark_ticks[6] = benchmark_ticks[0] - benchmark_ticks[2];
499 benchmark_ticks[5] = benchmark_ticks[0] - benchmark_ticks[4] -
501 benchmark_ticks[7] = benchmark_ticks[0] - benchmark_ticks[1];
503 printf("Benchmark results (%d frames): \n", event_cycles_trigger);
504 for(i = 0; i < 8; i++)
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)));
518 get_ticks_us(benchmark_ticks + event_number);
523 static u32 frames_drawn = 60;
527 irq_type irq_raised = IRQ_NONE;
531 cpu_ticks += execute_cycles;
533 reg[CHANGED_PC_STATUS] = 0;
538 update_gbc_sound(cpu_ticks);
539 gbc_sound_update = 0;
547 video_count -= execute_cycles;
551 u32 vcount = io_registers[REG_VCOUNT];
552 u32 dispstat = io_registers[REG_DISPSTAT];
554 if((dispstat & 0x02) == 0)
556 // Transition from hrefresh to hblank
557 video_count += (272);
560 if((dispstat & 0x01) == 0)
567 io_registers[REG_BLDCNT] = 0;
570 // If in visible area also fire HDMA
571 for(i = 0; i < 4; i++)
573 if(dma[i].start_type == DMA_START_HBLANK)
574 dma_transfer(dma + i);
579 irq_raised |= IRQ_HBLANK;
583 // Transition from hblank to next line
591 // Transition from vrefresh to vblank
597 irq_raised |= IRQ_VBLANK;
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;
609 for(i = 0; i < 4; i++)
611 if(dma[i].start_type == DMA_START_VBLANK)
612 dma_transfer(dma + i);
619 // Transition from vblank to next screen
624 /* printf("frame update (%x), %d instructions total, %d RAM flushes\n",
625 reg[REG_PC], instruction_count - last_frame, flush_ram_count);
626 last_frame = instruction_count;
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;
638 update_gbc_sound(cpu_ticks);
642 char print_buffer[32];
643 sprintf(print_buffer, "%2d (%2d)", fps, frames_drawn);
644 print_string(print_buffer, 0xFFFF, 0x000, 0, 0);
646 if(!synchronize_flag)
647 print_string("-FF-", 0xFFFF, 0x000, 216, 0);
653 if(update_backup_flag)
659 if(event_cycles == event_cycles_trigger)
668 if(vcount == (dispstat >> 8))
674 irq_raised |= IRQ_VCOUNT;
682 io_registers[REG_VCOUNT] = vcount;
684 io_registers[REG_DISPSTAT] = dispstat;
688 raise_interrupt(irq_raised);
690 execute_cycles = video_count;
696 } while(reg[CPU_HALT_STATE] != CPU_ACTIVE);
698 return execute_cycles;
703 u32 real_frame_count = 0;
704 u32 virtual_frame_count = 0;
705 u32 num_skipped_frames = 0;
707 void vblank_interrupt_handler(u32 sub, u32 *parg)
714 char char_buffer[64];
715 u64 new_ticks, time_delta;
716 s32 used_frameskip = frameskip_value;
718 if(!synchronize_flag)
721 virtual_frame_count = real_frame_count - 1;
726 virtual_frame_count++;
728 if(real_frame_count >= virtual_frame_count)
730 if((real_frame_count > virtual_frame_count) &&
731 (current_frameskip_type == auto_frameskip) &&
732 (num_skipped_frames < frameskip_value))
735 num_skipped_frames++;
739 virtual_frame_count = real_frame_count;
740 num_skipped_frames = 0;
743 // Here so that the home button return will eventually work.
744 // If it's not running fullspeed anyway this won't really hurt
752 sceDisplayWaitVblankStart();
755 if(current_frameskip_type == manual_frameskip)
757 frameskip_counter = (frameskip_counter + 1) %
758 (used_frameskip + 1);
761 if(frameskip_counter != (rand() % (used_frameskip + 1)))
766 if(frameskip_counter)
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); */
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);
786 u32 real_frame_count = 0;
787 u32 virtual_frame_count = 0;
788 u32 num_skipped_frames = 0;
789 u32 interval_skipped_frames;
792 const u32 frame_interval = 60;
799 get_ticks_us(&new_ticks);
802 virtual_frame_count++;
804 real_frame_count = (new_ticks * 3) / 50000;
806 if(real_frame_count >= virtual_frame_count)
808 if((real_frame_count > virtual_frame_count) &&
809 (current_frameskip_type == auto_frameskip) &&
810 (num_skipped_frames < frameskip_value))
813 num_skipped_frames++;
817 virtual_frame_count = real_frame_count;
818 num_skipped_frames = 0;
821 else if (synchronize_flag)
823 #if defined(PND_BUILD)
825 #elif !defined(GP2X_BUILD) // sleeping on GP2X is a bad idea
826 delay_us((u64)virtual_frame_count * 50000 / 3 - new_ticks + 2);
832 if(frames == frame_interval)
835 u32 new_frames_drawn;
837 time_delta = new_ticks - last_frame_interval_timestamp;
838 new_fps = (u64)((u64)1000000 * (u64)frame_interval) / time_delta;
840 (frame_interval - interval_skipped_frames) * (60 / frame_interval);
842 // Left open for rolling averages
844 frames_drawn = new_frames_drawn;
846 last_frame_interval_timestamp = new_ticks;
847 interval_skipped_frames = 0;
851 if(current_frameskip_type == manual_frameskip)
853 frameskip_counter = (frameskip_counter + 1) %
854 (frameskip_value + 1);
857 if(frameskip_counter != (rand() % (frameskip_value + 1)))
862 if(frameskip_counter)
867 interval_skipped_frames += skip_next_frame;
869 #if !defined(GP2X_BUILD) && !defined(PND_BUILD)
870 char char_buffer[64];
871 sprintf(char_buffer, "gpSP: %2d (%2d) fps", fps, frames_drawn);
872 SDL_WM_SetCaption(char_buffer, "gpSP");
882 if(!update_backup_flag)
883 update_backup_force();
887 #ifdef REGISTER_USAGE_ANALYZE
888 print_register_usage();
914 u32 file_length(char *filename, s32 dummy)
917 sceIoGetstat(filename, &stats);
918 return stats.st_size;
921 void delay_us(u32 us_count)
923 sceKernelDelayThread(us_count);
926 void get_ticks_us(u64 *tick_return)
929 sceRtcGetCurrentTick(&ticks);
931 *tick_return = (ticks * 1000000) / sceRtcGetTickResolution();
936 u32 file_length(char *dummy, FILE *fp)
940 fseek(fp, 0, SEEK_END);
942 fseek(fp, 0, SEEK_SET);
949 void delay_us(u32 us_count)
951 SDL_Delay(us_count / 1000);
954 void get_ticks_us(u64 *ticks_return)
956 *ticks_return = (u64)SDL_GetTicks() * 1000;
961 void delay_us(u32 us_count)
964 SDL_Delay(us_count / 1000);
967 void get_ticks_us(u64 *ticks_return)
969 struct timeval current_time;
970 gettimeofday(¤t_time, NULL);
973 (u64)current_time.tv_sec * 1000000 + current_time.tv_usec;
980 void change_ext(const char *src, char *buffer, const char *extension)
984 dot_position = strrchr(buffer, '.');
987 strcpy(dot_position, extension);
990 // make path: <main_path>/<romname>.<ext>
991 void make_rpath(char *buff, size_t size, const char *ext)
994 p = strrchr(gamepak_filename, PATH_SEPARATOR_CHAR);
996 p = gamepak_filename;
998 snprintf(buff, size, "%s/%s", main_path, p);
999 p = strrchr(buff, '.');
1004 #define main_savestate_builder(type) \
1005 void main_##type##_savestate(file_tag_type savestate_file) \
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); \
1013 main_savestate_builder(read);
1014 main_savestate_builder(write_mem);
1017 void printout(void *str, u32 val)
1022 void set_clock_speed()
1024 static u32 clock_speed_old = default_clock_speed;
1025 if (clock_speed != clock_speed_old)
1027 printf("about to set CPU clock to %iMHz\n", clock_speed);
1029 scePowerSetClockFrequency(clock_speed, clock_speed, clock_speed / 2);
1030 #elif defined(GP2X_BUILD)
1031 set_FCLK(clock_speed);
1033 clock_speed_old = clock_speed;