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 void ChangeWorkingDirectory(char *exe)
140 char *s = strrchr(exe, '/');
156 for(i = 0; i < 4; i++)
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;
165 timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH;
166 timer[1].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
171 execute_cycles = 960;
174 flush_translation_cache_rom();
175 flush_translation_cache_ram();
176 flush_translation_cache_bios();
179 int main(int argc, char *argv[])
182 sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0,
183 vblank_interrupt_handler, NULL);
184 sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
187 init_gamepak_buffer();
189 // Copy the directory path of the executable into main_path
192 // ChangeWorkingDirectory will null out the filename out of the path
193 ChangeWorkingDirectory(argv[0]);
196 getcwd(main_path, 512);
207 gamepak_filename[0] = 0;
212 char bios_filename[512];
213 sprintf(bios_filename, "%s/%s", main_path, "gba_bios.bin");
214 if(load_bios(bios_filename) == -1)
216 if(load_bios("gba_bios.bin") == -1)
219 gui_action_type gui_action = CURSOR_NONE;
221 debug_screen_start();
222 debug_screen_printl("Sorry, but gpSP requires a Gameboy Advance BIOS ");
223 debug_screen_printl("image to run correctly. Make sure to get an ");
224 debug_screen_printl("authentic one, it'll be exactly 16384 bytes large ");
225 debug_screen_printl("and should have the following md5sum value: ");
226 debug_screen_printl(" ");
227 debug_screen_printl("a860e8c0b6d573d191e4ec7db1b1e4f6 ");
228 debug_screen_printl(" ");
229 debug_screen_printl("When you do get it name it gba_bios.bin and put it");
231 debug_screen_printl("in <CD card>/pandora/appdata/gpsp/ . ");
233 debug_screen_printl("in the same directory as gpSP. ");
235 debug_screen_printl(" ");
236 debug_screen_printl("Press any button to exit. ");
238 debug_screen_update();
240 while(gui_action == CURSOR_NONE)
242 gui_action = get_gui_input();
251 if(bios_rom[0] != 0x18)
253 gui_action_type gui_action = CURSOR_NONE;
255 debug_screen_start();
256 debug_screen_printl("You have an incorrect BIOS image. ");
257 debug_screen_printl("While many games will work fine, some will not. It");
258 debug_screen_printl("is strongly recommended that you obtain the ");
259 debug_screen_printl("correct BIOS file. Do NOT report any bugs if you ");
260 debug_screen_printl("are seeing this message. ");
261 debug_screen_printl(" ");
262 debug_screen_printl("Press any button to resume, at your own risk. ");
264 debug_screen_update();
266 while(gui_action == CURSOR_NONE)
268 gui_action = get_gui_input();
280 video_resolution_large();
284 if(load_gamepak(argv[1]) == -1)
287 printf("Failed to load gamepak %s, exiting.\n", argv[1]);
292 set_gba_resolution(screen_scale);
293 video_resolution_small();
300 char load_filename[512];
301 if(load_file(file_ext, load_filename) == -1)
307 if(load_gamepak(load_filename) == -1)
310 printf("Failed to load gamepak %s, exiting.\n", load_filename);
316 set_gba_resolution(screen_scale);
317 video_resolution_small();
326 // We'll never actually return from here.
329 execute_arm_translate(execute_cycles);
332 /* u8 current_savestate_filename[512];
333 get_savestate_filename_noshot(savestate_slot,
334 current_savestate_filename);
335 load_state(current_savestate_filename); */
341 current_debug_state = COUNTDOWN_BREAKPOINT;
342 breakpoint_value = strtol(argv[2], NULL, 16);
347 execute_arm_translate(execute_cycles);
348 execute_arm(execute_cycles);
353 void print_memory_stats(u32 *counter, u32 *region_stats, char *stats_str)
355 u32 other_region_counter = region_stats[0x1] + region_stats[0xE] +
357 u32 rom_region_counter = region_stats[0x8] + region_stats[0x9] +
358 region_stats[0xA] + region_stats[0xB] + region_stats[0xC] +
360 u32 _counter = *counter;
362 printf("memory access stats: %s (out of %d)\n", stats_str, _counter);
363 printf("bios: %f%%\tiwram: %f%%\tewram: %f%%\tvram: %f\n",
364 region_stats[0x0] * 100.0 / _counter, region_stats[0x3] * 100.0 /
366 region_stats[0x2] * 100.0 / _counter, region_stats[0x6] * 100.0 /
369 printf("oam: %f%%\tpalette: %f%%\trom: %f%%\tother: %f%%\n",
370 region_stats[0x7] * 100.0 / _counter, region_stats[0x5] * 100.0 /
372 rom_region_counter * 100.0 / _counter, other_region_counter * 100.0 /
376 memset(region_stats, 0, sizeof(u32) * 16);
379 u32 event_cycles = 0;
380 const u32 event_cycles_trigger = 60 * 5;
383 void trigger_ext_event()
385 static u32 event_number = 0;
386 static u64 benchmark_ticks[16];
388 char current_savestate_filename[512];
394 get_ticks_us(&new_ticks);
395 benchmark_ticks[event_number - 1] =
396 new_ticks - benchmark_ticks[event_number - 1];
399 current_frameskip_type = no_frameskip;
401 synchronize_flag = 0;
403 get_savestate_filename_noshot(savestate_slot,
404 current_savestate_filename);
405 load_state(current_savestate_filename);
410 // Full benchmark, run normally
419 // No video benchmark
420 // Set frameskip really high + manual
421 current_frameskip_type = manual_frameskip;
422 frameskip_value = 1000000;
427 // Put CPU in halt mode, put it in IRQ mode with interrupts off
428 reg[CPU_HALT_STATE] = CPU_HALT;
429 reg[REG_CPSR] = 0xD2;
433 // No CPU or video benchmark
434 reg[CPU_HALT_STATE] = CPU_HALT;
435 reg[REG_CPSR] = 0xD2;
436 current_frameskip_type = manual_frameskip;
437 frameskip_value = 1000000;
443 char *print_strings[] =
456 benchmark_ticks[6] = benchmark_ticks[0] - benchmark_ticks[2];
457 benchmark_ticks[5] = benchmark_ticks[0] - benchmark_ticks[4] -
459 benchmark_ticks[7] = benchmark_ticks[0] - benchmark_ticks[1];
461 printf("Benchmark results (%d frames): \n", event_cycles_trigger);
462 for(i = 0; i < 8; i++)
464 printf(" %s: %d ms (%f ms per frame)\n",
465 print_strings[i], (u32)benchmark_ticks[i] / 1000,
466 (float)(benchmark_ticks[i] / (1000.0 * event_cycles_trigger)));
476 get_ticks_us(benchmark_ticks + event_number);
481 static u32 frames_drawn = 60;
485 irq_type irq_raised = IRQ_NONE;
489 cpu_ticks += execute_cycles;
491 reg[CHANGED_PC_STATUS] = 0;
496 update_gbc_sound(cpu_ticks);
497 gbc_sound_update = 0;
505 video_count -= execute_cycles;
509 u32 vcount = io_registers[REG_VCOUNT];
510 u32 dispstat = io_registers[REG_DISPSTAT];
512 if((dispstat & 0x02) == 0)
514 // Transition from hrefresh to hblank
515 video_count += (272);
518 if((dispstat & 0x01) == 0)
525 io_registers[REG_BLDCNT] = 0;
528 // If in visible area also fire HDMA
529 for(i = 0; i < 4; i++)
531 if(dma[i].start_type == DMA_START_HBLANK)
532 dma_transfer(dma + i);
537 irq_raised |= IRQ_HBLANK;
541 // Transition from hblank to next line
549 // Transition from vrefresh to vblank
555 irq_raised |= IRQ_VBLANK;
558 affine_reference_x[0] =
559 (s32)(address32(io_registers, 0x28) << 4) >> 4;
560 affine_reference_y[0] =
561 (s32)(address32(io_registers, 0x2C) << 4) >> 4;
562 affine_reference_x[1] =
563 (s32)(address32(io_registers, 0x38) << 4) >> 4;
564 affine_reference_y[1] =
565 (s32)(address32(io_registers, 0x3C) << 4) >> 4;
567 for(i = 0; i < 4; i++)
569 if(dma[i].start_type == DMA_START_VBLANK)
570 dma_transfer(dma + i);
577 // Transition from vblank to next screen
582 printf("frame update (%x), %d instructions total, %d RAM flushes\n",
583 reg[REG_PC], instruction_count - last_frame, flush_ram_count);
584 last_frame = instruction_count;
586 /* printf("%d gbc audio updates\n", gbc_update_count);
587 printf("%d oam updates\n", oam_update_count); */
588 gbc_update_count = 0;
589 oam_update_count = 0;
596 update_gbc_sound(cpu_ticks);
600 char print_buffer[32];
601 sprintf(print_buffer, "%2d (%2d)", fps, frames_drawn);
602 print_string(print_buffer, 0xFFFF, 0x000, 0, 0);
604 if(!synchronize_flag)
605 print_string("-FF-", 0xFFFF, 0x000, 216, 0);
611 if(update_backup_flag)
617 if(event_cycles == event_cycles_trigger)
626 if(vcount == (dispstat >> 8))
632 irq_raised |= IRQ_VCOUNT;
640 io_registers[REG_VCOUNT] = vcount;
642 io_registers[REG_DISPSTAT] = dispstat;
646 raise_interrupt(irq_raised);
648 execute_cycles = video_count;
654 } while(reg[CPU_HALT_STATE] != CPU_ACTIVE);
656 return execute_cycles;
661 u32 real_frame_count = 0;
662 u32 virtual_frame_count = 0;
663 u32 num_skipped_frames = 0;
665 void vblank_interrupt_handler(u32 sub, u32 *parg)
672 char char_buffer[64];
673 u64 new_ticks, time_delta;
674 s32 used_frameskip = frameskip_value;
676 if(!synchronize_flag)
679 virtual_frame_count = real_frame_count - 1;
684 virtual_frame_count++;
686 if(real_frame_count >= virtual_frame_count)
688 if((real_frame_count > virtual_frame_count) &&
689 (current_frameskip_type == auto_frameskip) &&
690 (num_skipped_frames < frameskip_value))
693 num_skipped_frames++;
697 virtual_frame_count = real_frame_count;
698 num_skipped_frames = 0;
701 // Here so that the home button return will eventually work.
702 // If it's not running fullspeed anyway this won't really hurt
710 sceDisplayWaitVblankStart();
713 if(current_frameskip_type == manual_frameskip)
715 frameskip_counter = (frameskip_counter + 1) %
716 (used_frameskip + 1);
719 if(frameskip_counter != (rand() % (used_frameskip + 1)))
724 if(frameskip_counter)
729 /* sprintf(char_buffer, "%08d %08d %d %d %d\n",
730 real_frame_count, virtual_frame_count, num_skipped_frames,
731 real_frame_count - virtual_frame_count, skip_next_frame);
732 print_string(char_buffer, 0xFFFF, 0x0000, 0, 10); */
735 sprintf(char_buffer, "%02d %02d %06d %07d", frameskip, (u32)ms_needed,
736 ram_translation_ptr - ram_translation_cache, rom_translation_ptr -
737 rom_translation_cache);
738 print_string(char_buffer, 0xFFFF, 0x0000, 0, 0);
744 u32 real_frame_count = 0;
745 u32 virtual_frame_count = 0;
746 u32 num_skipped_frames = 0;
747 u32 interval_skipped_frames;
750 const u32 frame_interval = 60;
757 get_ticks_us(&new_ticks);
760 virtual_frame_count++;
762 real_frame_count = (new_ticks * 3) / 50000;
764 if(real_frame_count >= virtual_frame_count)
766 if((real_frame_count > virtual_frame_count) &&
767 (current_frameskip_type == auto_frameskip) &&
768 (num_skipped_frames < frameskip_value))
771 num_skipped_frames++;
775 virtual_frame_count = real_frame_count;
776 num_skipped_frames = 0;
779 else if (synchronize_flag)
781 #if defined(PND_BUILD)
783 #elif !defined(GP2X_BUILD) // sleeping on GP2X is a bad idea
784 delay_us((u64)virtual_frame_count * 50000 / 3 - new_ticks + 2);
790 if(frames == frame_interval)
793 u32 new_frames_drawn;
795 time_delta = new_ticks - last_frame_interval_timestamp;
796 new_fps = (u64)((u64)1000000 * (u64)frame_interval) / time_delta;
798 (frame_interval - interval_skipped_frames) * (60 / frame_interval);
800 // Left open for rolling averages
802 frames_drawn = new_frames_drawn;
804 last_frame_interval_timestamp = new_ticks;
805 interval_skipped_frames = 0;
809 if(current_frameskip_type == manual_frameskip)
811 frameskip_counter = (frameskip_counter + 1) %
812 (frameskip_value + 1);
815 if(frameskip_counter != (rand() % (frameskip_value + 1)))
820 if(frameskip_counter)
825 interval_skipped_frames += skip_next_frame;
827 #if !defined(GP2X_BUILD) && !defined(PND_BUILD)
828 char char_buffer[64];
829 sprintf(char_buffer, "gpSP: %2d (%2d) fps", fps, frames_drawn);
830 SDL_WM_SetCaption(char_buffer, "gpSP");
838 if(!update_backup_flag)
839 update_backup_force();
843 #ifdef REGISTER_USAGE_ANALYZE
844 print_register_usage();
870 u32 file_length(char *filename, s32 dummy)
873 sceIoGetstat(filename, &stats);
874 return stats.st_size;
877 void delay_us(u32 us_count)
879 sceKernelDelayThread(us_count);
882 void get_ticks_us(u64 *tick_return)
885 sceRtcGetCurrentTick(&ticks);
887 *tick_return = (ticks * 1000000) / sceRtcGetTickResolution();
892 u32 file_length(char *dummy, FILE *fp)
896 fseek(fp, 0, SEEK_END);
898 fseek(fp, 0, SEEK_SET);
905 void delay_us(u32 us_count)
907 SDL_Delay(us_count / 1000);
910 void get_ticks_us(u64 *ticks_return)
912 *ticks_return = (u64)SDL_GetTicks() * 1000;
917 void delay_us(u32 us_count)
920 SDL_Delay(us_count / 1000);
923 void get_ticks_us(u64 *ticks_return)
925 struct timeval current_time;
926 gettimeofday(¤t_time, NULL);
929 (u64)current_time.tv_sec * 1000000 + current_time.tv_usec;
936 void change_ext(const char *src, char *buffer, const char *extension)
940 dot_position = strrchr(buffer, '.');
943 strcpy(dot_position, extension);
946 #define main_savestate_builder(type) \
947 void main_##type##_savestate(file_tag_type savestate_file) \
949 file_##type##_variable(savestate_file, cpu_ticks); \
950 file_##type##_variable(savestate_file, execute_cycles); \
951 file_##type##_variable(savestate_file, video_count); \
952 file_##type##_array(savestate_file, timer); \
955 main_savestate_builder(read);
956 main_savestate_builder(write_mem);
959 void printout(void *str, u32 val)
964 void set_clock_speed()
966 static u32 clock_speed_old = default_clock_speed;
967 if (clock_speed != clock_speed_old)
969 printf("about to set CPU clock to %iMHz\n", clock_speed);
971 scePowerSetClockFrequency(clock_speed, clock_speed, clock_speed / 2);
972 #elif defined(GP2X_BUILD)
973 set_FCLK(clock_speed);
975 clock_speed_old = clock_speed;