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)
175 snprintf(buff, sizeof(buff), "%s" PATH_SEPARATOR "romdir.txt", main_path);
176 file_open(romdir_file, buff, write);
178 if(file_check_valid(romdir_file))
180 if (getcwd(buff, sizeof(buff)))
182 file_write(romdir_file, buff, strlen(buff));
184 file_close(romdir_file);
188 void ChangeWorkingDirectory(char *exe) {}
189 static void switch_to_romdir(void) {}
190 static void save_romdir(void) {}
199 for(i = 0; i < 4; i++)
201 dma[i].start_type = DMA_INACTIVE;
202 dma[i].direct_sound_channel = DMA_NO_DIRECT_SOUND;
203 timer[i].status = TIMER_INACTIVE;
204 timer[i].reload = 0x10000;
205 timer[i].stop_cpu_ticks = 0;
208 timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH;
209 timer[1].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
214 execute_cycles = 960;
217 flush_translation_cache_rom();
218 flush_translation_cache_ram();
219 flush_translation_cache_bios();
222 int main(int argc, char *argv[])
224 char bios_filename[512];
228 sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0,
229 vblank_interrupt_handler, NULL);
230 sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
233 init_gamepak_buffer();
235 // Copy the directory path of the executable into main_path
237 // ChangeWorkingDirectory will null out the filename out of the path
238 ChangeWorkingDirectory(argv[0]);
240 getcwd(main_path, 512);
251 gamepak_filename[0] = 0;
255 sprintf(bios_filename, "%s" PATH_SEPARATOR "%s", main_path, "gba_bios.bin");
256 ret = load_bios(bios_filename);
258 ret = load_bios("gba_bios.bin");
261 gui_action_type gui_action = CURSOR_NONE;
263 debug_screen_start();
264 debug_screen_printl("Sorry, but gpSP requires a Gameboy Advance BIOS ");
265 debug_screen_printl("image to run correctly. Make sure to get an ");
266 debug_screen_printl("authentic one, it'll be exactly 16384 bytes large ");
267 debug_screen_printl("and should have the following md5sum value: ");
268 debug_screen_printl(" ");
269 debug_screen_printl("a860e8c0b6d573d191e4ec7db1b1e4f6 ");
270 debug_screen_printl(" ");
271 debug_screen_printl("When you do get it name it gba_bios.bin and put it");
273 debug_screen_printl("in <CD card>/pandora/appdata/gpsp/ . ");
275 debug_screen_printl("in the same directory as gpSP. ");
277 debug_screen_printl(" ");
278 debug_screen_printl("Press any button to exit. ");
280 debug_screen_update();
282 while(gui_action == CURSOR_NONE)
284 gui_action = get_gui_input();
293 if(bios_rom[0] != 0x18)
295 gui_action_type gui_action = CURSOR_NONE;
297 debug_screen_start();
298 debug_screen_printl("You have an incorrect BIOS image. ");
299 debug_screen_printl("While many games will work fine, some will not. It");
300 debug_screen_printl("is strongly recommended that you obtain the ");
301 debug_screen_printl("correct BIOS file. Do NOT report any bugs if you ");
302 debug_screen_printl("are seeing this message. ");
303 debug_screen_printl(" ");
304 debug_screen_printl("Press any button to resume, at your own risk. ");
306 debug_screen_update();
308 while(gui_action == CURSOR_NONE)
310 gui_action = get_gui_input();
322 video_resolution_large();
327 if(load_gamepak(argv[1]) == -1)
330 printf("Failed to load gamepak %s, exiting.\n", argv[1]);
335 set_gba_resolution(screen_scale);
336 video_resolution_small();
343 char load_filename[512];
344 if(load_file(file_ext, load_filename) == -1)
350 if(load_gamepak(load_filename) == -1)
353 printf("Failed to load gamepak %s, exiting.\n", load_filename);
359 set_gba_resolution(screen_scale);
360 video_resolution_small();
369 // We'll never actually return from here.
372 execute_arm_translate(execute_cycles);
375 /* u8 current_savestate_filename[512];
376 get_savestate_filename_noshot(savestate_slot,
377 current_savestate_filename);
378 load_state(current_savestate_filename); */
384 current_debug_state = COUNTDOWN_BREAKPOINT;
385 breakpoint_value = strtol(argv[2], NULL, 16);
390 execute_arm_translate(execute_cycles);
391 execute_arm(execute_cycles);
396 void print_memory_stats(u32 *counter, u32 *region_stats, char *stats_str)
398 u32 other_region_counter = region_stats[0x1] + region_stats[0xE] +
400 u32 rom_region_counter = region_stats[0x8] + region_stats[0x9] +
401 region_stats[0xA] + region_stats[0xB] + region_stats[0xC] +
403 u32 _counter = *counter;
405 printf("memory access stats: %s (out of %d)\n", stats_str, _counter);
406 printf("bios: %f%%\tiwram: %f%%\tewram: %f%%\tvram: %f\n",
407 region_stats[0x0] * 100.0 / _counter, region_stats[0x3] * 100.0 /
409 region_stats[0x2] * 100.0 / _counter, region_stats[0x6] * 100.0 /
412 printf("oam: %f%%\tpalette: %f%%\trom: %f%%\tother: %f%%\n",
413 region_stats[0x7] * 100.0 / _counter, region_stats[0x5] * 100.0 /
415 rom_region_counter * 100.0 / _counter, other_region_counter * 100.0 /
419 memset(region_stats, 0, sizeof(u32) * 16);
422 u32 event_cycles = 0;
423 const u32 event_cycles_trigger = 60 * 5;
426 void trigger_ext_event()
428 static u32 event_number = 0;
429 static u64 benchmark_ticks[16];
431 char current_savestate_filename[512];
437 get_ticks_us(&new_ticks);
438 benchmark_ticks[event_number - 1] =
439 new_ticks - benchmark_ticks[event_number - 1];
442 current_frameskip_type = no_frameskip;
444 synchronize_flag = 0;
446 get_savestate_filename_noshot(savestate_slot,
447 current_savestate_filename);
448 load_state(current_savestate_filename);
453 // Full benchmark, run normally
462 // No video benchmark
463 // Set frameskip really high + manual
464 current_frameskip_type = manual_frameskip;
465 frameskip_value = 1000000;
470 // Put CPU in halt mode, put it in IRQ mode with interrupts off
471 reg[CPU_HALT_STATE] = CPU_HALT;
472 reg[REG_CPSR] = 0xD2;
476 // No CPU or video benchmark
477 reg[CPU_HALT_STATE] = CPU_HALT;
478 reg[REG_CPSR] = 0xD2;
479 current_frameskip_type = manual_frameskip;
480 frameskip_value = 1000000;
486 char *print_strings[] =
499 benchmark_ticks[6] = benchmark_ticks[0] - benchmark_ticks[2];
500 benchmark_ticks[5] = benchmark_ticks[0] - benchmark_ticks[4] -
502 benchmark_ticks[7] = benchmark_ticks[0] - benchmark_ticks[1];
504 printf("Benchmark results (%d frames): \n", event_cycles_trigger);
505 for(i = 0; i < 8; i++)
507 printf(" %s: %d ms (%f ms per frame)\n",
508 print_strings[i], (u32)benchmark_ticks[i] / 1000,
509 (float)(benchmark_ticks[i] / (1000.0 * event_cycles_trigger)));
519 get_ticks_us(benchmark_ticks + event_number);
524 static u32 frames_drawn = 60;
528 irq_type irq_raised = IRQ_NONE;
532 cpu_ticks += execute_cycles;
534 reg[CHANGED_PC_STATUS] = 0;
539 update_gbc_sound(cpu_ticks);
540 gbc_sound_update = 0;
548 video_count -= execute_cycles;
552 u32 vcount = io_registers[REG_VCOUNT];
553 u32 dispstat = io_registers[REG_DISPSTAT];
555 if((dispstat & 0x02) == 0)
557 // Transition from hrefresh to hblank
558 video_count += (272);
561 if((dispstat & 0x01) == 0)
568 io_registers[REG_BLDCNT] = 0;
571 // If in visible area also fire HDMA
572 for(i = 0; i < 4; i++)
574 if(dma[i].start_type == DMA_START_HBLANK)
575 dma_transfer(dma + i);
580 irq_raised |= IRQ_HBLANK;
584 // Transition from hblank to next line
592 // Transition from vrefresh to vblank
598 irq_raised |= IRQ_VBLANK;
601 affine_reference_x[0] =
602 (s32)(address32(io_registers, 0x28) << 4) >> 4;
603 affine_reference_y[0] =
604 (s32)(address32(io_registers, 0x2C) << 4) >> 4;
605 affine_reference_x[1] =
606 (s32)(address32(io_registers, 0x38) << 4) >> 4;
607 affine_reference_y[1] =
608 (s32)(address32(io_registers, 0x3C) << 4) >> 4;
610 for(i = 0; i < 4; i++)
612 if(dma[i].start_type == DMA_START_VBLANK)
613 dma_transfer(dma + i);
620 // Transition from vblank to next screen
625 /* printf("frame update (%x), %d instructions total, %d RAM flushes\n",
626 reg[REG_PC], instruction_count - last_frame, flush_ram_count);
627 last_frame = instruction_count;
629 /* printf("%d gbc audio updates\n", gbc_update_count);
630 printf("%d oam updates\n", oam_update_count); */
631 gbc_update_count = 0;
632 oam_update_count = 0;
639 update_gbc_sound(cpu_ticks);
643 char print_buffer[32];
644 sprintf(print_buffer, "%2d (%2d)", fps, frames_drawn);
645 print_string(print_buffer, 0xFFFF, 0x000, 0, 0);
647 if(!synchronize_flag)
648 print_string("-FF-", 0xFFFF, 0x000, 216, 0);
654 if(update_backup_flag)
660 if(event_cycles == event_cycles_trigger)
669 if(vcount == (dispstat >> 8))
675 irq_raised |= IRQ_VCOUNT;
683 io_registers[REG_VCOUNT] = vcount;
685 io_registers[REG_DISPSTAT] = dispstat;
689 raise_interrupt(irq_raised);
691 execute_cycles = video_count;
697 } while(reg[CPU_HALT_STATE] != CPU_ACTIVE);
699 return execute_cycles;
704 u32 real_frame_count = 0;
705 u32 virtual_frame_count = 0;
706 u32 num_skipped_frames = 0;
708 void vblank_interrupt_handler(u32 sub, u32 *parg)
715 char char_buffer[64];
716 u64 new_ticks, time_delta;
717 s32 used_frameskip = frameskip_value;
719 if(!synchronize_flag)
722 virtual_frame_count = real_frame_count - 1;
727 virtual_frame_count++;
729 if(real_frame_count >= virtual_frame_count)
731 if((real_frame_count > virtual_frame_count) &&
732 (current_frameskip_type == auto_frameskip) &&
733 (num_skipped_frames < frameskip_value))
736 num_skipped_frames++;
740 virtual_frame_count = real_frame_count;
741 num_skipped_frames = 0;
744 // Here so that the home button return will eventually work.
745 // If it's not running fullspeed anyway this won't really hurt
753 sceDisplayWaitVblankStart();
756 if(current_frameskip_type == manual_frameskip)
758 frameskip_counter = (frameskip_counter + 1) %
759 (used_frameskip + 1);
762 if(frameskip_counter != (rand() % (used_frameskip + 1)))
767 if(frameskip_counter)
772 /* sprintf(char_buffer, "%08d %08d %d %d %d\n",
773 real_frame_count, virtual_frame_count, num_skipped_frames,
774 real_frame_count - virtual_frame_count, skip_next_frame);
775 print_string(char_buffer, 0xFFFF, 0x0000, 0, 10); */
778 sprintf(char_buffer, "%02d %02d %06d %07d", frameskip, (u32)ms_needed,
779 ram_translation_ptr - ram_translation_cache, rom_translation_ptr -
780 rom_translation_cache);
781 print_string(char_buffer, 0xFFFF, 0x0000, 0, 0);
787 u32 real_frame_count = 0;
788 u32 virtual_frame_count = 0;
789 u32 num_skipped_frames = 0;
790 u32 interval_skipped_frames;
793 const u32 frame_interval = 60;
800 get_ticks_us(&new_ticks);
803 virtual_frame_count++;
805 real_frame_count = (new_ticks * 3) / 50000;
807 if(real_frame_count >= virtual_frame_count)
809 if((real_frame_count > virtual_frame_count) &&
810 (current_frameskip_type == auto_frameskip) &&
811 (num_skipped_frames < frameskip_value))
814 num_skipped_frames++;
818 virtual_frame_count = real_frame_count;
819 num_skipped_frames = 0;
822 else if (synchronize_flag)
824 #if defined(PND_BUILD)
826 #elif !defined(GP2X_BUILD) // sleeping on GP2X is a bad idea
827 delay_us((u64)virtual_frame_count * 50000 / 3 - new_ticks + 2);
833 if(frames == frame_interval)
836 u32 new_frames_drawn;
838 time_delta = new_ticks - last_frame_interval_timestamp;
839 new_fps = (u64)((u64)1000000 * (u64)frame_interval) / time_delta;
841 (frame_interval - interval_skipped_frames) * (60 / frame_interval);
843 // Left open for rolling averages
845 frames_drawn = new_frames_drawn;
847 last_frame_interval_timestamp = new_ticks;
848 interval_skipped_frames = 0;
852 if(current_frameskip_type == manual_frameskip)
854 frameskip_counter = (frameskip_counter + 1) %
855 (frameskip_value + 1);
858 if(frameskip_counter != (rand() % (frameskip_value + 1)))
863 if(frameskip_counter)
868 interval_skipped_frames += skip_next_frame;
870 #if !defined(GP2X_BUILD) && !defined(PND_BUILD)
871 char char_buffer[64];
872 sprintf(char_buffer, "gpSP: %2d (%2d) fps", fps, frames_drawn);
873 SDL_WM_SetCaption(char_buffer, "gpSP");
883 if(!update_backup_flag)
884 update_backup_force();
888 #ifdef REGISTER_USAGE_ANALYZE
889 print_register_usage();
915 u32 file_length(char *filename, s32 dummy)
918 sceIoGetstat(filename, &stats);
919 return stats.st_size;
922 void delay_us(u32 us_count)
924 sceKernelDelayThread(us_count);
927 void get_ticks_us(u64 *tick_return)
930 sceRtcGetCurrentTick(&ticks);
932 *tick_return = (ticks * 1000000) / sceRtcGetTickResolution();
937 u32 file_length(char *dummy, FILE *fp)
941 fseek(fp, 0, SEEK_END);
943 fseek(fp, 0, SEEK_SET);
950 void delay_us(u32 us_count)
952 SDL_Delay(us_count / 1000);
955 void get_ticks_us(u64 *ticks_return)
957 *ticks_return = (u64)SDL_GetTicks() * 1000;
962 void delay_us(u32 us_count)
965 SDL_Delay(us_count / 1000);
968 void get_ticks_us(u64 *ticks_return)
970 struct timeval current_time;
971 gettimeofday(¤t_time, NULL);
974 (u64)current_time.tv_sec * 1000000 + current_time.tv_usec;
981 void change_ext(const char *src, char *buffer, const char *extension)
985 dot_position = strrchr(buffer, '.');
988 strcpy(dot_position, extension);
991 // make path: <main_path>/<romname>.<ext>
992 void make_rpath(char *buff, size_t size, const char *ext)
995 p = strrchr(gamepak_filename, PATH_SEPARATOR_CHAR);
997 p = gamepak_filename;
999 snprintf(buff, size, "%s/%s", main_path, p);
1000 p = strrchr(buff, '.');
1005 #define main_savestate_builder(type) \
1006 void main_##type##_savestate(file_tag_type savestate_file) \
1008 file_##type##_variable(savestate_file, cpu_ticks); \
1009 file_##type##_variable(savestate_file, execute_cycles); \
1010 file_##type##_variable(savestate_file, video_count); \
1011 file_##type##_array(savestate_file, timer); \
1014 main_savestate_builder(read);
1015 main_savestate_builder(write_mem);
1018 void printout(void *str, u32 val)
1023 void set_clock_speed()
1025 static u32 clock_speed_old = default_clock_speed;
1026 if (clock_speed != clock_speed_old)
1028 printf("about to set CPU clock to %iMHz\n", clock_speed);
1030 scePowerSetClockFrequency(clock_speed, clock_speed, clock_speed / 2);
1031 #elif defined(GP2X_BUILD)
1032 set_FCLK(clock_speed);
1034 clock_speed_old = clock_speed;