Add copyright message to gles_video
[gpsp.git] / main.c
diff --git a/main.c b/main.c
index 3bdc850..c084181 100644 (file)
--- a/main.c
+++ b/main.c
@@ -38,23 +38,21 @@ debug_state current_debug_state = RUN;
 
 //u32 breakpoint_value = 0;
 
+#ifdef RPI_BUILD
+frameskip_type current_frameskip_type = manual_frameskip; //manual; //auto_frameskip;
+u32 global_cycles_per_instruction = 1;
+#else
 frameskip_type current_frameskip_type = auto_frameskip;
 u32 global_cycles_per_instruction = 1;
+#endif
+
 u32 random_skip = 0;
+u32 fps_debug = 0;
 
-#ifdef GP2X_BUILD
 u32 frameskip_value = 2;
 
-u64 frame_count_initial_timestamp = 0;
 u64 last_frame_interval_timestamp;
-u32 gp2x_fps_debug = 0;
 
-void gp2x_init(void);
-void gp2x_quit(void);
-#else
-
-u32 frameskip_value = 4;
-#endif
 u32 skip_next_frame = 0;
 
 u32 frameskip_counter = 0;
@@ -90,7 +88,7 @@ u32 clock_speed = 200;
 #else
 u32 clock_speed = 333;
 #endif
-u8 main_path[512];
+char main_path[512];
 
 void trigger_ext_event();
 
@@ -139,10 +137,10 @@ void trigger_ext_event();
     }                                                                         \
   }                                                                           \
 
-u8 *file_ext[] = { ".gba", ".bin", ".zip", NULL };
+static const char *file_ext[] = { ".gba", ".bin", ".zip", NULL };
 
-#ifdef ARM_ARCH
-void ChangeWorkingDirectory(char *exe)
+#ifndef PSP_BUILD
+static void ChangeWorkingDirectory(char *exe)
 {
 #ifndef _WIN32_WCE
   char *s = strrchr(exe, '/');
@@ -153,6 +151,48 @@ void ChangeWorkingDirectory(char *exe)
   }
 #endif
 }
+
+static void switch_to_romdir(void)
+{
+  char buff[256];
+  int r;
+  
+  file_open(romdir_file, "romdir.txt", read);
+
+  if(file_check_valid(romdir_file))
+  {
+    r = file_read(romdir_file, buff, sizeof(buff) - 1);
+    if (r > 0)
+    {
+      buff[r] = 0;
+      while (r > 0 && isspace(buff[r-1]))
+        buff[--r] = 0;
+      chdir(buff);
+    }
+    file_close(romdir_file);
+  }
+}
+
+static void save_romdir(void)
+{
+  char buff[512];
+
+  snprintf(buff, sizeof(buff), "%s" PATH_SEPARATOR "romdir.txt", main_path);
+  file_open(romdir_file, buff, write);
+
+  if(file_check_valid(romdir_file))
+  {
+    if (getcwd(buff, sizeof(buff)))
+    {
+      file_write(romdir_file, buff, strlen(buff));
+    }
+    file_close(romdir_file);
+  }
+}
+#else
+void ChangeWorkingDirectory(char *exe) {}
+static void switch_to_romdir(void) {}
+static void save_romdir(void) {}
 #endif
 
 void init_main()
@@ -186,58 +226,47 @@ void init_main()
 
 int main(int argc, char *argv[])
 {
-  u32 i;
-  u32 vcount = 0;
-  u32 ticks;
-  u32 dispstat;
-  u8 load_filename[512];
-  u8 bios_filename[512];
+  char bios_filename[512];
+  int ret;
 
 #ifdef PSP_BUILD
   sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0,
    vblank_interrupt_handler, NULL);
   sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
-#elif !defined(GP2X_BUILD)
-  freopen("CON", "wb", stdout);
 #endif
 
-  extern char *cpu_mode_names[];
-
   init_gamepak_buffer();
 
   // Copy the directory path of the executable into main_path
 
-#ifdef ARM_ARCH
   // ChangeWorkingDirectory will null out the filename out of the path
   ChangeWorkingDirectory(argv[0]);
-#endif
 
   getcwd(main_path, 512);
-  load_config_file();
-
-  gamepak_filename[0] = 0;
 
 #ifdef PSP_BUILD
   delay_us(2500000);
 #endif
 
-#ifdef GP2X_BUILD
-  // Overclocking GP2X and MMU patch goes here
-  gp2x_init();
+#ifndef PC_BUILD
+  gpsp_plat_init();
 #endif
