Merge branch 'libretro' into libretro-reset
[picodrive.git] / platform / libretro / libretro.c
index 332cf65..7896ffb 100644 (file)
@@ -53,6 +53,7 @@ int _newlib_vm_size_user = 1 << TARGET_SIZE_2;
 
 #include <pico/pico_int.h>
 #include <pico/state.h>
+#include <pico/patch.h>
 #include "../common/input_pico.h"
 #include "../common/version.h"
 #include "libretro.h"
@@ -97,6 +98,7 @@ void cache_flush_d_inval_i(void *start, void *end)
 {
 #ifdef __arm__
    size_t len = (char *)end - (char *)start;
+   (void)len;
 #if defined(__BLACKBERRY_QNX__)
    msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
 #elif defined(__MACH__)
@@ -434,14 +436,25 @@ void plat_munmap(void *ptr, size_t size)
 }
 #endif
 
+// if NULL is returned, static buffer is used
+void *plat_mem_get_for_drc(size_t size)
+{
+   void *mem = NULL;
+#ifdef VITA
+   sceKernelGetMemBlockBase(sceBlock, &mem);
+#endif
+   return mem;
+}
+
 int plat_mem_set_exec(void *ptr, size_t size)
 {
+   int ret = -1;
 #ifdef _WIN32
-   int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0);
+   ret = VirtualProtect(ptr, size, PAGE_EXECUTE_READWRITE, 0);
    if (ret == 0 && log_cb)
-      log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, 0);
+      log_cb(RETRO_LOG_ERROR, "VirtualProtect(%p, %d) failed: %d\n", ptr, (int)size,
+             GetLastError());
 #elif defined(_3DS)
-   int ret = -1;
    if (ctr_svchack_successful)
    {
       unsigned int currentHandle;
@@ -460,9 +473,9 @@ int plat_mem_set_exec(void *ptr, size_t size)
    }
 
 #elif defined(VITA)
-   int ret = sceKernelOpenVMDomain();
+   ret = sceKernelOpenVMDomain();
 #else
-   int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
+   ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
    if (ret != 0 && log_cb)
       log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
 #endif
@@ -471,6 +484,8 @@ int plat_mem_set_exec(void *ptr, size_t size)
 
 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
 {
+   struct retro_system_av_info av_info;
+
    memset(vout_buf, 0, 320 * 240 * 2);
    vout_width = is_32cols ? 256 : 320;
    PicoDrawSetOutBuf(vout_buf, vout_width * 2);
@@ -481,7 +496,6 @@ void emu_video_mode_change(int start_line, int line_count, int is_32cols)
    vout_offset = vout_width * start_line;
 
    // Update the geometry
-   struct retro_system_av_info av_info;
    retro_get_system_av_info(&av_info);
    environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info);
 }
@@ -511,7 +525,6 @@ void retro_set_environment(retro_environment_t cb)
       { "picodrive_sprlim",      "No sprite limit; disabled|enabled" },
       { "picodrive_ramcart",     "MegaCD RAM cart; disabled|enabled" },
       { "picodrive_region",      "Region; Auto|Japan NTSC|Japan PAL|US|Europe" },
-      { "picodrive_region_fps",  "Region FPS; Auto|NTSC|PAL" },
       { "picodrive_aspect",      "Core-provided aspect ratio; PAR|4/3|CRT" },
       { "picodrive_overscan",    "Show Overscan; disabled|enabled" },
 #ifdef DRC_SH2
@@ -554,6 +567,8 @@ void retro_get_system_info(struct retro_system_info *info)
 
 void retro_get_system_av_info(struct retro_system_av_info *info)
 {
+   float common_width;
+
    memset(info, 0, sizeof(*info));
    info->timing.fps            = Pico.m.pal ? 50 : 60;
    info->timing.sample_rate    = 44100;
@@ -562,7 +577,7 @@ void retro_get_system_av_info(struct retro_system_av_info *info)
    info->geometry.max_width    = vout_width;
    info->geometry.max_height   = vout_height;
 
-   float common_width = vout_width;
+   common_width = vout_width;
    if (user_vout_width != 0)
       common_width = user_vout_width;
 
@@ -691,13 +706,84 @@ bool retro_unserialize(const void *data, size_t size)
    return ret == 0;
 }
 
