defaut dualshock to digital again, unless overriden
[pcsx_rearmed.git] / frontend / 3ds / 3ds_utils.h
index fe97985..75ab63b 100644 (file)
@@ -1,16 +1,78 @@
 #ifndef _3DS_UTILS_H
 #define _3DS_UTILS_H
 
-void ctr_invalidate_ICache(void);
-void ctr_flush_DCache(void);
+#include <stdio.h>
+#include <stdbool.h>
+#include <3ds.h>
 
-void ctr_flush_invalidate_cache(void);
+#ifdef OS_HEAP_AREA_BEGIN // defined in libctru 2.0+
+#define USE_CTRULIB_2 1
+#endif
 
-int ctr_svchack_init(void);
+#define MEMOP_PROT      6
+#define MEMOP_MAP       4
+#define MEMOP_UNMAP     5
 
-#include <stdio.h>
 #define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0)
 
 void wait_for_input(void);
 
+extern __attribute__((weak)) int  __ctr_svchax;
+
+static bool has_rosalina;
+
+static inline void check_rosalina() {
+  int64_t version;
+  uint32_t major;
+
+  has_rosalina = false;
+
+  if (!svcGetSystemInfo(&version, 0x10000, 0)) {
+     major = GET_VERSION_MAJOR(version);
+
+     if (major >= 8)
+       has_rosalina = true;
+  }
+}
+
+void ctr_clear_cache(void);
+
+typedef int32_t (*ctr_callback_type)(void);
+
+static inline void ctr_invalidate_ICache_kernel(void)
+{
+   __asm__ volatile(
+      "cpsid aif\n\t"
+      "mov r0, #0\n\t"
+      "mcr p15, 0, r0, c7, c5, 0\n\t");
+}
+
+static inline void ctr_flush_DCache_kernel(void)
+{
+   __asm__ volatile(
+      "cpsid aif\n\t"
+      "mov r0, #0\n\t"
+      "mcr p15, 0, r0, c7, c10, 0\n\t");
+}
+
+static inline void ctr_invalidate_ICache(void)
+{
+   svcBackdoor((ctr_callback_type)ctr_invalidate_ICache_kernel);
+}
+
+static inline void ctr_flush_DCache(void)
+{
+   svcBackdoor((ctr_callback_type)ctr_flush_DCache_kernel);
+}
+
+static inline void ctr_flush_invalidate_cache(void)
+{
+   if (has_rosalina) {
+      ctr_clear_cache();
+   } else {
+      ctr_flush_DCache();
+      ctr_invalidate_ICache();
+   }
+}
+
 #endif // _3DS_UTILS_H