+  load_config_file();
+
+  gamepak_filename[0] = 0;
 
   init_video();
 
-#ifdef GP2X_BUILD
-  sprintf(bios_filename, "%s/%s", main_path, "gba_bios.bin");
-  if(load_bios(bios_filename) == -1)
-#else
-  if(load_bios("gba_bios.bin") == -1)
-#endif
+  sprintf(bios_filename, "%s" PATH_SEPARATOR "%s", main_path, "gba_bios.bin");
+  ret = load_bios(bios_filename);
+  if (ret != 0)
+    ret = load_bios("gba_bios.bin");
+  if (ret != 0)
   {
     gui_action_type gui_action = CURSOR_NONE;
 
     debug_screen_start();
+    debug_screen_printl("                                                  ");
     debug_screen_printl("Sorry, but gpSP requires a Gameboy Advance BIOS   ");
     debug_screen_printl("image to run correctly. Make sure to get an       ");
     debug_screen_printl("authentic one, it'll be exactly 16384 bytes large ");
@@ -246,7 +275,11 @@ int main(int argc, char *argv[])
     debug_screen_printl("a860e8c0b6d573d191e4ec7db1b1e4f6                  ");
     debug_screen_printl("                                                  ");
     debug_screen_printl("When you do get it name it gba_bios.bin and put it");
+#ifdef PND_BUILD
+    debug_screen_printl("in <SD card>/pandora/appdata/gpsp/ .              ");
+#else
     debug_screen_printl("in the same directory as gpSP.                    ");
+#endif
     debug_screen_printl("                                                  ");
     debug_screen_printl("Press any button to exit.                         ");
 
@@ -288,7 +321,7 @@ int main(int argc, char *argv[])
   }
 
   init_main();
-  init_sound();
+  init_sound(1);
 
   init_input();
 