-/* cheats - TODO */
+typedef struct patch
+{
+       unsigned int addr;
+       unsigned short data;
+       unsigned char comp;
+} patch;
+
+extern void decode(char *buff, patch *dest);
+extern uint16_t m68k_read16(uint32_t a);
+extern void m68k_write16(uint32_t a, uint16_t d);
+
 void retro_cheat_reset(void)
 {
+       int i=0;
+       unsigned int addr;
+
+       for (i = 0; i < PicoPatchCount; i++)
+       {
+               addr = PicoPatches[i].addr;
+               if (addr < Pico.romsize) {
+                       if (PicoPatches[i].active)
+                               *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
+               } else {
+                       if (PicoPatches[i].active)
+                               m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
+               }
+       }
+
+       PicoPatchUnload();
 }
 
 void retro_cheat_set(unsigned index, bool enabled, const char *code)
 {
+       patch pt;
+       int array_len = PicoPatchCount;
+       char codeCopy[256];
+       char *buff;
+
+       if (code=='\0') return;
+       strcpy(codeCopy,code);
+       buff = strtok(codeCopy,"+");
+
+       while (buff != NULL)
+       {
+               decode(buff, &pt);
+               if (pt.addr == (uint32_t) -1 || pt.data == (uint16_t) -1)
+               {
+                       log_cb(RETRO_LOG_ERROR,"CHEATS: Invalid code: %s\n",buff);
+                       return;
+               }
+
+               /* code was good, add it */
+               if (array_len < PicoPatchCount + 1)
+               {
+                       void *ptr;
+                       array_len *= 2;
+                       array_len++;
+                       ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
+                       if (ptr == NULL) {
+                               log_cb(RETRO_LOG_ERROR,"CHEATS: Failed to allocate memory for: %s\n",buff);
+                               return;
+                       }
+                       PicoPatches = ptr;
+               }
+               strcpy(PicoPatches[PicoPatchCount].code, buff);
+
+               PicoPatches[PicoPatchCount].active = enabled;
+               PicoPatches[PicoPatchCount].addr = pt.addr;
+               PicoPatches[PicoPatchCount].data = pt.data;
+               PicoPatches[PicoPatchCount].comp = pt.comp;
+               if (PicoPatches[PicoPatchCount].addr < Pico.romsize)
+                       PicoPatches[PicoPatchCount].data_old = *(uint16_t *)(Pico.rom + PicoPatches[PicoPatchCount].addr);
+               else
+                       PicoPatches[PicoPatchCount].data_old = (uint16_t) m68k_read16(PicoPatches[PicoPatchCount].addr);
+               PicoPatchCount++;
+
+               buff = strtok(NULL,"+");
+       }
 }
 
 /* multidisk support */
@@ -887,22 +973,6 @@ static const char *find_bios(int *region, const char *cd_fname)
    return NULL;
 }
 
-static void sram_reset()
-{
-   SRam.data = NULL;
-   SRam.start = 0;
-   SRam.end = 0;
-   SRam.flags = '\0';
-   SRam.unused2 = '\0';
-   SRam.changed = '\0' ;
-   SRam.eeprom_type = '\0';
-   SRam.unused3 = '\0';
-   SRam.eeprom_bit_cl = '\0';
-   SRam.eeprom_bit_in = '\0';
-   SRam.eeprom_bit_out = '\0';
-   SRam.size = 0;
-}
-
 bool retro_load_game(const struct retro_game_info *info)
 {
    enum media_type_e media_type;
@@ -959,8 +1029,6 @@ bool retro_load_game(const struct retro_game_info *info)
       { 0 },
    };
 
-   sram_reset();
-
    enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
    if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
       if (log_cb)
@@ -1050,13 +1118,13 @@ void *retro_get_memory_data(unsigned type)
          if (PicoAHW & PAHW_MCD)
             data = Pico_mcd->bram;
          else