@@ -298,8 +331,8 @@ int main(int argc, char *argv[])
   {
     if(load_gamepak(argv[1]) == -1)
     {
-#ifdef PC_BUILD
-      printf("Failed to load gamepak %s, exiting.\n", load_filename);
+#ifndef PSP_BUILD
+      printf("Failed to load gamepak %s, exiting.\n", argv[1]);
 #endif
       exit(-1);
     }
@@ -312,6 +345,8 @@ int main(int argc, char *argv[])
   }
   else
   {
+    char load_filename[512];
+    switch_to_romdir();
     if(load_file(file_ext, load_filename) == -1)
     {
       menu(copy_screen());
@@ -320,12 +355,13 @@ int main(int argc, char *argv[])
     {
       if(load_gamepak(load_filename) == -1)
       {
-#ifdef PC_BUILD
+#ifndef PSP_BUILD
         printf("Failed to load gamepak %s, exiting.\n", load_filename);
 #endif
         exit(-1);
       }
 
+      set_clock_speed();
       set_gba_resolution(screen_scale);
       video_resolution_small();
 
@@ -342,10 +378,6 @@ int main(int argc, char *argv[])
   execute_arm_translate(execute_cycles);
 #else
 
-#ifdef GP2X_BUILD
-  get_ticks_us(&frame_count_initial_timestamp);
-#endif
-
 /*  u8 current_savestate_filename[512];
   get_savestate_filename_noshot(savestate_slot,
    current_savestate_filename);
@@ -402,7 +434,7 @@ void trigger_ext_event()
   static u32 event_number = 0;
   static u64 benchmark_ticks[16];
   u64 new_ticks;
-  u8 current_savestate_filename[512];
+  char current_savestate_filename[512];
 
   return;
 
@@ -494,6 +526,9 @@ void trigger_ext_event()
   event_number++;
 }
 
+static u32 fps = 60;
+static u32 frames_drawn = 60;
+
 u32 update_gba()
 {
   irq_type irq_raised = IRQ_NONE;
@@ -593,10 +628,10 @@ u32 update_gba()
           frame_ticks++;
 
   #ifdef PC_BUILD
-        printf("frame update (%x), %d instructions total, %d RAM flushes\n",
+/*        printf("frame update (%x), %d instructions total, %d RAM flushes\n",
            reg[REG_PC], instruction_count - last_frame, flush_ram_count);
           last_frame = instruction_count;
-
+*/
 /*          printf("%d gbc audio updates\n", gbc_update_count);
           printf("%d oam updates\n", oam_update_count); */
           gbc_update_count = 0;
@@ -608,10 +643,20 @@ u32 update_gba()
             continue;
 
           update_gbc_sound(cpu_ticks);
-          synchronize();
+
+          if(fps_debug)
+          {
+            char print_buffer[32];
+            sprintf(print_buffer, "%2d (%2d)", fps, frames_drawn);
+            print_string(print_buffer, 0xFFFF, 0x000, 0, 0);
+          }
+          if(!synchronize_flag)
+            print_string("-FF-", 0xFFFF, 0x000, 216, 0);
 
           update_screen();
 
+          synchronize();
+
           if(update_backup_flag)
             update_backup();
 
@@ -660,10 +705,6 @@ u32 update_gba()
   return execute_cycles;
 }
 
-u64 last_screen_timestamp = 0;
-u32 frame_speed = 15000;
-
-
 #ifdef PSP_BUILD
 
 u32 real_frame_count = 0;
@@ -683,7 +724,6 @@ void synchronize()
 
   if(!synchronize_flag)
   {
-    print_string("--FF--", 0xFFFF, 0x000, 0, 0);
     used_frameskip = 4;
     virtual_frame_count = real_frame_count - 1;
   }
@@ -748,9 +788,7 @@ void synchronize()
 */
 }
 
-#endif
-
-#ifdef GP2X_BUILD
+#else
 
 u32 real_frame_count = 0;
 u32 virtual_frame_count = 0;
@@ -758,34 +796,19 @@ u32 num_skipped_frames = 0;
 u32 interval_skipped_frames;
 u32 frames;
 
-u32 skipped_frames = 0;
-u32 ticks_needed_total = 0;
 const u32 frame_interval = 60;
 
 void synchronize()
 {
   u64 new_ticks;
   u64 time_delta;
-  static u32 fps = 60;
-  static u32 frames_drawn = 60;
-
-  if(gp2x_fps_debug)
-  {
-    char print_buffer[128];
-    sprintf(print_buffer, "%d (%d)", fps, frames_drawn);
-    print_string(print_buffer, 0xFFFF, 0x000, 0, 0);
-  }
 
   get_ticks_us(&new_ticks);
-  time_delta = new_ticks - last_screen_timestamp;
-  last_screen_timestamp = new_ticks;
-  ticks_needed_total += time_delta;
 
   skip_next_frame = 0;
   virtual_frame_count++;
 
-  real_frame_count = ((new_ticks -
-    frame_count_initial_timestamp) * 3) / 50000;
+  real_frame_count = (new_ticks * 3) / 50000;
 
   if(real_frame_count >= virtual_frame_count)
   {
@@ -802,13 +825,13 @@ void synchronize()
       num_skipped_frames = 0;
     }
   }
-  else
+  else if (synchronize_flag)
   {
-    if((synchronize_flag) &&
-     ((time_delta < frame_speed) && synchronize_flag))
-    {
-      delay_us(frame_speed - time_delta);
-    }
+#if defined(PND_BUILD) || defined(RPI_BUILD)
+    fb_wait_vsync();
+#elif !defined(GP2X_BUILD) // sleeping on GP2X is a bad idea
+    delay_us((u64)virtual_frame_count * 50000 / 3 - new_ticks + 2);
+#endif
   }
 
   frames++;
@@ -829,7 +852,6 @@ void synchronize()
 
     last_frame_interval_timestamp = new_ticks;
     interval_skipped_frames = 0;
-    ticks_needed_total = 0;
     frames = 0;
   }
 
@@ -851,82 +873,19 @@ void synchronize()
 
   interval_skipped_frames += skip_next_frame;
 
-  if(!synchronize_flag)
-    print_string("--FF--", 0xFFFF, 0x000, 0, 0);
-}
-
-#endif
-
-
-#ifdef PC_BUILD
-
-u32 ticks_needed_total = 0;
-float us_needed = 0.0;
-u32 frames = 0;
-const u32 frame_interval = 60;
-
-void synchronize()
-{
-  u64 new_ticks;
-  u64 time_delta;
+#if !defined(GP2X_BUILD) && !defined(PND_BUILD) && !defined(RPI_BUILD)
   char char_buffer[64];
-
-  get_ticks_us(&new_ticks);
-  time_delta = new_ticks - last_screen_timestamp;
-  last_screen_timestamp = new_ticks;
-  ticks_needed_total += time_delta;
-
-  skip_next_frame = 0;
-
-  if((time_delta < frame_speed) && synchronize_flag)
-  {
-    delay_us(frame_speed - time_delta);
-  }
-
-  frames++;
-
-  if(frames == frame_interval)
-  {
-    us_needed = (float)ticks_needed_total / frame_interval;
-    ticks_needed_total = 0;
-    frames = 0;
-  }
-
-  if(current_frameskip_type == manual_frameskip)
-  {
-    frameskip_counter = (frameskip_counter + 1) %
-     (frameskip_value + 1);
-    if(random_skip)
-    {
-      if(frameskip_counter != (rand() % (frameskip_value + 1)))
-        skip_next_frame = 1;
-    }
-    else
-    {
-      if(frameskip_counter)
-        skip_next_frame = 1;
-    }
-  }
-
-  if(synchronize_flag == 0)
-    print_string("--FF--", 0xFFFF, 0x000, 0, 0);
-
-  sprintf(char_buffer, "gpSP: %.1fms %.1ffps", us_needed / 1000.0,
-   1000000.0 / us_needed);
+  sprintf(char_buffer, "gpSP: %2d (%2d) fps", fps, frames_drawn);
   SDL_WM_SetCaption(char_buffer, "gpSP");
-
-/*
-    sprintf(char_buffer, "%02d %02d %06d %07d", frameskip, (u32)ms_needed,
-     ram_translation_ptr - ram_translation_cache, rom_translation_ptr -
-     rom_translation_cache);
-    print_string(char_buffer, 0xFFFF, 0x0000, 0, 0);
-*/
+#endif
 }
 
 #endif
 
 void quit()
 {
+  save_romdir();
+
   if(!update_backup_flag)
     update_backup_force();
 
@@ -941,8 +900,8 @@ void quit()
 #else
   SDL_Quit();
 
-#ifdef GP2X_BUILD
-  gp2x_quit();
+#ifndef PC_BUILD
+  gpsp_plat_quit();
 #endif
 
   exit(0);
@@ -959,7 +918,7 @@ void reset_gba()
 
 #ifdef PSP_BUILD
 
-u32 file_length(u8 *filename, s32 dummy)
+u32 file_length(char *filename, s32 dummy)
 {
   SceIoStat stats;
   sceIoGetstat(filename, &stats);
@@ -981,7 +940,7 @@ void get_ticks_us(u64 *tick_return)
 
 #else
 
-u32 file_length(u8 *dummy, FILE *fp)
+u32 file_length(char *dummy, FILE *fp)
 {
   u32 length;
 
@@ -1001,7 +960,7 @@ void delay_us(u32 us_count)
 
 void get_ticks_us(u64 *ticks_return)
 {
-  *ticks_return = (SDL_GetTicks() * 1000);
+  *ticks_return = (u64)SDL_GetTicks() * 1000;
 }
 
 #else
@@ -1025,9 +984,9 @@ void get_ticks_us(u64 *ticks_return)
 
 #endif
 
-void change_ext(u8 *src, u8 *buffer, u8 *extension)
+void change_ext(const char *src, char *buffer, const char *extension)
 {
-  u8 *dot_position;
+  char *dot_position;
   strcpy(buffer, src);
   dot_position = strrchr(buffer, '.');
 
@@ -1035,6 +994,20 @@ void change_ext(u8 *src, u8 *buffer, u8 *extension)
     strcpy(dot_position, extension);
 }
 
+// make path: <main_path>/<romname>.<ext>
+void make_rpath(char *buff, size_t size, const char *ext)
+{
+  char *p;
+  p = strrchr(gamepak_filename, PATH_SEPARATOR_CHAR);
+  if (p == NULL)
+    p = gamepak_filename;
+
+  snprintf(buff, size, "%s/%s", main_path, p);
+  p = strrchr(buff, '.');
+  if (p != NULL)
+    strcpy(p, ext);
+}
+
 #define main_savestate_builder(type)                                          \
 void main_##type##_savestate(file_tag_type savestate_file)                    \
 {                                                                             \
@@ -1052,3 +1025,19 @@ void printout(void *str, u32 val)
 {
   printf(str, val);
 }
+
+void set_clock_speed()
+{
+  static u32 clock_speed_old = default_clock_speed;
+  if (clock_speed != clock_speed_old)
+  {
+    printf("about to set CPU clock to %iMHz\n", clock_speed);
+  #ifdef PSP_BUILD
+    scePowerSetClockFrequency(clock_speed, clock_speed, clock_speed / 2);
+  #elif defined(GP2X_BUILD)
+    set_FCLK(clock_speed);
+  #endif
+    clock_speed_old = clock_speed;
+  }
+}
+