-            data = SRam.data;
+            data = Pico.sv.data;
          break;
       case RETRO_MEMORY_SYSTEM_RAM:
          if (PicoAHW & PAHW_SMS)
-            data = Pico.vramb;
+            data = PicoMem.zram;
          else
-            data = Pico.ram;
+            data = PicoMem.ram;
          break;
       default:
          data = NULL;
@@ -1079,20 +1147,20 @@ size_t retro_get_memory_size(unsigned type)
             return 0x2000;
 
          if (Pico.m.frame_count == 0)
-            return SRam.size;
+            return Pico.sv.size;
 
          // if game doesn't write to sram, don't report it to
          // libretro so that RA doesn't write out zeroed .srm
-         for (i = 0, sum = 0; i < SRam.size; i++)
-            sum |= SRam.data[i];
+         for (i = 0, sum = 0; i < Pico.sv.size; i++)
+            sum |= Pico.sv.data[i];
 
-         return (sum != 0) ? SRam.size : 0;
+         return (sum != 0) ? Pico.sv.size : 0;
 
       case RETRO_MEMORY_SYSTEM_RAM:
          if (PicoAHW & PAHW_SMS)
-            return sizeof(Pico.vramb);
+            return 0x2000;
          else
-            return sizeof(Pico.ram);
+            return sizeof(PicoMem.ram);
 
       default:
          return 0;
@@ -1143,6 +1211,8 @@ static enum input_device input_name_to_val(const char *name)
 static void update_variables(void)
 {
    struct retro_variable var;
+   int OldPicoRegionOverride;
+   float old_user_vout_width;
 
    var.value = NULL;
    var.key = "picodrive_input1";
@@ -1172,7 +1242,7 @@ static void update_variables(void)
          PicoOpt &= ~POPT_EN_MCD_RAMCART;
    }
 
-   int OldPicoRegionOverride = PicoRegionOverride;
+   OldPicoRegionOverride = PicoRegionOverride;
    var.value = NULL;
    var.key = "picodrive_region";
    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
@@ -1188,28 +1258,15 @@ static void update_variables(void)
          PicoRegionOverride = 8;
    }
 
-   int OldPicoRegionFPSOverride = PicoRegionFPSOverride;
-   var.value = NULL;
-   var.key = "picodrive_region_fps";
-   if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
-      if (strcmp(var.value, "Auto") == 0)
-         PicoRegionFPSOverride = 0;
-      else if (strcmp(var.value, "NTSC") == 0)
-         PicoRegionFPSOverride = 1;
-      else if (strcmp(var.value, "PAL") == 0)
-         PicoRegionFPSOverride = 2;
-   }
-
    // Update region, fps and sound flags if needed
-   if (PicoRegionOverride    != OldPicoRegionOverride ||
-       PicoRegionFPSOverride != OldPicoRegionFPSOverride)
+   if (Pico.rom && PicoRegionOverride != OldPicoRegionOverride)
    {
       PicoDetectRegion();
       PicoLoopPrepare();
       PsndRerate(1);
    }
 
-   float old_user_vout_width = user_vout_width;
+   old_user_vout_width = user_vout_width;
    var.value = NULL;
    var.key = "picodrive_aspect";
    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
@@ -1270,19 +1327,13 @@ void retro_run(void)
          if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
             PicoPad[pad] |= retro_pico_map[i];
 
+   PicoPatchApply();
    PicoFrame();
 
    video_cb((short *)vout_buf + vout_offset,
       vout_width, vout_height, vout_width * 2);
 }
 
-static void check_system_specs(void)
-{
-   /* TODO - set different performance level for 32X - 6 for ARM dynarec, higher for interpreter core */
-   unsigned level = 5;
-   environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
-}
-
 void retro_init(void)
 {
    struct retro_log_callback log;
@@ -1346,3 +1397,5 @@ void retro_deinit(void)
    vout_buf = NULL;
    PicoExit();
 }
+
+// vim:shiftwidth=3:ts=3:expandtab